class BinData::BasePrimitive
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
:value
param. :assert
-
Raise an error unless the value read or assigned meets this criteria. The variable
value
is 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
:assert
and:value
.
Public Class Methods
# File lib/bindata/alignment.rb, line 72 def BasePrimitive.bit_aligned include BitAligned end
# File lib/bindata/trace.rb, line 53 def turn_off_tracing alias_method :do_read, :do_read_without_hook end
# 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
Public Instance Methods
# File lib/bindata/base_primitive.rb, line 115 def <=>(other) snapshot <=> other end
# 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
# File lib/bindata/trace.rb, line 58 def do_read_with_hook(io) do_read_without_hook(io) trace_value end
# File lib/bindata/base_primitive.rb, line 119 def eql?(other) # double dispatch other.eql?(snapshot) end
# File lib/bindata/base_primitive.rb, line 124 def hash snapshot.hash end
# File lib/bindata/base_primitive.rb, line 64 def initialize_instance @value = nil end
# File lib/bindata/base_primitive.rb, line 85 def snapshot _value end
# File lib/bindata/trace.rb, line 63 def trace_value BinData.trace_message do |tracer| value_string = _value.inspect tracer.trace_obj(debug_name, value_string) end end
# File lib/bindata/base_primitive.rb, line 89 def value snapshot end
# File lib/bindata/base_primitive.rb, line 93 def value=(val) assign(val) end
Private Instance Methods
Read a number of bytes from io
and return the value they
represent.
# File lib/bindata/base_primitive.rb, line 236 def read_and_return_value(io) raise NotImplementedError end
Return a sensible default for this data.
# File lib/bindata/base_primitive.rb, line 241 def sensible_default raise NotImplementedError end
Return the string representation that val
will take when
written.
# File lib/bindata/base_primitive.rb, line 231 def value_to_binary_string(val) raise NotImplementedError end