Class: Hornetseye::GCCType

Inherits:
Object show all
Defined in:
lib/multiarray/gcctype.rb

Overview

Class for translating native types from Ruby to C

Instance Method Summary collapse

Constructor Details

#initialize(typecode) ⇒ GCCType

Construct GCC type

Parameters:

  • typecode (Class)

    Native type (e.g. UBYTE).



30
31
32
# File 'lib/multiarray/gcctype.rb', line 30

def initialize( typecode )
  @typecode = typecode
end

Instance Method Details

#identifierString

Get C identifier for native type

Returns:

  • (String)

    String with valid C syntax to declare type.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/multiarray/gcctype.rb', line 39

def identifier
  case @typecode
  when nil
    'void'
  when BOOL
    'char'
  when BYTE
    'char'
  when UBYTE
    'unsigned char'
  when SINT
    'short int'
  when USINT
    'unsigned short int'
  when INT
    'int'
  when UINT
    'unsigned int'
  when SFLOAT
    'float'
  when DFLOAT
    'double'
  else
    if @typecode < Pointer_
      'unsigned char *'
    elsif @typecode < INDEX_
      'int'
    else
      raise "No identifier available for #{@typecode.inspect}"
    end
  end
end

#identifiersArray<String>

Get array of C identifiers for native type

Returns:

  • (Array<String>)

    Array of C declarations for the elements of the type.



77
78
79
80
81
82
83
# File 'lib/multiarray/gcctype.rb', line 77

def identifiers
  if @typecode < Composite
    GCCType.new( @typecode.element_type ).identifiers * @typecode.num_elements
  else
    [ GCCType.new( @typecode ).identifier ]
  end
end

#r2cProc

Get code for converting Ruby VALUE to C value

This method returns a nameless function. The nameless function is used for getting the code to convert a given parameter to a C value of this type.

Returns:

  • (Proc)

    Nameless function accepting a C expression to be converted.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/multiarray/gcctype.rb', line 93

def r2c
  case @typecode
  when BOOL
    [ proc { |expr| "( #{expr} ) != Qfalse" } ]
  when BYTE, UBYTE, SINT, USINT, INT, UINT
    [ proc { |expr| "NUM2INT( #{expr} )" } ]
  when SFLOAT, DFLOAT
    [ proc { |expr| "NUM2DBL( #{expr} )" } ]
  else
    if @typecode < Pointer_
      [ proc { |expr| "(#{identifier})mallocToPtr( #{expr} )" } ]
    elsif @typecode < Composite
      GCCType.new( @typecode.element_type ).r2c * @typecode.num_elements
    else
      raise "No conversion available for #{@typecode.inspect}"
    end
  end
end