Class: FFI::StructLayout

Inherits:
Type
  • Object
show all
Defined in:
lib/ffi/struct.rb,
ext/ffi_c/StructLayout.c

Defined Under Namespace

Classes: Array, CharArray, Enum, Field, Function, InnerStruct, Mapped, Number, Pointer, String

Constant Summary

Constants inherited from Type

Type::Array, Type::Function, Type::Struct

Instance Method Summary collapse

Constructor Details

#initialize(fields, size, align) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'ext/ffi_c/StructLayout.c', line 343

static VALUE
struct_layout_initialize(VALUE self, VALUE fields, VALUE size, VALUE align)
{
    StructLayout* layout;
    ffi_type* ltype;
    int i;

    Data_Get_Struct(self, StructLayout, layout);
    layout->fieldCount = RARRAY_LEN(fields);
    layout->rbFieldMap = rb_hash_new();
    layout->rbFieldNames = rb_ary_new2(layout->fieldCount);
    layout->size = NUM2INT(size);
    layout->align = NUM2INT(align);
    layout->fields = xcalloc(layout->fieldCount, sizeof(StructField *));
    layout->ffiTypes = xcalloc(layout->fieldCount + 1, sizeof(ffi_type *));
    layout->rbFields = rb_ary_new2(layout->fieldCount);
    layout->referenceFieldCount = 0;
    layout->base.ffiType->elements = layout->ffiTypes;
    layout->base.ffiType->size = layout->size;
    layout->base.ffiType->alignment = layout->align;

    ltype = layout->base.ffiType;
    for (i = 0; i < (int) layout->fieldCount; ++i) {
        VALUE rbField = rb_ary_entry(fields, i);
        VALUE rbName;
        StructField* field;
        ffi_type* ftype;


        if (!rb_obj_is_kind_of(rbField, rbffi_StructLayoutFieldClass)) {
            rb_raise(rb_eTypeError, "wrong type for field %d.", i);
        }
        rbName = rb_funcall2(rbField, rb_intern("name"), 0, NULL);

        field = layout->fields[i];
        Data_Get_Struct(rbField, StructField, field);

        if (field->type == NULL || field->type->ffiType == NULL) {
            rb_raise(rb_eRuntimeError, "type of field %d not supported", i);
        }

        ftype = field->type->ffiType;
        if (ftype->size == 0) {
            rb_raise(rb_eTypeError, "type of field %d has zero size", i);
        }

        if (field->referenceRequired) {
            field->referenceIndex = layout->referenceFieldCount++;
        }

        layout->ffiTypes[i] = ftype;
        st_insert(layout->fieldSymbolTable, rbName, rbField);
        rb_hash_aset(layout->rbFieldMap, rbName, rbField);
        rb_ary_push(layout->rbFields, rbField);
        rb_ary_push(layout->rbFieldNames, rbName);
    }

    if (ltype->size == 0) {
        rb_raise(rb_eRuntimeError, "Struct size is zero");
    }

    return self;
}

Instance Method Details

#[](field) ⇒ Object



407
408
409
410
411
412
413
414
415
# File 'ext/ffi_c/StructLayout.c', line 407

static VALUE
struct_layout_aref(VALUE self, VALUE field)
{
    StructLayout* layout;

    Data_Get_Struct(self, StructLayout, layout);

    return rb_hash_aref(layout->rbFieldMap, field);
}

#fieldsObject



417
418
419
420
421
422
423
424
425
# File 'ext/ffi_c/StructLayout.c', line 417

static VALUE
struct_layout_fields(VALUE self)
{
    StructLayout* layout;

    Data_Get_Struct(self, StructLayout, layout);

    return rb_ary_dup(layout->rbFields);
}

#membersObject



427
428
429
430
431
432
433
434
435
# File 'ext/ffi_c/StructLayout.c', line 427

static VALUE
struct_layout_members(VALUE self)
{
    StructLayout* layout;

    Data_Get_Struct(self, StructLayout, layout);

    return rb_ary_dup(layout->rbFieldNames);
}

#offset_of(field_name) ⇒ Object



34
35
36
# File 'lib/ffi/struct.rb', line 34

def offset_of(field_name)
  self[field_name].offset
end

#offsetsObject



30
31
32
# File 'lib/ffi/struct.rb', line 30

def offsets
  members.map { |m| [ m, self[m].offset ] }
end

#to_aObject



437
438
439
440
441
442
443
444
445
# File 'ext/ffi_c/StructLayout.c', line 437

static VALUE
struct_layout_to_a(VALUE self)
{
    StructLayout* layout;

    Data_Get_Struct(self, StructLayout, layout);

    return rb_ary_dup(layout->rbFields);
}