Class: GirFFI::ClassBase

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, InstanceMethodSetup, MethodSetup, RegisteredTypeBase
Defined in:
lib/gir_ffi/class_base.rb

Overview

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

Direct Known Subclasses

ObjectBase, StructBase, UnionBase

Constant Summary collapse

GIR_FFI_BUILDER =
Builders::NullClassBuilder.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RegisteredTypeBase

gtype

Methods included from TypeBase

#gir_ffi_builder, #gir_info

Methods included from MethodSetup

setup_method, setup_method!

Methods included from InstanceMethodSetup

setup_instance_method, setup_instance_method!

Instance Attribute Details

#structObject (readonly)

Returns the value of attribute struct.



20
21
22
# File 'lib/gir_ffi/class_base.rb', line 20

def struct
  @struct
end

Class Method Details

.direct_wrap(ptr) ⇒ Object

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



71
72
73
74
75
76
77
# File 'lib/gir_ffi/class_base.rb', line 71

def self.direct_wrap(ptr)
  return nil if !ptr || ptr.null?

  obj = allocate
  obj.__send__ :assign_pointer, ptr
  obj
end

.from(val) ⇒ Object

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



82
83
84
# File 'lib/gir_ffi/class_base.rb', line 82

def self.from(val)
  val
end

.setup_and_call(method, arguments, &block) ⇒ Object

Raises:

  • (NoMethodError)


37
38
39
40
41
42
43
# 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)

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

  send method_name, *arguments, &block
end

.to_callback_ffi_typeObject



59
60
61
# File 'lib/gir_ffi/class_base.rb', line 59

def self.to_callback_ffi_type
  :pointer
end

.to_ffi_typeObject



55
56
57
# File 'lib/gir_ffi/class_base.rb', line 55

def self.to_ffi_type
  self::Struct
end

.try_in_ancestors(method, *arguments) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/gir_ffi/class_base.rb', line 45

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) ⇒ Object

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



65
66
67
# File 'lib/gir_ffi/class_base.rb', line 65

def self.wrap(ptr)
  direct_wrap ptr
end

Instance Method Details

#==(other) ⇒ Object

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



33
34
35
# File 'lib/gir_ffi/class_base.rb', line 33

def ==(other)
  other.class == self.class && to_ptr.address == other.to_ptr.address
end

#setup_and_call(method, arguments, &block) ⇒ Object

Raises:

  • (NoMethodError)


23
24
25
26
27
28
29
# File 'lib/gir_ffi/class_base.rb', line 23

def setup_and_call(method, arguments, &block)
  method_name = self.class.try_in_ancestors(:setup_instance_method, method.to_s)

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

  send method_name, *arguments, &block
end