Skip to content

Commit 55ab426

Browse files
committedAug 18, 2017
Modernize various files
* range-based for loops * emplace_back instead of push_back * code style * C++ headers instead of C headers * Default operators * empty stl function
1 parent 13e995b commit 55ab426

12 files changed

+100
-118
lines changed
 

Diff for: ‎src/face_position_cache.cpp

+37-37
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,39 @@ const std::vector<v3s16> &FacePositionCache::generateFacePosition(u16 d)
4040
cache[d] = std::vector<v3s16>();
4141
std::vector<v3s16> &c = cache[d];
4242
if (d == 0) {
43-
c.push_back(v3s16(0,0,0));
43+
c.emplace_back(0,0,0);
4444
return c;
4545
}
4646
if (d == 1) {
4747
// This is an optimized sequence of coordinates.
48-
c.push_back(v3s16( 0, 1, 0)); // Top
49-
c.push_back(v3s16( 0, 0, 1)); // Back
50-
c.push_back(v3s16(-1, 0, 0)); // Left
51-
c.push_back(v3s16( 1, 0, 0)); // Right
52-
c.push_back(v3s16( 0, 0,-1)); // Front
53-
c.push_back(v3s16( 0,-1, 0)); // Bottom
48+
c.emplace_back(0, 1, 0); // Top
49+
c.emplace_back(0, 0, 1); // Back
50+
c.emplace_back(-1, 0, 0); // Left
51+
c.emplace_back(1, 0, 0); // Right
52+
c.emplace_back(0, 0,-1); // Front
53+
c.emplace_back(0,-1, 0); // Bottom
5454
// 6
55-
c.push_back(v3s16(-1, 0, 1)); // Back left
56-
c.push_back(v3s16( 1, 0, 1)); // Back right
57-
c.push_back(v3s16(-1, 0,-1)); // Front left
58-
c.push_back(v3s16( 1, 0,-1)); // Front right
59-
c.push_back(v3s16(-1,-1, 0)); // Bottom left
60-
c.push_back(v3s16( 1,-1, 0)); // Bottom right
61-
c.push_back(v3s16( 0,-1, 1)); // Bottom back
62-
c.push_back(v3s16( 0,-1,-1)); // Bottom front
63-
c.push_back(v3s16(-1, 1, 0)); // Top left
64-
c.push_back(v3s16( 1, 1, 0)); // Top right
65-
c.push_back(v3s16( 0, 1, 1)); // Top back
66-
c.push_back(v3s16( 0, 1,-1)); // Top front
55+
c.emplace_back(-1, 0, 1); // Back left
56+
c.emplace_back(1, 0, 1); // Back right
57+
c.emplace_back(-1, 0,-1); // Front left
58+
c.emplace_back(1, 0,-1); // Front right
59+
c.emplace_back(-1,-1, 0); // Bottom left
60+
c.emplace_back(1,-1, 0); // Bottom right
61+
c.emplace_back(0,-1, 1); // Bottom back
62+
c.emplace_back(0,-1,-1); // Bottom front
63+
c.emplace_back(-1, 1, 0); // Top left
64+
c.emplace_back(1, 1, 0); // Top right
65+
c.emplace_back(0, 1, 1); // Top back
66+
c.emplace_back(0, 1,-1); // Top front
6767
// 18
68-
c.push_back(v3s16(-1, 1, 1)); // Top back-left
69-
c.push_back(v3s16( 1, 1, 1)); // Top back-right
70-
c.push_back(v3s16(-1, 1,-1)); // Top front-left
71-
c.push_back(v3s16( 1, 1,-1)); // Top front-right
72-
c.push_back(v3s16(-1,-1, 1)); // Bottom back-left
73-
c.push_back(v3s16( 1,-1, 1)); // Bottom back-right
74-
c.push_back(v3s16(-1,-1,-1)); // Bottom front-left
75-
c.push_back(v3s16( 1,-1,-1)); // Bottom front-right
68+
c.emplace_back(-1, 1, 1); // Top back-left
69+
c.emplace_back(1, 1, 1); // Top back-right
70+
c.emplace_back(-1, 1,-1); // Top front-left
71+
c.emplace_back(1, 1,-1); // Top front-right
72+
c.emplace_back(-1,-1, 1); // Bottom back-left
73+
c.emplace_back(1,-1, 1); // Bottom back-right
74+
c.emplace_back(-1,-1,-1); // Bottom front-left
75+
c.emplace_back(1,-1,-1); // Bottom front-right
7676
// 26
7777
return c;
7878
}
@@ -81,20 +81,20 @@ const std::vector<v3s16> &FacePositionCache::generateFacePosition(u16 d)
8181
for (s16 y = 0; y <= d - 1; y++) {
8282
// Left and right side, including borders
8383
for (s16 z =- d; z <= d; z++) {
84-
c.push_back(v3s16( d, y, z));
85-
c.push_back(v3s16(-d, y, z));
84+
c.emplace_back(d, y, z);
85+
c.emplace_back(-d, y, z);
8686
if (y != 0) {
87-
c.push_back(v3s16( d, -y, z));
88-
c.push_back(v3s16(-d, -y, z));
87+
c.emplace_back(d, -y, z);
88+
c.emplace_back(-d, -y, z);
8989
}
9090
}
9191
// Back and front side, excluding borders
9292
for (s16 x = -d + 1; x <= d - 1; x++) {
93-
c.push_back(v3s16(x, y, d));
94-
c.push_back(v3s16(x, y, -d));
93+
c.emplace_back(x, y, d);
94+
c.emplace_back(x, y, -d);
9595
if (y != 0) {
96-
c.push_back(v3s16(x, -y, d));
97-
c.push_back(v3s16(x, -y, -d));
96+
c.emplace_back(x, -y, d);
97+
c.emplace_back(x, -y, -d);
9898
}
9999
}
100100
}
@@ -103,8 +103,8 @@ const std::vector<v3s16> &FacePositionCache::generateFacePosition(u16 d)
103103
// -d < x < d, y = +-d, -d < z < d
104104
for (s16 x = -d; x <= d; x++)
105105
for (s16 z = -d; z <= d; z++) {
106-
c.push_back(v3s16(x, -d, z));
107-
c.push_back(v3s16(x, d, z));
106+
c.emplace_back(x, -d, z);
107+
c.emplace_back(x, d, z);
108108
}
109109
return c;
110110
}

