Home > C++, Programming > Thinking about the problem (C++ Reflection, part 1)

Thinking about the problem (C++ Reflection, part 1)

April 22, 2008

Setting Goals

So you want to build a reflection system in C++? Well, I assume so since you’re reading this. =) Ok, enough jokes…

There are a few ways to go about building a reflection system in C++ and there are different levels that we can accomplish. For instance, to create a reflection based system that works without any modification to standard C++ types and classes would be very challenging, to say the least. In order to help us with this problem, we’ll start off with a set of goals so that we know what we want to aim for.

  1. Maintainable – It would be nice if (and when) we need to make modifications or bug fixes the code base is clean and logical.
  2. Robust – I don’t like when things only work when sprinkling some magic pixie dust, so let’s make this thing as robust as possible. This means we’ll be using templates rather than MACROs to do our magic.
  3. Effecient – The idea isn’t to create a reflection system takes five minutes to perform something, we want a small and fast system.
  4. Flexible – Let’s shoot for making an extensible system so that we can add to it later without too many problems.

I think that those goals are good enough to get started.

Where to Start

In order to being programming this thing we need to understand what type of information we want. For now the only things that we will concern ourselves with are unique ID for the class and the name of the class; we’ll add to it later to actually make it do some cool stuff.

To start off we’ll need the following classes to provide us with some basic run-time information: RuntimeInfo and RuntimeClass.

Code:

struct RuntimeInfo
{
    RuntimeInfo( unsigned int classId, const char* name );

    static const unsigned int CLASS_NAME_LENGTH = 32;

    unsigned int ClassId;
    char         ClassName[ CLASS_NAME_LENGTH ];
};

This class will define all of the cool details for our class; currently we only need to know the ID for the and the name for the class. The ID could be used to create a new type of class or to compare is two classes are the same.

Code:

template< class T, class BaseClass, unsigned int ClassId >
class RuntimeClass : public BaseClass
{
public:
    static inline RuntimeInfo* GetRuntimeInfo() { return &_info; }

protected:
    static RuntimeInfo _info;
};

template< class T, class BaseClass, unsigned int ClassId >
RuntimeInfo
RuntimeClass< T, BaseClass, ClassId >::_info( ClassId, typeid( T ).name() );

I hope you’re up to speed with your templates =). This class is a tricky way of injecting information that is shared by each type of class we’ll be using in our reflection system. All this class currently has is the runtime information that is created based on the templated information.

The next step is to create the base class we’ll be using for all of the objects in our system; it’s appropriately named Object.

Code:

class NullObject{};
class Object : public RuntimeClass< Object, NullObject, 0x0A >
{
public:
    Object();
    virtual ~Object();
};

One of the first things to notice is that it implements our RuntimeClass and passes in the type that it is, the type it derives from, and the ID to use for the class. As you can see, there is another class NullObject here; this is used so that are template parameters will be satisfied.

That’s it! I’ve given a very brief breakdown of what it takes to get started building a reflection system. Next time we’ll learn how set it up so we can create new instances and set some properties on our classes. I’ll leave you with a little sample app that shows it working.

Code:

class MyClass : public RuntimeClass< MyClass, Object, 0xF0 >
{
public:
    MyClass();
    virtual ~MyClass();
};

int
_tmain( int argc, _TCHAR* argv[] )
{
    Object o;
    MyClass c;

    cout << "-- OBJECT --" << endl;
    cout << "\tClass ID: " << o.GetRuntimeInfo()->ClassId << endl;
    cout << "\tClass Name: " << o.GetRuntimeInfo()->ClassName << endl;
    cout << endl;

    cout << "-- MYCLASS --" << endl;
    cout << "\tClass ID: " << c.GetRuntimeInfo()->ClassId << endl;
    cout << "\tClass Name: " << c.GetRuntimeInfo()->ClassName << endl;
    cout << endl;

    cout << "Press enter to exit." << endl;
    cin.get();

    return 0;
}

-David

Advertisements
Categories: C++, Programming Tags:
%d bloggers like this: