License Public Domain
Lines 230
Keywords
function pointers (1) functors (1) template (3)
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Hide
Writing an article is easy - try our reStructured Text demo Join Siafoo Now or Learn More

functor.h Atom Feed 0

In Brief This class is templated to hold function pointers. It currently can take function pointers with up to three arguments, but is easily expanded to more. This can be helpful so that you can use one interface for different types of function pointers.
# 's
  1#ifndef FUNCTOR_H
2#define FUNCTOR_H
3
4// started with help from: http://www.newty.de/fpt/functor.html
5
6#include <iostream>
7#include <typeinfo>
8
9struct Empty {};
10
11
12// abstract base class
13template<class Arg1, class Arg2, class Arg3>
14class TFunctor
15{
16 public:
17
18 // two possible functions to call member function. virtual cause derived
19 // classes will use a pointer to an object and a pointer to a member function
20 // to make the function call
21 virtual void operator()()=0; // call using operator
22 virtual void Call()=0; // call using function
23 virtual void operator()(const Arg1 arg1)=0; // call using operator
24 virtual void Call(const Arg1 arg1)=0; // call using function
25 virtual void operator()(const Arg1 arg1, const Arg2 arg2)=0; // call using operator
26 virtual void Call(const Arg1 arg1, const Arg2 arg2)=0; // call using function
27 virtual void operator()(const Arg1 arg1, const Arg2 arg2, const Arg3 arg3)=0; // call using operator
28 virtual void Call(const Arg1 arg1, const Arg2 arg2, const Arg3 arg3)=0; // call using function
29
30};
31
32
33// derived template class
34template <class TClass, class Arg1 = Empty, class Arg2 = Empty, class Arg3 = Empty>
35class TSpecificFunctor : public TFunctor<Arg1, Arg2, Arg3>
36{
37 private:
38 union FunctionPtr
39 {
40 void (TClass::*fptNoArg)(void);
41 void (TClass::*fptOneArg)(Arg1);
42 void (TClass::*fptTwoArg)(Arg1, Arg2);
43 void (TClass::*fptThreeArg)(Arg1, Arg2, Arg3);
44 } FPtr;
45
46 TClass* pt2Object; // pointer to object
47 int m_numArg;
48
49 public:
50 // default constructor
51 // TSpecificFunctor() { fpt = 0; pt2Object = 0; }
52 TSpecificFunctor() {
53 pt2Object = NULL;
54 if (typeid(Arg1) == typeid(Empty))
55 {
56 m_numArg = 0;
57 FPtr.fptNoArg = NULL;
58 } else if (typeid(Arg2) == typeid(Empty))
59 {
60 m_numArg = 1;
61 FPtr.fptOneArg = NULL;
62 } else if (typeid(Arg3) == typeid(Empty))
63 {
64 m_numArg = 2;
65 FPtr.fptTwoArg = NULL;
66 } else {
67 m_numArg = 3;
68 FPtr.fptThreeArg = NULL;
69 }
70 }
71
72 //////////////////
73 // No Arguments //
74 //////////////////
75
76 // constructor - takes pointer to an object and pointer to a member and stores
77 // them in two private variables
78 TSpecificFunctor(TClass* _pt2Object, void(TClass::*_fpt)(void))
79 {
80 m_numArg = 0;
81 pt2Object = _pt2Object;
82 FPtr.fptNoArg = _fpt;
83 };
84
85 // set up the function pointers, and object
86 void initialize(TClass* _pt2Object, void(TClass::*_fpt)(void))
87 {
88 m_numArg = 0;
89 pt2Object = _pt2Object;
90 FPtr.fptNoArg = _fpt;
91 }
92
93 // override operator "()"
94 virtual void operator()()
95 {
96 if (pt2Object && FPtr.fptNoArg )
97 {
98 (*pt2Object.*(FPtr.fptNoArg))();
99 } else {
100 std::cerr << "warning: either function pointer not initialized, "
101 << "or incorrect number of arguments" << std::endl;
102 }
103 } // execute member function
104
105 // override function "Call"
106 virtual void Call()
107 {
108 if (pt2Object && FPtr.fptNoArg )
109 {
110 (*pt2Object.*(FPtr.fptNoArg))();
111 } else {
112 std::cerr << "warning: either function pointer not initialized, "
113 << "or incorrect number of arguments" << std::endl;
114 }
115 } // execute member function
116
117
118 //////////////////
119 // One Argument //
120 //////////////////
121
122 // constructor - takes pointer to an object and pointer to a member and stores
123 // them in two private variables
124 TSpecificFunctor(TClass* _pt2Object, void(TClass::*_fpt)(const Arg1))
125 {
126 m_numArg = 1;
127 pt2Object = _pt2Object;
128 FPtr.fptOneArg = _fpt;
129 };
130
131 // set up the function pointers, and object
132 void initialize(TClass* _pt2Object, void(TClass::*_fpt)(const Arg1))
133 {
134 m_numArg = 1;
135 pt2Object = _pt2Object;
136 FPtr.fptOneArg = _fpt;
137 }
138
139 // override operator "()"
140 virtual void operator()(const Arg1 arg1)
141 {
142 if (pt2Object && FPtr.fptOneArg )
143 {
144 (*pt2Object.*(FPtr.fptOneArg))(arg1);
145 } else {
146 std::cerr << "warning: either function pointer not initialized, "
147 << "or incorrect number of arguments" << std::endl;
148 }
149 } // execute member function
150
151 // override function "Call"
152 virtual void Call(const Arg1 arg1)
153 {
154 if (pt2Object && FPtr.fptOneArg )
155 {
156 (*pt2Object.*(FPtr.fptOneArg))(arg1);
157 } else {
158 std::cerr << "warning: either function pointer not initialized, "
159 << "or incorrect number of arguments" << std::endl;
160 }
161 } // execute member function
162
163 ///////////////////
164 // Two Arguments //
165 ///////////////////
166
167 // constructor - takes pointer to an object and pointer to a member and stores
168 // them in two private variables
169 TSpecificFunctor(TClass* _pt2Object, void(TClass::*_fpt)(const Arg1, const Arg2))
170 {
171 m_numArg = 2;
172 pt2Object = _pt2Object;
173 FPtr.fptTwoArg = _fpt;
174 };
175
176 // set up the function pointers, and object
177 void initialize(TClass* _pt2Object, void(TClass::*_fpt)(const Arg1, const Arg2))
178 {
179 m_numArg = 2;
180 pt2Object = _pt2Object;
181 FPtr.fptTwoArg = _fpt;
182 }
183
184 // override operator "()"
185 virtual void operator()(const Arg1 arg1, const Arg2 arg2)
186 {
187 if (pt2Object && FPtr.fptTwoArg )
188 {
189 (*pt2Object.*(FPtr.fptTwoArg))(arg1, arg2);
190 } else {
191 std::cerr << "warning: either function pointer not initialized, "
192 << "or incorrect number of arguments" << std::endl;
193 }
194 } // execute member function
195
196 // override function "Call"
197 virtual void Call(const Arg1 arg1, const Arg2 arg2)
198 {
199 if (pt2Object && FPtr.fptTwoArg )
200 {
201 (*pt2Object.*(FPtr.fptTwoArg))(arg1, arg2);
202 } else {
203 std::cerr << "warning: either function pointer not initialized, "
204 << "or incorrect number of arguments" << std::endl;
205 }
206 } // execute member function
207
208 /////////////////////
209 // Three Arguments //
210 /////////////////////
211
212 // constructor - takes pointer to an object and pointer to a member and stores
213 // them in two private variables
214 TSpecificFunctor(TClass* _pt2Object,
215 void(TClass::*_fpt)(const Arg1, const Arg2, const Arg3))
216 {
217 m_numArg = 3;
218 pt2Object = _pt2Object;
219 FPtr.fptThreeArg = _fpt;
220 };
221
222 // set up the function pointers, and object
223 void initialize(TClass* _pt2Object,
224 void(TClass::*_fpt)(const Arg1, const Arg2, const Arg3))
225 {
226 m_numArg = 3;
227 pt2Object = _pt2Object;
228 FPtr.fptThreeArg = _fpt;
229 }
230
231 // override operator "()"
232 virtual void operator()(const Arg1 arg1, const Arg2 arg2, const Arg3 arg3)
233 {
234 if (pt2Object && FPtr.fptThreeArg )
235 {
236 (*pt2Object.*(FPtr.fptThreeArg))(arg1, arg2, arg3);
237 } else {
238 std::cerr << "warning: either function pointer not initialized, "
239 << "or incorrect number of arguments" << std::endl;
240 }
241 } // execute member function
242
243 // override function "Call"
244 virtual void Call(const Arg1 arg1, const Arg2 arg2, const Arg3 arg3)
245 {
246 if (pt2Object && FPtr.fptThreeArg )
247 {
248 (*pt2Object.*(FPtr.fptThreeArg))(arg1, arg2, arg3);
249 } else {
250 std::cerr << "warning: either function pointer not initialized, "
251 << "or incorrect number of arguments" << std::endl;
252 }
253 } // execute member function
254
255
256};
257
258
259#endif

This class is templated to hold function pointers. It currently can take function pointers with up to three arguments, but is easily expanded to more. This can be helpful so that you can use one interface for different types of function pointers.