Class: FFI::StructLayout::Field

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

Direct Known Subclasses

Array, Function, StructByValue

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/ffi_c/StructLayout.c', line 81

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);

    return self;
}

Instance Method Details

#alignmentObject



128
129
130
131
132
133
134
# File 'ext/ffi_c/StructLayout.c', line 128

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

#ffi_typeObject



136
137
138
139
140
141
142
# File 'ext/ffi_c/StructLayout.c', line 136

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

#get(pointer) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'ext/ffi_c/StructLayout.c', line 152

static VALUE
struct_field_get(VALUE self, VALUE pointer)
{
    StructField* f;
    MemoryOp* op;
    AbstractMemory* memory = MEMORY(pointer);

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

    return (*op->get)(memory, f->offset);
}

#nameObject



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

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

#offsetObject



112
113
114
115
116
117
118
# File 'ext/ffi_c/StructLayout.c', line 112

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

#put(pointer, value) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'ext/ffi_c/StructLayout.c', line 169

static VALUE
struct_field_put(VALUE self, VALUE pointer, VALUE value)
{
    StructField* f;
    MemoryOp* op;
    AbstractMemory* memory = MEMORY(pointer);

    Data_Get_Struct(self, StructField, f);
    op = memory_get_op(memory, f->type);
    if (op == NULL) {
        rb_raise(rb_eArgError, "put not supported for %s", rb_obj_classname(self));
        return self;
    }

    (*op->put)(memory, f->offset, value);

    return self;
}

#sizeObject



120
121
122
123
124
125
126
# File 'ext/ffi_c/StructLayout.c', line 120

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