ConfigurationHelpers.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2020 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 #include <mc_rtc/Configuration.h>
7 #include <mc_rtc/logging.h>
8 
9 namespace mc_rtc
10 {
11 
41 template<typename T>
42 std::vector<T> fromVectorOrElement(const mc_rtc::Configuration & config,
43  const std::string & key,
44  const std::vector<T> & defaultVec)
45 {
46  if(!config.has(key)) { return defaultVec; }
47  const auto & c = config(key);
48  try
49  {
50  // Try to convert configuration as a vector
51  std::vector<T> vec = c;
52  return vec;
53  }
54  catch(mc_rtc::Configuration::Exception & notAVec)
55  { // If that fails, try to convert as an element
56  notAVec.silence();
57  try
58  {
59  T elem = c;
60  return std::vector<T>(1, elem);
61  }
62  catch(mc_rtc::Configuration::Exception & notAnElem)
63  { // If that fails as well, return the default
64  notAnElem.silence();
65  return defaultVec;
66  }
67  }
68 }
69 
77 template<typename T>
78 std::vector<T> fromVectorOrElement(const mc_rtc::Configuration & config, const std::string & key)
79 {
80  std::vector<T> vec;
81  const auto & c = config(key);
82  try
83  {
84  vec = c;
85  }
86  catch(mc_rtc::Configuration::Exception & notAVec)
87  {
88  notAVec.silence();
89  try
90  {
91  T elem = c;
92  vec.push_back(elem);
93  }
94  catch(mc_rtc::Configuration::Exception & notAnElem)
95  {
96  notAnElem.silence();
97  auto msg = fmt::format("Configuration {} is not valid. It should be a vector or single element.", key);
98  log::critical(msg);
100  }
101  }
102  return vec;
103 }
104 
120 void MC_RTC_UTILS_DLLAPI overwriteRotationRPY(const mc_rtc::Configuration & config, Eigen::Matrix3d & rotation);
121 
122 } // namespace mc_rtc
mc_rtc::Configuration
Simplify access to values hold within a JSON file.
Definition: Configuration.h:165
mc_rtc::Configuration::Exception::silence
void silence() const noexcept
mc_rtc::log::critical
void critical(Args &&... args)
Definition: logging.h:57
mc_rtc::Configuration::has
bool has(const std::string &key) const
Check if the key is part of the conf.
mc_rtc::Configuration::Exception
Exception thrown by this class when something bad occurs.
Definition: Configuration.h:223
MC_RTC_UTILS_DLLAPI
#define MC_RTC_UTILS_DLLAPI
Definition: utils_api.h:50
mc_rtc::fromVectorOrElement
std::vector< T > fromVectorOrElement(const mc_rtc::Configuration &config, const std::string &key, const std::vector< T > &defaultVec)
Definition: ConfigurationHelpers.h:42
Configuration.h
logging.h
mc_rtc
Definition: Contact.h:87