-
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
5 changed files
with
79 additions
and
98 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,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace GObject.Internal; | ||
|
||
/// <summary> | ||
/// Creates new instances of classes and records | ||
/// </summary> | ||
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"); | ||
|
||
//TODO: What happens if type is not registered. Original TypeDirectory had a fallback until a parent was found?! | ||
|
||
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 gclass = Unsafe.AsRef<TypeInstanceData>((void*) handle).GClass; | ||
var gtype = Unsafe.AsRef<TypeClassData>((void*) gclass).GType; | ||
|
||
if (gtype == 0) | ||
throw new Exception("Could not retrieve type from class struct - is the struct valid?"); | ||
|
||
return new Type(gtype); | ||
} | ||
} |
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