Config/ConfigInfo: Cleanups.

This commit is contained in:
Jordan Woyak 2025-10-31 16:26:06 -05:00
parent 9c28f19e56
commit fc9f25a418
2 changed files with 27 additions and 42 deletions

View File

@ -72,10 +72,10 @@ T Get(const Info<T>& info)
cached.value = GetUncached(info); cached.value = GetUncached(info);
cached.config_version = config_version; cached.config_version = config_version;
info.SetCachedValue(cached); info.TryToSetCachedValue(cached);
} }
return cached.value; return std::move(cached.value);
} }
template <typename T> template <typename T>

View File

@ -35,73 +35,58 @@ template <typename T>
class Info class Info
{ {
public: public:
constexpr Info(const Location& location, const T& default_value) constexpr Info(Location location, T default_value)
: m_location{location}, m_default_value{default_value}, m_cached_value{default_value, 0} : m_location{std::move(location)}, m_default_value{default_value},
m_cached_value{std::move(default_value), 0}
{ {
} }
Info(const Info<T>& other) { *this = other; } Info(const Info<T>& other)
: m_location{other.m_location}, m_default_value{other.m_default_value},
// Not thread-safe m_cached_value(other.GetCachedValue())
Info(Info<T>&& other) { *this = std::move(other); } {
}
// Make it easy to convert Info<Enum> into Info<UnderlyingType<Enum>> // Make it easy to convert Info<Enum> into Info<UnderlyingType<Enum>>
// so that enum settings can still easily work with code that doesn't care about the enum values. // so that enum settings can still easily work with code that doesn't care about the enum values.
template <Common::TypedEnum<T> Enum> template <Common::TypedEnum<T> Enum>
Info(const Info<Enum>& other) Info(const Info<Enum>& other)
: m_location{other.GetLocation()}, m_default_value{static_cast<T>(other.GetDefaultValue())},
m_cached_value(other.template GetCachedValueCasted<T>())
{ {
*this = other;
} }
Info<T>& operator=(const Info<T>& other) ~Info() = default;
{
m_location = other.GetLocation();
m_default_value = other.GetDefaultValue();
m_cached_value = other.GetCachedValue();
return *this;
}
// Not thread-safe // Assignments after construction would require more locking to be thread safe.
Info<T>& operator=(Info<T>&& other) // It seems unnecessary to have this functionality anyways.
{ Info& operator=(const Info&) = delete;
m_location = std::move(other.m_location); Info& operator=(Info&&) = delete;
m_default_value = std::move(other.m_default_value); // Moves are also unnecessary and would be thread unsafe without additional locking.
m_cached_value = std::move(other.m_cached_value); Info(Info&&) = delete;
return *this;
}
// Make it easy to convert Info<Enum> into Info<UnderlyingType<Enum>>
// so that enum settings can still easily work with code that doesn't care about the enum values.
template <Common::TypedEnum<T> Enum>
Info<T>& operator=(const Info<Enum>& other)
{
m_location = other.GetLocation();
m_default_value = static_cast<T>(other.GetDefaultValue());
m_cached_value = other.template GetCachedValueCasted<T>();
return *this;
}
constexpr const Location& GetLocation() const { return m_location; } constexpr const Location& GetLocation() const { return m_location; }
constexpr const T& GetDefaultValue() const { return m_default_value; } constexpr const T& GetDefaultValue() const { return m_default_value; }
CachedValue<T> GetCachedValue() const CachedValue<T> GetCachedValue() const
{ {
std::shared_lock lock(m_cached_value_mutex); std::shared_lock lk{m_cached_value_mutex};
return m_cached_value; return m_cached_value;
} }
template <typename U> template <typename U>
CachedValue<U> GetCachedValueCasted() const CachedValue<U> GetCachedValueCasted() const
{ {
std::shared_lock lock(m_cached_value_mutex); std::shared_lock lk{m_cached_value_mutex};
return CachedValue<U>{static_cast<U>(m_cached_value.value), m_cached_value.config_version}; return {static_cast<U>(m_cached_value.value), m_cached_value.config_version};
} }
void SetCachedValue(const CachedValue<T>& cached_value) const // Only updates if the provided config_version is newer.
void TryToSetCachedValue(CachedValue<T> new_value) const
{ {
std::unique_lock lock(m_cached_value_mutex); std::lock_guard lk{m_cached_value_mutex};
if (m_cached_value.config_version < cached_value.config_version) if (new_value.config_version > m_cached_value.config_version)
m_cached_value = cached_value; m_cached_value = std::move(new_value);
} }
private: private: