Class: BinData::Struct

Inherits:
Base
  • Object
show all
Defined in:
lib/bindata/struct.rb,
lib/bindata/deprecated.rb

Overview

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.

: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.

Direct Known Subclasses

Record

Defined Under Namespace

Classes: Snapshot

Constant Summary collapse

RESERVED =

These reserved words may not be used as field names

(::Hash.instance_methods + 
%w{alias and begin break case class def defined do else elsif
   end ensure false for if in module next nil not or redo
   rescue retry return self super then true undef unless until
   when while yield} +
%w{array element index value} ).uniq

Instance Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, accepted_parameters, #assign, #debug_name, default_parameters, #eval_parameter, #get_parameter, #has_parameter?, #inspect, mandatory_parameters, mutually_exclusive_parameters, #num_bytes, #offset, optional_parameters, #read, read, #single_value?, #snapshot, #to_binary_s, #to_s, #write

Constructor Details

#initialize(params = {}, parent = nil) ⇒ Struct

Returns a new instance of Struct.



142
143
144
145
146
147
# File 'lib/bindata/struct.rb', line 142

def initialize(params = {}, parent = nil)
  super(params, parent)

  @field_names = get_parameter(:fields).field_names
  @field_objs  = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/bindata/struct.rb', line 174

def method_missing(symbol, *args, &block)
  obj = find_obj_for_name(symbol)
  if obj
    invoke_field(obj, symbol, args)
  else
    super
  end
end

Class Method Details

.inherited(subclass) ⇒ Object

:nodoc:



100
101
102
103
104
# File 'lib/bindata/deprecated.rb', line 100

def inherited(subclass) #:nodoc:
  if subclass != Record
    fail "error: inheriting from BinData::Struct has been deprecated. Inherit from BinData::Record instead."
  end
end

.sanitize_parameters!(params, sanitizer) ⇒ Object



72
73
74
75
76
# File 'lib/bindata/struct.rb', line 72

def sanitize_parameters!(params, sanitizer)
  sanitize_endian(params, sanitizer)
  sanitize_fields(params, sanitizer)
  sanitize_hide(params, sanitizer)
end

Instance Method Details

#_do_num_bytes(name) ⇒ Object

:nodoc:



130
131
132
133
# File 'lib/bindata/deprecated.rb', line 130

def _do_num_bytes(deprecated)
  instantiate_all_objs
  sum_num_bytes_for_all_fields.ceil
end

#clear(name = nil) ⇒ Object

:nodoc:



108
109
110
# File 'lib/bindata/deprecated.rb', line 108

def clear
  @field_objs.each { |f| f.clear unless f.nil? }
end

#clear?(name = nil) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


119
120
121
# File 'lib/bindata/deprecated.rb', line 119

def clear?
  @field_objs.inject(true) { |all_clear, f| all_clear and (f.nil? or f.clear?) }
end

#debug_name_of(child) ⇒ Object



183
184
185
186
# File 'lib/bindata/struct.rb', line 183

def debug_name_of(child)
  field_name = @field_names[find_index_of(child)]
  "#{debug_name}.#{field_name}"
end

#field_names(include_hidden = false) ⇒ Object

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



160
161
162
163
164
165
166
167
# File 'lib/bindata/struct.rb', line 160

def field_names(include_hidden = false)
  if include_hidden
    @field_names.dup
  else
    hidden = get_parameter(:hide) || []
    @field_names - hidden
  end
end

#offset_of(child) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/bindata/struct.rb', line 188

def offset_of(child)
  instantiate_all_objs
  sum = sum_num_bytes_below_index(find_index_of(child))
  child_offset = child.do_num_bytes.is_a?(Integer) ? sum.ceil : sum.floor

  offset + child_offset
end

#orig__do_num_bytesObject



129
130
131
132
# File 'lib/bindata/deprecated.rb', line 129

def _do_num_bytes(deprecated)
  instantiate_all_objs
  sum_num_bytes_for_all_fields.ceil
end

#orig_clearObject



107
108
109
# File 'lib/bindata/deprecated.rb', line 107

def clear
  @field_objs.each { |f| f.clear unless f.nil? }
end

#orig_clear?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/bindata/deprecated.rb', line 118

def clear?
  @field_objs.inject(true) { |all_clear, f| all_clear and (f.nil? or f.clear?) }
end

#orig_offset_ofObject



140
141
142
143
144
145
146
# File 'lib/bindata/deprecated.rb', line 140

def offset_of(child)
  instantiate_all_objs
  sum = sum_num_bytes_below_index(find_index_of(child))
  child_offset = child.do_num_bytes.is_a?(Integer) ? sum.ceil : sum.floor

  offset + child_offset
end

#respond_to?(symbol, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
172
# File 'lib/bindata/struct.rb', line 169

def respond_to?(symbol, include_private = false)
  super(symbol, include_private) ||
    field_names(true).include?(symbol.to_s.chomp("="))
end