SPH
VirtualSettings.cpp
Go to the documentation of this file.
1 #include "run/VirtualSettings.h"
2 
4 
5 EntryControl& EntryControl::setTooltip(const std::string& newTooltip) {
6  tooltip = newTooltip;
7  return *this;
8 }
9 
11  mult = newMult;
12  return *this;
13 }
14 
16  enabler = newEnabler;
17  return *this;
18 }
19 
21  accessors.insert(owner, accessor);
22  return *this;
23 }
24 
26  validator = newValidator;
27  return *this;
28 }
29 
31  sideEffect = true;
32  return *this;
33 }
34 
36  pathType = newType;
37  return *this;
38 }
39 
41  fileFormats = std::move(formats);
42  return *this;
43 }
44 
46  return pathType;
47 }
48 
50  return fileFormats.clone();
51 }
52 
53 bool EntryControl::isValid(const Value& value) const {
54  return !validator || validator(value);
55 }
56 
57 bool EntryControl::enabled() const {
58  return enabler ? enabler() : true;
59 }
60 
61 std::string EntryControl::getTooltip() const {
62  return tooltip;
63 }
64 
66  return sideEffect;
67 }
68 
69 bool EntryControl::set(const Value& value) {
70  if (!this->isValid(value)) {
71  return false;
72  }
73  this->setImpl(value);
74  accessors(value);
75  return true;
76 }
77 
78 
79 void VirtualSettings::set(const std::string& key, const IVirtualEntry::Value& value) {
80  for (auto& category : categories) {
81  auto entry = category.value.entries.tryGet(key);
82  if (entry) {
83  entry.value()->set(value);
84  return;
85  }
86  }
87  throw InvalidSetup("Key '" + key + "' not found");
88 }
89 
90 IVirtualEntry::Value VirtualSettings::get(const std::string& key) const {
91  for (auto& category : categories) {
92  auto entry = category.value.entries.tryGet(key);
93  if (entry) {
94  return entry.value()->get();
95  }
96  }
97  throw InvalidSetup("Key '" + key + "' not found");
98 }
99 
101  return categories.insert(name, Category{});
102 }
103 
105  for (auto& category : categories) {
106  proc.onCategory(category.key);
107  for (auto& entry : category.value.entries) {
108  proc.onEntry(entry.key, *entry.value);
109  }
110  }
111 }
112 
113 static Array<IVirtualEntry::FileFormat> getFormats(const IoCapability capability) {
115  for (IoEnum id : EnumMap::getAll<IoEnum>()) {
116  if (getIoCapabilities(id).has(capability)) {
118  format.description = getIoDescription(id);
119  format.extension = getIoExtension(id).value();
120  formats.push(format);
121  }
122  }
123  return formats;
124 }
125 
127  return getFormats(IoCapability::INPUT);
128 }
129 
131  return getFormats(IoCapability::OUTPUT);
132 }
133 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
#define NAMESPACE_SPH_END
Definition: Object.h:12
Array< IVirtualEntry::FileFormat > getInputFormats()
Convenience function, returning the list of input file formats defined by IoEnum.
Array< IVirtualEntry::FileFormat > getOutputFormats()
Convenience function, returning the list of output file formats defined by IoEnum.
Object providing connection between component parameters and values exposed to the user.
Generic dynamically allocated resizable storage.
Definition: Array.h:43
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
Helper object, allowing to add units, tooltips and additional properties into the entry created with ...
EntryControl & setEnabler(const Enabler &newEnabler)
Adds or replaces the enabler functor of the entry.
EntryControl & setPathType(const PathType &newType)
Sets the type of the path.
Array< FileFormat > fileFormats
virtual bool hasSideEffect() const override final
Returns true if the entry can modify multiple values simultaneously.
virtual Optional< PathType > getPathType() const override final
Returns the type of the path.
CallbackSet< Accessor > accessors
EntryControl & setValidator(const Validator &newValidator)
Adds or replaces the functor called to validate the new value.
Optional< PathType > pathType
EntryControl & setUnits(const Float newMult)
Sets units in which the entry is stored.
virtual Array< FileFormat > getFileFormats() const override final
Returns the allowed file format for this file entry.
EntryControl & setFileFormats(Array< FileFormat > &&formats)
Sets the allowed file formats.
EntryControl & setTooltip(const std::string &newTooltip)
Adds or replaces the previous tooltip associanted with the entry.
Validator validator
virtual bool enabled() const override final
Returns if this entry is currently enabled.
virtual bool isValid(const Value &value) const override final
Checks if the given value is a valid input for this entry.
virtual void setImpl(const Value &value)=0
EntryControl & addAccessor(const SharedToken &owner, const Accessor &accessor)
Adds a functor called when the entry changes, i.e. when set function is called.
virtual std::string getTooltip() const override final
Returns an optional description of the entry.
std::string tooltip
EntryControl & setSideEffect()
Specifies that the entry has a side effect, i.e. in changes values of other entries.
Thrown when components of the run are mutually incompatible.
Definition: Exceptions.h:24
Wrapper of type value of which may or may not be present.
Definition: Optional.h:23
INLINE Type & value()
Returns the reference to the stored value.
Definition: Optional.h:172
Variant, an implementation of type-safe union, similar to std::variant or boost::variant.
Definition: Variant.h:171
Optional< T > tryGet() const
Returns the stored value in the variant.
Definition: Variant.h:371
Interface allowing to enumerate all entries in the settings.
virtual void onEntry(const std::string &key, IVirtualEntry &entry) const =0
Called for every entry in the current category.
virtual void onCategory(const std::string &name) const =0
Called for every category in the settings.
void enumerate(const IEntryProc &proc)
Enumerates all entries stored in the settings.
IVirtualEntry::Value get(const std::string &key) const
Returns a value of an entry.
void set(const std::string &key, const IVirtualEntry::Value &value)
Modifies an existing entry in the settings.
Category & addCategory(const std::string &name)
Creates a new category of entries.
Optional< std::string > getIoExtension(const IoEnum type)
Returns the file extension associated with given IO type.
Definition: Settings.cpp:343
std::string getIoDescription(const IoEnum type)
Returns a short description of the file format.
Definition: Settings.cpp:387
Flags< IoCapability > getIoCapabilities(const IoEnum type)
Returns the capabilities of given file format.
Definition: Settings.cpp:412
IoEnum
Definition: Settings.h:859
IoCapability
Definition: Settings.h:906
@ OUTPUT
The format can be used as file output.
@ INPUT
The format can be used as file input.