|
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
http://lists.boost.org/Archives/boost/2007/08/126006.php 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>::name("animal");
reflection_class<animal>::variable("age", &animal::age);
// etc
reflection_class<mammal>::name("mammal");
reflection_class<mammal>::base<animal>(virtual_);
// etc
reflection_class<carnivore>::name("carnivore");
reflection_class<carnivore>::base<animal>(virtual_);
// etc
reflection_class<dog>::name("dog");
reflection_class<dog>::base<mammal>();
reflection_class<dog>::base<carnivore>();
// a function
reflection_class<dog>::function("bark", &dog::bark);
// overloaded functions
reflection_class<dog>::function<string (dog::*)(void) const>("name",
&dog::name);
reflection_class<dog>::function<void (dog::*)(const string&)>("name",
&dog::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?
Jean-Louis
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk