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 Key, class T>
95 YAML::Node YAML::convert<std::unordered_map<Key, T>>::encode(const std::unordered_map<Key, T>& rhs)
96 {
97  Node node(NodeType::Map);
98  for (auto it = std::begin(rhs); it != std::end(rhs); ++it)
99  {
100  node[it->first] = it->second;
101  }
102  return node;
103 }
104 
105 template <class Key, class T>
106 bool YAML::convert<std::unordered_map<Key, T>>::decode(const Node& node, std::unordered_map<Key, T>& rhs) //NOLINT
107 {
108  if (!node.IsMap())
109  {
110  return false;
111  }
112 
113  bool result = true;
114  for (auto it = node.begin(); it != node.end(); ++it)
115  {
116  try
117  {
118  rhs[it->first.as<Key>()] = it->second.as<T>();
119  }
120  catch (YAML::RepresentationException)
121  {
122  result = false;
124  SURGSIM_LOG(logger, WARNING) << __FUNCTION__ << ": Bad conversion";
125  }
126  }
127  return result;
128 }
129 
130 template <class Value>
131 YAML::Node YAML::convert<std::unordered_set<Value>>::encode(const std::unordered_set<Value>& rhs)
132 {
133  Node node(NodeType::Sequence);
134  for (auto it = std::begin(rhs); it != std::end(rhs); ++it)
135  {
136  node.push_back(*it);
137  }
138  return node;
139 }
140 
141 template <class Value>
142 bool YAML::convert<std::unordered_set<Value>>::decode(const Node& node, std::unordered_set<Value>& rhs) //NOLINT
143 {
144  if (!node.IsSequence())
145  {
146  return false;
147  }
148 
149  bool result = true;
150  for (auto it = node.begin(); it != node.end(); ++it)
151  {
152  try
153  {
154  rhs.insert(it->as<Value>());
155  }
156  catch (YAML::RepresentationException)
157  {
158  result = false;
160  SURGSIM_LOG(logger, WARNING) << __FUNCTION__ << ": Bad conversion";
161  }
162  }
163  return result;
164 }
165 
166 #endif // SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H
SurgSim::Framework::Logger::getLogger
static std::shared_ptr< Logger > getLogger(const std::string &name)
Get a logger by name from Logger Manager.
Definition: Logger.h:109
SurgSim::DataStructures::OptionalValue
Container class that can indicate whether the object has been assigned a value.
Definition: OptionalValue.h:29
SURGSIM_LOG
#define SURGSIM_LOG(logger, level)
Logs a message to the specified logger with the short level name.
Definition: LogMacros.h:60
SurgSim::DataStructures::OptionalValue::setValue
void setValue(const T &val)
Set the value of this object, and mark it as valid.
Definition: OptionalValue.h:69
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
Log.h
SurgSim::DataStructures::Convert::hasValueName
const std::string hasValueName
Definition: DataStructuresConvert-inl.h:30
SurgSim::DataStructures::OptionalValue::hasValue
bool hasValue() const
Query if this object has been assigned a value.
Definition: OptionalValue.h:56
SurgSim::DataStructures::Convert::serializeLogger
const std::string serializeLogger
Definition: DataStructuresConvert-inl.h:29
SurgSim::DataStructures::Convert::valueName
const std::string valueName
Definition: DataStructuresConvert-inl.h:31
SurgSim::DataStructures::OptionalValue::getValue
const T & getValue() const
Gets the value.
Definition: OptionalValue.h:78
string
string(TOUPPER ${DEVICE} DEVICE_UPPER_CASE) option(BUILD_DEVICE_$
Definition: CMakeLists.txt:38
SurgSim::DataStructures::OptionalValue::invalidate
void invalidate()
Mark this object as invalid.
Definition: OptionalValue.h:62