Class: BinData::BasePrimitive

Inherits:
Base
  • Object
show all
Extended by:
TraceHook
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 :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.

Defined Under Namespace

Modules: AssertPlugin, AssertedValuePlugin, InitialValuePlugin, ValuePlugin

Instance Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TraceHook

turn_off_tracing, turn_on_tracing

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 RegisterNamePlugin

included

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:



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bindata/base_primitive.rb', line 96

def method_missing(symbol, *args, &block) # :nodoc:
  child = snapshot
  if child.respond_to?(symbol)
    self.class.class_eval <<-END, __FILE__, __LINE__ + 1
      def #{symbol}(*args, &block)         # def clamp(*args, &block)
        snapshot.#{symbol}(*args, &block)  #   snapshot.clamp(*args, &block)
      end                                  # end
    END
    child.__send__(symbol, *args, &block)
  else
    super
  end
end

Class Method Details

.bit_alignedObject



80
81
82
# File 'lib/bindata/alignment.rb', line 80

def BasePrimitive.bit_aligned
  include BitAligned
end

Instance Method Details

#<=>(other) ⇒ Object



110
111
112
# File 'lib/bindata/base_primitive.rb', line 110

def <=>(other)
  snapshot <=> other
end

#assign(val) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
76
77
# 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 = raw_val.dup
end

#clear?Boolean

:nodoc:

Returns:

  • (Boolean)


68
69
70
# File 'lib/bindata/base_primitive.rb', line 68

def clear? # :nodoc:
  @value.nil?
end

#do_num_bytesObject

:nodoc:



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

def do_num_bytes # :nodoc:
  value_to_binary_string(_value).length
end

#do_read(io) ⇒ Object

:nodoc:



123
124
125
# File 'lib/bindata/base_primitive.rb', line 123

def do_read(io) # :nodoc:
  @value = read_and_return_value(io)
end

#do_read_with_hook(io) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/bindata/trace.rb', line 66

def do_read_with_hook(io)
  do_read_without_hook(io)

  BinData.trace_message do |tracer|
    value_string = _value.inspect
    tracer.trace_obj(debug_name, value_string)
  end
end

#do_write(io) ⇒ Object

:nodoc:



127
128
129
# File 'lib/bindata/base_primitive.rb', line 127

def do_write(io) # :nodoc:
  io.writebytes(value_to_binary_string(_value))
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
# File 'lib/bindata/base_primitive.rb', line 114

def eql?(other)
  # double dispatch
  other.eql?(snapshot)
end

#hashObject



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

def hash
  snapshot.hash
end

#initialize_instanceObject



64
65
66
# File 'lib/bindata/base_primitive.rb', line 64

def initialize_instance
  @value = nil
end

#initialize_shared_instanceObject



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_missing?(symbol, include_all = false) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/bindata/base_primitive.rb', line 91

def respond_to_missing?(symbol, include_all = false) # :nodoc:
  child = snapshot
  child.respond_to?(symbol, include_all) || super
end

#snapshotObject



79
80
81
# File 'lib/bindata/base_primitive.rb', line 79

def snapshot
  _value
end

#valueObject



83
84
85
# File 'lib/bindata/base_primitive.rb', line 83

def value
  snapshot
end

#value=(val) ⇒ Object



87
88
89
# File 'lib/bindata/base_primitive.rb', line 87

def value=(val)
  assign(val)
end