Class: FFI::Struct

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

Direct Known Subclasses

ManagedStruct, Union

Defined Under Namespace

Classes: ManagedStructConverter

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Object) initialize



93
94
95
96
97
98
99
100
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
# File 'ext/ffi_c/Struct.c', line 93

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, (int) 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

+ (Object) alignment



123
124
125
# File 'lib/ffi/struct.rb', line 123

def self.alignment
  @layout.alignment
end

+ (Object) array_layout(builder, spec) (private)



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/ffi/struct.rb', line 264

def array_layout(builder, spec)
  i = 0
  while i < spec.size
    name, type = spec[i, 2]
    i += 2

    # If the next param is a Integer, it specifies the offset
    if spec[i].kind_of?(Integer)
      offset = spec[i]
      i += 1
    else
      offset = nil
    end

    builder.add name, find_field_type(type), offset
  end
end

+ (Object) auto_ptr



177
178
179
# File 'lib/ffi/struct.rb', line 177

def self.auto_ptr
  @managed_type ||= Type::Mapped.new(ManagedStructConverter.new(self))
end

+ (Object) by_ref(flags = :inout)



159
160
161
# File 'lib/ffi/struct.rb', line 159

def self.by_ref(flags = :inout)
  self.ptr(flags)
end

+ (Object) by_value



155
156
157
# File 'lib/ffi/struct.rb', line 155

def self.by_value
  self.val
end

+ (Object) hash_layout(builder, spec) (private)



257
258
259
260
261
262
# File 'lib/ffi/struct.rb', line 257

def hash_layout(builder, spec)
  raise "Ruby version not supported" if RUBY_VERSION =~ /1.8.*/
  spec[0].each do |name, type|
    builder.add name, find_field_type(type), nil
  end
end

+ (Object) in



139
140
141
# File 'lib/ffi/struct.rb', line 139

def self.in
  ptr(:in)
end

+ (Object) layout(*spec)



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/ffi/struct.rb', line 185

def layout(*spec)
#        raise RuntimeError, "struct layout already defined for #{self.inspect}" if defined?(@layout)
  return @layout if spec.size == 0

  builder = StructLayoutBuilder.new
  builder.union = self < Union
  builder.packed = @packed if defined?(@packed)
  builder.alignment = @min_alignment if defined?(@min_alignment)

  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 == Struct
  @size = cspec.size
  return cspec
end

+ (Object) members



127
128
129
# File 'lib/ffi/struct.rb', line 127

def self.members
  @layout.members
end

+ (Object) offset_of(name)



135
136
137
# File 'lib/ffi/struct.rb', line 135

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

+ (Object) offsets



131
132
133
# File 'lib/ffi/struct.rb', line 131

def self.offsets
  @layout.offsets
end

+ (Object) out



143
144
145
# File 'lib/ffi/struct.rb', line 143

def self.out
  ptr(:out)
end

+ (Object) ptr(flags = :inout)



147
148
149
# File 'lib/ffi/struct.rb', line 147

def self.ptr(flags = :inout)
  @ref_data_type ||= Type::Mapped.new(StructByReference.new(self))
end

+ (Object) size



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

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

+ (Object) size=(size)

Raises:

  • (ArgumentError)


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

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

+ (Object) val



151
152
153
# File 'lib/ffi/struct.rb', line 151

def self.val
  @val_data_type ||= StructByValue.new(self)
end

Instance Method Details

- (Object) []



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'ext/ffi_c/Struct.c', line 282

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

- (Object) []=



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'ext/ffi_c/Struct.c', line 307

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

    if (f->referenceRequired) {
        store_reference_value(f, s, value);
    }
    
    return value;
}

- (Object) align



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

def alignment
  self.class.alignment
end

- (Object) alignment



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

def alignment
  self.class.alignment
end

- (Object) clear



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

def clear
  pointer.clear
  self
end

- (Object) initialize_copy



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/ffi_c/Struct.c', line 127

