D RTTI?

Artur Skawina art.08.09 at gmail.com
Mon Mar 5 16:51:51 PST 2012


On 03/05/12 21:16, H. S. Teoh wrote:
> I know D doesn't really have RTTI yet, but I'm experimenting with
> "faking" it by doing something like:
> 
> 	class A {
> 		string prop1;
> 		int prop2;
> 		...
> 		void serialize() {
> 			__serialize(this);
> 		}
> 	}
> 
> 	void __serialize(T)(T obj) {
> 		writeln(typeid(obj));
> 		foreach (name; __traits(derivedMembers, T)) {
> 			writefln("%s = %s", name,
> 				__traits(getMember,obj,name));
> 		}
> 	}
> 
> The only thing is, serialize() has to be declared in every derived
> class, because T needs to be known at compile-time. Is there a way to
> "automate" this? I.e., automatically insert the serialize() boilerplate
> code into derived classes?

Well, what exactly are you trying to do? IOW why "derivedMembers" - do you
really need to skip serializing parts of the class hierarchy?

If not, this should work.
 
class A {
   string prop1;
   int prop2;

   void serialize(this THIS)() {
      __serialize(cast(THIS*)&this);
   }
}

void __serialize(T)(T* obj) {
   writef("%s {\n", typeid(*obj));
   foreach (name; __traits(allMembers, T)) {
      static if (__traits(compiles,&__traits(getMember,obj,name))) {
         alias typeof(__traits(getMember,obj,name)) MT;
         static if (is(MT==function))
            continue;
         else {
            auto m = __traits(getMember,obj,name);
            if (is(MT:const(char[])))
               writef("   %s %s = \"%s\";\n", typeid(MT), name, m);
            else
               writef("   %s %s = %s;\n", typeid(MT), name, m);
         }
      }
   }
   writef("}\n");
}

And it will do the right thing for derived classes too.

Real programmers don't use mixins, :^)

artur


More information about the Digitalmars-d-learn mailing list