
org.gnome.glib.SourceFuncs Maven / Gradle / Ivy
// Java-GI - Java language bindings for GObject-Introspection-based libraries
// Copyright (C) 2022-2024 Jan-Willem Harmannij
//
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see .
//
// This file has been generated with Java-GI.
// Do not edit this file directly!
// Visit for more information.
//
package org.gnome.glib;
import io.github.jwharm.javagi.base.ManagedInstance;
import io.github.jwharm.javagi.interop.Interop;
import java.lang.Deprecated;
import java.lang.FunctionalInterface;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.Linker;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import javax.annotation.processing.Generated;
/**
* The {@code GSourceFuncs} struct contains a table of
* functions used to handle event sources in a generic manner.
*
* For idle sources, the prepare and check functions always return {@code true}
* to indicate that the source is always ready to be processed. The prepare
* function also returns a timeout value of 0 to ensure that the poll() call
* doesn't block (since that would be time wasted which could have been spent
* running the idle function).
*
* For timeout sources, the prepare and check functions both return {@code true}
* if the timeout interval has expired. The prepare function also returns
* a timeout value to ensure that the poll() call doesn't block too long
* and miss the next timeout.
*
* For file descriptor sources, the prepare function typically returns {@code false},
* since it must wait until poll() has been called before it knows whether
* any events need to be processed. It sets the returned timeout to -1 to
* indicate that it doesn't mind how long the poll() call blocks. In the
* check function, it tests the results of the poll() call to see if the
* required condition has been met, and returns {@code true} if so.
*/
@Generated("io.github.jwharm.JavaGI")
public class SourceFuncs extends ManagedInstance {
static {
GLib.javagi$ensureInitialized();
}
/**
* Create a SourceFuncs proxy instance for the provided memory address.
*
* @param address the memory address of the native object
*/
public SourceFuncs(MemorySegment address) {
super(Interop.reinterpret(address, getMemoryLayout().byteSize()));
}
/**
* Allocate a new SourceFuncs.
*
* @param arena to control the memory allocation scope
*/
public SourceFuncs(Arena arena) {
super(arena.allocate(getMemoryLayout()));
}
/**
* Allocate a new SourceFuncs.
* The memory is allocated with {@link Arena#ofAuto}.
*/
public SourceFuncs() {
super(Arena.ofAuto().allocate(getMemoryLayout()));
}
/**
* Allocate a new SourceFuncs with the fields set to the provided values.
*
* @param prepare callback function for the field {@code prepare}
* @param check callback function for the field {@code check}
* @param dispatch callback function for the field {@code dispatch}
* @param finalize_ callback function for the field {@code finalize_}
* @param closureCallback value for the field {@code closureCallback}
* @param closureMarshal value for the field {@code closureMarshal}
* @param arena to control the memory allocation scope
*/
public SourceFuncs(PrepareCallback prepare, CheckCallback check, DispatchCallback dispatch,
FinalizeCallback finalize_, SourceFunc closureCallback,
SourceDummyMarshal closureMarshal, Arena arena) {
this(arena);
overridePrepare(prepare, arena);
overrideCheck(check, arena);
overrideDispatch(dispatch, arena);
overrideFinalize(finalize_, arena);
writeClosureCallback(closureCallback, arena);
writeClosureMarshal(closureMarshal, arena);
}
/**
* Allocate a new SourceFuncs with the fields set to the provided values.
* The memory is allocated with {@link Arena#ofAuto}.
*
* @param prepare callback function for the field {@code prepare}
* @param check callback function for the field {@code check}
* @param dispatch callback function for the field {@code dispatch}
* @param finalize_ callback function for the field {@code finalize_}
* @param closureCallback value for the field {@code closureCallback}
* @param closureMarshal value for the field {@code closureMarshal}
*/
public SourceFuncs(PrepareCallback prepare, CheckCallback check, DispatchCallback dispatch,
FinalizeCallback finalize_, SourceFunc closureCallback,
SourceDummyMarshal closureMarshal) {
this(Arena.ofAuto());
overridePrepare(prepare, Arena.ofAuto());
overrideCheck(check, Arena.ofAuto());
overrideDispatch(dispatch, Arena.ofAuto());
overrideFinalize(finalize_, Arena.ofAuto());
writeClosureCallback(closureCallback, Arena.ofAuto());
writeClosureMarshal(closureMarshal, Arena.ofAuto());
}
/**
* The memory layout of the native struct.
* @return the memory layout
*/
public static MemoryLayout getMemoryLayout() {
return MemoryLayout.structLayout(
ValueLayout.ADDRESS.withName("prepare"),
ValueLayout.ADDRESS.withName("check"),
ValueLayout.ADDRESS.withName("dispatch"),
ValueLayout.ADDRESS.withName("finalize"),
ValueLayout.ADDRESS.withName("closure_callback"),
ValueLayout.ADDRESS.withName("closure_marshal")
).withName("GSourceFuncs");
}
/**
* Allocate a new SourceFuncs.
*
* @param arena to control the memory allocation scope
* @return a new, uninitialized {@link SourceFuncs}
* @deprecated Replaced by {@link SourceFuncs#SourceFuncs(Arena)}
*/
@Deprecated
public static SourceFuncs allocate(Arena arena) {
MemorySegment segment = arena.allocate(getMemoryLayout());
return new SourceFuncs(segment);
}
/**
* Allocate a new SourceFuncs with the fields set to the provided values.
*
* @param arena to control the memory allocation scope
* @param prepare callback function for the field {@code prepare}
* @param check callback function for the field {@code check}
* @param dispatch callback function for the field {@code dispatch}
* @param finalize_ callback function for the field {@code finalize_}
* @param closureCallback value for the field {@code closureCallback}
* @param closureMarshal value for the field {@code closureMarshal}
* @return a new {@link SourceFuncs} with the fields set to the provided values
* @deprecated Replaced by {@link SourceFuncs#SourceFuncs(org.gnome.glib.SourceFuncs.PrepareCallback, org.gnome.glib.SourceFuncs.CheckCallback, org.gnome.glib.SourceFuncs.DispatchCallback, org.gnome.glib.SourceFuncs.FinalizeCallback, org.gnome.glib.SourceFunc, org.gnome.glib.SourceDummyMarshal, Arena)}
*/
@Deprecated
public static SourceFuncs allocate(Arena arena, PrepareCallback prepare, CheckCallback check,
DispatchCallback dispatch, FinalizeCallback finalize_, SourceFunc closureCallback,
SourceDummyMarshal closureMarshal) {
return new SourceFuncs(prepare, check, dispatch, finalize_, closureCallback, closureMarshal, arena);
}
/**
* Write a value in the field {@code prepare}.
*
* @param prepare The new value for the field {@code prepare}
* @param _arena to control the memory allocation scope
*/
public void overridePrepare(PrepareCallback prepare, Arena _arena) {
getMemoryLayout().varHandle(MemoryLayout.PathElement.groupElement("prepare"))
.set(handle(), (prepare == null ? MemorySegment.NULL : prepare.toCallback(_arena)));
}
/**
* Write a value in the field {@code check}.
*
* @param check The new value for the field {@code check}
* @param _arena to control the memory allocation scope
*/
public void overrideCheck(CheckCallback check, Arena _arena) {
getMemoryLayout().varHandle(MemoryLayout.PathElement.groupElement("check"))
.set(handle(), (check == null ? MemorySegment.NULL : check.toCallback(_arena)));
}
/**
* Write a value in the field {@code dispatch}.
*
* @param dispatch The new value for the field {@code dispatch}
* @param _arena to control the memory allocation scope
*/
public void overrideDispatch(DispatchCallback dispatch, Arena _arena) {
getMemoryLayout().varHandle(MemoryLayout.PathElement.groupElement("dispatch"))
.set(handle(), (dispatch == null ? MemorySegment.NULL : dispatch.toCallback(_arena)));
}
/**
* Write a value in the field {@code finalize}.
*
* @param finalize_ The new value for the field {@code finalize}
* @param _arena to control the memory allocation scope
*/
public void overrideFinalize(FinalizeCallback finalize_, Arena _arena) {
getMemoryLayout().varHandle(MemoryLayout.PathElement.groupElement("finalize"))
.set(handle(), (finalize_ == null ? MemorySegment.NULL : finalize_.toCallback(_arena)));
}
/**
* Read the value of the field {@code closure_callback}.
*
* @return The value of the field {@code closure_callback}
*/
public SourceFunc readClosureCallback() {
Arena _arena = Arena.ofAuto();
var _result = (MemorySegment) getMemoryLayout()
.varHandle(MemoryLayout.PathElement.groupElement("closure_callback")).get(handle());
return null /* Unsupported parameter type */;
}
/**
* Write a value in the field {@code closure_callback}.
*
* @param closureCallback The new value for the field {@code closure_callback}
* @param _arena to control the memory allocation scope
*/
public void writeClosureCallback(SourceFunc closureCallback, Arena _arena) {
getMemoryLayout().varHandle(MemoryLayout.PathElement.groupElement("closure_callback"))
.set(handle(), (closureCallback == null ? MemorySegment.NULL : closureCallback.toCallback(_arena)));
}
/**
* Read the value of the field {@code closure_marshal}.
*
* @return The value of the field {@code closure_marshal}
*/
public SourceDummyMarshal readClosureMarshal() {
Arena _arena = Arena.ofAuto();
var _result = (MemorySegment) getMemoryLayout()
.varHandle(MemoryLayout.PathElement.groupElement("closure_marshal")).get(handle());
return null /* Unsupported parameter type */;
}
/**
* Write a value in the field {@code closure_marshal}.
*
* @param closureMarshal The new value for the field {@code closure_marshal}
* @param _arena to control the memory allocation scope
*/
public void writeClosureMarshal(SourceDummyMarshal closureMarshal, Arena _arena) {
getMemoryLayout().varHandle(MemoryLayout.PathElement.groupElement("closure_marshal"))
.set(handle(), (closureMarshal == null ? MemorySegment.NULL : closureMarshal.toCallback(_arena)));
}
/**
* Functional interface declaration of the {@code PrepareCallback} callback.
*/
@FunctionalInterface
public interface PrepareCallback {
boolean run(Source source, MemorySegment timeout);
/**
* The {@code upcall} method is called from native code. The parameters
* are marshaled and {@link #run} is executed.
*/
default int upcall(MemorySegment source, MemorySegment timeout) {
Arena _arena = Arena.ofAuto();
var _result = run(MemorySegment.NULL.equals(source) ? null : new Source(source), timeout);
return _result ? 1 : 0;
}
/**
* Creates a native function pointer to the {@link #upcall} method.
*
* @return the native function pointer
*/
default MemorySegment toCallback(Arena arena) {
FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.JAVA_INT,
ValueLayout.ADDRESS, ValueLayout.ADDRESS);
MethodHandle _handle = Interop.upcallHandle(MethodHandles.lookup(), PrepareCallback.class, _fdesc);
return Linker.nativeLinker().upcallStub(_handle.bindTo(this), _fdesc, arena);
}
}
/**
* Functional interface declaration of the {@code CheckCallback} callback.
*/
@FunctionalInterface
public interface CheckCallback {
boolean run(Source source);
/**
* The {@code upcall} method is called from native code. The parameters
* are marshaled and {@link #run} is executed.
*/
default int upcall(MemorySegment source) {
Arena _arena = Arena.ofAuto();
var _result = run(MemorySegment.NULL.equals(source) ? null : new Source(source));
return _result ? 1 : 0;
}
/**
* Creates a native function pointer to the {@link #upcall} method.
*
* @return the native function pointer
*/
default MemorySegment toCallback(Arena arena) {
FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.JAVA_INT,
ValueLayout.ADDRESS);
MethodHandle _handle = Interop.upcallHandle(MethodHandles.lookup(), CheckCallback.class, _fdesc);
return Linker.nativeLinker().upcallStub(_handle.bindTo(this), _fdesc, arena);
}
}
/**
* Functional interface declaration of the {@code DispatchCallback} callback.
*/
@FunctionalInterface
public interface DispatchCallback {
boolean run(Source source);
/**
* The {@code upcall} method is called from native code. The parameters
* are marshaled and {@link #run} is executed.
*/
default int upcall(MemorySegment source, MemorySegment callback, MemorySegment userData) {
Arena _arena = Arena.ofAuto();
var _result = run(MemorySegment.NULL.equals(source) ? null : new Source(source));
return _result ? 1 : 0;
}
/**
* Creates a native function pointer to the {@link #upcall} method.
*
* @return the native function pointer
*/
default MemorySegment toCallback(Arena arena) {
FunctionDescriptor _fdesc = FunctionDescriptor.of(ValueLayout.JAVA_INT,
ValueLayout.ADDRESS, ValueLayout.ADDRESS, ValueLayout.ADDRESS);
MethodHandle _handle = Interop.upcallHandle(MethodHandles.lookup(), DispatchCallback.class, _fdesc);
return Linker.nativeLinker().upcallStub(_handle.bindTo(this), _fdesc, arena);
}
}
/**
* Functional interface declaration of the {@code FinalizeCallback} callback.
*/
@FunctionalInterface
public interface FinalizeCallback {
void run(Source source);
/**
* The {@code upcall} method is called from native code. The parameters
* are marshaled and {@link #run} is executed.
*/
default void upcall(MemorySegment source) {
Arena _arena = Arena.ofAuto();
run(MemorySegment.NULL.equals(source) ? null : new Source(source));
}
/**
* Creates a native function pointer to the {@link #upcall} method.
*
* @return the native function pointer
*/
default MemorySegment toCallback(Arena arena) {
FunctionDescriptor _fdesc = FunctionDescriptor.ofVoid(ValueLayout.ADDRESS);
MethodHandle _handle = Interop.upcallHandle(MethodHandles.lookup(), FinalizeCallback.class, _fdesc);
return Linker.nativeLinker().upcallStub(_handle.bindTo(this), _fdesc, arena);
}
}
}