NDEVR
API Documentation
ApplicationOptions.h
1#pragma once
2#include "DLLInfo.h"
3#include <NDEVR/String.h>
4#include <NDEVR/GenericOption.h>
5#include <NDEVR/INIFactory.h>
6#include <NDEVR/Dictionary.h>
7namespace NDEVR
8{
9 class INIFactory;
10 class INIOption;
11 class ResourceListener;
12
21 {
22 public:
27
32 virtual StringAllocatingView toString() const = 0;
33
38 virtual void fromString(const String&) = 0;
39
44 virtual bool isDefault() const = 0;
45
49 virtual void setToDefaultValue() = 0;
50
55 virtual INIOption* iniOption() = 0;
56 };
57
64 class NDEVR_BASE_API ApplicationOptions
65 {
66 public:
71
78 {
80 }
81
88 void addOption(const TranslatedString& group_name, const TranslatedString& name, ApplicationOptionBase* value)
89 {
90 lib_assert(!m_options[group_name].hasKey(name), "Duplicate entry");
91 m_options[group_name][name] = value;
92 }
93
101 {
102 return m_options[group][name];
103 }
104
111 {
113 }
114
121
126 void addToINI(INIFactory& factory);
127
133 void addToINI(const TranslatedString& group_name, INIFactory& factory);
134
139
144 void setToDefaults(const TranslatedString& group_name);
145
151 void logOptions(LogPtr& log, bool log_only_non_defaults);
152
158 protected:
160 };
161
169 template<class t_type>
171 {
172 public:
181 : m_group(std::move(group))
182 , m_label(std::move(label))
183 , m_value(value)
184 , m_options(options)
185 , m_default_value(value)
186 {
187 m_options.addOption(m_group, m_label, this);
188 }
189
198 : m_group(group)
199 , m_label(label)
200 , m_value(value)
201 , m_options(options)
202 , m_default_value(value)
203 {
204 m_options.addOption(group, label, this);
205 }
206
214 : m_label(label)
215 , m_value(value)
216 , m_options(options)
217 , m_default_value(value)
218 {
219 m_options.addOption(label, this);
220 }
221
226 template<class t_other_type = t_type>
227 decltype(auto) get() const
228 {
229 return m_value.get();// m_options.getValue<t_type>(m_group, m_label);
230 }
231
237 {
238 return m_value;
239 }
240
246 void set(const t_type& value, bool make_default = false)
247 {
248 if (make_default)
249 m_default_value = value;
250 return m_value.set(value);
251 }
252
257 void setDefaultValue(const t_type& value)
258 {
259 m_default_value = value;
260 }
261
267 void addListener(ResourceListener* listener, bool silent_add = false)
268 {
269 m_value.addListener(listener, silent_add);
270 }
271
276 virtual StringAllocatingView toString() const override
277 {
278 return StringAllocatingView(m_value.get());
279 }
280
285 virtual void fromString(const String& value) override
286 {
287 m_value.set(value.getAs<t_type>());
288 }
289
294 virtual bool isDefault() const override
295 {
296 return m_value.get() == m_default_value;
297 }
298
302 virtual void setToDefaultValue() override
303 {
305 }
306
311 virtual INIOption* iniOption() override
312 {
314 }
315
320 const TranslatedString& label() const
321 {
322 return m_label;
323 }
324
329 const TranslatedString& group() const
330 {
331 return m_group;
332 }
333 protected:
339 };
340}
The core of the default object to store data of any type that should persist through sessions of the ...
virtual ~ApplicationOptionBase()
Destructor.
virtual StringAllocatingView toString() const =0
Converts the stored option value to a string representation.
virtual INIOption * iniOption()=0
Creates an INIOption representation of this option for INI file serialization.
virtual bool isDefault() const =0
Checks whether the current value matches the default value.
virtual void fromString(const String &)=0
Sets the option value by parsing the given string.
virtual void setToDefaultValue()=0
Resets the option value back to its default.
const TranslatedString m_label
The display label identifying this option.
ApplicationOption(const TranslatedString &group, const TranslatedString &label, const t_type &value, ApplicationOptions &options=ApplicationOptions::UserOptions())
Constructs an ApplicationOption with a group, label, and default value using copy semantics.
virtual void setToDefaultValue() override
Resets the option value back to its default.
t_type m_default_value
The default value used for reset operations.
const TranslatedString & group() const
Returns the translated group name for this option.
const TranslatedString m_group
The group this option belongs to for categorization.
const TranslatedString & label() const
Returns the translated display label for this option.
ApplicationOption(TranslatedString &&group, TranslatedString &&label, const t_type &value, ApplicationOptions &options=ApplicationOptions::UserOptions())
Constructs an ApplicationOption with a group, label, and default value using move semantics.
decltype(auto) get() const
Retrieves the current value of this option.
virtual void fromString(const String &value) override
Sets the option value by parsing the given string.
ApplicationOption(const TranslatedString &label, const t_type &value, ApplicationOptions &options=ApplicationOptions::UserOptions())
Constructs an ApplicationOption with no group, using the default group.
void addListener(ResourceListener *listener, bool silent_add=false)
Registers a ResourceListener to be notified when this option's value changes.
void setDefaultValue(const t_type &value)
Updates the default value without changing the current value.
ApplicationOptions & m_options
Reference to the owning ApplicationOptions container.
Resource< t_type > & getResource()
Returns a reference to the underlying Resource holding this option's value.
void set(const t_type &value, bool make_default=false)
Sets the option to a new value, optionally updating the default as well.
virtual bool isDefault() const override
Checks whether the current value matches the default value.
virtual StringAllocatingView toString() const override
Converts the stored option value to a string representation.
Resource< t_type > m_value
The resource holding the current option value.
virtual INIOption * iniOption() override
Creates an INIOption representation of this option for INI file serialization.
The container for storing N-number of ApplicationOption for the program.
Dictionary< TranslatedString, ApplicationOptionBase * > optionGroup(TranslatedString)
Returns all options belonging to a specific group.
void logOptions(LogPtr &log, bool log_only_non_defaults)
Writes option names and values to the given log.
void addToINI(INIFactory &factory)
Serializes all option groups into the given INI factory.
Dictionary< TranslatedString, Dictionary< TranslatedString, ApplicationOptionBase * > > m_options
Nested dictionary mapping group names to option dictionaries.
void addToINI(const TranslatedString &group_name, INIFactory &factory)
Serializes a specific option group into the given INI factory.
const ApplicationOptionBase * getOption(const TranslatedString &name) const
Retrieves an option from the default group by name.
static ApplicationOptions & UserOptions()
Returns the global user options container singleton.
void setToDefaults(const TranslatedString &group_name)
Resets all options in a specific group to their default values.
const ApplicationOptionBase * getOption(const TranslatedString &group, const TranslatedString &name) const
Retrieves an option by group and name.
void addOption(const TranslatedString &group_name, const TranslatedString &name, ApplicationOptionBase *value)
Adds an option under a specific group name.
void addOption(const TranslatedString &name, ApplicationOptionBase *value)
Adds an option with no group, placing it in the default group.
void setToDefaults()
Resets all options across all groups to their default values.
ApplicationOptions()
Constructs an empty ApplicationOptions container.
A hash-based key-value store, useful for quick associative lookups.
Definition Dictionary.h:64
Contains methods for easily reading and writing to an INI file including efficient casting,...
Definition INIReader.h:107
static INIOption * ToOption(t_type &mem_loc)
Creates an INIOption wrapper for a raw typed variable.
Definition INIReader.h:333
A class used with INIFactory to store a reference to an object in the program that can be inherited t...
Definition INIReader.h:49
A light-weight wrapper that will be a no-op if there is not a valid log reference,...
A class that can subscribe to any number of Resources which will get updates when the Resource(s) hav...
A core part of the engine, stores variables that can be listened to with ResourceListener which will ...
Definition Resource.h:42
This class is like a string view, but may optionally store the data internally Useful if the return t...
Definition String.h:1278
The core String class for the NDEVR API.
Definition String.h:95
decltype(auto) getAs() const
Converts a string into an object.
Definition String.h:188
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
The primary namespace for the NDEVR SDK.
@ name
The display name of the object.
STL namespace.