Class: CA::Struct
- Inherits:
-
Object
- Object
- CA::Struct
- Includes:
- Enumerable
- Defined in:
- lib/carray/base/struct.rb,
lib/carray/base/struct.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Builder
Class Method Summary collapse
- .[](*argv) ⇒ Object
-
.decode(data) ⇒ Object
required element as data class.
- .inspect ⇒ Object
- .members ⇒ Object
- .size ⇒ Object
- .spec ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](name) ⇒ Object
- #[]=(name, val) ⇒ Object
- #decode(data) ⇒ Object
- #each ⇒ Object
- #each_pair ⇒ Object
-
#encode ⇒ Object
(also: #to_s)
required element as data class.
-
#initialize(*argv) ⇒ Struct
constructor
A new instance of Struct.
- #inspect ⇒ Object
- #length ⇒ Object (also: #size)
- #members ⇒ Object
- #spec ⇒ Object
- #swap_bytes ⇒ Object
- #swap_bytes! ⇒ Object
- #to_ptr ⇒ Object
- #values ⇒ Object (also: #to_a)
- #values_at(*names) ⇒ Object
Constructor Details
#initialize(*argv) ⇒ Struct
Returns a new instance of Struct.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/carray/base/struct.rb', line 125 def initialize (*argv) @data = CScalar.new(self.class) mems = members if argv.size == 1 and argv.first.is_a?(Hash) argv.first.each do |k,v| self[k] = v end elsif argv.size <= mems.size argv.each_with_index do |v, i| self[mems[i]] = v end else raise ArgumentError, format("too many arguments for %s.new (<%i> for <%i>)", self.class.inspect, argv.size, members.size) end end |
Class Method Details
.[](*argv) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/carray/base/struct.rb', line 103 def [] (*argv) obj = new() members.each do |name| obj[name] = argv.shift end return obj end |
.decode(data) ⇒ Object
required element as data class
115 116 117 |
# File 'lib/carray/base/struct.rb', line 115 def decode (data) ### required element as data class return new.decode(data) end |
.inspect ⇒ Object
99 100 101 |
# File 'lib/carray/base/struct.rb', line 99 def inspect return name.empty? ? "AnonStruct" : name end |
.members ⇒ Object
111 112 113 |
# File 'lib/carray/base/struct.rb', line 111 def members return self::MEMBERS.clone end |
.size ⇒ Object
119 120 121 |
# File 'lib/carray/base/struct.rb', line 119 def size return self::DATA_SIZE end |
.spec ⇒ Object
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
# File 'lib/carray/base/struct.rb', line 561 def self.spec output = "" table = self::MEMBER_TABLE stlist = [] if self.name.empty? if self <= CA::Union prefix = "union" else prefix = "struct" end output << sprintf("%s_%i = ", prefix, [object_id].pack("V").unpack("V").first) else output << sprintf("%s = ", self.name) end if self < CA::Union output << sprintf("CA.union(:size=>%i) {\n", self::DATA_SIZE) else output << sprintf("CA.struct(:size=>%i) {\n", self::DATA_SIZE) end members.each do |member| offset, type, option = *table[member] case type when Class if type < CA::Struct stlist << type if type.name.empty? if type <= CA::Union prefix = "union" else prefix = "struct" end output << sprintf(" member %s_%i, :%s, :offset=>%i\n", prefix, [type.object_id].pack("V").unpack("V").first, member, offset) else output << sprintf(" member %s, :%s, :offset=>%i\n", type.name, member, offset) end else raise "unknown type" end when CArray output << sprintf(" member %s, :%s, :offset=>%i\n", type.spec, member, offset) when :fixlen output << sprintf(" member :fixlen, :%s, :bytes=>%i, :offset=>%i\n", member, option[:bytes], offset) else output << sprintf(" member :%s, :%s, :offset=>%i\n", type, member, offset) end end output << sprintf("}\n") if stlist.empty? return output else stlist.uniq! preface = "" stlist.each do |st| preface << st.spec end return preface + output end end |
Instance Method Details
#==(other) ⇒ Object
225 226 227 228 229 230 231 232 |
# File 'lib/carray/base/struct.rb', line 225 def == (other) case other when self.class return @data == other.__data__ else return false end end |
#[](name) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/carray/base/struct.rb', line 151 def [] (name) if name.kind_of?(Integer) name = members[name] end offset, type, opts = *self.class::MEMBER_TABLE[name.to_s] case type when nil return send(name) when Class return type.decode(@data.field(offset, type)) when CArray return @data.field(offset,type)[0,false] else return @data.field(offset,type,opts)[0] end end |
#[]=(name, val) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/carray/base/struct.rb', line 168 def []= (name, val) if name.kind_of?(Integer) name = members[name] end offset, type, opts = *self.class::MEMBER_TABLE[name.to_s] case type when nil send(name.to_s + "=", val) when Class @data.field(offset, type)[0] = val when CArray @data.field(offset, type)[0,false] = val else @data.field(offset,type,opts)[0] = val end end |
#decode(data) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/carray/base/struct.rb', line 234 def decode (data) case data when String @data.load_binary(data) when CArray @data = data else raise "unkown data to decode" end return self end |
#each ⇒ Object
185 186 187 188 189 |
# File 'lib/carray/base/struct.rb', line 185 def each members.each do |name| yield(self[name]) end end |
#each_pair ⇒ Object
191 192 193 194 195 |
# File 'lib/carray/base/struct.rb', line 191 def each_pair members.each do |name| yield(name.intern, self[name]) end end |
#encode ⇒ Object Also known as: to_s
required element as data class
246 247 248 |
# File 'lib/carray/base/struct.rb', line 246 def encode ### required element as data class return @data.dump_binary end |
#inspect ⇒ Object
217 218 219 220 221 222 223 |
# File 'lib/carray/base/struct.rb', line 217 def inspect table = {} members.each do |key| table[key] = self[key] end return ["<", self.class.inspect, " ", table.inspect[1..-2], ">"].join end |
#length ⇒ Object Also known as: size
197 198 199 |
# File 'lib/carray/base/struct.rb', line 197 def length return self.class::MEMBERS.length end |
#members ⇒ Object
203 204 205 |
# File 'lib/carray/base/struct.rb', line 203 def members return self.class::MEMBERS.clone end |
#spec ⇒ Object
628 629 630 |
# File 'lib/carray/base/struct.rb', line 628 def spec return self.class.spec end |
#swap_bytes ⇒ Object
257 258 259 |
# File 'lib/carray/base/struct.rb', line 257 def swap_bytes return self.class.decode(@data.swap_bytes.dump_binary) end |
#swap_bytes! ⇒ Object
252 253 254 255 |
# File 'lib/carray/base/struct.rb', line 252 def swap_bytes! @data.swap_bytes! return self end |
#to_ptr ⇒ Object
261 262 263 |
# File 'lib/carray/base/struct.rb', line 261 def to_ptr return @data.to_ptr end |
#values ⇒ Object Also known as: to_a
207 208 209 |
# File 'lib/carray/base/struct.rb', line 207 def values return members.map{|name| self[name] } end |
#values_at(*names) ⇒ Object
213 214 215 |
# File 'lib/carray/base/struct.rb', line 213 def values_at (*names) return names.map{|name| self[name] } end |