Robot Raconteur Companion C++ Library
Loading...
Searching...
No Matches
yaml_parser_overrides.h
1#include <string>
2#include "yaml-cpp/yaml.h"
4
5#include <boost/uuid/uuid.hpp>
6#include <boost/uuid/uuid_io.hpp>
7
8#pragma once
9
10namespace RobotRaconteur
11{
12namespace Companion
13{
14namespace InfoParser
15{
16namespace yaml
17{
18static boost::array<uint8_t, 16> parse_uuid_bytes_override(const YAML::Node& node, const std::string& key)
19{
20 if (node[key])
21 {
22 auto string_uuid = node[key].as<std::string>();
23 auto boost_uuid = boost::lexical_cast<boost::uuids::uuid>(string_uuid);
24 boost::array<uint8_t, 16> ret{};
25 std::copy(boost_uuid.begin(), boost_uuid.end(), ret.begin());
26 return ret;
27 }
28 else
29 {
30 throw RobotRaconteur::InvalidArgumentException("Key not found: " + key);
31 }
32}
33
34static void device_info_extra_parse(const YAML::Node& node, com::robotraconteur::device::DeviceInfoPtr& rhs)
35{
36 RobotRaconteur::RRListPtr<RobotRaconteur::RRValue> tags_out =
37 RobotRaconteur::AllocateEmptyRRList<RobotRaconteur::RRValue>();
38
39 if (node["tags"])
40 {
41 auto tags = node["tags"].as<std::vector<com::robotraconteur::identifier::IdentifierPtr> >();
42 for (auto& e : tags)
43 {
44 tags_out->push_back(e);
45 }
46 }
47
48 if (node["extended"])
49 {
50 auto extended = node["extended"];
51 if (extended["tags"])
52 {
53 auto tags = extended["tags"].as<std::vector<com::robotraconteur::identifier::IdentifierPtr> >();
54 for (auto& e : tags)
55 {
56 tags_out->push_back(e);
57 }
58 }
59 }
60
61 if (tags_out->empty())
62 {
63 return;
64 }
65
66 if (!rhs->extended)
67 {
68 rhs->extended = RobotRaconteur::AllocateEmptyRRMap<std::string, RobotRaconteur::RRValue>();
69 }
70
71 rhs->extended->insert(std::make_pair("tags", tags_out));
72}
73
74} // namespace yaml
75} // namespace InfoParser
76} // namespace Companion
77} // namespace RobotRaconteur
78
79namespace YAML
80{
81template <>
82struct convert<com::robotraconteur::uuid::UUID>
83{
84 static Node encode(const com::robotraconteur::uuid::UUID& rhs)
85 {
86 RR_UNUSED(rhs);
87 Node node;
88 return node;
89 }
90
91 static bool decode(const Node& node, com::robotraconteur::uuid::UUID& rhs)
92 {
93 auto string_uuid = node.as<std::string>();
94 auto boost_uuid = boost::lexical_cast<boost::uuids::uuid>(string_uuid);
95 std::copy(boost_uuid.begin(), boost_uuid.end(), rhs.s.uuid_bytes.begin());
96 return true;
97 }
98};
99
100template <>
101struct convert<com::robotraconteur::identifier::IdentifierPtr>
102{
103 static Node encode(const com::robotraconteur::identifier::IdentifierPtr& rhs)
104 {
105 RR_UNUSED(rhs);
106 Node node;
107 return node;
108 }
109
110 static bool decode(const Node& node, com::robotraconteur::identifier::IdentifierPtr& rhs)
111 {
112 if (!rhs)
113 rhs.reset(new com::robotraconteur::identifier::Identifier); // NOLINT(cppcoreguidelines-owning-memory)
114 if (node.IsScalar())
115 {
116 rhs->name = node.as<std::string>();
117 std::fill(rhs->uuid.a.begin(), rhs->uuid.a.end(), 0);
118 return true;
119 }
120 rhs->name = RobotRaconteur::Companion::InfoParser::yaml::parse_string(node, "name", true);
121 rhs->uuid = RobotRaconteur::Companion::InfoParser::yaml::parse_namedarray<com::robotraconteur::uuid::UUID>(
122 node, "uuid", true);
123 return true;
124 }
125};
126} // namespace YAML