xxxxxxxxxx
module app;
void main()
{
auto n = new Node();
auto r = new RootNode();
auto s = new SpriteNode();
auto nt = new NotTagged();
auto un = new Unique();
workFunction(nt);
workFunction(un);
workFunction(n);
workFunction(r);
workFunction(s);
}
class NotTagged : Node{}
/* https://issues.dlang.org/show_bug.cgi?id=18718
static foreach(m; __traits(allMembers, mixin("app")))
{
pragma(msg, m);
}
*/
class Node{}
class RootNode : Node{}
class SpriteNode : Node{}
class Unique : Node{}
template isSpecified(T)
{
bool isSpecified(U)()
{
return is(T == U);
}
}
void workFunction(T)(T object)
{
import std.meta : anySatisfy, staticIndexOf;
import std.stdio : writeln;
/*
case for non exclusive tags, the else branch handle an abstract, untagged case.
*/
static if(__traits(getAttributes, T).length)
{
static if(anySatisfy!(isSpecified!SpriteNode, __traits(getAttributes, T)))
{
writeln("got SpriteNode");
}
static if(anySatisfy!(isSpecified!RootNode, __traits(getAttributes, T)))
{
writeln("got RootNode");
}
// nothing happens, when there is a tag, which is not explicitely described.
}
else
{
writeln("got something abstract");
writeln(__traits(getAttributes, T).stringof);
}
/*
case for exclusive tags, there can be only one, because of "else static if's".
Case "without a tag" is asserted as false.
*/
static if(__traits(getAttributes, T).length)
{
static if(staticIndexOf!(SpriteNode, __traits(getAttributes, T)) > -1)
{
writeln("got SpriteNode");
}
else static if(staticIndexOf!(RootNode, __traits(getAttributes, T)) > -1)
{
writeln("got RootNode");
}
else
{
writeln("got something abstract");
writeln(__traits(getAttributes, T).stringof);
}
}
else
{
writeln("got an untagged class");
}
}