2002-04-10 17:39:34 +00:00
|
|
|
/**************************************************************************
|
|
|
|
* fg_callback.hxx -- Wrapper classes to treat function and method pointers
|
|
|
|
* as objects.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-02-21 01:16:04 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-04-10 17:39:34 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#ifndef _FG_CALLBACK_HXX
|
|
|
|
#define _FG_CALLBACK_HXX
|
|
|
|
|
2002-04-25 20:31:38 +00:00
|
|
|
/**
|
|
|
|
* Abstract base class for all FlightGear callbacks.
|
|
|
|
*/
|
2002-04-10 17:39:34 +00:00
|
|
|
class fgCallback
|
|
|
|
{
|
|
|
|
public:
|
2002-04-25 20:31:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2002-04-10 17:39:34 +00:00
|
|
|
virtual ~fgCallback() {}
|
|
|
|
|
2002-04-25 20:31:38 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2002-04-10 17:39:34 +00:00
|
|
|
virtual fgCallback* clone() const = 0;
|
|
|
|
|
2002-04-25 20:31:38 +00:00
|
|
|
/**
|
|
|
|
* Execute the callback function.
|
|
|
|
*/
|
|
|
|
virtual void operator()() = 0;
|
2002-04-10 17:39:34 +00:00
|
|
|
|
|
|
|
protected:
|
2002-04-25 20:31:38 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
fgCallback() {}
|
2002-04-10 17:39:34 +00:00
|
|
|
|
|
|
|
private:
|
2002-04-25 20:31:38 +00:00
|
|
|
// Not implemented.
|
|
|
|
void operator=( const fgCallback& );
|
2002-04-10 17:39:34 +00:00
|
|
|
};
|
|
|
|
|
2002-04-25 20:31:38 +00:00
|
|
|
/**
|
|
|
|
* Callback for invoking a file scope function.
|
|
|
|
*/
|
|
|
|
template< typename Fun >
|
2002-04-10 17:39:34 +00:00
|
|
|
class fgFunctionCallback : public fgCallback
|
|
|
|
{
|
|
|
|
public:
|
2002-04-25 20:31:38 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
fgFunctionCallback( const Fun& fun )
|
|
|
|
: fgCallback(), f_(fun) {}
|
2002-04-10 17:39:34 +00:00
|
|
|
|
2002-04-25 20:31:38 +00:00
|
|
|
fgCallback* clone() const
|
|
|
|
{
|
|
|
|
return new fgFunctionCallback( *this );
|
|
|
|
}
|
2002-04-10 17:39:34 +00:00
|
|
|
|
2002-04-25 20:31:38 +00:00
|
|
|
void operator()() { f_(); }
|
2002-04-10 17:39:34 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Not defined.
|
|
|
|
fgFunctionCallback();
|
|
|
|
|
|
|
|
private:
|
2002-04-25 20:31:38 +00:00
|
|
|
Fun f_;
|
2002-04-10 17:39:34 +00:00
|
|
|
};
|
|
|
|
|
2002-04-25 20:31:38 +00:00
|
|
|
/**
|
|
|
|
* Callback for invoking a member function.
|
|
|
|
*/
|
|
|
|
template< class ObjPtr, typename MemFn >
|
2002-04-10 17:39:34 +00:00
|
|
|
class fgMethodCallback : public fgCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2002-04-25 20:31:38 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
fgMethodCallback( const ObjPtr& pObj, MemFn pMemFn )
|
|
|
|
: fgCallback(),
|
|
|
|
pObj_(pObj),
|
|
|
|
pMemFn_(pMemFn)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
fgCallback* clone() const
|
|
|
|
{
|
|
|
|
return new fgMethodCallback( *this );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void operator()()
|
|
|
|
{
|
|
|
|
((*pObj_).*pMemFn_)();
|
|
|
|
}
|
2002-04-10 17:39:34 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Not defined.
|
|
|
|
fgMethodCallback();
|
|
|
|
|
|
|
|
private:
|
2002-04-25 20:31:38 +00:00
|
|
|
ObjPtr pObj_;
|
|
|
|
MemFn pMemFn_;
|
2002-04-10 17:39:34 +00:00
|
|
|
};
|
|
|
|
|
2002-04-25 20:31:38 +00:00
|
|
|
/**
|
|
|
|
* Helper template functions.
|
|
|
|
*/
|
2002-04-10 17:39:34 +00:00
|
|
|
|
2002-04-25 20:31:38 +00:00
|
|
|
template< typename Fun >
|
|
|
|
fgCallback*
|
|
|
|
make_callback( const Fun& fun )
|
2002-04-10 17:39:34 +00:00
|
|
|
{
|
2002-04-25 20:31:38 +00:00
|
|
|
return new fgFunctionCallback<Fun>(fun);
|
2002-04-10 17:39:34 +00:00
|
|
|
}
|
|
|
|
|
2002-04-25 20:31:38 +00:00
|
|
|
template< class ObjPtr, typename MemFn >
|
|
|
|
fgCallback*
|
|
|
|
make_callback( const ObjPtr& pObj, MemFn pMemFn )
|
2002-04-10 17:39:34 +00:00
|
|
|
{
|
2002-04-25 20:31:38 +00:00
|
|
|
return new fgMethodCallback<ObjPtr,MemFn>(pObj, pMemFn );
|
2002-04-10 17:39:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // _FG_CALLBACK_HXX
|
|
|
|
|