Support as<uint8_t>/as<int8_t>.

Fix issue 844/848.
This commit is contained in:
Chen
2020-06-15 23:31:59 +08:00
committed by GitHub
parent 33316d531b
commit 4dbfeb0bbc
3 changed files with 55 additions and 17 deletions

View File

@@ -15,7 +15,6 @@
#include <sstream>
#include <type_traits>
#include <vector>
#include <type_traits>
#include "yaml-cpp/binary.h"
#include "yaml-cpp/node/impl.h"
@@ -114,6 +113,31 @@ typename std::enable_if<!std::is_floating_point<T>::value, void>::type
inner_encode(const T& rhs, std::stringstream& stream){
stream << rhs;
}
template <typename T>
typename std::enable_if<(std::is_same<T, unsigned char>::value ||
std::is_same<T, signed char>::value), bool>::type
ConvertStreamTo(std::stringstream& stream, T& rhs) {
int num;
if ((stream >> std::noskipws >> num) && (stream >> std::ws).eof()) {
if (num >= std::numeric_limits<T>::min() &&
num <= std::numeric_limits<T>::max()) {
rhs = num;
return true;
}
}
return false;
}
template <typename T>
typename std::enable_if<!(std::is_same<T, unsigned char>::value ||
std::is_same<T, signed char>::value), bool>::type
ConvertStreamTo(std::stringstream& stream, T& rhs) {
if ((stream >> std::noskipws >> rhs) && (stream >> std::ws).eof()) {
return true;
}
return false;
}
}
#define YAML_DEFINE_CONVERT_STREAMABLE(type, negative_op) \
@@ -137,7 +161,7 @@ inner_encode(const T& rhs, std::stringstream& stream){
if ((stream.peek() == '-') && std::is_unsigned<type>::value) { \
return false; \
} \
if ((stream >> std::noskipws >> rhs) && (stream >> std::ws).eof()) { \
if (conversion::ConvertStreamTo(stream, rhs)) { \
return true; \
} \
if (std::numeric_limits<type>::has_infinity) { \