class GObject::GObject
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
Attributes
get the pointer we were built from … to_ptr gets the pointer after we have wrapped it up with an auto unref
Public Class Methods
# File lib/vips/gobject.rb, line 105 def ffi_managed_struct const_get :ManagedStruct end
# File lib/vips/gobject.rb, line 94 def ffi_struct const_get :Struct end
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
# 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
Public Instance Methods
access to the managed struct for this class
# File lib/vips/gobject.rb, line 100 def ffi_managed_struct self.class.ffi_managed_struct end
access to the casting struct for this class
# File lib/vips/gobject.rb, line 85 def ffi_struct self.class.ffi_struct end