diff --git a/simgear/structure/callback.hxx b/simgear/structure/callback.hxx index f1d16e2a..745f1818 100644 --- a/simgear/structure/callback.hxx +++ b/simgear/structure/callback.hxx @@ -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 #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); -} - -template< class ObjPtr, typename MemFn > -SGCallback* -make_callback( const ObjPtr& pObj, MemFn pMemFn ) -{ - return new SGMethodCallback(pObj, pMemFn ); +namespace simgear { + using Callback = std::function; } #endif // _SG_CALLBACK_HXX - diff --git a/simgear/structure/event_mgr.cxx b/simgear/structure/event_mgr.cxx index 3bdfda18..56247ed3 100644 --- a/simgear/structure/event_mgr.cxx +++ b/simgear/structure/event_mgr.cxx @@ -8,18 +8,12 @@ #include -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) { diff --git a/simgear/structure/event_mgr.hxx b/simgear/structure/event_mgr.hxx index 9fc529a6..8b661f02 100644 --- a/simgear/structure/event_mgr.hxx +++ b/simgear/structure/event_mgr.hxx @@ -4,6 +4,8 @@ #include #include +#include + #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 - 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 - 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 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 - 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);