Class: Magick::Enum

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

Direct Known Subclasses

GeometryValue

Instance Method Summary collapse

Constructor Details

#initialize(sym, val) ⇒ Magick::Enum

Initialize a new Enum instance.

Parameters:

  • sym (Symbol)

    the symbol as enum name

  • val (Numeric)

    the value for enum



162
163
164
165
166
167
168
169
170
171
172
# File 'ext/RMagick/rmenum.cpp', line 162

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

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

    return self;
}

Instance Method Details

#<=>(other) ⇒ -1, ...

Support Comparable module in Enum.

Parameters:

  • other (Object)

    the other object

Returns:

  • (-1, 0, 1, nil)

    the result of compare



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'ext/RMagick/rmenum.cpp', line 196

VALUE
Enum_spaceship(VALUE self, VALUE other)
{
    MagickEnum *self_enum, *other_enum;

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

    TypedData_Get_Struct(self, MagickEnum, &rm_enum_data_type, self_enum);
    TypedData_Get_Struct(other, MagickEnum, &rm_enum_data_type, other_enum);

    if (self_enum->val > other_enum->val)
    {
        return INT2FIX(1);
    }
    else if (self_enum->val < other_enum->val)
    {
        return INT2FIX(-1);
    }

    return INT2FIX(0);
}

#===(other) ⇒ Boolean

“Case equal” operator for Enum.

Parameters:

  • other (Object)

    the other object

Returns:

  • (Boolean)

    true or false



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'ext/RMagick/rmenum.cpp', line 139

VALUE
Enum_case_eq(VALUE self, VALUE other)
{
    if (CLASS_OF(self) == CLASS_OF(other))
    {
        MagickEnum *self_enum, *other_enum;

        TypedData_Get_Struct(self, MagickEnum, &rm_enum_data_type, self_enum);
        TypedData_Get_Struct(other, MagickEnum, &rm_enum_data_type, other_enum);
        return self_enum->val == other_enum->val ? Qtrue : Qfalse;
    }

    return Qfalse;
}

#to_iNumeric

Return the value of an enum.

Returns:

  • (Numeric)

    this object’s value



180
181
182
183
184
185
186
187
# File 'ext/RMagick/rmenum.cpp', line 180

VALUE
Enum_to_i(VALUE self)
{
    MagickEnum *magick_enum;

    TypedData_Get_Struct(self, MagickEnum, &rm_enum_data_type, magick_enum);
    return INT2NUM(magick_enum->val);
}

#to_sString

Return the name of an enum.

Returns:

  • (String)

    the name of an enum



255
256
257
258
259
# File 'ext/RMagick/rmenum.cpp', line 255

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

#|(other) ⇒ Magick::Enum

Bitwise OR for enums

Parameters:

Returns:



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'ext/RMagick/rmenum.cpp', line 226

VALUE
Enum_bitwise_or(VALUE self, VALUE other)
{
    VALUE new_enum, klass;
    MagickEnum *self_enum, *other_enum, *new_enum_data;

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

    new_enum = Enum_alloc(klass);

    TypedData_Get_Struct(self, MagickEnum, &rm_enum_data_type, self_enum);
    TypedData_Get_Struct(other, MagickEnum, &rm_enum_data_type, other_enum);
    TypedData_Get_Struct(new_enum, MagickEnum, &rm_enum_data_type, new_enum_data);

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

    return new_enum;
}