Advertisement
WeltEnSTurm

Untitled

Aug 3rd, 2011
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. template<typename T_ret, typename... T_args>
  2. class functor {
  3.     protected:
  4.         T_ret (*mFunction)(T_args...);
  5.         std::vector<bool(*)(T_args...)> mFunctionHooks;
  6.     public:
  7.         T_ret operator()(T_args... a){
  8.             for(long i = (long)mFunctionHooks.size() - 1; i>=0; i--)
  9.                 if(mFunctionHooks[i](a...)) return T_ret(); // hacky shit
  10.             return mFunction(a...);
  11.         }
  12.         functor& operator=(T_ret(*f)(T_args...)){
  13.             mFunction = f;
  14.             return *this;
  15.         }
  16.         functor& operator+=(bool(*f)(T_args...)){
  17.             mFunctionHooks.push_back(f);
  18.             return *this;
  19.         }
  20.         functor& operator-=(bool(*f)(T_args...)){
  21.             for(long i = (long)mFunctionHooks.size(); i>=0; i--)
  22.                 if(mFunctionHooks == f)
  23.                     mFunctionHooks.erase(mFunctionHooks.begin()+i);
  24.             return *this;
  25.         }
  26. };
  27.  
  28.  
  29. class MethodContainer {
  30.     public:
  31.         functor<void, MethodContainer&> Method;
  32. };
  33.  
  34. int main(){
  35.     MethodContainer TestObject;
  36.     TestObject.Method = [](MethodContainer& self){
  37.         std::cout << "Not really." << std::endl;
  38.     };
  39.     TestObject.Method += [](MethodContainer& self){
  40.         std::cout << "ERROR" << std::endl;
  41.         return false;
  42.     };
  43.     TestObject.Method(TestObject);
  44.     return 0;
  45. }
  46.  
  47. // Output:
  48. // ERROR
  49. // Not really.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement