1 /**************************************************************************
2 * fg_callback.hxx -- Wrapper classes to treat function and method pointers
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.
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.
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.
20 **************************************************************************/
22 #ifndef _FG_CALLBACK_HXX
23 #define _FG_CALLBACK_HXX
27 // -dw- need size_t for params() function
29 typedef unsigned long size_t;
33 //-----------------------------------------------------------------------------
35 // Abstract base class for all FlightGear callbacks.
40 virtual ~fgCallback() {}
42 virtual fgCallback* clone() const = 0;
43 virtual void* call( void** ) = 0;
45 size_t params() const { return n_params; }
48 fgCallback( size_t params )
52 // The number of parameters to pass to the callback routine.
58 //-----------------------------------------------------------------------------
60 // Callback for invoking a file scope function.
62 class fgFunctionCallback : public fgCallback
65 // Pointer to function taking no arguments and returning void.
66 typedef void (*Proc0v)();
68 // A callback instance to invoke the function 'p'
69 fgFunctionCallback( Proc0v p );
71 // Create a clone on the heap.
72 virtual fgCallback* clone() const;
75 void* call( void** in );
76 inline void* call0v( void** );
84 typedef void* (fgFunctionCallback::*DoPtr)( void** );
90 fgFunctionCallback::fgFunctionCallback( Proc0v p )
92 doPtr(&fgFunctionCallback::call0v),
99 fgFunctionCallback::clone() const
101 return new fgFunctionCallback( *this );
105 fgFunctionCallback::call( void** in )
107 return (this->*doPtr)( in );
111 fgFunctionCallback::call0v( void** )
117 //-----------------------------------------------------------------------------
119 // Callback for invoking a member function.
121 //template< typename Ret, class T >
123 class fgMethodCallback : public fgCallback
126 // Pointer to method taking no arguments and returning void.
127 typedef void (T::*Method0v)();
128 typedef void (T::*Method0vc)() const;
130 // A callback instance to invoke method 'm' of object 'o'
131 fgMethodCallback( T* o, Method0v m )
135 doPtr(&fgMethodCallback<T>::call0v) {}
137 // A callback instance to invoke a const method 'm' of object 'o'
138 fgMethodCallback( T* o, Method0vc m )
142 doPtr(&fgMethodCallback<T>::call0v) {}
144 // Create a clone on the heap.
145 fgCallback* clone() const;
149 void* call( void** in );
152 void* call0v( void** );
165 // typedef void * (fgMethodCallback::*DoPtr)( void ** );
166 typedef void * (fgMethodCallback<T>::*DoPtr)( void ** );
170 template< class T > inline fgCallback*
171 fgMethodCallback<T>::clone() const
173 return new fgMethodCallback( *this );
176 template< class T > inline void*
177 fgMethodCallback<T>::call( void** in )
179 return (this->*doPtr)( in );
183 template< class T > inline void*
184 fgMethodCallback<T>::call0v( void** )
186 (object->*method0v)();
190 #endif // _FG_CALLBACK_HXX