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. |
---|
13 | template<typename ReturnType> |
---|
14 | class IFunctor0 |
---|
15 | { |
---|
16 | public: |
---|
17 | virtual ~IFunctor0() {} |
---|
18 | virtual ReturnType operator()() const = 0; |
---|
19 | virtual bool Compare(void* pObject, void* pFunction) const = 0; |
---|
20 | }; |
---|
21 | |
---|
22 | template<typename Class, typename ReturnType> |
---|
23 | class FUFunctor0 : public IFunctor0<ReturnType> |
---|
24 | { |
---|
25 | private: |
---|
26 | Class* m_pObject; |
---|
27 | ReturnType (Class::*m_pFunction) (); |
---|
28 | |
---|
29 | public: |
---|
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 | |
---|
39 | template<typename ReturnType> |
---|
40 | class FUStaticFunctor0 : public IFunctor0<ReturnType> |
---|
41 | { |
---|
42 | private: |
---|
43 | ReturnType (*m_pFunction) (); |
---|
44 | |
---|
45 | public: |
---|
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. |
---|
56 | template<typename Arg1, typename ReturnType> |
---|
57 | class IFunctor1 |
---|
58 | { |
---|
59 | public: |
---|
60 | virtual ~IFunctor1() {} |
---|
61 | virtual ReturnType operator()(Arg1 sArgument1) const = 0; |
---|
62 | virtual bool Compare(void* pObject, void* pFunction) const = 0; |
---|
63 | }; |
---|
64 | |
---|
65 | template<typename Class, typename Arg1, typename ReturnType> |
---|
66 | class FUFunctor1 : public IFunctor1<Arg1, ReturnType> |
---|
67 | { |
---|
68 | private: |
---|
69 | Class* m_pObject; |
---|
70 | ReturnType (Class::*m_pFunction) (Arg1); |
---|
71 | |
---|
72 | public: |
---|
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 | |
---|
82 | template<typename Arg1, typename ReturnType> |
---|
83 | class FUStaticFunctor1 : public IFunctor1<Arg1, ReturnType> |
---|
84 | { |
---|
85 | private: |
---|
86 | ReturnType (*m_pFunction) (Arg1); |
---|
87 | |
---|
88 | public: |
---|
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. |
---|
99 | template<typename Arg1, typename Arg2, typename ReturnType> |
---|
100 | class IFunctor2 |
---|
101 | { |
---|
102 | public: |
---|
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 | |
---|
108 | template<typename Class, typename Arg1, typename Arg2, typename ReturnType> |
---|
109 | class FUFunctor2 : public IFunctor2<Arg1, Arg2, ReturnType> |
---|
110 | { |
---|
111 | private: |
---|
112 | Class* m_pObject; |
---|
113 | ReturnType (Class::*m_pFunction) (Arg1, Arg2); |
---|
114 | |
---|
115 | public: |
---|
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 | |
---|
125 | template<typename Arg1, typename Arg2, typename ReturnType> |
---|
126 | class FUStaticFunctor2 : public IFunctor2<Arg1, Arg2, ReturnType> |
---|
127 | { |
---|
128 | private: |
---|
129 | ReturnType (*m_pFunction) (Arg1, Arg2); |
---|
130 | |
---|
131 | public: |
---|
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_ |
---|