#include <iostream> #include <functional> template <typename ReturnType,typename Param1,typename Param2> class FDelegateTwoParams { public: using bindFunc = ReturnType(*)(Param1, Param2); void BindGlobalFunc(bindFunc f,Param1 a,Param2 b) { Fun = std::bind(f, a, b); }
template<class T> void BindRaw(T* user_class, ReturnType(T::*f)(Param1,Param2),Param1 a,Param2 b) { Fun = std::bind(f,user_class, a, b); }
bool IsBound() const { return !!Fun; }
ReturnType Execute() { return Fun(); }
private: std::function<ReturnType()> Fun; };
#define DECLARE_DELEGATE_RetVal_TwoParams(_return_type,_name,_param1,_param2) using _name = FDelegateTwoParams<_return_type,_param1,_param2>
int AddNum(int a, int b) { auto c = a + b; std::cout << c << std::endl; return c; }
class Test { public: int testIt(int a, int b) { auto c = a + b; std::cout << c << std::endl; return c; } };
int main() { DECLARE_DELEGATE_RetVal_TwoParams(int, FTestDelegate2p, int, int); FTestDelegate2p delegate; Test t; delegate.BindRaw(&t,&Test::testIt, 1, 2); delegate.Execute(); }
|