static VALUE
struct_initialize_copy(VALUE self, VALUE other)
{
    Struct* src;
    Struct* dst;
    
    Data_Get_Struct(self, Struct, dst);
    Data_Get_Struct(other, Struct, src);
    if (dst == src) {
        return self;
    }
    
    dst->rbLayout = src->rbLayout;
    dst->layout = src->layout;
    
    /*
     * A new MemoryPointer instance is allocated here instead of just calling
     * #dup on rbPointer, since the Pointer may not know its length, or may
     * be longer than just this struct.
     */
    if (src->pointer->address != NULL) {
        dst->rbPointer = rbffi_MemoryPointer_NewInstance(1, src->layout->size, false);
        dst->pointer = MEMORY(dst->rbPointer);
        memcpy(dst->pointer->address, src->pointer->address, src->layout->size);
    } else {
        dst->rbPointer = src->rbPointer;
        dst->pointer = src->pointer;
    }

    if (src->layout->referenceFieldCount > 0) {
        dst->rbReferences = ALLOC_N(VALUE, dst->layout->referenceFieldCount);
        memcpy(dst->rbReferences, src->rbReferences, dst->layout->referenceFieldCount * sizeof(VALUE));
    }
        
    return self;
}

- (Object) layout



399
400
401
402
403
404
405
406
407
# File 'ext/ffi_c/Struct.c', line 399

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

    Data_Get_Struct(self, Struct, s);

    return s->rbLayout;
}

- (Object) layout= (private)



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'ext/ffi_c/Struct.c', line 381

static VALUE
struct_set_layout(VALUE self, VALUE layout)
{
    Struct* s;
    Data_Get_Struct(self, Struct, s);

    if (!rb_obj_is_kind_of(layout, rbffi_StructLayoutClass)) {
        rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
                rb_obj_classname(layout), rb_class2name(rbffi_StructLayoutClass));
        return Qnil;
    }

    Data_Get_Struct(layout, StructLayout, s->layout);
    rb_ivar_set(self, id_layout_ivar, layout);

    return self;
}

- (Object) members



93
94
95
# File 'lib/ffi/struct.rb', line 93

def members
  self.class.members
end

- (Boolean) null?

Returns:

  • (Boolean)


410
411
412
413
414
415
416
417
418
# File 'ext/ffi_c/Struct.c', line 410

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

    Data_Get_Struct(self, Struct, s);

    return s->pointer->address == NULL ? Qtrue : Qfalse;
}

- (Object) offset_of(name)



89
90
91
# File 'lib/ffi/struct.rb', line 89

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

- (Object) offsets



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

def offsets
  self.class.offsets
end

- (Object) order



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'ext/ffi_c/Struct.c', line 420

static VALUE
struct_order(int argc, VALUE* argv, VALUE self)
{
    Struct* s;

    Data_Get_Struct(self, Struct, s);
    if (argc == 0) {
        return rb_funcall(s->rbPointer, rb_intern("order"), 0);

    } else {
        VALUE retval = rb_obj_dup(self);
        VALUE rbPointer = rb_funcall2(s->rbPointer, rb_intern("order"), argc, argv);
        struct_set_pointer(retval, rbPointer);
        
        return retval;
    }
}

- (Object) pointer



371
372
373
374
375
376
377
378
379
# File 'ext/ffi_c/Struct.c', line 371

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

    Data_Get_Struct(self, Struct, s);

    return s->rbPointer;
}

- (Object) pointer= (private)



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'ext/ffi_c/Struct.c', line 341

static VALUE
struct_set_pointer(VALUE self, VALUE pointer)
{
    Struct* s;
    StructLayout* layout;
    AbstractMemory* memory;

    if (!rb_obj_is_kind_of(pointer, rbffi_AbstractMemoryClass)) {
        rb_raise(rb_eTypeError, "wrong argument type %s (expected Pointer or Buffer)",
                rb_obj_classname(pointer));
        return Qnil;
    }

    
    Data_Get_Struct(self, Struct, s);
    Data_Get_Struct(pointer, AbstractMemory, memory);
    layout = struct_layout(self);

    if ((int) layout->base.ffiType->size > memory->size) {
        rb_raise(rb_eArgError, "memory of %ld bytes too small for struct %s (expected at least %ld)",
                memory->size, rb_obj_classname(self), (long) layout->base.ffiType->size);
    }
    
    s->pointer = MEMORY(pointer);
    s->rbPointer = pointer;
    rb_ivar_set(self, id_pointer_ivar, pointer);

    return self;
}

- (Object) size



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

def size
  self.class.size
end

- (Object) to_ptr



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

def to_ptr
  pointer
end

- (Object) values



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

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