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.

Parameters:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'ext/ffi_c/StructLayout.c', line 140

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

    TypedData_Get_Struct(self, StructField, &rbffi_struct_field_data_type, 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);
    RB_OBJ_WRITE(self, &field->rbName, (TYPE(rbName) == T_SYMBOL) ? rbName : rb_str_intern(rbName));
    RB_OBJ_WRITE(self, &field->rbType, rbType);
    TypedData_Get_Struct(field->rbType, Type, &rbffi_type_data_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_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;
    }

    rb_obj_freeze(self);

    return self;
}

Instance Method Details

#alignmentNumeric

Get the field alignment.

Returns:

  • (Numeric)


220
221
222
223
224
225
226
# File 'ext/ffi_c/StructLayout.c', line 220

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

#get(pointer) ⇒ Object

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

Parameters:

Returns:

  • (Object)


261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'ext/ffi_c/StructLayout.c', line 261

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

    TypedData_Get_Struct(self, StructField, &rbffi_struct_field_data_type, 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.

Returns:

  • (Symbol)


247
248
249
250
251
252
253
# File 'ext/ffi_c/StructLayout.c', line 247

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

#offsetNumeric

Get the field offset.

Returns:

  • (Numeric)


194
195
196
197
198
199
200
# File 'ext/ffi_c/StructLayout.c', line 194

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

#put(pointer, value) ⇒ self

Put an object to memory pointed by pointer.

Parameters:

Returns:

  • (self)


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'ext/ffi_c/StructLayout.c', line 282

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

    TypedData_Get_Struct(self, StructField, &rbffi_struct_field_data_type, 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.

Returns:

  • (Numeric)


207
208
209
210
211
212
213
# File 'ext/ffi_c/StructLayout.c', line 207

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

#typeType

Get the field type.

Returns:



233
234
235
236
237
238
239
240
# File 'ext/ffi_c/StructLayout.c', line 233

static VALUE
struct_field_type(VALUE self)
{
    StructField* field;
    TypedData_Get_Struct(self, StructField, &rbffi_struct_field_data_type, field);

    return field->rbType;
}