1#include <RobotRaconteur/DataTypes.h>
2#include "yaml-cpp/yaml.h"
6namespace RobotRaconteur
16T parse_number(
const YAML::Node& node,
const std::string& key,
bool optional)
20 return node[key].as<T>();
30 throw RobotRaconteur::InvalidArgumentException(
"Key not found: " + key);
36RobotRaconteur::RRArrayPtr<T> parse_numeric_array(
const YAML::Node& node,
const std::string& key,
bool optional,
37 bool varlength =
true,
size_t len = 0)
41 auto vec = node[key].as<std::vector<T> >();
46 if (vec.size() != len)
48 throw RobotRaconteur::InvalidArgumentException(
"Bad conversion");
51 if (vec.size() >= len)
53 throw RobotRaconteur::InvalidArgumentException(
"Bad conversion");
56 return RobotRaconteur::VectorToRRArray<T>(vec);
64 return RobotRaconteur::AllocateRRArray<T>(0);
68 return RobotRaconteur::AllocateRRArray<T>(len);
73 throw RobotRaconteur::InvalidArgumentException(
"Key not found: " + key);
78template <
typename T,
int N>
79boost::array<T, N> parse_numeric_array_na(
const YAML::Node& node,
const std::string& key)
83 auto arr = node[key].as<std::vector<T> >();
86 throw RobotRaconteur::InvalidArgumentException(
"Invalid array length");
88 boost::array<T, N> ret{};
89 std::copy(arr.begin(), arr.end(), ret.begin());
94 throw RobotRaconteur::InvalidArgumentException(
"Key not found: " + key);
98static RobotRaconteur::rr_bool parse_bool(
const YAML::Node& node,
const std::string& key,
bool optional)
102 bool val = node[key].as<
bool>();
103 RobotRaconteur::rr_bool ret;
104 ret.value = (val) ? 1 : 0;
111 RobotRaconteur::rr_bool ret;
117 throw RobotRaconteur::InvalidArgumentException(
"Key not found: " + key);
122static std::string parse_string(
const YAML::Node& node,
const std::string& key,
bool optional)
126 return node[key].as<std::string>();
136 throw RobotRaconteur::InvalidArgumentException(
"Key not found: " + key);
141static RobotRaconteur::RRListPtr<RRArray<char> > parse_string_list(
const YAML::Node& node,
const std::string& key,
146 return RobotRaconteur::stringVectorToRRList(node[key].as<std::vector<std::string> >());
152 return RobotRaconteur::RRListPtr<RRArray<char> >();
156 throw RobotRaconteur::InvalidArgumentException(
"Key not found: " + key);
162RobotRaconteur::RRListPtr<RobotRaconteur::RRArray<T> > parse_numeric_array_list(
const YAML::Node& node,
163 const std::string& key,
bool optional,
164 bool varlength =
true,
size_t len = 0)
168 if (!node[key].IsSequence())
170 throw RobotRaconteur::InvalidArgumentException(
"Bad conversion");
173 auto ret = RobotRaconteur::AllocateEmptyRRList<RRArray<double> >();
174 for (
auto e : node[key])
177 auto vec = e.as<std::vector<T> >();
182 if (vec.size() != len)
184 throw RobotRaconteur::InvalidArgumentException(
"Bad conversion");
187 if (vec.size() >= len)
189 throw RobotRaconteur::InvalidArgumentException(
"Bad conversion");
192 ret->push_back(RobotRaconteur::VectorToRRArray<T>(vec));
200 return RobotRaconteur::AllocateEmptyRRList<RRArray<T> >();
204 throw RobotRaconteur::InvalidArgumentException(
"Key not found: " + key);
210RobotRaconteur::RRMapPtr<std::string, RobotRaconteur::RRArray<T> > parse_numeric_array_map_string(
211 const YAML::Node& node,
const std::string& key,
bool optional,
bool varlength =
true,
size_t len = 0)
215 if (!node[key].IsMap())
217 throw RobotRaconteur::InvalidArgumentException(
"Bad conversion");
220 auto ret = RobotRaconteur::AllocateEmptyRRMap<std::string, RRArray<double> >();
221 for (
auto e : node[key])
224 auto vec = e.second.as<std::vector<T> >();
229 if (vec.size() != len)
231 throw RobotRaconteur::InvalidArgumentException(
"Bad conversion");
234 if (vec.size() >= len)
236 throw RobotRaconteur::InvalidArgumentException(
"Bad conversion");
239 ret->insert(std::make_pair(e.first.as<std::string>(), RobotRaconteur::VectorToRRArray<T>(vec)));
247 return RobotRaconteur::AllocateEmptyRRMap<std::string, RobotRaconteur::RRArray<T> >();
251 throw RobotRaconteur::InvalidArgumentException(
"Key not found: " + key);
257RobotRaconteur::RRMultiDimArrayPtr<T> parse_numeric_multidimarray(
const YAML::Node& node,
const std::string& key,
258 bool optional, uint32_t m, uint32_t n)
262 auto vec = node[key].as<std::vector<T> >();
263 if (vec.size() != m * n)
265 throw RobotRaconteur::InvalidArgumentException(
"Bad conversion");
267 std::vector<uint32_t> dims = {m, n};
268 auto ret = RobotRaconteur::AllocateEmptyRRMultiDimArray<T>(dims);
269 std::copy(vec.begin(), vec.end(), ret->Array->begin());
277 std::vector<uint32_t> dims = {m, n};
278 return RobotRaconteur::AllocateEmptyRRMultiDimArray<T>(dims);
282 throw RobotRaconteur::InvalidArgumentException(
"Key not found: " + key);