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



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/Struct.c', line 107

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 {
        s->rbLayout = struct_class_layout(klass);
    }

    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 {
        struct_malloc(s);
    }

    return self;
}

Class Method Details

.alignObject



117
118
119
# File 'lib/ffi/struct.rb', line 117

def self.align
  @layout.alignment
end

.alignmentObject



113
114
115
# File 'lib/ffi/struct.rb', line 113

def self.alignment
  @layout.alignment
end

.by_valueObject



141
142
143
# File 'lib/ffi/struct.rb', line 141

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

.inObject



133
134
135
# File 'lib/ffi/struct.rb', line 133

def self.in
  :buffer_in
end

.layout(*spec) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ffi/struct.rb', line 150

def layout(*spec)
  return @layout if spec.size == 0

  builder = FFI::StructLayoutBuilder.new
  builder.union = self < Union
  if spec[0].kind_of?(Hash)
    hash_layout(builder, spec)
  else
    array_layout(builder, spec)
  end
  builder.size = @size if defined?(@size) && @size > builder.size
  cspec = builder.build
  @layout = cspec unless self == FFI::Struct
  @size = cspec.size
  return cspec
end

.membersObject



121
122
123
# File 'lib/ffi/struct.rb', line 121

def self.members
  @layout.members
end

.offset_of(name) ⇒ Object



129
130
131
# File 'lib/ffi/struct.rb', line 129

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

.offsetsObject



125
126
127
# File 'lib/ffi/struct.rb', line 125

def self.offsets
  @layout.offsets
end

.outObject



137
138
139
# File 'lib/ffi/struct.rb', line 137

def self.out
  :buffer_out
end

.sizeObject



104
105
106
# File 'lib/ffi/struct.rb', line 104

def self.size
  defined?(@layout) ? @layout.size : defined?(@size) ? @size : 0
end

.size=(size) ⇒ Object

Raises:

  • (ArgumentError)


108
109
110
111
# File 'lib/ffi/struct.rb', line 108

def self.size=(size)
  raise ArgumentError, "Size already set" if defined?(@size) || defined?(@layout)
  @size = size
end

Instance Method Details

#[](fieldName) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'ext/ffi_c/Struct.c', line 225

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

    s = struct_validate(self);

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

    if (f->get != NULL) {
        return (*f->get)(f, s);
    
    } else if (f->memoryOp != NULL) {
        return (*f->memoryOp->get)(s->pointer, f->offset);

    } else {
    
        /* call up to the ruby code to fetch the value */
        return rb_funcall2(rbField, id_get, 1, &s->rbPointer);
    }
}

#[]=(fieldName, value) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'ext/ffi_c/Struct.c', line 250

static VALUE
struct_aset(VALUE self, VALUE fieldName, VALUE value)
{
    Struct* s;
    VALUE rbField;
    StructField* f;


    s = struct_validate(self);

    rbField = struct_field(s, fieldName);
    f = (StructField *) DATA_PTR(rbField);
    if (f->put != NULL) {
        (*f->put)(f, s, value);

    } else if (f->memoryOp != NULL) {

        (*f->memoryOp->put)(s->pointer, f->offset, value);
    
    } else {
        /* call up to the ruby code to set the value */
        VALUE argv[2];
        argv[0] = s->rbPointer;
        argv[1] = value;
        rb_funcall2(rbField, id_put, 2, argv);
    }
    
    return value;
}

#alignObject



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

def alignment
  self.class.alignment
end

#alignmentObject



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

def alignment
  self.class.alignment
end

#clearObject



95
96
97
98
# File 'lib/ffi/struct.rb', line 95

def clear
  pointer.clear
  self
end

#layoutObject



338
339
340
341
342
343
344
345
346
# File 'ext/ffi_c/Struct.c', line 338

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

    Data_Get_Struct(self, Struct, s);

    return s->rbLayout;
}

#membersObject



83
84
85
# File 'lib/ffi/struct.rb', line 83

def members
  self.class.members
end

#offset_of(name) ⇒ Object



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

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

#offsetsObject



91
92
93
# File 'lib/ffi/struct.rb', line 91

def offsets
  self.class.offsets
end

#pointerObject



310
311
312
313
314
315
316
317
318
# File 'ext/ffi_c/Struct.c', line 310

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

    Data_Get_Struct(self, Struct, s);

    return s->rbPointer;
}

#sizeObject



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

def size
  self.class.size
end

#to_ptrObject



100
101
102
# File 'lib/ffi/struct.rb', line 100

def to_ptr
  pointer
end

#valuesObject



87
88
89
# File 'lib/ffi/struct.rb', line 87

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