1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
template<typename Return, typename Parameter, typename... Parameters>
class Delegate<Return (Parameter, Parameters...)>{
public:
/// Constructors & destructors:
Delegate(void) : _m_fpFunc(nullptr), _m_voidpInstance(nullptr){
}
Delegate(Delegate const& delegate) = default;
Delegate(Delegate&& delegate) = default;
/// Member functions:
template<Return (*Function)(Parameter, Parameters...)>
Delegate& bind(void){
_m_fpFunc = &_get_function_static<Function>;
_m_voidpInstance = nullptr;
return *this;
}
template<class C, Return (C::*Function)(Parameter, Parameters...)>
Delegate& bind(C& c){
_m_fpFunc = &_get_function_member<C, Function>;
_m_voidpInstance = &c;
return *this;
}
inline bool bound(void) const{
return (_m_fpFunc != nullptr);
}
/// Member functions (overloaded operators):
inline Return operator()(Parameter param, Parameters... params) const{
return _m_fpFunc(_m_voidpInstance, param, params...);
}
private:
/// Member data:
Return (*_m_fpFunc)(void*, Parameter, Parameters...);
void* _m_voidpInstance;
/// Static member functions:
template<class C, Return (C::*Function)(Parameter, Parameters...)>
static inline Return _get_function_member(void* instance, Parameter param, Parameters... params){
return (static_cast<C*>(instance)->*Function)(param, params...);
}
template<Return (*Function)(Parameter, Parameters...)>
static inline Return _get_function_static(void*, Parameter param, Parameters... params){
return (Function)(param, params...);
}
};
| |