13 template <
typename TEnum>
15 for (
auto& entry : list) {
16 SPH_ASSERT(!entries.contains(entry.id),
"Duplicate settings ID ",
int(entry.id));
17 entries.insert(entry.id, entry);
21 template <
typename TEnum>
25 template <
typename TEnum>
28 template <
typename TEnum>
30 : entries(other.entries.clone()) {}
32 template <
typename TEnum>
34 : entries(
std::move(other.entries)) {}
36 template <
typename TEnum>
39 for (
auto& entry : list) {
40 entries.insert(entry.id, entry);
45 template <
typename TEnum>
47 entries = other.entries.
clone();
51 template <
typename TEnum>
53 entries = std::move(other.entries);
57 template <
typename TEnum>
59 const Size typeIdx = defaultValue.getTypeIdx();
60 std::stringstream ss(str.c_str());
67 }
else if (value ==
"true") {
70 }
else if (value ==
"false") {
102 if (s1 ==
"-infinity") {
111 if (s2 ==
"infinity") {
123 entry.value =
Interval(lower, upper);
129 const std::string trimmed =
trim(str);
130 entry.value = trimmed;
135 ss >> v1 >> v2 >> v3;
139 entry.value =
Vector(v1, v2, v3);
143 Float sxx, syy, szz, sxy, sxz, syz;
144 ss >> sxx >> syy >> szz >> sxy >> sxz >> syz;
152 Float txx, tyy, txy, txz, tyz;
153 ss >> txx >> tyy >> txy >> txz >> tyz;
161 const EnumIndex index = defaultValue.template get<EnumWrapper>().index;
162 std::string textValue;
169 if (textValue ==
"0") {
173 if (!ss && flags == 0) {
184 flags |= value.
value();
187 if (!ss || textValue !=
"|") {
199 template <
typename TEnum>
201 std::ifstream ifs(path.
native());
203 return makeFailed(
"File ", path.
native(),
" cannot be opened for reading.");
207 while (std::getline(ifs, line,
'\n')) {
208 if (line.empty() || line[0] ==
'#') {
211 const std::size_t idx = line.find(
"=", 0);
212 if (idx == std::string::npos) {
213 return makeFailed(
"Invalid format of the file, didn't find separating '='");
215 std::string key = line.substr(0, idx);
216 std::string value = line.substr(idx + 1);
218 std::string trimmedKey =
trim(key);
222 for (
auto& e : descriptors.entries) {
223 if (e.value.name == trimmedKey) {
224 entries.insert(e.value.id, e.value);
225 if (!setValueByType(entries[e.value.id], e.value.value, value)) {
226 return makeFailed(
"Invalid value of key ", trimmedKey,
": ", value);
233 return makeFailed(
"Key ", trimmedKey,
" was not find in settings");
240 template <
typename TEnum>
247 const Settings& descriptors = getDefaults();
249 std::ofstream ofs(path.
native());
250 for (
auto& e : entries) {
251 const Entry& entry = e.value;
252 const Entry& descriptor = descriptors.entries[e.key];
253 if (!descriptor.desc.empty()) {
254 std::string desc =
"# " + descriptor.desc;
257 ofs << desc << std::endl;
260 ofs << std::setw(30) << std::left << descriptor.name <<
" = ";
261 switch (entry.value.getTypeIdx()) {
263 ofs << (entry.value.template get<bool>() ?
"true" :
"false");
266 ofs << entry.value.template get<int>();
269 ofs << entry.value.template get<Float>();
272 ofs << entry.value.template get<Interval>();
275 ofs << entry.value.template get<std::string>();
278 ofs << entry.value.template get<Vector>();
281 ofs << entry.value.template get<SymmetricTensor>();
284 ofs << entry.value.template get<TracelessTensor>();
298 }
catch (
const std::exception& e) {
299 return makeFailed(
"Cannot save settings: ", e.what());
303 template <
typename TEnum>
307 const Outcome result = this->loadFromFile(path);
311 this->addEntries(overrides);
315 this->addEntries(overrides);
321 template <
typename TEnum>
334 if (type >= 0 && type <
int(names.
size())) {
337 throw Exception(
"Unknown settings type " + std::to_string(type));
341 template <
typename TEnum>
346 template <
typename TEnum>
351 template <
typename TEnum>
353 return entries.size();
356 template <
typename TEnum>
362 template <
typename TEnum>
366 template <
typename TEnum>
368 return { iter->key, iter->
value.value };
371 template <
typename TEnum>
377 template <
typename TEnum>
379 return iter == other.iter;
382 template <
typename TEnum>
384 return iter != other.iter;
#define SPH_ASSERT(x,...)
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
void saveToFile(const wxBitmap &wx, const Path &path)
uint32_t Size
Integral type used to index arrays (by default).
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
#define NAMESPACE_SPH_END
Return value of function that may fail, containing either SUCCEES (true) or error message.
const SuccessTag SUCCESS
Global constant for successful outcome.
INLINE Outcome makeFailed(TArgs &&... args)
Constructs failed object with error message.
std::string setLineBreak(const std::string &s, const Size lineWidth)
Inserts to string so that no line is longer than given limit.
std::string trim(const std::string &s)
Removes all leading and trailing spaces from a string.
std::string replaceAll(const std::string &source, const std::string &old, const std::string &s)
Replaces all occurences of string with a new string.
BasicVector< Float > Vector
Helper class used to allow calling a function only from within T.
INLINE const TError & error() const
Returns the error message.
static Optional< TEnum > fromString(const std::string &value)
static std::string toString(const TEnum value)
Object representing a 1D interval of real numbers.
Exception thrown when file cannot be read, it has invalid format, etc.
INLINE Type & value()
Returns the reference to the stored value.
Object representing a path on a filesystem.
std::string native() const
Returns the native version of the path.
Path parentPath() const
Returns the parent directory. If the path is empty or root, return empty path.
Iterator useful for iterating over all entries in the settings.
SettingsIterator & operator++()
Moves to next entry.
bool operator==(const SettingsIterator &other) const
Equality operator between settings operators.
SettingsIterator(const ActIterator &iter, Badge< Settings< TEnum >>)
Constructs an iterator from iternal implementation; use Settings::begin and Settings::end.
IteratorValue operator*() const
Dereference the iterator, yielding a pair of entry ID and its value.
bool operator!=(const SettingsIterator &other) const
Unequality operator between settings operators.
Generic object containing various settings and parameters of the run.
static std::string typeToString(const int type)
Returns the string name for given type index.
static const Settings & getDefaults()
\brief Returns a reference to object containing default values of all settings.
Outcome saveToFile(const Path &path) const
Saves all values stored in settings into file.
Settings & operator=(std::initializer_list< Entry > list)
Assigns a list of settings into the object, erasing all previous entries.
Settings()
Initialize settings by settings all value to their defaults.
SettingsIterator< TEnum > begin() const
Iterator to the first entry of the settings storage.
Size size() const
Returns the number of entries in the settings.
bool tryLoadFileOrSaveCurrent(const Path &path, const Settings &overrides=EMPTY_SETTINGS)
If the specified file exists, loads the settings from it, otherwise creates the file and saves the cu...
Outcome loadFromFile(const Path &path)
Loads the settings from file.
SettingsIterator< TEnum > end() const
Iterator to the one-past-end entry the settings storage.
Array with fixed number of allocated elements.
INLINE TCounter size() const
Returns the current size of the array (number of constructed elements).
Symmetric tensor of 2nd order.
Symmetric traceless 2nd order tensor.
Generic storage and input/output routines of settings.
bool pathExists(const Path &path)
Checks if a file or directory exists (or more precisely, if a file or directory is accessible).
Outcome createDirectory(const Path &path, const Flags< CreateDirectoryFlag > flags=CreateDirectoryFlag::ALLOW_EXISTING)
Creates a directory with given path. Creates all parent directories as well.
Overload of std::swap for Sph::Array.
Tag for initialization of empty settings object.
Settings< TEnum >::Value value
Variant holding the value of the entry.