Class: BinData::BasePrimitive

Inherits:
Base
  • Object
show all
Defined in:
lib/bindata/base_primitive.rb,
lib/bindata/trace.rb,
lib/bindata/alignment.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.

:check_value

Raise an error unless the value read in 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.

Instance Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, #=~, arg_extractor, bindata_name, #debug_name, #debug_name_of, #eval_parameter, #get_parameter, #has_parameter?, #initialize_with_warning, #inspect, #lazy_evaluator, #new, #num_bytes, #offset, #offset_of, #pretty_print, #read, read, register_subclasses, #rel_offset, #safe_respond_to?, #to_binary_s, #to_s, unregister_self, #write

Methods included from CheckOrAdjustOffsetMixin

#do_read_with_adjust_offset, #do_read_with_check_offset, included

Methods included from AcceptedParametersMixin

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

:nodoc:



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

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



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

def self.bit_aligned
  include BitAligned
end

.turn_off_tracingObject



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

def turn_off_tracing
  alias_method :hook_after_do_read, :null_method
end

.turn_on_tracingObject



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

def turn_on_tracing
  alias_method :hook_after_do_read, :trace_value
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#assign(val) ⇒ Object Also known as: value=

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bindata/base_primitive.rb', line 84

def assign(val)
  raise ArgumentError, "can't set a nil value for #{debug_name}" if val.nil?

  unless has_parameter?(:value)
    raw_val = val.respond_to?(:snapshot) ? val.snapshot : val
    @value = begin
               raw_val.dup
             rescue TypeError
               # can't dup Fixnums
               raw_val
             end
  end
end

#clearObject

:nodoc:



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

def clear #:nodoc:
  @value = nil
end

#clear?Boolean

:nodoc:

Returns:

  • (Boolean)


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

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

#do_num_bytesObject

:nodoc:



148
149
150
# File 'lib/bindata/base_primitive.rb', line 148

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

#do_read(io) ⇒ Object

:nodoc:



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

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

#do_read_with_check_value(io) ⇒ Object

:nodoc:



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

def do_read_with_check_value(io) #:nodoc:
  do_read_without_check_value(io)
  check_value(snapshot)
end

#do_write(io) ⇒ Object

:nodoc:



144
145
146
# File 'lib/bindata/base_primitive.rb', line 144

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hashObject



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

def hash
  snapshot.hash
end

#initialize_instanceObject



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

def initialize_instance
  @value = nil
end

#initialize_shared_instanceObject



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

def initialize_shared_instance
  if has_parameter?(:check_value)
    class << self
      alias_method :do_read_without_check_value, :do_read
      alias_method :do_read, :do_read_with_check_value
    end
  end
  if has_parameter?(:value)
    class << self
      alias_method :_value, :_value_with_value
    end
  end
  if has_parameter?(:initial_value)
    class << self
      alias_method :_value, :_value_with_initial_value
    end
  end
end

#respond_to?(symbol, include_private = false) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


107
108
109
110
# File 'lib/bindata/base_primitive.rb', line 107

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

#snapshotObject



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

def snapshot
  _value
end

#valueObject



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

def value
  snapshot
end