2017-10-30 10:22:37 -06:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-12-05 10:24:41 -07:00
|
|
|
#include <mutex>
|
|
|
|
#include <shared_mutex>
|
2017-10-30 10:22:37 -06:00
|
|
|
#include <string>
|
2018-05-11 14:38:44 -06:00
|
|
|
#include <type_traits>
|
2020-12-05 10:24:41 -07:00
|
|
|
#include <utility>
|
2017-10-30 10:22:37 -06:00
|
|
|
|
2020-12-05 10:24:41 -07:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-10-30 10:22:37 -06:00
|
|
|
#include "Common/Config/Enums.h"
|
|
|
|
|
|
|
|
namespace Config
|
|
|
|
{
|
2018-05-11 14:38:44 -06:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
// std::underlying_type may only be used with enum types, so make sure T is an enum type first.
|
|
|
|
template <typename T>
|
|
|
|
using UnderlyingType = typename std::enable_if_t<std::is_enum<T>{}, std::underlying_type<T>>::type;
|
|
|
|
} // namespace detail
|
|
|
|
|
2020-05-02 06:39:40 -06:00
|
|
|
struct Location
|
2017-10-30 10:22:37 -06:00
|
|
|
{
|
|
|
|
System system;
|
|
|
|
std::string section;
|
|
|
|
std::string key;
|
|
|
|
|
2020-05-02 06:39:40 -06:00
|
|
|
bool operator==(const Location& other) const;
|
|
|
|
bool operator!=(const Location& other) const;
|
|
|
|
bool operator<(const Location& other) const;
|
2017-10-30 10:22:37 -06:00
|
|
|
};
|
|
|
|
|
2020-12-05 10:24:41 -07:00
|
|
|
template <typename T>
|
|
|
|
struct CachedValue
|
|
|
|
{
|
|
|
|
T value;
|
|
|
|
u64 config_version;
|
|
|
|
};
|
|
|
|
|
2017-10-30 10:22:37 -06:00
|
|
|
template <typename T>
|
2020-09-20 05:58:17 -06:00
|
|
|
class Info
|
2017-10-30 10:22:37 -06:00
|
|
|
{
|
2020-09-20 05:58:17 -06:00
|
|
|
public:
|
|
|
|
constexpr Info(const Location& location, const T& default_value)
|
2020-12-05 10:24:41 -07:00
|
|
|
: m_location{location}, m_default_value{default_value}, m_cached_value{default_value, 0}
|
2018-05-11 14:38:44 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-12-05 10:24:41 -07:00
|
|
|
Info(const Info<T>& other) { *this = other; }
|
|
|
|
|
|
|
|
// Not thread-safe
|
|
|
|
Info(Info<T>&& other) { *this = std::move(other); }
|
|
|
|
|
2020-05-02 06:39:40 -06:00
|
|
|
// Make it easy to convert Info<Enum> into Info<UnderlyingType<Enum>>
|
2018-05-11 14:38:44 -06:00
|
|
|
// so that enum settings can still easily work with code that doesn't care about the enum values.
|
|
|
|
template <typename Enum,
|
|
|
|
std::enable_if_t<std::is_same<T, detail::UnderlyingType<Enum>>::value>* = nullptr>
|
2020-12-05 10:24:41 -07:00
|
|
|
Info(const Info<Enum>& other)
|
2018-05-11 14:38:44 -06:00
|
|
|
{
|
2020-12-05 10:24:41 -07:00
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
Info<T>& operator=(const Info<T>& other)
|
|
|
|
{
|
|
|
|
m_location = other.GetLocation();
|
|
|
|
m_default_value = other.GetDefaultValue();
|
|
|
|
m_cached_value = other.GetCachedValue();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not thread-safe
|
|
|
|
Info<T>& operator=(Info<T>&& other)
|
|
|
|
{
|
|
|
|
m_location = std::move(other.m_location);
|
|
|
|
m_default_value = std::move(other.m_default_value);
|
|
|
|
m_cached_value = std::move(other.m_cached_value);
|
|
|
|
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 <typename Enum,
|
|
|
|
std::enable_if_t<std::is_same<T, detail::UnderlyingType<Enum>>::value>* = nullptr>
|
|
|
|
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;
|
2018-05-11 14:38:44 -06:00
|
|
|
}
|
|
|
|
|
2020-09-20 05:58:17 -06:00
|
|
|
constexpr const Location& GetLocation() const { return m_location; }
|
|
|
|
constexpr const T& GetDefaultValue() const { return m_default_value; }
|
|
|
|
|
2020-12-05 10:24:41 -07:00
|
|
|
CachedValue<T> GetCachedValue() const
|
|
|
|
{
|
|
|
|
std::shared_lock lock(m_cached_value_mutex);
|
|
|
|
return m_cached_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
CachedValue<U> GetCachedValueCasted() const
|
|
|
|
{
|
|
|
|
std::shared_lock lock(m_cached_value_mutex);
|
|
|
|
return CachedValue<U>{static_cast<U>(m_cached_value.value), m_cached_value.config_version};
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetCachedValue(const CachedValue<T>& cached_value) const
|
|
|
|
{
|
|
|
|
std::unique_lock lock(m_cached_value_mutex);
|
|
|
|
if (m_cached_value.config_version < cached_value.config_version)
|
|
|
|
m_cached_value = cached_value;
|
|
|
|
}
|
|
|
|
|
2020-09-20 05:58:17 -06:00
|
|
|
private:
|
|
|
|
Location m_location;
|
|
|
|
T m_default_value;
|
2020-12-05 10:24:41 -07:00
|
|
|
|
|
|
|
mutable CachedValue<T> m_cached_value;
|
|
|
|
mutable std::shared_mutex m_cached_value_mutex;
|
2017-10-30 10:22:37 -06:00
|
|
|
};
|
2019-05-05 17:48:12 -06:00
|
|
|
} // namespace Config
|