Split conversion call that uses std::signbit with unsupported parameters with enable_if (#824)

This commit is contained in:
Dekken
2020-03-11 14:59:16 +01:00
committed by GitHub
parent 1bfbd2be4c
commit b2f89386d8

View File

@@ -23,6 +23,7 @@
#include "yaml-cpp/node/type.h" #include "yaml-cpp/node/type.h"
#include "yaml-cpp/null.h" #include "yaml-cpp/null.h"
namespace YAML { namespace YAML {
class Binary; class Binary;
struct _Null; struct _Null;
@@ -90,27 +91,38 @@ struct convert<_Null> {
} }
}; };
namespace conversion {
template <typename T>
typename std::enable_if< std::is_floating_point<T>::value, void>::type
inner_encode(const T& rhs, std::stringstream& stream){
if (std::isnan(rhs)) {
stream << ".nan";
} else if (std::isinf(rhs)) {
if (std::signbit(rhs)) {
stream << "-.inf";
} else {
stream << ".inf";
}
} else {
stream << rhs;
}
}
template <typename T>
typename std::enable_if<!std::is_floating_point<T>::value, void>::type
inner_encode(const T& rhs, std::stringstream& stream){
stream << rhs;
}
}
#define YAML_DEFINE_CONVERT_STREAMABLE(type, negative_op) \ #define YAML_DEFINE_CONVERT_STREAMABLE(type, negative_op) \
template <> \ template <> \
struct convert<type> { \ struct convert<type> { \
\
static Node encode(const type& rhs) { \ static Node encode(const type& rhs) { \
std::stringstream stream; \ std::stringstream stream; \
stream.precision(std::numeric_limits<type>::max_digits10); \ stream.precision(std::numeric_limits<type>::max_digits10); \
if (std::is_floating_point<type>::value) { \ conversion::inner_encode(rhs, stream); \
if (std::isnan(rhs)) { \
stream << ".nan"; \
} else if (std::isinf(rhs)) { \
if (std::signbit(rhs)) { \
stream << "-.inf"; \
} else { \
stream << ".inf"; \
} \
} else { \
stream << rhs; \
} \
} else { \
stream << rhs; \
} \
return Node(stream.str()); \ return Node(stream.str()); \
} \ } \
\ \