Diff for: ‎src/filecache.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626
#include <string>
2727
#include <iostream>
2828
#include <fstream>
29-
#include <stdlib.h>
29+
#include <cstdlib>
3030

3131
bool FileCache::loadByPath(const std::string &path, std::ostream &os)
3232
{

Diff for: ‎src/filesys.cpp

+35-41
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#include "filesys.h"
2121
#include "util/string.h"
2222
#include <iostream>
23-
#include <stdio.h>
24-
#include <string.h>
25-
#include <errno.h>
23+
#include <cstdio>
24+
#include <cstring>
25+
#include <cerrno>
2626
#include <fstream>
2727
#include "log.h"
2828
#include "config.h"
@@ -246,7 +246,7 @@ std::vector<DirListNode> GetDirListing(const std::string &pathstring)
246246
If so, try stat().
247247
*/
248248
if(isdir == -1) {
249-
struct stat statbuf;
249+
struct stat statbuf{};
250250
if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
251251
continue;
252252
isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
@@ -262,22 +262,20 @@ std::vector<DirListNode> GetDirListing(const std::string &pathstring)
262262
bool CreateDir(const std::string &path)
263263
{
264264
int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
265-
if(r == 0)
266-
{
265+
if (r == 0) {
267266
return true;
268267
}
269-
else
270-
{
271-
// If already exists, return true
272-
if(errno == EEXIST)
273-
return true;
274-
return false;
275-
}
268+
269+
// If already exists, return true
270+
if (errno == EEXIST)
271+
return true;
272+
return false;
273+
276274
}
277275

278276
bool PathExists(const std::string &path)
279277
{
280-
struct stat st;
278+
struct stat st{};
281279
return (stat(path.c_str(),&st) == 0);
282280
}
283281

@@ -288,7 +286,7 @@ bool IsPathAbsolute(const std::string &path)
288286

289287
bool IsDir(const std::string &path)
290288
{
291-
struct stat statbuf;
289+
struct stat statbuf{};
292290
if(stat(path.c_str(), &statbuf))
293291
return false; // Actually error; but certainly not a directory
294292
return ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
@@ -347,19 +345,19 @@ bool RecursiveDelete(const std::string &path)
347345

348346
bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
349347
{
350-
if(IsDir(path)){
348+
if (IsDir(path)) {
351349
bool did = (rmdir(path.c_str()) == 0);
352-
if(!did)
353-
errorstream<<"rmdir errno: "<<errno<<": "<<strerror(errno)
354-
<<std::endl;
355-
return did;
356-
} else {
357-
bool did = (unlink(path.c_str()) == 0);
358-
if(!did)
359-
errorstream<<"unlink errno: "<<errno<<": "<<strerror(errno)
360-
<<std::endl;
350+
if (!did)
351+
errorstream << "rmdir errno: " << errno << ": " << strerror(errno)
352+
<< std::endl;
361353
return did;
362354
}
355+
356+
bool did = (unlink(path.c_str()) == 0);
357+
if (!did)
358+
errorstream << "unlink errno: " << errno << ": " << strerror(errno)
359+
<< std::endl;
360+
return did;
363361
}
364362

365363
std::string TempPath()
@@ -385,8 +383,7 @@ std::string TempPath()
385383
void GetRecursiveSubPaths(const std::string &path, std::vector<std::string> &dst)
386384
{
387385
std::vector<DirListNode> content = GetDirListing(path);
388-
for(unsigned int i=0; i<content.size(); i++){
389-
const DirListNode &n = content[i];
386+
for (const auto &n : content) {
390387
std::string fullpath = path + DIR_DELIM + n.name;
391388
dst.push_back(fullpath);
392389
if (n.dir) {
@@ -414,15 +411,13 @@ bool RecursiveDeleteContent(const std::string &path)
414411
{
415412
infostream<<"Removing content of \""<<path<<"\""<<std::endl;
416413
std::vector<DirListNode> list = GetDirListing(path);
417-
for(unsigned int i=0; i<list.size(); i++)
418-
{
419-
if(trim(list[i].name) == "." || trim(list[i].name) == "..")
414+
for (const DirListNode &dln : list) {
415+
if(trim(dln.name) == "." || trim(dln.name) == "..")
420416
continue;
421-
std::string childpath = path + DIR_DELIM + list[i].name;
417+
std::string childpath = path + DIR_DELIM + dln.name;
422418
bool r = RecursiveDelete(childpath);
423-
if(r == false)
424-
{
425-
errorstream<<"Removing \""<<childpath<<"\" failed"<<std::endl;
419+
if(!r) {
420+
errorstream << "Removing \"" << childpath << "\" failed" << std::endl;
426421
return false;
427422
}
428423
}
@@ -510,10 +505,10 @@ bool CopyDir(const std::string &source, const std::string &target)
510505
bool retval = true;
511506
std::vector<DirListNode> content = fs::GetDirListing(source);
512507

513-
for(unsigned int i=0; i < content.size(); i++){
514-
std::string sourcechild = source + DIR_DELIM + content[i].name;
515-
std::string targetchild = target + DIR_DELIM + content[i].name;
516-
if(content[i].dir){
508+
for (const auto &dln : content) {
509+
std::string sourcechild = source + DIR_DELIM + dln.name;
510+
std::string targetchild = target + DIR_DELIM + dln.name;
511+
if(dln.dir){
517512
if(!fs::CopyDir(sourcechild, targetchild)){
518513
retval = false;
519514
}
@@ -526,9 +521,8 @@ bool CopyDir(const std::string &source, const std::string &target)
526521
}
527522
return retval;
528523
}
529-
else {
530-
return false;
531-
}
524+
525+
return false;
532526
}
533527

534528
bool PathStartsWith(const std::string &path, const std::string &prefix)

Diff for: ‎src/filesys.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626
#ifdef _WIN32 // WINDOWS
2727
#define DIR_DELIM "\\"
2828
#define DIR_DELIM_CHAR '\\'
29-
#define FILESYS_CASE_INSENSITIVE 1
29+
#define FILESYS_CASE_INSENSITIVE true
3030
#define PATH_DELIM ";"
3131
#else // POSIX
3232
#define DIR_DELIM "/"
3333
#define DIR_DELIM_CHAR '/'
34-
#define FILESYS_CASE_INSENSITIVE 0
34+
#define FILESYS_CASE_INSENSITIVE false
3535
#define PATH_DELIM ":"
3636
#endif
3737

Diff for: ‎src/fontengine.h

+7-16
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2121

2222
#include <map>
2323
#include <vector>
24-
#include "IGUIFont.h"
25-
#include "IGUISkin.h"
26-
#include "IGUIEnvironment.h"
24+
#include "util/basic_macros.h"
25+
#include <IGUIFont.h>
26+
#include <IGUISkin.h>
27+
#include <IGUIEnvironment.h>
2728
#include "settings.h"
2829

2930
#define FONT_SIZE_UNSPECIFIED 0xFFFFFFFF
@@ -81,17 +82,6 @@ class FontEngine
8182
void readSettings();
8283

8384
private:
84-
/** disable copy constructor */
85-
FontEngine() :
86-
m_settings(NULL),
87-
m_env(NULL),
88-
m_font_cache(),
89-
m_currentMode(FM_Standard),
90-
m_lastMode(),
91-
m_lastSize(0),
92-
m_lastFont(NULL)
93-
{};
94-
9585
/** update content of font cache in case of a setting change made it invalid */
9686
void updateFontCache();
9787

@@ -108,10 +98,10 @@ class FontEngine
10898
void cleanCache();
10999

110100
/** pointer to settings for registering callbacks or reading config */
111-
Settings* m_settings;
101+
Settings* m_settings = nullptr;
112102

113103
/** pointer to irrlicht gui environment */
114-
gui::IGUIEnvironment* m_env;
104+
gui::IGUIEnvironment* m_env = nullptr;
115105

116106
/** internal storage for caching fonts of different size */
117107
std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FM_MaxMode];
@@ -131,6 +121,7 @@ class FontEngine
131121
/** last font returned */
132122
irr::gui::IGUIFont* m_lastFont = nullptr;
133123

124+
DISABLE_CLASS_COPY(FontEngine);
134125
};
135126

136127
/** interface to access main font engine*/

Diff for: ‎src/genericobject.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,9 @@ std::string gob_cmd_update_armor_groups(const ItemGroupList &armor_groups)
109109
std::ostringstream os(std::ios::binary);
110110
writeU8(os, GENERIC_CMD_UPDATE_ARMOR_GROUPS);
111111
writeU16(os, armor_groups.size());
112-
for(ItemGroupList::const_iterator i = armor_groups.begin();
113-
i != armor_groups.end(); ++i){
114-
os<<serializeString(i->first);
115-
writeS16(os, i->second);
112+
for (const auto &armor_group : armor_groups) {
113+
os<<serializeString(armor_group.first);
114+
writeS16(os, armor_group.second);
116115
}
117116
return os.str();
118117
}

Diff for: ‎src/gettext.cpp

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

2020
#include <string>
21-
#include <string.h>
21+
#include <cstring>
2222
#include <iostream>
23-
#include <stdlib.h>
23+
#include <cstdlib>
2424
#include "gettext.h"
2525
#include "util/string.h"
2626
#include "log.h"

Diff for: ‎src/guiMainMenu.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626

2727
struct MainMenuDataForScript {
2828

29-
MainMenuDataForScript() {}
29+
MainMenuDataForScript() = default;
3030

3131
// Whether the server has requested a reconnect
3232
bool reconnect_requested = false;
@@ -51,5 +51,5 @@ struct MainMenuData {
5151
// Data to be passed to the script
5252
MainMenuDataForScript script_data;
5353

54-
MainMenuData() {}
54+
MainMenuData() = default;
5555
};

Diff for: ‎src/guiPathSelectMenu.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void GUIFileSelectMenu::drawMenu()
6767

6868
void GUIFileSelectMenu::acceptInput()
6969
{
70-
if ((m_text_dst != 0) && (this->m_formname != "")) {
70+
if (m_text_dst && !m_formname.empty()) {
7171
StringMap fields;
7272
if (m_accepted) {
7373
std::string path;
@@ -82,7 +82,7 @@ void GUIFileSelectMenu::acceptInput()
8282
} else {
8383
fields[m_formname + "_canceled"] = m_formname;
8484
}
85-
this->m_text_dst->gotText(fields);
85+
m_text_dst->gotText(fields);
8686
}
8787
quitMenu();
8888
}

Diff for: ‎src/guiTable.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2222
#include <queue>
2323
#include <sstream>
2424
#include <utility>
25-
#include <string.h>
25+
#include <cstring>
2626
#include <IGUISkin.h>
2727
#include <IGUIFont.h>
2828
#include <IGUIScrollBar.h>

Diff for: ‎src/guiscalingfilter.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#include "imagefilters.h"
2121
#include "settings.h"
2222
#include "util/numeric.h"
23-
#include <stdio.h>
23+
#include <cstdio>
2424
#include "client/renderingengine.h"
2525

2626
/* Maintain a static cache to store the images that correspond to textures
@@ -51,16 +51,14 @@ void guiScalingCache(io::path key, video::IVideoDriver *driver, video::IImage *v
5151
// Manually clear the cache, e.g. when switching to different worlds.
5252
void guiScalingCacheClear()
5353
{
54-
for (std::map<io::path, video::IImage *>::iterator it = g_imgCache.begin();
55-
it != g_imgCache.end(); ++it) {
56-
if (it->second)
57-
it->second->drop();
54+
for (auto &it : g_imgCache) {
55+
if (it.second)
56+
it.second->drop();
5857
}
5958
g_imgCache.clear();
60-
for (std::map<io::path, video::ITexture *>::iterator it = g_txrCache.begin();
61-
it != g_txrCache.end(); ++it) {
62-
if (it->second)
63-
RenderingEngine::get_video_driver()->removeTexture(it->second);
59+
for (auto &it : g_txrCache) {
60+
if (it.second)
61+
RenderingEngine::get_video_driver()->removeTexture(it.second);
6462
}
6563
g_txrCache.clear();
6664
}

Diff for: ‎src/guiscalingfilter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ video::ITexture *guiScalingImageButton(video::IVideoDriver *driver, video::IText
4646
*/
4747
void draw2DImageFilterScaled(video::IVideoDriver *driver, video::ITexture *txr,
4848
const core::rect<s32> &destrect, const core::rect<s32> &srcrect,
49-
const core::rect<s32> *cliprect = 0, const video::SColor *const colors = 0,
49+
const core::rect<s32> *cliprect = 0, video::SColor *const colors = 0,
5050
bool usealpha = false);

0 commit comments

Comments
 (0)
Please sign in to comment.