Skip to content

Commit c427533

Browse files
authoredAug 18, 2017
Modernize various files (src/m*) (#6267)
* Modernize various files (src/m*) * range-based for loops * code style * C++ headers instead of C headers * Default operators * empty function Thanks to clang-tidy
1 parent fb196be commit c427533

34 files changed

+254
-355
lines changed
 

Diff for: ‎src/main.cpp

+35-35
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ int main(int argc, char *argv[])
211211
#endif
212212

213213
// Update configuration file
214-
if (g_settings_path != "")
214+
if (!g_settings_path.empty())
215215
g_settings->updateConfigFile(g_settings_path.c_str());
216216

217217
print_modified_quicktune_values();
@@ -306,17 +306,16 @@ static void print_help(const OptionList &allowed_options)
306306

307307
static void print_allowed_options(const OptionList &allowed_options)
308308
{
309-
for (OptionList::const_iterator i = allowed_options.begin();
310-
i != allowed_options.end(); ++i) {
309+
for (const auto &allowed_option : allowed_options) {
311310
std::ostringstream os1(std::ios::binary);
312-
os1 << " --" << i->first;
313-
if (i->second.type != VALUETYPE_FLAG)
311+
os1 << " --" << allowed_option.first;
312+
if (allowed_option.second.type != VALUETYPE_FLAG)
314313
os1 << _(" <value>");
315314

316315
std::cout << padStringRight(os1.str(), 30);
317316

318-
if (i->second.help)
319-
std::cout << i->second.help;
317+
if (allowed_option.second.help)
318+
std::cout << allowed_option.second.help;
320319

321320
std::cout << std::endl;
322321
}
@@ -335,9 +334,8 @@ static void print_version()
335334
static void list_game_ids()
336335
{
337336
std::set<std::string> gameids = getAvailableGameIds();
338-
for (std::set<std::string>::const_iterator i = gameids.begin();
339-
i != gameids.end(); ++i)
340-
std::cout << (*i) <<std::endl;
337+
for (const std::string &gameid : gameids)
338+
std::cout << gameid <<std::endl;
341339
}
342340

343341
static void list_worlds()
@@ -350,10 +348,10 @@ static void list_worlds()
350348
static void print_worldspecs(const std::vector<WorldSpec> &worldspecs,
351349
std::ostream &os)
352350
{
353-
for (size_t i = 0; i < worldspecs.size(); i++) {
354-
std::string name = worldspecs[i].name;
355-
std::string path = worldspecs[i].path;
356-
if (name.find(" ") != std::string::npos)
351+
for (const WorldSpec &worldspec : worldspecs) {
352+
std::string name = worldspec.name;
353+
std::string path = worldspec.path;
354+
if (name.find(' ') != std::string::npos)
357355
name = std::string("'") + name + "'";
358356
path = std::string("'") + path + "'";
359357
name = padStringRight(name, 14);
@@ -366,15 +364,15 @@ static void print_modified_quicktune_values()
366364
bool header_printed = false;
367365
std::vector<std::string> names = getQuicktuneNames();
368366

369-
for (u32 i = 0; i < names.size(); i++) {
370-
QuicktuneValue val = getQuicktuneValue(names[i]);
367+
for (const std::string &name : names) {
368+
QuicktuneValue val = getQuicktuneValue(name);
371369
if (!val.modified)
372370
continue;
373371
if (!header_printed) {
374372
dstream << "Modified quicktune values:" << std::endl;
375373
header_printed = true;
376374
}
377-
dstream << names[i] << " = " << val.getString() << std::endl;
375+
dstream << name << " = " << val.getString() << std::endl;
378376
}
379377
}
380378

@@ -485,16 +483,16 @@ static bool read_config_file(const Settings &cmd_args)
485483
DIR_DELIM + ".." + DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
486484
#endif
487485

488-
for (size_t i = 0; i < filenames.size(); i++) {
489-
bool r = g_settings->readConfigFile(filenames[i].c_str());
486+
for (const std::string &filename : filenames) {
487+
bool r = g_settings->readConfigFile(filename.c_str());
490488
if (r) {
491-
g_settings_path = filenames[i];
489+
g_settings_path = filename;
492490
break;
493491
}
494492
}
495493

496494
// If no path found, use the first one (menu creates the file)
497-
if (g_settings_path == "")
495+
if (g_settings_path.empty())
498496
g_settings_path = filenames[0];
499497
}
500498

@@ -540,7 +538,7 @@ static void init_log_streams(const Settings &cmd_args)
540538

541539
verbosestream << "log_filename = " << log_filename << std::endl;
542540

543-
file_log_output.open(log_filename.c_str());
541+
file_log_output.open(log_filename);
544542
g_logger.addOutputMaxLevel(&file_log_output, log_level);
545543
}
546544

@@ -573,6 +571,7 @@ static bool game_configure_world(GameParams *game_params, const Settings &cmd_ar
573571
{
574572
if (get_world_from_cmdline(game_params, cmd_args))
575573
return true;
574+
576575
if (get_world_from_config(game_params, cmd_args))
577576
return true;
578577

@@ -581,24 +580,24 @@ static bool game_configure_world(GameParams *game_params, const Settings &cmd_ar
581580

582581
static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_args)
583582
{
584-
std::string commanded_world = "";
583+
std::string commanded_world;
585584

586585
// World name
587-
std::string commanded_worldname = "";
586+
std::string commanded_worldname;
588587
if (cmd_args.exists("worldname"))
589588
commanded_worldname = cmd_args.get("worldname");
590589

591590
// If a world name was specified, convert it to a path
592-
if (commanded_worldname != "") {
591+
if (!commanded_worldname.empty()) {
593592
// Get information about available worlds
594593
std::vector<WorldSpec> worldspecs = getAvailableWorlds();
595594
bool found = false;
596-
for (u32 i = 0; i < worldspecs.size(); i++) {
597-
std::string name = worldspecs[i].name;
595+
for (const WorldSpec &worldspec : worldspecs) {
596+
std::string name = worldspec.name;
598597
if (name == commanded_worldname) {
599598
dstream << _("Using world specified by --worldname on the "
600599
"command line") << std::endl;
601-
commanded_world = worldspecs[i].path;
600+
commanded_world = worldspec.path;
602601
found = true;
603602
break;
604603
}
@@ -611,7 +610,7 @@ static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_
611610
}
612611

613612
game_params->world_path = get_clean_world_path(commanded_world);
614-
return commanded_world != "";
613+
return !commanded_world.empty();
615614
}
616615

617616
if (cmd_args.exists("world"))
@@ -622,20 +621,20 @@ static bool get_world_from_cmdline(GameParams *game_params, const Settings &cmd_
622621
commanded_world = cmd_args.get("nonopt0");
623622

624623
game_params->world_path = get_clean_world_path(commanded_world);
625-
return commanded_world != "";
624+
return !commanded_world.empty();
626625
}
627626

628627
static bool get_world_from_config(GameParams *game_params, const Settings &cmd_args)
629628
{
630629
// World directory
631-
std::string commanded_world = "";
630+
std::string commanded_world;
632631

633632
if (g_settings->exists("map-dir"))
634633
commanded_world = g_settings->get("map-dir");
635634

636635
game_params->world_path = get_clean_world_path(commanded_world);
637636

638-
return commanded_world != "";
637+
return !commanded_world.empty();
639638
}
640639

641640
static bool auto_select_world(GameParams *game_params)
@@ -729,8 +728,8 @@ static bool determine_subgame(GameParams *game_params)
729728

730729
verbosestream << _("Determining gameid/gamespec") << std::endl;
731730
// If world doesn't exist
732-
if (game_params->world_path != ""
733-
&& !getWorldExists(game_params->world_path)) {
731+
if (!game_params->world_path.empty()
732+
&& !getWorldExists(game_params->world_path)) {
734733
// Try to take gamespec from command line
735734
if (game_params->game_spec.isValid()) {
736735
gamespec = game_params->game_spec;
@@ -810,7 +809,8 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
810809
// Database migration
811810
if (cmd_args.exists("migrate"))
812811
return migrate_map_database(game_params, cmd_args);
813-
else if (cmd_args.exists("migrate-players"))
812+
813+
if (cmd_args.exists("migrate-players"))
814814
return ServerEnvironment::migratePlayersDatabase(game_params, cmd_args);
815815

816816
if (cmd_args.exists("terminal")) {

Diff for: ‎src/mainmenumanager.h

+6-11
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@ class MainMenuManager : public IMenuManager
4848
public:
4949
virtual void createdMenu(gui::IGUIElement *menu)
5050
{
51-
for(std::list<gui::IGUIElement*>::iterator
52-
i = m_stack.begin();
53-
i != m_stack.end(); ++i)
54-
{
55-
assert(*i != menu);
51+
for (gui::IGUIElement *i : m_stack) {
52+
assert(i != menu);
5653
}
5754

5855
if(!m_stack.empty())
@@ -103,10 +100,8 @@ class MainMenuManager : public IMenuManager
103100

104101
bool pausesGame()
105102
{
106-
for(std::list<gui::IGUIElement*>::iterator
107-
i = m_stack.begin(); i != m_stack.end(); ++i)
108-
{
109-
GUIModalMenu *mm = dynamic_cast<GUIModalMenu*>(*i);
103+
for (gui::IGUIElement *i : m_stack) {
104+
GUIModalMenu *mm = dynamic_cast<GUIModalMenu*>(i);
110105
if (mm && mm->pausesGame())
111106
return true;
112107
}
@@ -123,8 +118,8 @@ extern bool isMenuActive();
123118
class MainGameCallback : public IGameCallback
124119
{
125120
public:
126-
MainGameCallback() {}
127-
virtual ~MainGameCallback() {}
121+
MainGameCallback() = default;
122+
virtual ~MainGameCallback() = default;
128123

129124
virtual void exitToOS()
130125
{

Diff for: ‎src/map.cpp

+10-13
Original file line numberDiff line numberDiff line change
@@ -527,14 +527,13 @@ void Map::unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks)
527527

528528
void Map::deleteSectors(std::vector<v2s16> &sectorList)
529529
{
530-
for(std::vector<v2s16>::iterator j = sectorList.begin();
531-
j != sectorList.end(); ++j) {
532-
MapSector *sector = m_sectors[*j];
530+
for (v2s16 j : sectorList) {
531+
MapSector *sector = m_sectors[j];
533532
// If sector is in sector cache, remove it from there
534533
if(m_sector_cache == sector)
535534
m_sector_cache = NULL;
536535
// Remove from map and delete
537-
m_sectors.erase(*j);
536+
m_sectors.erase(j);
538537
delete sector;
539538
}
540539
}
@@ -1724,7 +1723,7 @@ MapBlock * ServerMap::emergeBlock(v3s16 p, bool create_blank)
17241723

17251724
{
17261725
MapBlock *block = getBlockNoCreateNoEx(p);
1727-
if(block && block->isDummy() == false)
1726+
if (block && !block->isDummy())
17281727
return block;
17291728
}
17301729

@@ -1855,8 +1854,7 @@ bool ServerMap::loadFromFolders() {
18551854

18561855
void ServerMap::createDirs(std::string path)
18571856
{
1858-
if(fs::CreateAllDirs(path) == false)
1859-
{
1857+
if (!fs::CreateAllDirs(path)) {
18601858
m_dout<<"ServerMap: Failed to create directory "
18611859
<<"\""<<path<<"\""<<std::endl;
18621860
throw BaseException("ServerMap failed to create directory");
@@ -1940,7 +1938,7 @@ std::string ServerMap::getBlockFilename(v3s16 p)
19401938
void ServerMap::save(ModifiedState save_level)
19411939
{
19421940
DSTACK(FUNCTION_NAME);
1943-
if(m_map_saving_enabled == false) {
1941+
if (!m_map_saving_enabled) {
19441942
warningstream<<"Not saving map, saving disabled."<<std::endl;
19451943
return;
19461944
}
@@ -2081,8 +2079,7 @@ MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load
20812079

20822080
std::string fullpath = sectordir + DIR_DELIM + "meta";
20832081
std::ifstream is(fullpath.c_str(), std::ios_base::binary);
2084-
if(is.good() == false)
2085-
{
2082+
if (!is.good()) {
20862083
// If the directory exists anyway, it probably is in some old
20872084
// format. Just go ahead and create the sector.
20882085
if(fs::PathExists(sectordir))
@@ -2494,7 +2491,7 @@ MapBlock* ServerMap::loadBlock(v3s16 blockpos)
24942491
*/
24952492

24962493
std::string blockfilename = getBlockFilename(blockpos);
2497-
if (fs::PathExists(sectordir + DIR_DELIM + blockfilename) == false)
2494+
if (!fs::PathExists(sectordir + DIR_DELIM + blockfilename))
24982495
return NULL;
24992496

25002497
/*
@@ -2667,8 +2664,8 @@ void MMVManip::blitBackAll(std::map<v3s16, MapBlock*> *modified_blocks,
26672664
v3s16 p = i->first;
26682665
MapBlock *block = m_map->getBlockNoCreateNoEx(p);
26692666
bool existed = !(i->second & VMANIP_BLOCK_DATA_INEXIST);
2670-
if ((existed == false) || (block == NULL) ||
2671-
(overwrite_generated == false && block->isGenerated() == true))
2667+
if (!existed || (block == NULL) ||
2668+
(!overwrite_generated && block->isGenerated()))
26722669
continue;
26732670

26742671
block->copyFrom(*this);

Diff for: ‎src/map.h

+4-8
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct MapEditEvent
7777
std::set<v3s16> modified_blocks;
7878
u16 already_known_by_peer = 0;
7979

80-
MapEditEvent() {}
80+
MapEditEvent() = default;
8181

8282
MapEditEvent * clone()
8383
{
@@ -107,11 +107,7 @@ struct MapEditEvent
107107
case MEET_OTHER:
108108
{
109109
VoxelArea a;
110-
for(std::set<v3s16>::iterator
111-
i = modified_blocks.begin();
112-
i != modified_blocks.end(); ++i)
113-
{
114-
v3s16 p = *i;
110+
for (v3s16 p : modified_blocks) {
115111
v3s16 np1 = p*MAP_BLOCKSIZE;
116112
v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
117113
a.addPoint(np1);
@@ -216,8 +212,8 @@ class Map /*: public NodeContainer*/
216212
bool removeNodeWithEvent(v3s16 p);
217213

218214
// Call these before and after saving of many blocks
219-
virtual void beginSave() { return; }
220-
virtual void endSave() { return; }
215+
virtual void beginSave() {}
216+
virtual void endSave() {}
221217

222218
virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
223219

0 commit comments

Comments
 (0)
Please sign in to comment.