Class: CAStruct::Builder
Overview
Struct Builder Class
Defined Under Namespace
Classes: Member
Constant Summary collapse
- ALIGN_TABLE =
{ CA_FIXLEN => CA_ALIGN_FIXLEN, CA_BOOLEAN => CA_ALIGN_INT8, CA_INT8 => CA_ALIGN_INT8, CA_UINT8 => CA_ALIGN_INT8, CA_INT16 => CA_ALIGN_INT16, CA_UINT16 => CA_ALIGN_INT16, CA_INT32 => CA_ALIGN_INT32, CA_UINT32 => CA_ALIGN_INT32, CA_INT64 => CA_ALIGN_INT64, CA_UINT64 => CA_ALIGN_INT64, CA_FLOAT32 => CA_ALIGN_FLOAT32, CA_FLOAT64 => CA_ALIGN_FLOAT64, CA_CMPLX64 => CA_ALIGN_FLOAT32, CA_CMPLX128 => CA_ALIGN_FLOAT64, }
- CARRAY_METHODS =
CArray.instance_methods.map{|m| m.intern }
Instance Method Summary collapse
- #array(*args) ⇒ Object
- #define(&block) ⇒ Object
-
#initialize(type, opt = {}) ⇒ Builder
constructor
A new instance of Builder.
- #member(data_type, id = nil, opt = {}) ⇒ Object
-
#struct(*args, &block) ⇒ Object
method for nested struct.
-
#union(*args, &block) ⇒ Object
method for nested union.
Constructor Details
#initialize(type, opt = {}) ⇒ Builder
336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/carray/struct.rb', line 336 def initialize (type, opt = {}) if not opt[:pack].nil? and not opt[:pack].is_a?(Integer) raise "invalid alignment given '#{align}'" end @type = type ### :struct or :union @align = opt[:pack] ### nil for alignment, int for pack(n) @members = [] ### array of CArray::Struct::Builder::Member @offset = 0 ### offset of each member and size of struct @align_max = 1 ### maximum of alignment among members @size = opt[:size] ### user defined struct size end |
Instance Method Details
#array(*args) ⇒ Object
508 509 510 511 512 513 514 515 516 |
# File 'lib/carray/struct.rb', line 508 def array (*args) opt = args.last.is_a?(Hash) ? args.pop : {} if not opt[:type] or not opt[:type].kind_of?(CArray) raise "type is not given for array member of struct" end args.each do |arg| member(opt[:type], arg) end end |
#define(&block) ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/carray/struct.rb', line 348 def define (&block) # --- case block.arity when 1 block.call(self) ### struct/union definition block when -1, 0 instance_exec(&block) ### struct/union definition block else raise "invalid # of block parameters" end # --- case @type when :struct klass = Class.new(CAStruct) when :union klass = Class.new(CAUnion) end # --- if @align.nil? @offset = alignment(@offset, :align_max) end if @size if @size < @offset raise RuntimeError, "struct size exceeds the fixlen size" end @offset = @size end klass.const_set(:DATA_SIZE, @offset) ### required element as data class # --- table = Hash.new names = [] @members.each do |mem| name = mem.name type = mem.type offset = mem.offset bytes = mem.bytes if bytes table[name] = [offset, type, {:bytes=>bytes}] else table[name] = [offset, type] end names.push(name) end klass.const_set(:MEMBER_TABLE, table) ### required element as data class klass.const_set(:MEMBERS, names) # --- klass.module_eval { names.each do |name| define_method(name) { return self[name] } define_method("#{name}=") { |val| return self[name] = val } end } # --- return klass end |
#member(data_type, id = nil, opt = {}) ⇒ Object
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/carray/struct.rb', line 440 def member (data_type, id = nil, opt = {}) opt = opt.clone if id id = id.to_s else id = "#{@members.size}" end case @type when :struct ### struct case @align when nil ### -- aligned @offset = alignment(@offset, data_type, opt) opt[:offset] = @offset else ### -- packed if opt[:offset] ### ---- explicit offset @offset = pack(opt[:offset], @align, opt) else opt[:offset] = @offset ### ---- auto offset end end mem = Member.new(id, data_type, opt) @members.push(mem) @offset += mem.byte_length when :union ### union alignment(0, data_type, opt) opt[:offset] = 0 mem = Member.new(id, data_type, opt) @members.push(mem) if mem.byte_length > @offset @offset = mem.byte_length end end end |
#struct(*args, &block) ⇒ Object
method for nested struct
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/carray/struct.rb', line 475 def struct (*args, &block) opt = args.last.is_a?(Hash) ? args.pop : {} if block opt = {:pack => @align}.update(opt) st = self.class.new(:struct, opt).define(&block) elsif opt[:type] and opt[:type] <= CAStruct st = opt[:type] else raise "type is not given for struct member of struct" end args.each do |arg| member(st, arg) end return st end |
#union(*args, &block) ⇒ Object
method for nested union
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/carray/struct.rb', line 492 def union (*args, &block) opt = args.last.is_a?(Hash) ? args.pop : {} if block opt = {:pack => @align}.update(opt) st = self.class.new(:union, opt).define(&block) elsif opt[:type] and opt[:type] <= CAStruct st = opt[:type] else raise "type is not given for union member of struct" end args.each do |arg| member(st, arg) end return st end |