-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #775 from gircore/signal-return-value
- Loading branch information
Showing
24 changed files
with
406 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
|
||
namespace GObject; | ||
|
||
/// <summary> | ||
/// SignalHandler for signals without any extra data. | ||
/// </summary> | ||
/// <param name="sender">The sender of this signal.</param> | ||
/// <param name="args">Event args. Will always have the value of <see cref="EventArgs.Empty"/>.</param> | ||
public delegate TReturn ReturningSignalHandler<in TSender, out TReturn>(TSender sender, EventArgs args) | ||
where TSender : Object; | ||
|
||
/// <summary> | ||
/// SignalHandler for signals with extra data. | ||
/// </summary> | ||
/// <param name="sender">The sender of this signal.</param> | ||
/// <param name="args"><see cref="SignalArgs"/> with additional data.</param> | ||
public delegate TReturn ReturningSignalHandler<in TSender, in TSignalArgs, out TReturn>(TSender sender, TSignalArgs args) | ||
where TSender : Object | ||
where TSignalArgs : SignalArgs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
namespace GObject; | ||
|
||
/// <summary> | ||
/// Describes a GSignal. | ||
/// </summary> | ||
public class ReturningSignal<TSender, TReturn> : SignalDefinition | ||
where TSender : Object, GTypeProvider | ||
{ | ||
private uint? _id; | ||
|
||
public string UnmanagedName { get; } | ||
public string ManagedName { get; } | ||
public uint Id => _id ??= GetId(); | ||
|
||
public ReturningSignal(string unmanagedName, string managedName) | ||
{ | ||
UnmanagedName = unmanagedName; | ||
ManagedName = managedName; | ||
} | ||
|
||
private uint GetId() | ||
{ | ||
#if NET7_0_OR_GREATER | ||
var gType = TSender.GetGType(); | ||
#else | ||
var gType = Internal.GTypeProviderHelper.GetGType<TSender>(); | ||
#endif | ||
|
||
return Internal.Functions.SignalLookup(UnmanagedName, gType); | ||
} | ||
|
||
/// <summary> | ||
/// Connects a <paramref name="signalHandler"/> to this signal. | ||
/// </summary> | ||
/// <param name="o">The object on which connect the handler.</param> | ||
/// <param name="signalHandler">The signal handler function.</param> | ||
/// <param name="after">Define if this action must be called before or after the default handler of this signal.</param> | ||
/// <param name="detail">Define for which signal detail the connection should be made.</param> | ||
public void Connect(TSender o, ReturningSignalHandler<TSender, TReturn> signalHandler, bool after = false, string? detail = null) | ||
{ | ||
var closure = new Closure((returnValue, parameters) => | ||
{ | ||
if (returnValue is null) | ||
throw new System.Exception($"{nameof(TSender)}.{ManagedName}: C did not provide a value pointer to return the signal result"); | ||
|
||
var result = signalHandler(o, System.EventArgs.Empty); | ||
returnValue.Set(result); | ||
}); | ||
|
||
o.SignalConnectClosure(this, signalHandler, closure, after, detail); | ||
} | ||
|
||
/// <summary> | ||
/// Disconnects a <paramref name="signalHandler"/> previously connected to this signal. | ||
/// </summary> | ||
/// <param name="o">The object from which disconnect the handler.</param> | ||
/// <param name="signalHandler">The signal handler function.</param> | ||
public void Disconnect(TSender o, ReturningSignalHandler<TSender, TReturn> signalHandler) | ||
{ | ||
o.Disconnect(this, signalHandler); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/Libs/GObject-2.0/Public/ReturningSignalTSenderTSignalArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
namespace GObject; | ||
|
||
/// <summary> | ||
/// Describes a GSignal. | ||
/// </summary> | ||
public class ReturningSignal<TSender, TSignalArgs, TReturn> : SignalDefinition | ||
where TSender : Object, GTypeProvider | ||
where TSignalArgs : SignalArgs, new() | ||
{ | ||
private uint? _id; | ||
|
||
public string UnmanagedName { get; } | ||
public string ManagedName { get; } | ||
public uint Id => _id ??= GetId(); | ||
|
||
public ReturningSignal(string unmanagedName, string managedName) | ||
{ | ||
UnmanagedName = unmanagedName; | ||
ManagedName = managedName; | ||
} | ||
|
||
private uint GetId() | ||
{ | ||
#if NET7_0_OR_GREATER | ||
var gType = TSender.GetGType(); | ||
#else | ||
var gType = Internal.GTypeProviderHelper.GetGType<TSender>(); | ||
#endif | ||
|
||
return Internal.Functions.SignalLookup(UnmanagedName, gType); | ||
} | ||
|
||
/// <summary> | ||
/// Connects a <paramref name="signalHandler"/> to this signal. | ||
/// </summary> | ||
/// <param name="o">The object on which connect the handler.</param> | ||
/// <param name="signalHandler">The signal handler function.</param> | ||
/// <param name="after">Define if this action must be called before or after the default handler of this signal.</param> | ||
/// <param name="detail">Define for which signal detail the connection should be made.</param> | ||
public void Connect(TSender o, ReturningSignalHandler<TSender, TSignalArgs, TReturn> signalHandler, bool after = false, string? detail = null) | ||
{ | ||
var closure = new Closure((returnValue, parameters) => | ||
{ | ||
if (returnValue is null) | ||
throw new System.Exception($"{nameof(TSender)}.{ManagedName}: C did not provide a value pointer to return the signal result"); | ||
|
||
var args = new TSignalArgs(); | ||
args.SetArgs(parameters); | ||
|
||
var result = signalHandler(o, args); | ||
returnValue.Set(result); | ||
}); | ||
|
||
o.SignalConnectClosure(this, signalHandler, closure, after, detail); | ||
} | ||
|
||
/// <summary> | ||
/// Disconnects a <paramref name="signalHandler"/> previously connected to this signal. | ||
/// </summary> | ||
/// <param name="o">The object from which disconnect the handler.</param> | ||
/// <param name="signalHandler">The signal handler function.</param> | ||
public void Disconnect(TSender o, ReturningSignalHandler<TSender, TSignalArgs, TReturn> signalHandler) | ||
{ | ||
o.Disconnect(this, signalHandler); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "girtest-class-tester.h" | ||
|
||
/** | ||
* GirTestClassTester: | ||
* | ||
* Contains functions for testing bindings with class types. | ||
*/ | ||
|
||
struct _GirTestClassTester | ||
{ | ||
GObject parent_instance; | ||
}; | ||
|
||
G_DEFINE_TYPE(GirTestClassTester, girtest_class_tester, G_TYPE_OBJECT) | ||
|
||
static void | ||
girtest_class_tester_init(GirTestClassTester *value) | ||
{ | ||
} | ||
|
||
static void | ||
girtest_class_tester_class_init(GirTestClassTesterClass *class) | ||
{ | ||
} | ||
|
||
/** | ||
* girtest_class_tester_transfer_ownership_full_and_unref: | ||
* @object: (transfer full): Any object | ||
* | ||
* Simple test to transfer ownership to C. The object is unrefed immediately. | ||
*/ | ||
void | ||
girtest_class_tester_transfer_ownership_full_and_unref(GObject *object) | ||
{ | ||
g_object_unref(object); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include <glib-object.h> | ||
|
||
G_BEGIN_DECLS | ||
|
||
#define GIRTEST_TYPE_CLASS_TESTER girtest_class_tester_get_type() | ||
|
||
G_DECLARE_FINAL_TYPE(GirTestClassTester, girtest_class_tester, GIRTEST, CLASS_TESTER, GObject) | ||
|
||
void | ||
girtest_class_tester_transfer_ownership_full_and_unref(GObject *object); | ||
|
||
G_END_DECLS | ||
|
38 changes: 38 additions & 0 deletions
38
src/Native/GirTestLib/girtest-primitive-value-type-tester.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "girtest-primitive-value-type-tester.h" | ||
|
||
/** | ||
* GirTestPrimitiveValueTypeTester: | ||
* | ||
* Contains functions for testing bindings with primitive value types. | ||
*/ | ||
|
||
struct _GirTestPrimitiveValueTypeTester | ||
{ | ||
GObject parent_instance; | ||
}; | ||
|
||
G_DEFINE_TYPE(GirTestPrimitiveValueTypeTester, girtest_primitive_value_type_tester, G_TYPE_OBJECT) | ||
|
||
static void | ||
girtest_primitive_value_type_tester_init(GirTestPrimitiveValueTypeTester *value) | ||
{ | ||
} | ||
|
||
static void | ||
girtest_primitive_value_type_tester_class_init(GirTestPrimitiveValueTypeTesterClass *class) | ||
{ | ||
} | ||
|
||
/** | ||
* girtest_primitive_value_type_tester_int_in: | ||
* @val: (in): An integer value | ||
* | ||
* Simple test for an input integer parameter. | ||
* | ||
* Returns: The input value multiplied by 2 | ||
*/ | ||
int | ||
girtest_primitive_value_type_tester_int_in(int val) | ||
{ | ||
return 2 * val; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Native/GirTestLib/girtest-primitive-value-type-tester.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include <glib-object.h> | ||
|
||
G_BEGIN_DECLS | ||
|
||
#define GIRTEST_TYPE_PRIMITIVE_VALUE_TYPE_TESTER girtest_primitive_value_type_tester_get_type() | ||
|
||
G_DECLARE_FINAL_TYPE(GirTestPrimitiveValueTypeTester, girtest_primitive_value_type_tester, | ||
GIRTEST, PRIMITIVE_VALUE_TYPE_TESTER, GObject) | ||
|
||
int | ||
girtest_primitive_value_type_tester_int_in(int val); | ||
|
||
G_END_DECLS | ||
|
Oops, something went wrong.