Debian Codegraph @

Source/Core/Common/Config/ConfigInfo.h

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
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 // Make it easy to convert Info<Enum> into Info<UnderlyingType<Enum>>
51 // so that enum settings can still easily work with code that doesn't care about the enum values.
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 // Assignments after construction would require more locking to be thread safe.
62 // It seems unnecessary to have this functionality anyways.
63 Info& operator=(const Info&) = delete;
64 Info& operator=(Info&&) = delete;
65 // Moves are also unnecessary and would be thread unsafe without additional locking.
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 // Only updates if the provided config_version is newer.
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 // In testing, this mutex is effectively never contested.
99 // The lock durations are brief and each `Info` object is mostly relevant to one thread.
100 // Common::SpinMutex is ~3x faster than std::shared_mutex when uncontested.
101 mutable Common::SpinMutex m_cached_value_mutex;
102 };
103 } // namespace Config