• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/Delegate.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef _delegate_h
00018 #define _delegate_h
00019 
00020 #include <spl/Debug.h>
00021 #include <spl/Memory.h>
00022 #include <spl/RefCountPtr.h>
00023 
00030 class IDelegate : public IMemoryValidate
00031 {
00032 public:
00033         virtual void Call() = 0;
00034         virtual void Clear() = 0;
00035 
00036 #if defined(DEBUG)
00037         virtual void CheckMem() const {}
00038         virtual void ValidateMem() const {}
00039 #endif
00040 };
00041 
00046 template<class T>
00047 class Delegate : public IDelegate
00048 {
00049 private:
00050         T* m_object;
00051         void (T::*m_member)();
00052 
00053 public:
00054         inline Delegate(T *object, void (T::* func)())
00055         : m_object(object), m_member(func)
00056         {
00057         }
00058 
00059         virtual void Call()
00060         {
00061                 (m_object->*m_member)();
00062         }
00063 
00064         static RefCountPtr<IDelegate> Create(T *object, void (T::* func)())
00065         {
00066                 return RefCountPtr<IDelegate>(new Delegate<T>(object, func));
00067         }
00068 
00069         virtual void Clear()
00070         {
00071                 m_object = NULL;
00072         }
00073 };
00074 
00075 template<typename ARG>
00076 class IDelegateOneParameter
00077 {
00078 public:
00079         virtual void Call(ARG arg) = 0;
00080 
00081 #if defined(DEBUG)
00082         virtual void CheckMem() const {}
00083         virtual void ValidateMem() const {}
00084 #endif
00085 };
00086 
00091 template<class T, typename ARG>
00092 class DelegateOneParameter : public IDelegateOneParameter<ARG>
00093 {
00094 private:
00095         T* m_object;
00096         void (T::*m_member)(ARG arg);
00097 
00098 public:
00099         inline DelegateOneParameter(T *object, void (T::* func)(ARG arg))
00100         : m_object(object), m_member(func)
00101         {
00102         }
00103 
00104         virtual void Call(ARG arg)
00105         {
00106                 (m_object->*m_member)(arg);
00107         }
00108 
00109         static RefCountPtr<IDelegateOneParameter<ARG> > Create(T *object, void (T::* func)(ARG arg))
00110         {
00111                 return RefCountPtr<IDelegateOneParameter<ARG> >(new DelegateOneParameter<T, ARG>(object, func));
00112         }
00113 };
00114 
00115 template<typename ARG1, typename ARG2>
00116 class IDelegateTwoParameter
00117 {
00118 public:
00119         virtual void Call(ARG1 arg1, ARG2 arg2) = 0;
00120 
00121 #if defined(DEBUG)
00122         virtual void CheckMem() const {}
00123         virtual void ValidateMem() const {}
00124 #endif
00125 };
00126 
00131 template<class T, typename ARG1, typename ARG2>
00132 class DelegateTwoParameter : public IDelegateTwoParameter<ARG1, ARG2>
00133 {
00134 private:
00135         T* m_object;
00136         void (T::*m_member)(ARG1 arg1, ARG2 arg2);
00137 
00138 public:
00139         inline DelegateTwoParameter(T *object, void (T::* func)(ARG1 arg1, ARG2 arg2))
00140         : m_object(object), m_member(func)
00141         {
00142         }
00143 
00144         inline void Call(ARG1 arg1, ARG2 arg2)
00145         {
00146                 (m_object->*m_member)(arg1, arg2);
00147         }
00148 
00149         static RefCountPtr<IDelegateTwoParameter<ARG1, ARG2> > Create(T *object, void (T::* func)(ARG1 arg1, ARG2 arg2))
00150         {
00151                 return RefCountPtr<IDelegateTwoParameter<ARG1, ARG2> >(new DelegateTwoParameter<T, ARG1, ARG2>(object, func));
00152         }
00153 };
00154 
00155 template<typename ARG1, typename ARG2, typename ARG3>
00156 class IDelegateThreeParameter
00157 {
00158 public:
00159         virtual void Call(ARG1 arg1, ARG2 arg2, ARG3 arg3) = 0;
00160 
00161 #if defined(DEBUG)
00162         virtual void CheckMem() const {}
00163         virtual void ValidateMem() const {}
00164 #endif
00165 };
00166 
00171 template<class T, typename ARG1, typename ARG2, typename ARG3>
00172 class DelegateThreeParameter : public IDelegateThreeParameter<ARG1, ARG2, ARG3>
00173 {
00174 private:
00175         T* m_object;
00176         void (T::*m_member)(ARG1 arg1, ARG2 arg2, ARG3 arg3);
00177 
00178 public:
00179         inline DelegateThreeParameter(T *object, void (T::* func)(ARG1 arg1, ARG2 arg2, ARG3 arg3))
00180         : m_object(object), m_member(func)
00181         {
00182         }
00183 
00184         inline void Call(ARG1 arg1, ARG2 arg2, ARG3 arg3)
00185         {
00186                 (m_object->*m_member)(arg1, arg2, arg3);
00187         }
00188 
00189         static RefCountPtr<IDelegateThreeParameter<ARG1, ARG2, ARG3> > Create(T *object, void (T::* func)(ARG1 arg1, ARG2 arg2, ARG3 arg3))
00190         {
00191                 return RefCountPtr<IDelegateThreeParameter<ARG1, ARG2, ARG3> >(new DelegateThreeParameter<T, ARG1, ARG2, ARG3>(object, func));
00192         }
00193 };
00194 
00197 #endif