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



134
135
136
137
138
139
140
141
142
143
144
# File 'ext/RMagick/rmenum.c', line 134

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



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'ext/RMagick/rmenum.c', line 179

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'ext/RMagick/rmenum.c', line 107

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



156
157
158
159
160
161
162
163
# File 'ext/RMagick/rmenum.c', line 156

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



249
250
251
252
253
254
255
256
# File 'ext/RMagick/rmenum.c', line 249

VALUE
Enum_to_s(VALUE self)
{
   MagickEnum *magick_enum;

   Data_Get_Struct(self, MagickEnum, magick_enum);
   return rb_str_new2(rb_id2name(magick_enum->id));
}

#|(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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'ext/RMagick/rmenum.c', line 216

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;
}