Class: Cosmos::StructureItem
- Includes:
- Comparable
- Defined in:
- lib/cosmos/packets/structure_item.rb,
ext/cosmos/ext/structure/structure.c
Overview
Maintains knowledge of an item in a Structure. Multiple StructureItems compose a Structure.
Constant Summary collapse
- DATA_TYPES =
Valid data types adds :DERIVED to those defined by BinaryAccessor
BinaryAccessor::DATA_TYPES << :DERIVED
- LARGE_BUFFER_SIZE_BITS =
A large buffer size in bits (1 Megabyte)
1024 * 1024 * 8
Instance Attribute Summary collapse
-
#array_size ⇒ Integer?
The total number of bits in the binary buffer that create the array.
-
#bit_offset ⇒ Integer
Indicates where in the binary buffer the StructureItem exists.
-
#bit_size ⇒ Integer
The number of bits which represent this StructureItem in the binary buffer.
-
#data_type ⇒ Symbol
The data type is what kind of data this StructureItem represents when extracted from the binary buffer.
-
#endianness ⇒ Symbol
Used to interpret how to read the item from the binary data buffer.
-
#name ⇒ String
Name is used by higher level classes to access the StructureItem.
-
#overflow ⇒ Symbol
How to handle overflow for :INT, :UINT, :STRING, and :BLOCK data types Note: Has no meaning for :FLOAT data types.
Instance Method Summary collapse
-
#<=>(other_item) ⇒ Object
Comparison Operator based on bit_offset.
-
#clone ⇒ Object
(also: #dup)
Make a light weight clone of this item.
-
#initialize(name, bit_offset, bit_size, data_type, endianness, array_size = nil, overflow = :ERROR) ⇒ StructureItem
constructor
Create a StructureItem by setting all the attributes.
- #to_hash ⇒ Object
Constructor Details
#initialize(name, bit_offset, bit_size, data_type, endianness, array_size = nil, overflow = :ERROR) ⇒ StructureItem
Create a StructureItem by setting all the attributes. It calls all the setter routines to do the attribute verification and then verifies the overall integrity.
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/cosmos/packets/structure_item.rb', line 74 def initialize(name, bit_offset, bit_size, data_type, endianness, array_size = nil, overflow = :ERROR) @structure_item_constructed = false # Assignment order matters due to verifications! self.name = name self.endianness = endianness self.data_type = data_type self.bit_offset = bit_offset self.bit_size = bit_size self.array_size = array_size self.overflow = overflow @structure_item_constructed = true verify_overall() end |
Instance Attribute Details
#array_size ⇒ Integer?
The total number of bits in the binary buffer that create the array. The array size can be set to nil to indicate the StructureItem is not represented as an array. For example, if the bit_size is 8 bits, an array_size of 16 would result in two 8 bit items.
53 54 55 |
# File 'lib/cosmos/packets/structure_item.rb', line 53 def array_size @array_size end |
#bit_offset ⇒ Integer
Indicates where in the binary buffer the StructureItem exists.
29 30 31 |
# File 'lib/cosmos/packets/structure_item.rb', line 29 def bit_offset @bit_offset end |
#bit_size ⇒ Integer
The number of bits which represent this StructureItem in the binary buffer.
33 34 35 |
# File 'lib/cosmos/packets/structure_item.rb', line 33 def bit_size @bit_size end |
#data_type ⇒ Symbol
The data type is what kind of data this StructureItem represents when extracted from the binary buffer. :INT and :UINT are turned into Integers (Ruby Fixnum). :FLOAT are turned into floating point numbers (Ruby Float). :STRING is turned into an ASCII string (Ruby String). :BLOCK is turned into a binary buffer (Ruby String). :DERIVED is interpreted by the subclass and can result in any type.
42 43 44 |
# File 'lib/cosmos/packets/structure_item.rb', line 42 def data_type @data_type end |
#endianness ⇒ Symbol
Used to interpret how to read the item from the binary data buffer.
46 47 48 |
# File 'lib/cosmos/packets/structure_item.rb', line 46 def endianness @endianness end |
#name ⇒ String
Name is used by higher level classes to access the StructureItem.
25 26 27 |
# File 'lib/cosmos/packets/structure_item.rb', line 25 def name @name end |
#overflow ⇒ Symbol
How to handle overflow for :INT, :UINT, :STRING, and :BLOCK data types Note: Has no meaning for :FLOAT data types
58 59 60 |
# File 'lib/cosmos/packets/structure_item.rb', line 58 def overflow @overflow end |
Instance Method Details
#<=>(other_item) ⇒ Object
Comparison Operator based on bit_offset. This means that StructureItems with different names or bit sizes are equal if they have the same bit offset.
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 |
# File 'ext/cosmos/ext/structure/structure.c', line 693 static VALUE structure_item_spaceship(VALUE self, VALUE other_item) { int bit_offset = FIX2INT(rb_ivar_get(self, id_ivar_bit_offset)); int other_bit_offset = FIX2INT(rb_ivar_get(other_item, id_ivar_bit_offset)); int bit_size = 0; int other_bit_size = 0; /* Handle same bit offset case */ if ((bit_offset == 0) && (other_bit_offset == 0)) { /* Both bit_offsets are 0 so sort by bit_size * This allows derived items with bit_size of 0 to be listed first * Compare based on bit size */ bit_size = FIX2INT(rb_ivar_get(self, id_ivar_bit_size)); other_bit_size = FIX2INT(rb_ivar_get(other_item, id_ivar_bit_size)); if (bit_size == other_bit_size) { return INT2FIX(0); } if (bit_size < other_bit_size) { return INT2FIX(-1); } else { return INT2FIX(1); } } /* Handle different bit offsets */ if (((bit_offset >= 0) && (other_bit_offset >= 0)) || ((bit_offset < 0) && (other_bit_offset < 0))) { /* Both Have Same Sign */ if (bit_offset == other_bit_offset) { return INT2FIX(0); } else if (bit_offset < other_bit_offset) { return INT2FIX(-1); } else { return INT2FIX(1); } } else { /* Different Signs */ if (bit_offset == other_bit_offset) { return INT2FIX(0); } else if (bit_offset < other_bit_offset) { return INT2FIX(1); } else { return INT2FIX(-1); } } } |
#clone ⇒ Object Also known as: dup
Make a light weight clone of this item
180 181 182 183 184 |
# File 'lib/cosmos/packets/structure_item.rb', line 180 def clone item = super() item.name = self.name.clone if self.name item end |
#to_hash ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/cosmos/packets/structure_item.rb', line 187 def to_hash hash = {} hash['name'] = self.name hash['bit_offset'] = self.bit_offset hash['bit_size'] = self.bit_size hash['data_type'] = self.data_type hash['endianness'] = self.endianness hash['array_size'] = self.array_size hash['overflow'] = self.overflow hash end |