ngscopeclient v0.2.2
Loading...
Searching...
No Matches
Preference.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* ngscopeclient *
4* *
5* Copyright (c) 2012-2026 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
36#ifndef Preference_h
37#define Preference_h
38
39#include <algorithm>
40#include <string>
41#include <type_traits>
42#include <utility>
43#include <type_traits>
44#include <cstdint>
45#include <set>
46#include <vector>
47
48#include <imgui.h>
49
50#include "Unit.h"
51#include "FontManager.h"
52
53enum class PreferenceType
54{
55 Boolean,
56 String,
57 Real,
58 Color,
59 Enum,
60 Font,
61 Int,
62 None // Only for moved-from values
63};
64
65namespace impl
66{
67 class PreferenceBuilder;
68
69 struct Color
70 {
71 Color(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a)
72 : m_r{r}, m_g{g}, m_b{b}, m_a{a}
73 {
74
75 }
76
77 std::uint8_t m_r, m_g, m_b, m_a;
78 };
79}
80
82{
83 using base_type = std::int64_t;
84
85 public:
87 {
88
89 }
90
91 public:
92 void AddEnumMember(const std::string& name, base_type value);
93 const std::string& GetName(base_type value) const;
94 base_type GetValue(const std::string& name) const;
95 const std::vector<std::string>& GetNames() const;
96 bool HasNameFor(base_type value) const;
97 bool HasValueFor(const std::string& name) const;
98
99 public:
100 template<
101 typename E/*,
102 typename = typename std::enable_if<
103 std::is_convertible<E, base_type>::value &&
104 not std::is_same<E, base_type>::value
105 >::type*/
106 >
107 void AddEnumMember(const std::string& name, E value)
108 {
109 const auto val = static_cast<base_type>(value);
110 this->AddEnumMember(name, val);
111 }
112
113 protected:
114 std::map<std::string, base_type> m_forwardMap;
115 std::map<base_type, std::string> m_backwardMap;
116 std::vector<std::string> m_names;
117};
118
120{
121 friend class impl::PreferenceBuilder;
122
123private:
124 using PreferenceValue =
125 typename std::aligned_union<
126 std::max(
127 {
128 sizeof(std::int64_t),
129 sizeof(impl::Color),
130 sizeof(bool),
131 sizeof(double),
132 sizeof(std::string),
133 sizeof(FontDescription)
134 }),
135 bool,
136 std::string,
137 double,
139 std::int64_t,
140 FontDescription
141 >::type;
142
143private:
144 std::string m_identifier;
145 std::string m_label;
146 std::string m_description;
147 PreferenceType m_type;
148 PreferenceValue m_value;
149 PreferenceValue m_defaultValue;
150 bool m_isVisible{true};
151 Unit m_unit{Unit::UNIT_COUNTS};
152 bool m_hasValue{false};
153 EnumMapping m_mapping;
154
155public:
156 Preference(PreferenceType type, std::string identifier)
157 : m_identifier{std::move(identifier)}, m_type{type}
158 {
159
160 }
161
163 {
164 CleanUp();
165 }
166
167public:
168 Preference(const Preference&) = delete;
169 Preference(Preference&& other)
170 {
171 MoveFrom(other);
172 }
173
174 Preference& operator=(const Preference&) = delete;
175 Preference& operator=(Preference&& other)
176 {
177 CleanUp();
178 MoveFrom(other);
179 return *this;
180 }
181
182public:
183 const std::string& GetIdentifier() const;
184 const std::string& GetLabel() const;
185 const std::string& GetDescription() const;
186 void SetFont(const FontDescription& font);
187 FontDescription GetFont() const;
188 std::int64_t GetEnumRaw() const;
189 void SetEnumRaw(std::int64_t value);
190 PreferenceType GetType() const;
191 bool GetBool() const;
192 double GetReal() const;
193 std::int64_t GetInt() const;
194 const std::string& GetString() const;
195 std::string ToString() const;
196 bool GetIsVisible() const;
197 ImU32 GetColor() const;
198 const impl::Color& GetColorRaw() const;
199 void SetBool(bool value);
200 void SetReal(double value);
201 void SetInt(std::int64_t value);
202 void SetString(const std::string& value);
203 void SetColor(const ImU32& value);
204 void SetColorRaw(const impl::Color& value);
205 void SetLabel(std::string label);
206 void SetDescription(std::string description);
207 bool HasUnit();
208 void ResetToDefault();
209 Unit& GetUnit();
210 const EnumMapping& GetMapping() const;
211
212 template< typename E >
213 E GetEnum() const
214 {
215 return static_cast<E>(this->GetEnumRaw());
216 }
217
218 template< typename E >
219 void SetEnum(E value)
220 {
221 this->SetEnumRaw(static_cast<std::int64_t>(value));
222 }
223
224public:
225 static impl::PreferenceBuilder Int(std::string identifier, int64_t defaultValue);
226 static impl::PreferenceBuilder Real(std::string identifier, double defaultValue);
227 static impl::PreferenceBuilder Bool(std::string identifier, bool defaultValue);
228 static impl::PreferenceBuilder String(std::string identifier, std::string defaultValue);
229 static impl::PreferenceBuilder Color(std::string identifier, const ImU32& defaultValue);
230 static impl::PreferenceBuilder EnumRaw(std::string identifier, std::int64_t defaultValue);
231 static impl::PreferenceBuilder Font(std::string identifier, FontDescription defaultValue);
232
233 template< typename E >
234 static impl::PreferenceBuilder Enum(std::string identifier, E defaultValue);
235
236
237private:
238 void SetMapping(EnumMapping mapping);
239
240 template<typename T>
241 const T& GetValueRaw() const
242 {
243 return *reinterpret_cast<const T*>(&m_value);
244 }
245
246 template<typename T>
247 T& GetValueRaw()
248 {
249 return *reinterpret_cast<T*>(&m_value);
250 }
251
252 void CleanUp();
253
254 template<typename T>
255 void Construct(T value)
256 {
257 new (&m_value) T(std::move(value));
258 m_hasValue = true;
259 }
260
261 void MoveFrom(Preference& other);
262};
263
264namespace impl
265{
266 class PreferenceBuilder
267 {
268 protected:
269 Preference m_pref;
270
271 public:
272 PreferenceBuilder(Preference&& pref);
273
274 public:
275 PreferenceBuilder Invisible() &&;
276 PreferenceBuilder Label(std::string label) &&;
277 PreferenceBuilder Description(std::string description) &&;
278 PreferenceBuilder Unit(Unit::UnitType type) &&;
279 Preference Build() &&;
280
281 template< typename E >
282 PreferenceBuilder EnumValue(const std::string& name, E value) &&
283 {
284 this->m_pref.m_mapping.AddEnumMember<E>(name, value);
285 return std::move(*this);
286 }
287 };
288}
289
290template< typename E >
291impl::PreferenceBuilder Preference::Enum(std::string identifier, E defaultValue)
292{
293 return EnumRaw(std::move(identifier), static_cast<std::int64_t>(defaultValue));
294}
295
296#endif // Preference_h
Declaration of FontManager.
Declaration of Unit.
Definition AcceleratorBuffer.h:204
Definition Preference.h:82
Definition Preference.h:120
A unit of measurement, plus conversion to pretty-printed output.
Definition Unit.h:59
Definition Preference.h:70