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 @@
/************************************************************************** // Declaration for simgear callback
* callback.hxx -- Wrapper classes to treat function and method pointers // SPDX-License-Identifier: GPL-2.0-or-later
* as objects.
* #include <functional>
* 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$
**************************************************************************/
#ifndef _SG_CALLBACK_HXX #ifndef _SG_CALLBACK_HXX
#define _SG_CALLBACK_HXX #define _SG_CALLBACK_HXX
/** namespace simgear {
* Abstract base class for all callbacks. using Callback = std::function<void()>;
*/
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 );
} }
#endif // _SG_CALLBACK_HXX #endif // _SG_CALLBACK_HXX

View File

@ -8,18 +8,12 @@
#include <simgear/debug/logstream.hxx> #include <simgear/debug/logstream.hxx>
SGTimer::~SGTimer()
{
delete callback;
callback = nullptr;
}
void SGTimer::run() 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, double interval, double delay,
bool repeat, bool simtime) bool repeat, bool simtime)
{ {

View File

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