Class: FFI::StructLayout::Function

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

Overview

A function pointer Field in a FFI::StructLayout.

Instance Method Summary collapse

Methods inherited from Field

#alignment, #initialize, #name, #offset, #size, #type

Constructor Details

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

Instance Method Details

#get(pointer) ⇒ Function

Get a FFI::StructLayout::Function from memory pointed by pointer.

Parameters:

Returns:


260
261
262
263
264
265
266
267
268
# File 'ext/ffi_c/StructLayout.c', line 260

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

    return rbffi_Function_NewInstance(f->rbType, (*rbffi_AbstractMemoryOps.pointer->get)(MEMORY(pointer), f->offset));
}

#put(pointer, proc) ⇒ Function

Set a FFI::StructLayout::Function to memory pointed by pointer as a function.

If a Proc is submitted as proc, it is automatically transformed to a FFI::StructLayout::Function.

Parameters:

Returns:


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

static VALUE
function_field_put(VALUE self, VALUE pointer, VALUE proc)
{
    StructField* f;
    VALUE value = Qnil;

    Data_Get_Struct(self, StructField, f);

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

    (*rbffi_AbstractMemoryOps.pointer->put)(MEMORY(pointer), f->offset, value);

    return self;
}