Class: GObject::GObject

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, SingleForwardable
Defined in:
lib/vips/gobject.rb

Overview

we have a number of things we need to inherit in different ways:

  • we want to be able to subclass GObject in Ruby in a simple way
  • the layouts of the nested structs need to inherit
  • we need to be able to cast between structs which share a base struct without creating new wrappers or messing up refcounting
  • we need automatic gobject refcounting

the solution is to split the class into four areas which we treat differently:

  • we have a "wrapper" Ruby class to allow easy subclassing ... this has a @struct member which holds the actual pointer
  • we use "forwardable" to forward the various ffi methods on to the @struct member ... we arrange things so that subclasses do not need to do the forwarding themselves
  • we have two versions of the struct: a plain one which we can use for casting that will not change the refcounts
  • and a managed one with an unref which we just use for .new
  • we separate the struct layout into a separate module to avoid repeating ourselves

Direct Known Subclasses

Vips::Object

Defined Under Namespace

Modules: GObjectLayout Classes: ManagedStruct, Struct

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr) ⇒ GObject

don't allow ptr == nil, we never want to allocate a GObject struct ourselves, we just want to wrap GLib-allocated GObjects

here we use ManagedStruct, not Struct, since this is the ref that will need the unref


75
76
77
78
79
80
81
82
# File 'lib/vips/gobject.rb', line 75

def initialize ptr
  # GLib::logger.debug("GObject::GObject.initialize") {"ptr = #{ptr}"}
  @ptr = ptr
  @struct = ffi_managed_struct.new ptr

  # sometimes we need to keep refs across C calls ... hide them here
  @references = []
end

Instance Attribute Details

#ptrObject (readonly)

get the pointer we were built from ... #to_ptr gets the pointer after we have wrapped it up with an auto unref


91
92
93
# File 'lib/vips/gobject.rb', line 91

def ptr
  @ptr
end

#referencesObject (readonly)

Returns the value of attribute references


40
41
42
# File 'lib/vips/gobject.rb', line 40

def references
  @references
end

Class Method Details

.ffi_managed_structObject


105
106
107
# File 'lib/vips/gobject.rb', line 105

def ffi_managed_struct
  const_get :ManagedStruct
end

.ffi_structObject


94
95
96
# File 'lib/vips/gobject.rb', line 94

def ffi_struct
  const_get :Struct
end

Instance Method Details

#ffi_managed_structObject

access to the managed struct for this class


100
101
102
# File 'lib/vips/gobject.rb', line 100

def ffi_managed_struct
  self.class.ffi_managed_struct
end

#ffi_structObject

access to the casting struct for this class


85
86
87
# File 'lib/vips/gobject.rb', line 85

def ffi_struct
  self.class.ffi_struct
end