class FFIDB::Type

Public Class Methods

for(type_spec) click to toggle source

@param [String, to_s] spec @return [Type]

# 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
new(spec) click to toggle source

@param [String, to_s] spec

Calls superclass method
# File lib/ffidb/type.rb, line 17
def initialize(spec)
  super(spec.to_s)
end

Public Instance Methods

<=>(other) click to toggle source

@param [Type] other @return [Integer]

# File lib/ffidb/type.rb, line 24
def <=>(other) self.spec <=> other.spec end
array?() click to toggle source

@return [Boolean]

# File lib/ffidb/type.rb, line 137
def array?
  self.spec.include?('[')
end
array_pointer?() click to toggle source

@return [Boolean]

# File lib/ffidb/type.rb, line 172
def array_pointer?
  self.spec.end_with?('[]')
end
array_size() click to toggle source

@return [Integer]

# File lib/ffidb/type.rb, line 151
def array_size
  if self.array?
    (/\[([^\]]+)\]/ =~ self.spec) && $1.to_i
  end
end
array_type() click to toggle source

@return [Type]

# File lib/ffidb/type.rb, line 143
def array_type
  if self.array?
    self.class.for(self.spec.gsub(/\s+(\[[^\]]+\])/, ''))
  end
end
atomic?() click to toggle source

@return [Boolean]

# File lib/ffidb/type.rb, line 51
def atomic?
  self.bool? || self.integer? || self.floating_point? || self.pointer? || nil # TODO
end
bool?() click to toggle source

@return [Boolean]

# File lib/ffidb/type.rb, line 63
def bool?
  self.spec == '_Bool'
end
const_qualified?() click to toggle source

@return [Boolean]

# File lib/ffidb/type.rb, line 45
def const_qualified?
  self.spec.start_with?('const ')
end
enum?() click to toggle source

@return [Boolean]

# File lib/ffidb/type.rb, line 69
def enum?
  !(self.pointer?) && (self.spec == 'enum' || self.spec.start_with?('enum '))
end
enum_pointer?() click to toggle source

@return [Boolean]

# File lib/ffidb/type.rb, line 178
def enum_pointer?
  self.pointer? && self.spec.start_with?('enum ')
end
floating_point?() click to toggle source

@return [Boolean]

# 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?() click to toggle source

@return [Boolean]

# File lib/ffidb/type.rb, line 196
def function_pointer?
  self.spec.include?('(*)')
end
inspect() click to toggle source

@return [String]

# File lib/ffidb/type.rb, line 214
def inspect
  "#{self.class}(#{self.spec.inspect})"
end
integer?() click to toggle source

@return [Boolean]

# 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
name() click to toggle source

@return [Symbol]

# File lib/ffidb/type.rb, line 39
def name
  self.spec.gsub(/\s*\*+$/, '').to_sym # TODO
end
pointer?() click to toggle source

@return [Boolean]

# 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?() click to toggle source

@return [Boolean]

# 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?() click to toggle source

@return [Boolean]

# 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?() click to toggle source

@return [Boolean]

# File lib/ffidb/type.rb, line 184
def struct_pointer?
  self.pointer? && (self.spec.start_with?('struct ') || self.spec.start_with?('const struct '))
end
tag() click to toggle source

@return [Symbol]

# 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_h() click to toggle source

@return [Hash<Symbol, Object>]

# File lib/ffidb/type.rb, line 208
def to_h
  {spec: self.spec}
end
to_s() click to toggle source

@return [String]

# File lib/ffidb/type.rb, line 202
def to_s
  self.spec
end
union?() click to toggle source

@return [Boolean]

# 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?() click to toggle source

@return [Boolean]

# 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?() click to toggle source

@return [Boolean]

# 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?() click to toggle source

@return [Boolean]

# File lib/ffidb/type.rb, line 57
def void?
  self.spec == 'void'
end

Protected Instance Methods

alignof() click to toggle source

@return [Integer, Range, nil]

# File lib/ffidb/type.rb, line 229
def alignof
  nil # TODO
end
size()
Alias for: sizeof
sizeof() click to toggle source

@return [Integer, Range, nil]

# File lib/ffidb/type.rb, line 222
def sizeof
  nil # TODO
end
Also aliased as: size