ngscopeclient 0.1-dev+51fbda87c
Preference.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* glscopeclient *
4* *
5* Copyright (c) 2012-2022 Andrew D. Zonenberg *
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 bool m_isVisible{true};
150 Unit m_unit{Unit::UNIT_COUNTS};
151 bool m_hasValue{false};
152 EnumMapping m_mapping;
153
154public:
155 Preference(PreferenceType type, std::string identifier)
156 : m_identifier{std::move(identifier)}, m_type{type}
157 {
158
159 }
160
162 {
163 CleanUp();
164 }
165
166public:
167 Preference(const Preference&) = delete;
168 Preference(Preference&& other)
169 {
170 MoveFrom(other);
171 }
172
173 Preference& operator=(const Preference&) = delete;
174 Preference& operator=(Preference&& other)
175 {
176 CleanUp();
177 MoveFrom(other);
178 return *this;
179 }
180
181public:
182 const std::string& GetIdentifier() const;
183 const std::string& GetLabel() const;
184 const std::string& GetDescription() const;
185 void SetFont(const FontDescription& font);
186 FontDescription GetFont() const;
187 std::int64_t GetEnumRaw() const;
188 void SetEnumRaw(std::int64_t value);
189 PreferenceType GetType() const;
190 bool GetBool() const;
191 double GetReal() const;
192 std::int64_t GetInt() const;
193 const std::string& GetString() const;
194 std::string ToString() const;
195 bool GetIsVisible() const;
196 ImU32 GetColor() const;
197 const impl::Color& GetColorRaw() const;
198 void SetBool(bool value);
199 void SetReal(double value);
200 void SetInt(std::int64_t value);
201 void SetString(std::string value);
202 void SetColor(const ImU32& value);
203 void SetColorRaw(const impl::Color& value);
204 void SetLabel(std::string label);
205 void SetDescription(std::string description);
206 bool HasUnit();
207 Unit& GetUnit();
208 const EnumMapping& GetMapping() const;
209
210 template< typename E >
211 E GetEnum() const
212 {
213 return static_cast<E>(this->GetEnumRaw());
214 }
215
216 template< typename E >
217 void SetEnum(E value)
218 {
219 this->SetEnumRaw(static_cast<std::int64_t>(value));
220 }
221
222public:
223 static impl::PreferenceBuilder Int(std::string identifier, int64_t defaultValue);
224 static impl::PreferenceBuilder Real(std::string identifier, double defaultValue);
225 static impl::PreferenceBuilder Bool(std::string identifier, bool defaultValue);
226 static impl::PreferenceBuilder String(std::string identifier, std::string defaultValue);
227 static impl::PreferenceBuilder Color(std::string identifier, const ImU32& defaultValue);
228 static impl::PreferenceBuilder EnumRaw(std::string identifier, std::int64_t defaultValue);
229 static impl::PreferenceBuilder Font(std::string identifier, FontDescription defaultValue);
230
231 template< typename E >
232 static impl::PreferenceBuilder Enum(std::string identifier, E defaultValue);
233
234
235private:
236 void SetMapping(EnumMapping mapping);
237
238 template<typename T>
239 const T& GetValueRaw() const
240 {
241 return *reinterpret_cast<const T*>(&m_value);
242 }
243
244 template<typename T>
245 T& GetValueRaw()
246 {
247 return *reinterpret_cast<T*>(&m_value);
248 }
249
250 void CleanUp();
251
252 template<typename T>
253 void Construct(T value)
254 {
255 new (&m_value) T(std::move(value));
256 m_hasValue = true;
257 }
258
259 void MoveFrom(Preference& other);
260};
261
262namespace impl
263{
265 {
266 protected:
267 Preference m_pref;
268
269 public:
271
272 public:
273 PreferenceBuilder Invisible() &&;
274 PreferenceBuilder Label(std::string label) &&;
275 PreferenceBuilder Description(std::string description) &&;
276 PreferenceBuilder Unit(Unit::UnitType type) &&;
277 Preference Build() &&;
278
279 template< typename E >
280 PreferenceBuilder EnumValue(const std::string& name, E value) &&
281 {
282 this->m_pref.m_mapping.AddEnumMember<E>(name, value);
283 return std::move(*this);
284 }
285 };
286}
287
288template< typename E >
289impl::PreferenceBuilder Preference::Enum(std::string identifier, E defaultValue)
290{
291 return EnumRaw(std::move(identifier), static_cast<std::int64_t>(defaultValue));
292}
293
294#endif // Preference_h
Declaration of FontManager.
Declaration of Unit.
Definition: Preference.h:82
Definition: Preference.h:120
A unit of measurement, plus conversion to pretty-printed output.
Definition: Unit.h:57
Definition: Preference.h:265
Definition: Preference.h:70