Parent

Files

Class/Module Index [+]

Quicksearch

BinData::Struct

A Struct is an ordered collection of named data objects.

require 'bindata'

class Tuple < BinData::Record
  int8  :x
  int8  :y
  int8  :z
end

obj = BinData::Struct.new(:hide => :a,
                          :fields => [ [:int32le, :a],
                                       [:int16le, :b],
                                       [:tuple, :s] ])
obj.field_names   =># [:b, :s]

Parameters

Parameters may be provided at initialisation to control the behaviour of an object. These params are:

:fields

An array specifying the fields for this struct. Each element of the array is of the form [type, name, params]. Type is a symbol representing a registered type. Name is the name of this field. Params is an optional hash of parameters to pass to this field when instantiating it. If name is “” or nil, then that field is anonymous and behaves as a hidden field.

:hide

A list of the names of fields that are to be hidden from the outside world. Hidden fields don’t appear in snapshot or field_names but are still accessible by name.

:endian

Either :little or :big. This specifies the default endian of any numerics in this struct, or in any nested data objects.

Field Parameters

Fields may have have extra parameters as listed below:

:onlyif

Used to indicate a data object is optional. if false, this object will not be included in any calls to read, write, num_bytes or snapshot.

:byte_align

This field’s rel_offset must be a multiple of :byte_align.

Constants

RESERVED

These reserved words may not be used as field names

Public Instance Methods

[](key) click to toggle source
# File lib/bindata/struct.rb, line 144
def [](key)
  find_obj_for_name(key)
end
[]=(key, value) click to toggle source
# File lib/bindata/struct.rb, line 148
def []=(key, value)
  obj = find_obj_for_name(key)
  if obj
    obj.assign(value)
  end
end
assign(val) click to toggle source
# File lib/bindata/struct.rb, line 92
def assign(val)
  clear
  assign_fields(val)
end
do_write(io) click to toggle source
# File lib/bindata/struct.rb, line 134
def do_write(io) #:nodoc
  instantiate_all_objs
  @field_objs.each { |f| f.do_write(io) if include_obj?(f) }
end
each_pair() click to toggle source
# File lib/bindata/struct.rb, line 159
def each_pair
  @field_names.compact.each do |name|
    yield [name, find_obj_for_name(name)]
  end
end
field_names(include_hidden = false) click to toggle source

Returns a list of the names of all fields accessible through this object. include_hidden specifies whether to include hidden names in the listing.

# File lib/bindata/struct.rb, line 109
def field_names(include_hidden = false)
  if include_hidden
    @field_names.compact
  else
    hidden = get_parameter(:hide) || []
    @field_names.compact - hidden
  end
end
has_key?(key) click to toggle source
# File lib/bindata/struct.rb, line 155
def has_key?(key)
  @field_names.index(base_field_name(key))
end
initialize_instance() click to toggle source
# File lib/bindata/struct.rb, line 80
def initialize_instance
  @field_objs  = []
end
initialize_shared_instance() click to toggle source
# File lib/bindata/struct.rb, line 72
def initialize_shared_instance
  fields = get_parameter(:fields)
  @field_names = fields.field_names.freeze
  extend ByteAlignPlugin if fields.any_field_has_parameter?(:byte_align)
  define_field_accessors
  super
end
snapshot() click to toggle source
# File lib/bindata/struct.rb, line 97
def snapshot
  snapshot = Snapshot.new
  field_names.each do |name|
    obj = find_obj_for_name(name)
    snapshot[name] = obj.snapshot if include_obj?(obj)
  end
  snapshot
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.