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.
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 |
# File 'ext/cosmos/ext/structure/structure.c', line 1187
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 |