1#ifndef DATAIO_CFG_UTILS_HH
2#define DATAIO_CFG_UTILS_HH
7#include <boost/core/demangle.hpp>
9#include <yaml-cpp/yaml.h>
11#include "../core/exceptions.hh"
12#include "../core/string.hh"
13#include "../core/types.hh"
78 std::stringstream
e_msg;
80 e_msg <<
"Got " << boost::core::demangle(
typeid(
e).name()) <<
". ";
87 e_msg <<
"The given node was a Zombie! Check that the key you are "
88 "trying to read from actually exists. ";
90 else if (
not node.Mark().is_null())
95 e_msg <<
"Check that the corresponding line of the config file "
96 "matches the desired read operation or type conversion. ";
100 e_msg <<
"The content of the node is:" << std::endl << YAML::Dump(
node);
103 return YAML::Exception(
node.Mark(),
e_msg.str());
113 s << YAML::Dump(
node);
156template <
typename ReturnType >
164 catch (YAML::Exception&
e)
172 "Could not read key '" +
key +
173 "' from given config node!");
175 catch (std::exception&
e)
178 std::cerr << boost::core::demangle(
typeid(
e).name())
179 <<
" occurred during reading key '" <<
key
180 <<
"' from config!" << std::endl;
187 throw std::runtime_error(
"Unexpected exception occurred during "
188 "reading key '" +
key +
"'' from config!");
193template <
typename ReturnType >
226template <
typename CVecT, DimType dim = 0 >
231 using element_t =
typename CVecT::elem_type;
234 if constexpr (std::is_constructible< CVecT, std::vector< element_t > >())
240 static_assert(dim > 0,
241 "Need template argument dim given if target type is not "
242 "constructible from std::vector.");
267template < DimType dim >
271 return DataIO::get_as_arma_vec< SpaceVecType< dim >, dim >(
key,
node);
277template < DimType dim >
281 return DataIO::get_as_arma_vec< MultiIndexType< dim >, dim >(
key,
node);
301template<
class T,
class Keys = std::list<std::
string> >
313 throw std::invalid_argument(
314 "During recursive_setitem, failed to retrieve a valid key for "
315 "continuing recursion. Make sure the given key sequence ("
374 "recursive_getitem failed for key or key sequence '"
400 const std::string&
delims =
".")
427 throw std::invalid_argument(
428 "Key sequence for recursive_setitem may not be empty!"
452 const std::string&
delims =
".")
For access to a dict-like structure with a bad key.
Definition exceptions.hh:67
MultiIndexType< dim > get_as_MultiIndex(const std::string &key, const DataIO::Config &node)
Special case of Utopia::get_as to retrieve an entry as MultiIndex.
Definition cfg_utils.hh:279
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition types.hh:71
SpaceVecType< dim > get_as_SpaceVec(const std::string &key, const DataIO::Config &node)
Special case of Utopia::get_as to retrieve an entry as SpaceVec.
Definition cfg_utils.hh:269
ReturnType get_as(const std::string &key, const DataIO::Config &node)
This function is a wrapper around the yaml-cpp YAML::Node::as function.
Definition cfg_utils.hh:158
Config __recursive_setitem(Config d, Keys &&key_sequence, const T &val)
Helper function for recursive_setitem.
Definition cfg_utils.hh:302
Config recursive_getitem(const Config &d, const std::vector< std::string > &key_sequence)
Recursively retrieve an element from the configuration tree.
Definition cfg_utils.hh:360
YAML::Exception improve_yaml_exception(const Exc &e, const Config &node, std::string prefix={})
Improves yaml-cpp exceptions occurring for a given node.
Definition cfg_utils.hh:73
void recursive_setitem(Config &d, std::list< std::string > key_sequence, const T val)
Recursively sets an element in a configuration tree.
Definition cfg_utils.hh:422
std::string to_string(const Config &node)
Given a config node, returns a string representation of it.
Definition cfg_utils.hh:110
CVecT get_as_arma_vec(const std::string &key, const DataIO::Config &node)
Retrieve a config entry as Armadillo column vector using get_.
Definition cfg_utils.hh:228
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213
DataIO::Config Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition types.hh:80
SeqCont split(const std::string &s, const std::string &delims=" ")
Splits a string and returns a container of string segments.
Definition string.hh:45
std::string join(const Cont &cont, const std::string &delim=", ")
Joins together the strings in a container.
Definition string.hh:18
unsigned short DimType
Type for dimensions, i.e. very small unsigned integers.
Definition types.hh:34