Class: FFI::StructLayout::Field

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

Overview

A field in a FFI::StructLayout.

Direct Known Subclasses

Array, Enum, Function, InnerStruct, Mapped, Number, Pointer, String

Instance Method Summary collapse

Constructor Details

#initialize(name, offset, type) ⇒ self

A new FFI::StructLayout::Field instance.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'ext/ffi_c/StructLayout.c', line 97

static VALUE
struct_field_initialize(int argc, VALUE* argv, VALUE self)
{
    VALUE rbOffset = Qnil, rbName = Qnil, rbType = Qnil;
    StructField* field;
    int nargs;

    Data_Get_Struct(self, StructField, field);

    nargs = rb_scan_args(argc, argv, "3", &rbName, &rbOffset, &rbType);

    if (TYPE(rbName) != T_SYMBOL && TYPE(rbName) != T_STRING) {
        rb_raise(rb_eTypeError, "wrong argument type %s (expected Symbol/String)",
                rb_obj_classname(rbName));
    }

    Check_Type(rbOffset, T_FIXNUM);

    if (!rb_obj_is_kind_of(rbType, rbffi_TypeClass)) {
        rb_raise(rb_eTypeError, "wrong argument type %s (expected FFI::Type)",
                rb_obj_classname(rbType));
    }

    field->offset = NUM2UINT(rbOffset);
    field->rbName = (TYPE(rbName) == T_SYMBOL) ? rbName : rb_str_intern(rbName);
    field->rbType = rbType;
    Data_Get_Struct(field->rbType, Type, field->type);
    field->memoryOp = get_memory_op(field->type);
    field->referenceIndex = -1;

    switch (field->type->nativeType == NATIVE_MAPPED ? ((MappedType *) field->type)->type->nativeType : field->type->nativeType) {
        case NATIVE_FUNCTION:
        case NATIVE_CALLBACK:
        case NATIVE_POINTER:
            field->referenceRequired = true;
            break;

        default:
            field->referenceRequired = (rb_respond_to(self, rb_intern("reference_required?"))
                    && RTEST(rb_funcall2(self, rb_intern("reference_required?"), 0, NULL)))
                    || (rb_respond_to(rbType, rb_intern("reference_required?"))
                        && RTEST(rb_funcall2(rbType, rb_intern("reference_required?"), 0, NULL)));
            break;
    }
    
    return self;
}

Instance Method Details

#alignmentNumeric

Get the field alignment.



176
177
178
179
180
181
182
# File 'ext/ffi_c/StructLayout.c', line 176

static VALUE
struct_field_alignment(VALUE self)
{
    StructField* field;
    Data_Get_Struct(self, StructField, field);
    return UINT2NUM(field->type->ffiType->alignment);
}

#get(pointer) ⇒ Object

Get an object of type #type from memory pointed by pointer.



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'ext/ffi_c/StructLayout.c', line 217

static VALUE
struct_field_get(VALUE self, VALUE pointer)
{
    StructField* f;

    Data_Get_Struct(self, StructField, f);
    if (f->memoryOp == NULL) {
        rb_raise(rb_eArgError, "get not supported for %s", rb_obj_classname(f->rbType));
        return Qnil;
    }

    return (*f->memoryOp->get)(MEMORY(pointer), f->offset);
}

#nameSymbol

Get the field name.



203
204
205
206
207
208
209
# File 'ext/ffi_c/StructLayout.c', line 203

static VALUE
struct_field_name(VALUE self)
{
    StructField* field;
    Data_Get_Struct(self, StructField, field);
    return field->rbName;
}

#offsetNumeric

Get the field offset.



150
151
152
153
154
155
156
# File 'ext/ffi_c/StructLayout.c', line 150

static VALUE
struct_field_offset(VALUE self)
{
    StructField* field;
    Data_Get_Struct(self, StructField, field);
    return UINT2NUM(field->offset);
}

#put(pointer, value) ⇒ self

Put an object to memory pointed by pointer.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/ffi_c/StructLayout.c', line 238

static VALUE
struct_field_put(VALUE self, VALUE pointer, VALUE value)
{
    StructField* f;
    
    Data_Get_Struct(self, StructField, f);
    if (f->memoryOp == NULL) {
        rb_raise(rb_eArgError, "put not supported for %s", rb_obj_classname(f->rbType));
        return self;
    }

    (*f->memoryOp->put)(MEMORY(pointer), f->offset, value);

    return self;
}

#sizeNumeric

Get the field size.



163
164
165
166
167
168
169
# File 'ext/ffi_c/StructLayout.c', line 163

static VALUE
struct_field_size(VALUE self)
{
    StructField* field;
    Data_Get_Struct(self, StructField, field);
    return UINT2NUM(field->type->ffiType->size);
}

#typeType

Get the field type.



189
190
191
192
193
194
195
196
# File 'ext/ffi_c/StructLayout.c', line 189

static VALUE
struct_field_type(VALUE self)
{
    StructField* field;
    Data_Get_Struct(self, StructField, field);

    return field->rbType;
}