Skip to content

Commit

Permalink
Update .Ctx() functions to support managed types, update app init cal…
Browse files Browse the repository at this point in the history
…lback signature
  • Loading branch information
BeanCheeseBurrito committed Nov 22, 2024
1 parent 0a7ff9c commit 1a27c4b
Show file tree
Hide file tree
Showing 87 changed files with 3,205 additions and 559 deletions.
67 changes: 46 additions & 21 deletions src/Flecs.NET.Codegen/Generators/Observer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public override void Generate()

private static string GenerateObserver(int i)
{
string typeName = Generator.GetTypeName(Type.Observer, i);
return $$"""
#nullable enable
Expand All @@ -31,7 +32,7 @@ namespace Flecs.NET.Core;
/// A type-safe wrapper around <see cref="Observer"/> that takes 16 type arguments.
/// </summary>
/// {{Generator.XmlTypeParameters[i]}}
public unsafe partial struct {{Generator.GetTypeName(Type.Observer, i)}} : IEquatable<{{Generator.GetTypeName(Type.Observer, i)}}>, IDisposable
public unsafe partial struct {{typeName}} : IDisposable, IEquatable<{{typeName}}>, IEntity<{{typeName}}>
{
private Observer _observer;
Expand Down Expand Up @@ -74,22 +75,46 @@ public void Dispose()
_observer.Dispose();
}
///
public void Ctx(void* ctx)
/// <inheritdoc cref="Observer.Ctx{T}(T)"/>
public void Ctx<T>(T value)
{
_observer.Ctx(ctx);
_observer.Ctx(ref value);
}
/// <inheritdoc cref="Observer.Ctx()"/>
public void* Ctx()
/// <inheritdoc cref="Observer.Ctx{T}(T, Ecs.UserContextFinish{T})"/>
public void Ctx<T>(T value, Ecs.UserContextFinish<T> callback)
{
return _observer.Ctx();
_observer.Ctx(ref value, callback);
}
/// <inheritdoc cref="Observer.Ctx{T}(T, Ecs.UserContextFinish{T})"/>
public void Ctx<T>(T value, delegate*<ref T, void> callback)
{
_observer.Ctx(ref value, callback);
}
/// <inheritdoc cref="Observer.Ctx{T}(ref T)"/>
public void Ctx<T>(ref T value)
{
_observer.Ctx(ref value);
}
/// <inheritdoc cref="Observer.Ctx{T}(ref T, Ecs.UserContextFinish{T})"/>
public void Ctx<T>(ref T value, Ecs.UserContextFinish<T> callback)
{
_observer.Ctx(ref value, callback);
}
/// <inheritdoc cref="Observer.Ctx{T}(ref T, Ecs.UserContextFinish{T})"/>
public void Ctx<T>(ref T value, delegate*<ref T, void> callback)
{
_observer.Ctx(ref value, callback);
}
/// <inheritdoc cref="Observer.Ctx{T}()"/>
public T* Ctx<T>() where T : unmanaged
public ref T Ctx<T>()
{
return _observer.Ctx<T>();
return ref _observer.Ctx<T>();
}
/// <inheritdoc cref="Observer.Query()"/>
Expand All @@ -99,51 +124,51 @@ public void Ctx(void* ctx)
}
/// <inheritdoc cref="Observer.ToUInt64"/>
public static implicit operator ulong({{Generator.GetTypeName(Type.Observer, i)}} observer)
public static implicit operator ulong({{typeName}} observer)
{
return ToUInt64(observer);
}
/// <inheritdoc cref="Observer.ToId"/>
public static implicit operator Id({{Generator.GetTypeName(Type.Observer, i)}} observer)
public static implicit operator Id({{typeName}} observer)
{
return ToId(observer);
}
/// <inheritdoc cref="Observer.ToEntity(Observer)"/>
public static implicit operator Entity({{Generator.GetTypeName(Type.Observer, i)}} observer)
public static implicit operator Entity({{typeName}} observer)
{
return ToEntity(observer);
}
/// <inheritdoc cref="Observer.ToUInt64"/>
public static ulong ToUInt64({{Generator.GetTypeName(Type.Observer, i)}} observer)
public static ulong ToUInt64({{typeName}} observer)
{
return observer.Entity;
}
/// <inheritdoc cref="Observer.ToId"/>
public static Id ToId({{Generator.GetTypeName(Type.Observer, i)}} observer)
public static Id ToId({{typeName}} observer)
{
return observer.Id;
}
/// <inheritdoc cref="Observer.ToEntity(Observer)"/>
public static Entity ToEntity({{Generator.GetTypeName(Type.Observer, i)}} observer)
public static Entity ToEntity({{typeName}} observer)
{
return observer.Entity;
}
/// <inheritdoc cref="Observer.Equals(Observer)"/>
public bool Equals({{Generator.GetTypeName(Type.Observer, i)}} other)
public bool Equals({{typeName}} other)
{
return _observer == other._observer;
}
/// <inheritdoc cref="Observer.Equals(object)"/>
public override bool Equals(object? obj)
{
return obj is {{Generator.GetTypeName(Type.Observer, i)}} other && Equals(other);
return obj is {{typeName}} other && Equals(other);
}
/// <inheritdoc cref="Observer.GetHashCode()"/>
Expand All @@ -153,13 +178,13 @@ public override int GetHashCode()
}
/// <inheritdoc cref="Observer.op_Equality"/>
public static bool operator ==({{Generator.GetTypeName(Type.Observer, i)}} left, {{Generator.GetTypeName(Type.Observer, i)}} right)
public static bool operator ==({{typeName}} left, {{typeName}} right)
{
return left.Equals(right);
}
/// <inheritdoc cref="Observer.op_Inequality"/>
public static bool operator !=({{Generator.GetTypeName(Type.Observer, i)}} left, {{Generator.GetTypeName(Type.Observer, i)}} right)
public static bool operator !=({{typeName}} left, {{typeName}} right)
{
return !(left == right);
}
Expand Down
41 changes: 38 additions & 3 deletions src/Flecs.NET.Codegen/Generators/ObserverBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,45 @@ public void Dispose()
return ref this;
}
///
public ref {{Generator.GetTypeName(Type.ObserverBuilder, i)}} Ctx(void* data)
/// <inheritdoc cref="ObserverBuilder.Ctx{T}(T)"/>
public ref {{Generator.GetTypeName(Type.ObserverBuilder, i)}} Ctx<T>(T value)
{
_observerBuilder.Ctx(data);
_observerBuilder.Ctx(ref value);
return ref this;
}
/// <inheritdoc cref="ObserverBuilder.Ctx{T}(T, Ecs.UserContextFinish{T})"/>
public ref {{Generator.GetTypeName(Type.ObserverBuilder, i)}} Ctx<T>(T value, Ecs.UserContextFinish<T> callback)
{
_observerBuilder.Ctx(ref value, callback);
return ref this;
}
/// <inheritdoc cref="ObserverBuilder.Ctx{T}(T, Ecs.UserContextFinish{T})"/>
public ref {{Generator.GetTypeName(Type.ObserverBuilder, i)}} Ctx<T>(T value, delegate*<ref T, void> callback)
{
_observerBuilder.Ctx(ref value, callback);
return ref this;
}
/// <inheritdoc cref="ObserverBuilder.Ctx{T}(ref T)"/>
public ref {{Generator.GetTypeName(Type.ObserverBuilder, i)}} Ctx<T>(ref T value)
{
_observerBuilder.Ctx(ref value);
return ref this;
}
/// <inheritdoc cref="ObserverBuilder.Ctx{T}(ref T, Ecs.UserContextFinish{T})"/>
public ref {{Generator.GetTypeName(Type.ObserverBuilder, i)}} Ctx<T>(ref T value, Ecs.UserContextFinish<T> callback)
{
_observerBuilder.Ctx(ref value, callback);
return ref this;
}
/// <inheritdoc cref="ObserverBuilder.Ctx{T}(ref T, Ecs.UserContextFinish{T})"/>
public ref {{Generator.GetTypeName(Type.ObserverBuilder, i)}} Ctx<T>(ref T value, delegate*<ref T, void> callback)
{
_observerBuilder.Ctx(ref value, callback);
return ref this;
}
Expand Down
41 changes: 38 additions & 3 deletions src/Flecs.NET.Codegen/Generators/SystemBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,45 @@ public void Dispose()
return ref this;
}
/// <inheritdoc cref="SystemBuilder.Ctx"/>
public ref {{Generator.GetTypeName(Type.SystemBuilder, i)}} Ctx(void* ctx)
/// <inheritdoc cref="SystemBuilder.Ctx{T}(T)"/>
public ref {{Generator.GetTypeName(Type.SystemBuilder, i)}} Ctx<T>(T value)
{
_systemBuilder.Ctx(ctx);
_systemBuilder.Ctx(ref value);
return ref this;
}
/// <inheritdoc cref="SystemBuilder.Ctx{T}(T, Ecs.UserContextFinish{T})"/>
public ref {{Generator.GetTypeName(Type.SystemBuilder, i)}} Ctx<T>(T value, Ecs.UserContextFinish<T> callback)
{
_systemBuilder.Ctx(ref value, callback);
return ref this;
}
/// <inheritdoc cref="SystemBuilder.Ctx{T}(T, Ecs.UserContextFinish{T})"/>
public ref {{Generator.GetTypeName(Type.SystemBuilder, i)}} Ctx<T>(T value, delegate*<ref T, void> callback)
{
_systemBuilder.Ctx(ref value, callback);
return ref this;
}
/// <inheritdoc cref="SystemBuilder.Ctx{T}(ref T)"/>
public ref {{Generator.GetTypeName(Type.SystemBuilder, i)}} Ctx<T>(ref T value)
{
_systemBuilder.Ctx(ref value);
return ref this;
}
/// <inheritdoc cref="SystemBuilder.Ctx{T}(ref T, Ecs.UserContextFinish{T})"/>
public ref {{Generator.GetTypeName(Type.SystemBuilder, i)}} Ctx<T>(ref T value, Ecs.UserContextFinish<T> callback)
{
_systemBuilder.Ctx(ref value, callback);
return ref this;
}
/// <inheritdoc cref="SystemBuilder.Ctx{T}(ref T, Ecs.UserContextFinish{T})"/>
public ref {{Generator.GetTypeName(Type.SystemBuilder, i)}} Ctx<T>(ref T value, delegate*<ref T, void> callback)
{
_systemBuilder.Ctx(ref value, callback);
return ref this;
}
Expand Down
50 changes: 43 additions & 7 deletions src/Flecs.NET.Codegen/Generators/System_.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace Flecs.NET.Core;
/// A type-safe wrapper around <see cref="System"/> that takes {{i + 1}} type arguments.
/// </summary>
/// {{Generator.XmlTypeParameters[i]}}
public unsafe partial struct {{systemTypeName}} : IEquatable<{{systemTypeName}}>, IEntity<{{systemTypeName}}>
public unsafe partial struct {{systemTypeName}} : IDisposable, IEquatable<{{systemTypeName}}>, IEntity<{{systemTypeName}}>
{
private System_ _system;
Expand Down Expand Up @@ -72,17 +72,53 @@ public System(Entity entity)
{{Generator.GetTypeName(Type.TypeHelper, i)}}.AssertNoTags();
_system = new System_(entity);
}
/// <inheritdoc cref="System_.Dispose"/>
public void Dispose()
{
_system.Dispose();
}
///
public void Ctx(void* ctx)
/// <inheritdoc cref="System_.Ctx{T}(T)"/>
public void Ctx<T>(T value)
{
_system.Ctx(ref value);
}
/// <inheritdoc cref="System_.Ctx{T}(T, Ecs.UserContextFinish{T})"/>
public void Ctx<T>(T value, Ecs.UserContextFinish<T> callback)
{
_system.Ctx(ref value, callback);
}
/// <inheritdoc cref="System_.Ctx{T}(T, Ecs.UserContextFinish{T})"/>
public void Ctx<T>(T value, delegate*<ref T, void> callback)
{
_system.Ctx(ref value, callback);
}
/// <inheritdoc cref="System_.Ctx{T}(ref T)"/>
public void Ctx<T>(ref T value)
{
_system.Ctx(ref value);
}
/// <inheritdoc cref="System_.Ctx{T}(ref T, Ecs.UserContextFinish{T})"/>
public void Ctx<T>(ref T value, Ecs.UserContextFinish<T> callback)
{
_system.Ctx(ref value, callback);
}
/// <inheritdoc cref="System_.Ctx{T}(ref T, Ecs.UserContextFinish{T})"/>
public void Ctx<T>(ref T value, delegate*<ref T, void> callback)
{
_system.Ctx(ctx);
_system.Ctx(ref value, callback);
}
/// <inheritdoc cref="System_.Ctx()"/>
public void* Ctx()
/// <inheritdoc cref="System_.Ctx{T}()"/>
public ref T Ctx<T>()
{
return _system.Ctx();
return ref _system.Ctx<T>();
}
/// <inheritdoc cref="System_.Query()"/>
Expand Down
2 changes: 1 addition & 1 deletion src/Flecs.NET.Examples/Systems/SystemCtx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void Main()
using Query<Position, Radius> qCollide = world.Query<Position, Radius>();

System<Position, Radius> system = world.System<Position, Radius>("Collide")
.Ctx(&qCollide)
.Ctx(qCollide)
.Each((Iter it, int i, ref Position p, ref Radius r) =>
{
ref Query<Position, Radius> q = ref it.Ctx<Query<Position, Radius>>();
Expand Down
27 changes: 27 additions & 0 deletions src/Flecs.NET.Tests/CSharp/Core/MiscTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Flecs.NET.Core;
using Xunit;

namespace Flecs.NET.Tests.CSharp.Core;

public class MiscTests
{
[Fact]
private void AppInit()
{
using World world = World.Create();

world.System()
.Each(static (Iter it, int _) =>
{
Assert.Equal("TestString", it.World().Get<string>());
});

world.App()
.Init(static (World world) =>
{
world.Set("TestString");
})
.Frames(1)
.Run();
}
}
Loading

0 comments on commit 1a27c4b

Please sign in to comment.