Created
March 12, 2009 15:40
-
-
Save simonask/78128 to your computer and use it in GitHub Desktop.
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 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