]> git.lizzy.rs Git - minetest.git/blobdiff - src/settings.cpp
Move `setlocale` from Lua to C++.
[minetest.git] / src / settings.cpp
index 1fa4ac52837aea4282702ea352acab5888c42623..0c34eb10c921b7236db6423204ec7a5fd3d4a1b0 100644 (file)
@@ -169,9 +169,8 @@ void Settings::writeLines(std::ostream &os, u32 tab_depth) const
 {
        MutexAutoLock lock(m_mutex);
 
-       for (SettingEntries::const_iterator it = m_settings.begin();
-                       it != m_settings.end(); ++it)
-               printEntry(os, it->first, it->second, tab_depth);
+       for (const auto &setting_it : m_settings)
+               printEntry(os, setting_it.first, setting_it.second, tab_depth);
 }
 
 
@@ -323,7 +322,7 @@ bool Settings::parseCommandLine(int argc, char *argv[],
 
                ValueType type = n->second.type;
 
-               std::string value = "";
+               std::string value;
 
                if (type == VALUETYPE_FLAG) {
                        value = "true";
@@ -362,6 +361,18 @@ const SettingsEntry &Settings::getEntry(const std::string &name) const
 }
 
 
+const SettingsEntry &Settings::getEntryDefault(const std::string &name) const
+{
+       MutexAutoLock lock(m_mutex);
+
+       SettingEntries::const_iterator n;
+       if ((n = m_defaults.find(name)) == m_defaults.end()) {
+               throw SettingNotFoundException("Setting [" + name + "] not found.");
+       }
+       return n->second;
+}
+
+
 Settings *Settings::getGroup(const std::string &name) const
 {
        const SettingsEntry &entry = getEntry(name);
@@ -380,6 +391,15 @@ const std::string &Settings::get(const std::string &name) const
 }
 
 
+const std::string &Settings::getDefault(const std::string &name) const
+{
+       const SettingsEntry &entry = getEntryDefault(name);
+       if (entry.is_group)
+               throw SettingNotFoundException("Setting [" + name + "] is a group.");
+       return entry.value;
+}
+
+
 bool Settings::getBool(const std::string &name) const
 {
        return is_yes(get(name));
@@ -506,7 +526,7 @@ bool Settings::getNoiseParamsFromValue(const std::string &name,
        np.persist  = stof(f.next(","));
 
        std::string optional_params = f.next("");
-       if (optional_params != "")
+       if (!optional_params.empty())
                np.lacunarity = stof(optional_params);
 
        return true;
@@ -549,9 +569,8 @@ bool Settings::exists(const std::string &name) const
 std::vector<std::string> Settings::getNames() const
 {
        std::vector<std::string> names;
-       for (SettingEntries::const_iterator i = m_settings.begin();
-                       i != m_settings.end(); ++i) {
-               names.push_back(i->first);
+       for (const auto &settings_it : m_settings) {
+               names.push_back(settings_it.first);
        }
        return names;
 }
@@ -573,6 +592,17 @@ bool Settings::getEntryNoEx(const std::string &name, SettingsEntry &val) const
 }
 
 
+bool Settings::getEntryDefaultNoEx(const std::string &name, SettingsEntry &val) const
+{
+       try {
+               val = getEntryDefault(name);
+               return true;
+       } catch (SettingNotFoundException &e) {
+               return false;
+       }
+}
+
+
 bool Settings::getGroupNoEx(const std::string &name, Settings *&val) const
 {
        try {
@@ -595,6 +625,17 @@ bool Settings::getNoEx(const std::string &name, std::string &val) const
 }
 
 
+bool Settings::getDefaultNoEx(const std::string &name, std::string &val) const
+{
+       try {
+               val = getDefault(name);
+               return true;
+       } catch (SettingNotFoundException &e) {
+               return false;
+       }
+}
+
+
 bool Settings::getFlag(const std::string &name) const
 {
        try {
@@ -861,9 +902,9 @@ bool Settings::remove(const std::string &name)
                delete it->second.group;
                m_settings.erase(it);
                return true;
-       } else {
-               return false;
        }
+
+       return false;
 }
 
 
@@ -887,8 +928,7 @@ void Settings::updateValue(const Settings &other, const std::string &name)
        MutexAutoLock lock(m_mutex);
 
        try {
-               std::string val = other.get(name);
-               m_settings[name] = val;
+               m_settings[name] = other.get(name);
        } catch (SettingNotFoundException &e) {
        }
 }
@@ -965,7 +1005,7 @@ void Settings::registerChangedCallback(const std::string &name,
        SettingsChangedCallback cbf, void *userdata)
 {
        MutexAutoLock lock(m_callback_mutex);
-       m_callbacks[name].push_back(std::make_pair(cbf, userdata));
+       m_callbacks[name].emplace_back(cbf, userdata);
 }
 
 void Settings::deregisterChangedCallback(const std::string &name,