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(:check_value => 3)
obj.read("\005") #=> BinData::ValidityError: value is '5' but expected '3'

obj = BinData::Uint8.new(:check_value => lambda { 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

Equavalent to :assert and :value.

Defined Under Namespace

Modules: AssertPlugin, AssertedValuePlugin, CheckValuePlugin, InitialValuePlugin, ValuePlugin

Instance Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, #=~, arg_extractor, bindata_name, #clear, #debug_name, #eval_parameter, #get_parameter, #has_parameter?, #initialize_with_warning, #inspect, #lazy_evaluator, #new, #num_bytes, #offset, #pretty_print, #read, read, register_subclasses, #rel_offset, #safe_respond_to?, #to_binary_s, #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 CheckOrAdjustOffsetPlugin

included

Methods included from Framework

#debug_name_of, included, #offset_of

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

:nodoc:



110
111
112
113
114
115
116
117
# File 'lib/bindata/base_primitive.rb', line 110

def method_missing(symbol, *args, &block) #:nodoc:
  child = snapshot
  if child.respond_to?(symbol)
    child.__send__(symbol, *args, &block)
  else
    super
  end
end

Class Method Details

.bit_alignedObject



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

def BasePrimitive.bit_aligned
  include BitAligned
end

.turn_off_tracingObject



52
53
54
# File 'lib/bindata/trace.rb', line 52

def turn_off_tracing
  alias_method :do_read, :do_read_without_hook
end

.turn_on_tracingObject



47
48
49
50
# File 'lib/bindata/trace.rb', line 47

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



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

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

#assign(val) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bindata/base_primitive.rb', line 81

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)


77
78
79
# File 'lib/bindata/base_primitive.rb', line 77

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

#do_num_bytesObject

:nodoc:



140
141
142
# File 'lib/bindata/base_primitive.rb', line 140

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

#do_read(io) ⇒ Object

:nodoc:



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

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

#do_read_with_hook(io) ⇒ Object



57
58
59
60
# File 'lib/bindata/trace.rb', line 57

def do_read_with_hook(io)
  do_read_without_hook(io)
  trace_value
end

#do_write(io) ⇒ Object

:nodoc:



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hashObject



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

def hash
  snapshot.hash
end

#initialize_instanceObject



73
74
75
# File 'lib/bindata/base_primitive.rb', line 73

def initialize_instance
  @value = nil
end

#initialize_shared_instanceObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bindata/base_primitive.rb', line 58

def initialize_shared_instance
  if has_parameter?(:check_value) and has_parameter?(:value)
    warn ":check_value has been deprecated.  Consider using :asserted_value instead of :check_value and :value in #{self.class}."
  elsif has_parameter?(:check_value)
    warn ":check_value has been deprecated.  Use :assert instead in #{self.class}."
  end

  extend InitialValuePlugin  if has_parameter?(:initial_value)
  extend ValuePlugin         if has_parameter?(:value)
  extend CheckValuePlugin    if has_parameter?(:check_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)


105
106
107
108
# File 'lib/bindata/base_primitive.rb', line 105

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

#snapshotObject



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

def snapshot
  _value
end

#trace_valueObject



62
63
64
65
66
67
# File 'lib/bindata/trace.rb', line 62

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

#valueObject



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

def value
  snapshot
end

#value=(val) ⇒ Object



101
102
103
# File 'lib/bindata/base_primitive.rb', line 101

def value=(val)
  assign(val)
end