Robot Raconteur Companion C++ Library
Loading...
Searching...
No Matches
yaml_namedarray_parser.h
1#include <RobotRaconteur/DataTypes.h>
2#include "yaml-cpp/yaml.h"
3
4#pragma once
5
6namespace RobotRaconteur
7{
8namespace Companion
9{
10namespace InfoParser
11{
12namespace yaml
13{
14
15template <typename T>
16T parse_namedarray(const YAML::Node& node, const std::string& key, bool optional)
17{
18 if (node[key])
19 {
20 return node[key].as<T>();
21 }
22 else
23 {
24 if (optional)
25 {
26 T ret{};
27 memset(&ret, 0, sizeof(T));
28 return ret;
29 }
30 else
31 {
32 throw RobotRaconteur::InvalidArgumentException("Key not found: " + key);
33 }
34 }
35}
36
37template <typename T>
38RobotRaconteur::RRNamedArrayPtr<T> parse_namedarray_array(const YAML::Node& node, const std::string& key, bool optional,
39 bool varlength = true, size_t len = 0)
40{
41 if (node[key])
42 {
43 auto vec = node[key].as<std::vector<T> >();
44 if (len != 0)
45 {
46 if (!varlength)
47 {
48 if (vec.size() != len)
49 {
50 throw RobotRaconteur::InvalidArgumentException("Bad conversion");
51 }
52 }
53 if (vec.size() >= len)
54 {
55 throw RobotRaconteur::InvalidArgumentException("Bad conversion");
56 }
57 }
58 auto ret = RobotRaconteur::AllocateEmptyRRNamedArray<T>(vec.size());
59 for (size_t i = 0; i < vec.size(); i++)
60 {
61 ret->at(i) = vec.at(i);
62 }
63 return ret;
64 }
65 else
66 {
67 if (optional)
68 {
69 if (varlength)
70 {
71 return RobotRaconteur::AllocateEmptyRRNamedArray<T>(0);
72 }
73 else
74 {
75 return RobotRaconteur::AllocateEmptyRRNamedArray<T>(len);
76 }
77 }
78 else
79 {
80 throw RobotRaconteur::InvalidArgumentException("Key not found: " + key);
81 }
82 }
83}
84
85} // namespace yaml
86} // namespace InfoParser
87} // namespace Companion
88} // namespace RobotRaconteur