simplify life a little bit

This commit is contained in:
marcel 2021-04-28 17:08:34 +02:00
parent 92559b8ec8
commit 5f5b183275
4 changed files with 25 additions and 18 deletions

View File

@ -180,7 +180,7 @@ struct convert<Vec3> {
static Vec3 decode(const Node& node) { static Vec3 decode(const Node& node) {
if(!node.IsSequence() || node.size() != 3) { if(!node.IsSequence() || node.size() != 3) {
return YAML::conversion::DecodeException(""); throw YAML::conversion::DecodeException("");
} }
Vec3 rhs; Vec3 rhs;

View File

@ -302,7 +302,8 @@ class YAML_CPP_API BadFile : public Exception {
namespace conversion{ namespace conversion{
class DecodeException : public std::runtime_error { class DecodeException : public std::runtime_error {
using runtime_error::runtime_error; public:
DecodeException(const std::string& s="") : std::runtime_error(s) {};
}; };
} }

View File

@ -41,10 +41,16 @@ class DecodeException : public std::runtime_error {
namespace YAML { namespace YAML {
class Binary; class Binary;
struct _Null; struct _Null;
template <typename T> template <typename T>
struct convert; struct convert {
using this_type = T;
};
} // namespace YAML } // namespace YAML
#define BAD_DECODE_EXCEPTION throw YAML::conversion::DecodeException();
namespace YAML { namespace YAML {
namespace conversion { namespace conversion {
inline bool IsInfinity(const std::string& input) { inline bool IsInfinity(const std::string& input) {
@ -81,7 +87,7 @@ struct convert<std::string> {
static std::string decode(const Node& node) { static std::string decode(const Node& node) {
if (!node.IsScalar()) if (!node.IsScalar())
throw conversion::DecodeException(""); BAD_DECODE_EXCEPTION
return node.Scalar(); return node.Scalar();
} }
@ -109,7 +115,7 @@ struct convert<_Null> {
static _Null decode(const Node& node) { static _Null decode(const Node& node) {
if (!node.IsNull()) if (!node.IsNull())
throw conversion::DecodeException(""); BAD_DECODE_EXCEPTION
return _Null(); return _Null();
} }
}; };
@ -176,13 +182,13 @@ ConvertStreamTo(std::stringstream& stream, T& rhs) {
\ \
static type decode(const Node& node) { \ static type decode(const Node& node) { \
if (node.Type() != NodeType::Scalar) { \ if (node.Type() != NodeType::Scalar) { \
throw conversion::DecodeException(""); \ BAD_DECODE_EXCEPTION; \
} \ } \
const std::string& input = node.Scalar(); \ const std::string& input = node.Scalar(); \
std::stringstream stream(input); \ std::stringstream stream(input); \
stream.unsetf(std::ios::dec); \ stream.unsetf(std::ios::dec); \
if ((stream.peek() == '-') && std::is_unsigned<type>::value) { \ if ((stream.peek() == '-') && std::is_unsigned<type>::value) { \
throw conversion::DecodeException(""); \ BAD_DECODE_EXCEPTION \
} \ } \
type rhs; \ type rhs; \
if (conversion::ConvertStreamTo(stream, rhs)) { \ if (conversion::ConvertStreamTo(stream, rhs)) { \
@ -202,7 +208,7 @@ ConvertStreamTo(std::stringstream& stream, T& rhs) {
} \ } \
} \ } \
\ \
throw conversion::DecodeException(""); \ BAD_DECODE_EXCEPTION \
} \ } \
} }
@ -253,7 +259,7 @@ struct convert<std::map<K, V, C, A>> {
static std::map<K, V, C, A> decode(const Node& node) { static std::map<K, V, C, A> decode(const Node& node) {
if (!node.IsMap()) if (!node.IsMap())
throw conversion::DecodeException(""); BAD_DECODE_EXCEPTION
std::map<K, V, C, A> rhs; std::map<K, V, C, A> rhs;
for (const auto& element : node) for (const auto& element : node)
@ -279,7 +285,7 @@ struct convert<std::vector<T, A>> {
static std::vector<T, A> decode(const Node& node) { static std::vector<T, A> decode(const Node& node) {
if (!node.IsSequence()) if (!node.IsSequence())
throw conversion::DecodeException(""); BAD_DECODE_EXCEPTION
std::vector<T, A> rhs; std::vector<T, A> rhs;
for (const auto& element : node) for (const auto& element : node)
@ -305,7 +311,7 @@ struct convert<std::list<T,A>> {
static std::list<T,A> decode(const Node& node) { static std::list<T,A> decode(const Node& node) {
if (!node.IsSequence()) if (!node.IsSequence())
throw conversion::DecodeException(""); BAD_DECODE_EXCEPTION
std::list<T,A> rhs; std::list<T,A> rhs;
for (const auto& element : node) for (const auto& element : node)
@ -332,7 +338,7 @@ struct convert<std::array<T, N>> {
static std::array<T, N> decode(const Node& node) { static std::array<T, N> decode(const Node& node) {
if (!isNodeValid(node)) if (!isNodeValid(node))
throw conversion::DecodeException(""); BAD_DECODE_EXCEPTION
std::array<T, N> rhs; std::array<T, N> rhs;
for (auto i = 0u; i < node.size(); ++i) { for (auto i = 0u; i < node.size(); ++i) {
@ -364,7 +370,7 @@ struct convert<std::pair<T, U>> {
static std::pair<T, U> decode(const Node& node) { static std::pair<T, U> decode(const Node& node) {
if (!node.IsSequence() or node.size() != 2) if (!node.IsSequence() or node.size() != 2)
throw conversion::DecodeException(""); BAD_DECODE_EXCEPTION
std::pair<T, U> rhs; std::pair<T, U> rhs;
#if defined(__GNUC__) && __GNUC__ < 4 #if defined(__GNUC__) && __GNUC__ < 4
@ -392,11 +398,11 @@ struct convert<Binary> {
static Binary decode(const Node& node) { static Binary decode(const Node& node) {
if (!node.IsScalar()) if (!node.IsScalar())
throw conversion::DecodeException(""); BAD_DECODE_EXCEPTION
std::vector<unsigned char> data = DecodeBase64(node.Scalar()); std::vector<unsigned char> data = DecodeBase64(node.Scalar());
if (data.empty() && !node.Scalar().empty()) if (data.empty() && !node.Scalar().empty())
throw conversion::DecodeException(""); BAD_DECODE_EXCEPTION
Binary rhs; Binary rhs;
rhs.swap(data); rhs.swap(data);

View File

@ -40,7 +40,7 @@ bool IsFlexibleCase(const std::string& str) {
namespace YAML { namespace YAML {
bool convert<bool>::decode(const Node& node) { bool convert<bool>::decode(const Node& node) {
if (!node.IsScalar()) if (!node.IsScalar())
throw conversion::DecodeException(""); BAD_DECODE_EXCEPTION
// we can't use iostream bool extraction operators as they don't // we can't use iostream bool extraction operators as they don't
// recognize all possible values in the table below (taken from // recognize all possible values in the table below (taken from
@ -55,7 +55,7 @@ bool convert<bool>::decode(const Node& node) {
}; };
if (!IsFlexibleCase(node.Scalar())) if (!IsFlexibleCase(node.Scalar()))
throw conversion::DecodeException(""); BAD_DECODE_EXCEPTION
for (const auto& name : names) { for (const auto& name : names) {
if (name.truename == tolower(node.Scalar())) { if (name.truename == tolower(node.Scalar())) {
@ -67,6 +67,6 @@ bool convert<bool>::decode(const Node& node) {
} }
} }
throw conversion::DecodeException(""); BAD_DECODE_EXCEPTION
} }
} // namespace YAML } // namespace YAML