Robot Raconteur Companion C++ Library
Loading...
Searching...
No Matches
yaml_structure_parser.h
1#pragma once
2#include <RobotRaconteur/DataTypes.h>
3#include "yaml-cpp/yaml.h"
4
5namespace RobotRaconteur
6{
7namespace Companion
8{
9namespace InfoParser
10{
11namespace yaml
12{
13
14template <typename T>
15T parse_structure(const YAML::Node& node, const std::string& key, bool optional)
16{
17 if (node[key])
18 {
19 return node[key].as<T>();
20 }
21 else
22 {
23 if (optional)
24 {
25 return T();
26 }
27 else
28 {
29 throw RobotRaconteur::InvalidArgumentException("Key not found: " + key);
30 }
31 }
32}
33
34template <typename T>
35class parse_structure_container_traits
36{
37};
38
39template <typename T>
40struct parse_structure_container_traits<boost::intrusive_ptr<T> >
41{
42 typedef T type;
43 static RobotRaconteur::RRListPtr<T> parse_structure_list(const YAML::Node& node, const std::string& key,
44 bool optional)
45 {
46 if (node[key])
47 {
48 RobotRaconteur::RRListPtr<T> ret = RobotRaconteur::AllocateEmptyRRList<T>();
49 auto val = node[key].as<std::list<boost::intrusive_ptr<T> > >();
50 std::copy(val.begin(), val.end(), std::back_inserter(*ret));
51 return ret;
52 }
53 else
54 {
55 if (optional)
56 {
57 return RobotRaconteur::RRListPtr<T>();
58 }
59 else
60 {
61 throw RobotRaconteur::InvalidArgumentException("Key not found: " + key);
62 }
63 }
64 }
65
66 static RobotRaconteur::RRMapPtr<int32_t, T> parse_structure_map_int32(const YAML::Node& node,
67 const std::string& key, bool optional)
68 {
69 if (node[key])
70 {
71 RobotRaconteur::RRMapPtr<int32_t, T> ret = RobotRaconteur::AllocateEmptyRRMap<int32_t, T>();
72 auto val = node[key].as<std::map<int32_t, boost::intrusive_ptr<T> > >();
73 // std::copy(val.begin(), val.end(), std::inserter(*ret,ret->end()));
74 ret->insert(val.begin(), val.end());
75 return ret;
76 }
77 else
78 {
79 if (optional)
80 {
81 return RobotRaconteur::RRMapPtr<int32_t, T>();
82 }
83 else
84 {
85 throw RobotRaconteur::InvalidArgumentException("Key not found: " + key);
86 }
87 }
88 }
89
90 static RobotRaconteur::RRMapPtr<std::string, T> parse_structure_map_string(const YAML::Node& node,
91 const std::string& key, bool optional)
92 {
93 if (node[key])
94 {
95 RobotRaconteur::RRMapPtr<std::string, T> ret = RobotRaconteur::AllocateEmptyRRMap<std::string, T>();
96 auto val = node[key].as<std::map<std::string, boost::intrusive_ptr<T> > >();
97 // std::copy(val.begin(), val.end(), std::inserter(*ret,ret->end()));
98 ret->insert(val.begin(), val.end());
99 return ret;
100 }
101 else
102 {
103 if (optional)
104 {
105 return RobotRaconteur::RRMapPtr<std::string, T>();
106 }
107 else
108 {
109 throw RobotRaconteur::InvalidArgumentException("Key not found: " + key);
110 }
111 }
112 }
113};
114
115template <typename T>
116RobotRaconteur::RRListPtr<typename parse_structure_container_traits<T>::type> parse_structure_list(
117 const YAML::Node& node, const std::string& key, bool optional)
118{
119 return parse_structure_container_traits<T>::parse_structure_list(node, key, optional);
120}
121
122template <typename T>
123RobotRaconteur::RRMapPtr<int32_t, typename parse_structure_container_traits<T>::type> parse_structure_map_int32(
124 const YAML::Node& node, const std::string& key, bool optional)
125{
126 return parse_structure_container_traits<T>::parse_structure_map_int32(node, key, optional);
127}
128
129template <typename T>
130RobotRaconteur::RRMapPtr<std::string, typename parse_structure_container_traits<T>::type> parse_structure_map_string(
131 const YAML::Node& node, const std::string& key, bool optional)
132{
133 return parse_structure_container_traits<T>::parse_structure_map_string(node, key, optional);
134}
135
136} // namespace yaml
137} // namespace InfoParser
138} // namespace Companion
139} // namespace RobotRaconteur