Skip to content

Commit

Permalink
Use instance version of Callback.Set
Browse files Browse the repository at this point in the history
  • Loading branch information
BeanCheeseBurrito committed Oct 14, 2024
1 parent 87efb44 commit c8162b3
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 42 deletions.
18 changes: 0 additions & 18 deletions src/Flecs.NET/Core/BindingContext/Callback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,6 @@ internal void Set<T>(T callback, nint invoker) where T : Delegate
Delegate = GCHandle.Alloc(callback);
}

internal static void Set(ref Callback dest, nint callback, nint invoker)
{
if (dest != default)
dest.Dispose();

dest.Invoker = invoker;
dest.Pointer = callback;
}

internal static void Set<T>(ref Callback dest, T callback, nint invoker) where T : Delegate
{
if (dest != default)
dest.Dispose();

dest.Invoker = invoker;
dest.Delegate = GCHandle.Alloc(callback);
}

public bool Equals(Callback other)
{
return Delegate.Equals(other.Delegate) && Pointer == other.Pointer && Invoker == other.Invoker;
Expand Down
8 changes: 4 additions & 4 deletions src/Flecs.NET/Core/Ecs/OsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal static void OverrideOsApi()
/// <param name="callback">The callback.</param>
public static void SetAbort(Action callback)
{
Callback.Set(ref Context.Abort, callback, Pointers.AbortCallbackDelegate);
Context.Abort.Set(callback, Pointers.AbortCallbackDelegate);
}

/// <summary>
Expand All @@ -68,7 +68,7 @@ public static void SetAbort(Action callback)
/// <param name="callback">The callback.</param>
public static void SetAbort(delegate*<void> callback)
{
Callback.Set(ref Context.Abort, (IntPtr)callback, Pointers.AbortCallbackPointer);
Context.Abort.Set((IntPtr)callback, Pointers.AbortCallbackPointer);
}

/// <summary>
Expand All @@ -77,7 +77,7 @@ public static void SetAbort(delegate*<void> callback)
/// <param name="callback">The callback.</param>
public static void SetLog(LogCallback callback)
{
Callback.Set(ref Context.Log, callback, Pointers.LogCallbackDelegate);
Context.Log.Set(callback, Pointers.LogCallbackDelegate);
}

/// <summary>
Expand All @@ -86,7 +86,7 @@ public static void SetLog(LogCallback callback)
/// <param name="callback">The callback.</param>
public static void SetLog(delegate*<int, string, int, string, void> callback)
{
Callback.Set(ref Context.Log, (IntPtr)callback, Pointers.LogCallbackPointer);
Context.Log.Set((IntPtr)callback, Pointers.LogCallbackPointer);
}

private static void DefaultAbort()
Expand Down
4 changes: 2 additions & 2 deletions src/Flecs.NET/Core/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3797,7 +3797,7 @@ private ref Entity SetInternal<T>(ulong id, T* component)
private ref Entity ObserveInternal<T>(ulong eventId, T callback, nint invoker) where T : Delegate
{
IteratorContext* iteratorContext = Memory.AllocZeroed<IteratorContext>(1);
Callback.Set(ref iteratorContext->Callback, callback, invoker);
iteratorContext->Callback.Set(callback, invoker);

ecs_observer_desc_t desc = default;
desc.events[0] = eventId;
Expand All @@ -3816,7 +3816,7 @@ private ref Entity ObserveInternal<T>(ulong eventId, T callback, nint invoker) w
private ref Entity ObserveInternal<T>(ulong eventId, IntPtr callback, nint invoker) where T : Delegate
{
IteratorContext* iteratorContext = Memory.AllocZeroed<IteratorContext>(1);
Callback.Set(ref iteratorContext->Callback, callback, invoker);
iteratorContext->Callback.Set(callback, invoker);

ecs_observer_desc_t desc = default;
desc.events[0] = eventId;
Expand Down
8 changes: 4 additions & 4 deletions src/Flecs.NET/Core/ObserverBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ internal ref ObserverBuilder SetCallback<T>(T callback, nint invoker) where T :
{
FreeCallback();
IteratorContext context = default;
Callback.Set(ref context.Callback, callback, invoker);
context.Callback.Set(callback, invoker);
Desc.callback = Pointers.IteratorCallback;
Desc.callback_ctx = Memory.Alloc(context);
Desc.callback_ctx_free = Pointers.IteratorContextFree;
Expand All @@ -311,7 +311,7 @@ internal ref ObserverBuilder SetCallback(nint callback, nint invoker)
{
FreeCallback();
IteratorContext context = default;
Callback.Set(ref context.Callback, callback, invoker);
context.Callback.Set(callback, invoker);
Desc.callback = Pointers.IteratorCallback;
Desc.callback_ctx = Memory.Alloc(context);
Desc.callback_ctx_free = Pointers.IteratorContextFree;
Expand All @@ -322,7 +322,7 @@ internal ref ObserverBuilder SetRun<T>(T callback, nint invoker) where T : Deleg
{
FreeRun();
RunContext context = default;
Callback.Set(ref context.Callback, callback, invoker);
context.Callback.Set(callback, invoker);
Desc.run = Pointers.RunCallback;
Desc.run_ctx = Memory.Alloc(context);
Desc.run_ctx_free = Pointers.RunContextFree;
Expand All @@ -333,7 +333,7 @@ internal ref ObserverBuilder SetRun(nint callback, nint invoker)
{
FreeRun();
RunContext context = default;
Callback.Set(ref context.Callback, callback, invoker);
context.Callback.Set(callback, invoker);
Desc.run = Pointers.RunCallback;
Desc.run_ctx = Memory.Alloc(context);
Desc.run_ctx_free = Pointers.RunContextFree;
Expand Down
12 changes: 6 additions & 6 deletions src/Flecs.NET/Core/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ public ref QueryBuilder TermAt(int termIndex)
/// <returns></returns>
public ref QueryBuilder OrderBy(ulong component, Ecs.OrderByCallback callback)
{
Callback.Set(ref QueryContext.OrderBy, callback, 0);
QueryContext.OrderBy.Set(callback, 0);
Desc.order_by_callback = Marshal.GetFunctionPointerForDelegate(callback);
Desc.order_by = component;
return ref this;
Expand Down Expand Up @@ -1688,7 +1688,7 @@ public ref QueryBuilder GroupBy<T>()
/// <returns></returns>
public ref QueryBuilder GroupBy(ulong component, Ecs.GroupByCallback callback)
{
Callback.Set(ref GroupByContext.GroupBy, callback, Pointers.GroupByCallbackDelegate);
GroupByContext.GroupBy.Set(callback, Pointers.GroupByCallbackDelegate);
Desc.group_by_callback = Pointers.GroupByCallback;
Desc.group_by = component;
return ref this;
Expand All @@ -1712,7 +1712,7 @@ public ref QueryBuilder GroupBy<T>(Ecs.GroupByCallback callback)
/// <returns></returns>
public ref QueryBuilder OnGroupCreate(Ecs.GroupCreateCallback callback)
{
Callback.Set(ref GroupByContext.GroupCreate, callback, Pointers.GroupCreateCallbackDelegate);
GroupByContext.GroupCreate.Set(callback, Pointers.GroupCreateCallbackDelegate);
Desc.on_group_create = Pointers.GroupCreateCallback;
return ref this;
}
Expand All @@ -1725,7 +1725,7 @@ public ref QueryBuilder OnGroupCreate(Ecs.GroupCreateCallback callback)
/// <returns></returns>
public ref QueryBuilder OnGroupCreate<T>(Ecs.GroupCreateCallback<T> callback)
{
Callback.Set(ref GroupByContext.GroupCreate, callback, Pointers<T>.GroupCreateCallbackDelegate);
GroupByContext.GroupCreate.Set(callback, Pointers<T>.GroupCreateCallbackDelegate);
Desc.on_group_create = Pointers.GroupCreateCallback;
return ref this;
}
Expand All @@ -1737,7 +1737,7 @@ public ref QueryBuilder OnGroupCreate<T>(Ecs.GroupCreateCallback<T> callback)
/// <returns></returns>
public ref QueryBuilder OnGroupDelete(Ecs.GroupDeleteCallback callback)
{
Callback.Set(ref GroupByContext.GroupDelete, callback, Pointers.GroupDeleteCallbackDelegate);
GroupByContext.GroupDelete.Set(callback, Pointers.GroupDeleteCallbackDelegate);
Desc.on_group_delete = Pointers.GroupDeleteCallback;
return ref this;
}
Expand All @@ -1750,7 +1750,7 @@ public ref QueryBuilder OnGroupDelete(Ecs.GroupDeleteCallback callback)
/// <returns></returns>
public ref QueryBuilder OnGroupDelete<T>(Ecs.GroupDeleteCallback<T> callback)
{
Callback.Set(ref GroupByContext.GroupDelete, callback, Pointers<T>.GroupDeleteCallbackDelegate);
GroupByContext.GroupDelete.Set(callback, Pointers<T>.GroupDeleteCallbackDelegate);
Desc.on_group_delete = Pointers.GroupDeleteCallback;
return ref this;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Flecs.NET/Core/SystemBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ internal ref SystemBuilder SetCallback<T>(T callback, nint invoker) where T : De
{
FreeCallback();
IteratorContext context = default;
Callback.Set(ref context.Callback, callback, invoker);
context.Callback.Set(callback, invoker);
Desc.callback = Pointers.IteratorCallback;
Desc.callback_ctx = Memory.Alloc(context);
Desc.callback_ctx_free = Pointers.IteratorContextFree;
Expand All @@ -409,7 +409,7 @@ internal ref SystemBuilder SetCallback(nint callback, nint invoker)
{
FreeCallback();
IteratorContext context = default;
Callback.Set(ref context.Callback, callback, invoker);
context.Callback.Set(callback, invoker);
Desc.callback = Pointers.IteratorCallback;
Desc.callback_ctx = Memory.Alloc(context);
Desc.callback_ctx_free = Pointers.IteratorContextFree;
Expand All @@ -421,7 +421,7 @@ internal ref SystemBuilder SetRun<T>(T callback, nint invoker) where T : Delegat
{
FreeRun();
RunContext context = default;
Callback.Set(ref context.Callback, callback, invoker);
context.Callback.Set(callback, invoker);
Desc.run = Pointers.RunCallback;
Desc.run_ctx = Memory.Alloc(context);
Desc.run_ctx_free = Pointers.RunContextFree;
Expand All @@ -432,7 +432,7 @@ internal ref SystemBuilder SetRun(nint callback, nint invoker)
{
FreeRun();
RunContext context = default;
Callback.Set(ref context.Callback, callback, invoker);
context.Callback.Set(callback, invoker);
Desc.run = Pointers.RunCallback;
Desc.run_ctx = Memory.Alloc(context);
Desc.run_ctx_free = Pointers.RunContextFree;
Expand Down
8 changes: 4 additions & 4 deletions src/Flecs.NET/Core/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void Quit()
public void AtFini(Ecs.WorldFinishCallback callback)
{
WorldFinishContext* context = AllocateWorldFinishContext();
Callback.Set(ref context->Callback, callback, Pointers.WorldFinishCallbackDelegate);
context->Callback.Set(callback, Pointers.WorldFinishCallbackDelegate);
ecs_atfini(Handle, Pointers.WorldFinishCallback, context);
}

Expand All @@ -137,7 +137,7 @@ public void AtFini(Ecs.WorldFinishCallback callback)
public void AtFini(delegate*<World, void> callback)
{
WorldFinishContext* context = AllocateWorldFinishContext();
Callback.Set(ref context->Callback, (IntPtr)callback, Pointers.WorldFinishCallbackPointer);
context->Callback.Set((IntPtr)callback, Pointers.WorldFinishCallbackPointer);
ecs_atfini(Handle, Pointers.WorldFinishCallback, context);
}

Expand Down Expand Up @@ -2356,7 +2356,7 @@ public Entity MakeAlive(ulong entity)
public void RunPostFrame(Ecs.PostFrameCallback callback)
{
PostFrameContext* postFrameContext = AllocatePostFrameContext();
Callback.Set(ref postFrameContext->Callback, callback, Pointers.PostFrameCallbackDelegate);
postFrameContext->Callback.Set(callback, Pointers.PostFrameCallbackDelegate);
ecs_run_post_frame(Handle, Pointers.PostFrameCallback, postFrameContext);
}

Expand All @@ -2367,7 +2367,7 @@ public void RunPostFrame(Ecs.PostFrameCallback callback)
public void RunPostFrame(delegate*<void> callback)
{
PostFrameContext* postFrameContext = AllocatePostFrameContext();
Callback.Set(ref postFrameContext->Callback, (nint)callback, Pointers.PostFrameCallbackPointer);
postFrameContext->Callback.Set((nint)callback, Pointers.PostFrameCallbackPointer);
ecs_run_post_frame(Handle, Pointers.PostFrameCallback, postFrameContext);
}

Expand Down

0 comments on commit c8162b3

Please sign in to comment.