Modernize various files
authorLoic Blot <loic.blot@unix-experience.fr>
Fri, 18 Aug 2017 05:44:42 +0000 (07:44 +0200)
committerLoic Blot <loic.blot@unix-experience.fr>
Fri, 18 Aug 2017 05:44:52 +0000 (07:44 +0200)
* range-based for loops
* emplace_back instead of push_back
* code style
* C++ headers instead of C headers
* Default operators
* empty stl function

12 files changed:
src/face_position_cache.cpp
src/filecache.cpp
src/filesys.cpp
src/filesys.h
src/fontengine.h
src/genericobject.cpp
src/gettext.cpp
src/guiMainMenu.h
src/guiPathSelectMenu.cpp
src/guiTable.cpp
src/guiscalingfilter.cpp
src/guiscalingfilter.h

index e6dd12170f8019ebaca8a8ee1e15752e2932ab1d..7a8f235fa4964a963b3265ddb28118ddabe0e0ec 100644 (file)
@@ -40,39 +40,39 @@ const std::vector<v3s16> &FacePositionCache::generateFacePosition(u16 d)
        cache[d] = std::vector<v3s16>();
        std::vector<v3s16> &c = cache[d];
        if (d == 0) {
-               c.push_back(v3s16(0,0,0));
+               c.emplace_back(0,0,0);
                return c;
        }
        if (d == 1) {
                // This is an optimized sequence of coordinates.
-               c.push_back(v3s16( 0, 1, 0)); // Top
-               c.push_back(v3s16( 0, 0, 1)); // Back
-               c.push_back(v3s16(-1, 0, 0)); // Left
-               c.push_back(v3s16( 1, 0, 0)); // Right
-               c.push_back(v3s16( 0, 0,-1)); // Front
-               c.push_back(v3s16( 0,-1, 0)); // Bottom
+               c.emplace_back(0, 1, 0); // Top
+               c.emplace_back(0, 0, 1); // Back
+               c.emplace_back(-1, 0, 0); // Left
+               c.emplace_back(1, 0, 0); // Right
+               c.emplace_back(0, 0,-1); // Front
+               c.emplace_back(0,-1, 0); // Bottom
                // 6
-               c.push_back(v3s16(-1, 0, 1)); // Back left
-               c.push_back(v3s16( 1, 0, 1)); // Back right
-               c.push_back(v3s16(-1, 0,-1)); // Front left
-               c.push_back(v3s16( 1, 0,-1)); // Front right
-               c.push_back(v3s16(-1,-1, 0)); // Bottom left
-               c.push_back(v3s16( 1,-1, 0)); // Bottom right
-               c.push_back(v3s16( 0,-1, 1)); // Bottom back
-               c.push_back(v3s16( 0,-1,-1)); // Bottom front
-               c.push_back(v3s16(-1, 1, 0)); // Top left
-               c.push_back(v3s16( 1, 1, 0)); // Top right
-               c.push_back(v3s16( 0, 1, 1)); // Top back
-               c.push_back(v3s16( 0, 1,-1)); // Top front
+               c.emplace_back(-1, 0, 1); // Back left
+               c.emplace_back(1, 0, 1); // Back right
+               c.emplace_back(-1, 0,-1); // Front left
+               c.emplace_back(1, 0,-1); // Front right
+               c.emplace_back(-1,-1, 0); // Bottom left
+               c.emplace_back(1,-1, 0); // Bottom right
+               c.emplace_back(0,-1, 1); // Bottom back
+               c.emplace_back(0,-1,-1); // Bottom front
+               c.emplace_back(-1, 1, 0); // Top left
+               c.emplace_back(1, 1, 0); // Top right
+               c.emplace_back(0, 1, 1); // Top back
+               c.emplace_back(0, 1,-1); // Top front
                // 18
-               c.push_back(v3s16(-1, 1, 1)); // Top back-left
-               c.push_back(v3s16( 1, 1, 1)); // Top back-right
-               c.push_back(v3s16(-1, 1,-1)); // Top front-left
-               c.push_back(v3s16( 1, 1,-1)); // Top front-right
-               c.push_back(v3s16(-1,-1, 1)); // Bottom back-left
-               c.push_back(v3s16( 1,-1, 1)); // Bottom back-right
-               c.push_back(v3s16(-1,-1,-1)); // Bottom front-left
-               c.push_back(v3s16( 1,-1,-1)); // Bottom front-right
+               c.emplace_back(-1, 1, 1); // Top back-left
+               c.emplace_back(1, 1, 1); // Top back-right
+               c.emplace_back(-1, 1,-1); // Top front-left
+               c.emplace_back(1, 1,-1); // Top front-right
+               c.emplace_back(-1,-1, 1); // Bottom back-left
+               c.emplace_back(1,-1, 1); // Bottom back-right
+               c.emplace_back(-1,-1,-1); // Bottom front-left
+               c.emplace_back(1,-1,-1); // Bottom front-right
                // 26
                return c;
        }
