1 // Copyright (C) 2005  Davis E. King (davis@dlib.net)
2 // License: Boost Software License   See LICENSE.txt for the full license.
3 #undef DLIB_MEMBER_FUNCTION_POINTER_KERNEl_ABSTRACT_
4 #ifdef DLIB_MEMBER_FUNCTION_POINTER_KERNEl_ABSTRACT_
5 
6 namespace dlib
7 {
8 
9 // ----------------------------------------------------------------------------------------
10 
11     template <
12         typename PARAM1 = void,
13         typename PARAM2 = void,
14         typename PARAM3 = void,
15         typename PARAM4 = void
16         >
17     class member_function_pointer;
18 
19 // ----------------------------------------------------------------------------------------
20 
21     template <>
22     class member_function_pointer<void,void,void,void>
23     {
24         /*!
25             INITIAL VALUE
26                 is_set() == false
27 
28             WHAT THIS OBJECT REPRESENTS
29                 This object represents a member function pointer.  It is useful because
30                 instances of this object can be created without needing to know the type
31                 of object whose member function we will be calling.
32 
33                 There are five template specializations of this object.  The first
34                 represents a pointer to a member function taking no parameters, the
35                 second represents a pointer to a member function taking one parameter,
36                 the third to one taking two parameters, and so on.
37 
38                 You specify the parameters to your member function pointer by filling in
39                 the PARAM template parameters.  For example:
40 
41                     To use a pointer to a function with no parameters you would say:
42                         member_function_pointer<> my_pointer;
43                     To use a pointer to a function that takes a single int you would say:
44                         member_function_pointer<int> my_pointer;
45                     To use a pointer to a function that takes an int and then a reference
46                     to a string you would say:
47                         member_function_pointer<int,string&> my_pointer;
48 
49                 Also note that the formal comments are only present for the first
50                 template specialization.  They are all exactly the same except for the
51                 number of parameters each takes in its member function pointer.
52         !*/
53 
54     public:
55         typedef void param1_type;
56         typedef void param2_type;
57         typedef void param3_type;
58         typedef void param4_type;
59 
60         member_function_pointer (
61         );
62         /*!
63             ensures
64                 - #*this is properly initialized
65         !*/
66 
67         member_function_pointer(
68             const member_function_pointer& item
69         );
70         /*!
71             ensures
72                 - *this == item
73         !*/
74 
75         ~member_function_pointer (
76         );
77         /*!
78             ensures
79                 - any resources associated with *this have been released
80         !*/
81 
82         member_function_pointer& operator=(
83             const member_function_pointer& item
84         );
85         /*!
86             ensures
87                 - *this == item
88         !*/
89 
90         bool operator == (
91             const member_function_pointer& item
92         ) const;
93         /*!
94             ensures
95                 - if (is_set() == false && item.is_set() == false) then
96                     - returns true
97                 - else if (both *this and item point to the same member function
98                   in the same object instance) then
99                     - returns true
100                 - else
101                     - returns false
102         !*/
103 
104         bool operator != (
105             const member_function_pointer& item
106         ) const;
107         /*!
108             ensures
109                 - returns !(*this == item)
110         !*/
111 
112         void clear(
113         );
114         /*!
115             ensures
116                 - #*this has its initial value
117         !*/
118 
119         bool is_set (
120         ) const;
121         /*!
122             ensures
123                 - if (this->set() has been called) then
124                     - returns true
125                 - else
126                     - returns false
127         !*/
128 
129         template <
130             typename T
131             >
132         void set (
133             T& object,
134             void (T::*cb)()
135         );
136         /*!
137             requires
138                 - cb == a valid member function pointer for class T
139             ensures
140                 - #is_set() == true
141                 - calls to this->operator() will call (object.*cb)()
142         !*/
143 
144         template <
145             typename T
146             >
147         void set (
148             const T& object,
149             void (T::*cb)()const
150         );
151         /*!
152             requires
153                 - cb == a valid member function pointer for class T
154             ensures
155                 - #is_set() == true
156                 - calls to this->operator() will call (object.*cb)()
157         !*/
158 
159         operator some_undefined_pointer_type (
160         ) const;
161         /*!
162             ensures
163                 - if (is_set()) then
164                     - returns a non 0 value
165                 - else
166                     - returns a 0 value
167         !*/
168 
169         bool operator! (
170         ) const;
171         /*!
172             ensures
173                 - returns !is_set()
174         !*/
175 
176         void operator () (
177         ) const;
178         /*!
179             requires
180                 - is_set() == true
181             ensures
182                 - calls the member function on the object specified by the last
183                   call to this->set()
184             throws
185                 - any exception thrown by the member function specified by
186                   the previous call to this->set().
187                     If any of these exceptions are thrown then the call to this
188                     function will have no effect on *this.
189         !*/
190 
191         void swap (
192             member_function_pointer& item
193         );
194         /*!
195             ensures
196                 - swaps *this and item
197         !*/
198 
199     };
200 
201 // ----------------------------------------------------------------------------------------
202 
203     template <
204         typename PARAM1
205         >
206     class member_function_pointer<PARAM1,void,void,void>
207     {
208     public:
209         typedef PARAM1 param1_type;
210         typedef void param2_type;
211         typedef void param3_type;
212         typedef void param4_type;
213 
214         member_function_pointer ();
215 
216         member_function_pointer(
217             const member_function_pointer& item
218         );
219 
220         ~member_function_pointer (
221         );
222 
223         member_function_pointer& operator=(
224             const member_function_pointer& item
225         );
226 
227         bool operator == (
228             const member_function_pointer& item
229         ) const;
230 
231         bool operator != (
232             const member_function_pointer& item
233         ) const;
234 
235         void clear();
236 
237         bool is_set () const;
238 
239         template <typename T>
240         void set (
241             T& object,
242             void (T::*cb)(PARAM1)
243         );
244 
245         template <typename T>
246         void set (
247             const T& object,
248             void (T::*cb)(PARAM1)const
249         );
250 
251         operator some_undefined_pointer_type (
252         ) const;
253 
254         bool operator! (
255         ) const;
256 
257         void operator () (
258             PARAM1 param1
259         ) const;
260 
261         void swap (
262             member_function_pointer& item
263         );
264 
265     };
266 
267 // ----------------------------------------------------------------------------------------
268 
269     template <
270         typename PARAM1,
271         typename PARAM2
272         >
273     class member_function_pointer<PARAM1,PARAM2,void,void>
274     {
275     public:
276         typedef PARAM1 param1_type;
277         typedef PARAM2 param2_type;
278         typedef void param3_type;
279         typedef void param4_type;
280 
281         member_function_pointer ();
282 
283         member_function_pointer(
284             const member_function_pointer& item
285         );
286 
287         ~member_function_pointer (
288         );
289 
290         member_function_pointer& operator=(
291             const member_function_pointer& item
292         );
293 
294         bool operator == (
295             const member_function_pointer& item
296         ) const;
297 
298         bool operator != (
299             const member_function_pointer& item
300         ) const;
301 
302         void clear();
303 
304         bool is_set () const;
305 
306         template <typename T>
307         void set (
308             T& object,
309             void (T::*cb)(PARAM1,PARAM2)
310         );
311 
312         template <typename T>
313         void set (
314             const T& object,
315             void (T::*cb)(PARAM1,PARAM2)const
316         );
317 
318         operator some_undefined_pointer_type (
319         ) const;
320 
321         bool operator! (
322         ) const;
323 
324         void operator () (
325             PARAM1 param1,
326             PARAM2 param2
327         ) const;
328 
329         void swap (
330             member_function_pointer& item
331         );
332 
333     };
334 
335 // ----------------------------------------------------------------------------------------
336 
337     template <
338         typename PARAM1,
339         typename PARAM2,
340         typename PARAM3
341         >
342     class member_function_pointer<PARAM1,PARAM2,PARAM3,void>
343     {
344     public:
345         typedef PARAM1 param1_type;
346         typedef PARAM2 param2_type;
347         typedef PARAM3 param3_type;
348         typedef void param4_type;
349 
350         member_function_pointer ();
351 
352         member_function_pointer(
353             const member_function_pointer& item
354         );
355 
356         ~member_function_pointer (
357         );
358 
359         member_function_pointer& operator=(
360             const member_function_pointer& item
361         );
362 
363         bool operator == (
364             const member_function_pointer& item
365         ) const;
366 
367         bool operator != (
368             const member_function_pointer& item
369         ) const;
370 
371         void clear();
372 
373         bool is_set () const;
374 
375         template <typename T>
376         void set (
377             T& object,
378             void (T::*cb)(PARAM1,PARAM2,PARAM3)
379         );
380 
381         template <typename T>
382         void set (
383             const T& object,
384             void (T::*cb)(PARAM1,PARAM2,PARAM3)const
385         );
386 
387         operator some_undefined_pointer_type (
388         ) const;
389 
390         bool operator! (
391         ) const;
392 
393         void operator () (
394             PARAM1 param1,
395             PARAM2 param2,
396             PARAM2 param3
397         ) const;
398 
399         void swap (
400             member_function_pointer& item
401         );
402 
403     };
404 
405 // ----------------------------------------------------------------------------------------
406 
407     template <
408         typename PARAM1,
409         typename PARAM2,
410         typename PARAM3,
411         typename PARAM4
412         >
413     class member_function_pointer
414     {
415     public:
416         typedef PARAM1 param1_type;
417         typedef PARAM2 param2_type;
418         typedef PARAM3 param3_type;
419         typedef PARAM4 param4_type;
420 
421         member_function_pointer ();
422 
423         member_function_pointer(
424             const member_function_pointer& item
425         );
426 
427         ~member_function_pointer (
428         );
429 
430         member_function_pointer& operator=(
431             const member_function_pointer& item
432         );
433 
434         bool operator == (
435             const member_function_pointer& item
436         ) const;
437 
438         bool operator != (
439             const member_function_pointer& item
440         ) const;
441 
442         void clear();
443 
444         bool is_set () const;
445 
446         template <typename T>
447         void set (
448             T& object,
449             void (T::*cb)(PARAM1,PARAM2,PARAM3,PARAM4)
450         );
451 
452         template <typename T>
453         void set (
454             const T& object,
455             void (T::*cb)(PARAM1,PARAM2,PARAM3,PARAM4)const
456         );
457 
458         operator some_undefined_pointer_type (
459         ) const;
460 
461         bool operator! (
462         ) const;
463 
464         void operator () (
465             PARAM1 param1,
466             PARAM2 param2,
467             PARAM2 param3,
468             PARAM2 param4
469         ) const;
470 
471         void swap (
472             member_function_pointer& item
473         );
474 
475     };
476 
477 // ----------------------------------------------------------------------------------------
478 
479 
480 }
481 
482 #endif // DLIB_MEMBER_FUNCTION_POINTER_KERNEl_ABSTRACT_
483 
484