Class: GirFFI::ClassBase

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, 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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RegisteredTypeBase

get_gtype

Methods included from TypeBase

#gir_ffi_builder, #gir_info

Instance Attribute Details

#structObject (readonly)

Returns the value of attribute struct.



11
12
13
# File 'lib/gir_ffi/class_base.rb', line 11

def struct
  @struct
end

Class Method Details

._allocateObject



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

def _allocate
  obj = _real_new
  obj.instance_variable_set :@struct, self::Struct.new
  obj
end

.constructor_wrap(ptr) ⇒ Object

Wraps a pointer retrieved from a constructor method. Here, it is simply defined as a wrapper around direct_wrap, but, e.g., 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.

Parameters:

  • ptr

    Pointer to the object’s C structure

Returns:

  • An object of the current class wrapping the pointer



106
107
108
# File 'lib/gir_ffi/class_base.rb', line 106

def self.constructor_wrap ptr
  direct_wrap ptr
end

.direct_wrap(ptr) ⇒ Object

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



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

def direct_wrap ptr
  return nil if !ptr || ptr.null?
  obj = _real_new
  obj.instance_variable_set :@struct, self::Struct.new(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.



89
90
91
# File 'lib/gir_ffi/class_base.rb', line 89

def from val
  val
end

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



30
31
32
33
34
35
36
37
38
# File 'lib/gir_ffi/class_base.rb', line 30

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

  unless method_name
    raise "Unable to set up method '#{method}' in #{self}"
  end

  send method_name, *arguments, &block
end

.setup_instance_method(name) ⇒ Object



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

def setup_instance_method name
  gir_ffi_builder.setup_instance_method name
end

.setup_method(name) ⇒ Object



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

def setup_method name
  gir_ffi_builder.setup_method name
end

.to_ffitypeObject



50
51
52
# File 'lib/gir_ffi/class_base.rb', line 50

def to_ffitype
  self::Struct
end

.try_in_ancestors(method, *arguments) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/gir_ffi/class_base.rb', line 40

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
end

.wrap(ptr) ⇒ Object

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



67
68
69
# File 'lib/gir_ffi/class_base.rb', line 67

def 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



26
27
28
# File 'lib/gir_ffi/class_base.rb', line 26

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

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



14
15
16
17
18
19
20
21
22
# File 'lib/gir_ffi/class_base.rb', line 14

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

  unless method_name
    raise "Unable to set up instance method '#{method}' in #{self}"
  end

  send method_name, *arguments, &block
end