@@ -81,20 +81,20 @@ const std::vector<v3s16> &FacePositionCache::generateFacePosition(u16 d)
        for (s16 y = 0; y <= d - 1; y++) {
                // Left and right side, including borders
                for (s16 z =- d; z <= d; z++) {
-                       c.push_back(v3s16( d, y, z));
-                       c.push_back(v3s16(-d, y, z));
+                       c.emplace_back(d, y, z);
+                       c.emplace_back(-d, y, z);
                        if (y != 0) {
-                               c.push_back(v3s16( d, -y, z));
-                               c.push_back(v3s16(-d, -y, z));
+                               c.emplace_back(d, -y, z);
+                               c.emplace_back(-d, -y, z);
                        }
                }
                // Back and front side, excluding borders
                for (s16 x = -d + 1; x <= d - 1; x++) {
-                       c.push_back(v3s16(x, y, d));
-                       c.push_back(v3s16(x, y, -d));
+                       c.emplace_back(x, y, d);
+                       c.emplace_back(x, y, -d);
                        if (y != 0) {
-                               c.push_back(v3s16(x, -y, d));
-                               c.push_back(v3s16(x, -y, -d));
+                               c.emplace_back(x, -y, d);
+                               c.emplace_back(x, -y, -d);
                        }
                }
        }
@@ -103,8 +103,8 @@ const std::vector<v3s16> &FacePositionCache::generateFacePosition(u16 d)
        // -d < x < d, y = +-d, -d < z < d
        for (s16 x = -d; x <= d; x++)
        for (s16 z = -d; z <= d; z++) {
-               c.push_back(v3s16(x, -d, z));
-               c.push_back(v3s16(x, d, z));
+               c.emplace_back(x, -d, z);
+               c.emplace_back(x, d, z);
        }
        return c;
 }
index f1694d8d58ac049e69408527af0e1e34dce375ab..3d1b302a8cc59e8a5c4cbe9eef2061fac8ba18f2 100644 (file)
@@ -26,7 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <string>
 #include <iostream>
 #include <fstream>
