0

This is a simple delegate class that only works for methods of the format void ClassType::MethodType( InputType& ), but can easily be expanded to more generic functions, not shown simply because it would be too large.

class Delegate
{
public:
    Delegate( void ) : Object( NULL ), Argument( NULL ) { }
    virtual ~Delegate( void ) { }

    template <class ClassType, class InputType, void (ClassType::*MethodType)( InputType )>
    void Create( ClassType* SetObject, void* SetArgument = NULL )
    {
        Object = SetObject;
        Argument = SetArgument;
        StaticCall = &CallMethod<ClassType, InputType, MethodType>;
    }

    template <class InputType>
    inline void operator()( InputType InputValue ) const
    {
        (*StaticCall)( Object, static_cast<void*>(InputValue) );
    }

    inline void operator()( void ) const
    {
        (*StaticCall)( Object, Argument );
    }

protected:
    typedef void (*FunctionCallType)( void*, void* );

    void* Object;
    void* Argument;
    FunctionCallType StaticCall;

private:
    template <class ClassType, class InputType, void (ClassType::*MethodType)( InputType )>
    static inline void CallMethod( void* SetObject, void* PassArgument )
    {
        (static_cast<ClassType*>( SetObject )->*MethodType)( static_cast<InputType>(PassArgument) );
    }
};

It's flexible and can be used to pool callback classes, but one problem I have with it is that so far it's on par with (or even slower when used in large vectors like I plan to) than a virtual call if it's used as a base class. I'm looking for any suggestions on how to increase performance since I'm out of ideas, even if it affects functionality.

The simplest performance measuring code I used (with -O3) was:

class VirtualBase
{
public: 
    virtual void TestCall( int* Data ) {}
};

class VirtualTest : public VirtualBase
{
public:
    VirtualTest() : Value(0) {}

    void TestCall( int* Data )
    {
        Value += *Data;
    }
private:
    int Value;
};


class DelTest : public Delegate
{
public:
    DelTest() : Value(0)
    {
        Create<DelTest, int*, &DelTest::TestCall>( this );
    }

    void TestCall( int* Data )
    {
        Value += *Data;
    }
private:
    int Value;
};

int main( int argc, char **argv )
{
    clock_t start;
    int Value = 1;

    VirtualBase* NewBase = new VirtualTest;

    start = clock();
    for( size_t Index = 0; Index < 1000000000; ++Index )
    {
        NewBase->TestCall( &Value );
    }
    delete NewBase;
    std::cout << (( std::clock() - start ) / (double)CLOCKS_PER_SEC) << std::endl;


    Delegate* NewDBase = new DelTest;
    start = clock();
    for( size_t Index = 0; Index < 1000000000; ++Index )
    {
        NewDBase->operator()( &Value );
    }
    delete NewDBase;
    std::cout << (( std::clock() - start ) / (double)CLOCKS_PER_SEC) << std::endl;

    return 0;
}

I should mention that I'd like the class to stay non-template, as it makes classes using callbacks to anything easy to iterate through in a single vector.

  • Have you checked [The Impossibly Fast C++ Delegates](http://www.codeproject.com/Articles/11015/The-Impossibly-Fast-C-Delegates)? It's quite similar to yours, but one difference is that the member function pointer is compile-time constant, so you don't need to pass it as a run-time argument. – iavr May 06 '14 at 04:17
  • I would remove `Argument`. That's a job for `std::bind` and it's not a good design to mix two different functionalities in a single class - the delegate is complex enough by itself. Also, for the general case, a variadic implementation in C++11 would not be much longer than your 1-parameter version. – iavr May 06 '14 at 04:19
  • Did you try std::function? – D Drmmr May 06 '14 at 07:14

1 Answers1

0

You might want to look at this Lightweight Generic C++ Callbacks article on CodeProject

Some of the code from the linked article, showing the use of a function template to do the forwarding:

template<typename R, typename P1, typename P2>
class Callback
{
public:
    typedef R (*FuncType)(void*, P1, P2);
    Callback() : func(0), obj(0) {}
    Callback(FuncType f, void* o) : func(f), obj(o) {}
    R operator()(P1 a1, P2 a2)
    {
        return (*func)(obj, a1, a2);
    }

private:
    FuncType func;
    void* obj;
};

template<typename R, class T, typename P1, typename P2, R (T::*Func)(P1, P2)>
R Wrapper(void* o, P1 a1, P2 a2)
{
    return (static_cast<T*>(o)->*Func)(a1, a2);
}

class Foo
{
public:
    float Average(int n1, int n2)
    {
        return (n1 + n2) / 2.0f;
    }
};

float Calculate(int n1, int n2, Callback<float, int, int> callback)
{
    return callback(n1, n2);
}

int main()
{
    Foo f;
    Callback<float, int, int> cb         
        (&Wrapper<float, Foo, int, int, &Foo::Average>, &f);
    float result = Calculate(50, 100, cb);
    // result == 75.0f
    return 0;
}

There is also a great write up on stackoverflow here which will give you better insight.

Community
  • 1
  • 1
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213