views:

51

answers:

2

I have an unmanaged C++ application (unmanaged meaning: not using anything of the the fancy .Net stuff). I want to extend it with some meta information, and it looks like I could use the concept of attributes.

What I actually try to achieve is the following. Starting from something a simple class like this:

class Book
   {
   public:
      ...
   private:
      string m_name;
      string m_author;
      int    m_year;
   };

I want to build functionality that can access the 'meta information' of the class and use it to dynamically build logic on it, e.g.

  • a dialog containing 3 edit fields (name, author, year)
  • a data grid with 3 columns
  • serialization logic
  • logic that maps this class to a database table with 3 columns
  • ...

I my wildest dreams I imagine modifying this class like this:

[id="Book"]
class Book
   {
   public:
      ...
   private:
      [id="Name", defaultValue="", maximumLength=100]
      string m_name;

      [id="Author", defaultValue="", maximumLength=100]
      string m_author;

      [id="Year", defaultValue=2000, minimum=1900]
      int    m_year;
   };

And then being able to get this 'meta' information to build up dialogs, filling data grids, serializing and deserializing instances, ...

But, is the concept of attributes limited to .Net/managed code?

And if I could use attributes in unmanaged code, would it be possible to do something like this? And what is a good place to start? (examples, ...)

Also, can the same (or similar) concepts be found in other compilers, on other platforms?

I am using Visual Studio 2010 and, as said before, unmanaged/native C++.

+1  A: 

No. C++ does not have introspection or attributes.

Look into Boost Serialization for the serialization stuff, for the others you need to implement it manually, as far as I know.

GMan
+1  A: 

Visual C++ for a while supported a similar attribute notation when defining COM objects. I think support was eventually dropped because programmers use C++ for COM implementation when they want complete control, and the compiler doing things magically outside the programmer's control runs counter to that.

OTOH IDL does still allow you to define metadata, it compiles to C++ source code along with a type library which contains the metadata, and it can be retrieved at runtime.

Ben Voigt