diff --git a/docs/Tutorial.md b/docs/Tutorial.md index 7494b1a..ba44d67 100644 --- a/docs/Tutorial.md +++ b/docs/Tutorial.md @@ -180,7 +180,7 @@ struct convert { static Vec3 decode(const Node& node) { if(!node.IsSequence() || node.size() != 3) { - return YAML::conversion::DecodeException(""); + throw YAML::conversion::DecodeException(""); } Vec3 rhs; diff --git a/include/yaml-cpp/exceptions.h b/include/yaml-cpp/exceptions.h index f08f17a..0d62c3a 100644 --- a/include/yaml-cpp/exceptions.h +++ b/include/yaml-cpp/exceptions.h @@ -302,7 +302,8 @@ class YAML_CPP_API BadFile : public Exception { namespace conversion{ class DecodeException : public std::runtime_error { - using runtime_error::runtime_error; + public: + DecodeException(const std::string& s="") : std::runtime_error(s) {}; }; } diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index 70927dd..9d22c74 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -41,10 +41,16 @@ class DecodeException : public std::runtime_error { namespace YAML { class Binary; struct _Null; + template -struct convert; +struct convert { + using this_type = T; +}; } // namespace YAML +#define BAD_DECODE_EXCEPTION throw YAML::conversion::DecodeException(); + + namespace YAML { namespace conversion { inline bool IsInfinity(const std::string& input) { @@ -81,7 +87,7 @@ struct convert { static std::string decode(const Node& node) { if (!node.IsScalar()) - throw conversion::DecodeException(""); + BAD_DECODE_EXCEPTION return node.Scalar(); } @@ -109,7 +115,7 @@ struct convert<_Null> { static _Null decode(const Node& node) { if (!node.IsNull()) - throw conversion::DecodeException(""); + BAD_DECODE_EXCEPTION return _Null(); } }; @@ -176,13 +182,13 @@ ConvertStreamTo(std::stringstream& stream, T& rhs) { \ static type decode(const Node& node) { \ if (node.Type() != NodeType::Scalar) { \ - throw conversion::DecodeException(""); \ + BAD_DECODE_EXCEPTION; \ } \ const std::string& input = node.Scalar(); \ std::stringstream stream(input); \ stream.unsetf(std::ios::dec); \ if ((stream.peek() == '-') && std::is_unsigned::value) { \ - throw conversion::DecodeException(""); \ + BAD_DECODE_EXCEPTION \ } \ type 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> { static std::map decode(const Node& node) { if (!node.IsMap()) - throw conversion::DecodeException(""); + BAD_DECODE_EXCEPTION std::map rhs; for (const auto& element : node) @@ -279,7 +285,7 @@ struct convert> { static std::vector decode(const Node& node) { if (!node.IsSequence()) - throw conversion::DecodeException(""); + BAD_DECODE_EXCEPTION std::vector rhs; for (const auto& element : node) @@ -305,7 +311,7 @@ struct convert> { static std::list decode(const Node& node) { if (!node.IsSequence()) - throw conversion::DecodeException(""); + BAD_DECODE_EXCEPTION std::list rhs; for (const auto& element : node) @@ -332,7 +338,7 @@ struct convert> { static std::array decode(const Node& node) { if (!isNodeValid(node)) - throw conversion::DecodeException(""); + BAD_DECODE_EXCEPTION std::array rhs; for (auto i = 0u; i < node.size(); ++i) { @@ -364,7 +370,7 @@ struct convert> { static std::pair decode(const Node& node) { if (!node.IsSequence() or node.size() != 2) - throw conversion::DecodeException(""); + BAD_DECODE_EXCEPTION std::pair rhs; #if defined(__GNUC__) && __GNUC__ < 4 @@ -392,11 +398,11 @@ struct convert { static Binary decode(const Node& node) { if (!node.IsScalar()) - throw conversion::DecodeException(""); + BAD_DECODE_EXCEPTION std::vector data = DecodeBase64(node.Scalar()); if (data.empty() && !node.Scalar().empty()) - throw conversion::DecodeException(""); + BAD_DECODE_EXCEPTION Binary rhs; rhs.swap(data); diff --git a/src/convert.cpp b/src/convert.cpp index c299d5f..7dda819 100644 --- a/src/convert.cpp +++ b/src/convert.cpp @@ -40,7 +40,7 @@ bool IsFlexibleCase(const std::string& str) { namespace YAML { bool convert::decode(const Node& node) { if (!node.IsScalar()) - throw conversion::DecodeException(""); + BAD_DECODE_EXCEPTION // we can't use iostream bool extraction operators as they don't // recognize all possible values in the table below (taken from @@ -55,7 +55,7 @@ bool convert::decode(const Node& node) { }; if (!IsFlexibleCase(node.Scalar())) - throw conversion::DecodeException(""); + BAD_DECODE_EXCEPTION for (const auto& name : names) { if (name.truename == tolower(node.Scalar())) { @@ -67,6 +67,6 @@ bool convert::decode(const Node& node) { } } - throw conversion::DecodeException(""); + BAD_DECODE_EXCEPTION } } // namespace YAML