47 #ifndef FLATLAND_SERVER_YAML_READER_H 48 #define FLATLAND_SERVER_YAML_READER_H 50 #include <Box2D/Box2D.h> 51 #include <flatland_server/exceptions.h> 52 #include <flatland_server/types.h> 53 #include <yaml-cpp/yaml.h> 55 #include <boost/algorithm/string.hpp> 56 #include <boost/version.hpp> 59 #if BOOST_VERSION / 100 % 1000 >= 56 60 #include <boost/type_index.hpp> 61 #define TYPESTRING(T) (boost::typeindex::type_id<T>().pretty_name()) 65 #define TYPESTRING(T) (typeid(T).name()) 78 enum NodeTypeCheck { MAP, LIST, NO_CHECK };
81 std::set<std::string> accessed_keys_;
84 std::string file_path_;
87 std::string entry_name_;
118 void SetErrorInfo(std::string entry_location, std::string entry_name =
"");
125 void SetFile(
const std::string &file_path);
159 std::string sub_node_location =
"");
171 std::string sub_node_location =
"");
184 std::string sub_node_location =
"");
192 template <
typename T>
200 template <
typename T>
201 std::vector<T>
AsList(
int min_size,
int max_size);
208 template <
typename T,
int N>
217 template <
typename T>
218 T
Get(
const std::string &key);
228 template <
typename T>
229 T
Get(
const std::string &key,
const T &default_val);
239 template <
typename T>
240 std::vector<T>
GetList(
const std::string &key,
int min_size,
int max_size);
251 template <
typename T>
252 std::vector<T>
GetList(
const std::string &key,
253 const std::vector<T> default_val,
int min_size,
262 template <
typename T,
int N>
263 std::array<T, N>
GetArray(
const std::string &key);
272 template <
typename T,
int N>
273 std::array<T, N>
GetArray(
const std::string &key,
274 const std::array<T, N> default_val);
308 inline std::string Q(
const std::string &str) {
return "\"" + str +
"\""; }
310 template <
typename T>
316 }
catch (
const YAML::RepresentationException &e) {
319 }
catch (
const YAML::Exception &e) {
326 template <
typename T>
330 if (min_size > 0 && max_size > 0 && min_size == max_size &&
333 std::to_string(min_size) +
fmt_in_);
336 if (min_size > 0 &&
NodeSize() < min_size) {
338 std::to_string(min_size) +
fmt_in_);
341 if (max_size > 0 &&
NodeSize() > max_size) {
343 std::to_string(max_size) +
fmt_in_);
346 for (
int i = 0; i <
NodeSize(); i++) {
347 list.push_back(
Subnode(i, NO_CHECK).As<T>());
353 template <
typename T,
int N>
355 std::vector<T> list_ret = AsList<T>(N, N);
356 std::array<T, N> array_ret;
358 for (
int i = 0; i < N; i++) {
359 array_ret[i] = list_ret[i];
364 template <
typename T>
369 template <
typename T>
372 accessed_keys_.insert(key);
378 template <
typename T>
384 template <
typename T>
386 const std::vector<T> default_val,
387 int min_size,
int max_size) {
389 accessed_keys_.insert(key);
393 return GetList<T>(key, min_size, max_size);
396 template <
typename T,
int N>
401 template <
typename T,
int N>
403 const std::array<T, N> default_val) {
405 accessed_keys_.insert(key);
409 return GetArray<T, N>(key);
417 struct convert<b2Vec2> {
418 static bool decode(
const Node &node, b2Vec2 &rhs) {
419 if (!node.IsSequence() || node.size() != 2) {
423 rhs.x = node[0].as<
double>();
424 rhs.y = node[1].as<
double>();
432 if (!node.IsSequence() || node.size() != 2) {
436 rhs.x = node[0].as<
double>();
437 rhs.y = node[1].as<
double>();
445 if (!node.IsSequence() || node.size() != 4) {
449 rhs.r = node[0].as<
double>();
450 rhs.g = node[1].as<
double>();
451 rhs.b = node[2].as<
double>();
452 rhs.a = node[3].as<
double>();
460 if (!node.IsSequence() || node.size() != 3) {
464 rhs.x = node[0].as<
double>();
465 rhs.y = node[1].as<
double>();
466 rhs.
theta = node[2].as<
double>();
YamlReader SubnodeOpt(const std::string &key, NodeTypeCheck type_check, std::string sub_node_location="")
Optionally get one of the subnode using a key, throws exception on failure, file path is inherited fr...
Definition: yaml_reader.cpp:135
T As()
Convert the current node in yaml reader to a given template type. It uses yaml-cpp's as<T>() function...
Definition: yaml_reader.h:311
Vec2 GetVec2(const std::string &key)
Definition: yaml_reader.cpp:145
void EnsureAccessedAllKeys()
This method checks all keys in the yaml node are used, otherwise it throws an exception.
Definition: yaml_reader.cpp:223
YAML::Node Node()
Definition: yaml_reader.cpp:80
std::vector< T > GetList(const std::string &key, int min_size, int max_size)
Get subnode with a given key and converted to list of the given type, throws on failure.
Definition: yaml_reader.h:379
std::string fmt_in_
formatted entry location for display
Definition: yaml_reader.h:88
Pose GetPose(const std::string &key)
Definition: yaml_reader.cpp:165
YamlReader()
Default constructor for yaml reader, initialize with a empty yaml Node.
Definition: yaml_reader.cpp:53
std::string fmt_name_
formatted entry location for display
Definition: yaml_reader.h:89
std::vector< T > AsList(int min_size, int max_size)
Convert the current node to a list of given type, throws exception on failure.
Definition: yaml_reader.h:327
Definition: exceptions.h:86
YAML::Node node_
The YAML Node this processes.
Definition: yaml_reader.h:80
YamlReader Subnode(int index, NodeTypeCheck type_check, std::string sub_node_location="")
Get one of the subnode using a index, throws exception on failure, file path is inherited from the pa...
Definition: yaml_reader.cpp:86
Color GetColor(const std::string &key, const Color &default_val)
Definition: yaml_reader.cpp:159
int NodeSize()
Definition: yaml_reader.cpp:84
T Get(const std::string &key)
Get subnode with a given key and converted to the given type, throws on failure.
Definition: yaml_reader.h:365
std::string filename_
< location of the entry, used to show where the error come from
Definition: yaml_reader.h:83
void SetFile(const std::string &file_path)
Use this method to set the file location of the yaml node.
Definition: yaml_reader.cpp:213
double theta
theta
Definition: types.h:87
std::string entry_location_
name of the yaml entry, used to show where the error come from
Definition: yaml_reader.h:85
Definition: yaml_reader.h:415
void SetErrorInfo(std::string entry_location, std::string entry_name="")
Use this method to set the entry location and entry name for error message purposes.
Definition: yaml_reader.cpp:179
Definition: yaml_reader.h:76
std::array< T, N > GetArray(const std::string &key)
Get subnode with a given key and converted to array of the given type, throws on failure.
Definition: yaml_reader.h:397
bool IsNodeNull()
Definition: yaml_reader.cpp:82
std::array< T, N > AsArray()
Convert the current node to a array of given type, throws exception on failure.
Definition: yaml_reader.h:354