Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to more modern GObject strategy #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pkg/glibobject/gcancellable.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ import (
// GIO types

type GCancellable struct {
*GObject
*Object
}

func (self *GCancellable) native() *C.GCancellable {
return (*C.GCancellable)(unsafe.Pointer(self))
}

func (self *GCancellable) Ptr() unsafe.Pointer {
return unsafe.Pointer(self)
if self == nil || self.Object == nil {
return unsafe.Pointer(nil)
}
return unsafe.Pointer(self.Object)
}

// At the moment, no cancellable API, just pass nil
6 changes: 6 additions & 0 deletions pkg/glibobject/glibobject.go.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ _g_variant_lookup_string (GVariant *v, const char *key)
return r;
return NULL;
}

static GObject *
toGObject(void *p)
{
return (G_OBJECT(p));
}
45 changes: 24 additions & 21 deletions pkg/glibobject/gobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,50 @@ import (
* GObject
*/

// ToGObject type converts an unsafe.Pointer as a native C GObject.
// This function is exported for visibility in other gotk3 packages and
// is not meant to be used by applications.
func ToGObject(p unsafe.Pointer) *C.GObject {
return C.toGObject(p)
}

// IObject is an interface type implemented by Object and all types which embed
// an Object. It is meant to be used as a type for function arguments which
// require GObjects or any subclasses thereof.
type IObject interface {
toGObject() *C.GObject
ToObject() *GObject
ToObject() *Object
}

// GObject is a representation of GLib's GObject.
type GObject struct {
ptr unsafe.Pointer
}

func (v *GObject) Ptr() unsafe.Pointer {
return v.ptr
type Object struct {
GObject *C.GObject
}

func (v *GObject) native() *C.GObject {
if v == nil {
return nil
func (v *Object) Ptr() unsafe.Pointer {
if v == nil || v.GObject == nil {
return unsafe.Pointer(nil)
}
return (*C.GObject)(v.ptr)
return unsafe.Pointer(v.GObject)
}

func (v *GObject) Ref() {
C.g_object_ref(C.gpointer(v.Ptr()))
func (v *Object) Ref() {
C.g_object_ref(C.gpointer(v.GObject))
}

func (v *GObject) Unref() {
C.g_object_unref(C.gpointer(v.Ptr()))
func (v *Object) Unref() {
C.g_object_unref(C.gpointer(v.GObject))
}

func (v *GObject) RefSink() {
C.g_object_ref_sink(C.gpointer(v.native()))
func (v *Object) RefSink() {
C.g_object_ref_sink(C.gpointer(v.GObject))
}

func (v *GObject) IsFloating() bool {
c := C.g_object_is_floating(C.gpointer(v.native()))
func (v *Object) IsFloating() bool {
c := C.g_object_is_floating(C.gpointer(v.GObject))
return GoBool(GBoolean(c))
}

func (v *GObject) ForceFloating() {
C.g_object_force_floating(v.native())
func (v *Object) ForceFloating() {
C.g_object_force_floating(v.GObject)
}