On 2011-10-24 05:37, J Arrizza wrote:
Robert, I tried .stringof and it didn't quite get me as far as I wanted.
But the using a template got me much closer:
unittest
{
//create bob to kick off the whole thing
new Bob();
}
class Base
{
// find all the derived members and run them
// Base doesn't have to know anything about the derived classes
public void runallin(alias T) ()
{
void delegate() dg;
//create a delegate and run it
foreach (i, m; __traits(derivedMembers, T))
{
//skip ctor, no need to run it again.
if (m != "__ctor")
{
writeln("== i=", i, " m=", m);
//by trial and error, I found I need to use
// 5 + the number of methods in class Base (exclude ctor)
//right now, no additional methods, so 5 it is.
dg.funcptr = cast(void function()) this.classinfo.vtbl[i
+ 5];
dg.ptr = cast(void*) this;
writeln(" calling dg");
dg();
}
}
}
}
// the class to auto-run
class Bob: Base
{
this()
{
runallin!Bob;
}
public void inBob1()
{
writeln(" in bob : inBob1()");
}
public void inBob2()
{
writeln(" in bob : inBob2()");
}
}
Here's the output:
== i=1 m=inBob1
calling dg
in bob : inBob1()
== i=2 m=inBob2
calling dg
in bob : inBob2()
Getting closer. Next:
- check the signature of the methods (I want to run void fn(void) only)
- find all the classes in a given module so I don't have to "new Bob()"
You can foreach over ModuleInfo and access the classes using
"localClasses". Have a look at the implementation of
object.TypeInfo_Class.find in druntime.
--
/Jacob Carlborg