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
25 // -dw- need size_t for params() function
27 typedef unsigned long size_t;
31 //-----------------------------------------------------------------------------
33 // Abstract base class for all FlightGear callbacks.
38 virtual ~fgCallback() {}
40 virtual fgCallback* clone() const = 0;
41 virtual void* call( void** ) = 0;
43 size_t params() const { return n_params; }
46 fgCallback( size_t params )
50 // The number of parameters to pass to the callback routine.
56 //-----------------------------------------------------------------------------
58 // Callback for invoking a file scope function.
60 class fgFunctionCallback : public fgCallback
63 // Pointer to function taking no arguments and returning void.
64 typedef void (*Proc0v)();
66 // A callback instance to invoke the function 'p'
67 fgFunctionCallback( Proc0v p );
69 // Create a clone on the heap.
70 virtual fgCallback* clone() const;
73 void* call( void** in );
74 inline void* call0v( void** );
82 typedef void* (fgFunctionCallback::*DoPtr)( void** );
88 fgFunctionCallback::fgFunctionCallback( Proc0v p )
90 doPtr(&fgFunctionCallback::call0v),
97 fgFunctionCallback::clone() const
99 return new fgFunctionCallback( *this );
103 fgFunctionCallback::call( void** in )
105 return (this->*doPtr)( in );
109 fgFunctionCallback::call0v( void** )
115 //-----------------------------------------------------------------------------
117 // Callback for invoking an object method.
120 class fgMethodCallback : public fgCallback
123 // Pointer to method taking no arguments and returning void.
124 typedef void (T::*Method0v)();
126 // A callback instance to invoke method 'm' of object 'o'
127 fgMethodCallback( T* o, Method0v m )
131 doPtr(&fgMethodCallback<T>::call0v) {}
133 // Create a clone on the heap.
134 fgCallback* clone() const;
138 void* call( void** in );
141 void* call0v( void** );
151 typedef void * (fgMethodCallback::*DoPtr)( void ** );
155 template< class T > inline fgCallback*
156 fgMethodCallback<T>::clone() const
158 return new fgMethodCallback( *this );
161 template< class T > inline void*
162 fgMethodCallback<T>::call( void** in )
164 return (this->*doPtr)( in );
168 template< class T > inline void*
169 fgMethodCallback<T>::call0v( void** )
171 (object->*method0v)();
175 #endif // _FG_CALLBACK_HXX