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

Add gobject data funcs #150

Open
wants to merge 3 commits into
base: 4
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
4 changes: 3 additions & 1 deletion .github/workflows/qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ jobs:
name: "${{ github.event_name }} / test"
runs-on: ubuntu-latest
steps:
- name: Install xfvb
run: sudo apt-get install -y xvfb x11-xserver-utils
- uses: actions/checkout@v3
- uses: diamondburned/cache-install@main
with:
Expand All @@ -65,7 +67,7 @@ jobs:

- run: go test ./...

- run: cd pkg && go test ./...
- run: cd pkg && xvfb-run -a go test ./...

docker:
name: "${{ github.event_name }} / docker"
Expand Down
6 changes: 6 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.22.0

use (
.
./pkg
)
74 changes: 74 additions & 0 deletions pkg/core/glib/bind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package glib

import (
"reflect"
"sync"
"unsafe"

"github.com/diamondburned/gotk4/pkg/core/gbox"
)

// #include <glib.h>
// #include <glib-object.h>
// #include "glib.go.h"
import "C"

var bindingNames sync.Map // map[reflect.Type]C.GQuark

// Associate value with object
func Bind[T any](obj Objector, value T) {
object := BaseObject(obj)
name := bindingName[T]()

ptr := C.gpointer(gbox.Assign(value))

C.g_object_set_data_full(object.native(), (*C.gchar)(name), ptr, (*[0]byte)(C._gotk4_data_destroy))
}

// Disassociate value from object
func Unbind[T any](obj Objector) {
name := bindingName[T]()

ptr := C.g_object_steal_data(BaseObject(obj).native(), (*C.gchar)(name))
defer gbox.Delete(uintptr(ptr))
}

// Obtain value associated with object
func Bounded[T any](obj Objector) *T {
name := bindingName[T]()

ptr := C.g_object_get_data(BaseObject(obj).native(), name)

value, ok := gbox.Get(uintptr(ptr)).(T)
if !ok {
return nil
}

return &value
}

func bindingName[T any]() *C.gchar {
t := reflect.TypeFor[T]()

if v, ok := bindingNames.Load(t); ok {
quark := v.(C.GQuark)
return C.g_quark_to_string(quark)
}

name := "_gotk4_" + t.String()

nameC := C.CString(name)
defer C.free(unsafe.Pointer(nameC))

quark := C.g_quark_from_string(nameC)
if v, lost := bindingNames.LoadOrStore(t, quark); lost {
quark = v.(C.GQuark)
}

return C.g_quark_to_string(quark)
}

//export _gotk4_data_destroy
func _gotk4_data_destroy(ptr C.gpointer) {
gbox.Delete(uintptr(ptr))
}
2 changes: 2 additions & 0 deletions pkg/core/glib/glib.go.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,6 @@ static void init_i18n(const char *domain, const char *dir) {

static const char *localize(const char *string) { return _(string); }

extern void _gotk4_data_destroy(gpointer ptr);

#endif
27 changes: 27 additions & 0 deletions pkg/core/test/glib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test

import (
"runtime"
"testing"

"github.com/diamondburned/gotk4/pkg/core/glib"
"github.com/diamondburned/gotk4/pkg/gio/v2"
)

func TestObjectData(t *testing.T) {
testObjectData(t)

runtime.GC()
}

func testObjectData(t *testing.T) {
app := gio.NewApplication("foo.bar", gio.ApplicationFlagsNone)

glib.Bind(app, "foo")

if value := glib.Bounded[string](app); value == nil || *value != "foo" {
t.Fatal("returned data did not match expected data")
}

glib.Unbind[string](app)
}
Loading