Compare commits

...
Sign in to create a new pull request.

2 commits

Author SHA1 Message Date
1a5452d6cc Improve fullName template
fullName now support multi-pararmeter templates
2023-05-24 22:15:05 +02:00
6a600d22c8 Refactored fullName template
New fullName implementation gives same result as fullyQualifiedName for normal types and templated ones, and still works in BetterC
2023-04-27 23:09:21 +02:00
4 changed files with 100 additions and 31 deletions

View file

@ -386,6 +386,7 @@ export struct EntityManager
else else
assert(pass < passes.length, "Update pass (ID " ~ pass.to!string ~ ") doesn't exist."); assert(pass < passes.length, "Update pass (ID " ~ pass.to!string ~ ") doesn't exist.");
// enum SystemName = __traits(fullyQualifiedName,Sys);
// enum SystemName = fullyQualifiedName!Sys; // enum SystemName = fullyQualifiedName!Sys;
enum SystemName = fullName!Sys; enum SystemName = fullName!Sys;
//enum SystemName = Sys.stringof; //enum SystemName = Sys.stringof;
@ -427,7 +428,8 @@ export struct EntityManager
{ {
alias EventParamType = Params[1]; alias EventParamType = Params[1];
enum EventName = fullName!(Unqual!(EventParamType)); 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); ushort evt = events_map.get(cast(char[]) EventName, ushort.max);
assert(evt != ushort.max, assert(evt != ushort.max,
"Can't register system \"" ~ SystemName "Can't register system \"" ~ SystemName
@ -489,8 +491,10 @@ export struct EntityManager
string name; string name;
static if (isArray!MemberType) static if (isArray!MemberType)
{ // Workaround. This code is never called with: not an array type, but compiler prints an error { // Workaround. This code is never called with: not an array type, but compiler prints an error
// name = fullyQualifiedName!(Unqual!(ForeachType!MemberType));//.stringof; // name = __traits(fullyQualifiedName,Unqual!(ForeachType!MemberType));
name = fullName!(Unqual!(typeof(MemberType.init[0]))); // name = fullyQualifiedName!(Unqual!(ForeachType!MemberType));
// name = fullName!(Unqual!(typeof(MemberType.init[0])));
name = fullName!(Unqual!(Unqual!(ForeachType!MemberType)));
} }
bool is_optional; bool is_optional;
@ -715,8 +719,9 @@ export struct EntityManager
string name; string name;
static if (isArray!MemberType) static if (isArray!MemberType)
{ // Workaround. This code is never called with: not an array type, but compiler prints an error { // 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 = fullyQualifiedName!(Unqual!(ForeachType!MemberType));
name = fullName!(Unqual!(typeof(MemberType.init[0]))); name = fullName!(Unqual!(ForeachType!MemberType));
//name = Unqual!(ForeachType!MemberType).stringof; //name = Unqual!(ForeachType!MemberType).stringof;
} }
@ -1296,6 +1301,7 @@ export struct EntityManager
{ {
ComponentInfo info; ComponentInfo info;
// enum ComponentName = __traits(fullyQualifiedName,Comp);
// enum ComponentName = fullyQualifiedName!Comp; // enum ComponentName = fullyQualifiedName!Comp;
enum ComponentName = fullName!Comp; enum ComponentName = fullName!Comp;
// enum ComponentName = Comp.stringof; // enum ComponentName = Comp.stringof;

View file

@ -63,34 +63,30 @@ static long getIndexOfTypeInEntitiesData(EntitiesData, Type)()
return index; return index;
} }
static string attachParentName(alias T, string str)() template fullName(alias T : X!A, alias X, A...)
{ {
alias parent = __traits(parent, T); alias parent = __traits(parent, X);
enum parent_str = parent.stringof; enum fullName = fullName!parent ~ '.' ~ __traits(identifier, X) ~ "!(" ~ fullName!A ~ ")";
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;
} }
static string fullName(T)() template fullName(T...)
{ {
return attachParentName!(T, T.stringof); 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)))
{
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;
} }

View file

@ -160,6 +160,22 @@ void afterEveryTest()
gEntityManager.destroy(); 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") @("EntityMeta")
unittest unittest
{ {

View file

@ -173,3 +173,54 @@ unittest
gEntityManager.destroy(); gEntityManager.destroy();
} }
@("3-template-system-compilation-file")
unittest
{
struct TemplateSystem(T)
{
struct EntitiesData
{
uint length;
}
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);
gEntityManager.beginRegister();
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();
}