- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# FUtils/FUFunctor.h

    00001 /*
    00002     Copyright (C) 2005-2007 Feeling Software Inc.
    00003     Portions of the code are:
    00004     Copyright (C) 2005-2007 Sony Computer Entertainment America
    00005
    00006     MIT License: http://www.opensource.org/licenses/mit-license.php
    00007 */
    00008 /*
    00009     This file was taken off the Protect project on 26-09-2005
    00010 */
    00011
    00012 #ifndef _FU_FUNCTOR_H_
    00013 #define _FU_FUNCTOR_H_
    00014
    00019 template<class ReturnType>
    00020 class IFunctor0
    00021 {
    00022 public:
    00024     virtual ~IFunctor0() {}
    00025
    00028     virtual ReturnType operator()() const = 0;
    00029
    00034     virtual bool Compare(void* object, void* function) const = 0;
    00035
    00037     virtual IFunctor0<ReturnType>* Copy() const = 0;
    00038 };
    00039
    00044 template<class Class, class ReturnType>
    00045 class FUFunctor0 : public IFunctor0<ReturnType>
    00046 {
    00047 private:
    00048     Class* m_pObject;
    00049     ReturnType (Class::*m_pFunction) ();
    00050
    00051 public:
    00055     FUFunctor0(Class* object, ReturnType (Class::*function) ()) { m_pObject = object; m_pFunction = function; }
    00056
    00058     virtual ~FUFunctor0() {}
    00059
    00062     virtual ReturnType operator()() const
    00063     { return ((*m_pObject).*m_pFunction)(); }
    00064
    00069     virtual bool Compare(void* object, void* function) const
    00070     { return object == m_pObject && (size_t)function == *(size_t*)&m_pFunction; }
    00071
    00073     virtual IFunctor0<ReturnType>* Copy() const
    00074     { return new FUFunctor0<Class, ReturnType>(m_pObject, m_pFunction); }
    00075 };
    00076
    00080 template <class ClassName, class ReturnType>
    00081 inline FUFunctor0<ClassName, ReturnType>* NewFUFunctor0(ClassName* o, ReturnType (ClassName::*function) ())
    00082 {
    00083     return new FUFunctor0<ClassName, ReturnType>(o, function);
    00084 }
    00085
    00090 template<class ReturnType>
    00091 class FUStaticFunctor0 : public IFunctor0<ReturnType>
    00092 {
    00093 private:
    00094     ReturnType (*m_pFunction) ();
    00095
    00096 public:
    00099     FUStaticFunctor0(ReturnType (*function) ()) { m_pFunction = function; }
    00100
    00102     virtual ~FUStaticFunctor0() {}
    00103
    00106     virtual ReturnType operator()() const
    00107     { return (*m_pFunction)(); }
    00108
    00113     virtual bool Compare(void* UNUSED(object), void* function) const
    00114     { return (size_t)function == *(size_t*)(size_t)&m_pFunction; }
    00115
    00117     virtual IFunctor0<ReturnType>* Copy() const
    00118     { return new FUStaticFunctor0<ReturnType>(m_pFunction); }
    00119 };
    00120
    00125 template<class Arg1, class ReturnType>
    00126 class IFunctor1
    00127 {
    00128 public:
    00130     virtual ~IFunctor1() {}
    00131
    00135     virtual ReturnType operator()(Arg1 argument1) const = 0;
    00136
    00141     virtual bool Compare(void* object, void* function) const = 0;
    00142
    00144     virtual IFunctor1<Arg1, ReturnType>* Copy() const = 0;
    00145 };
    00146
    00151 template<class Class, class Arg1, class ReturnType>
    00152 class FUFunctor1 : public IFunctor1<Arg1, ReturnType>
    00153 {
    00154 private:
    00155     Class* m_pObject;
    00156     ReturnType (Class::*m_pFunction) (Arg1);
    00157
    00158 public:
    00162     FUFunctor1(Class* object, ReturnType (Class::*function) (Arg1)) { m_pObject = object; m_pFunction = function; }
    00163
    00165     virtual ~FUFunctor1() {}
    00166
    00170     virtual ReturnType operator()(Arg1 argument1) const
    00171     { return ((*m_pObject).*m_pFunction)(argument1); }
    00172
    00177     virtual bool Compare(void* object, void* function) const
    00178     { return object == m_pObject && (size_t)function == *(size_t*)(size_t)&m_pFunction; }
    00179
    00181     virtual IFunctor1<Arg1, ReturnType>* Copy() const
    00182     { return new FUFunctor1<Class, Arg1, ReturnType>(m_pObject, m_pFunction); }
    00183 };
    00184
    00188 template <class ClassName, class Argument1, class ReturnType>
    00189 inline FUFunctor1<ClassName, Argument1, ReturnType>* NewFUFunctor1(ClassName* o, ReturnType (ClassName::*function) (Argument1))
    00190 {
    00191     return new FUFunctor1<ClassName, Argument1, ReturnType>(o, function);
    00192 }
    00193
    00198 template<class Arg1, class ReturnType>
    00199 class FUStaticFunctor1 : public IFunctor1<Arg1, ReturnType>
    00200 {
    00201 private:
    00202     ReturnType (*m_pFunction) (Arg1);
    00203
    00204 public:
    00207     FUStaticFunctor1(ReturnType (*function) (Arg1)) { m_pFunction = function; }
    00208
    00210     virtual ~FUStaticFunctor1() {}
    00211
    00215     virtual ReturnType operator()(Arg1 argument1) const
    00216     { return (*m_pFunction)(argument1); }
    00217
    00222     virtual bool Compare(void* UNUSED(object), void* function) const
    00223     { return (size_t)function == *(size_t*)(size_t)&m_pFunction; }
    00224
    00226     virtual IFunctor1<Arg1, ReturnType>* Copy() const
    00227     { return new FUStaticFunctor1<Arg1, ReturnType>(m_pFunction); }
    00228 };
    00229
    00234 template<class Arg1, class Arg2, class ReturnType>
    00235 class IFunctor2
    00236 {
    00237 public:
    00239     virtual ~IFunctor2() {}
    00240
    00245     virtual ReturnType operator()(Arg1 argument1, Arg2 argument2) const = 0;
    00246
    00251     virtual bool Compare(void* object, void* function) const = 0;
    00252
    00254     virtual IFunctor2<Arg1, Arg2, ReturnType>* Copy() const = 0;
    00255 };
    00256
    00261 template<class Class, class Arg1, class Arg2, class ReturnType>
    00262 class FUFunctor2 : public IFunctor2<Arg1, Arg2, ReturnType>
    00263 {
    00264 private:
    00265     Class* m_pObject;
    00266     ReturnType (Class::*m_pFunction) (Arg1, Arg2);
    00267
    00268 public:
    00272     FUFunctor2(Class* object, ReturnType (Class::*function) (Arg1, Arg2)) { m_pObject = object; m_pFunction = function; }
    00273
    00275     virtual ~FUFunctor2() {}
    00276
    00281     virtual ReturnType operator()(Arg1 argument1, Arg2 argument2) const
    00282     { return ((*m_pObject).*m_pFunction)(argument1, argument2); }
    00283
    00288     virtual bool Compare(void* object, void* function) const
    00289     { return object == m_pObject && (size_t)function == *(size_t*)(size_t)&m_pFunction; }
    00290
    00292     virtual IFunctor2<Arg1, Arg2, ReturnType>* Copy() const
    00293     { return new FUFunctor2<Class, Arg1, Arg2, ReturnType>(m_pObject, m_pFunction); }
    00294 };
    00295
    00299 template <class ClassName, class Argument1, class Argument2, class ReturnType>
    00300 inline FUFunctor2<ClassName, Argument1, Argument2, ReturnType>* NewFUFunctor2(ClassName* o, ReturnType (ClassName::*function) (Argument1, Argument2))
    00301 {
    00302     return new FUFunctor2<ClassName, Argument1, Argument2, ReturnType>(o, function);
    00303 }
    00304
    00309 template<class Arg1, class Arg2, class ReturnType>
    00310 class FUStaticFunctor2 : public IFunctor2<Arg1, Arg2, ReturnType>
    00311 {
    00312 private:
    00313     ReturnType (*m_pFunction) (Arg1, Arg2);
    00314
    00315 public:
    00318     FUStaticFunctor2(ReturnType (*function) (Arg1, Arg2)) { m_pFunction = function; }
    00319
    00321     virtual ~FUStaticFunctor2() {}
    00322
    00327     virtual ReturnType operator()(Arg1 argument1, Arg2 argument2) const
    00328     { return (*m_pFunction)(argument1, argument2); }
    00329
    00334     virtual bool Compare(void* UNUSED(object), void* function) const
    00335     { return (size_t)function == *(size_t*)(size_t)&m_pFunction; }
    00336
    00338     virtual IFunctor2<Arg1, Arg2, ReturnType>* Copy() const
    00339     { return new FUStaticFunctor2<Arg1, Arg2, ReturnType>(m_pFunction); }
    00340 };
    00341
    00346 template<class Arg1, class Arg2, class Arg3, class ReturnType>
    00347 class IFunctor3
    00348 {
    00349 public:
    00351     virtual ~IFunctor3() {}
    00352
    00358     virtual ReturnType operator()(Arg1 argument1, Arg2 argument2, Arg3 argument3) const = 0;
    00359
    00364     virtual bool Compare(void* object, void* function) const = 0;
    00365
    00367     virtual IFunctor3<Arg1, Arg2, Arg3, ReturnType>* Copy() const = 0;
    00368 };
    00369
    00374 template<class Class, class Arg1, class Arg2, class Arg3, class ReturnType>
    00375 class FUFunctor3 : public IFunctor3<Arg1, Arg2, Arg3, ReturnType>
    00376 {
    00377 private:
    00378     Class* m_pObject;
    00379     ReturnType (Class::*m_pFunction) (Arg1, Arg2, Arg3);
    00380
    00381 public:
    00385     FUFunctor3(Class* object, ReturnType (Class::*function) (Arg1, Arg2, Arg3)) { m_pObject = object; m_pFunction = function; }
    00386
    00388     virtual ~FUFunctor3() {}
    00389
    00395     virtual ReturnType operator()(Arg1 argument1, Arg2 argument2, Arg3 argument3) const
    00396     { return ((*m_pObject).*m_pFunction)(argument1, argument2, argument3); }
    00397
    00402     virtual bool Compare(void* object, void* function) const
    00403     { return object == m_pObject && (size_t)function == *(size_t*)(size_t)&m_pFunction; }
    00404
    00406     virtual IFunctor3<Arg1, Arg2, Arg3, ReturnType>* Copy() const
    00407     { return new FUFunctor3<Class, Arg1, Arg2, Arg3, ReturnType>(m_pObject, m_pFunction); };
    00408 };
    00409
    00413 template <class ClassName, class Argument1, class Argument2, class Argument3, class ReturnType>
    00414 inline FUFunctor3<ClassName, Argument1, Argument2, Argument3, ReturnType>* NewFUFunctor3(ClassName* o, ReturnType (ClassName::*function) (Argument1, Argument2, Argument3))
    00415 {
    00416     return new FUFunctor3<ClassName, Argument1, Argument2, Argument3, ReturnType>(o, function);
    00417 }
    00418
    00423 template<class Arg1, class Arg2, class Arg3, class ReturnType>
    00424 class FUStaticFunctor3 : public IFunctor3<Arg1, Arg2, Arg3, ReturnType>
    00425 {
    00426 private:
    00427     ReturnType (*m_pFunction) (Arg1, Arg2, Arg3);
    00428
    00429 public:
    00432     FUStaticFunctor3(ReturnType (*function) (Arg1, Arg2, Arg3)) { m_pFunction = function; }
    00433
    00435     virtual ~FUStaticFunctor3() {}
    00436
    00442     virtual ReturnType operator()(Arg1 argument1, Arg2 argument2, Arg3 argument3) const
    00443     { return (*m_pFunction)(argument1, argument2, argument3); }
    00444
    00449     virtual bool Compare(void* UNUSED(object), void* function) const
    00450     { return (size_t)function == *(size_t*)(size_t)&m_pFunction; }
    00451
    00453     virtual IFunctor3<Arg1, Arg2, Arg3, ReturnType>* Copy() const
    00454     { return new FUStaticFunctor3<Arg1, Arg2, Arg3, ReturnType>(m_pFunction); };
    00455 };
    00456
    00457 #endif //_FU_FUNCTOR_H_
    00458