]> git.mxchange.org Git - flightgear.git/blob - src/Include/fg_callback.hxx
Bernie Bright:
[flightgear.git] / src / Include / fg_callback.hxx
1 /**************************************************************************
2  * fg_callback.hxx -- Wrapper classes to treat function and method pointers
3  * as objects.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * $Id$
20  **************************************************************************/
21
22 #ifndef _FG_CALLBACK_HXX
23 #define _FG_CALLBACK_HXX
24
25 #include <stdlib.h>
26
27 // -dw- need size_t for params() function
28 #ifdef __MWERKS__
29 typedef unsigned long   size_t;
30 #endif
31
32
33 //-----------------------------------------------------------------------------
34 //
35 // Abstract base class for all FlightGear callbacks.
36 //
37 class fgCallback
38 {
39 public:
40     virtual ~fgCallback() {}
41
42     virtual fgCallback* clone() const = 0;
43     virtual void* call( void** ) = 0;
44
45     size_t params() const { return n_params; }
46
47 protected:
48     fgCallback( size_t params )
49         : n_params(params) {}
50
51 protected:
52     // The number of parameters to pass to the callback routine.
53     size_t n_params;
54
55 private:
56 };
57
58 //-----------------------------------------------------------------------------
59 //
60 // Callback for invoking a file scope function.
61 //
62 class fgFunctionCallback : public fgCallback
63 {
64 public:
65     // Pointer to function taking no arguments and returning void.
66     typedef void (*Proc0v)();
67
68     // A callback instance to invoke the function 'p'
69     fgFunctionCallback( Proc0v p );
70
71     // Create a clone on the heap.
72     virtual fgCallback* clone() const;
73
74 private:
75     void* call( void** in );
76     inline void* call0v( void** );
77
78 private:
79     // Not defined.
80     fgFunctionCallback();
81
82 private:
83
84     typedef void* (fgFunctionCallback::*DoPtr)( void** );
85     DoPtr doPtr;
86     Proc0v proc0v;
87 };
88
89 inline
90 fgFunctionCallback::fgFunctionCallback( Proc0v p )
91     : fgCallback(0),
92       doPtr(&fgFunctionCallback::call0v),
93       proc0v(p)
94 {
95     // empty
96 }
97
98 inline fgCallback*
99 fgFunctionCallback::clone() const
100 {
101     return new fgFunctionCallback( *this );
102 }
103
104 inline void*
105 fgFunctionCallback::call( void** in )
106 {
107     return (this->*doPtr)( in );
108 }
109
110 inline void*
111 fgFunctionCallback::call0v( void** )
112 {
113     (*proc0v)();
114     return (void*) NULL;
115 }
116
117 //-----------------------------------------------------------------------------
118 //
119 // Callback for invoking a member function.
120 //
121 //template< typename Ret, class T >
122 template< class T >
123 class fgMethodCallback : public fgCallback
124 {
125 public:
126     // Pointer to method taking no arguments and returning void.
127     typedef void (T::*Method0v)();
128     typedef void (T::*Method0vc)() const;
129
130     // A callback instance to invoke method 'm' of object 'o' 
131     fgMethodCallback( T* o, Method0v m )
132         : fgCallback(0),
133           object(o),
134           method0v(m),
135           doPtr(&fgMethodCallback<T>::call0v) {}
136
137     // A callback instance to invoke a const method 'm' of object 'o' 
138     fgMethodCallback( T* o, Method0vc m )
139         : fgCallback(0),
140           object(o),
141           method0vc(m),
142           doPtr(&fgMethodCallback<T>::call0v) {}
143
144     // Create a clone on the heap.
145     fgCallback* clone() const;
146
147 private:
148     //
149     void* call( void** in );
150
151     //
152     void* call0v( void** );
153
154 private:
155     // Not defined.
156     fgMethodCallback();
157
158 private:
159     T* object;
160     union {
161         Method0v  method0v;
162         Method0vc method0vc;
163     };
164
165     // typedef void * (fgMethodCallback::*DoPtr)( void ** );
166     typedef void * (fgMethodCallback<T>::*DoPtr)( void ** );
167     DoPtr doPtr;
168 };
169
170 template< class T > inline fgCallback*
171 fgMethodCallback<T>::clone() const
172 {
173     return new fgMethodCallback( *this );
174 }
175
176 template< class T > inline void*
177 fgMethodCallback<T>::call( void** in )
178 {
179     return (this->*doPtr)( in );
180 }
181
182
183 template< class T > inline void*
184 fgMethodCallback<T>::call0v( void** )
185 {
186     (object->*method0v)();
187     return (void*) NULL;
188 }
189
190 #endif // _FG_CALLBACK_HXX
191