summaryrefslogtreecommitdiff
path: root/src/script/cpp_api
diff options
context:
space:
mode:
authorLoïc Blot <nerzhul@users.noreply.github.com>2017-08-20 13:30:50 +0200
committerGitHub <noreply@github.com>2017-08-20 13:30:50 +0200
commit1c1c97cbd1d7913ac12bf550ec02c97f843a0fd3 (patch)
tree03dd0c39e323c7f0b1f06014ff30e74f429bfa01 /src/script/cpp_api
parent50669cd2822a11570ae462972194eeb2d585a8c1 (diff)
downloadminetest-1c1c97cbd1d7913ac12bf550ec02c97f843a0fd3.tar.gz
minetest-1c1c97cbd1d7913ac12bf550ec02c97f843a0fd3.tar.bz2
minetest-1c1c97cbd1d7913ac12bf550ec02c97f843a0fd3.zip
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
Diffstat (limited to 'src/script/cpp_api')
-rw-r--r--src/script/cpp_api/s_async.cpp25
-rw-r--r--src/script/cpp_api/s_async.h2
-rw-r--r--src/script/cpp_api/s_base.cpp2
-rw-r--r--src/script/cpp_api/s_base.h4
-rw-r--r--src/script/cpp_api/s_inventory.cpp22
-rw-r--r--src/script/cpp_api/s_item.cpp10
-rw-r--r--src/script/cpp_api/s_node.cpp10
-rw-r--r--src/script/cpp_api/s_node.h4
-rw-r--r--src/script/cpp_api/s_nodemeta.cpp9
-rw-r--r--src/script/cpp_api/s_nodemeta.h4
-rw-r--r--src/script/cpp_api/s_player.cpp5
-rw-r--r--src/script/cpp_api/s_player.h2
-rw-r--r--src/script/cpp_api/s_security.cpp14
13 files changed, 44 insertions, 69 deletions
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 <stdio.h>
-#include <stdlib.h>
+#include <cstdio>
+#include <cstdlib>
extern "C" {
#include "lua.h"
@@ -38,9 +38,8 @@ AsyncEngine::~AsyncEngine()
{
// Request all threads to stop
- for (std::vector<AsyncWorkerThread *>::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<AsyncWorkerThread *>::iterator it = workerThreads.begin();
- it != workerThreads.end(); ++it) {
- (*it)->wait();
+ for (AsyncWorkerThread *workerThread : workerThreads) {
+ workerThread->wait();
}
// Force kill all threads
- for (std::vector<AsyncWorkerThread *>::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<StateInitializer>::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 <stdio.h>
+#include <cstdio>
#include <cstdarg>
#include "script/common/c_content.h"
#include <sstream>
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 \""<<name<<"\" not defined"<<std::endl;
lua_pop(L, 1);
return false;
@@ -230,20 +229,17 @@ bool ScriptApiDetached::getDetachedInventoryCallback(
lua_getfield(L, -1, callbackname);
lua_remove(L, -2);
// Should be a function or nil
- if(lua_type(L, -1) == LUA_TFUNCTION)
- {
+ if (lua_type(L, -1) == LUA_TFUNCTION) {
return true;
}
- else if(lua_isnil(L, -1))
- {
- lua_pop(L, 1);
- return false;
- }
- else
- {
- errorstream<<"Detached inventory \""<<name<<"\" callback \""
- <<callbackname<<"\" is not a function"<<std::endl;
+
+ if (lua_isnil(L, -1)) {
lua_pop(L, 1);
return false;
}
+
+ errorstream << "Detached inventory \"" << name << "\" callback \""
+ << callbackname << "\" is not a function" << std::endl;
+ lua_pop(L, 1);
+ return false;
}
diff --git a/src/script/cpp_api/s_item.cpp b/src/script/cpp_api/s_item.cpp
index 032018f2f..d48a3aee9 100644
--- a/src/script/cpp_api/s_item.cpp
+++ b/src/script/cpp_api/s_item.cpp
@@ -113,12 +113,12 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item, ServerActiveObject *user)
{
SCRIPTAPI_PRECHECKHEADER
-
+
int error_handler = PUSH_ERROR_HANDLER(L);
-
+
if (!getItemCallback(item.name.c_str(), "on_secondary_use"))
return false;
-
+
LuaItemStack::create(L, item);
objectrefGetOrCreate(L, user);
PointedThing pointed;
@@ -237,7 +237,9 @@ bool ScriptApiItem::getItemCallback(const char *name, const char *callbackname)
// Should be a function or nil
if (lua_type(L, -1) == LUA_TFUNCTION) {
return true;
- } else if (!lua_isnil(L, -1)) {
+ }
+
+ if (!lua_isnil(L, -1)) {
errorstream << "Item \"" << name << "\" callback \""
<< callbackname << "\" is not a function" << std::endl;
}
diff --git a/src/script/cpp_api/s_node.cpp b/src/script/cpp_api/s_node.cpp
index aa28e3fb5..591e26975 100644
--- a/src/script/cpp_api/s_node.cpp
+++ b/src/script/cpp_api/s_node.cpp
@@ -93,12 +93,6 @@ struct EnumString ScriptApiNode::es_NodeBoxType[] =
{0, NULL},
};
-ScriptApiNode::ScriptApiNode() {
-}
-
-ScriptApiNode::~ScriptApiNode() {
-}
-
bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
ServerActiveObject *puncher, PointedThing pointed)
{
@@ -198,7 +192,7 @@ bool ScriptApiNode::node_on_flood(v3s16 p, MapNode node, MapNode newnode)
pushnode(L, newnode, ndef);
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
lua_remove(L, error_handler);
- return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
+ return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1);
}
void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
@@ -237,7 +231,7 @@ bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
lua_pushnumber(L,dtime);
PCALL_RES(lua_pcall(L, 2, 1, error_handler));
lua_remove(L, error_handler);
- return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
+ return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1);
}
void ScriptApiNode::node_on_receive_fields(v3s16 p,
diff --git a/src/script/cpp_api/s_node.h b/src/script/cpp_api/s_node.h
index 92f07cee7..5b6509c83 100644
--- a/src/script/cpp_api/s_node.h
+++ b/src/script/cpp_api/s_node.h
@@ -32,8 +32,8 @@ class ScriptApiNode
public ScriptApiNodemeta
{
public:
- ScriptApiNode();
- virtual ~ScriptApiNode();
+ ScriptApiNode() = default;
+ virtual ~ScriptApiNode() = default;
bool node_on_punch(v3s16 p, MapNode node,
ServerActiveObject *puncher, PointedThing pointed);
diff --git a/src/script/cpp_api/s_nodemeta.cpp b/src/script/cpp_api/s_nodemeta.cpp
index d050c0bc9..2e6d3a373 100644
--- a/src/script/cpp_api/s_nodemeta.cpp
+++ b/src/script/cpp_api/s_nodemeta.cpp
@@ -232,12 +232,3 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
PCALL_RES(lua_pcall(L, 5, 0, error_handler));
lua_pop(L, 1); // Pop error handler
}
-
-ScriptApiNodemeta::ScriptApiNodemeta()
-{
-}
-
-ScriptApiNodemeta::~ScriptApiNodemeta()
-{
-}
-
diff --git a/src/script/cpp_api/s_nodemeta.h b/src/script/cpp_api/s_nodemeta.h
index 51b8a5eb2..4d3257dcf 100644
--- a/src/script/cpp_api/s_nodemeta.h
+++ b/src/script/cpp_api/s_nodemeta.h
@@ -30,8 +30,8 @@ class ScriptApiNodemeta
public ScriptApiItem
{
public:
- ScriptApiNodemeta();
- virtual ~ScriptApiNodemeta();
+ ScriptApiNodemeta() = default;
+ virtual ~ScriptApiNodemeta() = default;
// Return number of accepted items to be moved
int nodemeta_inventory_AllowMove(v3s16 p,
diff --git a/src/script/cpp_api/s_player.cpp b/src/script/cpp_api/s_player.cpp
index a8c07476c..b7f2f10f9 100644
--- a/src/script/cpp_api/s_player.cpp
+++ b/src/script/cpp_api/s_player.cpp
@@ -192,8 +192,3 @@ void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
}
-ScriptApiPlayer::~ScriptApiPlayer()
-{
-}
-
-
diff --git a/src/script/cpp_api/s_player.h b/src/script/cpp_api/s_player.h
index 70b06bfc7..faf394de5 100644
--- a/src/script/cpp_api/s_player.h
+++ b/src/script/cpp_api/s_player.h
@@ -28,7 +28,7 @@ struct ToolCapabilities;
class ScriptApiPlayer : virtual public ScriptApiBase
{
public:
- virtual ~ScriptApiPlayer();
+ virtual ~ScriptApiPlayer() = default;
void on_newplayer(ServerActiveObject *player);
void on_dieplayer(ServerActiveObject *player);
diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp
index 761597701..690a3b47f 100644
--- a/src/script/cpp_api/s_security.cpp
+++ b/src/script/cpp_api/s_security.cpp
@@ -260,7 +260,7 @@ void ScriptApiSecurity::initializeSecurityClient()
static const char *os_whitelist[] = {
"clock",
"date",
- "difftime",
+ "difftime",
"time",
"setlocale",
};
@@ -504,7 +504,7 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path,
// by the operating system anyways.
return false;
}
- removed = component + (removed.empty() ? "" : DIR_DELIM + removed);
+ removed.append(component).append(removed.empty() ? "" : DIR_DELIM + removed);
abs_path = fs::AbsolutePath(cur_path);
}
if (abs_path.empty())
@@ -550,9 +550,9 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path,
// Allow read-only access to all mod directories
if (!write_required) {
- const std::vector<ModSpec> mods = gamedef->getMods();
- for (size_t i = 0; i < mods.size(); ++i) {
- str = fs::AbsolutePath(mods[i].path);
+ const std::vector<ModSpec> &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;