-#include <stdlib.h>
+#include <cstdlib>
 
 bool FileCache::loadByPath(const std::string &path, std::ostream &os)
 {
index bd8b94aff98ff86ebf2ca6a0a5383c4625fd8717..d965384a298669a6289d547d84c40297a2dc9a51 100644 (file)
@@ -20,9 +20,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "filesys.h"
 #include "util/string.h"
 #include <iostream>
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
+#include <cstdio>
+#include <cstring>
+#include <cerrno>
 #include <fstream>
 #include "log.h"
 #include "config.h"
@@ -246,7 +246,7 @@ std::vector<DirListNode> GetDirListing(const std::string &pathstring)
                        If so, try stat().
                */
                if(isdir == -1) {
-                       struct stat statbuf;
+                       struct stat statbuf{};
                        if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
                                continue;
                        isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
@@ -262,22 +262,20 @@ std::vector<DirListNode> GetDirListing(const std::string &pathstring)
 bool CreateDir(const std::string &path)
 {
        int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
-       if(r == 0)
-       {
+       if (r == 0) {
                return true;
        }
-       else
-       {
-               // If already exists, return true
-               if(errno == EEXIST)
-                       return true;
-               return false;
-       }
+
+       // If already exists, return true
+       if (errno == EEXIST)
+               return true;
+       return false;
+
 }
 
 bool PathExists(const std::string &path)
 {
-       struct stat st;
+       struct stat st{};
        return (stat(path.c_str(),&st) == 0);
 }
 
@@ -288,7 +286,7 @@ bool IsPathAbsolute(const std::string &path)
 
 bool IsDir(const std::string &path)
 {
-       struct stat statbuf;
+       struct stat statbuf{};
        if(stat(path.c_str(), &statbuf))
                return false; // Actually error; but certainly not a directory
        return ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
@@ -347,19 +345,19 @@ bool RecursiveDelete(const std::string &path)
 
 bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
 {
-       if(IsDir(path)){
+       if (IsDir(path)) {
                bool did = (rmdir(path.c_str()) == 0);
-               if(!did)
-                       errorstream<<"rmdir errno: "<<errno<<": "<<strerror(errno)
-                                       <<std::endl;
-               return did;
-       } else {
-               bool did = (unlink(path.c_str()) == 0);
-               if(!did)
-                       errorstream<<"unlink errno: "<<errno<<": "<<strerror(errno)
-                                       <<std::endl;
+               if (!did)
+                       errorstream << "rmdir errno: " << errno << ": " << strerror(errno)
+                                       << std::endl;
                return did;
        }
+
+       bool did = (unlink(path.c_str()) == 0);
+       if (!did)
+               errorstream << "unlink errno: " << errno << ": " << strerror(errno)
+                               << std::endl;
+       return did;
 }
 
 std::string TempPath()
@@ -385,8 +383,7 @@ std::string TempPath()
 void GetRecursiveSubPaths(const std::string &path, std::vector<std::string> &dst)
 {
        std::vector<DirListNode> content = GetDirListing(path);
-       for(unsigned int  i=0; i<content.size(); i++){
-               const DirListNode &n = content[i];
+       for (const auto &n : content) {
                std::string fullpath = path + DIR_DELIM + n.name;
                dst.push_back(fullpath);
                if (n.dir) {
@@ -414,15 +411,13 @@ bool RecursiveDeleteContent(const std::string &path)
 {
        infostream<<"Removing content of \""<<path<<"\""<<std::endl;
        std::vector<DirListNode> list = GetDirListing(path);
-       for(unsigned int i=0; i<list.size(); i++)
-       {
-               if(trim(list[i].name) == "." || trim(list[i].name) == "..")
+       for (const DirListNode &dln : list) {
+               if(trim(dln.name) == "." || trim(dln.name) == "..")
                        continue;
-               std::string childpath = path + DIR_DELIM + list[i].name;
+               std::string childpath = path + DIR_DELIM + dln.name;
                bool r = RecursiveDelete(childpath);
-               if(r == false)
-               {
-                       errorstream<<"Removing \""<<childpath<<"\" failed"<<std::endl;
+               if(!r) {
+                       errorstream << "Removing \"" << childpath << "\" failed" << std::endl;
                        return false;
                }
        }
@@ -510,10 +505,10 @@ bool CopyDir(const std::string &source, const std::string &target)
                bool retval = true;
                std::vector<DirListNode> content = fs::GetDirListing(source);
 
-               for(unsigned int i=0; i < content.size(); i++){
-                       std::string sourcechild = source + DIR_DELIM + content[i].name;
-                       std::string targetchild = target + DIR_DELIM + content[i].name;
-                       if(content[i].dir){
+               for (const auto &dln : content) {
+                       std::string sourcechild = source + DIR_DELIM + dln.name;
+                       std::string targetchild = target + DIR_DELIM + dln.name;
+                       if(dln.dir){
                                if(!fs::CopyDir(sourcechild, targetchild)){
                                        retval = false;
                                }
@@ -526,9 +521,8 @@ bool CopyDir(const std::string &source, const std::string &target)
                }
                return retval;
        }
-       else {
-               return false;
-       }
+
+       return false;
 }
 
 bool PathStartsWith(const std::string &path, const std::string &prefix)
index 58846a2654cfc44b895586a67a334f857f866abe..5f246e3875deefb436ae590bfd1cde934c394018 100644 (file)
@@ -26,12 +26,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #ifdef _WIN32 // WINDOWS
 #define DIR_DELIM "\\"
 #define DIR_DELIM_CHAR '\\'
-#define FILESYS_CASE_INSENSITIVE 1
+#define FILESYS_CASE_INSENSITIVE true
 #define PATH_DELIM ";"
 #else // POSIX
 #define DIR_DELIM "/"
 #define DIR_DELIM_CHAR '/'
-#define FILESYS_CASE_INSENSITIVE 0
+#define FILESYS_CASE_INSENSITIVE false
 #define PATH_DELIM ":"
 #endif
 
index 0719edfc471d569908243faf200fd199f15241d4..a75618f8600f296807813b69611fce752b2cc2c8 100644 (file)
@@ -21,9 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include <map>
 #include <vector>
-#include "IGUIFont.h"
-#include "IGUISkin.h"
-#include "IGUIEnvironment.h"
+#include "util/basic_macros.h"
+#include <IGUIFont.h>
+#include <IGUISkin.h>
+#include <IGUIEnvironment.h>
 #include "settings.h"
 
 #define FONT_SIZE_UNSPECIFIED 0xFFFFFFFF
@@ -81,17 +82,6 @@ public:
        void readSettings();
 
 private:
-       /** disable copy constructor */
-       FontEngine() :
-               m_settings(NULL),
-               m_env(NULL),
-               m_font_cache(),
-               m_currentMode(FM_Standard),
-               m_lastMode(),
-               m_lastSize(0),
-               m_lastFont(NULL)
-       {};
-
        /** update content of font cache in case of a setting change made it invalid */
        void updateFontCache();
 
@@ -108,10 +98,10 @@ private:
        void cleanCache();
 
        /** pointer to settings for registering callbacks or reading config */
-       Settings* m_settings;
+       Settings* m_settings = nullptr;
 
        /** pointer to irrlicht gui environment */
-       gui::IGUIEnvironment* m_env;
+       gui::IGUIEnvironment* m_env = nullptr;
 
        /** internal storage for caching fonts of different size */
        std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FM_MaxMode];
@@ -131,6 +121,7 @@ private:
        /** last font returned */
        irr::gui::IGUIFont* m_lastFont = nullptr;
 
+       DISABLE_CLASS_COPY(FontEngine);
 };
 
 /** interface to access main font engine*/
index 58f4b997e828067dee65f7c92ecfd8f250cc88e8..15977b0e034d7479e4f5a87580cecd35572a461a 100644 (file)
@@ -109,10 +109,9 @@ std::string gob_cmd_update_armor_groups(const ItemGroupList &armor_groups)
        std::ostringstream os(std::ios::binary);
        writeU8(os, GENERIC_CMD_UPDATE_ARMOR_GROUPS);
        writeU16(os, armor_groups.size());
-       for(ItemGroupList::const_iterator i = armor_groups.begin();
-                       i != armor_groups.end(); ++i){
-               os<<serializeString(i->first);
-               writeS16(os, i->second);
+       for (const auto &armor_group : armor_groups) {
+               os<<serializeString(armor_group.first);
+               writeS16(os, armor_group.second);
        }
        return os.str();
 }
index 81d93fef3f0666418064b4ba98093d3ee2306137..81621ba891d10c29326ff7cd85520cb8c887cc12 100644 (file)
@@ -18,9 +18,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include <string>
-#include <string.h>
+#include <cstring>
 #include <iostream>
-#include <stdlib.h>
+#include <cstdlib>
 #include "gettext.h"
 #include "util/string.h"
 #include "log.h"
index 0bc5c7be1e6a34e9f828e03d68265ab08fc1e9fa..43a3b1a33e851cc20fd63430dddf42340d71ab94 100644 (file)
@@ -26,7 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 struct MainMenuDataForScript {
 
-       MainMenuDataForScript() {}
+       MainMenuDataForScript() = default;
 
        // Whether the server has requested a reconnect
        bool reconnect_requested = false;
@@ -51,5 +51,5 @@ struct MainMenuData {
        // Data to be passed to the script
        MainMenuDataForScript script_data;
 
-       MainMenuData() {}
+       MainMenuData() = default;
 };
index e56a6224c5aeb4fffabe1faf1025bcc254639a72..b999f0a683fd754aee24ee4b295b4c789aae54dd 100644 (file)
@@ -67,7 +67,7 @@ void GUIFileSelectMenu::drawMenu()
 
 void GUIFileSelectMenu::acceptInput()
 {
-       if ((m_text_dst != 0) && (this->m_formname != "")) {
+       if (m_text_dst && !m_formname.empty()) {
                StringMap fields;
                if (m_accepted) {
                        std::string path;
@@ -82,7 +82,7 @@ void GUIFileSelectMenu::acceptInput()
                } else {
                        fields[m_formname + "_canceled"] = m_formname;
                }
-               this->m_text_dst->gotText(fields);
+               m_text_dst->gotText(fields);
        }
        quitMenu();
 }
index 9354eef3d6e6281bd5749c151204ab207374e9f6..ab6de9435d5cd245d6fd4acc492a9efd6dac63fb 100644 (file)
@@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <queue>
 #include <sstream>
 #include <utility>
-#include <string.h>
+#include <cstring>
 #include <IGUISkin.h>
 #include <IGUIFont.h>
 #include <IGUIScrollBar.h>
index 1b0dfac799d16ba967030af231a8e51e434effc4..dc6219b60ba9b611f80d60786424283942a116b1 100644 (file)
@@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "imagefilters.h"
 #include "settings.h"
 #include "util/numeric.h"
-#include <stdio.h>
+#include <cstdio>
 #include "client/renderingengine.h"
 
 /* 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
 // Manually clear the cache, e.g. when switching to different worlds.
 void guiScalingCacheClear()
 {
-       for (std::map<io::path, video::IImage *>::iterator it = g_imgCache.begin();
-                       it != g_imgCache.end(); ++it) {
-               if (it->second)
-                       it->second->drop();
+       for (auto &it : g_imgCache) {
+               if (it.second)
+                       it.second->drop();
        }
        g_imgCache.clear();
-       for (std::map<io::path, video::ITexture *>::iterator it = g_txrCache.begin();
-                       it != g_txrCache.end(); ++it) {
-               if (it->second)
-                       RenderingEngine::get_video_driver()->removeTexture(it->second);
+       for (auto &it : g_txrCache) {
+               if (it.second)
+                       RenderingEngine::get_video_driver()->removeTexture(it.second);
        }
        g_txrCache.clear();
 }
index 4661bf8daff3e4df3c4209392a29cec58cea1bb0..7408ff5ebd85c767602ff956517012bc65b32be6 100644 (file)
@@ -46,5 +46,5 @@ video::ITexture *guiScalingImageButton(video::IVideoDriver *driver, video::IText
  */
 void draw2DImageFilterScaled(video::IVideoDriver *driver, video::ITexture *txr,
                const core::rect<s32> &destrect, const core::rect<s32> &srcrect,
-               const core::rect<s32> *cliprect = 0, const video::SColor *const colors = 0,
+               const core::rect<s32> *cliprect = 0, video::SColor *const colors = 0,
                bool usealpha = false);