views:

231

answers:

2

Looking at Java and C# they manage to do some wicked processing based on special languaged based anotation (forgive me if that is the incorrect name).

In C++ we have two problems with this:

1) There is no way to annotate a class with type information that is accessable at runtime.
2) Parsing the source to generate stuff is way to complex.

But I was thinking that this could be done with some template meta-programming to achieve the same basic affect as anotations (still just thinking about it). Like char_traits that are specialised for the different types an xml_traits template could be used in a declaritive way. This traits class could be used to define how a class is serialised/deserialized by specializing the traits for the class you are trying to serialize.

Example Thoughs:

template<typename T>
struct XML_traits
{
    typedef XML_Empty   Children;
};

template<>
struct XML_traits<Car>
{
    typedef boost::mpl::vector<Body,Wheels,Engine>   Children;
};

template<typename T>
std::ostream& Serialize(T const&)
{
    // my template foo is not that strong.
    // but somthing like this.
    boost::mpl::for_each<typename XML_Traits<T>::Children,Serialize>(data);
}
template<>
std::ostream& Serialize<XML_Empty>(T const&)
{ /* Do Nothing */ }

My question is:

Has anybody seen any projects/decumentation (not just XML) out there that uses techniques like this (template meta-programming) to emulate the concept of annotation used in languges like Java and C# that can then be used in code generation (to effectively automate the task by using a declaritive style).

At this point in my research I am looking for more reading material and examples.

+4  A: 

Lately, I had a look at the following:

Have a good read :)

Gregory Pakosz
+2  A: 

There is a really good book on using the C++ template processor:

Andrei Alexandrescu Modern C++ Design Generic Programming and Design Patterns Applied Addison-Wesley, U.S.A., 2001 ISBN 0-201-70431-5

Andrei starts writing programs using C++ templates!

Thomas Matthews