1 #ifndef NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 2 #define NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 4 #if defined(_MSC_VER) || \ 5 (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \ 6 (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 15 #include <unordered_map> 17 #include <type_traits> 21 #include "yaml-cpp/binary.h" 22 #include "yaml-cpp/node/impl.h" 23 #include "yaml-cpp/node/iterator.h" 24 #include "yaml-cpp/node/node.h" 25 #include "yaml-cpp/node/type.h" 26 #include "yaml-cpp/null.h" 37 namespace conversion {
38 inline bool IsInfinity(
const std::string& input) {
39 return input ==
".inf" || input ==
".Inf" || input ==
".INF" ||
40 input ==
"+.inf" || input ==
"+.Inf" || input ==
"+.INF";
43 inline bool IsNegativeInfinity(
const std::string& input) {
44 return input ==
"-.inf" || input ==
"-.Inf" || input ==
"-.INF";
47 inline bool IsNaN(
const std::string& input) {
48 return input ==
".nan" || input ==
".NaN" || input ==
".NAN";
54 struct convert<Node> {
55 static Node encode(
const Node& rhs) {
return rhs; }
57 static bool decode(
const Node& node, Node& rhs) {
65 struct convert<
std::string> {
66 static Node encode(
const std::string& rhs) {
return Node(rhs); }
68 static bool decode(
const Node& node, std::string& rhs) {
78 struct convert<const char*> {
79 static Node encode(
const char* rhs) {
return Node(rhs); }
83 struct convert<char*> {
84 static Node encode(
const char* rhs) {
return Node(rhs); }
87 template <std::
size_t N>
88 struct convert<char[N]> {
89 static Node encode(
const char* rhs) {
return Node(rhs); }
93 struct convert<_Null> {
94 static Node encode(
const _Null& ) {
return Node(); }
96 static bool decode(
const Node& node, _Null& ) {
101 namespace conversion {
102 template <
typename T>
103 typename std::enable_if< std::is_floating_point<T>::value,
void>::type
104 inner_encode(
const T& rhs, std::stringstream& stream){
105 if (std::isnan(rhs)) {
107 }
else if (std::isinf(rhs)) {
108 if (std::signbit(rhs)) {
118 template <
typename T>
119 typename std::enable_if<!std::is_floating_point<T>::value,
void>::type
120 inner_encode(
const T& rhs, std::stringstream& stream){
124 template <
typename T>
125 typename std::enable_if<(std::is_same<T, unsigned char>::value ||
126 std::is_same<T, signed char>::value),
bool>::type
127 ConvertStreamTo(std::stringstream& stream, T& rhs) {
129 if ((stream >> std::noskipws >> num) && (stream >> std::ws).eof()) {
130 if (num >= (std::numeric_limits<T>::min)() &&
131 num <= (std::numeric_limits<T>::max)()) {
132 rhs =
static_cast<T
>(num);
139 template <
typename T>
140 typename std::enable_if<!(std::is_same<T, unsigned char>::value ||
141 std::is_same<T, signed char>::value),
bool>::type
142 ConvertStreamTo(std::stringstream& stream, T& rhs) {
143 if ((stream >> std::noskipws >> rhs) && (stream >> std::ws).eof()) {
150 #define YAML_DEFINE_CONVERT_STREAMABLE(type, negative_op) \ 152 struct convert<type> { \ 154 static Node encode(const type& rhs) { \ 155 std::stringstream stream; \ 156 stream.precision(std::numeric_limits<type>::max_digits10); \ 157 conversion::inner_encode(rhs, stream); \ 158 return Node(stream.str()); \ 161 static bool decode(const Node& node, type& rhs) { \ 162 if (node.Type() != NodeType::Scalar) { \ 165 const std::string& input = node.Scalar(); \ 166 std::stringstream stream(input); \ 167 stream.unsetf(std::ios::dec); \ 168 if ((stream.peek() == '-') && std::is_unsigned<type>::value) { \ 171 if (conversion::ConvertStreamTo(stream, rhs)) { \ 174 if (std::numeric_limits<type>::has_infinity) { \ 175 if (conversion::IsInfinity(input)) { \ 176 rhs = std::numeric_limits<type>::infinity(); \ 178 } else if (conversion::IsNegativeInfinity(input)) { \ 179 rhs = negative_op std::numeric_limits<type>::infinity(); \ 184 if (std::numeric_limits<type>::has_quiet_NaN) { \ 185 if (conversion::IsNaN(input)) { \ 186 rhs = std::numeric_limits<type>::quiet_NaN(); \ 195 #define YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(type) \ 196 YAML_DEFINE_CONVERT_STREAMABLE(type, -) 198 #define YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(type) \ 199 YAML_DEFINE_CONVERT_STREAMABLE(type, +) 201 YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(
int);
202 YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(
short);
203 YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(
long);
204 YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(
long long);
205 YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(
unsigned);
206 YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(
unsigned short);
207 YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(
unsigned long);
208 YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(
unsigned long long);
210 YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(
char);
211 YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(
signed char);
212 YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED(
unsigned char);
214 YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(
float);
215 YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(
double);
216 YAML_DEFINE_CONVERT_STREAMABLE_SIGNED(
long double);
218 #undef YAML_DEFINE_CONVERT_STREAMABLE_SIGNED 219 #undef YAML_DEFINE_CONVERT_STREAMABLE_UNSIGNED 220 #undef YAML_DEFINE_CONVERT_STREAMABLE 224 struct convert<bool> {
225 static Node encode(
bool rhs) {
return rhs ? Node(
"true") : Node(
"false"); }
227 YAML_CPP_API
static bool decode(
const Node& node,
bool& rhs);
231 template <
typename K,
typename V,
typename C,
typename A>
232 struct convert<
std::map<K, V, C, A>> {
233 static Node encode(
const std::map<K, V, C, A>& rhs) {
234 Node node(NodeType::Map);
235 for (
const auto& element : rhs)
236 node.force_insert(element.first, element.second);
240 static bool decode(
const Node& node, std::map<K, V, C, A>& rhs) {
245 for (
const auto& element : node)
246 #if defined(__GNUC__) && __GNUC__ < 4 248 rhs[element.first.template as<K>()] = element.second.template as<V>();
250 rhs[element.first.as<K>()] = element.second.as<V>();
257 template <
typename K,
typename V,
typename H,
typename P,
typename A>
258 struct convert<
std::unordered_map<K, V, H, P, A>> {
259 static Node encode(
const std::unordered_map<K, V, H, P, A>& rhs) {
260 Node node(NodeType::Map);
261 for (
const auto& element : rhs)
262 node.force_insert(element.first, element.second);
266 static bool decode(
const Node& node, std::unordered_map<K, V, H, P, A>& rhs) {
271 for (
const auto& element : node)
272 #if defined(__GNUC__) && __GNUC__ < 4 274 rhs[element.first.template as<K>()] = element.second.template as<V>();
276 rhs[element.first.as<K>()] = element.second.as<V>();
283 template <
typename T,
typename A>
284 struct convert<
std::vector<T, A>> {
285 static Node encode(
const std::vector<T, A>& rhs) {
286 Node node(NodeType::Sequence);
287 for (
const auto& element : rhs)
288 node.push_back(element);
292 static bool decode(
const Node& node, std::vector<T, A>& rhs) {
293 if (!node.IsSequence())
297 for (
const auto& element : node)
298 #if defined(__GNUC__) && __GNUC__ < 4 300 rhs.push_back(element.template as<T>());
302 rhs.push_back(element.as<T>());
309 template <
typename T,
typename A>
310 struct convert<
std::list<T,A>> {
311 static Node encode(
const std::list<T,A>& rhs) {
312 Node node(NodeType::Sequence);
313 for (
const auto& element : rhs)
314 node.push_back(element);
318 static bool decode(
const Node& node, std::list<T,A>& rhs) {
319 if (!node.IsSequence())
323 for (
const auto& element : node)
324 #if defined(__GNUC__) && __GNUC__ < 4 326 rhs.push_back(element.template as<T>());
328 rhs.push_back(element.as<T>());
335 template <
typename T, std::
size_t N>
336 struct convert<
std::array<T, N>> {
337 static Node encode(
const std::array<T, N>& rhs) {
338 Node node(NodeType::Sequence);
339 for (
const auto& element : rhs) {
340 node.push_back(element);
345 static bool decode(
const Node& node, std::array<T, N>& rhs) {
346 if (!isNodeValid(node)) {
350 for (
auto i = 0u; i < node.size(); ++i) {
351 #if defined(__GNUC__) && __GNUC__ < 4 353 rhs[i] = node[i].template as<T>();
355 rhs[i] = node[i].as<T>();
362 static bool isNodeValid(
const Node& node) {
363 return node.IsSequence() && node.size() == N;
369 template <
typename T>
370 struct convert<
std::valarray<T>> {
371 static Node encode(
const std::valarray<T>& rhs) {
372 Node node(NodeType::Sequence);
373 for (
const auto& element : rhs) {
374 node.push_back(element);
379 static bool decode(
const Node& node, std::valarray<T>& rhs) {
380 if (!node.IsSequence()) {
384 rhs.resize(node.size());
385 for (
auto i = 0u; i < node.size(); ++i) {
386 #if defined(__GNUC__) && __GNUC__ < 4 388 rhs[i] = node[i].template as<T>();
390 rhs[i] = node[i].as<T>();
399 template <
typename T,
typename U>
400 struct convert<
std::pair<T, U>> {
401 static Node encode(
const std::pair<T, U>& rhs) {
402 Node node(NodeType::Sequence);
403 node.push_back(rhs.first);
404 node.push_back(rhs.second);
408 static bool decode(
const Node& node, std::pair<T, U>& rhs) {
409 if (!node.IsSequence())
411 if (node.size() != 2)
414 #if defined(__GNUC__) && __GNUC__ < 4 416 rhs.first = node[0].template as<T>();
418 rhs.first = node[0].as<T>();
420 #if defined(__GNUC__) && __GNUC__ < 4 422 rhs.second = node[1].template as<U>();
424 rhs.second = node[1].as<U>();
432 struct convert<Binary> {
433 static Node encode(
const Binary& rhs) {
434 return Node(EncodeBase64(rhs.data(), rhs.size()));
437 static bool decode(
const Node& node, Binary& rhs) {
438 if (!node.IsScalar())
441 std::vector<unsigned char> data = DecodeBase64(node.Scalar());
442 if (data.empty() && !node.Scalar().empty())
451 #endif // NODE_CONVERT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 Definition: gtest-internal.h:1322