header_utils
Loading...
Searching...
No Matches
configs.h
1
4
5#pragma once
6
7#include <string>
8#include <functional>
9
10#include <nlohmann/json.hpp>
11
12#include "enum_flags.h"
13
15{
19
20 struct cvar_base_t;
21
22 struct cvar_manager_t;
23
24 struct cvar_group_t
25 {
26 cvar_group_t(cvar_group_t& parent, std::string_view name)
27 {
28
29 }
30
31 cvar_group_t(std::string_view name)
32 : cvar_group_t(global_group(), name)
33 {
34
35 }
36
37 static cvar_group_t& global_group() noexcept
38 {
39 static cvar_group_t m_global;
40 return m_global;
41 }
42
43 cvar_group_t* resolve_group_path(std::string_view group_path)
44 {
45 throw "unimplemented";
46 return nullptr;
47 }
48
49 private:
50
51 friend struct cvar_manager_t;
52
53 std::vector<cvar_base_t*> m_children_cvars;
54 std::vector<cvar_group_t*> m_children_groups;
55
56 cvar_group_t()
57 {
58
59 }
60 };
61
62 enum class cvar_flags
63 {
64
65 };
66
67 struct config_source_t
68 {
69 friend struct cvar_manager_t;
70 };
71
72 struct cvar_base_t
73 {
74 void set(cvar_flags flag) noexcept { m_flags += flag; }
75
76 cvar_group_t& group() const noexcept { return m_group ? *m_group : resolve_group_path(); }
77 std::string_view name() const noexcept { return m_name; }
78 enum_flags<cvar_flags> flag() const noexcept { return m_flags; }
79
80 cvar_base_t(cvar_group_t& group, std::string_view name)
81 : m_group(&group)
82 , m_name(name)
83 {
84
85 }
86
87 cvar_base_t(std::string_view group_path, std::string_view name)
88 : m_group_path(group_path)
89 , m_name(name)
90 {
91
92 }
93
94 virtual nlohmann::json json() const = 0;
95 virtual void json(nlohmann::json const& json) = 0;
96
97 virtual ~cvar_base_t() noexcept = default;
98
99 protected:
100
101 friend struct cvar_manager_t;
102
103 std::string const m_group_path;
104 mutable cvar_group_t* m_group = nullptr;
105 std::string const m_name;
106 enum_flags<cvar_flags> m_flags{};
107
108 config_source_t* m_current_source = nullptr;
109
110 cvar_group_t& resolve_group_path() const
111 {
112 auto group = cvar_group_t::global_group().resolve_group_path(m_group_path);
113 if (!group)
114 throw std::invalid_argument("group path does not resolve to any defined group");
115 return *(m_group = group);
116 }
117 };
118
119 template <typename T>
120 struct cvar_t : cvar_base_t
121 {
122 using cvar_change_callback = std::function<void(cvar_t<T>&)>;
123
124 T const& default_value() const noexcept { return m_default_value; }
125
126 template <typename... ARGS>
127 cvar_t(cvar_group_t& group, std::string_view name, T value, ARGS&&... args)
128 : cvar_base_t(group, name)
129 , m_default_value(std::move(value))
130 {
131 (this->set(std::forward<ARGS>(args)), ...);
132 }
133
134 template <typename... ARGS>
135 cvar_t(std::string_view group_name, std::string_view name, T value, ARGS&&... args)
136 : cvar_base_t(group_name, name)
137 , m_default_value(std::move(value))
138 {
139 (this->set(std::forward<ARGS>(args)), ...);
140 }
141
142 template <typename CALLBACK>
143 requires std::constructible_from<cvar_change_callback, CALLBACK>
144 void set(CALLBACK&&)
145 {
146
147 }
148
149 explicit operator T const& () const noexcept
150 {
151 return m_current_value;
152 }
153
154
155 virtual nlohmann::json json() const override
156 {
157 return nlohmann::json{ m_current_value };
158 }
159
160 virtual void json(nlohmann::json const& json) override
161 {
162 m_current_value = json;
163 }
164
165 private:
166
167 friend struct cvar_manager_t;
168
169 T m_current_value{};
170 T const m_default_value;
171
172 };
173
174 struct cvar_manager_t
175 {
176
177 };
178
180}
constexpr auto bit_count
Equal to the number of bits in the type.
Definition bits.h:33
A (constexpr) value struct that represents a set of bits mapped to an enum (implemented as a bitset)
Definition enum_flags.h:29