Use std::function for callbacks

Reduce complexity by using std::function for callbacks instead of our own class.
This commit is contained in:
Lars Toenning 2022-05-21 13:33:17 +02:00 committed by James Turner
parent e1aba1364b
commit e04f89e8b2
3 changed files with 19 additions and 181 deletions

View File

@ -1,148 +1,13 @@
/**************************************************************************
* 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $Id$
**************************************************************************/
// Declaration for simgear callback
// SPDX-License-Identifier: GPL-2.0-or-later
#include <functional>
#ifndef _SG_CALLBACK_HXX
#define _SG_CALLBACK_HXX
/**
* Abstract base class for all callbacks.
*/
class SGCallback
{
public:
/**
*
*/
virtual ~SGCallback() {}
/**
*
*/
virtual SGCallback* clone() const = 0;
/**
* Execute the callback function.
*/
virtual void operator()() = 0;
protected:
/**
*
*/
SGCallback() {}
private:
// Not implemented.
void operator=( const SGCallback& );
};
/**
* Callback for invoking a file scope function.
*/
template< typename Fun >
class SGFunctionCallback : public SGCallback
{
public:
/**
*
*/
SGFunctionCallback( const Fun& fun )
: SGCallback(), f_(fun) {}
SGCallback* clone() const
{
return new SGFunctionCallback( *this );
}
void operator()() { f_(); }
private:
// Not defined.
SGFunctionCallback();
private:
Fun f_;
};
/**
* Callback for invoking a member function.
*/
template< class ObjPtr, typename MemFn >
class SGMethodCallback : public SGCallback
{
public:
/**
*
*/
SGMethodCallback( const ObjPtr& pObj, MemFn pMemFn )
: SGCallback(),
pObj_(pObj),
pMemFn_(pMemFn)
{
}
/**
*
*/
SGCallback* clone() const
{
return new SGMethodCallback( *this );
}
/**
*
*/
void operator()()
{
((*pObj_).*pMemFn_)();
}
private:
// Not defined.
SGMethodCallback();
private:
ObjPtr pObj_;
MemFn pMemFn_;
};
/**
* Helper template functions.
*/
template< typename Fun >
SGCallback*
make_callback( const Fun& fun )
{
return new SGFunctionCallback<Fun>(fun);
}
template< class ObjPtr, typename MemFn >
SGCallback*
make_callback( const ObjPtr& pObj, MemFn pMemFn )
{
return new SGMethodCallback<ObjPtr,MemFn>(pObj, pMemFn );
namespace simgear {
using Callback = std::function<void()>;
}
#endif // _SG_CALLBACK_HXX

View File

@ -8,18 +8,12 @@
#include <simgear/debug/logstream.hxx>
SGTimer::~SGTimer()
{
delete callback;
callback = nullptr;
}
void SGTimer::run()
{
(*callback)();
callback();
}
void SGEventMgr::add(const std::string& name, SGCallback* cb,
void SGEventMgr::add(const std::string& name, simgear::Callback cb,
double interval, double delay,
bool repeat, bool simtime)
{

View File

@ -4,6 +4,8 @@
#include <simgear/props/props.hxx>
#include <simgear/structure/subsystem_mgr.hxx>
#include <utility>
#include "callback.hxx"
class SGEventMgr;
@ -12,12 +14,12 @@ class SGTimer
{
public:
SGTimer() = default;
~SGTimer();
void run();
std::string name;
double interval = 0.0;
SGCallback* callback = nullptr;
simgear::Callback callback;
bool repeat = false;
bool running = false;
@ -76,42 +78,19 @@ public:
void setRealtimeProperty(SGPropertyNode* node) { _rtProp = node; }
/**
* Add a single function callback event as a repeating task.
* ex: addTask("foo", &Function ... )
* Add a callback as a one-shot event.
*/
template<typename FUNC>
inline void addTask(const std::string& name, const FUNC& f,
double interval, double delay=0, bool sim=false)
{ add(name, make_callback(f), interval, delay, true, sim); }
/**
* Add a single function callback event as a one-shot event.
* ex: addEvent("foo", &Function ... )
*/
template<typename FUNC>
inline void addEvent(const std::string& name, const FUNC& f,
inline void addEvent(const std::string& name, simgear::Callback cb,
double delay, bool sim=false)
{ add(name, make_callback(f), 0, delay, false, sim); }
{ add(name, std::move(cb), 0, delay, false, sim); }
/**
* Add a object/method pair as a repeating task.
* ex: addTask("foo", &object, &ClassName::Method, ...)
* Add a callback as a repeating task.
*/
template<class OBJ, typename METHOD>
inline void addTask(const std::string& name,
const OBJ& o, METHOD m,
simgear::Callback cb,
double interval, double delay=0, bool sim=false)
{ add(name, make_callback(o,m), interval, delay, true, sim); }
/**
* Add a object/method pair as a repeating task.
* ex: addEvent("foo", &object, &ClassName::Method, ...)
*/
template<class OBJ, typename METHOD>
inline void addEvent(const std::string& name,
const OBJ& o, METHOD m,
double delay, bool sim=false)
{ add(name, make_callback(o,m), 0, delay, false, sim); }
{ add(name, std::move(cb), interval, delay, true, sim); }
void removeTask(const std::string& name);
@ -121,7 +100,7 @@ public:
private:
friend class SGTimer;
void add(const std::string& name, SGCallback* cb,
void add(const std::string& name, simgear::Callback cb,
double interval, double delay,
bool repeat, bool simtime);