1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

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
This commit is contained in:
Loïc Blot 2017-08-20 13:30:50 +02:00 committed by GitHub
parent 50669cd282
commit 1c1c97cbd1
72 changed files with 446 additions and 584 deletions

View file

@ -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();

View file

@ -68,7 +68,7 @@ class AsyncEngine {
friend class AsyncWorkerThread;
typedef void (*StateInitializer)(lua_State *L, int top);
public:
AsyncEngine() {};
AsyncEngine() = default;
~AsyncEngine();
/**

View file

@ -40,7 +40,7 @@ extern "C" {
#endif
}
#include <stdio.h>
#include <cstdio>
#include <cstdarg>
#include "script/common/c_content.h"
#include <sstream>

View file

@ -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__)

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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,

View file

@ -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);

View file

@ -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()
{
}

View file

@ -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,

View file

@ -192,8 +192,3 @@ void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
}
ScriptApiPlayer::~ScriptApiPlayer()
{
}

View file

@ -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);

View file

@ -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;