| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <map> |
| 7 |
#include <memory> |
| 8 |
#include <optional> |
| 9 |
#include <string> |
| 10 |
#include <type_traits> |
| 11 |
|
| 12 |
#include "Common/Config/ConfigInfo.h" |
| 13 |
#include "Common/Config/Enums.h" |
| 14 |
#include "Common/StringUtil.h" |
| 15 |
|
| 16 |
namespace Config |
| 17 |
{ |
| 18 |
|
| 19 |
struct DefaultState |
| 20 |
{ |
| 21 |
friend constexpr bool operator==(DefaultState, DefaultState) { return true; } |
| 22 |
}; |
| 23 |
|
| 24 |
namespace detail |
| 25 |
{ |
| 26 |
template <typename T> |
| 27 |
requires(!Common::Enum<T>) |
| 28 |
std::optional<T> TryParse(const std::string& str_value) |
| 29 |
{ |
| 30 |
T value; |
| 31 |
if (!::TryParse(str_value, &value)) |
| 32 |
return std::nullopt; |
| 33 |
return value; |
| 34 |
} |
| 35 |
|
| 36 |
template <Common::Enum T> |
| 37 |
std::optional<T> TryParse(const std::string& str_value) |
| 38 |
{ |
| 39 |
const auto result = TryParse<std::underlying_type_t<T>>(str_value); |
| 40 |
if (result) |
| 41 |
return static_cast<T>(*result); |
| 42 |
return {}; |
| 43 |
} |
| 44 |
|
| 45 |
template <> |
| 46 |
inline std::optional<std::string> TryParse(const std::string& str_value) |
| 47 |
{ |
| 48 |
return str_value; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
template <typename T> |
| 53 |
class Info; |
| 54 |
|
| 55 |
class Layer; |
| 56 |
using LayerMap = std::map<Location, std::optional<std::string>>; |
| 57 |
|
| 58 |
class ConfigLayerLoader |
| 59 |
{ |
| 60 |
public: |
| 61 |
explicit ConfigLayerLoader(LayerType layer); |
| 62 |
virtual ~ConfigLayerLoader(); |
| 63 |
virtual void Load(Layer* config_layer) = 0; |
| 64 |
virtual void Save(Layer* config_layer) = 0; |
| 65 |
|
| 66 |
LayerType GetLayer() const; |
| 67 |
|
| 68 |
private: |
| 69 |
const LayerType m_layer; |
| 70 |
}; |
| 71 |
|
| 72 |
class Section |
| 73 |
{ |
| 74 |
public: |
| 75 |
using iterator = LayerMap::iterator; |
| 76 |
Section(iterator begin_, iterator end_) : m_begin(begin_), m_end(end_) {} |
| 77 |
iterator begin() const { return m_begin; } |
| 78 |
iterator end() const { return m_end; } |
| 79 |
|
| 80 |
private: |
| 81 |
iterator m_begin; |
| 82 |
iterator m_end; |
| 83 |
}; |
| 84 |
|
| 85 |
class ConstSection |
| 86 |
{ |
| 87 |
public: |
| 88 |
using iterator = LayerMap::const_iterator; |
| 89 |
ConstSection(iterator begin_, iterator end_) : m_begin(begin_), m_end(end_) {} |
| 90 |
iterator begin() const { return m_begin; } |
| 91 |
iterator end() const { return m_end; } |
| 92 |
|
| 93 |
private: |
| 94 |
iterator m_begin; |
| 95 |
iterator m_end; |
| 96 |
}; |
| 97 |
|
| 98 |
class Layer |
| 99 |
{ |
| 100 |
public: |
| 101 |
explicit Layer(LayerType layer); |
| 102 |
explicit Layer(std::unique_ptr<ConfigLayerLoader> loader); |
| 103 |
virtual ~Layer(); |
| 104 |
|
| 105 |
|
| 106 |
bool Exists(const Location& location) const; |
| 107 |
bool DeleteKey(const Location& location); |
| 108 |
void DeleteAllKeys(); |
| 109 |
|
| 110 |
template <typename T> |
| 111 |
T Get(const Info<T>& config_info) const |
| 112 |
{ |
| 113 |
return Get<T>(config_info.GetLocation()).value_or(config_info.GetDefaultValue()); |
| 114 |
} |
| 115 |
|
| 116 |
template <typename T> |
| 117 |
std::optional<T> Get(const Location& location) const |
| 118 |
{ |
| 119 |
const auto iter = m_map.find(location); |
| 120 |
if (iter == m_map.end() || !iter->second.has_value()) |
| 121 |
return std::nullopt; |
| 122 |
return detail::TryParse<T>(*iter->second); |
| 123 |
} |
| 124 |
|
| 125 |
template <typename T> |
| 126 |
bool Set(const Info<T>& config_info, DefaultState) |
| 127 |
{ |
| 128 |
return DeleteKey(config_info.GetLocation()); |
| 129 |
} |
| 130 |
|
| 131 |
template <typename T> |
| 132 |
bool Set(const Info<T>& config_info, const std::common_type_t<T>& value) |
| 133 |
{ |
| 134 |
return Set(config_info.GetLocation(), value); |
| 135 |
} |
| 136 |
|
| 137 |
bool Set(const Location& location, DefaultState) { return DeleteKey(location); } |
| 138 |
|
| 139 |
template <typename T> |
| 140 |
bool Set(const Location& location, const T& value) |
| 141 |
{ |
| 142 |
return Set(location, ValueToString(value)); |
| 143 |
} |
| 144 |
|
| 145 |
bool Set(const Location& location, std::string new_value) |
| 146 |
{ |
| 147 |
const auto iter = m_map.find(location); |
| 148 |
if (iter != m_map.end() && iter->second == new_value) |
| 149 |
return false; |
| 150 |
m_is_dirty = true; |
| 151 |
m_map.insert_or_assign(location, std::move(new_value)); |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
void MarkAsDirty() { m_is_dirty = true; } |
| 156 |
|
| 157 |
Section GetSection(System system, const std::string& section); |
| 158 |
ConstSection GetSection(System system, const std::string& section) const; |
| 159 |
|
| 160 |
|
| 161 |
void Load(); |
| 162 |
void Save(); |
| 163 |
|
| 164 |
LayerType GetLayer() const; |
| 165 |
const LayerMap& GetLayerMap() const; |
| 166 |
|
| 167 |
protected: |
| 168 |
bool m_is_dirty = false; |
| 169 |
LayerMap m_map; |
| 170 |
const LayerType m_layer; |
| 171 |
std::unique_ptr<ConfigLayerLoader> m_loader; |
| 172 |
}; |
| 173 |
} |