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

Constant Summary collapse

GIR_FFI_BUILDER =
NullBuilder.new

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.



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

def struct
  @struct
end

Class Method Details

._allocateObject



83
84
85
86
87
# File 'lib/gir_ffi/class_base.rb', line 83

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



109
110
111
# File 'lib/gir_ffi/class_base.rb', line 109

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.



76
77
78
79
80
81
# File 'lib/gir_ffi/class_base.rb', line 76

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



92
93
94
# File 'lib/gir_ffi/class_base.rb', line 92

def from val
  val
end

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



33
34
35
36
37
38
39
40
41
# File 'lib/gir_ffi/class_base.rb', line 33

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

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

  send method_name, *arguments, &block
end

.setup_instance_method(name) ⇒ Object



61
62
63
# File 'lib/gir_ffi/class_base.rb', line 61

def setup_instance_method name
  gir_ffi_builder.setup_instance_method name
end

.setup_method(name) ⇒ Object



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

def setup_method name
  gir_ffi_builder.setup_method name
end

.to_ffitypeObject



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

def to_ffitype
  self::Struct
end

.try_in_ancestors(method, *arguments) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/gir_ffi/class_base.rb', line 43

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.



70
71
72
# File 'lib/gir_ffi/class_base.rb', line 70

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



29
30
31
# 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) ⇒ Object



17
18
19
20
21
22
23
24
25
# 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 RuntimeError, "Unable to set up instance method '#{method}' in #{self}"
  end

  send method_name, *arguments, &block
end