source: NonGTP/FCollada/FUtils/FUFunctor.h @ 964

Revision 964, 4.2 KB checked in by igarcia, 18 years ago (diff)
RevLine 
[964]1/*
2        Copyright (C) 2005-2006 Feeling Software Inc.
3        MIT License: http://www.opensource.org/licenses/mit-license.php
4*/
5/*
6        This file was taken off the Protect project on 26-09-2005
7*/
8
9#ifndef _FUNCTOR_H_
10#define _FUNCTOR_H_
11
12// Functor with no argument.
13template<typename ReturnType>
14class IFunctor0
15{
16public:
17        virtual ~IFunctor0() {}
18        virtual ReturnType operator()() const = 0;
19        virtual bool Compare(void* pObject, void* pFunction) const = 0;
20};
21
22template<typename Class, typename ReturnType>
23class FUFunctor0 : public IFunctor0<ReturnType>
24{
25private:
26        Class* m_pObject;
27        ReturnType (Class::*m_pFunction) ();
28
29public:
30        FUFunctor0(Class* pObject, ReturnType (Class::*pFunction) ()) { m_pObject = pObject; m_pFunction = pFunction; }
31
32        virtual ReturnType operator()() const
33        { return ((*m_pObject).*m_pFunction)(); }
34
35        virtual bool Compare(void* pObject, void* pFunction) const
36        { return pObject == m_pObject && (size_t)pFunction == *(size_t*)&m_pFunction; }
37};
38
39template<typename ReturnType>
40class FUStaticFunctor0 : public IFunctor0<ReturnType>
41{
42private:
43        ReturnType (*m_pFunction) ();
44
45public:
46        FUStaticFunctor0(ReturnType (*pFunction) ()) { m_pFunction = pFunction; }
47
48        virtual ReturnType operator()() const
49        { return (*m_pFunction)(); }
50
51        virtual bool Compare(void*, void* pFunction) const
52        { return (size_t)pFunction == *(size_t*)&m_pFunction; }
53};
54
55// Functor with one argument.
56template<typename Arg1, typename ReturnType>
57class IFunctor1
58{
59public:
60        virtual ~IFunctor1() {}
61        virtual ReturnType operator()(Arg1 sArgument1) const = 0;
62        virtual bool Compare(void* pObject, void* pFunction) const = 0;
63};
64
65template<typename Class, typename Arg1, typename ReturnType>
66class FUFunctor1 : public IFunctor1<Arg1, ReturnType>
67{
68private:
69        Class* m_pObject;
70        ReturnType (Class::*m_pFunction) (Arg1);
71
72public:
73        FUFunctor1(Class* pObject, ReturnType (Class::*pFunction) (Arg1)) { m_pObject = pObject; m_pFunction = pFunction; }
74
75        virtual ReturnType operator()(Arg1 sArgument1) const
76        { return ((*m_pObject).*m_pFunction)(sArgument1); }
77
78        virtual bool Compare(void* pObject, void* pFunction) const
79        { return pObject == m_pObject && (size_t)pFunction == *(size_t*)&m_pFunction; }
80};
81
82template<typename Arg1, typename ReturnType>
83class FUStaticFunctor1 : public IFunctor1<Arg1, ReturnType>
84{
85private:
86        ReturnType (*m_pFunction) (Arg1);
87
88public:
89        FUStaticFunctor1(ReturnType (*pFunction) (Arg1)) { m_pFunction = pFunction; }
90
91        virtual ReturnType operator()(Arg1 sArgument1) const
92        { return (*m_pFunction)(sArgument1); }
93
94        virtual bool Compare(void*, void* pFunction) const
95        { return (size_t)pFunction == *(size_t*)&m_pFunction; }
96};
97
98// Functor with two argument.
99template<typename Arg1, typename Arg2, typename ReturnType>
100class IFunctor2
101{
102public:
103        virtual ~IFunctor2() {}
104        virtual ReturnType operator()(Arg1 sArgument1, Arg2 sArgument2) const = 0;
105        virtual bool Compare(void* pObject, void* pFunction) const = 0;
106};
107
108template<typename Class, typename Arg1, typename Arg2, typename ReturnType>
109class FUFunctor2 : public IFunctor2<Arg1, Arg2, ReturnType>
110{
111private:
112        Class* m_pObject;
113        ReturnType (Class::*m_pFunction) (Arg1, Arg2);
114
115public:
116        FUFunctor2(Class* pObject, ReturnType (Class::*pFunction) (Arg1, Arg2)) { m_pObject = pObject; m_pFunction = pFunction; }
117
118        virtual ReturnType operator()(Arg1 sArgument1, Arg2 sArgument2) const
119        { return ((*m_pObject).*m_pFunction)(sArgument1, sArgument2); }
120
121        virtual bool Compare(void* pObject, void* pFunction) const
122        { return pObject == m_pObject && (size_t)pFunction == *(size_t*)&m_pFunction; }
123};
124
125template<typename Arg1, typename Arg2, typename ReturnType>
126class FUStaticFunctor2 : public IFunctor2<Arg1, Arg2, ReturnType>
127{
128private:
129        ReturnType (*m_pFunction) (Arg1, Arg2);
130
131public:
132        FUStaticFunctor2(ReturnType (*pFunction) (Arg1, Arg2)) { m_pFunction = pFunction; }
133
134        virtual ReturnType operator()(Arg1 sArgument1, Arg2 sArgument2) const
135        { return (*m_pFunction)(sArgument1, sArgument2); }
136
137        virtual bool Compare(void*, void* pFunction) const
138        { return (size_t)pFunction == *(size_t*)&m_pFunction; }
139};
140
141// Macro for member function pointer type bypass
142#define FUNCTOR_COMPARE(sFunctor, pObject, pFunction, bRet) \
143{ \
144        void* pVoid; \
145        __asm { __asm push eax __asm mov eax, pFunction __asm mov pVoid, eax __asm pop eax } \
146        bRet = sFunctor.Compare(pObject, pVoid); \
147}
148
149#endif //_FUNCTOR_H_
Note: See TracBrowser for help on using the repository browser.