| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <mutex> |
| 7 |
#include <string> |
| 8 |
#include <utility> |
| 9 |
|
| 10 |
#include "Common/CommonTypes.h" |
| 11 |
#include "Common/Config/Enums.h" |
| 12 |
#include "Common/Mutex.h" |
| 13 |
#include "Common/TypeUtils.h" |
| 14 |
|
| 15 |
namespace Config |
| 16 |
{ |
| 17 |
struct Location |
| 18 |
{ |
| 19 |
System system{}; |
| 20 |
std::string section; |
| 21 |
std::string key; |
| 22 |
|
| 23 |
bool operator==(const Location& other) const; |
| 24 |
bool operator<(const Location& other) const; |
| 25 |
}; |
| 26 |
|
| 27 |
template <typename T> |
| 28 |
struct CachedValue |
| 29 |
{ |
| 30 |
T value; |
| 31 |
u64 config_version; |
| 32 |
}; |
| 33 |
|
| 34 |
template <typename T> |
| 35 |
class Info |
| 36 |
{ |
| 37 |
public: |
| 38 |
constexpr Info(Location location, T default_value) |
| 39 |
: m_location{std::move(location)}, m_default_value{default_value}, |
| 40 |
m_cached_value{std::move(default_value), 0} |
| 41 |
{ |
| 42 |
} |
| 43 |
|
| 44 |
Info(const Info<T>& other) |
| 45 |
: m_location{other.m_location}, m_default_value{other.m_default_value}, |
| 46 |
m_cached_value(other.GetCachedValue()) |
| 47 |
{ |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
template <Common::TypedEnum<T> Enum> |
| 53 |
Info(const Info<Enum>& other) |
| 54 |
: m_location{other.GetLocation()}, m_default_value{static_cast<T>(other.GetDefaultValue())}, |
| 55 |
m_cached_value(other.template GetCachedValueCasted<T>()) |
| 56 |
{ |
| 57 |
} |
| 58 |
|
| 59 |
~Info() = default; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
Info& operator=(const Info&) = delete; |
| 64 |
Info& operator=(Info&&) = delete; |
| 65 |
|
| 66 |
Info(Info&&) = delete; |
| 67 |
|
| 68 |
constexpr const Location& GetLocation() const { return m_location; } |
| 69 |
constexpr const T& GetDefaultValue() const { return m_default_value; } |
| 70 |
|
| 71 |
CachedValue<T> GetCachedValue() const |
| 72 |
{ |
| 73 |
std::lock_guard lk{m_cached_value_mutex}; |
| 74 |
return m_cached_value; |
| 75 |
} |
| 76 |
|
| 77 |
template <typename U> |
| 78 |
CachedValue<U> GetCachedValueCasted() const |
| 79 |
{ |
| 80 |
std::lock_guard lk{m_cached_value_mutex}; |
| 81 |
return {static_cast<U>(m_cached_value.value), m_cached_value.config_version}; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
void TryToSetCachedValue(CachedValue<T> new_value) const |
| 86 |
{ |
| 87 |
std::lock_guard lk{m_cached_value_mutex}; |
| 88 |
if (new_value.config_version > m_cached_value.config_version) |
| 89 |
m_cached_value = std::move(new_value); |
| 90 |
} |
| 91 |
|
| 92 |
private: |
| 93 |
Location m_location; |
| 94 |
T m_default_value; |
| 95 |
|
| 96 |
mutable CachedValue<T> m_cached_value; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
mutable Common::SpinMutex m_cached_value_mutex; |
| 102 |
}; |
| 103 |
} |