Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 9th, 2011  |  syntax: C++  |  size: 2.16 KB  |  views: 364  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #pragma once
  2.  
  3. template<typename ReturnType, typename ...Params>
  4. class FunctionBase {
  5.   public:
  6.     int referencecount;
  7.     FunctionBase():referencecount(1){}
  8.     virtual ReturnType operator()(Params... params)=0;
  9. };
  10.  
  11.  
  12. template<typename ReturnType, typename ...Params>
  13. class CFunction : public FunctionBase<ReturnType, Params...> {
  14.   private:
  15.     ReturnType (*function)(Params...);
  16.   public:
  17.   CFunction(ReturnType (*func)(Params...)):function(func){}
  18.  
  19.   virtual ReturnType operator()(Params... params){
  20.     return function(params...);
  21.   }
  22. };
  23.  
  24. template<typename ClassType, typename ReturnType, typename ...Params>
  25. class MemberFunction : public FunctionBase<ReturnType, Params...> {
  26.   private:
  27.     ClassType* _thisptr;
  28.     ReturnType (ClassType::*function)(Params...);
  29.   public:
  30.   MemberFunction(ClassType* thisptr, ReturnType (ClassType::*func)(Params...)):_thisptr(thisptr), function(func){}
  31.  
  32.   virtual ReturnType operator()(Params... params){
  33.     return (_thisptr->*function)(params...);
  34.   }
  35. };
  36.  
  37.  
  38. template <typename ReturnType, typename ...Params>
  39. class Function {
  40. private:
  41.   FunctionBase<ReturnType, Params...>* function;
  42.  
  43.   void destroy(){
  44.     function->referencecount--;
  45.     if(function->referencecount<=0){
  46.       delete function;
  47.     }
  48.   }
  49. public:
  50.  
  51.   //c function constructor
  52.   Function(ReturnType (*func)(Params...)){
  53.     function = new CFunction<ReturnType, Params...>(func);
  54.   }
  55.  
  56.   //member function constructor
  57.   template<typename ClassType>
  58.   Function(ClassType* thisptr, ReturnType (ClassType::*func)(Params...)){
  59.     function = new MemberFunction<ClassType, ReturnType, Params...>(thisptr, func);
  60.   }
  61.  
  62.   //copy constructor
  63.   Function(const Function& rhs) {
  64.     function = rhs.function;
  65.     function->referencecount++;
  66.   }
  67.  
  68.   //assignment operator
  69.   Function& operator=(const Function& rhs) {
  70.     if(&rhs != this){
  71.       if(rhs->function != function){
  72.         destroy();
  73.         function = rhs.function;
  74.         function->referencecount++;
  75.       }
  76.     }
  77.     return *this;
  78.   }
  79.  
  80.   ReturnType operator()(Params... params){
  81.     return (*function)(params...);
  82.   }
  83.  
  84.  
  85.   //destructor
  86.   ~Function(){
  87.     destroy();
  88.   }
  89. };
clone this paste RAW Paste Data