Re: Walking the inheritance tree
On Jun 8, 9:21 pm, Sebastian Karlsson <Sebastian.Karls...@mmorpgs.org>
wrote:
I realise it's fairly unlikely that this is possible, but seeing what
people have achieved with the language in the past, perhaps there's
some smart solution to this. I wonder if anyone has devised a way to
at compile time iterate over the parent classes of a particular class
without resorting to manually maintained typedefs.
One simple solution is to generate code using source code annotation.
Example code with annotation:
struct One {};
struct Two {};
struct /* @iterate_class */ Three
: /* @iterate_base */ One
, /* @iterate_base */ Two
{};
The code generated from the above annotation can look something like
that:
template<class Functor>
void iterate_object(Three& object, Functor f)
{
f.onObjectBegin(object, "Three");
f.onBaseSubobject(static_cast<One&>(object), "One");
f.onBaseSubobject(static_cast<Two&>(object), "Two");
f.onObjectEnd(object, "Three");
}
Essentially, this way you define an algorithm for traversing an
object. The actual code which does something useful is passed as a
functor, just like with standard algorithms (i.e. std::for_each()
etc..). Only in this case the functor has a rich interface.
You can also annotate members to do more interesting things, like
serialisation.
It is quite easy to parse the annotation and generate the code with
Perl (this is what I do) or with another scripting language of your
choice.
--
Max
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]