Class: FFI::Type

Inherits:
Object
  • Object
show all
Defined in:
ext/ffi_c/Type.c,
ext/ffi_c/Type.c

Overview

This class manages C types.

It embbed Builtin objects as constants (for names, see NativeType).

Direct Known Subclasses

ArrayType, StructByValue, StructLayout, Builtin, Mapped

Defined Under Namespace

Classes: Builtin, Mapped

Constant Summary collapse

Struct =
rbffi_StructByValueClass

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ self

Parameters:

  • value (Fixnum, Type)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'ext/ffi_c/Type.c', line 113

static VALUE
type_initialize(VALUE self, VALUE value)
{
    Type* type;
    Type* other;

    TypedData_Get_Struct(self, Type, &rbffi_type_data_type, type);

    if (FIXNUM_P(value)) {
        type->nativeType = FIX2INT(value);
    } else if (rb_obj_is_kind_of(value, rbffi_TypeClass)) {
        TypedData_Get_Struct(value, Type, &rbffi_type_data_type, other);
        type->nativeType = other->nativeType;
        type->ffiType = other->ffiType;
    } else {
        rb_raise(rb_eArgError, "wrong type");
    }

    rb_obj_freeze(self);

    return self;
}

Instance Method Details

#alignmentFixnum

Get Type alignment.

Returns:

  • (Fixnum)


156
157
158
159
160
161
162
163
164
# File 'ext/ffi_c/Type.c', line 156

static VALUE
type_alignment(VALUE self)
{
    Type *type;

    TypedData_Get_Struct(self, Type, &rbffi_type_data_type, type);

    return INT2FIX(type->ffiType->alignment);
}

#inspectString

Inspect FFI::Type object.

Returns:

  • (String)


171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'ext/ffi_c/Type.c', line 171

static VALUE
type_inspect(VALUE self)
{
    char buf[100];
    Type *type;

    TypedData_Get_Struct(self, Type, &rbffi_type_data_type, type);

    snprintf(buf, sizeof(buf), "#<%s::%p size=%d alignment=%d>",
            rb_obj_classname(self), type, (int) type->ffiType->size, (int) type->ffiType->alignment);

    return rb_str_new2(buf);
}

#sizeFixnum

Return type’s size, in bytes.

Returns:

  • (Fixnum)


141
142
143
144
145
146
147
148
149
# File 'ext/ffi_c/Type.c', line 141

static VALUE
type_size(VALUE self)
{
    Type *type;

    TypedData_Get_Struct(self, Type, &rbffi_type_data_type, type);

    return INT2FIX(type->ffiType->size);
}