Class: BinData::Struct
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. 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.
:search_prefix
-
Allows abbreviated type names. If a type is unrecognised, then each prefix is applied until a match is found.
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
.
Direct Known Subclasses
Defined Under Namespace
Classes: Snapshot
Constant Summary collapse
- RESERVED =
These reserved words may not be used as field names
Hash[* (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} ).collect { |name| name.to_sym }. uniq.collect { |key| [key, true]
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #assign(val) ⇒ Object
-
#clear ⇒ Object
:nodoc:.
-
#clear? ⇒ Boolean
:nodoc:.
-
#debug_name_of(child) ⇒ Object
:nodoc:.
-
#do_num_bytes ⇒ Object
:nodoc:.
-
#do_read(io) ⇒ Object
:nodoc:.
-
#do_write(io) ⇒ Object
:nodoc.
- #each_pair ⇒ Object
-
#field_names(include_hidden = false) ⇒ Object
Returns a list of the names of all fields accessible through this object.
- #has_key?(key) ⇒ Boolean
- #initialize_instance ⇒ Object
- #initialize_shared_instance ⇒ Object
-
#offset_of(child) ⇒ Object
:nodoc:.
- #snapshot ⇒ Object
Methods inherited from Base
#==, #=~, #abs_offset, arg_processor, auto_call_delayed_io, bindata_name, #debug_name, #eval_parameter, #get_parameter, #has_parameter?, #initialize_with_warning, #inspect, #lazy_evaluator, #new, #num_bytes, #pretty_print, #read, read, register_subclasses, #rel_offset, #safe_respond_to?, #to_binary_s, #to_hex, #to_s, unregister_self, #write
Methods included from AcceptedParametersPlugin
#accepted_parameters, #default_parameters, #mandatory_parameters, #mutually_exclusive_parameters, #optional_parameters
Methods included from CheckOrAdjustOffsetPlugin
Methods included from RegisterNamePlugin
Methods included from Framework
Instance Method Details
#[](key) ⇒ Object
147 148 149 |
# File 'lib/bindata/struct.rb', line 147 def [](key) find_obj_for_name(key) end |
#[]=(key, value) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/bindata/struct.rb', line 151 def []=(key, value) obj = find_obj_for_name(key) if obj obj.assign(value) end end |
#assign(val) ⇒ Object
95 96 97 98 |
# File 'lib/bindata/struct.rb', line 95 def assign(val) clear assign_fields(val) end |
#clear ⇒ Object
:nodoc:
87 88 89 |
# File 'lib/bindata/struct.rb', line 87 def clear #:nodoc: @field_objs.each { |f| f.clear unless f.nil? } end |
#clear? ⇒ Boolean
:nodoc:
91 92 93 |
# File 'lib/bindata/struct.rb', line 91 def clear? #:nodoc: @field_objs.all? { |f| f.nil? or f.clear? } end |
#debug_name_of(child) ⇒ Object
:nodoc:
121 122 123 124 |
# File 'lib/bindata/struct.rb', line 121 def debug_name_of(child) #:nodoc: field_name = @field_names[find_index_of(child)] "#{debug_name}.#{field_name}" end |
#do_num_bytes ⇒ Object
:nodoc:
142 143 144 145 |
# File 'lib/bindata/struct.rb', line 142 def do_num_bytes #:nodoc: instantiate_all_objs sum_num_bytes_for_all_fields end |
#do_read(io) ⇒ Object
:nodoc:
132 133 134 135 |
# File 'lib/bindata/struct.rb', line 132 def do_read(io) #:nodoc: instantiate_all_objs @field_objs.each { |f| f.do_read(io) if include_obj?(f) } end |
#do_write(io) ⇒ Object
:nodoc
137 138 139 140 |
# File 'lib/bindata/struct.rb', line 137 def do_write(io) #:nodoc instantiate_all_objs @field_objs.each { |f| f.do_write(io) if include_obj?(f) } end |
#each_pair ⇒ Object
162 163 164 165 166 |
# File 'lib/bindata/struct.rb', line 162 def each_pair @field_names.compact.each do |name| yield [name, find_obj_for_name(name)] end 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.
112 113 114 115 116 117 118 119 |
# File 'lib/bindata/struct.rb', line 112 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) ⇒ Boolean
158 159 160 |
# File 'lib/bindata/struct.rb', line 158 def has_key?(key) @field_names.index(base_field_name(key)) end |
#initialize_instance ⇒ Object
83 84 85 |
# File 'lib/bindata/struct.rb', line 83 def initialize_instance @field_objs = [] end |
#initialize_shared_instance ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/bindata/struct.rb', line 75 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 |
#offset_of(child) ⇒ Object
:nodoc:
126 127 128 129 130 |
# File 'lib/bindata/struct.rb', line 126 def offset_of(child) #:nodoc: instantiate_all_objs sum = sum_num_bytes_below_index(find_index_of(child)) child.bit_aligned? ? sum.floor : sum.ceil end |