SPH
VirtualSettings.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "io/Path.h"
11 #include "system/Settings.h"
12 
14 
20 class IExtraEntry : public Polymorphic {
21 public:
22  virtual std::string toString() const = 0;
23 
24  virtual void fromString(const std::string& s) = 0;
25 
27  virtual AutoPtr<IExtraEntry> clone() const = 0;
28 };
29 
31 class ExtraEntry {
32 public:
34 
35 public:
37  : entry(std::move(entry)) {}
38 
39  ExtraEntry(const ExtraEntry& other)
40  : entry(other.entry->clone()) {}
41 
42  ExtraEntry& operator=(const ExtraEntry& other) {
43  entry = other.entry->clone();
44  return *this;
45  }
46 
47  std::string toString() const {
48  return entry->toString();
49  }
50 
51  void fromString(const std::string& s) const {
52  entry->fromString(s);
53  }
54 
56  return entry.get();
57  }
58 };
59 
64 class IVirtualEntry : public Polymorphic {
65 public:
66  enum class Type { BOOL, INT, FLOAT, VECTOR, INTERVAL, STRING, PATH, ENUM, EXTRA, FLAGS };
67 
69 
74  virtual bool enabled() const = 0;
75 
81  virtual bool set(const Value& value) = 0;
82 
84  virtual Value get() const = 0;
85 
87  virtual bool isValid(const Value& value) const = 0;
88 
90  virtual Type getType() const = 0;
91 
97  virtual std::string getName() const = 0;
98 
100  virtual std::string getTooltip() const {
101  return "";
102  }
103 
109  virtual bool hasSideEffect() const {
110  return false;
111  }
112 
113  enum class PathType {
114  DIRECTORY,
115  INPUT_FILE,
116  OUTPUT_FILE,
117  };
118 
122  virtual Optional<PathType> getPathType() const {
123  return NOTHING;
124  }
125 
126  struct FileFormat {
127  std::string description;
128  std::string extension;
129  };
130 
136  return {};
137  }
138 };
139 
149 class EntryControl : public IVirtualEntry {
150 public:
151  using Enabler = Function<bool()>;
152  using Accessor = Function<void(const Value& newValue)>;
153  using Validator = Function<bool(const Value& newValue)>;
154 
155 protected:
156  std::string tooltip;
157  Float mult = 1._f;
160  Enabler enabler = nullptr;
162  Validator validator = nullptr;
163  bool sideEffect = false;
164 
165 public:
167  EntryControl& setTooltip(const std::string& newTooltip);
168 
173  EntryControl& setUnits(const Float newMult);
174 
182  EntryControl& setEnabler(const Enabler& newEnabler);
183 
189  EntryControl& addAccessor(const SharedToken& owner, const Accessor& accessor);
190 
194  EntryControl& setValidator(const Validator& newValidator);
195 
198 
200  EntryControl& setPathType(const PathType& newType);
201 
204 
205 protected:
206  virtual bool enabled() const override final;
207 
208  virtual std::string getTooltip() const override final;
209 
210  virtual bool hasSideEffect() const override final;
211 
212  virtual Optional<PathType> getPathType() const override final;
213 
214  virtual Array<FileFormat> getFileFormats() const override final;
215 
216  virtual bool isValid(const Value& value) const override final;
217 
218  virtual void setImpl(const Value& value) = 0;
219 
220 private:
221  virtual bool set(const Value& value) override final;
222 };
223 
230 public:
231  class Category {
232  friend class VirtualSettings;
233 
234  private:
236 
237  public:
242  void addEntry(const std::string& key, AutoPtr<IVirtualEntry>&& entry);
243 
245  template <typename TValue>
246  EntryControl& connect(const std::string& name, const std::string& key, TValue& value);
247 
249  template <typename TValue, typename TEnum>
250  EntryControl& connect(const std::string& name, Settings<TEnum>& settings, const TEnum id);
251  };
252 
253 
254 private:
256 
257 public:
264  void set(const std::string& key, const IVirtualEntry::Value& value);
265 
277  template <typename TEnum, typename = std::enable_if_t<std::is_enum<TEnum>::value>>
278  void set(const TEnum id, const IVirtualEntry::Value& value);
279 
284  IVirtualEntry::Value get(const std::string& key) const;
285 
290  Category& addCategory(const std::string& name);
291 
293  class IEntryProc : public Polymorphic {
294  public:
299  virtual void onCategory(const std::string& name) const = 0;
300 
305  virtual void onEntry(const std::string& key, IVirtualEntry& entry) const = 0;
306  };
307 
309  void enumerate(const IEntryProc& proc);
310 };
311 
314 
317 
319 
320 #include "run/VirtualSettings.inl.h"
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Container of callbacks.
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
const NothingType NOTHING
Definition: Optional.h:16
Object representing a path on a filesystem, similar to std::filesystem::path in c++17.
Key-value associative container.
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.
Generic dynamically allocated resizable storage.
Definition: Array.h:43
INLINE RawPtr< T > get() const
Definition: AutoPtr.h:69
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.
Copyable wrapper of a IExtraEntry.
ExtraEntry(const ExtraEntry &other)
AutoPtr< IExtraEntry > entry
void fromString(const std::string &s) const
std::string toString() const
ExtraEntry(AutoPtr< IExtraEntry > &&entry)
RawPtr< IExtraEntry > getEntry() const
ExtraEntry & operator=(const ExtraEntry &other)
Provides an interface for implementing new types of entries.
virtual std::string toString() const =0
virtual void fromString(const std::string &s)=0
virtual AutoPtr< IExtraEntry > clone() const =0
Represents a virtual entry in the settings.
virtual bool set(const Value &value)=0
Modifies the current value of the entry.
virtual Array< FileFormat > getFileFormats() const
Returns the allowed file format for this file entry.
virtual bool isValid(const Value &value) const =0
Checks if the given value is a valid input for this entry.
virtual std::string getTooltip() const
Returns an optional description of the entry.
virtual Optional< PathType > getPathType() const
Returns the type of the path.
virtual std::string getName() const =0
Returns a descriptive name of the entry.
virtual bool hasSideEffect() const
Returns true if the entry can modify multiple values simultaneously.
virtual Value get() const =0
Returns the currently stored value.
virtual Type getType() const =0
Returns the type of this entry.
virtual bool enabled() const =0
Returns if this entry is currently enabled.
Non-owning wrapper of pointer.
Definition: RawPtr.h:19
Generic object containing various settings and parameters of the run.
Definition: Settings.h:108
Variant, an implementation of type-safe union, similar to std::variant or boost::variant.
Definition: Variant.h:171
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.
Holds a map of virtual entries, associated with a unique name.
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.
Generic storage and input/output routines of settings.
Overload of std::swap for Sph::Array.
Definition: Array.h:578
Base class for all polymorphic objects.
Definition: Object.h:88