lib/bindata/base.rb in bindata-1.2.1 vs lib/bindata/base.rb in bindata-1.2.2
- old
+ new
@@ -41,10 +41,15 @@
data = self.new
data.read(io)
data
end
+ # The name of this class as used by Records, Arrays etc.
+ def bindata_name
+ RegisteredClasses.underscore_name(self.name)
+ end
+
# Registers this class for use.
def register_self
register_class(self)
end
@@ -72,10 +77,12 @@
# +parent+ is the parent data object (e.g. struct, array, choice) this
# object resides under.
def initialize(parameters = {}, parent = nil)
@params = Sanitizer.sanitize(parameters, self.class)
@parent = parent
+
+ prepare_for_read_with_offset
end
attr_reader :parent
# Returns the result of evaluating the parameter identified by +key+.
@@ -102,61 +109,43 @@
# Reads data into this data object.
def read(io)
io = BinData::IO.new(io) unless BinData::IO === io
+ @in_read = true
+ clear
do_read(io)
- done_read
+ @in_read = false
+
self
end
- def do_read(io) #:nodoc:
- check_or_adjust_offset(io)
- clear
- _do_read(io)
- end
+ #:nodoc:
+ attr_reader :in_read
+ protected :in_read
- def done_read #:nodoc:
- _done_read
+ # Returns if this object is currently being read. This is used
+ # internally by BasePrimitive.
+ def reading? #:nodoc:
+ furthest_ancestor.in_read
end
- protected :do_read, :done_read
+ protected :reading?
# Writes the value for this data object to +io+.
def write(io)
io = BinData::IO.new(io) unless BinData::IO === io
do_write(io)
io.flush
self
end
- def do_write(io) #:nodoc:
- _do_write(io)
- end
- protected :do_write
-
# Returns the number of bytes it will take to write this data object.
def num_bytes
do_num_bytes.ceil
end
- def do_num_bytes #:nodoc:
- _do_num_bytes
- end
- protected :do_num_bytes
-
- # Assigns the value of +val+ to this data object. Note that +val+ will
- # always be deep copied to ensure no aliasing problems can occur.
- def assign(val)
- _assign(val)
- end
-
- # Returns a snapshot of this data object.
- def snapshot
- _snapshot
- end
-
# Returns the string representation of this data object.
def to_binary_s
io = BinData::IO.create_string_io
write(io)
io.rewind
@@ -211,10 +200,35 @@
end
#---------------
private
+ def furthest_ancestor
+ if parent.nil?
+ self
+ else
+ an = parent
+ an = an.parent while an.parent
+ an
+ end
+ end
+
+ def prepare_for_read_with_offset
+ if has_parameter?(:check_offset) or has_parameter?(:adjust_offset)
+ class << self
+ alias_method :do_read_without_offset, :do_read
+ alias_method :do_read, :do_read_with_offset
+ public :do_read # Ruby 1.9.2 bug. Should be protected
+ end
+ end
+ end
+
+ def do_read_with_offset(io) #:nodoc:
+ check_or_adjust_offset(io)
+ do_read_without_offset(io)
+ end
+
def check_or_adjust_offset(io)
if has_parameter?(:check_offset)
check_offset(io)
elsif has_parameter?(:adjust_offset)
adjust_offset(io)
@@ -266,10 +280,21 @@
# Returns true if the object has not been changed since creation.
def clear?
raise NotImplementedError
end
+ # Assigns the value of +val+ to this data object. Note that +val+ will
+ # always be deep copied to ensure no aliasing problems can occur.
+ def assign(val)
+ raise NotImplementedError
+ end
+
+ # Returns a snapshot of this data object.
+ def snapshot
+ raise NotImplementedError
+ end
+
# Returns the debug name of +child+. This only needs to be implemented
# by objects that contain child objects.
def debug_name_of(child) #:nodoc:
debug_name
end
@@ -279,42 +304,26 @@
def offset_of(child) #:nodoc:
0
end
# Reads the data for this data object from +io+.
- def _do_read(io)
+ def do_read(io) #:nodoc:
raise NotImplementedError
end
- # Trigger function that is called after #do_read.
- def _done_read
- raise NotImplementedError
- end
-
# Writes the value for this data to +io+.
- def _do_write(io)
+ def do_write(io) #:nodoc:
raise NotImplementedError
end
# Returns the number of bytes it will take to write this data.
- def _do_num_bytes
+ def do_num_bytes #:nodoc:
raise NotImplementedError
end
- # Assigns the value of +val+ to this data object. Note that +val+ will
- # always be deep copied to ensure no aliasing problems can occur.
- def _assign(val)
- raise NotImplementedError
- end
-
- # Returns a snapshot of this data object.
- def _snapshot
- raise NotImplementedError
- end
-
# Set visibility requirements of methods to implement
- public :clear, :clear?, :debug_name_of, :offset_of
- private :_do_read, :_done_read, :_do_write, :_do_num_bytes, :_assign, :_snapshot
+ public :clear, :clear?, :assign, :snapshot, :debug_name_of, :offset_of
+ protected :do_read, :do_write, :do_num_bytes
# End To be implemented by subclasses
###########################################################################
end
end