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

Implement unions #25

Closed
diamondburned opened this issue Aug 13, 2021 · 2 comments
Closed

Implement unions #25

diamondburned opened this issue Aug 13, 2021 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@diamondburned
Copy link
Owner

diamondburned commented Aug 13, 2021

Currently, gotk4 does not implement union generation. It can probably be
implemented like so:

type Event struct {
	*event
}

type event struct {
	native *C.GdkEvent
}

// All finalizers omitted for brevity.

func (e *Event) EventType() EventType {
	return *(*EventType)(unsafe.Pointer(e.native))
}

func (e *Event) EventKey() EventKey {
	return EventKey{
		eventKey: &eventKey{
			native: (*C.GdkEventKey)(unsafe.Pointer(e.native)),
		},
	}
}

func Example() {
	var e Event

	switch e.EventType() {
	case gdk.KeyPress:
		e := e.EventKey()
		log.Println("keyval =", e.Keyval())
	}
}

Using this is pretty unsafe, but it's probably the cleanest way to implement
this. Type checking is up to the caller to do, because there's no way to do that
with C unions.

@diamondburned diamondburned added the enhancement New feature or request label Aug 24, 2021
@diamondburned diamondburned self-assigned this Oct 22, 2021
@diamondburned diamondburned pinned this issue Oct 22, 2021
@diamondburned
Copy link
Owner Author

GTK+3 needs events just to handle keys. Without this implemented, that's not
possible.

diamondburned added a commit that referenced this issue Oct 23, 2021
This commit changes most record return values to return a pointer
instead of value, to both reflect how it'll always pass-by-reference and
how most record methods need a pointer receiver.

This commit also changes the underlying types for enums and bitfields to
be C.int and C.uint, respectively, to allow for direct casting of
enums/bitfields lists.

The record pointer change is expected to be followed up with a similar
change for classes. This will make signal generation slightly more
accurate as well, since both of those types are always passed by
pointers.

This commit closes issue #25.
@diamondburned
Copy link
Owner Author

Closed by commit d16c0d1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant