Skip to content
Freeman Zhang edited this page Apr 20, 2015 · 1 revision

This page explains the implementation of fast c++ delegate in include/cppevent/delegate.hpp, which is the core of this small project.

There're some excellent articles about fast c++ delegate on Code Project:

A minimal delegate

class delegate
{
public:
    delegate()
        : object_ptr(0)
        , stub_ptr(0)
    {}

    template <class T, void (T::*TMethod)(int)>
    static delegate from_method(T* object_ptr)
    {
        delegate d;
        d.object_ptr = object_ptr;
        d.stub_ptr = &method_stub<T, TMethod>; // #1
        return d;
    }

    void operator()(int a1) const
    {
        return (*stub_ptr)(object_ptr, a1);
    }

private:
    typedef void (*stub_type)(void* object_ptr, int);

    void* object_ptr;
    stub_type stub_ptr;

    template <class T, void (T::*TMethod)(int)>
    static void method_stub(void* object_ptr, int a1)
    {
        T* p = static_cast<T*>(object_ptr);
        return (p->*TMethod)(a1); // #2
    }
};

Use template for improvement

Use variadic template in C++11 for improvement

Improve the syntax