Created
July 26, 2014 19:48
-
-
Save thejustinwalsh/d2f25e4a526b798692f8 to your computer and use it in GitHub Desktop.
Delegate Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename... params> | |
class Delegate | |
{ | |
typedef void (*Type)(void* callee, params...); | |
public: | |
Delegate(void* callee, Type function) : _callee(callee), _delegate(function) {} | |
template <class T, void (T::*TMethod)(params...)> | |
static Delegate make(T* callee) | |
{ | |
return Delegate(callee, &methodCaller<T, TMethod>); | |
} | |
void operator()(params... args) const | |
{ | |
return (*_delegate)(_callee, args...); | |
} | |
private: | |
void* _callee; | |
Type _delegate; | |
template <class T, void (T::*TMethod)(params...)> | |
static void methodCaller(void* callee, params... args) | |
{ | |
T* p = static_cast<T*>(callee); | |
return (p->*TMethod)(args...); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment