Skip to content

Commit

Permalink
Merge pull request otland#512 from dalkon/master
Browse files Browse the repository at this point in the history
Only use the move operator.
  • Loading branch information
marksamman committed Jan 28, 2014
2 parents dd2450b + 51badd6 commit 3719c84
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
31 changes: 17 additions & 14 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4238,38 +4238,41 @@ int32_t LuaScriptInterface::luaAddEvent(lua_State* L)
delay, std::bind(&LuaEnvironment::executeTimerEvent, &g_luaEnvironment, lastTimerEventId)
));

g_luaEnvironment.m_timerEvents[lastTimerEventId] = eventDesc;
g_luaEnvironment.m_timerEvents.insert(std::make_pair(lastTimerEventId, std::move(eventDesc)));

pushNumber(L, lastTimerEventId++);
return 1;
}

int32_t LuaScriptInterface::luaStopEvent(lua_State* L)
{
//stopEvent(eventid)
uint32_t eventId = popNumber(L);
if (!g_luaEnvironment.getLuaState()) {
lua_State* globalState = g_luaEnvironment.getLuaState();
if (!globalState) {
reportErrorFunc("No valid script interface!");
pushBoolean(L, false);
return 1;
}

uint32_t eventId = getNumber<uint32_t>(L, 1);

auto& timerEvents = g_luaEnvironment.m_timerEvents;
auto it = timerEvents.find(eventId);
if (it == timerEvents.end()) {
pushBoolean(L, false);
return 1;
}

const LuaTimerEventDesc& timerEventDesc = it->second;
LuaTimerEventDesc timerEventDesc = std::move(it->second);
timerEvents.erase(it);

g_scheduler.stopEvent(timerEventDesc.eventId);
luaL_unref(globalState, LUA_REGISTRYINDEX, timerEventDesc.function);

for (auto parameter : timerEventDesc.parameters) {
luaL_unref(g_luaEnvironment.m_luaState, LUA_REGISTRYINDEX, parameter);
luaL_unref(globalState, LUA_REGISTRYINDEX, parameter);
}

luaL_unref(g_luaEnvironment.m_luaState, LUA_REGISTRYINDEX, timerEventDesc.function);
timerEvents.erase(it);

pushBoolean(L, true);
return 1;
}
Expand Down Expand Up @@ -12045,27 +12048,27 @@ bool LuaEnvironment::closeState()
for (const auto& combatEntry : m_combatIdMap) {
clearCombatObjects(combatEntry.first);
}
m_combatIdMap.clear();

for (const auto& conditionEntry : m_conditionIdMap) {
clearConditionObjects(conditionEntry.first);
}
m_conditionIdMap.clear();

for (const auto& areaEntry : m_areaIdMap) {
clearAreaObjects(areaEntry.first);
}
m_areaIdMap.clear();

for (auto timerEntry : m_timerEvents) {
const LuaTimerEventDesc& timerEventDesc = timerEntry.second;
for (auto& timerEntry : m_timerEvents) {
LuaTimerEventDesc timerEventDesc = std::move(timerEntry.second);
for (int32_t parameter : timerEventDesc.parameters) {
luaL_unref(m_luaState, LUA_REGISTRYINDEX, parameter);
}
luaL_unref(m_luaState, LUA_REGISTRYINDEX, timerEventDesc.function);
}
m_timerEvents.clear();

m_combatIdMap.clear();
m_conditionIdMap.clear();
m_areaIdMap.clear();
m_timerEvents.clear();
m_cacheFiles.clear();

lua_close(m_luaState);
Expand Down
4 changes: 0 additions & 4 deletions src/luascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ struct LuaTimerEventDesc {
LuaTimerEventDesc() :
scriptId(-1), function(-1), eventId(0) {}

LuaTimerEventDesc(const LuaTimerEventDesc& other) :
scriptId(other.scriptId), function(other.function),
parameters(other.parameters), eventId(other.eventId) {}

LuaTimerEventDesc(LuaTimerEventDesc&& other) :
scriptId(other.scriptId), function(other.function),
parameters(std::move(other.parameters)), eventId(other.eventId) {}
Expand Down

0 comments on commit 3719c84

Please sign in to comment.