common/param_package: Allow heterogenous lookup
Allows the functions that only query or retrieve from the underlying map to be able to handle keys in various string formats. This means we can make these bits of the API non-allocating by default.
This commit is contained in:
@@ -73,8 +73,8 @@ std::string ParamPackage::Serialize() const {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ParamPackage::Get(const std::string& key, const std::string& default_value) const {
|
std::string ParamPackage::Get(std::string_view key, const std::string& default_value) const {
|
||||||
auto pair = data.find(key);
|
const auto pair = data.find(key);
|
||||||
if (pair == data.end()) {
|
if (pair == data.end()) {
|
||||||
LOG_DEBUG(Common, "key '{}' not found", key);
|
LOG_DEBUG(Common, "key '{}' not found", key);
|
||||||
return default_value;
|
return default_value;
|
||||||
@@ -83,8 +83,8 @@ std::string ParamPackage::Get(const std::string& key, const std::string& default
|
|||||||
return pair->second;
|
return pair->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ParamPackage::Get(const std::string& key, int default_value) const {
|
int ParamPackage::Get(std::string_view key, int default_value) const {
|
||||||
auto pair = data.find(key);
|
const auto pair = data.find(key);
|
||||||
if (pair == data.end()) {
|
if (pair == data.end()) {
|
||||||
LOG_DEBUG(Common, "key '{}' not found", key);
|
LOG_DEBUG(Common, "key '{}' not found", key);
|
||||||
return default_value;
|
return default_value;
|
||||||
@@ -98,8 +98,8 @@ int ParamPackage::Get(const std::string& key, int default_value) const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float ParamPackage::Get(const std::string& key, float default_value) const {
|
float ParamPackage::Get(std::string_view key, float default_value) const {
|
||||||
auto pair = data.find(key);
|
const auto pair = data.find(key);
|
||||||
if (pair == data.end()) {
|
if (pair == data.end()) {
|
||||||
LOG_DEBUG(Common, "key {} not found", key);
|
LOG_DEBUG(Common, "key {} not found", key);
|
||||||
return default_value;
|
return default_value;
|
||||||
@@ -125,8 +125,8 @@ void ParamPackage::Set(const std::string& key, float value) {
|
|||||||
data.insert_or_assign(key, std::to_string(value));
|
data.insert_or_assign(key, std::to_string(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ParamPackage::Has(const std::string& key) const {
|
bool ParamPackage::Has(std::string_view key) const {
|
||||||
return data.find(key) != data.end();
|
return data.contains(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ParamPackage::Erase(const std::string& key) {
|
void ParamPackage::Erase(const std::string& key) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
@@ -13,7 +14,17 @@ namespace Common {
|
|||||||
/// A string-based key-value container supporting serializing to and deserializing from a string
|
/// A string-based key-value container supporting serializing to and deserializing from a string
|
||||||
class ParamPackage {
|
class ParamPackage {
|
||||||
public:
|
public:
|
||||||
using DataType = std::unordered_map<std::string, std::string>;
|
struct DataHash final {
|
||||||
|
using is_transparent = void;
|
||||||
|
|
||||||
|
[[nodiscard]] size_t operator()(std::string_view view) const noexcept {
|
||||||
|
return std::hash<std::string_view>{}(view);
|
||||||
|
}
|
||||||
|
[[nodiscard]] size_t operator()(const std::string& str) const noexcept {
|
||||||
|
return std::hash<std::string>{}(str);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
using DataType = std::unordered_map<std::string, std::string, DataHash, std::equal_to<>>;
|
||||||
|
|
||||||
ParamPackage() = default;
|
ParamPackage() = default;
|
||||||
explicit ParamPackage(const std::string& serialized);
|
explicit ParamPackage(const std::string& serialized);
|
||||||
@@ -25,13 +36,13 @@ public:
|
|||||||
ParamPackage& operator=(ParamPackage&& other) = default;
|
ParamPackage& operator=(ParamPackage&& other) = default;
|
||||||
|
|
||||||
[[nodiscard]] std::string Serialize() const;
|
[[nodiscard]] std::string Serialize() const;
|
||||||
[[nodiscard]] std::string Get(const std::string& key, const std::string& default_value) const;
|
[[nodiscard]] std::string Get(std::string_view key, const std::string& default_value) const;
|
||||||
[[nodiscard]] int Get(const std::string& key, int default_value) const;
|
[[nodiscard]] int Get(std::string_view key, int default_value) const;
|
||||||
[[nodiscard]] float Get(const std::string& key, float default_value) const;
|
[[nodiscard]] float Get(std::string_view key, float default_value) const;
|
||||||
void Set(const std::string& key, std::string value);
|
void Set(const std::string& key, std::string value);
|
||||||
void Set(const std::string& key, int value);
|
void Set(const std::string& key, int value);
|
||||||
void Set(const std::string& key, float value);
|
void Set(const std::string& key, float value);
|
||||||
[[nodiscard]] bool Has(const std::string& key) const;
|
[[nodiscard]] bool Has(std::string_view key) const;
|
||||||
void Erase(const std::string& key);
|
void Erase(const std::string& key);
|
||||||
void Clear();
|
void Clear();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user