-
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.
- Loading branch information
Showing
4 changed files
with
80 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace GObject.Internal; | ||
|
||
internal class DemoTypeRegistration | ||
{ | ||
internal static void RegisterTypes() | ||
{ | ||
Register<GObject.Binding>(Binding.GetGType, (ptr, ownedRef) => new GObject.Binding(ptr, ownedRef), OSPlatform.Linux, OSPlatform.OSX, OSPlatform.Windows); | ||
} | ||
|
||
private static void Register<T>(Func<nuint> getType, Func<IntPtr, bool, GObject.Object> factory, params OSPlatform[] supportedPlatforms) where T : class | ||
{ | ||
try | ||
{ | ||
if (supportedPlatforms.Any(RuntimeInformation.IsOSPlatform)) | ||
InstanceFactory.AddFactoryForType(getType(), factory); | ||
} | ||
catch (System.Exception e) | ||
{ | ||
Debug.WriteLine($"Could not register type '{nameof(T)}': {e.Message}"); | ||
} | ||
} | ||
} |
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,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace GObject.Internal; | ||
|
||
internal class InstanceFactory | ||
{ | ||
private static readonly Dictionary<Type, Func<IntPtr, bool, object>> Factories = new(); | ||
|
||
public static object Create(IntPtr handle, bool ownedRef) | ||
{ | ||
var gtype = GetTypeFromInstance(handle); | ||
|
||
Debug.Assert( | ||
condition: Functions.TypeName(gtype.Value).ConvertToString() == Functions.TypeNameFromInstance(new TypeInstanceUnownedHandle(handle)).ConvertToString(), | ||
message: "GType name of instance and class do not match" | ||
); | ||
|
||
if (!Factories.TryGetValue(gtype, out var handleFactory)) | ||
throw new Exception("Cant create handle for unknown type"); | ||
|
||
return handleFactory(handle, ownedRef); | ||
} | ||
|
||
public static void AddFactoryForType(Type type, Func<IntPtr, bool, GObject.Object> factory) | ||
{ | ||
Factories[type] = factory; | ||
} | ||
|
||
private static unsafe Type GetTypeFromInstance(IntPtr handle) | ||
{ | ||
var klassPtr = Unsafe.AsRef<TypeInstanceData>((void*) handle).GClass; | ||
var typeId = Unsafe.AsRef<TypeClassData>((void*) klassPtr).GType; | ||
|
||
|
||
TypeInstanceData instance = Marshal.PtrToStructure<TypeInstanceData>(handle); | ||
TypeClassData klass = Marshal.PtrToStructure<TypeClassData>(instance.GClass); | ||
var typeid = klass.GType; | ||
|
||
if (typeid == 0) | ||
throw new Exception("Could not retrieve type from class struct - is the struct valid?"); | ||
|
||
return new Type(typeid); | ||
} | ||
} |
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