se2ez
utility.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef SE2EZ_CORE_YAML_UTILITY_
4 #define SE2EZ_CORE_YAML_UTILITY_
5 
6 #include <Eigen/Core>
7 
8 #include <yaml-cpp/yaml.h>
9 
10 #include <se2ez/core/log.h>
11 
12 namespace se2ez
13 {
14  namespace io
15  {
16  /** \brief Returns true if \e node is a valid YAML node.
17  * \param[in] node Node to check.
18  * \return True if \e node is a YAML node.
19  */
20  bool isNode(const YAML::Node &node);
21 
22  /** \brief Convert a sequence of doubles in YAML to an Eigen vector.
23  * \param[in] node Node to parse.
24  * \return The contents of the node as an Eigen vector.
25  */
26  Eigen::VectorXd toVector(const YAML::Node &node);
27 
28  /** \brief Convert a sequence of doubles in an Eigen vector to YAML.
29  * \param[in] vec Vector to convert
30  * \return The contents of the node as a YAML node.
31  */
32  YAML::Node toVector(const Eigen::Ref<const Eigen::VectorXd> &vec);
33 
34  /** \brief Convenience function to print out a YAML error message with line and column information.
35  * \param[in] node Node that was culprit.
36  * \param[in] fmt Format string for error message.
37  * \param[in] args Arguments to format string.
38  * \tparam Args Variadic arguments for format string.
39  * \throws std::runtime_error always.
40  */
41  template <typename... Args>
42  void throwParsingError[[noreturn]](const YAML::Node &node, const std::string &fmt, Args &&... args)
43  {
44  const auto &mark = node.Mark();
45  const std::string &error =
46  log::format("[line: %1%][col: %2%] YAML Error: %3%", mark.line, mark.column, fmt);
47  const std::string &message = log::format(error, std::forward<Args>(args)...);
48  throw std::runtime_error(message);
49  }
50  } // namespace io
51 } // namespace se2ez
52 
53 #endif
Eigen::VectorXd toVector(const YAML::Node &node)
Convert a sequence of doubles in YAML to an Eigen vector.
Definition: yaml.cpp:282
void throwParsingError(const YAML::Node &node, const std::string &fmt, Args &&... args)
Convenience function to print out a YAML error message with line and column information.
Definition: utility.h:42
std::string format(const std::string &fmt, Args &&... args)
Definition: log.h:25
Main namespace.
Definition: collision.h:11
bool isNode(const YAML::Node &node)
Returns true if node is a valid YAML node.
Definition: yaml.cpp:258