Use std::string_view in ConfigDB

Now no `std::string` has to be constructed to get a config-value.
To accomplish that the map has to have a "transparent" comparator.
This commit is contained in:
phosit 2025-04-15 20:49:09 +02:00 committed by phosit
parent 0b33921c06
commit 0afb5f3d06
2 changed files with 14 additions and 15 deletions

View file

@ -86,10 +86,8 @@ std::string EscapeString(const CStr& str)
} // anonymous namespace
typedef std::map<CStr, CConfigValueSet> TConfigMap;
#define GETVAL(type)\
void CConfigDB::GetValue(EConfigNamespace ns, const CStr& name, type& value)\
void CConfigDB::GetValue(EConfigNamespace ns, const std::string_view name, type& value)\
{\
CHECK_NS(;);\
std::lock_guard<std::recursive_mutex> s(m_Mutex);\

View file

@ -34,6 +34,7 @@
#include <map>
#include <memory>
#include <mutex>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
@ -84,31 +85,30 @@ public:
* will search CFG_COMMAND first, and then all namespaces from the specified
* namespace down.
*/
void GetValue(EConfigNamespace ns, const CStr& name, bool& value);
void GetValue(EConfigNamespace ns, const std::string_view name, bool& value);
///@copydoc CConfigDB::GetValue
void GetValue(EConfigNamespace ns, const CStr& name, int& value);
void GetValue(EConfigNamespace ns, const std::string_view name, int& value);
///@copydoc CConfigDB::GetValue
void GetValue(EConfigNamespace ns, const CStr& name, u32& value);
void GetValue(EConfigNamespace ns, const std::string_view name, u32& value);
///@copydoc CConfigDB::GetValue
void GetValue(EConfigNamespace ns, const CStr& name, float& value);
void GetValue(EConfigNamespace ns, const std::string_view name, float& value);
///@copydoc CConfigDB::GetValue
void GetValue(EConfigNamespace ns, const CStr& name, double& value);
void GetValue(EConfigNamespace ns, const std::string_view name, double& value);
///@copydoc CConfigDB::GetValue
void GetValue(EConfigNamespace ns, const CStr& name, std::string& value);
void GetValue(EConfigNamespace ns, const std::string_view name, std::string& value);
template<typename T>
[[nodiscard]] T Get(std::string name, T value, const EConfigNamespace ns = CFG_USER)
[[nodiscard]] T Get(const std::string_view name, T value, const EConfigNamespace ns = CFG_USER)
{
GetValue(ns, std::move(name), value);
GetValue(ns, name, value);
return value;
}
template<typename T>
[[nodiscard]] static T GetIfInitialised(std::string name, T defaultValue,
[[nodiscard]] static T GetIfInitialised(const std::string_view name, T defaultValue,
const EConfigNamespace ns = CFG_USER)
{
return IsInitialised() ? g_ConfigDB.Get(std::move(name), std::move(defaultValue), ns) :
defaultValue;
return IsInitialised() ? g_ConfigDB.Get(name, std::move(defaultValue), ns) : defaultValue;
}
/**
@ -217,7 +217,8 @@ public:
void UnregisterHook(std::unique_ptr<CConfigDBHook> hook);
private:
std::array<std::map<CStr, CConfigValueSet>, CFG_LAST> m_Map;
using TConfigMap = std::map<CStr, CConfigValueSet, std::less<>>;
std::array<TConfigMap, CFG_LAST> m_Map;
std::multimap<CStr, std::function<void()>> m_Hooks;
std::array<VfsPath, CFG_LAST> m_ConfigFile;
std::array<bool, CFG_LAST> m_HasChanges;