Class: RLTK::CG::StructType

Inherits:
AggregateType show all
Defined in:
lib/rltk/cg/type.rb

Overview

A type for representing an arbitrary collection of types.

Instance Attribute Summary

Attributes included from BindingClass

#ptr

Instance Method Summary collapse

Methods inherited from Type

#allignment, #context, #dump, from_ptr, #hash, #kind, #size, #to_s

Methods included from BindingClass

#==

Constructor Details

#initialize(overloaded, name = nil, packed = false, context = nil) ⇒ StructType

Create a new struct type.



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/rltk/cg/type.rb', line 408

def initialize(overloaded, name = nil, packed = false, context = nil)
  @ptr =
  case overloaded
  when FFI::Pointer
    overloaded
  else
    # Check the types of the elements of the overloaded parameter.

    @element_types = check_cg_array_type(overloaded, Type, 'overloaded')

    el_types_ptr = FFI::MemoryPointer.new(:pointer, @element_types.length)
    el_types_ptr.write_array_of_pointer(@element_types)

    if name
      @name = check_type(name, String, 'name')

      Bindings.struct_create_named(Context.global, @name).tap do |ptr|
        Bindings.struct_set_body(ptr, el_types_ptr, @element_types.length, packed.to_i) unless @element_types.empty?
      end

    elsif context
      check_type(context, Context, 'context')

      Bindings.struct_type_in_context(context, el_types_ptr, @element_types.length, is_packed.to_i)

    else
      Bindings.struct_type(el_types_ptr, @element_types.length, packed.to_i)
    end
  end
end

Instance Method Details

#element_typesArray<Type>



439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/rltk/cg/type.rb', line 439

def element_types
  @element_types ||=
  begin
    num_elements = Bindings.count_struct_element_types(@ptr)

    ret_ptr = FFI::MemoryPointer.new(:pointer)
    Bindings.get_struct_element_types(@ptr, ret_ptr)

    types_ptr = ret_ptr.get_pointer(0)

    types_ptr.get_array_of_pointer(0, num_elements).map { |ptr| Type.from_ptr(ptr) }
  end
end

#element_types=(el_types, packed = false) ⇒ void

This method returns an undefined value.

Set the types in the body of this struct type.



459
460
461
462
463
464
465
466
# File 'lib/rltk/cg/type.rb', line 459

def element_types=(el_types, packed = false)
  @element_types = check_cg_array_type(el_types, Type, 'el_types')

  el_types_ptr = FFI::MemoryPointer.new(:pointer, @element_types.length)
  el_types_ptr.write_array_of_pointer(@element_types)

  Bindings.struct_set_body(@ptr, el_types_ptr, @element_types.length, packed.to_i)
end

#nameString



469
470
471
# File 'lib/rltk/cg/type.rb', line 469

def name
  @name ||= Bindings.get_struct_name(@ptr)
end