History

make use of stl containers and RAII

The QTimer class automatically stops the timer, so no need to do it automatically.

RTUnreal committed 4 months ago

Commit b87aa041 parent 42bbbaf2 files · +12 −45Browse files

Showing the changes to libraries/script-engine/src/ScriptManager.cpp (1 of 2 files). Show all

Unified · Split

libraries/script-engine/src/ScriptManager.cpp+11−44
@@ -14,6 +14,7 @@ · 13 unmodified lines
1414 #include "ScriptManager.h"
1515
1616 #include <chrono>
17+#include <memory>
1718 #include <thread>
1819
1920 #include <QtCore/QCoreApplication>
@@ -1171,40 +1172,12 @@ void ScriptManager::run() { · 1151 unmodified lines
11711172 // NOTE: This is private because it must be called on the same thread that created the timers, which is why
11721173 // we want to only call it in our own run "shutdown" processing.
11731174 void ScriptManager::stopAllTimers() {
1174− for (const auto& pair : _timerFunctionMap) {
1175− QTimer* timer = std::get<0>(pair);
1176−
1177− timer->stop();
1178− delete timer;
1179− }
1180−
11811175 _timerFunctionMap.clear();
11821176 _timerHandleCounter = 1;
11831177 }
11841178
11851179 void ScriptManager::stopAllTimersForEntityScript(const EntityItemID& entityID) {
1186− std::vector<int> toDelete = {};
1187−
1188− for (
1189− auto i = _timerFunctionMap.constKeyValueBegin();
1190− i != _timerFunctionMap.constKeyValueEnd();
1191− ++i
1192− ) {
1193− const auto& handle = i->first;
1194− const auto& [timer, callbackData] = i->second;
1195−
1196− if (callbackData.definingEntityIdentifier != entityID) { continue; }
1197−
1198− timer->stop();
1199− delete timer;
1200−
1201− toDelete.push_back(handle);
1202− }
1203−
1204− for (auto handle : toDelete) {
1205− _timerFunctionMap.remove(handle);
1206− }
1207−
1180+ std::erase_if(_timerFunctionMap, [entityID](auto& i) { return i.second.second.definingEntityIdentifier == entityID; });
12081181 // the timer map is potentially shared across multiple entity scripts,
12091182 // so don't clear it or reset the handle counter
12101183 }
@@ -1257,12 +1230,13 @@ void ScriptManager::timerFired(int handle) { · 46 unmodified lines
12571230 return;
12581231 }
12591232
1260− auto [timer, data] = _timerFunctionMap.value(handle);
1233+ auto& functionPair = _timerFunctionMap[handle];
1234+ auto& timer = functionPair.first;
1235+ auto data = functionPair.second;
12611236
12621237 if (!timer->isActive()) {
12631238 // this timer is done, we can kill it
1264− _timerFunctionMap.remove(handle);
1265− delete timer;
1239+ _timerFunctionMap.erase(handle);
12661240 }
12671241
12681242 // call the associated JS function, if it exists
@@ -1286,7 +1260,9 @@ int ScriptManager::setupTimerWithInterval(const ScriptValue& function, int inter · 17 unmodified lines
12861260 int handle = _timerHandleCounter++;
12871261
12881262 // create the timer, add it to the map, and start it
1289− QTimer* newTimer = new QTimer(this);
1263+ CallbackData timerData = { function, currentEntityIdentifier, currentSandboxURL };
1264+ auto& newTimer =
1265+ _timerFunctionMap.emplace(handle, std::make_pair(std::make_unique<QTimer>(), timerData)).first->second.first;
12901266 newTimer->setSingleShot(isSingleShot);
12911267
12921268 // The default timer type is not very accurate below about 200ms http://doc.qt.io/qt-5/qt.html#TimerType-enum
@@ -1295,13 +1271,7 @@ int ScriptManager::setupTimerWithInterval(const ScriptValue& function, int inter · 2 unmodified lines
12951271 newTimer->setTimerType(Qt::PreciseTimer);
12961272 }
12971273
1298− connect(newTimer, &QTimer::timeout, [this, handle]() { timerFired(handle); });
1299−
1300− // make sure the timer stops when the script does
1301− connect(this, &ScriptManager::scriptEnding, newTimer, &QTimer::stop);
1302−
1303− CallbackData timerData = { function, currentEntityIdentifier, currentSandboxURL };
1304− _timerFunctionMap.insert(handle, { newTimer, timerData });
1274+ connect(newTimer.get(), &QTimer::timeout, [this, handle]() { timerFired(handle); });
13051275
13061276 if (intervalMS < 0) {
13071277 int lineNumber = -1;
@@ -1354,10 +1324,7 @@ int ScriptManager::setTimeout(const ScriptValue& function, int timeoutMS) { · 46 unmodified lines
13541324
13551325 void ScriptManager::stopTimer(int handle) {
13561326 if (_timerFunctionMap.contains(handle)) {
1357− auto [timer, callbackData] = _timerFunctionMap.value(handle);
1358− timer->stop();
1359− _timerFunctionMap.remove(handle);
1360− delete timer;
1327+ _timerFunctionMap.erase(handle);
13611328 } else {
13621329 qCDebug(scriptengine) << "stopTimer -- not in _timerFunctionMap" << handle;
13631330 }