Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Id and Entity extension generator #74

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Flecs.NET.Codegen/Generators/Alert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Flecs.NET.Codegen.Helpers;

namespace Flecs.NET.Codegen.Generators;

Check warning on line 3 in src/Flecs.NET.Codegen/Generators/Alert.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Convert to block scoped namespace (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0160)

Check warning on line 3 in src/Flecs.NET.Codegen/Generators/Alert.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Convert to block scoped namespace (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0160)

public class Alert : GeneratorBase
{
public override void Generate()
{
AddSource($"Alert.Id.g.cs", Id.GenerateExtensions(Type.Alert));
AddSource($"Alert.Entity.g.cs", Entity.GenerateExtensions(Type.Alert));
}
}
12 changes: 12 additions & 0 deletions src/Flecs.NET.Codegen/Generators/Component.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Flecs.NET.Codegen.Helpers;

namespace Flecs.NET.Codegen.Generators;

Check warning on line 3 in src/Flecs.NET.Codegen/Generators/Component.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Convert to block scoped namespace (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0160)

Check warning on line 3 in src/Flecs.NET.Codegen/Generators/Component.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Convert to block scoped namespace (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0160)

public class Component : GeneratorBase
{
public override void Generate()
{
AddSource($"Component.Id.g.cs", Id.GenerateExtensions(Type.Component));
AddSource($"Component.Entity.g.cs", Entity.GenerateExtensions(Type.Component));
}
}
2,004 changes: 2,000 additions & 4 deletions src/Flecs.NET.Codegen/Generators/Entity.cs

Large diffs are not rendered by default.

124 changes: 124 additions & 0 deletions src/Flecs.NET.Codegen/Generators/Id.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Flecs.NET.Codegen.Helpers;

namespace Flecs.NET.Codegen.Generators;

Check warning on line 3 in src/Flecs.NET.Codegen/Generators/Id.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Convert to block scoped namespace (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0160)

Check warning on line 3 in src/Flecs.NET.Codegen/Generators/Id.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Convert to block scoped namespace (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0160)

