On Sunday, 16 August 2015 at 01:39:54 UTC, DarthCthulhu wrote:
Say I want to do something like:
Propertery!int pi = 42;
PropertyCollection pc;
pc.attach("Life_and_Everything", pi);
assert(pc.Life_and_Everything == 42);
Property!string ps = "Hello World";
pc.attach("text", ps);
assert(pc.text == "Hello World");
How would one store the Property objects in the
PropertyCollection? You can't use something like an array
because the two Properties are of different types. Do you
really need to do something like make a member of
PropertyCollection for every type of Property you are
interested in storing and using static ifs to determine which
variable it goes into?
I feel like there is an obvious solution here that I'm missing.
You would need a kind of type info system:
---
enum Ti {tibyte, tiubyte, ...}
Ti getTypeInfo(T)(T t){statif is(t == ubyte) return Ti.tibyte;
else...}
pc.attach("Life_and_Everything", pi, getTypeInfo(pi));
pc.attach("BlaBlaBla", pi, getTypeInfo(pi));
pc.attach("zkfozekf", 42, getTypeInfo(42));
---
internally the container would store 3 infos (string, pointer to
data and a Ti) and when you query a property from its identifier,
it looks for the attached type info so that you can cast the
result (let's say it would return a pointer).
---
TypeInfo getTypeInfo(string identifier);
void* getValue(string identifier);
---
These kind of mechanism are really common in languages that have
a poor/none compile time reflection features, but in D you would
have to build your own run time type info system. My example is a
bit shitty but you should get the idea.