-
Notifications
You must be signed in to change notification settings - Fork 11
delegate
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:
- Member Function Pointers and the Fastest Possible C++ Delegates by Don Clugston
- The Impossibly Fast C++ Delegates by by Sergey Ryazanov
- Fast C++ Delegate: Boost.Function 'drop-in' replacement and multicast by JaeWook Choi
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
}
};
Table of contents
- Quick Guide
- Delegate Implementation