Class: FFI::StructLayout::Function

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

Instance Method Summary collapse

Methods inherited from Field

#alignment, #ffi_type, #initialize, #name, #offset, #size

Constructor Details

This class inherits a constructor from FFI::StructLayout::Field

Instance Method Details

#get(pointer) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'ext/ffi_c/StructLayout.c', line 188

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

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

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

#put(pointer, proc) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'ext/ffi_c/StructLayout.c', line 205

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

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

    if (NIL_P(proc) || rb_obj_is_kind_of(proc, rbffi_FunctionClass)) {
        value = proc;
    } else if (rb_obj_is_kind_of(proc, rb_cProc) || rb_respond_to(proc, rb_intern("call"))) {
        value = rbffi_Function_ForProc(f->rbType, proc);
    } else {
        rb_raise(rb_eTypeError, "wrong type (expected Proc or Function)");
    }

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

    return self;
}