Boost logo

Boost :

Subject: [boost] Reflection?
From: Jean-Louis Leroy (jl_at_[hidden])
Date: 2008-11-01 00:20:42

Is runtime reflection going to be available in boost soon? The last I
have found on the subject is but I see
nothing in the sandbox nor the vault.

I have toyed with an implementation myself. It supports member variables
and functions, multiple and virtual inheritance and object creation.

Here is what it looks like.

First a small hierarchy:

  class animal { /* ... */ };
  class mammal : public virtual animal { /* ... */ };
  class carnivore : public virtual animal{ /* ... */ };
  class dog : public mammal, public carnivore{ /* ... */ };
Reflection info is filled like this:

  reflection_class<animal>::variable("age", &animal::age);
  // etc

  // etc

  // etc
  // a function
  reflection_class<dog>::function("bark", &dog::bark);
  // overloaded functions
  reflection_class<dog>::function<string (dog::*)(void) const>("name",
  reflection_class<dog>::function<void (dog::*)(const string&)>("name",
  // constructor
  reflection_class<dog>::constructor<void* (*)()>();
  // etc

And here how it's used:

  animal* pa(new dog);

  object o(downcast(pa)); // recover dynamic type

  // access member variable inherited from virtual base `animal'
  member_variable<int> dog_age(o, "age");
  int age = o[dog_age];
  // call member function
  member_function<string (object::*)() const> dog_name(o, "name");
  string name = o[dog_name]();

  // create a dog using default ctor
  object o("dog");

Ok, there is a bit more to it (e.g. support macros, ambiguity detection,
handling of shadowed members, overloaded ctors) but the important part
is shown above. All in all it is a foundation upon which one can build
multimethods, generic object processing (like Perl's data::Dumper),
serialization, object-relational mapping and all sorts of nice tools.

Does anybody see interest in this work?


Boost list run by bdawes at, gregod at, cpdaniel at, john at