FUtils/FUFunctor.h

00001 /*
00002     Copyright (C) 2005-2006 Feeling Software Inc.
00003     MIT License: http://www.opensource.org/licenses/mit-license.php
00004 */
00005 /*
00006     This file was taken off the Protect project on 26-09-2005
00007 */
00008 
00009 #ifndef _FUNCTOR_H_
00010 #define _FUNCTOR_H_
00011 
00012 // Functor with no argument.
00013 template<typename ReturnType>
00014 class IFunctor0
00015 {
00016 public:
00017     virtual ~IFunctor0() {}
00018     virtual ReturnType operator()() const = 0;
00019     virtual bool Compare(void* pObject, void* pFunction) const = 0;
00020 };
00021 
00022 template<typename Class, typename ReturnType>
00023 class FUFunctor0 : public IFunctor0<ReturnType>
00024 {
00025 private:
00026     Class* m_pObject;
00027     ReturnType (Class::*m_pFunction) ();
00028 
00029 public:
00030     FUFunctor0(Class* pObject, ReturnType (Class::*pFunction) ()) { m_pObject = pObject; m_pFunction = pFunction; }
00031 
00032     virtual ReturnType operator()() const
00033     { return ((*m_pObject).*m_pFunction)(); }
00034 
00035     virtual bool Compare(void* pObject, void* pFunction) const
00036     { return pObject == m_pObject && (size_t)pFunction == *(size_t*)&m_pFunction; }
00037 };
00038 
00039 template<typename ReturnType>
00040 class FUStaticFunctor0 : public IFunctor0<ReturnType>
00041 {
00042 private:
00043     ReturnType (*m_pFunction) ();
00044 
00045 public:
00046     FUStaticFunctor0(ReturnType (*pFunction) ()) { m_pFunction = pFunction; }
00047 
00048     virtual ReturnType operator()() const
00049     { return (*m_pFunction)(); }
00050 
00051     virtual bool Compare(void*, void* pFunction) const
00052     { return (size_t)pFunction == *(size_t*)&m_pFunction; }
00053 };
00054 
00055 // Functor with one argument.
00056 template<typename Arg1, typename ReturnType>
00057 class IFunctor1
00058 {
00059 public:
00060     virtual ~IFunctor1() {}
00061     virtual ReturnType operator()(Arg1 sArgument1) const = 0;
00062     virtual bool Compare(void* pObject, void* pFunction) const = 0;
00063 };
00064 
00065 template<typename Class, typename Arg1, typename ReturnType>
00066 class FUFunctor1 : public IFunctor1<Arg1, ReturnType>
00067 {
00068 private:
00069     Class* m_pObject;
00070     ReturnType (Class::*m_pFunction) (Arg1);
00071 
00072 public:
00073     FUFunctor1(Class* pObject, ReturnType (Class::*pFunction) (Arg1)) { m_pObject = pObject; m_pFunction = pFunction; }
00074 
00075     virtual ReturnType operator()(Arg1 sArgument1) const
00076     { return ((*m_pObject).*m_pFunction)(sArgument1); }
00077 
00078     virtual bool Compare(void* pObject, void* pFunction) const
00079     { return pObject == m_pObject && (size_t)pFunction == *(size_t*)&m_pFunction; }
00080 };
00081 
00082 template<typename Arg1, typename ReturnType>
00083 class FUStaticFunctor1 : public IFunctor1<Arg1, ReturnType>
00084 {
00085 private:
00086     ReturnType (*m_pFunction) (Arg1);
00087 
00088 public:
00089     FUStaticFunctor1(ReturnType (*pFunction) (Arg1)) { m_pFunction = pFunction; }
00090 
00091     virtual ReturnType operator()(Arg1 sArgument1) const
00092     { return (*m_pFunction)(sArgument1); }
00093 
00094     virtual bool Compare(void*, void* pFunction) const
00095     { return (size_t)pFunction == *(size_t*)&m_pFunction; }
00096 };
00097 
00098 // Functor with two argument.
00099 template<typename Arg1, typename Arg2, typename ReturnType>
00100 class IFunctor2
00101 {
00102 public:
00103     virtual ~IFunctor2() {}
00104     virtual ReturnType operator()(Arg1 sArgument1, Arg2 sArgument2) const = 0;
00105     virtual bool Compare(void* pObject, void* pFunction) const = 0;
00106 };
00107 
00108 template<typename Class, typename Arg1, typename Arg2, typename ReturnType>
00109 class FUFunctor2 : public IFunctor2<Arg1, Arg2, ReturnType>
00110 {
00111 private:
00112     Class* m_pObject;
00113     ReturnType (Class::*m_pFunction) (Arg1, Arg2);
00114 
00115 public:
00116     FUFunctor2(Class* pObject, ReturnType (Class::*pFunction) (Arg1, Arg2)) { m_pObject = pObject; m_pFunction = pFunction; }
00117 
00118     virtual ReturnType operator()(Arg1 sArgument1, Arg2 sArgument2) const
00119     { return ((*m_pObject).*m_pFunction)(sArgument1, sArgument2); }
00120 
00121     virtual bool Compare(void* pObject, void* pFunction) const
00122     { return pObject == m_pObject && (size_t)pFunction == *(size_t*)&m_pFunction; }
00123 };
00124 
00125 template<typename Arg1, typename Arg2, typename ReturnType>
00126 class FUStaticFunctor2 : public IFunctor2<Arg1, Arg2, ReturnType>
00127 {
00128 private:
00129     ReturnType (*m_pFunction) (Arg1, Arg2);
00130 
00131 public:
00132     FUStaticFunctor2(ReturnType (*pFunction) (Arg1, Arg2)) { m_pFunction = pFunction; }
00133 
00134     virtual ReturnType operator()(Arg1 sArgument1, Arg2 sArgument2) const
00135     { return (*m_pFunction)(sArgument1, sArgument2); }
00136 
00137     virtual bool Compare(void*, void* pFunction) const
00138     { return (size_t)pFunction == *(size_t*)&m_pFunction; }
00139 };
00140 
00141 // Macro for member function pointer type bypass
00142 #define FUNCTOR_COMPARE(sFunctor, pObject, pFunction, bRet) \
00143 { \
00144     void* pVoid; \
00145     __asm { __asm push eax __asm mov eax, pFunction __asm mov pVoid, eax __asm pop eax } \
00146     bRet = sFunctor.Compare(pObject, pVoid); \
00147 }
00148 
00149 #endif //_FUNCTOR_H_

Generated on Fri May 12 16:44:39 2006 for FCollada by  doxygen 1.4.6-NO