Home > C++, Programming > Learning to call methods (C++ Reflection, part 2)

Learning to call methods (C++ Reflection, part 2)

April 22, 2008

Ok, last time we got the basics of the system in place. Tonight we’ll fix up some of the stuff that we had before (in case you were wondering, I’m figuring this out at the same time as you guys), then get the invocation of some functions.

First, our RuntimeInfo struct from last time is a little lacking and should be implementing a little better.


class MapComparer
    bool operator()( const char* s1, const char* s2 ) const
        return strcmp(s1, s2) < 0;

template< class T >
class RuntimeInfo
    static const unsigned int CLASS_NAME_LENGTH = 50;

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

    unsigned int GetClassId() const;
    const char* GetClassName() const;

    void RegisterMethod( Method< T >* method );

    Method< T >* GetMethod( const char* name );
    std::map< const char*, Method< T >*, MapComparer >* GetMethods();

    unsigned int _classId;
    char         _className[ CLASS_NAME_LENGTH ];
    std::map< const char*, Method< T >*, MapComparer > _methods;

The first changes to note is the change from a struct to a class. This is technically only a semantic change in C++, but we’ll be adding functionality via functions so it’s better to call it a class. Next, I moved the class ID and name to private member data, which it probably should have been in the first place. You should also notice that the class is now a templated class. The templace type will tell us the type for member methods will be called. The last important thing is the addition of the RegisterMethod function. This allows us to register any member function with the class so that we can later invoke it.

Alright, now that we’ve updated the RuntimeInfo class to support our Methods, let’s go ahead and define those. =)


template< class OwnerType >
class Method
    typedef void (OwnerType::*m0)();

    Method( const char* name, m0 method );
    virtual ~Method();

    const char* GetName() const;

    void Invoke( OwnerType& obj );

    Method( const char* name );

    char* _methodName;
    m0 _method;

Our Method class is pretty straight forward; it will be used to define functions that have no return type, and for now, take no parameters. First, this class is templated to define the type for the function that is being passed in. For instance, if I have a class MyInt and have exposed Double(), the type would be MyInt. Next thing to note is the typedef for the function signature; this is a function that returns void and takes no parameters. The next interesting piece is the Invoke() function; this is where all of the action will actually be done.

You might be wondering why I’ve created a protected Method constructor… that’s a good question. The reason is that when I start extending this class, I want to be able to take advantage of the base functionality for storing the name.

Alright, so this gives us the ability to call functions that don’t return anything, let’s extend it so that we can call a function and have it return something interesting.


template< class OwnerType, class ReturnType >
class Function : public Method< OwnerType >
    typedef ReturnType (OwnerType::*f0)();

    Function( const char* name, f0 function );
    virtual ~Function();
    ReturnType Invoke( OwnerType& obj );

    f0 _method;

The major difference between this class and the Method class is that this class takes two template arguments: the type for the member function and the return type for the function. Also notice the overloading of the Invoke() function so that it returns the proper type.

And that’s all there is to it. I’ll post a little sample application that shows it in use.


int _tmain( int argc, _TCHAR* argv[] )
   Object o;
   MyInt i;

   i.SetValue( 30 );

   Method< MyInt >* imi = i.GetRuntimeInfo()->GetMethod( "Double" );

    Function< MyInt, int >* imi2 =
        static_cast< Function< MyInt, int >* >( i.GetRuntimeInfo()->GetMethod( "GetValue" ) );

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

    cout << "-- MYINT --" << endl;
    cout << "\tClass ID: " << i.GetRuntimeInfo()->GetClassId() << endl;
    cout << "\tClass Name: " << i.GetRuntimeInfo()->GetClassName() << endl;
    cout << "\tOriginal Value: " << i.GetValue() << endl;
    imi->Invoke( i );
    cout << "\tDouble Value: " << i.GetValue() << endl;
    cout << "\tGetValue: " << imi2->Invoke( i ) << endl;
    cout << endl;

    cout << "Press enter to exit." << endl;

    return 0;

So there we have it. We’ve built a reflection system in C++. Is it the most robust and feature rich, no, most definately not. For instance, there is not way to pass in arguments to either the Method or Function class, there is no way to call the constructor, although, arguably that is just registering the constructor with by using the Function class. But overall, I think I’ve pretty much met the goal that I set out to achieve.

If you have any questions or comments, I’d be happy to answer them, but I think I’m relatively happy with the reflection system I have in C++… for now atleast. =)

Source code is here: Reflection.zip


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