Class: FFIDB::Type

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/ffidb/type.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ Type

Returns a new instance of Type.

Parameters:

  • spec (String, #to_s)


17
18
19
# File 'lib/ffidb/type.rb', line 17

def initialize(spec)
  super(spec.to_s)
end

Class Method Details

.for(type_spec) ⇒ Type

Parameters:

  • spec (String, #to_s)

Returns:



10
11
12
13
# File 'lib/ffidb/type.rb', line 10

def self.for(type_spec)
  return type_spec if type_spec.is_a?(Type)
  self.new(type_spec)
end

Instance Method Details

#<=>(other) ⇒ Integer

Parameters:

Returns:

  • (Integer)


24
# File 'lib/ffidb/type.rb', line 24

def <=>(other) self.spec <=> other.spec end

#array?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/ffidb/type.rb', line 137

def array?
  self.spec.include?('[')
end

#array_pointer?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/ffidb/type.rb', line 172

def array_pointer?
  self.spec.end_with?('[]')
end

#array_sizeInteger

Returns:

  • (Integer)


151
152
153
154
155
# File 'lib/ffidb/type.rb', line 151

def array_size
  if self.array?
    (/\[([^\]]+)\]/ =~ self.spec) && $1.to_i
  end
end

#array_typeType

Returns:



143
144
145
146
147
# File 'lib/ffidb/type.rb', line 143

def array_type
  if self.array?
    self.class.for(self.spec.gsub(/\s+(\[[^\]]+\])/, ''))
  end
end

#atomic?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/ffidb/type.rb', line 51

def atomic?
  self.bool? || self.integer? || self.floating_point? || self.pointer? || nil # TODO
end

#bool?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ffidb/type.rb', line 63

def bool?
  self.spec == '_Bool'
end

#const_qualified?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ffidb/type.rb', line 45

def const_qualified?
  self.spec.start_with?('const ')
end

#enum?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/ffidb/type.rb', line 69

def enum?
  !(self.pointer?) && (self.spec == 'enum' || self.spec.start_with?('enum '))
end

#enum_pointer?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/ffidb/type.rb', line 178

def enum_pointer?
  self.pointer? && self.spec.start_with?('enum ')
end

#floating_point?Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
# File 'lib/ffidb/type.rb', line 128

def floating_point?
  case self.spec
    when 'float', 'double', 'long double' then true
    else false
  end
end

#function_pointer?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/ffidb/type.rb', line 196

def function_pointer?
  self.spec.include?('(*)')
end

#inspectString

Returns:

  • (String)


214
215
216
# File 'lib/ffidb/type.rb', line 214

def inspect
  "#{self.class}(#{self.spec.inspect})"
end

#integer?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
# File 'lib/ffidb/type.rb', line 87

def integer?
  case self.spec
    when 'char', 'short', 'int', 'long', 'long long' then true
    when 'unsigned char', 'unsigned short', 'unsigned int', 'unsigned long', 'unsigned long long' then true
    when 'size_t', 'wchar_t' then true # <stddef.h>
    when 'ssize_t', 'off_t' then true  # <sys/types.h>
    when /^u?int\d+_t$/ then true
    else false
  end
end

#nameSymbol

Returns:

  • (Symbol)


39
40
41
# File 'lib/ffidb/type.rb', line 39

def name
  self.spec.gsub(/\s*\*+$/, '').to_sym # TODO
end

#pointer?Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
165
166
167
168
# File 'lib/ffidb/type.rb', line 159

def pointer?
  self.spec.end_with?('*') ||
  self.array_pointer?      ||
  self.function_pointer?   ||
  case self.spec
    when 'intptr_t', 'uintptr_t' then true
    when 'va_list' then true
    else false
  end
end

#signed_integer?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
109
# File 'lib/ffidb/type.rb', line 100

def signed_integer?
  return false unless self.integer?
  case self.spec
    when 'char', 'short', 'int', 'long', 'long long' then true
    when 'wchar_t' then nil  # <stddef.h>
    when 'ssize_t' then true # <sys/types.h>
    when /^int\d+_t$/ then true
    else false
  end
end

#struct?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/ffidb/type.rb', line 75

def struct?
  !(self.pointer?) && (self.spec == 'struct' || self.spec.start_with?('struct ') || self.spec.start_with?('const struct '))
end

#struct_pointer?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/ffidb/type.rb', line 184

def struct_pointer?
  self.pointer? && (self.spec.start_with?('struct ') || self.spec.start_with?('const struct '))
end

#tagSymbol

Returns:

  • (Symbol)


28
29
30
31
32
33
34
35
# File 'lib/ffidb/type.rb', line 28

def tag
  case self.spec.gsub(/^const /, '')
    when 'enum', /^enum\s+/ then :enum
    when 'struct', /^struct\s+/ then :struct
    when 'union', /^union\s+/ then :union
    else nil
  end
end

#to_hHash<Symbol, Object>

Returns:

  • (Hash<Symbol, Object>)


208
209
210
# File 'lib/ffidb/type.rb', line 208

def to_h
  {spec: self.spec}
end

#to_sString

Returns:

  • (String)


202
203
204
# File 'lib/ffidb/type.rb', line 202

def to_s
  self.spec
end

#union?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/ffidb/type.rb', line 81

def union?
  !(self.pointer?) && (self.spec.start_with?('union ') || self.spec.start_with?('const union '))
end

#union_pointer?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/ffidb/type.rb', line 190

def union_pointer?
  self.pointer? && (self.spec.start_with?('union ') || self.spec.start_with?('const union '))
end

#unsigned_integer?Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ffidb/type.rb', line 113

def unsigned_integer?
  return false unless self.integer?
  return true if self.spec.start_with?('u')
  case self.spec
    when 'unsigned char', 'unsigned short', 'unsigned int', 'unsigned long', 'unsigned long long' then true
    when 'size_t' then true # <stddef.h>
    when 'wchar_t' then nil # <stddef.h>
    when 'off_t' then true  # <sys/types.h>
    when /^uint\d+_t$/ then true
    else false
  end
end

#void?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ffidb/type.rb', line 57

def void?
  self.spec == 'void'
end