15

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.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Run Time Type Information (RTTI) is available. Many OOP experts consider using Types in decisions is of bad form. The design should be changed to be more generic and not need type information at run-time. – Thomas Matthews Dec 10 '09 at 21:11
  • @Thomas: I totally agree. Using the type information to __explicitly__ make runtime decisions is not a good idea (let the compiler make the choices via polymorphism). But I want to generate code based on declarative type information. Also RTTI may be available but it is not the rich type information that is available at compile time. Note: TMP is all about generatinting programs based on type informaiton at compile time. – Martin York Dec 10 '09 at 22:29

4 Answers4

7

Lately, I had a look at the following:

Have a good read :)

Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
2

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
  • 56,849
  • 17
  • 98
  • 154
1

The template metaprogramming is a really powerful tool, but the thing is simple: your just have to invent your own additional syntax for describing properties.

Yes, parsing C++ is hard, but we only need the limited parsing capabilities: read the class list with hierarchy information and the list of all the serialized properties.

This can be done even in a C/C++ tool with a linear scanning algorithm, if we define some dummy macros.

Templates can abstract the class instantiation and on top of that we add the intrusive RTTI.

The complete description of the technique is described in my answer to this question

How to implement serialization in C++

Community
  • 1
  • 1
Viktor Latypov
  • 14,289
  • 3
  • 40
  • 55
0

Boost.Serialization is the answer. It introduces required portable RTTI for serialization custom types.

What about runtime reflection in C++ - it's a separate question.

Iakov Minochkin
  • 432
  • 2
  • 10