]> git.mxchange.org Git - flightgear.git/blob - src/Include/fg_callback.hxx
Small tweaks to initialization sequence and logic so we can default to
[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 // -dw- need size_t for params() function
26 #ifdef __MWERKS__
27 typedef unsigned long   size_t;
28 #endif
29
30
31 //-----------------------------------------------------------------------------
32 //
33 // Abstract base class for all FlightGear callbacks.
34 //
35 class fgCallback
36 {
37 public:
38     virtual ~fgCallback() {}
39
40     virtual fgCallback* clone() const = 0;
41     virtual void* call( void** ) = 0;
42
43     size_t params() const { return n_params; }
44
45 protected:
46     fgCallback( size_t params )
47         : n_params(params) {}
48
49 protected:
50     // The number of parameters to pass to the callback routine.
51     size_t n_params;
52
53 private:
54 };
55
56 //-----------------------------------------------------------------------------
57 //
58 // Callback for invoking a file scope function.
59 //
60 class fgFunctionCallback : public fgCallback
61 {
62 public:
63     // Pointer to function taking no arguments and returning void.
64     typedef void (*Proc0v)();
65
66     // A callback instance to invoke the function 'p'
67     fgFunctionCallback( Proc0v p );
68
69     // Create a clone on the heap.
70     virtual fgCallback* clone() const;
71
72 private:
73     void* call( void** in );
74     inline void* call0v( void** );
75
76 private:
77     // Not defined.
78     fgFunctionCallback();
79
80 private:
81
82     typedef void* (fgFunctionCallback::*DoPtr)( void** );
83     DoPtr doPtr;
84     Proc0v proc0v;
85 };
86
87 inline
88 fgFunctionCallback::fgFunctionCallback( Proc0v p )
89     : fgCallback(0),
90       doPtr(&fgFunctionCallback::call0v),
91       proc0v(p)
92 {
93     // empty
94 }
95
96 inline fgCallback*
97 fgFunctionCallback::clone() const
98 {
99     return new fgFunctionCallback( *this );
100 }
101
102 inline void*
103 fgFunctionCallback::call( void** in )
104 {
105     return (this->*doPtr)( in );
106 }
107
108 inline void*
109 fgFunctionCallback::call0v( void** )
110 {
111     (*proc0v)();
112     return (void*) NULL;
113 }
114
115 //-----------------------------------------------------------------------------
116 //
117 // Callback for invoking an object method.
118 //
119 template< class T >
120 class fgMethodCallback : public fgCallback
121 {
122 public:
123     // Pointer to method taking no arguments and returning void.
124     typedef void (T::*Method0v)();
125
126     // A callback instance to invoke method 'm' of object 'o' 
127     fgMethodCallback( T* o, Method0v m )
128         : fgCallback(0),
129           object(o),
130           method0v(m),
131           doPtr(&fgMethodCallback<T>::call0v) {}
132
133     // Create a clone on the heap.
134     fgCallback* clone() const;
135
136 private:
137     //
138     void* call( void** in );
139
140     //
141     void* call0v( void** );
142
143 private:
144     // Not defined.
145     fgMethodCallback();
146
147 private:
148     T* object;
149     Method0v method0v;
150
151     // typedef void * (fgMethodCallback::*DoPtr)( void ** );
152     typedef void * (fgMethodCallback<T>::*DoPtr)( void ** );
153     DoPtr doPtr;
154 };
155
156 template< class T > inline fgCallback*
157 fgMethodCallback<T>::clone() const
158 {
159     return new fgMethodCallback( *this );
160 }
161
162 template< class T > inline void*
163 fgMethodCallback<T>::call( void** in )
164 {
165     return (this->*doPtr)( in );
166 }
167
168
169 template< class T > inline void*
170 fgMethodCallback<T>::call0v( void** )
171 {
172     (object->*method0v)();
173     return (void*) NULL;
174 }
175
176 #endif // _FG_CALLBACK_HXX
177