Class: RWin::API::CStruct

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rwin.rb

Overview

additional methods on Ruby side

Constant Summary collapse

Classes =
nil
ArgCount =
1
Offsets =
nil
References =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.+(arg) ⇒ Object



454
455
456
457
458
459
460
461
462
# File 'lib/rwin.rb', line 454

def self.+(arg)
  offset, ifunc, mblen, nth, klsidx = self::SOffsets[-8, 8].unpack("SCCSS")
#      dpp offset, ifunc, mblen, nth, klsidx

  if nth==0
    raise ArgumentError, "Not a variable length Array."
  else
    alloc(self::Size + mblen*arg)
  end
end

.[](*args) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/rwin.rb', line 435

def self.[](*args);
  if (cmembers = self::SOffsets.unpack("SSS")[2]) == 1
    offset, ifunc, mblen, nth, klsidx = self::SOffsets[8, 8].unpack("SCCSS")
    if nth==0
      __new_and_set_args__(*args)
    else
      if  args[0].is_a?(String) || args[0].is_a?(Array)
        obj = alloc(mblen*args[0].size)
        obj.value = args[0]
        obj
      else
        alloc(mblen*args[0])
      end
    end
  else
    __new_and_set_args__(*args)
  end
end

.defmembers(*args) ⇒ Object



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/rwin.rb', line 412

def self.defmembers(*args)
  #get num of membuers

  cmembers = self::SOffsets.unpack("SSS")[2]
  # get first member

  offset, ifunc, mblen, nth, klsidx = self::SOffsets[8, 8].unpack("SCCSS")
  if args[0] && cmembers==1 && nth>0
    alias_method(args[0], :value)
    alias_method(args[0]+'=', :value=)
  elsif args.size==1 && nth>0
    define_method(args[0]){ self[0] }
    define_method(args[0]+"=") {|x|
      return self[0] = x if ifunc==2 || ifunc==4
      raise TypeError, "Argument must be an Array at variable length." unless x.is_a? Array
      x.each_with_index{|v, i| self[i] = v }
    }
  else
    args.each_with_index{|s, i|
      define_method(s){ self[i] }
      define_method(s+"="){|x| self[i] = x }
    }
  end
end

Instance Method Details

#dumpObject



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/rwin.rb', line 474

def dump()
  q, r = self.bytesize.divmod(16)
  s = ""
  q.times{|i|
    b = self.slice(i*16, 16).to_b.unpack("C*")
    p b
    s += sprintf("%02X "*8+ " "+"%02X "*8, *b) +
                 b.inject(""){|a,b| a+(b>64 ? b.chr : '.')} + "\n"
  }
  b = self.slice(q*16, r).to_b.unpack("C*")
  p b
  if r > 8
    s += sprintf("%02X "*8 + " ", *b[0, 8]) 
    rr = r - 8
    offset = 8
    offset2 = 0
  else
    offset = 0
    offset2 = 1
    rr = r
  end
  s += sprintf("%02X "*rr, *b[offset, rr]) 
  s += " "*((16-r)*3+offset2)+b.inject(""){|a,b| a+(b>64 ? b.chr : '.')}
end

#to_a(rflag = true) ⇒ Object

alias to_ary to_a



408
409
410
# File 'lib/rwin.rb', line 408

def to_a(rflag=true)
  to_ary(rflag)
end

#to_hshObject Also known as: to_h



464
465
466
467
468
469
470
471
# File 'lib/rwin.rb', line 464

def to_hsh()
  members = self.class.instance_methods - RWin::API::CStruct.instance_methods
  members.select{|i| !(i.to_s=~/.*=$/)}.sort.inject({}){|h, m|
    v = __send__(m)
    h[m] = (v.is_a?(RWin::API::CStruct) ? v.to_hsh : v)
    h
  }
end