Class: Magick::Enum

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
ext/RMagick/rmmain.c

Direct Known Subclasses

GeometryValue

Instance Method Summary collapse

Constructor Details

#initialize(sym, val) ⇒ Object

Initialize a new Enum instance.

Ruby usage:

- @verbatim Enum#initialize(sym,val) @endverbatim

Parameters:

  • self

    this object

  • sym

    the symbol

  • val

    the value for the symbol



148
149
150
151
152
153
154
155
156
157
158
# File 'ext/RMagick/rmenum.c', line 148

VALUE
Enum_initialize(VALUE self, VALUE sym, VALUE val)
{
   MagickEnum *magick_enum;

   Data_Get_Struct(self, MagickEnum, magick_enum);
   magick_enum->id = rb_to_id(sym); /* convert symbol to ID */
   magick_enum->val = NUM2INT(val);

   return self;
}

Instance Method Details

#<=>(other) ⇒ Object

Support Comparable module in Enum.

Ruby usage:

- @verbatim Enum#<=> @endverbatim

Notes:

- Enums must be instances of the same class to be equal.

Parameters:

  • self

    this object

  • other

    the other object

Returns:

  • -1, 0, 1, or nil



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'ext/RMagick/rmenum.c', line 193

VALUE
Enum_spaceship(VALUE self, VALUE other)
{
    MagickEnum *this, *that;

    if(CLASS_OF(self) != CLASS_OF(other)) {
        return Qnil;
    }

    Data_Get_Struct(self, MagickEnum, this);
    Data_Get_Struct(other, MagickEnum, that);

    if (this->val > that->val)
    {
        return INT2FIX(1);
    }
    else if (this->val < that->val)
    {
        return INT2FIX(-1);
    }

    return INT2FIX(0);
}

#===(other) ⇒ Object

“Case equal” operator for Enum.

Ruby usage:

- @verbatim Enum#=== @endverbatim

Notes:

- Yes, I know "case equal" is a misnomer.

Parameters:

  • self

    this object

  • other

    the other object

Returns:

  • true or false



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'ext/RMagick/rmenum.c', line 121

VALUE
Enum_case_eq(VALUE self, VALUE other)
{
    MagickEnum *this, *that;

    if (CLASS_OF(self) == CLASS_OF(other))
    {
        Data_Get_Struct(self, MagickEnum, this);
        Data_Get_Struct(other, MagickEnum, that);
        return this->val == that->val ? Qtrue : Qfalse;
    }

    return Qfalse;
}

#to_iObject

Return the value of an enum.

Ruby usage:

- @verbatim Enum#to_i @endverbatim

Parameters:

  • self

    this object

Returns:

  • this object’s value



170
171
172
173
174
175
176
177
# File 'ext/RMagick/rmenum.c', line 170

VALUE
Enum_to_i(VALUE self)
{
   MagickEnum *magick_enum;

   Data_Get_Struct(self, MagickEnum, magick_enum);
   return INT2NUM(magick_enum->val);
}

#to_sObject

Return the name of an enum.

Ruby usage:

- @verbatim Enum#to_s @endverbatim

Parameters:

  • self

    this object

Returns:

  • the name



263
264
265
266
267
# File 'ext/RMagick/rmenum.c', line 263

VALUE
Enum_to_s(VALUE self)
{
   return rb_str_new2(rm_enum_to_cstr(self));
}

#|(another) ⇒ Object

Bitwise OR for enums

Ruby usage:

- @verbatim Enum1 | Enum2 @endverbatim

Notes:

- Enums must be instances of the same class.

Parameters:

  • Enum1

    this object

  • Enum2

    another enum

Returns:

  • new Enum instance



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/RMagick/rmenum.c', line 230

VALUE
Enum_bitwise_or(VALUE self, VALUE another)
{
  VALUE new_enum, cls;
  MagickEnum *this, *that, *new_enum_data;

  cls = CLASS_OF(self);
  if (CLASS_OF(another) != cls)
  {
    rb_raise(rb_eArgError, "Expected class %s but got %s", rb_class2name(cls), rb_class2name(CLASS_OF(another)));
  }

  new_enum = Enum_alloc(cls);

  Data_Get_Struct(self, MagickEnum, this);
  Data_Get_Struct(another, MagickEnum, that);
  Data_Get_Struct(new_enum, MagickEnum, new_enum_data);

  new_enum_data->id = rb_to_id(rb_sprintf("%s|%s", rb_id2name(this->id), rb_id2name(that->id)));
  new_enum_data->val = this->val | that->val;

  return new_enum;
}