Module: Rstruct::ClassMethods

Included in:
Rstruct
Defined in:
lib/rstruct.rb

Instance Method Summary collapse

Instance Method Details

#default_registryObject

Returns the default Rstruct registry



58
59
60
# File 'lib/rstruct.rb', line 58

def default_registry
  Registry::DEFAULT_REGISTRY
end

#get_type(typ, reg = Registry::DEFAULT_REGISTRY) ⇒ Object



62
63
64
# File 'lib/rstruct.rb', line 62

def get_type(typ, reg=Registry::DEFAULT_REGISTRY)
  reg[typ]
end

#sizeof(typ, reg = nil) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/rstruct.rb', line 48

def sizeof(typ, reg=nil)
  reg ||= default_registry
  if t=reg[typ]
    t.sizeof
  else
    raise(InvalidTypeError, "unknown type: #{typ}")
  end
end

#struct(name, opts = {}, &block) ⇒ Object

Take the following C struct for example:

struct mach_header {
  uint32_t      magic;      /* mach magic number identifier */
  cpu_type_t    cputype;    /* cpu specifier */
  cpu_subtype_t cpusubtype; /* machine specifier */
  uint32_t      filetype;   /* type of file */
  uint32_t      ncmds;      /* number of load commands */
  uint32_t      sizeofcmds; /* the size of all the load commands */
  uint32_t      flags;      /* flags */
}

Which might be defined with rstruct as follows:

extend(Rstruct::ClassMethods)

typedef :uint32_t, :cpu_type_t
typedef :uint32_t, :cpu_subtype_t

struct(:mach_header) {
  uint32_t      :magic      # mach magic number identifier
  cpu_type_t    :cputype    # cpu specifier
  cpu_subtype_t :cpusubtype # machine specifier
  uint32_t      :filetype   # type of file
  uint32_t      :ncmds      # number of load commands
  uint32_t      :sizeofcmds # the size of all the load commands
  uint32_t      :flags      # flags
}


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

def struct(name, opts={},&block)
  Rstruct::Structure.new(name, opts, &block)
end

#typedef(p, t, opts = {}) ⇒ Object



43
44
45
46
# File 'lib/rstruct.rb', line 43

def typedef(p, t, opts={})
  reg = opts[:register] || default_registry
  reg.typedef(p,t,opts)
end