1
0
Fork 0

Use std::function for callbacks

This commit is contained in:
Lars Toenning 2022-05-24 21:52:41 +02:00 committed by James Turner
parent 83694eabde
commit 5051d90487
8 changed files with 31 additions and 31 deletions

View file

@ -149,8 +149,8 @@ SGSubsystem::InitStatus FGEnvironmentMgr::incrementalInit()
if (r == INIT_DONE) { if (r == INIT_DONE) {
fgClouds->Init(); fgClouds->Init();
_multiplayerListener = new FGEnvironmentMgrMultiplayerListener(this); _multiplayerListener = new FGEnvironmentMgrMultiplayerListener(this);
globals->get_event_mgr()->addTask("updateClosestAirport", this, globals->get_event_mgr()->addTask("updateClosestAirport",
&FGEnvironmentMgr::updateClosestAirport, 10 ); [this](){ this->updateClosestAirport(); }, 10 );
} }
return r; return r;

View file

@ -282,8 +282,8 @@ void BasicRealWxController::init()
checkNearbyMetar(); checkNearbyMetar();
update(0); // fetch data ASAP update(0); // fetch data ASAP
globals->get_event_mgr()->addTask("checkNearbyMetar", this, globals->get_event_mgr()->addTask("checkNearbyMetar",
&BasicRealWxController::checkNearbyMetar, 10 ); [this](){ this->checkNearbyMetar(); }, 10 );
} }
void BasicRealWxController::reinit() void BasicRealWxController::reinit()

View file

