SPH
Config.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "io/Path.h"
9 #include "objects/Exceptions.h"
14 
16 
18 
25 class IConfigValue : public Polymorphic {
26 public:
28  virtual std::string write() const = 0;
29 
33  virtual void read(const std::string& source) = 0;
34 };
35 
37 std::string quoted(const std::string& value);
38 
40 std::string unquoted(const std::string& value);
41 
47 template <typename Type>
48 class ConfigValue : public IConfigValue {
49 private:
50  Type value;
51 
52 public:
53  ConfigValue() = default;
54 
55  ConfigValue(const Type& value)
56  : value(value) {}
57 
58  virtual std::string write() const override {
59  std::stringstream ss;
60  ss << value;
61  return ss.str();
62  }
63 
64  virtual void read(const std::string& source) override {
65  std::stringstream ss(trim(source));
66  ss >> value;
67 
68  if (!ss || !ss.eof()) {
69  throw ConfigException("Invalid type");
70  }
71  }
72 
73  Type get() const {
74  return value;
75  }
76 };
77 
81 template <>
82 class ConfigValue<Vector> : public IConfigValue {
83 private:
84  Vector value;
85 
86 public:
87  ConfigValue() = default;
88 
89  ConfigValue(const Vector& value)
90  : value(value) {}
91 
92  virtual std::string write() const override {
93  std::stringstream ss;
94  ss << value;
95  return ss.str();
96  }
97 
98  virtual void read(const std::string& source) override {
99  std::stringstream ss(source);
100  ss >> value[X] >> value[Y] >> value[Z];
101  }
102 
103  Vector get() const {
104  return value;
105  }
106 };
107 
111 template <>
113 private:
114  Interval value;
115 
116 public:
117  ConfigValue() = default;
118 
119  ConfigValue(const Interval& value)
120  : value(value) {}
121 
122  virtual std::string write() const override {
123  std::stringstream ss;
124  ss << value;
125  return ss.str();
126  }
127 
128  virtual void read(const std::string& source) override {
129  std::stringstream ss(source);
130  Float lower, upper;
131  ss >> lower >> upper;
132  value = Interval(lower, upper);
133  }
134 
135  Interval get() const {
136  return value;
137  }
138 };
139 
143 template <>
144 class ConfigValue<std::string> : public IConfigValue {
145 private:
146  std::string value;
147 
148 public:
149  ConfigValue() = default;
150 
151  ConfigValue(const std::string& value)
152  : value(value) {}
153 
154  virtual std::string write() const override {
155  return quoted(value);
156  }
157 
158  virtual void read(const std::string& source) override {
159  value = unquoted(source);
160  }
161 
162  const std::string& get() const {
163  return value;
164  }
165 };
166 
170 template <>
171 class ConfigValue<Path> : public IConfigValue {
172 private:
173  Path value;
174 
175 public:
176  ConfigValue() = default;
177 
178  ConfigValue(const Path& value)
179  : value(value) {}
180 
181  virtual std::string write() const override {
182  return quoted(value.native());
183  }
184 
185  virtual void read(const std::string& source) override {
186  value = Path(unquoted(source));
187  }
188 
189  Path get() const {
190  return value;
191  }
192 };
193 
198 class ConfigNode {
199  friend class Config;
200 
201 private:
204 
207 
208 public:
210  template <typename Type>
211  void set(const std::string& name, const Type& value) {
212  ConfigValue<Type> writer(value);
213  entries.insert(name, writer.write());
214  }
215 
219  template <typename Type>
220  Type get(const std::string& name) {
221  Optional<Type> opt = this->tryGet<Type>(name);
222  if (!opt) {
223  throw ConfigException("Entry '" + name + "' not in config");
224  }
225  return opt.value();
226  }
227 
231  template <typename Type>
232  Optional<Type> tryGet(const std::string& name) {
233  auto value = entries.tryGet(name);
234  if (!value) {
235  return NOTHING;
236  }
237  ConfigValue<Type> reader;
238  reader.read(value.value());
239  return reader.get();
240  }
241 
243  bool contains(const std::string& name) {
244  return entries.contains(name);
245  }
246 
248  Size size() const;
249 
251  SharedPtr<ConfigNode> addChild(const std::string& name);
252 
256  SharedPtr<ConfigNode> getChild(const std::string& name);
257 
259  void enumerateChildren(Function<void(std::string name, ConfigNode& node)> func);
260 
261 private:
263  void write(const std::string& padding, std::stringstream& source);
264 
266  void read(std::stringstream& source);
267 };
268 
272 class Config {
273 private:
275 
276 public:
278  SharedPtr<ConfigNode> addNode(const std::string& name);
279 
283  SharedPtr<ConfigNode> getNode(const std::string& name);
284 
286  SharedPtr<ConfigNode> tryGetNode(const std::string& name);
287 
292  void read(std::stringstream& source);
293 
295  std::string write();
296 
298  void load(const Path& path);
299 
301  void save(const Path& path);
302 
304  void enumerate(Function<void(std::string, ConfigNode&)> func);
305 };
306 
307 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
std::string unquoted(const std::string &value)
Removes leading and trailing quote from a string.
Definition: Config.cpp:12
Exception ConfigException
Definition: Config.h:17
std::string quoted(const std::string &value)
Helper function wrapping a string by quotes.
Definition: Config.cpp:8
Generic wrappers of lambdas, functors and other callables.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
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.
std::string trim(const std::string &s)
Removes all leading and trailing spaces from a string.
Definition: StringUtils.cpp:74
Key-value associative container.
Basic vector algebra. Computations are accelerated using SIMD.
@ Y
Definition: Vector.h:23
@ X
Definition: Vector.h:22
@ Z
Definition: Vector.h:24
Represents a single node in the hierarchy written into config file.
Definition: Config.h:198
void enumerateChildren(Function< void(std::string name, ConfigNode &node)> func)
Calls the provided functor for each child node.
Definition: Config.cpp:36
Optional< Type > tryGet(const std::string &name)
Tries to return a value stored in the node.
Definition: Config.h:232
Type get(const std::string &name)
Returns a value stored in the node.
Definition: Config.h:220
Size size() const
Returns the number of entries stored in the node.
Definition: Config.cpp:32
bool contains(const std::string &name)
Checks if the node contains an entry of given name.
Definition: Config.h:243
void set(const std::string &name, const Type &value)
Adds a new value into the node.
Definition: Config.h:211
SharedPtr< ConfigNode > getChild(const std::string &name)
Returns a child node.
Definition: Config.cpp:25
SharedPtr< ConfigNode > addChild(const std::string &name)
Adds a new child node to this node.
Definition: Config.cpp:21
virtual void read(const std::string &source) override
Reads the value from string and stores it internally.
Definition: Config.h:128
virtual std::string write() const override
Writes the value into a string.
Definition: Config.h:122
Interval get() const
Definition: Config.h:135
ConfigValue(const Interval &value)
Definition: Config.h:119
Path get() const
Definition: Config.h:189
ConfigValue(const Path &value)
Definition: Config.h:178
virtual std::string write() const override
Writes the value into a string.
Definition: Config.h:181
virtual void read(const std::string &source) override
Reads the value from string and stores it internally.
Definition: Config.h:185
ConfigValue(const Vector &value)
Definition: Config.h:89
virtual void read(const std::string &source) override
Reads the value from string and stores it internally.
Definition: Config.h:98
virtual std::string write() const override
Writes the value into a string.
Definition: Config.h:92
Vector get() const
Definition: Config.h:103
const std::string & get() const
Definition: Config.h:162
virtual std::string write() const override
Writes the value into a string.
Definition: Config.h:154
virtual void read(const std::string &source) override
Reads the value from string and stores it internally.
Definition: Config.h:158
ConfigValue(const std::string &value)
Definition: Config.h:151
Generic implementation of IConfigValue, using std::stringstream for the (de)serialization.
Definition: Config.h:48
Type get() const
Definition: Config.h:73
virtual void read(const std::string &source) override
Reads the value from string and stores it internally.
Definition: Config.h:64
ConfigValue()=default
ConfigValue(const Type &value)
Definition: Config.h:55
virtual std::string write() const override
Writes the value into a string.
Definition: Config.h:58
Provides functionality for reading and writing configuration files.
Definition: Config.h:272
void read(std::stringstream &source)
Deserializes the input string stream into nodes.
Definition: Config.cpp:101
SharedPtr< ConfigNode > addNode(const std::string &name)
Adds a new node to the config.
Definition: Config.cpp:81
SharedPtr< ConfigNode > getNode(const std::string &name)
Returns a node with given name.
Definition: Config.cpp:85
void save(const Path &path)
Serializes all nodes in the config into a file.
Definition: Config.cpp:126
SharedPtr< ConfigNode > tryGetNode(const std::string &name)
Returns a node with given name or nullptr if no such node exists.
Definition: Config.cpp:93
std::string write()
Serializes all nodes in the config into a string.
Definition: Config.cpp:115
void load(const Path &path)
Reads content of given file and deserializes the config from the loaded string.
Definition: Config.cpp:131
void enumerate(Function< void(std::string, ConfigNode &)> func)
Calls the provided functor for all nodes in the config.
Definition: Config.cpp:138
Generic exception.
Definition: Exceptions.h:10
Interface for value written to the config file.
Definition: Config.h:25
virtual void read(const std::string &source)=0
Reads the value from string and stores it internally.
virtual std::string write() const =0
Writes the value into a string.
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
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
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
INLINE bool contains(const TKey &key) const
Returns true if the map contains element of given key.
Definition: UnorderedMap.h:126
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
INLINE Optional< TValue & > tryGet(const TKey &key)
Returns a reference to the value matching the given key, or NOTHING if no such value exists.
Definition: UnorderedMap.h:104
Overload of std::swap for Sph::Array.
Definition: Array.h:578
Base class for all polymorphic objects.
Definition: Object.h:88