SPH
VirtualSettings.inl.h
Go to the documentation of this file.
1 #include "run/VirtualSettings.h"
2 
4 
5 namespace Detail {
6 
7 template <typename T, typename = void>
8 class UnitAdapter {
9 public:
10  INLINE const T& get(const T& input, const Float mult) const {
11  SPH_ASSERT(mult == 1._f, "Units not implemented for entries other than float or vector");
12  return input;
13  }
14 
15  INLINE const T& set(const T& input, const Float mult) const {
16  SPH_ASSERT(mult == 1._f, "Units not implemented for entries other than float or vector");
17  return input;
18  }
19 };
20 
21 template <typename T>
22 class UnitAdapter<T, std::enable_if_t<std::is_same<T, Float>::value || std::is_same<T, Vector>::value>> {
23 public:
24  INLINE T get(const T& input, const Float mult) const {
25  return input / mult;
26  }
27 
28  INLINE T set(const T& input, const Float mult) const {
29  return input * mult;
30  }
31 };
32 
33 } // namespace Detail
34 
35 inline void VirtualSettings::Category::addEntry(const std::string& key, AutoPtr<IVirtualEntry>&& entry) {
36  entries.insert(key, std::move(entry));
37 }
38 
39 namespace Detail {
40 
41 template <typename TValue, typename = void>
42 class ValueEntry : public EntryControl {
43 private:
44  TValue& ref;
45  std::string name;
46 
47 public:
48  ValueEntry(TValue& ref, const std::string& name)
49  : ref(ref)
50  , name(name) {}
51 
52  virtual void setImpl(const Value& value) override {
53  UnitAdapter<TValue> adapter;
54  ref = adapter.set(value.get<TValue>(), mult);
55  }
56 
57  virtual Value get() const override {
58  UnitAdapter<TValue> adapter;
59  return adapter.get(ref, mult);
60  }
61 
62  virtual Type getType() const override {
63  // path has to be included to get correct index of EnumWrapper
64  return Type(getTypeIndex<TValue,
65  bool,
66  int,
67  Float,
68  Vector,
69  Interval,
70  std::string,
71  Path,
73  ExtraEntry>);
74  }
75 
76  virtual std::string getName() const override {
77  return name;
78  }
79 };
80 
81 template <typename TValue>
82 class ValueEntry<TValue, std::enable_if_t<FlagsTraits<TValue>::isFlags>> : public EntryControl {
83 private:
84  TValue& ref;
85  std::string name;
86 
87  using TEnum = typename FlagsTraits<TValue>::Type;
88 
89 public:
90  ValueEntry(TValue& ref, const std::string& name)
91  : ref(ref)
92  , name(name) {}
93 
94  virtual void setImpl(const Value& value) override {
95  ref = TValue::fromValue(value.get<EnumWrapper>().value);
96  }
97 
98  virtual Value get() const override {
99  return EnumWrapper(TEnum(ref.value()));
100  }
101 
102  virtual Type getType() const override {
103  return Type::FLAGS;
104  }
105 
106  virtual std::string getName() const override {
107  return name;
108  }
109 };
110 
111 } // namespace Detail
112 
113 template <typename TValue>
114 inline EntryControl& VirtualSettings::Category::connect(const std::string& name,
115  const std::string& key,
116  TValue& value) {
117  auto entry = makeAuto<Detail::ValueEntry<TValue>>(value, name);
118  EntryControl& control = *entry;
119  entries.insert(key, std::move(entry));
120  return control;
121 }
122 
123 
124 namespace Detail {
125 
126 template <typename TEnum>
127 inline std::string makeTooltip(const TEnum id) {
130  std::string scriptTooltip;
131  if (key) {
132  std::string typeName = Settings<TEnum>::typeToString(type.value());
133  scriptTooltip = "Script name: " + key.value() + " (" + typeName + ")";
134  }
135 
137  if (desc) {
138  std::string tooltip = desc.value();
139  if (key) {
140  tooltip += "\n\n" + scriptTooltip;
141  }
142  return tooltip;
143  } else {
144  return scriptTooltip;
145  }
146 }
147 
148 template <typename TValue, typename TEnum, typename TEnabler = void>
149 class SettingsEntry : public EntryControl {
150 private:
151  Settings<TEnum>& settings;
152  std::string name;
153  TEnum id;
154 
155 public:
156  SettingsEntry(Settings<TEnum>& settings, const TEnum id, const std::string& name)
157  : settings(settings)
158  , name(name)
159  , id(id) {
160  tooltip = makeTooltip(id);
161  }
162 
163  virtual void setImpl(const Value& value) override {
164  UnitAdapter<TValue> adapter;
165  settings.set(id, adapter.set(value.get<TValue>(), mult));
166  }
167 
168  virtual Value get() const override {
169  UnitAdapter<TValue> adapter;
170  return adapter.get(settings.template get<TValue>(id), mult);
171  }
172 
173  virtual Type getType() const override {
174  return Type(getTypeIndex<TValue, bool, int, Float, Vector, Interval, std::string, Path, EnumWrapper>);
175  }
176 
177  virtual std::string getName() const override {
178  return name;
179  }
180 };
181 
183 template <typename TValue, typename TEnum>
184 class SettingsEntry<TValue, TEnum, std::enable_if_t<FlagsTraits<TValue>::isFlags>> : public EntryControl {
185 private:
186  Settings<TEnum>& settings;
187  std::string name;
188  TEnum id;
189 
190 public:
191  SettingsEntry(Settings<TEnum>& settings, const TEnum id, const std::string& name)
192  : settings(settings)
193  , name(name)
194  , id(id) {
195  tooltip = makeTooltip(id);
196  }
197 
198  virtual void setImpl(const Value& value) override {
199  settings.set(id, value.get<EnumWrapper>());
200  }
201 
202  virtual Value get() const override {
203  return settings.template get<EnumWrapper>(id);
204  }
205 
206  virtual Type getType() const override {
207  return Type::FLAGS;
208  }
209 
210  virtual std::string getName() const override {
211  return name;
212  }
213 };
214 
216 template <typename TEnum>
217 class SettingsEntry<Path, TEnum> : public EntryControl {
218 private:
219  Settings<TEnum>& settings;
220  std::string name;
221  TEnum id;
222 
223 public:
224  SettingsEntry(Settings<TEnum>& settings, const TEnum id, const std::string& name)
225  : settings(settings)
226  , name(name)
227  , id(id) {
228  tooltip = makeTooltip(id);
229  }
230 
231  virtual void setImpl(const Value& value) override {
232  settings.set(id, value.get<Path>().native());
233  }
234 
235  virtual Value get() const override {
236  return Path(settings.template get<std::string>(id));
237  }
238 
239  virtual Type getType() const override {
240  return Type::PATH;
241  }
242 
243  virtual std::string getName() const override {
244  return name;
245  }
246 };
247 
248 
249 } // namespace Detail
250 
251 template <typename TValue, typename TEnum>
253  Settings<TEnum>& settings,
254  const TEnum id) {
256  if (!key) {
257  throw InvalidSetup("No settings entry with id " + std::to_string(int(id)));
258  }
259  auto entry = makeAuto<Detail::SettingsEntry<TValue, TEnum>>(settings, id, name);
260  EntryControl& control = *entry;
261  entries.insert(key.value(), std::move(entry));
262  return control;
263 }
264 
265 
266 template <typename TEnum, typename>
267 void VirtualSettings::set(const TEnum id, const IVirtualEntry::Value& value) {
269  if (!key) {
270  throw InvalidSetup("No entry with ID " + std::to_string(int(id)));
271  }
272 
273  this->set(key.value(), value);
274 }
275 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
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 INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
constexpr int getTypeIndex
Definition: Traits.h:41
Object providing connection between component parameters and values exposed to the user.
Wrapper of pointer that deletes the resource from destructor.
Definition: AutoPtr.h:15
virtual std::string getName() const override
Returns a descriptive name of the entry.
virtual Value get() const override
Returns the currently stored value.
SettingsEntry(Settings< TEnum > &settings, const TEnum id, const std::string &name)
virtual Type getType() const override
Returns the type of this entry.
virtual void setImpl(const Value &value) override
SettingsEntry(Settings< TEnum > &settings, const TEnum id, const std::string &name)
virtual std::string getName() const override
Returns a descriptive name of the entry.
virtual Value get() const override
Returns the currently stored value.
virtual std::string getName() const override
Returns a descriptive name of the entry.
virtual Value get() const override
Returns the currently stored value.
SettingsEntry(Settings< TEnum > &settings, const TEnum id, const std::string &name)
virtual void setImpl(const Value &value) override
virtual Type getType() const override
Returns the type of this entry.
INLINE const T & set(const T &input, const Float mult) const
INLINE const T & get(const T &input, const Float mult) const
virtual Value get() const override
Returns the currently stored value.
virtual Type getType() const override
Returns the type of this entry.
virtual std::string getName() const override
Returns a descriptive name of the entry.
virtual void setImpl(const Value &value) override
virtual std::string getName() const override
Returns a descriptive name of the entry.
virtual Value get() const override
Returns the currently stored value.
virtual Type getType() const override
Returns the type of this entry.
ValueEntry(TValue &ref, const std::string &name)
Helper object, allowing to add units, tooltips and additional properties into the entry created with ...
Copyable wrapper of a IExtraEntry.
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
Thrown when components of the run are mutually incompatible.
Definition: Exceptions.h:24
INLINE Type & value()
Returns the reference to the stored value.
Definition: Optional.h:172
Object representing a path on a filesystem.
Definition: Path.h:17
std::string native() const
Returns the native version of the path.
Definition: Path.cpp:71
Generic object containing various settings and parameters of the run.
Definition: Settings.h:108
static Optional< std::string > getEntryDesc(const TEnum idx)
Returns a description of the entry with given index.
Definition: Settings.h:389
static std::string typeToString(const int type)
Returns the string name for given type index.
static Optional< std::string > getEntryName(const TEnum idx)
Returns the human-readable name of the entry with given index.
Definition: Settings.h:357
Settings & set(const TEnum idx, TValue &&value, std::enable_if_t<!std::is_enum< std::decay_t< TValue >>::value, int >=0)
Saves a value into the settings.
Definition: Settings.h:226
static Optional< int > getEntryType(const TEnum idx)
Returns the type of the entry with given index.
Definition: Settings.h:371
INLINE TValue & insert(const TKey &key, const TValue &value)
Adds a new element into the map or sets new value of element with the same key.
Definition: UnorderedMap.h:51
Variant, an implementation of type-safe union, similar to std::variant or boost::variant.
Definition: Variant.h:171
INLINE T & get()
Returns the stored value.
Definition: Variant.h:335
void addEntry(const std::string &key, AutoPtr< IVirtualEntry > &&entry)
Manually adds a new entry into the settings.
EntryControl & connect(const std::string &name, const std::string &key, TValue &value)
Connects to given reference.
void set(const std::string &key, const IVirtualEntry::Value &value)
Modifies an existing entry in the settings.
std::string makeTooltip(const TEnum id)
Overload of std::swap for Sph::Array.
Definition: Array.h:578
Wrapper of an enum.
Definition: Settings.h:37
int value
Definition: Settings.h:38