class GirFFI::ClassBase

Base class for all generated classes and structs. Contains code for dealing with the generated nested Struct classes.

Constants

GIR_FFI_BUILDER

Attributes

struct[R]

Public Class Methods

direct_wrap(ptr) click to toggle source

Wrap the passed pointer in an instance of the current class. Will not do any casting to subtypes or additional processing.

# File lib/gir_ffi/class_base.rb, line 77
def self.direct_wrap(ptr)
  return nil if !ptr || ptr.null?
  obj = allocate
  obj.__send__ :assign_pointer, ptr
  obj
end
from(val) click to toggle source

Pass-through casting method. This may become a type checking method. It is overridden by GValue to implement wrapping of plain Ruby objects.

# File lib/gir_ffi/class_base.rb, line 87
def self.from(val)
  val
end
new() click to toggle source
# File lib/gir_ffi/class_base.rb, line 33
def initialize
  raise NoMethodError
end
setup_and_call(method, arguments, &block) click to toggle source
# File lib/gir_ffi/class_base.rb, line 37
def self.setup_and_call(method, arguments, &block)
  method_name = try_in_ancestors(:setup_method, method.to_s)

  unless method_name
    raise NoMethodError, "undefined method `#{method}' for #{self}"
  end

  send method_name, *arguments, &block
end
setup_instance_method(name) click to toggle source
# File lib/gir_ffi/class_base.rb, line 65
def self.setup_instance_method(name)
  gir_ffi_builder.setup_instance_method name
end
setup_method(name) click to toggle source
# File lib/gir_ffi/class_base.rb, line 61
def self.setup_method(name)
  gir_ffi_builder.setup_method name
end
to_ffi_type() click to toggle source
# File lib/gir_ffi/class_base.rb, line 57
def self.to_ffi_type
  self::Struct
end
try_in_ancestors(method, *arguments) click to toggle source
# File lib/gir_ffi/class_base.rb, line 47
def self.try_in_ancestors(method, *arguments)
  ancestors.each do |klass|
    if klass.respond_to?(method)
      result = klass.send(method, *arguments)
      return result if result
    end
  end
  nil
end
wrap(ptr) click to toggle source

Wrap the passed pointer in an instance of the current class, or a descendant type if applicable.

# File lib/gir_ffi/class_base.rb, line 71
def self.wrap(ptr)
  direct_wrap ptr
end

Public Instance Methods

==(other) click to toggle source

NOTE: JRuby should fix FFI::MemoryPointer#== to return true for equivalent FFI::Pointer. For now, user to_ptr.address

# File lib/gir_ffi/class_base.rb, line 29
def ==(other)
  other.class == self.class && to_ptr.address == other.to_ptr.address
end
setup_and_call(method, arguments, &block) click to toggle source
# File lib/gir_ffi/class_base.rb, line 17
def setup_and_call(method, arguments, &block)
  method_name = self.class.try_in_ancestors(:setup_instance_method, method.to_s)

  unless method_name
    raise NoMethodError, "undefined method `#{method}' for #{self}"
  end

  send method_name, *arguments, &block
end

Private Instance Methods

assign_pointer(ptr) click to toggle source
# File lib/gir_ffi/class_base.rb, line 105
def assign_pointer(ptr)
  @struct = self.class::Struct.new(ptr)
end
store_pointer(ptr) click to toggle source

Stores a pointer created by a constructor function. Derived classes may perform additional processing. For example, InitiallyUnowned overrides it to sink the floating object.

This method assumes the pointer will always be of the type corresponding to the current class, and never of a subtype.

@param ptr Pointer to the object's C structure

# File lib/gir_ffi/class_base.rb, line 101
def store_pointer(ptr)
  assign_pointer ptr
end