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

Struct =
rbffi_StructByValueClass

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ self



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'ext/ffi_c/Type.c', line 75

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

    Data_Get_Struct(self, Type, type);

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

Instance Method Details

#alignmentFixnum

Get Type alignment.



116
117
118
119
120
121
122
123
124
# File 'ext/ffi_c/Type.c', line 116

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

    Data_Get_Struct(self, Type, type);

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

#inspectString

Inspect FFI::Type object.



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'ext/ffi_c/Type.c', line 131

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

    Data_Get_Struct(self, 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.



101
102
103
104
105
106
107
108
109
# File 'ext/ffi_c/Type.c', line 101

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

    Data_Get_Struct(self, Type, type);

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