1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-11 06:40:34 +00:00

Merge branch 'settings_string_view' into 'master'

Use std::string_view to access settings

See merge request OpenMW/openmw!2032
This commit is contained in:
psi29a 2022-06-19 10:54:06 +00:00
commit d7c9caf13c
3 changed files with 56 additions and 49 deletions

View File

@ -5,12 +5,24 @@
#include <set> #include <set>
#include <string> #include <string>
#include <utility> #include <utility>
#include <string_view>
namespace Settings namespace Settings
{ {
struct Less
{
using is_transparent = void;
bool operator()(const std::pair<std::string_view, std::string_view>& l,
const std::pair<std::string_view, std::string_view>& r) const
{
return l < r;
}
};
using CategorySetting = std::pair<std::string, std::string>; using CategorySetting = std::pair<std::string, std::string>;
using CategorySettingVector = std::set<CategorySetting>; using CategorySettingVector = std::set<CategorySetting>;
using CategorySettingValueMap = std::map<CategorySetting, std::string>; using CategorySettingValueMap = std::map<CategorySetting, std::string, Less>;
} }
#endif // COMPONENTS_SETTINGS_CATEGORIES_H #endif // COMPONENTS_SETTINGS_CATEGORIES_H

View File

@ -71,9 +71,9 @@ void Manager::saveUser(const std::string &file)
parser.saveSettingsFile(file, mUserSettings); parser.saveSettingsFile(file, mUserSettings);
} }
std::string Manager::getString(const std::string &setting, const std::string &category) std::string Manager::getString(std::string_view setting, std::string_view category)
{ {
CategorySettingValueMap::key_type key (category, setting); const auto key = std::make_pair(category, setting);
CategorySettingValueMap::iterator it = mUserSettings.find(key); CategorySettingValueMap::iterator it = mUserSettings.find(key);
if (it != mUserSettings.end()) if (it != mUserSettings.end())
return it->second; return it->second;
@ -82,11 +82,13 @@ std::string Manager::getString(const std::string &setting, const std::string &ca
if (it != mDefaultSettings.end()) if (it != mDefaultSettings.end())
return it->second; return it->second;
throw std::runtime_error(std::string("Trying to retrieve a non-existing setting: ") + setting std::string error("Trying to retrieve a non-existing setting: ");
+ ".\nMake sure the defaults.bin file was properly installed."); error += setting;
error += ".\nMake sure the defaults.bin file was properly installed.";
throw std::runtime_error(error);
} }
float Manager::getFloat (const std::string& setting, const std::string& category) float Manager::getFloat(std::string_view setting, std::string_view category)
{ {
const std::string& value = getString(setting, category); const std::string& value = getString(setting, category);
std::stringstream stream(value); std::stringstream stream(value);
@ -95,7 +97,7 @@ float Manager::getFloat (const std::string& setting, const std::string& category
return number; return number;
} }
double Manager::getDouble (const std::string& setting, const std::string& category) double Manager::getDouble(std::string_view setting, std::string_view category)
{ {
const std::string& value = getString(setting, category); const std::string& value = getString(setting, category);
std::stringstream stream(value); std::stringstream stream(value);
@ -104,7 +106,7 @@ double Manager::getDouble (const std::string& setting, const std::string& catego
return number; return number;
} }
int Manager::getInt (const std::string& setting, const std::string& category) int Manager::getInt(std::string_view setting, std::string_view category)
{ {
const std::string& value = getString(setting, category); const std::string& value = getString(setting, category);
std::stringstream stream(value); std::stringstream stream(value);
@ -113,7 +115,7 @@ int Manager::getInt (const std::string& setting, const std::string& category)
return number; return number;
} }
std::int64_t Manager::getInt64 (const std::string& setting, const std::string& category) std::int64_t Manager::getInt64(std::string_view setting, std::string_view category)
{ {
const std::string& value = getString(setting, category); const std::string& value = getString(setting, category);
std::stringstream stream(value); std::stringstream stream(value);
@ -122,13 +124,13 @@ std::int64_t Manager::getInt64 (const std::string& setting, const std::string& c
return number; return number;
} }
bool Manager::getBool (const std::string& setting, const std::string& category) bool Manager::getBool(std::string_view setting, std::string_view category)
{ {
const std::string& string = getString(setting, category); const std::string& string = getString(setting, category);
return Misc::StringUtils::ciEqual(string, "true"); return Misc::StringUtils::ciEqual(string, "true");
} }
osg::Vec2f Manager::getVector2 (const std::string& setting, const std::string& category) osg::Vec2f Manager::getVector2(std::string_view setting, std::string_view category)
{ {
const std::string& value = getString(setting, category); const std::string& value = getString(setting, category);
std::stringstream stream(value); std::stringstream stream(value);
@ -139,7 +141,7 @@ osg::Vec2f Manager::getVector2 (const std::string& setting, const std::string& c
return {x, y}; return {x, y};
} }
osg::Vec3f Manager::getVector3 (const std::string& setting, const std::string& category) osg::Vec3f Manager::getVector3(std::string_view setting, std::string_view category)
{ {
const std::string& value = getString(setting, category); const std::string& value = getString(setting, category);
std::stringstream stream(value); std::stringstream stream(value);
@ -150,75 +152,69 @@ osg::Vec3f Manager::getVector3 (const std::string& setting, const std::string& c
return {x, y, z}; return {x, y, z};
} }
void Manager::setString(const std::string &setting, const std::string &category, const std::string &value) void Manager::setString(std::string_view setting, std::string_view category, const std::string &value)
{ {
CategorySettingValueMap::key_type key (category, setting); auto found = mUserSettings.find(std::make_pair(category, setting));
auto found = mUserSettings.find(key);
if (found != mUserSettings.end()) if (found != mUserSettings.end())
{ {
if (found->second == value) if (found->second == value)
return; return;
} }
CategorySettingValueMap::key_type key(category, setting);
mUserSettings[key] = value; mUserSettings[key] = value;
mChangedSettings.insert(key); mChangedSettings.insert(std::move(key));
} }
void Manager::setInt (const std::string& setting, const std::string& category, const int value) void Manager::setInt(std::string_view setting, std::string_view category, const int value)
{ {
std::ostringstream stream; std::ostringstream stream;
stream << value; stream << value;
setString(setting, category, stream.str()); setString(setting, category, stream.str());
} }
void Manager::setInt64 (const std::string& setting, const std::string& category, const std::int64_t value) void Manager::setInt64(std::string_view setting, std::string_view category, const std::int64_t value)
{ {
std::ostringstream stream; std::ostringstream stream;
stream << value; stream << value;
setString(setting, category, stream.str()); setString(setting, category, stream.str());
} }
void Manager::setFloat (const std::string &setting, const std::string &category, const float value) void Manager::setFloat (std::string_view setting, std::string_view category, const float value)
{ {
std::ostringstream stream; std::ostringstream stream;
stream << value; stream << value;
setString(setting, category, stream.str()); setString(setting, category, stream.str());
} }
void Manager::setDouble (const std::string &setting, const std::string &category, const double value) void Manager::setDouble (std::string_view setting, std::string_view category, const double value)
{ {
std::ostringstream stream; std::ostringstream stream;
stream << value; stream << value;
setString(setting, category, stream.str()); setString(setting, category, stream.str());
} }
void Manager::setBool(const std::string &setting, const std::string &category, const bool value) void Manager::setBool(std::string_view setting, std::string_view category, const bool value)
{ {
setString(setting, category, value ? "true" : "false"); setString(setting, category, value ? "true" : "false");
} }
void Manager::setVector2 (const std::string &setting, const std::string &category, const osg::Vec2f value) void Manager::setVector2 (std::string_view setting, std::string_view category, const osg::Vec2f value)
{ {
std::ostringstream stream; std::ostringstream stream;
stream << value.x() << " " << value.y(); stream << value.x() << " " << value.y();
setString(setting, category, stream.str()); setString(setting, category, stream.str());
} }
void Manager::setVector3 (const std::string &setting, const std::string &category, const osg::Vec3f value) void Manager::setVector3 (std::string_view setting, std::string_view category, const osg::Vec3f value)
{ {
std::ostringstream stream; std::ostringstream stream;
stream << value.x() << ' ' << value.y() << ' ' << value.z(); stream << value.x() << ' ' << value.y() << ' ' << value.z();
setString(setting, category, stream.str()); setString(setting, category, stream.str());
} }
void Manager::resetPendingChange(const std::string &setting, const std::string &category)
{
CategorySettingValueMap::key_type key (category, setting);
mChangedSettings.erase(key);
}
CategorySettingVector Manager::getPendingChanges() CategorySettingVector Manager::getPendingChanges()
{ {
return mChangedSettings; return mChangedSettings;

View File

@ -6,6 +6,8 @@
#include <set> #include <set>
#include <map> #include <map>
#include <string> #include <string>
#include <string_view>
#include <osg/Vec2f> #include <osg/Vec2f>
#include <osg/Vec3f> #include <osg/Vec3f>
@ -44,9 +46,6 @@ namespace Settings
static void saveUser (const std::string& file); static void saveUser (const std::string& file);
///< save user settings to file ///< save user settings to file
static void resetPendingChange(const std::string &setting, const std::string &category);
///< resets a single pending change
static void resetPendingChanges(); static void resetPendingChanges();
///< resets the list of all pending changes ///< resets the list of all pending changes
@ -59,23 +58,23 @@ namespace Settings
static CategorySettingVector getPendingChanges(const CategorySettingVector& filter); static CategorySettingVector getPendingChanges(const CategorySettingVector& filter);
///< returns the list of changed settings intersecting with the filter ///< returns the list of changed settings intersecting with the filter
static int getInt (const std::string& setting, const std::string& category); static int getInt(std::string_view setting, std::string_view category);
static std::int64_t getInt64 (const std::string& setting, const std::string& category); static std::int64_t getInt64(std::string_view setting, std::string_view category);
static float getFloat (const std::string& setting, const std::string& category); static float getFloat(std::string_view setting, std::string_view category);
static double getDouble (const std::string& setting, const std::string& category); static double getDouble(std::string_view setting, std::string_view category);
static std::string getString (const std::string& setting, const std::string& category); static std::string getString(std::string_view setting, std::string_view category);
static bool getBool (const std::string& setting, const std::string& category); static bool getBool(std::string_view setting, std::string_view category);
static osg::Vec2f getVector2 (const std::string& setting, const std::string& category); static osg::Vec2f getVector2(std::string_view setting, std::string_view category);
static osg::Vec3f getVector3 (const std::string& setting, const std::string& category); static osg::Vec3f getVector3(std::string_view setting, std::string_view category);
static void setInt (const std::string& setting, const std::string& category, int value); static void setInt(std::string_view setting, std::string_view category, int value);
static void setInt64 (const std::string& setting, const std::string& category, std::int64_t value); static void setInt64(std::string_view setting, std::string_view category, std::int64_t value);
static void setFloat (const std::string& setting, const std::string& category, float value); static void setFloat(std::string_view setting, std::string_view category, float value);
static void setDouble (const std::string& setting, const std::string& category, double value); static void setDouble(std::string_view setting, std::string_view category, double value);
static void setString (const std::string& setting, const std::string& category, const std::string& value); static void setString(std::string_view setting, std::string_view category, const std::string& value);
static void setBool (const std::string& setting, const std::string& category, bool value); static void setBool(std::string_view setting, std::string_view category, bool value);
static void setVector2 (const std::string& setting, const std::string& category, osg::Vec2f value); static void setVector2(std::string_view setting, std::string_view category, osg::Vec2f value);
static void setVector3 (const std::string& setting, const std::string& category, osg::Vec3f value); static void setVector3(std::string_view setting, std::string_view category, osg::Vec3f value);
}; };
} }