File: DataStructuresConvert-inl.h

package info (click to toggle)
opensurgsim 0.7.0-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 23,744 kB
  • sloc: cpp: 135,363; ansic: 4,206; python: 3,934; makefile: 38
file content (166 lines) | stat: -rw-r--r-- 4,225 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H
#define SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H

#include <string>

#include "SurgSim/Framework/Log.h"

namespace SurgSim
{
namespace DataStructures
{
namespace Convert
{
const std::string serializeLogger = "Serialization";
const std::string hasValueName = "HasValue";
const std::string valueName = "Value";
};
};
};

template <class T>
YAML::Node YAML::convert<SurgSim::DataStructures::OptionalValue<T>>::encode(
			const SurgSim::DataStructures::OptionalValue<T>& rhs)
{
	Node node;
	node[SurgSim::DataStructures::Convert::hasValueName] = rhs.hasValue();
	if (rhs.hasValue())
	{
		node[SurgSim::DataStructures::Convert::valueName] = rhs.getValue();
	}
	else
	{
		node[SurgSim::DataStructures::Convert::valueName] = "Not set";
	}
	return node;
}

template <class T>
bool YAML::convert<SurgSim::DataStructures::OptionalValue<T>>::decode(
			const Node& node, SurgSim::DataStructures::OptionalValue<T>& rhs) //NOLINT
{
	bool result = true;
	if (node.IsMap())
	{
		if (node[SurgSim::DataStructures::Convert::hasValueName].as<bool>())
		{
			try
			{
				rhs.setValue(node[SurgSim::DataStructures::Convert::valueName].as<T>());
			}
			catch (YAML::RepresentationException)
			{
				result = false;
				auto logger = SurgSim::Framework::Logger::getLogger(SurgSim::DataStructures::Convert::serializeLogger);
				SURGSIM_LOG(logger, WARNING) << "Bad conversion";
			}
		}
		else
		{
			rhs.invalidate();
		}
	}
	else if (node.IsScalar())
	{
		try
		{
			rhs.setValue(node.as<T>());
		}
		catch (YAML::RepresentationException)
		{
			result = false;
			auto logger = SurgSim::Framework::Logger::getLogger(SurgSim::DataStructures::Convert::serializeLogger);
			SURGSIM_LOG(logger, WARNING) << "Bad conversion";
		}
	}
	return result;
}

template <class Key, class T>
YAML::Node YAML::convert<std::unordered_map<Key, T>>::encode(const std::unordered_map<Key, T>& rhs)
{
	Node node(NodeType::Map);
	for (auto it = std::begin(rhs); it != std::end(rhs); ++it)
	{
		node[it->first] = it->second;
	}
	return node;
}

template <class Key, class T>
bool YAML::convert<std::unordered_map<Key, T>>::decode(const Node& node, std::unordered_map<Key, T>& rhs) //NOLINT
{
	if (!node.IsMap())
	{
		return false;
	}

	bool result = true;
	for (auto it = node.begin(); it != node.end(); ++it)
	{
		try
		{
			rhs[it->first.as<Key>()] = it->second.as<T>();
		}
		catch (YAML::RepresentationException)
		{
			result = false;
			auto logger = SurgSim::Framework::Logger::getLogger(SurgSim::DataStructures::Convert::serializeLogger);
			SURGSIM_LOG(logger, WARNING) << __FUNCTION__ << ": Bad conversion";
		}
	}
	return result;
}

template <class Value>
YAML::Node YAML::convert<std::unordered_set<Value>>::encode(const std::unordered_set<Value>& rhs)
{
	Node node(NodeType::Sequence);
	for (auto it = std::begin(rhs); it != std::end(rhs); ++it)
	{
		node.push_back(*it);
	}
	return node;
}

template <class Value>
bool YAML::convert<std::unordered_set<Value>>::decode(const Node& node, std::unordered_set<Value>& rhs) //NOLINT
{
	if (!node.IsSequence())
	{
		return false;
	}

	bool result = true;
	for (auto it = node.begin(); it != node.end(); ++it)
	{
		try
		{
			rhs.insert(it->as<Value>());
		}
		catch (YAML::RepresentationException)
		{
			result = false;
			auto logger = SurgSim::Framework::Logger::getLogger(SurgSim::DataStructures::Convert::serializeLogger);
			SURGSIM_LOG(logger, WARNING) << __FUNCTION__ << ": Bad conversion";
		}
	}
	return result;
}

#endif // SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H