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
Public Class Methods
Source
# File lib/vips/gobject.rb, line 98 def ffi_managed_struct self.const_get :ManagedStruct end
Source
# File lib/vips/gobject.rb, line 73 def initialize ptr # GLib::logger.debug("GObject::GObject.initialize") {"ptr = #{ptr}"} @struct = ffi_managed_struct.new ptr # sometimes we need to keep refs across C calls ... hide them here @references = [] 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
Public Instance Methods
Source
# File lib/vips/gobject.rb, line 93 def ffi_managed_struct self.class.ffi_managed_struct end
access to the managed struct for this class
Source
# File lib/vips/gobject.rb, line 82 def ffi_struct self.class.ffi_struct end
access to the casting struct for this class