Introspection/Reflection/etc on Linux

J Arrizza cppgent0 at gmail.com
Thu Nov 3 23:29:17 PDT 2011


And here's a working example:

import std.stdio;
import variant;
import std.string;
import std.conv;

//----
class Base
  {
    private int y;  //check if a variable causes any problems

    //run all member functions that start with "in"
    public void RunAllIn(alias T) ()
    {
      writeln("RunAllIn: ", typeid(T));
      Variant.__register!T;
      Variant var = this;
      foreach (i, m; __traits(derivedMembers, T))
        {
        if (!startsWith(m, "in"))
          {
            continue;
          }

        writeln("== i=", i, "  m=", m);
        var.__reflect(m);
        }
    }
  }

//----
class Bob : Base
  {
    private int x;  //check if a variable causes any problems
    //private int inx;  //causes an exception

    this()
      {
        RunAllIn!Bob;
      }

    public void inBob1()
      {
        writeln("   in bob : inBob1()");
      }

    public void inBob2()
      {
        writeln("   in bob : inBob2()");
      }
  }

//----
class Jane: Base
  {
    this()
      {
        RunAllIn!Jane;
      }

    public void inJane1()
      {
        writeln("   in jane : inJane1()");
      }

    public void inJane2()
      {
        writeln("   in jane : inJane2()");
      }
  }

void main(string[] args)
  {
    new Bob();
    new Jane();
  }


The output is:

$ rm -f dtest; dmd   dtest.d variant.d  -ofdtest; ./dtest
RunAllIn: dtest.Bob
== i=2  m=inBob1
   in bob : inBob1()
== i=3  m=inBob2
   in bob : inBob2()
RunAllIn: dtest.Jane
== i=1  m=inJane1
   in jane : inJane1()
== i=2  m=inJane2
   in jane : inJane2()


Perfect, thanks Robert! Works like a charm.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20111103/512f1adc/attachment-0001.html>


More information about the Digitalmars-d mailing list