@ -7,6 +7,7 @@
#include "WaypointList.hxx" #include "WaypointList.hxx"
#include <algorithm> #include <algorithm>
#include <utility>
#include <plib/puAux.h> #include <plib/puAux.h>
@ -107,7 +108,7 @@ public:
} }
} }
virtual void setUpdateCallback(SGCallback* cb) virtual void setUpdateCallback(simgear::Callback cb)
{ {
_cb = cb; _cb = cb;
} }
@ -117,7 +118,7 @@ public:
{ {
if (prop->getNameString() == "edited") { if (prop->getNameString() == "edited") {
if (_cb) { if (_cb) {
(*_cb)(); _cb();
} }
} }
@ -128,7 +129,7 @@ public:
} }
private: private:
flightgear::FlightPlan* _fp; flightgear::FlightPlan* _fp;
SGCallback* _cb; simgear::Callback _cb;
}; };
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
@ -179,18 +180,16 @@ WaypointList::WaypointList(int x, int y, int width, int height) :
WaypointList::~WaypointList() WaypointList::~WaypointList()
{ {
delete _model; delete _model;
delete _updateCallback;
delete _scrollCallback;
} }
void WaypointList::setUpdateCallback(SGCallback* cb) void WaypointList::setUpdateCallback(simgear::Callback cb)
{ {
_updateCallback = cb; _updateCallback = std::move(cb);
} }
void WaypointList::setScrollCallback(SGCallback* cb) void WaypointList::setScrollCallback(simgear::Callback cb)
{ {
_scrollCallback = cb; _scrollCallback = std::move(cb);
} }
void WaypointList::setSize(int width, int height) void WaypointList::setSize(int width, int height)
@ -568,7 +567,7 @@ void WaypointList::doDragScroll()
} }
if (_scrollCallback) { if (_scrollCallback) {
(*_scrollCallback)(); _scrollCallback();
} }
} }
@ -608,7 +607,7 @@ void WaypointList::ensureRowVisible(int rowIndex)
puPostRefresh(); puPostRefresh();
if (_scrollCallback) { // keep scroll observers in sync if (_scrollCallback) { // keep scroll observers in sync
(*_scrollCallback)(); _scrollCallback();
} }
} }
@ -711,7 +710,7 @@ void WaypointList::setModel(Model* model)
} }
_model = model; _model = model;
_model->setUpdateCallback(make_callback(this, &WaypointList::modelUpdateCallback)); _model->setUpdateCallback([this](){ this->modelUpdateCallback(); });
puPostRefresh(); puPostRefresh();
} }
@ -813,7 +812,7 @@ void WaypointList::modelUpdateCallback()
// local stuff // local stuff
if (_updateCallback) { if (_updateCallback) {
(*_updateCallback)(); _updateCallback();
} }
} }
@ -859,12 +858,12 @@ void ScrolledWaypointList::setValue(int v)
void ScrolledWaypointList::init(int w, int h) void ScrolledWaypointList::init(int w, int h)
{ {
_list = new WaypointList(0, 0, w, h); _list = new WaypointList(0, 0, w, h);
_list->setUpdateCallback(make_callback(this, &ScrolledWaypointList::modelUpdated)); _list->setUpdateCallback([this](){ this->modelUpdated(); });
_hasVScroll = _list->wantsVScroll(); _hasVScroll = _list->wantsVScroll();
_list->setUserData(this); _list->setUserData(this);
_list->setCallback(waypointListCb); _list->setCallback(waypointListCb);
_list->setScrollCallback(make_callback(this, &ScrolledWaypointList::updateScroll)); _list->setScrollCallback([this](){ this->updateScroll(); });
_scrollbar = new puaScrollBar(w - _scrollWidth, 0, h, _scrollbar = new puaScrollBar(w - _scrollWidth, 0, h,
1 /*arrow*/, 1 /* vertical */, _scrollWidth); 1 /*arrow*/, 1 /* vertical */, _scrollWidth);

View file

@ -8,6 +8,7 @@
#include <simgear/compiler.h> #include <simgear/compiler.h>
#include <simgear/timing/timestamp.hxx> #include <simgear/timing/timestamp.hxx>
#include <simgear/misc/strutils.hxx> #include <simgear/misc/strutils.hxx>
#include <simgear/structure/callback.hxx>
#include "FGPUIDialog.hxx" // for GUI_ID #include "FGPUIDialog.hxx" // for GUI_ID
@ -62,8 +63,8 @@ public:
void ensureRowVisible(int row); void ensureRowVisible(int row);
void setUpdateCallback(SGCallback* cb); void setUpdateCallback(simgear::Callback cb);
void setScrollCallback(SGCallback* cb); void setScrollCallback(simgear::Callback cb);
/** /**
* Abstract interface for waypoint source * Abstract interface for waypoint source
@ -80,7 +81,7 @@ public:
virtual flightgear::FlightPlan* flightplan() const = 0; virtual flightgear::FlightPlan* flightplan() const = 0;
// update notifications // update notifications
virtual void setUpdateCallback(SGCallback* cb) = 0; virtual void setUpdateCallback(simgear::Callback cb) = 0;
// editing operations // editing operations
virtual void deleteAt(unsigned int index) = 0; virtual void deleteAt(unsigned int index) = 0;
@ -143,8 +144,8 @@ private:
bool _showLatLon; bool _showLatLon;
Model* _model; Model* _model;
SGCallback* _updateCallback; simgear::Callback _updateCallback;
SGCallback* _scrollCallback; simgear::Callback _scrollCallback;
SGTimeStamp _blinkTimer; SGTimeStamp _blinkTimer;
bool _blink; bool _blink;

View file

@ -527,7 +527,7 @@ namespace
FGRenderer *renderer = globals->get_renderer(); FGRenderer *renderer = globals->get_renderer();
renderer->resize(_xsize, _ysize); renderer->resize(_xsize, _ysize);
globals->get_event_mgr()->addTask("SnapShotTimer", globals->get_event_mgr()->addTask("SnapShotTimer",
this, &GUISnapShotOperation::timerExpired, [this](){ this->timerExpired(); },
0.1, false); 0.1, false);
} }

View file

@ -198,6 +198,6 @@ MPServerResolver::run ()
} }
// Relinguish control, call me back on the next frame // Relinguish control, call me back on the next frame
globals->get_event_mgr ()->addEvent ("MPServerResolver_update", this, &MPServerResolver::run, .0); globals->get_event_mgr ()->addEvent ("MPServerResolver_update", [this](){ this->run(); }, .0);
} }

View file

@ -174,9 +174,9 @@ public:
_isRunning = true; _isRunning = true;
if (_singleShot) { if (_singleShot) {
globals->get_event_mgr()->addEvent(_name, this, &TimerObj::invoke, _interval, _isSimTime); globals->get_event_mgr()->addEvent(_name, [this](){ this->invoke(); }, _interval, _isSimTime);
} else { } else {
globals->get_event_mgr()->addTask(_name, this, &TimerObj::invoke, globals->get_event_mgr()->addTask(_name, [this](){ this->invoke(); },
_interval, _interval /* delay */, _interval, _interval /* delay */,
_isSimTime); _isSimTime);
} }
@ -1708,7 +1708,7 @@ void FGNasalSys::setTimer(naContext c, int argc, naRef* args)
NasalTimer* t = new NasalTimer(handler, this); NasalTimer* t = new NasalTimer(handler, this);
_nasalTimers.push_back(t); _nasalTimers.push_back(t);
globals->get_event_mgr()->addEvent(name, globals->get_event_mgr()->addEvent(name,
t, &NasalTimer::timerExpired, [t](){ t->timerExpired(); },
delta.num, simtime); delta.num, simtime);
} }

View file

@ -75,8 +75,8 @@ void FGLight::init () {
_sky_tbl = std::make_unique<SGInterpTable>( sky_path ); _sky_tbl = std::make_unique<SGInterpTable>( sky_path );
// update all solar system body positions of interest // update all solar system body positions of interest
globals->get_event_mgr()->addTask("updateObjects", this, globals->get_event_mgr()->addTask("updateObjects",
&FGLight::updateObjects, 0.5 ); [this](){ this->updateObjects(); }, 0.5 );
} }