Class: FFI::Struct

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

Direct Known Subclasses

ManagedStruct, Union

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



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
140
141
# File 'ext/ffi_c/Struct.c', line 101

static VALUE
struct_initialize(int argc, VALUE* argv, VALUE self)
{
    Struct* s;
    VALUE rbPointer = Qnil, rest = Qnil, klass = CLASS_OF(self);
    int nargs;

    Data_Get_Struct(self, Struct, s);
    
    nargs = rb_scan_args(argc, argv, "01*", &rbPointer, &rest);

    /* Call up into ruby code to adjust the layout */
    if (nargs > 1) {
        s->rbLayout = rb_funcall2(CLASS_OF(self), id_layout, RARRAY_LEN(rest), RARRAY_PTR(rest));
    } else if (rb_cvar_defined(klass, id_layout_ivar)) {
        s->rbLayout = rb_cvar_get(klass, id_layout_ivar);
    } else {
        rb_raise(rb_eRuntimeError, "No Struct layout configured");
    }

    if (!rb_obj_is_kind_of(s->rbLayout, rbffi_StructLayoutClass)) {
        rb_raise(rb_eRuntimeError, "Invalid Struct layout");
    }

    Data_Get_Struct(s->rbLayout, StructLayout, s->layout);
    
    if (rbPointer != Qnil) {
        s->pointer = MEMORY(rbPointer);
        s->rbPointer = rbPointer;
    } else {
        s->rbPointer = rbffi_MemoryPointer_NewInstance(s->layout->size, 1, true);
        s->pointer = (AbstractMemory *) DATA_PTR(s->rbPointer);
    }

    if (s->pointer->ops == NULL) {
        VALUE name = rb_class_name(CLASS_OF(s->rbPointer));
        rb_raise(rb_eRuntimeError, "No memory ops set for %s", StringValueCStr(name));
    }

    return self;
}

Class Method Details

.alignObject



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

def self.align
  @layout.alignment
end

.by_valueObject



18
19
20
# File 'lib/ffi/struct.rb', line 18

def self.by_value
  ::FFI::StructByValue.new(self)
end

.inObject



74
75
76
# File 'lib/ffi/struct.rb', line 74

def self.in
  :buffer_in
end

.layout(*spec) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/ffi/struct.rb', line 156

def self.layout(*spec)
  return @layout if spec.size == 0
  cspec = spec[0].kind_of?(Hash) ? hash_layout(spec) : array_layout(spec)
  @layout = cspec unless self == FFI::Struct
  @size = cspec.size
  return cspec
end

.membersObject



26
27
28
# File 'lib/ffi/struct.rb', line 26

def self.members
  @layout.members
end

.offset_of(field_name) ⇒ Object



38
39
40
# File 'lib/ffi/struct.rb', line 38

def self.offset_of(field_name)
  @layout.offset_of(field_name)
end

.offsetsObject



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

def self.offsets
  @layout.offsets
end

.outObject



78
79
80
# File 'lib/ffi/struct.rb', line 78

def self.out
  :buffer_out
end

.sizeObject



22
23
24
# File 'lib/ffi/struct.rb', line 22

def self.size
  @layout.size
end

Instance Method Details

#[](fieldName) ⇒ Object



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

static VALUE
struct_aref(VALUE self, VALUE fieldName)
{
    Struct* s;
    VALUE rbField;
    StructField* f;
    MemoryOp* op;

    Data_Get_Struct(self, Struct, s);
    rbField = struct_field(s, fieldName);
    f = (StructField *) DATA_PTR(rbField);

    op = memory_get_op(s->pointer, f->type);
    if (op != NULL) {
        return (*op->get)(s->pointer, f->offset);
    }
    
    /* call up to the ruby code to fetch the value */
    return rb_funcall2(rbField, id_get, 1, &s->rbPointer);
}

#[]=(fieldName, value) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'ext/ffi_c/Struct.c', line 189

static VALUE
struct_aset(VALUE self, VALUE fieldName, VALUE value)
{
    Struct* s;
    VALUE rbField;
    StructField* f;
    MemoryOp* op;
    VALUE argv[2];

    Data_Get_Struct(self, Struct, s);
    rbField = struct_field(s, fieldName);
    f = (StructField *) DATA_PTR(rbField);

    op = memory_get_op(s->pointer, f->type);
    if (op != NULL) {
        (*op->put)(s->pointer, f->offset, value);
        return self;
    }
    
    /* call up to the ruby code to set the value */
    argv[0] = s->rbPointer;
    argv[1] = value;
    rb_funcall2(rbField, id_put, 2, argv);
    
    return self;
}

#alignObject



46
47
48
# File 'lib/ffi/struct.rb', line 46

def align
  self.class.align
end

#clearObject



65
66
67
68
# File 'lib/ffi/struct.rb', line 65

def clear
  pointer.clear
  self
end

#layoutObject



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

static VALUE
struct_get_layout(VALUE self)
{
    Struct* s;

    Data_Get_Struct(self, Struct, s);

    return s->rbLayout;
}

#membersObject



50
51
52
# File 'lib/ffi/struct.rb', line 50

def members
  layout.members
end

#offset_of(field_name) ⇒ Object



61
62
63
# File 'lib/ffi/struct.rb', line 61

def offset_of(field_name)
  self.class.offset_of(field_name)
end

#offsetsObject



57
58
59
# File 'lib/ffi/struct.rb', line 57

def offsets
  self.class.offsets
end

#pointerObject



233
234
235
236
237
238
239
240
241
# File 'ext/ffi_c/Struct.c', line 233

static VALUE
struct_get_pointer(VALUE self)
{
    Struct* s;

    Data_Get_Struct(self, Struct, s);

    return s->rbPointer;
}

#sizeObject



42
43
44
# File 'lib/ffi/struct.rb', line 42

def size
  self.class.size
end

#to_ptrObject



70
71
72
# File 'lib/ffi/struct.rb', line 70

def to_ptr
  pointer
end

#valuesObject



54
55
56
# File 'lib/ffi/struct.rb', line 54

def values
  layout.members.map { |m| self[m] }
end