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:


94
95
96
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
# File 'ext/ffi_c/StructLayout.c', line 94

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_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.

Returns:

  • (Numeric)

172
173
174
175
176
177
178
# File 'ext/ffi_c/StructLayout.c', line 172

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.

Parameters:

Returns:

  • (Object)

213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'ext/ffi_c/StructLayout.c', line 213

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.

Returns:

  • (Symbol)

199
200
201
202
203
204
205
# File 'ext/ffi_c/StructLayout.c', line 199

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

#offsetNumeric

Get the field offset.

Returns:

  • (Numeric)

146
147
148
149
150
151
152
# File 'ext/ffi_c/StructLayout.c', line 146

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.

Parameters:

Returns:

  • (self)

234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'ext/ffi_c/StructLayout.c', line 234

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.

Returns:

  • (Numeric)

159
160
161
162
163
164
165
# File 'ext/ffi_c/StructLayout.c', line 159

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.

Returns:


185
186
187
188
189
190
191
192
# File 'ext/ffi_c/StructLayout.c', line 185

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

    return field->rbType;
}