Skip to content

Instantly share code, notes, and snippets.

@simonask
Created March 12, 2009 15:40
Show Gist options
  • Save simonask/78128 to your computer and use it in GitHub Desktop.
Save simonask/78128 to your computer and use it in GitHub Desktop.
template <typename T> class Delegate;
template <typaname T> class DelegateFunction;
// Specialization for 2-argument callbacks:
template <typename C, typename R, typename A1, typename A2>
class DelegateMemberFunction : public DelegateFunction<R, A1, A2> {
private:
C* m_Instance;
R (*m_Function)(A1, A2);
public:
// ... constructor
R invoke(A1 a1, A2 a2) { return (m_Instance->(*m_Function))(a1, a2); }
};
template <typename R, typename A1, typename A2>
class Delegate<R(A1, A2)> {
private:
DelegateFunction<R(A1, A2)>* m_Function;
public:
// Member function constructor
template <typename C>
Delegate(C* instance, R(C::*function)(A1, A2)) {
m_Function = new DelegateMemberFunction<C, R, A1, A2>(instance, function);
}
// Free function constructor
Delegate(R(*function)(A1, A2)) {
m_Function = new DelegateFreeFunction(R, A1, A2>(function);
}
R invoke(A1 a1, A2 a2) { return m_Function->invoke(a1, a2); }
};
template <C, R, A1, A2>
Delegate<R, A1, A2> CreateDelegate(C* instance, R(C::*function)(A1, A2)) {
return Delegate<R, A1, A2>(instance, function);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment