Skip to content

Instantly share code, notes, and snippets.

@thejustinwalsh
Created July 26, 2014 19:48
Show Gist options
  • Save thejustinwalsh/d2f25e4a526b798692f8 to your computer and use it in GitHub Desktop.
Save thejustinwalsh/d2f25e4a526b798692f8 to your computer and use it in GitHub Desktop.
Delegate Implementation
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