From 7c33b3cdab633e59d159d03797017c90ffee8e18 Mon Sep 17 00:00:00 2001 From: Vincent Cogne Date: Mon, 13 Jun 2016 04:59:31 +0200 Subject: [PATCH 1/2] Add convert specialization for std::array. --- include/yaml-cpp/node/convert.h | 35 +++++++++++++++++++++++++++++++++ test/node/node_test.cpp | 24 ++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index f388a67..f232de7 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -7,6 +7,7 @@ #pragma once #endif +#include #include #include #include @@ -241,6 +242,40 @@ struct convert > { } }; +// std::array +template +struct convert> { + static Node encode(const std::array& rhs) { + Node node(NodeType::Sequence); + for (const auto& element : rhs) { + node.push_back(element); + } + return node; + } + + static bool decode(const Node& node, std::array& rhs) { + if (!isNodeValid(node)) { + return false; + } + + for (auto i = 0u; i < node.size(); ++i) { +#if defined(__GNUC__) && __GNUC__ < 4 + // workaround for GCC 3: + rhs[i] = node[i].template as(); +#else + rhs[i] = node[i].as(); +#endif + } + return true; + } + +private: + + static bool isNodeValid(const Node& node) { + return node.IsSequence() && node.size() == N; + } +}; + // std::pair template struct convert > { diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index 4105e72..2202f72 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -12,6 +12,14 @@ using ::testing::AnyOf; using ::testing::Eq; +#define EXPECT_THROW_REPRESENTATION_EXCEPTION(statement, message) \ + ASSERT_THROW(statement, RepresentationException); \ + try { \ + statement; \ + } catch (const RepresentationException& e) { \ + EXPECT_EQ(e.msg, message); \ + } + namespace YAML { namespace { TEST(NodeTest, SimpleScalar) { @@ -154,6 +162,22 @@ TEST(NodeTest, SimpleSubkeys) { EXPECT_EQ("monkey", node["username"].as()); } +TEST(NodeTest, StdArray) { + std::array evens {{ 2, 4, 6, 8, 10 }}; + Node node; + node["evens"] = evens; + std::array actualEvens = node["evens"].as>(); + EXPECT_EQ(evens, actualEvens); +} + +TEST(NodeTest, StdArrayWrongSize) { + std::array evens {{ 2, 4, 6 }}; + Node node; + node["evens"] = evens; + EXPECT_THROW_REPRESENTATION_EXCEPTION((node["evens"].as>()), + ErrorMsg::BAD_CONVERSION); +} + TEST(NodeTest, StdVector) { std::vector primes; primes.push_back(2); From f74ae543b43c76fa98c3418df05051a64c2623b4 Mon Sep 17 00:00:00 2001 From: Vincent Cogne Date: Mon, 13 Jun 2016 05:10:14 +0200 Subject: [PATCH 2/2] Fix some clang warnings (#378) * Remove extra semicolon * Fix automatic type conversion * Replace dynamic exception specifications by C++11 noexcept * Fix deprecated definition of implicit copy constructor for 'Exception' --- include/yaml-cpp/emitter.h | 4 ++-- include/yaml-cpp/exceptions.h | 6 ++++-- include/yaml-cpp/node/node.h | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/include/yaml-cpp/emitter.h b/include/yaml-cpp/emitter.h index 5bffc25..ef92cc4 100644 --- a/include/yaml-cpp/emitter.h +++ b/include/yaml-cpp/emitter.h @@ -162,12 +162,12 @@ inline Emitter& Emitter::WriteStreamable(T value) { template <> inline void Emitter::SetStreamablePrecision(std::stringstream& stream) { - stream.precision(GetFloatPrecision()); + stream.precision(static_cast(GetFloatPrecision())); } template <> inline void Emitter::SetStreamablePrecision(std::stringstream& stream) { - stream.precision(GetDoublePrecision()); + stream.precision(static_cast(GetDoublePrecision())); } // overloads of insertion diff --git a/include/yaml-cpp/exceptions.h b/include/yaml-cpp/exceptions.h index 285fc33..5d44c05 100644 --- a/include/yaml-cpp/exceptions.h +++ b/include/yaml-cpp/exceptions.h @@ -112,7 +112,9 @@ class Exception : public std::runtime_error { public: Exception(const Mark& mark_, const std::string& msg_) : std::runtime_error(build_what(mark_, msg_)), mark(mark_), msg(msg_) {} - virtual ~Exception() throw() {} + virtual ~Exception() noexcept {} + + Exception(const Exception&) = default; Mark mark; std::string msg; @@ -163,7 +165,7 @@ class TypedKeyNotFound : public KeyNotFound { public: TypedKeyNotFound(const Mark& mark_, const T& key_) : KeyNotFound(mark_, key_), key(key_) {} - virtual ~TypedKeyNotFound() throw() {} + virtual ~TypedKeyNotFound() noexcept {} T key; }; diff --git a/include/yaml-cpp/node/node.h b/include/yaml-cpp/node/node.h index 1207f6e..1ded7d2 100644 --- a/include/yaml-cpp/node/node.h +++ b/include/yaml-cpp/node/node.h @@ -58,7 +58,7 @@ class YAML_CPP_API Node { bool IsMap() const { return Type() == NodeType::Map; } // bool conversions - YAML_CPP_OPERATOR_BOOL(); + YAML_CPP_OPERATOR_BOOL() bool operator!() const { return !IsDefined(); } // access