From 1c1c97cbd1d7913ac12bf550ec02c97f843a0fd3 Mon Sep 17 00:00:00 2001 From: Loïc Blot Date: Sun, 20 Aug 2017 13:30:50 +0200 Subject: Modernize source code: last part (#6285) * Modernize source code: last par * Use empty when needed * Use emplace_back instead of push_back when needed * For range-based loops * Initializers fixes * constructors, destructors default * c++ C stl includes --- src/script/cpp_api/s_async.cpp | 25 ++++++++++--------------- src/script/cpp_api/s_async.h | 2 +- src/script/cpp_api/s_base.cpp | 2 +- src/script/cpp_api/s_base.h | 4 ++-- src/script/cpp_api/s_inventory.cpp | 22 +++++++++------------- src/script/cpp_api/s_item.cpp | 10 ++++++---- src/script/cpp_api/s_node.cpp | 10 ++-------- src/script/cpp_api/s_node.h | 4 ++-- src/script/cpp_api/s_nodemeta.cpp | 9 --------- src/script/cpp_api/s_nodemeta.h | 4 ++-- src/script/cpp_api/s_player.cpp | 5 ----- src/script/cpp_api/s_player.h | 2 +- src/script/cpp_api/s_security.cpp | 14 ++++++++------ 13 files changed, 44 insertions(+), 69 deletions(-) (limited to 'src/script/cpp_api') diff --git a/src/script/cpp_api/s_async.cpp b/src/script/cpp_api/s_async.cpp index 5cca5fc03..93a200c22 100644 --- a/src/script/cpp_api/s_async.cpp +++ b/src/script/cpp_api/s_async.cpp @@ -17,8 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include -#include +#include +#include extern "C" { #include "lua.h" @@ -38,9 +38,8 @@ AsyncEngine::~AsyncEngine() { // Request all threads to stop - for (std::vector::iterator it = workerThreads.begin(); - it != workerThreads.end(); ++it) { - (*it)->stop(); + for (AsyncWorkerThread *workerThread : workerThreads) { + workerThread->stop(); } @@ -51,15 +50,13 @@ AsyncEngine::~AsyncEngine() } // Wait for threads to finish - for (std::vector::iterator it = workerThreads.begin(); - it != workerThreads.end(); ++it) { - (*it)->wait(); + for (AsyncWorkerThread *workerThread : workerThreads) { + workerThread->wait(); } // Force kill all threads - for (std::vector::iterator it = workerThreads.begin(); - it != workerThreads.end(); ++it) { - delete *it; + for (AsyncWorkerThread *workerThread : workerThreads) { + delete workerThread; } jobQueueMutex.lock(); @@ -192,9 +189,8 @@ void AsyncEngine::pushFinishedJobs(lua_State* L) { /******************************************************************************/ void AsyncEngine::prepareEnvironment(lua_State* L, int top) { - for (std::vector::iterator it = stateInitializers.begin(); - it != stateInitializers.end(); it++) { - (*it)(L, top); + for (StateInitializer &stateInitializer : stateInitializers) { + stateInitializer(L, top); } } @@ -202,7 +198,6 @@ void AsyncEngine::prepareEnvironment(lua_State* L, int top) AsyncWorkerThread::AsyncWorkerThread(AsyncEngine* jobDispatcher, const std::string &name) : Thread(name), - ScriptApiBase(), jobDispatcher(jobDispatcher) { lua_State *L = getStack(); diff --git a/src/script/cpp_api/s_async.h b/src/script/cpp_api/s_async.h index 38eb4800a..b1f4bf45f 100644 --- a/src/script/cpp_api/s_async.h +++ b/src/script/cpp_api/s_async.h @@ -68,7 +68,7 @@ class AsyncEngine { friend class AsyncWorkerThread; typedef void (*StateInitializer)(lua_State *L, int top); public: - AsyncEngine() {}; + AsyncEngine() = default; ~AsyncEngine(); /** diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp index 6bea8230b..32a99826e 100644 --- a/src/script/cpp_api/s_base.cpp +++ b/src/script/cpp_api/s_base.cpp @@ -40,7 +40,7 @@ extern "C" { #endif } -#include +#include #include #include "script/common/c_content.h" #include diff --git a/src/script/cpp_api/s_base.h b/src/script/cpp_api/s_base.h index b2c8b4a18..a170f82dc 100644 --- a/src/script/cpp_api/s_base.h +++ b/src/script/cpp_api/s_base.h @@ -40,12 +40,12 @@ extern "C" { // use that name to bypass security! #define BUILTIN_MOD_NAME "*builtin*" -#define PCALL_RES(RES) do { \ +#define PCALL_RES(RES) { \ int result_ = (RES); \ if (result_ != 0) { \ scriptError(result_, __FUNCTION__); \ } \ -} while (0) +} #define runCallbacks(nargs, mode) \ runCallbacksRaw((nargs), (mode), __FUNCTION__) diff --git a/src/script/cpp_api/s_inventory.cpp b/src/script/cpp_api/s_inventory.cpp index c90c7d4e2..251ddb221 100644 --- a/src/script/cpp_api/s_inventory.cpp +++ b/src/script/cpp_api/s_inventory.cpp @@ -218,8 +218,7 @@ bool ScriptApiDetached::getDetachedInventoryCallback( lua_getfield(L, -1, name.c_str()); lua_remove(L, -2); // Should be a table - if(lua_type(L, -1) != LUA_TTABLE) - { + if (lua_type(L, -1) != LUA_TTABLE) { errorstream<<"Detached inventory \""< mods = gamedef->getMods(); - for (size_t i = 0; i < mods.size(); ++i) { - str = fs::AbsolutePath(mods[i].path); + const std::vector &mods = gamedef->getMods(); + for (const ModSpec &mod : mods) { + str = fs::AbsolutePath(mod.path); if (!str.empty() && fs::PathStartsWith(abs_path, str)) { return true; } @@ -617,7 +617,9 @@ int ScriptApiSecurity::sl_g_load(lua_State *L) int t = lua_type(L, -1); if (t == LUA_TNIL) { break; - } else if (t != LUA_TSTRING) { + } + + if (t != LUA_TSTRING) { lua_pushnil(L); lua_pushliteral(L, "Loader didn't return a string"); return 2; -- cgit v1.2.3