Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename T_ret, typename... T_args>
- class functor {
- protected:
- T_ret (*mFunction)(T_args...);
- std::vector<bool(*)(T_args...)> mFunctionHooks;
- public:
- T_ret operator()(T_args... a){
- for(long i = (long)mFunctionHooks.size() - 1; i>=0; i--)
- if(mFunctionHooks[i](a...)) return T_ret(); // hacky shit
- return mFunction(a...);
- }
- functor& operator=(T_ret(*f)(T_args...)){
- mFunction = f;
- return *this;
- }
- functor& operator+=(bool(*f)(T_args...)){
- mFunctionHooks.push_back(f);
- return *this;
- }
- functor& operator-=(bool(*f)(T_args...)){
- for(long i = (long)mFunctionHooks.size(); i>=0; i--)
- if(mFunctionHooks == f)
- mFunctionHooks.erase(mFunctionHooks.begin()+i);
- return *this;
- }
- };
- class MethodContainer {
- public:
- functor<void, MethodContainer&> Method;
- };
- int main(){
- MethodContainer TestObject;
- TestObject.Method = [](MethodContainer& self){
- std::cout << "Not really." << std::endl;
- };
- TestObject.Method += [](MethodContainer& self){
- std::cout << "ERROR" << std::endl;
- return false;
- };
- TestObject.Method(TestObject);
- return 0;
- }
- // Output:
- // ERROR
- // Not really.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement