views:

802

answers:

6

Most mature C++ projects seem to have an own reflection and attribute system, i.e for defining attributes which can be accessed by string and are automatically serializable. At least many C++ projects I participated in seemed to reinvent the wheel.

Do you know any good open source libraries for C++ which support reflection and attribute containers, specifically:

  • Defining RTTI and attributes via macros
  • Accessing RTTI and attributes via code
  • Automatic serialisation of attributes
  • Listening to attribute modifications (e.g. OnValueChanged)
+1  A: 

Not a general one but QT supports this via a meta compiler, and is GPL. My understanding from talking to the QT people was that this isn't possible with pure C++, hence the need for the moc.

Martin Beckett
A: 

This is a notorious weakness of the C++ language in general because the things that would need to be standardized to make reflection implementations portable and worthwhile aren't standard. Calling conventions, object layouts, and symbol mangling come to mind, but there are others as well.

The lack of direction from the standard means that compiler implementers will do some things differently, which means that very few people have the motivation to write a portable reflection library, which means that people who need reflection re-invent the wheel, but only just enough for what they need. This happens to ad infinitum, and here we are.

Ben Collins
+1  A: 
Skizz
Unless my app already uses Qt, I create streaming operators for each of my classes that need to be saved.
Branan
+1 : for the photo ;) kidding. I agree with the conclusion. Use handcraft serialisers or use another langage :)
neuro
+2  A: 

You could have a look at the two tools below. I've never used either of them, so I can't tell you how (im)practical they are.

XRTTI:

Xrtti is a tool and accompanying C++ library which extends the standard runtime type system of C++ to provide a much richer set of reflection information about classes and methods to manipulate these classes and their members.

OpenC++:

OpenC++ is C++ frontend library (lexer+parser+DOM/MOP) and source-to-source translator. OpenC++ enables development of C++ language tools, extensions, domain specific compiler optimizations and runtime metaobject protocols.

Carl Seleborg
+1  A: 

I looked at these things for quite a while but they tend to be very heavy-handed. They might prevent you from using inheritance, or having strange constructors etc etc. In the end they ended up being too much of a burden instead of a convenience.

This approach for exposing members that I now use is quite lightweight and lets you explore a class for serialization or setting all fields called "x" to 0, for example. It's also statically determined so is very very fast. No layers of library code or code-gen to worry about messing with the build process. It generalises to hierarchies of nested types.

Set your editor up with some macros to automate writing some of these things.

struct point
{
     int x;
     int y;

     // add this to your classes
     template <typename Visitor>
     void visit(Visitor v)
     {
         v->visit(x, "x"); 
         v->visit(y, "y");
     }
};


/** Outputs any type to standard output in key=value format */
struct stdout_visitor
{
     template <typename T>
     void visit(const T& rhs)
     {
         rhs.visit(this);
     }

     template <typename Scalar>
     void visit (const Scalar& s, const char* name)
     {
          std::cout << name << " = " << s << " ";
     }
}
Matthew Herrmann
While i like it, it should be noted that it isn't a general solution - e.g. it breaks as soon as the actual static type isn't known.
Georg Fritzsche
+1  A: 

There is a new project providing reflection in C++ using a totally different approach: CAMP. http://dev.tegesoft.com/projects/camp

CAMP doesn't use a precompiler, the classes/properties/functions/... are declared manually using a syntax similar to boost.python or luabind. Of course, people can use a precompiler like gccxml or open-c++ to generate this declaration if they prefer.

It's based on pure C++ and boost headers only, and thanks to the power of template meta-programming it supports any kind of bindable entity (inheritance and strange constructors are not a problem, for example).

It is distributed under the LGPL licence.

Laurent