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) //NOLINT
56 {
57  bool result = true;
58  if (node.IsMap())
59  {
61  {
62  try
63  {
65  }
66  catch (YAML::RepresentationException)
67  {
68  result = false;
70  SURGSIM_LOG(logger, WARNING) << "Bad conversion";
71  }
72  }
73  else
74  {
75  rhs.invalidate();
76  }
77  }
78  else if (node.IsScalar())
79  {
80  try
81  {
82  rhs.setValue(node.as<T>());
83  }
84  catch (YAML::RepresentationException)
85  {
86  result = false;
88  SURGSIM_LOG(logger, WARNING) << "Bad conversion";
89  }
90  }
91  return result;
92 }
93 
94 template <class T, size_t N>
95 YAML::Node YAML::convert<std::array<T, N>>::encode(const std::array<T, N>& rhs)
96 {
97  Node node(NodeType::Sequence);
98  for (auto it = rhs.cbegin(); it != rhs.cend(); ++it)
99  {
100  node.push_back(*it);
101  }
102  return node;
103 }
104 
105 template <class T, size_t N>
106 bool YAML::convert<std::array<T, N>>::decode(const Node& node, std::array<T, N>& rhs) //NOLINT
107 {
108  if (!node.IsSequence() || node.size() != N)
109  {
110  return false;
111  }
112 
113  bool result = true;
114  auto rhsit = rhs.begin();
115  for (YAML::const_iterator it = node.begin(); it != node.end(); ++it, ++rhsit)
116  {
117  try
118  {
119  (*rhsit) = it->as<T>();
120  }
121  catch (YAML::RepresentationException)
122  {
123  result = false;
125  SURGSIM_LOG(logger, WARNING) << __FUNCTION__ << ": Bad conversion";
126  }
127  }
128  return result;
129 }
130 
131 template <class Key, class T>
132 YAML::Node YAML::convert<std::unordered_map<Key, T>>::encode(const std::unordered_map<Key, T>& rhs)
133 {
134  Node node(NodeType::Map);
135  for (auto it = std::begin(rhs); it != std::end(rhs); ++it)
136  {
137  node[it->first] = it->second;
138  }
139  return node;
140 }
141 
142 template <class Key, class T>
143 bool YAML::convert<std::unordered_map<Key, T>>::decode(const Node& node, std::unordered_map<Key, T>& rhs) //NOLINT
144 {
145  if (!node.IsMap())
146  {
147  return false;
148  }
149 
150  bool result = true;
151  for (auto it = node.begin(); it != node.end(); ++it)
152  {
153  try
154  {
155  rhs[it->first.as<Key>()] = it->second.as<T>();
156  }
157  catch (YAML::RepresentationException)
158  {
159  result = false;
161  SURGSIM_LOG(logger, WARNING) << __FUNCTION__ << ": Bad conversion";
162  }
163  }
164  return result;
165 }
166 
167 template <class Value>
168 YAML::Node YAML::convert<std::unordered_set<Value>>::encode(const std::unordered_set<Value>& rhs)
169 {
170  Node node(NodeType::Sequence);
171  for (auto it = std::begin(rhs); it != std::end(rhs); ++it)
172  {
173  node.push_back(*it);
174  }
175  return node;
176 }
177 
178 template <class Value>
179 bool YAML::convert<std::unordered_set<Value>>::decode(const Node& node, std::unordered_set<Value>& rhs) //NOLINT
180 {
181  if (!node.IsSequence())
182  {
183  return false;
184  }
185 
186  bool result = true;
187  for (auto it = node.begin(); it != node.end(); ++it)
188  {
189  try
190  {
191  rhs.insert(it->as<Value>());
192  }
193  catch (YAML::RepresentationException)
194  {
195  result = false;
197  SURGSIM_LOG(logger, WARNING) << __FUNCTION__ << ": Bad conversion";
198  }
199  }
200  return result;
201 }
202 
203 #endif // SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H
Definition: CompoundShapeToGraphics.cpp:29
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.
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
bool hasValue() const
Query if this object has been assigned a value.
Definition: OptionalValue.h:56
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