Skip to content

Commit 951f120

Browse files
committedAug 18, 2017
Modernize various files (src/k*, src/l*)
* range-based for loops * code style * C++ headers instead of C headers * Default operators
1 parent 1d086ae commit 951f120

File tree

6 files changed

+25
-32
lines changed

6 files changed

+25
-32
lines changed
 

‎src/keycode.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -246,19 +246,19 @@ static const struct table_key table[] = {
246246

247247
struct table_key lookup_keyname(const char *name)
248248
{
249-
for (u16 i = 0; i < ARRLEN(table); i++) {
250-
if (strcmp(table[i].Name, name) == 0)
251-
return table[i];
249+
for (const auto &table_key : table) {
250+
if (strcmp(table_key.Name, name) == 0)
251+
return table_key;
252252
}
253253

254254
throw UnknownKeycode(name);
255255
}
256256

257257
struct table_key lookup_keykey(irr::EKEY_CODE key)
258258
{
259-
for (u16 i = 0; i < ARRLEN(table); i++) {
260-
if (table[i].Key == key)
261-
return table[i];
259+
for (const auto &table_key : table) {
260+
if (table_key.Key == key)
261+
return table_key;
262262
}
263263

264264
std::ostringstream os;
@@ -268,9 +268,9 @@ struct table_key lookup_keykey(irr::EKEY_CODE key)
268268

269269
struct table_key lookup_keychar(wchar_t Char)
270270
{
271-
for (u16 i = 0; i < ARRLEN(table); i++) {
272-
if (table[i].Char == Char)
273-
return table[i];
271+
for (const auto &table_key : table) {
272+
if (table_key.Char == Char)
273+
return table_key;
274274
}
275275

276276
std::ostringstream os;
@@ -285,7 +285,9 @@ KeyPress::KeyPress(const char *name)
285285
Char = L'\0';
286286
m_name = "";
287287
return;
288-
} else if (strlen(name) <= 4) {
288+
}
289+
290+
if (strlen(name) <= 4) {
289291
// Lookup by resulting character
290292
int chars_read = mbtowc(&Char, name, 1);
291293
FATAL_ERROR_IF(chars_read != 1, "Unexpected multibyte character");
@@ -339,7 +341,7 @@ const char *KeyPress::sym() const
339341

340342
const char *KeyPress::name() const
341343
{
342-
if (m_name == "")
344+
if (m_name.empty())
343345
return "";
344346
const char *ret;
345347
if (valid_kcode(Key))

‎src/keycode.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3030
class KeyPress
3131
{
3232
public:
33-
KeyPress() {}
33+
KeyPress() = default;
34+
3435
KeyPress(const char *name);
3536

3637
KeyPress(const irr::SEvent::SKeyInput &in, bool prefer_character = false);

‎src/light.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1818
*/
1919

2020
#include "light.h"
21-
#include <math.h>
21+
#include <cmath>
2222
#include "util/numeric.h"
2323
#include "settings.h"
2424

‎src/localplayer.cpp

+8-16
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,9 @@ LocalPlayer::LocalPlayer(Client *client, const char *name):
3838
{
3939
}
4040

41-
LocalPlayer::~LocalPlayer()
42-
{
43-
}
44-
4541
static aabb3f getNodeBoundingBox(const std::vector<aabb3f> &nodeboxes)
4642
{
47-
if (nodeboxes.size() == 0)
43+
if (nodeboxes.empty())
4844
return aabb3f(0, 0, 0, 0, 0, 0);
4945

5046
aabb3f b_max;
@@ -103,8 +99,8 @@ bool LocalPlayer::updateSneakNode(Map *map, const v3f &position,
10399
m_sneak_ladder_detected = false;
104100
f32 min_distance_f = 100000.0 * BS;
105101

106-
for (s16 d = 0; d < 9; d++) {
107-
const v3s16 p = current_node + dir9_center[d];
102+
for (const auto &d : dir9_center) {
103+
const v3s16 p = current_node + d;
108104
const v3f pf = intToFloat(p, BS);
109105
const v2f diff(position.X - pf.X, position.Z - pf.Z);
110106
f32 distance_f = diff.getLength();
@@ -389,9 +385,8 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
389385

390386
// Dont report if flying
391387
if(collision_info && !(g_settings->getBool("free_move") && fly_allowed)) {
392-
for(size_t i=0; i<result.collisions.size(); i++) {
393-
const CollisionInfo &info = result.collisions[i];
394-
collision_info->push_back(info);
388+
for (const auto &colinfo : result.collisions) {
389+
collision_info->push_back(colinfo);
395390
}
396391
}
397392

@@ -938,7 +933,7 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
938933

939934
// The node to be sneaked on has to be walkable
940935
node = map->getNodeNoEx(p, &is_valid_position);
941-
if (!is_valid_position || nodemgr->get(node).walkable == false)
936+
if (!is_valid_position || !nodemgr->get(node).walkable)
942937
continue;
943938
// And the node above it has to be nonwalkable
944939
node = map->getNodeNoEx(p + v3s16(0, 1, 0), &is_valid_position);
@@ -965,9 +960,7 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
965960
MapNode n = map->getNodeNoEx(m_sneak_node);
966961
std::vector<aabb3f> nodeboxes;
967962
n.getCollisionBoxes(nodemgr, &nodeboxes);
968-
for (std::vector<aabb3f>::iterator it = nodeboxes.begin();
969-
it != nodeboxes.end(); ++it) {
970-
aabb3f box = *it;
963+
for (const auto &box : nodeboxes) {
971964
if (box.MaxEdge.Y > cb_max)
972965
cb_max = box.MaxEdge.Y;
973966
}
@@ -994,8 +987,7 @@ void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
994987
*/
995988
// Dont report if flying
996989
if (collision_info && !(g_settings->getBool("free_move") && fly_allowed)) {
997-
for (size_t i = 0; i < result.collisions.size(); i++) {
998-
const CollisionInfo &info = result.collisions[i];
990+
for (const auto &info : result.collisions) {
999991
collision_info->push_back(info);
1000992
}
1001993
}

‎src/localplayer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class LocalPlayer : public Player
4343
{
4444
public:
4545
LocalPlayer(Client *client, const char *name);
46-
virtual ~LocalPlayer();
46+
virtual ~LocalPlayer() = default;
4747

4848
ClientActiveObject *parent = nullptr;
4949

‎src/log.h

-2
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,7 @@ extern std::ostream dstream;
205205
#define dout_con (*dout_con_ptr)
206206
#define derr_con (*derr_con_ptr)
207207
#define dout_server (*dout_server_ptr)
208-
#define derr_server (*derr_server_ptr)
209208

210209
#ifndef SERVER
211210
#define dout_client (*dout_client_ptr)
212-
#define derr_client (*derr_client_ptr)
213211
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.