Robot Raconteur Companion C++ Library
Loading...
Searching...
No Matches
yaml_loader_enums.h
1#pragma once
2#include <string>
3#include "yaml-cpp/yaml.h"
5#include "yaml_loader_enums_impl.h"
6
7namespace RobotRaconteur
8{
9namespace Companion
10{
11namespace InfoParser
12{
13namespace yaml
14{
15
16template <typename T>
17T parse_enum(const YAML::Node& node, const std::string& key, bool optional)
18{
19 if (node[key])
20 {
21 return string_to_enum_traits<T>::string_to_enum(node[key].as<std::string>(), node[key]);
22 }
23 else
24 {
25 if (optional)
26 {
27 return T();
28 }
29 else
30 {
31 throw RobotRaconteur::InvalidArgumentException("Key not found: " + key);
32 }
33 }
34}
35
36template <typename T>
37RobotRaconteur::RRListPtr<RRArray<int32_t> > parse_enum_list(const YAML::Node& node, const std::string& key,
38 bool optional)
39{
40 if (node[key])
41 {
42 auto ret = RobotRaconteur::AllocateEmptyRRList<RRArray<int32_t> >();
43 std::vector<std::string> vals = node[key].as<std::vector<std::string> >();
44 for (const auto& v : vals)
45 {
46 ret->push_back(
47 RobotRaconteur::ScalarToRRArray((int32_t)string_to_enum_traits<T>::string_to_enum(v, node[key])));
48 }
49 return ret;
50 }
51 else
52 {
53 if (optional)
54 {
55 return RobotRaconteur::AllocateEmptyRRList<RRArray<int32_t> >();
56 }
57 else
58 {
59 throw RobotRaconteur::InvalidArgumentException("Key not found: " + key);
60 }
61 }
62}
63
64template <typename T>
65uint32_t parse_enum_flags(const YAML::Node& node, const std::string& key, bool optional)
66{
67 if (node[key])
68 {
69 uint32_t ret = 0;
70 std::vector<std::string> vals = node[key].as<std::vector<std::string> >();
71 for (const auto& v : vals)
72 {
73 ret |= ((int32_t)string_to_enum_traits<T>::string_to_enum(v, node[key]));
74 }
75 return ret;
76 }
77 else
78 {
79 if (optional)
80 {
81 return 0;
82 }
83 else
84 {
85 throw RobotRaconteur::InvalidArgumentException("Key not found: " + key);
86 }
87 }
88}
89
90} // namespace yaml
91} // namespace InfoParser
92} // namespace Companion
93} // namespace RobotRaconteur