From 6a600d22c812a879b5991329067cb2f7c42debfd Mon Sep 17 00:00:00 2001 From: Mergul Date: Thu, 27 Apr 2023 23:08:27 +0200 Subject: [PATCH 1/2] Refactored fullName template New fullName implementation gives same result as fullyQualifiedName for normal types and templated ones, and still works in BetterC --- source/bubel/ecs/traits.d | 37 +++++++++++-------------------------- tests/basic.d | 16 ++++++++++++++++ tests/bugs.d | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 27 deletions(-) diff --git a/source/bubel/ecs/traits.d b/source/bubel/ecs/traits.d index 79901db..e18f656 100644 --- a/source/bubel/ecs/traits.d +++ b/source/bubel/ecs/traits.d @@ -63,34 +63,19 @@ static long getIndexOfTypeInEntitiesData(EntitiesData, Type)() return index; } -static string attachParentName(alias T, string str)() +template fullName(alias T : X!A, alias X, A...) { - alias parent = __traits(parent, T); - enum parent_str = parent.stringof; - static if(parent_str[0..7] == "module ") - { - static if(__traits(compiles, __traits(parent, parent))) - { - return attachParentName!(parent, parent_str[7 .. $] ~ '.' ~ str); - } - else return parent_str[7 .. $] ~ '.' ~ str; - } - else static if(parent_str[0..8] == "package ") - { - static if(__traits(compiles, __traits(parent, parent))) - { - return attachParentName!(parent, parent_str[8 .. $] ~ '.' ~ str); - } - else return parent_str[8 .. $] ~ '.' ~ str; - } - else static if(__traits(compiles, __traits(parent, parent))) - { - return attachParentName!(parent, parent_str ~ '.' ~ str); - } - else return parent_str ~ '.' ~ str; + alias parent = __traits(parent, X); + enum fullName = fullName!parent ~ '.' ~ __traits(identifier, X) ~ "!(" ~ fullName!A ~ ")"; } -static string fullName(T)() +template fullName(alias T) { - return attachParentName!(T, T.stringof); + static if(__traits(compiles, __traits(parent, T))) + { + alias parent = __traits(parent, T); + enum fullName = fullName!parent ~ '.' ~ __traits(identifier, T); + } + else static if(__traits(compiles, __traits(identifier, T)))enum fullName = __traits(identifier, T); + else enum fullName = T.stringof; } \ No newline at end of file diff --git a/tests/basic.d b/tests/basic.d index ea54382..e353549 100644 --- a/tests/basic.d +++ b/tests/basic.d @@ -160,6 +160,22 @@ void afterEveryTest() gEntityManager.destroy(); } +@("RegisteredNames") +unittest +{ + import bubel.ecs.traits; + + gEntityManager.beginRegister(); + gEntityManager.registerSystem!EmptySystem(0); + gEntityManager.registerSystem!EntityCounterSystem(0); + gEntityManager.endRegister(); + + System* empty_system = gEntityManager.getSystem(becsID!EmptySystem); + System* counter_system = gEntityManager.getSystem(becsID!EntityCounterSystem); + assert(empty_system.name == "tests.basic.EmptySystem", fullName!EmptySystem); + assert(counter_system.name == "tests.basic.EntityCounterSystem", fullName!EntityCounterSystem); +} + @("EntityMeta") unittest { diff --git a/tests/bugs.d b/tests/bugs.d index 10a3c62..9b59973 100644 --- a/tests/bugs.d +++ b/tests/bugs.d @@ -172,4 +172,35 @@ unittest gEntityManager.commit(); gEntityManager.destroy(); -} \ No newline at end of file +} + +@("3-template-system-compilation-file") +unittest +{ + struct TemplateSystem(T) + { + struct EntitiesData + { + uint length; + } + + uint a; + uint b; + } + + struct TempalteComponent(T) + { + T parameter; + } + + gEntityManager.initialize(0); + + gEntityManager.beginRegister(); + + gEntityManager.registerComponent!CInt; + gEntityManager.registerComponent!(TempalteComponent!uint); + gEntityManager.registerSystem!(TemplateSystem!CInt)(0); + + gEntityManager.endRegister(); + +} From 1a5452d6cc5ac0261b21b70fd45722d142ac06c4 Mon Sep 17 00:00:00 2001 From: Mergul Date: Wed, 24 May 2023 22:15:05 +0200 Subject: [PATCH 2/2] Improve fullName template fullName now support multi-pararmeter templates --- source/bubel/ecs/manager.d | 14 ++++++++++---- source/bubel/ecs/traits.d | 11 +++++++++++ tests/bugs.d | 24 ++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/source/bubel/ecs/manager.d b/source/bubel/ecs/manager.d index d6fae94..26db180 100644 --- a/source/bubel/ecs/manager.d +++ b/source/bubel/ecs/manager.d @@ -386,6 +386,7 @@ export struct EntityManager else assert(pass < passes.length, "Update pass (ID " ~ pass.to!string ~ ") doesn't exist."); + // enum SystemName = __traits(fullyQualifiedName,Sys); // enum SystemName = fullyQualifiedName!Sys; enum SystemName = fullName!Sys; //enum SystemName = Sys.stringof; @@ -427,7 +428,8 @@ export struct EntityManager { alias EventParamType = Params[1]; enum EventName = fullName!(Unqual!(EventParamType)); - // enum EventName = fullyQualifiedName!(Unqual!(EventParamType));//.stringof; + // enum EventName = __traits(fullyQualifiedName,Unqual!(EventParamType)); + // enum EventName = fullyQualifiedName!(Unqual!(EventParamType)); ushort evt = events_map.get(cast(char[]) EventName, ushort.max); assert(evt != ushort.max, "Can't register system \"" ~ SystemName @@ -489,8 +491,10 @@ export struct EntityManager string name; static if (isArray!MemberType) { // Workaround. This code is never called with: not an array type, but compiler prints an error - // name = fullyQualifiedName!(Unqual!(ForeachType!MemberType));//.stringof; - name = fullName!(Unqual!(typeof(MemberType.init[0]))); + // name = __traits(fullyQualifiedName,Unqual!(ForeachType!MemberType)); + // name = fullyQualifiedName!(Unqual!(ForeachType!MemberType)); + // name = fullName!(Unqual!(typeof(MemberType.init[0]))); + name = fullName!(Unqual!(Unqual!(ForeachType!MemberType))); } bool is_optional; @@ -715,8 +719,9 @@ export struct EntityManager string name; static if (isArray!MemberType) { // Workaround. This code is never called with: not an array type, but compiler prints an error + // name = __traits(fullyQualifiedName,Unqual!(ForeachType!MemberType)); // name = fullyQualifiedName!(Unqual!(ForeachType!MemberType)); - name = fullName!(Unqual!(typeof(MemberType.init[0]))); + name = fullName!(Unqual!(ForeachType!MemberType)); //name = Unqual!(ForeachType!MemberType).stringof; } @@ -1296,6 +1301,7 @@ export struct EntityManager { ComponentInfo info; + // enum ComponentName = __traits(fullyQualifiedName,Comp); // enum ComponentName = fullyQualifiedName!Comp; enum ComponentName = fullName!Comp; // enum ComponentName = Comp.stringof; diff --git a/source/bubel/ecs/traits.d b/source/bubel/ecs/traits.d index e18f656..24bc698 100644 --- a/source/bubel/ecs/traits.d +++ b/source/bubel/ecs/traits.d @@ -69,6 +69,17 @@ template fullName(alias T : X!A, alias X, A...) enum fullName = fullName!parent ~ '.' ~ __traits(identifier, X) ~ "!(" ~ fullName!A ~ ")"; } +template fullName(T...) +{ + static if(__traits(compiles, __traits(parent, T[0]))) + { + alias parent = __traits(parent, T[0]); + enum fullName = fullName!parent ~ '.' ~ __traits(identifier, T[0]) ~ ", " ~ fullName!(T[1 .. $]); + } + else static if(__traits(compiles, __traits(identifier, T[0])))enum fullName = __traits(identifier, T[0]) ~ ", " ~ fullName!(T[1 .. $]); + else enum fullName = T[0].stringof ~ ", " ~ fullName!(T[1 .. $]); +} + template fullName(alias T) { static if(__traits(compiles, __traits(parent, T))) diff --git a/tests/bugs.d b/tests/bugs.d index 9b59973..5eaa8aa 100644 --- a/tests/bugs.d +++ b/tests/bugs.d @@ -177,6 +177,7 @@ unittest @("3-template-system-compilation-file") unittest { + struct TemplateSystem(T) { struct EntitiesData @@ -184,14 +185,31 @@ unittest uint length; } - uint a; - uint b; + T a; + } + + struct TemplateSystem2(T, T2, T3) + { + struct EntitiesData + { + uint length; + } + + T a; + T2 b; + T3 c; } struct TempalteComponent(T) { T parameter; } + + struct TempalteComponent2(T, T2) + { + T parameter; + T2 parameter2; + } gEntityManager.initialize(0); @@ -199,7 +217,9 @@ unittest gEntityManager.registerComponent!CInt; gEntityManager.registerComponent!(TempalteComponent!uint); + gEntityManager.registerComponent!(TempalteComponent2!(float,int)); gEntityManager.registerSystem!(TemplateSystem!CInt)(0); + gEntityManager.registerSystem!(TemplateSystem2!(CInt, uint, float))(0); gEntityManager.endRegister();