NDEVR
API Documentation
GenericOptions.h
1#pragma once
2#include "DLLInfo.h"
3#include <NDEVR/Pointer.h>
4#include <NDEVR/TypeInfo.h>
5#include <NDEVR/String.h>
6#include <NDEVR/Unit.h>
7#include <NDEVR/TranslatedString.h>
8#include <NDEVR/Dictionary.h>
9#include <NDEVR/Resource.h>
10#include <utility>
11namespace NDEVR
12{
13 /*struct GenericContainer
14 {
15 union ContainerValue
16 {
17 ContainerValue() {};
18 ~ContainerValue() {}
19 String string_value;
20 fltp08 fltp08_value;
21 fltp04 fltp04_value;
22 sint08 sint08_value;
23 uint08 uint08_value;
24 sint04 sint04_value;
25 uint04 uint04_value;
26 bool bool_value;
27 };
28 template<class t_type> GenericContainer(const t_type& value) { value.string_value = String(value); }
29 GenericContainer(const String& value) { m_value.string_value = value; }
30 GenericContainer(fltp08 value) { m_value.fltp08_value = value; }
31 GenericContainer(fltp04 value) { m_value.fltp04_value = value; }
32 GenericContainer(sint08 value) { m_value.sint08_value = value; }
33 GenericContainer(sint04 value) { m_value.sint04_value = value; }
34 GenericContainer(uint08 value) { m_value.uint08_value = value; }
35 GenericContainer(uint04 value) { m_value.uint04_value = value; }
36 GenericContainer(bool value) { m_value.bool_value = value; }
37 template<class t_type> t_type getValue() const { return m_value.string_value.template getAs<t_type>(); }
38 template<> String getValue() const { return m_value.string_value.getAs<String>(); }
39 template<> fltp08 getValue<fltp08>() const { return m_value.fltp08_value; }
40 template<> fltp04 getValue<fltp04>() const { return m_value.fltp04_value; }
41 template<> sint08 getValue<sint08>() const { return m_value.sint08_value; }
42 template<> sint04 getValue<sint04>() const { return m_value.sint04_value; }
43 template<> uint08 getValue<uint08>() const { return m_value.uint08_value; }
44 template<> uint04 getValue<uint04>() const { return m_value.uint04_value; }
45 template<> bool getValue<bool>() const { return m_value.bool_value; }
46
47 protected:
48 ContainerValue m_value;
49 };*/
50 class INIFactory;
51 class JSONNode;
52 class InfoPipe;
57 struct NDEVR_BASE_API GenericOption
58 {
72 bool allow_custom_option = false;
73 bool is_editable = true;
74 bool is_viewable = true;
75 bool show_resolved_links = false;
76 bool is_password = false;
77 bool allow_multi_line = false;
78 bool is_create_file = false;
96 template<class t_type>
97 GenericOption(const StringView& id, const TranslatedString& name, const t_type& value)
98 : name(name)
99 , id(id)
100 , value(String(value))
102 , parameter_type(GetTypeInfo<t_type>())
103 {}
104
113 template<class t_type>
114 [[nodiscard]] t_type getAs() const
115 {
116 return value.get().getAs<t_type>();
117 }
118
122 template<class t_type>
123 void set(const t_type& new_value)
124 {
125 return value.set(String(new_value));
126 }
127
131 void set(const StringView& new_value)
132 {
133 return value.set(String(new_value));
134 }
135
139 void set(const String& new_value)
140 {
141 return value.set(new_value);
142 }
143
147 {
148 value.set(default_value);
149 }
150
167 void setupForFile(const StringView& extensions = StringView());
177 bool operator==(const GenericOption& option) const
178 {
179 return option.value.get() == value.get();
180 }
181
186 bool operator!=(const GenericOption& option) const
187 {
188 return option.value.get() != value.get();
189 }
190
191 };
192
195 class NDEVR_BASE_API GenericOptionGroup
196 {
197 public:
207 GenericOptionGroup(const TranslatedString& group_name, const StringView& group_id = StringView());
218 template<class t_type>
219 void addOption(const TranslatedString& name, const t_type& value, bool is_editable = true)
220 {
221 GenericOption option(name.readableID(), name, value);
222 option.is_editable = is_editable;
223 addOption(option);
224 }
225
229 void addOption(const GenericOption& option)
230 {
231 lib_assert(!options.hasKey(option.id), "Bad Option");
232 options.add(option.id, option);
233 ordered_options.add(option.id);
234 };
235
240 bool hasOption(const TranslatedString& option_name) const
241 {
242 return options.hasKey(option_name.readableID());
243 }
244
249 bool hasOption(const StringView& option_name) const
250 {
251 return options.hasKey(option_name);
252 }
253
259 template<class t_type>
260 void setValue(const TranslatedString& name, const t_type& value)
261 {
262 lib_assert(options.hasKey(name.readableID()), "Bad Option");
263 options[name.readableID()].value.set(String(value));
264 }
265
270 template<class t_type>
271 void setValue(const StringView& name, const t_type& value)
272 {
273 lib_assert(options.hasKey(name), "Bad Option");
274 options.get(name).value.set(String(value));
275 }
276
280 void setOption(const GenericOption& option)
281 {
282 lib_assert(options.hasKey(option.id), "Bad Option");
283 if (!options.hasKey(option.id))
284 ordered_options.add(option.id);
285 options[option.id] = option;
286
287 }
288
294 {
295 return options.get(n.readableID());
296 }
297
303 {
304 return options.get(n.readableID());
305 }
306
312 template<class t_type>
313 t_type getValue(const TranslatedString& name) const
314 {
315 return options.get(name.readableID()).getAs<t_type>();
316 }
317
322 template<class t_type>
323 t_type getValue(const StringView& name) const
324 {
325 return options.get(name).getAs<t_type>();
326 }
327
334 template<class t_type>
335 t_type getValue(const TranslatedString& name, const t_type& value_if_not_exist) const
336 {
337 auto iter = options.find(name.readableID());
338 if (iter != options.end())
339 return iter.value().getAs<t_type>();
340 else
341 return value_if_not_exist;
342 }
343
349 template<class t_type>
350 t_type getValue(const StringView& name, const t_type& value_if_not_exist) const
351 {
352 auto iter = options.find(name);
353 if (iter != options.end())
354 return iter.value().getAs<t_type>();
355 else
356 return value_if_not_exist;
357 }
358
363 bool operator==(const GenericOptionGroup& option_group) const;
369 bool operator!=(const GenericOptionGroup& option_group) const;
385 [[nodiscard]] virtual JSONNode toJSONNode(bool only_value = true) const;
390 virtual void fromJSONNode(const JSONNode& node);
394 void clear();
399 };
400}
A specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:54
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
Provides a constant, unmodifiable pointer that has shared ownership of a dynamically allocated object...
Definition Pointer.hpp:276
A hash-based key-value store, useful for quick associative lookups.
Definition Dictionary.h:64
virtual void fromJSONNode(const JSONNode &node)
Deserializes this option group from a JSON node.
t_type getValue(const StringView &name, const t_type &value_if_not_exist) const
Retrieves the value of an option by string identifier, returning a fallback if the option does not ex...
void setOption(const GenericOption &option)
Replaces an existing option with the given GenericOption, matched by its identifier.
void setValue(const StringView &name, const t_type &value)
Sets the value of an existing option identified by its string name.
TranslatedString name
The user-facing display name of this option group.
GenericOption & getOption(const TranslatedString &n)
Retrieves a mutable reference to the option with the given translated name.
Dictionary< String, GenericOption > options
The dictionary mapping option identifiers to their GenericOption instances.
String id
The unique identifier for this option group.
void addOption(const GenericOption &option)
Adds a fully configured GenericOption to this group.
void addOptionsToINI(INIFactory &factory)
Adds all options in this group to an existing INIFactory for serialization.
INIFactory ini()
Creates an INIFactory for serializing this option group to INI format.
const GenericOption & getOption(const TranslatedString &n) const
Retrieves a const reference to the option with the given translated name.
bool operator!=(const GenericOptionGroup &option_group) const
Checks inequality between two option groups.
bool operator==(const GenericOptionGroup &option_group) const
Checks equality between two option groups by comparing all option values.
Buffer< String > ordered_options
The insertion-ordered list of option identifiers for preserving display order.
virtual ~GenericOptionGroup()
Virtual destructor.
void setValue(const TranslatedString &name, const t_type &value)
Sets the value of an existing option identified by its translated name.
bool hasOption(const StringView &option_name) const
Checks whether an option with the given string identifier exists in this group.
t_type getValue(const TranslatedString &name, const t_type &value_if_not_exist) const
Retrieves the value of an option by translated name, returning a fallback if the option does not exis...
t_type getValue(const TranslatedString &name) const
Retrieves the value of an option converted to the requested type, looked up by translated name.
void clear()
Removes all options from this group.
virtual JSONNode toJSONNode(bool only_value=true) const
Serializes this option group to a JSON node.
void addOption(const TranslatedString &name, const t_type &value, bool is_editable=true)
Adds a new option to this group with the given name and typed value.
GenericOptionGroup()=default
Default constructor.
t_type getValue(const StringView &name) const
Retrieves the value of an option converted to the requested type, looked up by string identifier.
bool hasOption(const TranslatedString &option_name) const
Checks whether an option with the given translated name exists in this group.
GenericOptionGroup(const TranslatedString &group_name, const StringView &group_id=StringView())
Constructs an option group with the given name and optional identifier.
Contains methods for easily reading and writing to an INI file including efficient casting,...
Definition INIReader.h:107
A light-weight base class for Log that allows processes to update, without the need for additional in...
JavaScript Object Notation or JSON is an open - standard file format that uses human - readable text ...
Definition JSONParser.h:149
A core part of the engine, stores variables that can be listened to with ResourceListener which will ...
Definition Resource.h:42
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
const String readableID() const
Returns a human-readable identifier for this translated string, useful for debugging.
Stores information about a type, relevant for certain templated functions.
Definition TypeInfo.h:43
The primary namespace for the NDEVR SDK.
constexpr std::enable_if<!ObjectInfo< t_type >::Buffer, TypeInfo >::type GetTypeInfo()
Constructs a TypeInfo for a non-buffer type at compile time using ObjectInfo traits.
Definition TypeInfo.h:125
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Stores a generic option of any type as well as some information about how the user might interact wit...
GenericOption(const GenericOption &option)
Copy constructor.
GenericOption(const StringView &id, const TranslatedString &name, const t_type &value)
Constructs a GenericOption with the given identifier, display name, and typed value.
Bounds< 1, fltp08 > value_bounds
The numeric bounds constraining valid values for this option.
bool is_create_file
Whether this option represents a file path for creating a new file.
bool is_viewable
Whether this option is visible in the user interface.
String id
A unique identifier string for this option.
Resource< String > value
The current value of the option stored as a string Resource.
bool allow_multi_line
Whether the option value supports multi-line text input.
TypeInfo parameter_type
The type information describing the underlying data type of this option.
void setupForDelimiter()
Sets this option up for being a delimiter, commas, tabs, etc or custom.
TranslatedString description
A user-facing description of what this option controls.
bool is_password
Whether this option represents a password and should be masked.
void setupForFolder()
Sets this option up for being a folder, including the regax and extension.
void setToDefault()
Resets the option value back to its default.
GenericOption(const StringView &id, const TranslatedString &name, const StringView &value)
Constructs a GenericOption with the given identifier, display name, and string value.
TranslatedString name
The user-facing display name of the option.
String password_encode_key
The key used for password encoding when this option is a password.
void setupForFile(const StringView &extensions=StringView())
Sets this option up for being a file, including the regax and extension.
String file_extensions
Allowed file extensions for file-type options. If blank, the option is not a file.
String regex
A regular expression used to validate the option value.
GenericOption & operator=(const GenericOption &option)
Copy assignment operator.
void set(const String &new_value)
Sets the option value from a String.
bool is_editable
Whether the user is allowed to edit this option.
String default_value
The default value to revert to when resetting the option.
bool operator!=(const GenericOption &option) const
Checks inequality by comparing the current string values of two options.
Buffer< std::pair< TranslatedString, StringAllocatingView > > available_options
The set of predefined choices available for this option.
ConstPointer< Unit > unit
The unit of measurement associated with this option value.
void set(const StringView &new_value)
Sets the option value from a StringView.
bool allow_custom_option
Whether the user may enter a custom value outside the predefined choices.
t_type getAs() const
Retrieves the current value converted to the requested type.
bool operator==(const GenericOption &option) const
Checks equality by comparing the current string values of two options.
void set(const t_type &new_value)
Sets the option value from the given typed value by converting it to a String.
bool show_resolved_links
Whether to display resolved link paths instead of raw values.
GenericOption()
Constructs a default GenericOption with no values set.
String icon
The icon name associated with this option for display purposes.