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
26 * Abstract base class for all FlightGear callbacks.
35 virtual ~fgCallback() {}
40 virtual fgCallback* clone() const = 0;
43 * Execute the callback function.
45 virtual void operator()() = 0;
55 void operator=( const fgCallback& );
59 * Callback for invoking a file scope function.
61 template< typename Fun >
62 class fgFunctionCallback : public fgCallback
68 fgFunctionCallback( const Fun& fun )
69 : fgCallback(), f_(fun) {}
71 fgCallback* clone() const
73 return new fgFunctionCallback( *this );
76 void operator()() { f_(); }
87 * Callback for invoking a member function.
89 template< class ObjPtr, typename MemFn >
90 class fgMethodCallback : public fgCallback
97 fgMethodCallback( const ObjPtr& pObj, MemFn pMemFn )
107 fgCallback* clone() const
109 return new fgMethodCallback( *this );
117 ((*pObj_).*pMemFn_)();
130 * Helper template functions.
133 template< typename Fun >
135 make_callback( const Fun& fun )
137 return new fgFunctionCallback<Fun>(fun);
140 template< class ObjPtr, typename MemFn >
142 make_callback( const ObjPtr& pObj, MemFn pMemFn )
144 return new fgMethodCallback<ObjPtr,MemFn>(pObj, pMemFn );
147 #endif // _FG_CALLBACK_HXX