Class: BinData::BasePrimitive
- Defined in:
- lib/bindata/base_primitive.rb,
lib/bindata/trace.rb
Overview
A BinData::BasePrimitive object is a container for a value that has a particular binary representation. A value corresponds to a primitive type such as as integer, float or string. Only one value can be contained by this object. This value can be read from or written to an IO stream.
require 'bindata'
obj = BinData::Uint8.new(initial_value: 42)
obj #=> 42
obj.assign(5)
obj #=> 5
obj.clear
obj #=> 42
obj = BinData::Uint8.new(value: 42)
obj #=> 42
obj.assign(5)
obj #=> 42
obj = BinData::Uint8.new(assert: 3)
obj.read("\005") #=> BinData::ValidityError: value is '5' but expected '3'
obj = BinData::Uint8.new(assert: -> { value < 5 })
obj.read("\007") #=> BinData::ValidityError: value not as expected
Parameters
Parameters may be provided at initialisation to control the behaviour of an object. These params include those for BinData::Base as well as:
:initial_value-
This is the initial value to use before one is either #read or explicitly set with #value=.
:value-
The object will always have this value. Calls to #value= are ignored when using this param. While reading, #value will return the value of the data read from the IO, not the result of the
:valueparam. :assert-
Raise an error unless the value read or assigned meets this criteria. The variable
valueis made available to any lambda assigned to this parameter. A boolean return indicates success or failure. Any other return is compared to the value just read in. :asserted_value-
Equivalent to
:assertand:value.
Direct Known Subclasses
CountBytesRemaining, DoubleBe, DoubleLe, FloatBe, FloatLe, Int8, Primitive, Rest, Skip, String, Stringz, Uint8, Virtual
Defined Under Namespace
Modules: AssertPlugin, AssertedValuePlugin, InitialValuePlugin, ValuePlugin
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #assign(val) ⇒ Object
-
#clear? ⇒ Boolean
:nodoc:.
-
#do_num_bytes ⇒ Object
:nodoc:.
-
#do_read(io) ⇒ Object
:nodoc:.
- #do_read_with_hook(io) ⇒ Object
-
#do_write(io) ⇒ Object
:nodoc:.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #initialize_instance ⇒ Object
- #initialize_shared_instance ⇒ Object
-
#method_missing(symbol, *args, &block) ⇒ Object
:nodoc:.
-
#respond_to?(symbol, include_private = false) ⇒ Boolean
:nodoc:.
- #snapshot ⇒ Object
- #trace_value ⇒ Object
- #value ⇒ Object
- #value=(val) ⇒ Object
Methods inherited from Base
#==, #=~, #abs_offset, arg_processor, auto_call_delayed_io, bindata_name, #clear, #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
#bit_aligned?, #debug_name_of, #offset_of
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
:nodoc:
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/bindata/base_primitive.rb', line 101 def method_missing(symbol, *args, &block) #:nodoc: child = snapshot if child.respond_to?(symbol) self.class.class_eval "def #{symbol}(*args, &block);" \ " snapshot.#{symbol}(*args, &block);" \ "end" child.__send__(symbol, *args, &block) else super end end |
Class Method Details
.bit_aligned ⇒ Object
72 73 74 |
# File 'lib/bindata/alignment.rb', line 72 def BasePrimitive.bit_aligned include BitAligned end |
.turn_off_tracing ⇒ Object
53 54 55 |
# File 'lib/bindata/trace.rb', line 53 def turn_off_tracing alias_method :do_read, :do_read_without_hook end |
.turn_on_tracing ⇒ Object
48 49 50 51 |
# File 'lib/bindata/trace.rb', line 48 def turn_on_tracing alias_method :do_read_without_hook, :do_read alias_method :do_read, :do_read_with_hook end |
Instance Method Details
#<=>(other) ⇒ Object
113 114 115 |
# File 'lib/bindata/base_primitive.rb', line 113 def <=>(other) snapshot <=> other end |
#assign(val) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/bindata/base_primitive.rb', line 72 def assign(val) raise ArgumentError, "can't set a nil value for #{debug_name}" if val.nil? raw_val = val.respond_to?(:snapshot) ? val.snapshot : val @value = begin raw_val.dup rescue TypeError # can't dup Fixnums raw_val end end |
#clear? ⇒ Boolean
:nodoc:
68 69 70 |
# File 'lib/bindata/base_primitive.rb', line 68 def clear? #:nodoc: @value.nil? end |
#do_num_bytes ⇒ Object
:nodoc:
134 135 136 |
# File 'lib/bindata/base_primitive.rb', line 134 def do_num_bytes #:nodoc: value_to_binary_string(_value).length end |
#do_read(io) ⇒ Object
:nodoc:
126 127 128 |
# File 'lib/bindata/base_primitive.rb', line 126 def do_read(io) #:nodoc: @value = read_and_return_value(io) end |
#do_read_with_hook(io) ⇒ Object
58 59 60 61 |
# File 'lib/bindata/trace.rb', line 58 def do_read_with_hook(io) do_read_without_hook(io) trace_value end |
#do_write(io) ⇒ Object
:nodoc:
130 131 132 |
# File 'lib/bindata/base_primitive.rb', line 130 def do_write(io) #:nodoc: io.writebytes(value_to_binary_string(_value)) end |
#eql?(other) ⇒ Boolean
117 118 119 120 |
# File 'lib/bindata/base_primitive.rb', line 117 def eql?(other) # double dispatch other.eql?(snapshot) end |
#hash ⇒ Object
122 123 124 |
# File 'lib/bindata/base_primitive.rb', line 122 def hash snapshot.hash end |
#initialize_instance ⇒ Object
64 65 66 |
# File 'lib/bindata/base_primitive.rb', line 64 def initialize_instance @value = nil end |
#initialize_shared_instance ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/bindata/base_primitive.rb', line 56 def initialize_shared_instance extend InitialValuePlugin if has_parameter?(:initial_value) extend ValuePlugin if has_parameter?(:value) extend AssertPlugin if has_parameter?(:assert) extend AssertedValuePlugin if has_parameter?(:asserted_value) super end |
#respond_to?(symbol, include_private = false) ⇒ Boolean
:nodoc:
96 97 98 99 |
# File 'lib/bindata/base_primitive.rb', line 96 def respond_to?(symbol, include_private = false) #:nodoc: child = snapshot child.respond_to?(symbol, include_private) || super end |
#snapshot ⇒ Object
84 85 86 |
# File 'lib/bindata/base_primitive.rb', line 84 def snapshot _value end |
#trace_value ⇒ Object
63 64 65 66 67 68 |
# File 'lib/bindata/trace.rb', line 63 def trace_value BinData. do |tracer| value_string = _value.inspect tracer.trace_obj(debug_name, value_string) end end |
#value ⇒ Object
88 89 90 |
# File 'lib/bindata/base_primitive.rb', line 88 def value snapshot end |
#value=(val) ⇒ Object
92 93 94 |
# File 'lib/bindata/base_primitive.rb', line 92 def value=(val) assign(val) end |