Class: FFI::StructLayout
- Inherits:
-
Type
- Object
- Type
- FFI::StructLayout
- Defined in:
- lib/ffi/struct.rb,
ext/ffi_c/StructLayout.c
Defined Under Namespace
Classes: Array, CharArray, Enum, Field, Function, InnerStruct, Mapped, Number, Pointer, String
Constant Summary
Constant Summary
Constants inherited from Type
Type::Array, Type::Function, Type::Struct
Instance Method Summary (collapse)
- - (Object) []
- - (Object) __union!
- - (Object) fields
- - (Object) initialize constructor
- - (Object) members
- - (Object) offset_of(field_name)
- - (Object) offsets
- - (Object) to_a
Methods inherited from Type
Constructor Details
- (Object) initialize
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'ext/ffi_c/StructLayout.c', line 349
static VALUE
struct_layout_initialize(VALUE self, VALUE fields, VALUE size, VALUE align)
{
StructLayout* layout;
ffi_type* ltype;
int i;
Data_Get_Struct(self, StructLayout, layout);
layout->fieldCount = (int) RARRAY_LEN(fields);
layout->rbFieldMap = rb_hash_new();
layout->rbFieldNames = rb_ary_new2(layout->fieldCount);
layout->size = (int) FFI_ALIGN(NUM2INT(size), NUM2INT(align));
layout->align = NUM2INT(align);
layout->fields = xcalloc(layout->fieldCount, sizeof(StructField *));
layout->ffiTypes = xcalloc(layout->fieldCount + 1, sizeof(ffi_type *));
layout->rbFields = rb_ary_new2(layout->fieldCount);
layout->referenceFieldCount = 0;
layout->base.ffiType->elements = layout->ffiTypes;
layout->base.ffiType->size = layout->size;
layout->base.ffiType->alignment = layout->align;
ltype = layout->base.ffiType;
for (i = 0; i < (int) layout->fieldCount; ++i) {
VALUE rbField = rb_ary_entry(fields, i);
VALUE rbName;
StructField* field;
ffi_type* ftype;
if (!rb_obj_is_kind_of(rbField, rbffi_StructLayoutFieldClass)) {
rb_raise(rb_eTypeError, "wrong type for field %d.", i);
}
rbName = rb_funcall2(rbField, rb_intern("name"), 0, NULL);
Data_Get_Struct(rbField, StructField, field);
layout->fields[i] = field;
if (field->type == NULL || field->type->ffiType == NULL) {
rb_raise(rb_eRuntimeError, "type of field %d not supported", i);
}
ftype = field->type->ffiType;
if (ftype->size == 0) {
rb_raise(rb_eTypeError, "type of field %d has zero size", i);
}
if (field->referenceRequired) {
field->referenceIndex = layout->referenceFieldCount++;
}
layout->ffiTypes[i] = ftype;
st_insert(layout->fieldSymbolTable, rbName, rbField);
rb_hash_aset(layout->rbFieldMap, rbName, rbField);
rb_ary_push(layout->rbFields, rbField);
rb_ary_push(layout->rbFieldNames, rbName);
}
if (ltype->size == 0) {
rb_raise(rb_eRuntimeError, "Struct size is zero");
}
return self;
}
|
Instance Method Details
- (Object) []
447 448 449 450 451 452 453 454 455 |
# File 'ext/ffi_c/StructLayout.c', line 447
static VALUE
struct_layout_aref(VALUE self, VALUE field)
{
StructLayout* layout;
Data_Get_Struct(self, StructLayout, layout);
return rb_hash_aref(layout->rbFieldMap, field);
}
|
- (Object) __union!
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'ext/ffi_c/StructLayout.c', line 413
static VALUE
struct_layout_union_bang(VALUE self)
{
static const ffi_type *alignment_types[] = { &ffi_type_sint8, &ffi_type_sint16, &ffi_type_sint32, &ffi_type_sint64,
&ffi_type_float, &ffi_type_double, &ffi_type_longdouble, NULL };
StructLayout* layout;
ffi_type *t = NULL;
int count, i;
Data_Get_Struct(self, StructLayout, layout);
for (i = 0; alignment_types[i] != NULL; ++i) {
if (alignment_types[i]->alignment == layout->align) {
t = (ffi_type *) alignment_types[i];
break;
}
}
if (t == NULL) {
rb_raise(rb_eRuntimeError, "cannot create libffi union representation for alignment %d", layout->align);
return Qnil;
}
count = (int) layout->size / t->size;
xfree(layout->ffiTypes);
layout->ffiTypes = xcalloc(count + 1, sizeof(ffi_type *));
layout->base.ffiType->elements = layout->ffiTypes;
for (i = 0; i < count; ++i) {
layout->ffiTypes[i] = t;
}
return self;
}
|
- (Object) fields
457 458 459 460 461 462 463 464 465 |
# File 'ext/ffi_c/StructLayout.c', line 457
static VALUE
struct_layout_fields(VALUE self)
{
StructLayout* layout;
Data_Get_Struct(self, StructLayout, layout);
return rb_ary_dup(layout->rbFields);
}
|
- (Object) members
467 468 469 470 471 472 473 474 475 |
# File 'ext/ffi_c/StructLayout.c', line 467
static VALUE
struct_layout_members(VALUE self)
{
StructLayout* layout;
Data_Get_Struct(self, StructLayout, layout);
return rb_ary_dup(layout->rbFieldNames);
}
|
- (Object) offset_of(field_name)
34 35 36 |
# File 'lib/ffi/struct.rb', line 34 def offset_of(field_name) self[field_name].offset end |
- (Object) offsets
30 31 32 |
# File 'lib/ffi/struct.rb', line 30 def offsets members.map { |m| [ m, self[m].offset ] } end |
- (Object) to_a
477 478 479 480 481 482 483 484 485 |
# File 'ext/ffi_c/StructLayout.c', line 477
static VALUE
struct_layout_to_a(VALUE self)
{
StructLayout* layout;
Data_Get_Struct(self, StructLayout, layout);
return rb_ary_dup(layout->rbFields);
}
|