Class: BinData::BasePrimitive

Inherits:
Base
  • Object
show all
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 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

included

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:



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bindata/base_primitive.rb', line 102

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_alignedObject



72
73
74
# File 'lib/bindata/alignment.rb', line 72

def BasePrimitive.bit_aligned
  include BitAligned
end

.turn_off_tracingObject



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_tracingObject



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



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

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

#assign(val) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
80
81
82
83
# 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:

Returns:

  • (Boolean)


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

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

#do_num_bytesObject

:nodoc:



136
137
138
# File 'lib/bindata/base_primitive.rb', line 136

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

#do_read(io) ⇒ Object

:nodoc:



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

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:



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hashObject



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

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

:nodoc:

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/bindata/base_primitive.rb', line 97

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

#snapshotObject



85
86
87
# File 'lib/bindata/base_primitive.rb', line 85

def snapshot
  _value
end

#trace_valueObject



63
64
65
66
67
68
# 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

#valueObject



89
90
91
# File 'lib/bindata/base_primitive.rb', line 89

def value
  snapshot
end

#value=(val) ⇒ Object



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

def value=(val)
  assign(val)
end