Class: GMSEC::Field

Inherits:
Object
  • Object
show all
Extended by:
API
Defined in:
lib/gmsec/field.rb

Constant Summary collapse

GMSEC_TYPEDEF =

Map GMSEC values to deftypes. Note that we list backward compatible aliases first.

{
GMSEC_TYPE_DOUBLE => :double,
GMSEC_TYPE_FLOAT  => :float,
GMSEC_TYPE_LONG   => :long,
GMSEC_TYPE_SHORT  => :short,
GMSEC_TYPE_ULONG  => :ulong,
GMSEC_TYPE_USHORT => :ushort,
GMSEC_TYPE_BOOL   => :bool,
GMSEC_TYPE_CHAR   => :char,
GMSEC_TYPE_F32    => :f32,
GMSEC_TYPE_F64    => :f64,
GMSEC_TYPE_I16    => :i16,
GMSEC_TYPE_I32    => :i32,
GMSEC_TYPE_STR    => :str,
GMSEC_TYPE_U16    => :u16,
GMSEC_TYPE_U32    => :u32,
GMSEC_TYPE_UNSET  => :unset}
TYPE_TO_GMSEC_VALUE =
{
bool:    GMSEC_TYPE_BOOL,
char:    GMSEC_TYPE_CHAR,
double:  GMSEC_TYPE_DOUBLE,
f32:     GMSEC_TYPE_F32,
f64:     GMSEC_TYPE_F64,
float:   GMSEC_TYPE_FLOAT,
i16:     GMSEC_TYPE_I16,
i32:     GMSEC_TYPE_I32,
long:    GMSEC_TYPE_LONG,
short:   GMSEC_TYPE_SHORT,
str:     GMSEC_TYPE_STR,
u16:     GMSEC_TYPE_U16,
u32:     GMSEC_TYPE_U32,
ulong:   GMSEC_TYPE_ULONG,
unset:   GMSEC_TYPE_UNSET,
ushort:  GMSEC_TYPE_USHORT}
RUBY_TO_GMSEC_TYPE =
{
FalseClass => :bool,
Fixnum     => :i32,
Float      => :double,
String     => :str,
Symbol     => :str,
TrueClass  => :bool}

Instance Method Summary collapse

Methods included from API

bind, extended, has

Constructor Details

#initialize(name = nil, value = nil, type: nil) ⇒ Field

Returns a new instance of Field.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gmsec/field.rb', line 55

def initialize(name=nil, value=nil, type: nil)
  if name
    self.name = name
  end

  if type
    self.type = type
  end

  if value
    self.value = value
  end
end

Instance Method Details

#nameObject



69
70
71
72
73
# File 'lib/gmsec/field.rb', line 69

def name
  with_string_pointer do |pointer|
    gmsec_GetFieldName(self, pointer, status)
  end
end

#name=(value) ⇒ Object



75
76
77
# File 'lib/gmsec/field.rb', line 75

def name=(value)
  gmsec_SetFieldName(self, value.to_s, status)
end

#typeObject



124
125
126
127
128
129
130
131
132
# File 'lib/gmsec/field.rb', line 124

def type
  # Create a pointer to GMSEC_TYPE that we're going to read and convert to a symbol.
  pointer = FFI::MemoryPointer.new(find_type(:GMSEC_TYPE))

  gmsec_GetFieldType(self, pointer, status)

  # Default to GMSEC_STR if type is not registered in GMSEC_DEF
  GMSEC_TYPEDEF[pointer.read_ushort] || :str
end

#type=(type) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/gmsec/field.rb', line 134

def type=(type)
  if type.nil?
    raise TypeError.new("#{value.class} is not supported as a GMSEC type.")
  end

  field_type = TYPE_TO_GMSEC_VALUE[type]
  gmsec_SetFieldType(self, field_type, status)

  if status.is_error?
    raise RuntimeError.new("Error in specifying type '#{type}': #{status}")
  end
end

#valueObject



79
80
81
82
83
84
# File 'lib/gmsec/field.rb', line 79

def value
  field_type = "GMSEC_#{type.to_s.upcase}".to_sym
  pointer = FFI::MemoryPointer.new(find_type(field_type))
  send(get_field_value_method, self, pointer, status)
  read_pointer_value(pointer)
end

#value=(value) ⇒ Object



86
87
88
89
90
91
92
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
# File 'lib/gmsec/field.rb', line 86

def value=(value)
  if type == :unset
    # Guess what GMSEC type value is.
    self.type = RUBY_TO_GMSEC_TYPE[value.class]
  end

  # Determind method on value type
  method = "gmsec_SetFieldValue#{type.to_s.upcase}"

  # Cast value based on value type.
  value = case value
          when Symbol
            value.to_s
          when TrueClass, FalseClass
            value ? 1 : 0
          else
            value
          end

  # Cast value based on target type.
  value = case type
          when :bool
            value ? 1 : 0
          when :i16, :i32, :u16, :u32
            value.to_i
          when :f32, :f64
            value.to_f
          when :char
            value.ord
          when :str
            value.to_s
          else
            raise RuntimeError.new("Error in assigning value to field: type '#{type}' not supported")
          end

  send(method, self, value, status)
end