public static class Id
{
public static string GenerateExtensions(Type type, int i = -1)
{
return $$"""
namespace Flecs.NET.Core;

public unsafe partial struct {{Generator.GetTypeName(type, i)}}
{
/// <inheritdoc cref="Id.IsPair()"/>
public bool IsPair()
{
return Id.IsPair();
}

/// <inheritdoc cref="Id.IsWildCard()"/>
public bool IsWildCard()
{
return Id.IsWildCard();
}

/// <inheritdoc cref="Id.IsEntity()"/>
public bool IsEntity()
{
return Id.IsEntity();
}

/// <inheritdoc cref="Id.ToEntity()"/>
public Entity ToEntity()
{
return Id.ToEntity();
}

/// <inheritdoc cref="Id.AddFlags(ulong)"/>
public Entity AddFlags(ulong flags)
{
return Id.AddFlags(flags);
}

/// <inheritdoc cref="Id.RemoveFlags(ulong)"/>
public Entity RemoveFlags(ulong flags)
{
return Id.RemoveFlags(flags);
}

/// <inheritdoc cref="Id.RemoveFlags()"/>
public Entity RemoveFlags()
{
return Id.RemoveFlags();
}

/// <inheritdoc cref="Id.RemoveGeneration()"/>
public Entity RemoveGeneration()
{
return Id.RemoveGeneration();
}

/// <inheritdoc cref="Id.TypeId()"/>
public Entity TypeId()
{
return Id.TypeId();
}

/// <inheritdoc cref="Id.HasFlags(ulong)"/>
public bool HasFlags(ulong flags)
{
return Id.HasFlags(flags);
}

/// <inheritdoc cref="Id.HasFlags()"/>
public bool HasFlags()
{
return Id.HasFlags();
}

/// <inheritdoc cref="Id.Flags()"/>
public Entity Flags()
{
return Id.Flags();
}

/// <inheritdoc cref="Id.HasRelation(ulong)"/>
public bool HasRelation(ulong first)
{
return Id.HasRelation(first);
}

/// <inheritdoc cref="Id.First()"/>
public Entity First()
{
return Id.First();
}

/// <inheritdoc cref="Id.Second()"/>
public Entity Second()
{
return Id.Second();
}

/// <inheritdoc cref="Id.Str()"/>
public string Str()
{
return Id.Str();
}

/// <inheritdoc cref="Id.FlagsStr()"/>
public string FlagsStr()
{
return Id.FlagsStr();
}

/// <inheritdoc cref="Id.CsWorld()"/>
public World CsWorld()
{
return Id.CsWorld();
}
}
""";
}
}
17 changes: 8 additions & 9 deletions src/Flecs.NET.Codegen/Generators/Observer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ public class Observer : GeneratorBase
{
public override void Generate()
{
AddSource($"Observer.Id.g.cs", Id.GenerateExtensions(Type.Observer));
AddSource($"Observer.Entity.g.cs", Entity.GenerateExtensions(Type.Observer));

for (int i = 0; i < Generator.GenericCount; i++)
{
AddSource($"Observer/T{i + 1}.g.cs", GenerateObserver(i));
AddSource($"Observer.Id/T{i + 1}.g.cs", Id.GenerateExtensions(Type.Observer, i));
AddSource($"Observer.Entity/T{i + 1}.g.cs", Entity.GenerateExtensions(Type.Observer, i));
}
}

Expand All @@ -26,7 +31,7 @@ namespace Flecs.NET.Core;
/// A type-safe wrapper around <see cref="Observer"/> that takes 16 type arguments.
/// </summary>
/// {{Generator.XmlTypeParameters[i]}}
public unsafe struct {{Generator.GetTypeName(Type.Observer, i)}} : IEquatable<{{Generator.GetTypeName(Type.Observer, i)}}>, IDisposable
public unsafe partial struct {{Generator.GetTypeName(Type.Observer, i)}} : IEquatable<{{Generator.GetTypeName(Type.Observer, i)}}>, IDisposable
{
private Observer _observer;

Expand Down Expand Up @@ -69,12 +74,6 @@ public void Dispose()
_observer.Dispose();
}

/// <inheritdoc cref="Observer.Destruct"/>
public void Destruct()
{
_observer.Destruct();
}

///
public void Ctx(void* ctx)
{
Expand Down Expand Up @@ -111,7 +110,7 @@ public static implicit operator Id({{Generator.GetTypeName(Type.Observer, i)}} o
return ToId(observer);
}

/// <inheritdoc cref="Observer.ToEntity"/>
/// <inheritdoc cref="Observer.ToEntity(Observer)"/>
public static implicit operator Entity({{Generator.GetTypeName(Type.Observer, i)}} observer)
{
return ToEntity(observer);
Expand All @@ -129,7 +128,7 @@ public static Id ToId({{Generator.GetTypeName(Type.Observer, i)}} observer)
return observer.Id;
}

/// <inheritdoc cref="Observer.ToEntity"/>
/// <inheritdoc cref="Observer.ToEntity(Observer)"/>
public static Entity ToEntity({{Generator.GetTypeName(Type.Observer, i)}} observer)
{
return observer.Entity;
Expand Down
29 changes: 18 additions & 11 deletions src/Flecs.NET.Codegen/Generators/Pipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ public class Pipeline : GeneratorBase
{
public override void Generate()
{
AddSource($"Pipeline.Id.g.cs", Id.GenerateExtensions(Type.Pipeline));
AddSource($"Pipeline.Entity.g.cs", Entity.GenerateExtensions(Type.Pipeline));

for (int i = 0; i < Generator.GenericCount; i++)
{
AddSource($"Pipeline/T{i + 1}.g.cs", GeneratePipeline(i));
AddSource($"Pipeline.Id/T{i + 1}.g.cs", Id.GenerateExtensions(Type.Pipeline, i));
AddSource($"Pipeline.Entity/T{i + 1}.g.cs", Entity.GenerateExtensions(Type.Pipeline, i));
}
}

private static string GeneratePipeline(int i)
{
string typeName = Generator.GetTypeName(Type.Pipeline, i);

return $$"""
#nullable enable

Expand All @@ -26,7 +33,7 @@ namespace Flecs.NET.Core;
/// A type-safe wrapper around <see cref="Pipeline"/> that takes {{i + 1}} type arguments.
/// </summary>
/// {{Generator.XmlTypeParameters[i]}}
public unsafe partial struct {{Generator.GetTypeName(Type.Pipeline, i)}} : IEquatable<{{Generator.GetTypeName(Type.Pipeline, i)}}>, IEntity
public unsafe partial struct {{typeName}} : IEquatable<{{typeName}}>, IEntity<{{typeName}}>
{
private Pipeline _pipeline;

Expand Down Expand Up @@ -61,37 +68,37 @@ public Pipeline(Entity entity)
}

/// <inheritdoc cref="Pipeline.ToUInt64(Pipeline)"/>
public static implicit operator ulong({{Generator.GetTypeName(Type.Pipeline, i)}} pipeline)
public static implicit operator ulong({{typeName}} pipeline)
{
return ToUInt64(pipeline);
}

/// <inheritdoc cref="Pipeline.ToId(Pipeline)"/>
public static implicit operator Id({{Generator.GetTypeName(Type.Pipeline, i)}} pipeline)
public static implicit operator Id({{typeName}} pipeline)
{
return ToId(pipeline);
}

/// <inheritdoc cref="Pipeline.ToEntity(Pipeline)"/>
public static implicit operator Entity({{Generator.GetTypeName(Type.Pipeline, i)}} pipeline)
public static implicit operator Entity({{typeName}} pipeline)
{
return ToEntity(pipeline);
}

/// <inheritdoc cref="Pipeline.ToUInt64(Pipeline)"/>
public static ulong ToUInt64({{Generator.GetTypeName(Type.Pipeline, i)}} pipeline)
public static ulong ToUInt64({{typeName}} pipeline)
{
return pipeline.Entity;
}

/// <inheritdoc cref="Pipeline.ToId(Pipeline)"/>
public static Id ToId({{Generator.GetTypeName(Type.Pipeline, i)}} pipeline)
public static Id ToId({{typeName}} pipeline)
{
return pipeline.Id;
}

/// <inheritdoc cref="Pipeline.ToEntity(Pipeline)"/>
public static Entity ToEntity({{Generator.GetTypeName(Type.Pipeline, i)}} pipeline)
public static Entity ToEntity({{typeName}} pipeline)
{
return pipeline.Entity;
}
Expand All @@ -103,15 +110,15 @@ public override string ToString()
}

/// <inheritdoc cref="Pipeline.Equals(Pipeline)"/>
public bool Equals({{Generator.GetTypeName(Type.Pipeline, i)}} other)
public bool Equals({{typeName}} other)
{
return Entity == other.Entity;
}

/// <inheritdoc cref="Pipeline.Equals(object)"/>
public override bool Equals(object? obj)
{
return obj is {{Generator.GetTypeName(Type.Pipeline, i)}} other && Equals(other);
return obj is {{typeName}} other && Equals(other);
}

/// <inheritdoc cref="Pipeline.GetHashCode()"/>
Expand All @@ -121,13 +128,13 @@ public override int GetHashCode()
}

/// <inheritdoc cref="Pipeline.op_Equality"/>
public static bool operator ==({{Generator.GetTypeName(Type.Pipeline, i)}} left, {{Generator.GetTypeName(Type.Pipeline, i)}} right)
public static bool operator ==({{typeName}} left, {{typeName}} right)
{
return left.Equals(right);
}

/// <inheritdoc cref="Pipeline.op_Inequality"/>
public static bool operator !=({{Generator.GetTypeName(Type.Pipeline, i)}} left, {{Generator.GetTypeName(Type.Pipeline, i)}} right)
public static bool operator !=({{typeName}} left, {{typeName}} right)
{
return !(left == right);
}
Expand Down
17 changes: 8 additions & 9 deletions src/Flecs.NET.Codegen/Generators/System_.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ public class System_ : GeneratorBase
{
public override void Generate()
{
AddSource($"System.Id.g.cs", Id.GenerateExtensions(Type.System_));
AddSource($"System.Entity.g.cs", Entity.GenerateExtensions(Type.System_));

for (int i = 0; i < Generator.GenericCount; i++)
{
AddSource($"System/T{i + 1}.g.cs", GenerateSystem(i));
AddSource($"System.Id/T{i + 1}.g.cs", Id.GenerateExtensions(Type.System, i));
AddSource($"System.Entity/T{i + 1}.g.cs", Entity.GenerateExtensions(Type.System, i));
}
}

Expand All @@ -31,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 struct {{systemTypeName}} : IEquatable<{{systemTypeName}}>, IEntity
public unsafe partial struct {{systemTypeName}} : IEquatable<{{systemTypeName}}>, IEntity<{{systemTypeName}}>
{
private System_ _system;

Expand Down Expand Up @@ -68,12 +73,6 @@ public System(Entity entity)
_system = new System_(entity);
}

/// <inheritdoc cref="System_.Destruct()"/>
public void Destruct()
{
_system.Destruct();
}

///
public void Ctx(void* ctx)
{
Expand Down Expand Up @@ -188,7 +187,7 @@ public static implicit operator Id({{systemTypeName}} system)
return ToId(system);
}

/// <inheritdoc cref="System_.ToEntity"/>
/// <inheritdoc cref="System_.ToEntity(System_)"/>
public static implicit operator Entity({{systemTypeName}} system)
{
return ToEntity(system);
Expand All @@ -206,7 +205,7 @@ public static Id ToId({{systemTypeName}} system)
return system.Id;
}

/// <inheritdoc cref="System_.ToEntity"/>
/// <inheritdoc cref="System_.ToEntity(System_)"/>
public static Entity ToEntity({{systemTypeName}} system)
{
return system.Entity;
Expand Down
12 changes: 12 additions & 0 deletions src/Flecs.NET.Codegen/Generators/TimerEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Flecs.NET.Codegen.Helpers;

namespace Flecs.NET.Codegen.Generators;

public class TimerEntity : GeneratorBase
{
public override void Generate()
{
AddSource($"TimerEntity.Id.g.cs", Id.GenerateExtensions(Type.TimerEntity));
AddSource($"TimerEntity.Entity.g.cs", Entity.GenerateExtensions(Type.TimerEntity));
}
}
12 changes: 12 additions & 0 deletions src/Flecs.NET.Codegen/Generators/UntypedComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Flecs.NET.Codegen.Helpers;

namespace Flecs.NET.Codegen.Generators;

public class UntypedComponent : GeneratorBase
{
public override void Generate()
{
AddSource($"UntypedComponent.Id.g.cs", Id.GenerateExtensions(Type.UntypedComponent));
AddSource($"UntypedComponent.Entity.g.cs", Entity.GenerateExtensions(Type.UntypedComponent));
}
}
Loading
Loading