DataStructuresConvert-inl.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H
17 #define SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H
18 
19 #include <string>
20 
21 #include "SurgSim/Framework/Log.h"
22 
23 namespace SurgSim
24 {
25 namespace DataStructures
26 {
27 namespace Convert
28 {
29 const std::string serializeLogger = "Serialization";
30 const std::string hasValueName = "HasValue";
31 const std::string valueName = "Value";
32 };
33 };
34 };
35 
36 template <class T>
37 YAML::Node YAML::convert<SurgSim::DataStructures::OptionalValue<T>>::encode(
39 {
40  Node node;
42  if (rhs.hasValue())
43  {
45  }
46  else
47  {
49  }
50  return node;
51 }
52 
53 template <class T>
54 bool YAML::convert<SurgSim::DataStructures::OptionalValue<T>>::decode(
55  const Node& node, SurgSim::DataStructures::OptionalValue<T>& rhs)
56 {
57  bool result = true;
59  {
60  try
61  {
63  }
64  catch (YAML::RepresentationException)
65  {
66  result = false;
68  SURGSIM_LOG(logger, WARNING) << "Bad conversion";
69  }
70  }
71  else
72  {
73  rhs.invalidate();
74  }
75  return result;
76 }
77 
78 template <class T, size_t N>
79 YAML::Node YAML::convert<std::array<T, N>>::encode(const std::array<T, N>& rhs)
80 {
81  Node node(NodeType::Sequence);
82  for (auto it = rhs.cbegin(); it != rhs.cend(); ++it)
83  {
84  node.push_back(*it);
85  }
86  return node;
87 }
88 
89 template <class T, size_t N>
90 bool YAML::convert<std::array<T, N>>::decode(const Node& node, std::array<T, N>& rhs)
91 {
92  if (!node.IsSequence() || node.size() != N)
93  {
94  return false;
95  }
96 
97  bool result = true;
98  auto rhsit = rhs.begin();
99  for (YAML::const_iterator it = node.begin(); it != node.end(); ++it, ++rhsit)
100  {
101  try
102  {
103  (*rhsit) = it->as<T>();
104  }
105  catch (YAML::RepresentationException)
106  {
107  result = false;
109  SURGSIM_LOG(logger, WARNING) << __FUNCTION__ << ": Bad conversion";
110  }
111  }
112  return result;
113 }
114 
115 template <class Key, class T>
116 YAML::Node YAML::convert<std::unordered_map<Key, T>>::encode(const std::unordered_map<Key, T>& rhs)
117 {
118  Node node(NodeType::Map);
119  for (auto it = std::begin(rhs); it != std::end(rhs); ++it)
120  {
121  node[it->first] = it->second;
122  }
123  return node;
124 }
125 
126 template <class Key, class T>
127 bool YAML::convert<std::unordered_map<Key, T>>::decode(const Node& node, std::unordered_map<Key, T>& rhs)
128 {
129  if (!node.IsMap())
130  {
131  return false;
132  }
133 
134  bool result = true;
135  for (auto it = node.begin(); it != node.end(); ++it)
136  {
137  try
138  {
139  rhs[it->first.as<Key>()] = it->second.as<T>();
140  }
141  catch (YAML::RepresentationException)
142  {
143  result = false;
145  SURGSIM_LOG(logger, WARNING) << __FUNCTION__ << ": Bad conversion";
146  }
147  }
148  return result;
149 }
150 
151 template <class Value>
152 YAML::Node YAML::convert<std::unordered_set<Value>>::encode(const std::unordered_set<Value>& rhs)
153 {
154  Node node(NodeType::Sequence);
155  for (auto it = std::begin(rhs); it != std::end(rhs); ++it)
156  {
157  node.push_back(*it);
158  }
159  return node;
160 }
161 
162 template <class Value>
163 bool YAML::convert<std::unordered_set<Value>>::decode(const Node& node, std::unordered_set<Value>& rhs)
164 {
165  if (!node.IsSequence())
166  {
167  return false;
168  }
169 
170  bool result = true;
171  for (auto it = node.begin(); it != node.end(); ++it)
172  {
173  try
174  {
175  rhs.insert(it->as<Value>());
176  }
177  catch (YAML::RepresentationException)
178  {
179  result = false;
181  SURGSIM_LOG(logger, WARNING) << __FUNCTION__ << ": Bad conversion";
182  }
183  }
184  return result;
185 }
186 
187 #endif // SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H
Definition: DriveElementFromInputBehavior.cpp:27
void invalidate()
Mark this object as invalid.
Definition: OptionalValue.h:62
Container class that can indicate whether the object has been assigned a value.
Definition: OptionalValue.h:29
The convenience header that provides the entirety of the logging API.
string(TOUPPER ${DEVICE}DEVICE_UPPER_CASE) option(BUILD_DEVICE_ $
Definition: CMakeLists.txt:35
bool hasValue() const
Query if this object has been assigned a value.
Definition: OptionalValue.h:56
static std::shared_ptr< Logger > getLogger(const std::string &name)
Get a logger by name from Logger Manager.
Definition: Logger.h:109
#define SURGSIM_LOG(logger, level)
Logs a message to the specified logger with the short level name.
Definition: LogMacros.h:60
const std::string hasValueName
Definition: DataStructuresConvert-inl.h:30
void setValue(const T &val)
Set the value of this object, and mark it as valid.
Definition: OptionalValue.h:69
const std::string valueName
Definition: DataStructuresConvert-inl.h:31
const std::string serializeLogger
Definition: DataStructuresConvert-inl.h:29
const T & getValue() const
Gets the value.
Definition: OptionalValue.h:78