Class: AAlib::CPtr

Inherits:
DL::PtrData show all
Includes:
ArgumentChecks
Defined in:
lib/aalib.rb

Overview

Adds initialization and some other features to DL::PtrData. On initialization, PtrData is defined (struct!) using the given TYPE and NAMES that may be used alone or via StructAccessors.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ArgumentChecks

included

Methods inherited from DL::PtrData

#string?

Constructor Details

#initialize(with_ptr = nil) ⇒ CPtr

Initializes a CPtr based on the TYPE and NAMES definitions of the current class. If a subclass of CPtr defines the NAMES constant, then with_ptr will be initialized using DL::PtrData#struct! with the defined TYPE and NAMES so that members may be accessed using CPtr#[] with string or symbol names.

If no with_ptr is given, a new pointer is allocated using the CPtr.new_ptr or the new_ptr method of a subclass of CPtr.



425
426
427
428
429
430
431
# File 'lib/aalib.rb', line 425

def initialize(with_ptr=nil)
  with_ptr ||= self.class.new_ptr
  super(with_ptr)
  if self.class.const_defined? :NAMES
    struct! self.class.const_get(:TYPE), *(self.class.const_get(:NAMES))
  end
end

Class Method Details

.new_ptrObject

Allocates memory based on the class constant TYPE specified in DL style returning an uninitialized CPtr object.



389
390
391
# File 'lib/aalib.rb', line 389

def new_ptr
  malloc(DL.sizeof(const_get(:TYPE)))
end

Instance Method Details

#[](key, len = 0) ⇒ Object

When called with integral arguments, returns data from the pointer similar to String#[]. If passed one int, returns the integer value of the character stored at that index. If passed two ints, returns the substring of length len starting at position key.

When called with a Symbol or String, returns a DL::PtrData of the denoted member. If a subclass defines TYPE and NAMES and the type of the member is String, it will be returned as a String using DL::PtrData#to_s.



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/aalib.rb', line 442

def [](key, len=0)
  if key.kind_of? Integer
    if len == 0
      # We'd like this to behave more like String#[] where a single numeric
      # index gets you the char type integer at the given index.  Ruby
      # 1.9's DL possible fixes the need for this.
      super(key, 1)[0]
    else
      super(key, len)
    end
  elsif (names = self.class.const_get(:NAMES)) &&
         (type = self.class.const_get(:TYPE)) &&
         (type[(names.index(key)), 1] == 'S')
         super(key).to_s
  else
    super(key)
  end
end

#[]=(key, num, val = nil) ⇒ Object

Identical to DL::PtrData#[]= except that it raises an error on frozen objects.

With two ints, key and val, sets the value at key to val. With one int and one str sets the characters begining at key to those of str. With two ints, key and num, and one str, copies at most num characters of str, extending with zeroes if len is greater than the length of str.

When called with a Symbol or String, sets the value of the denoted member. A subclass must have defined TYPE and NAMES so that DL::PtrData knows how to handle each member.



474
475
476
477
478
479
480
481
482
483
# File 'lib/aalib.rb', line 474

def []=(key, num, val=nil)
  if frozen?
    @assignment_will_raise_error = true
  elsif val
    super(key, num, val)
  else
    val = num
    super(key, val)
  end
end

#inspectObject

:nodoc:



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/aalib.rb', line 485

def inspect  #:nodoc:
  if self.class.const_defined? :NAMES
    values = Array.new
    names = self.class.const_get(:NAMES)
    names.each_with_index do |name, i|
      label = name.to_s
      value = self[name]
      values << "%s=%s" % [label, value.inspect]
    end

    "#<%s:%x %s>" % [self.class, ~object_id, values.join(' ')]
  else
    super
  end
end