From 320b02b14ac9eed2dcafbe6d7d43a0526d3bd0d1 Mon Sep 17 00:00:00 2001 From: Michael Welsh Duggan Date: Thu, 13 Aug 2015 10:51:00 -0400 Subject: [PATCH 1/2] Allow using a Node as the key in force_insert. Node::force_insert() uses convert<> to convert its key to a node. Add a specialization for convert. --- include/yaml-cpp/node/convert.h | 11 +++++++++++ test/node/node_test.cpp | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index cbd3189..f388a67 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -43,6 +43,17 @@ inline bool IsNaN(const std::string& input) { } } +// Node +template <> +struct convert { + static Node encode(const Node& rhs) { return rhs; } + + static bool decode(const Node& node, Node& rhs) { + rhs.reset(node); + return true; + } +}; + // std::string template <> struct convert { diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index b00c69d..4105e72 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -80,6 +80,25 @@ TEST(NodeTest, MapWithUndefinedValues) { EXPECT_EQ(2, node.size()); } +TEST(NodeTest, MapForceInsert) { + Node node; + Node k1("k1"); + Node k2("k2"); + Node v1("v1"); + Node v2("v2"); + node[k1] = v1; + node[k2] = v1; + EXPECT_TRUE(node.IsMap()); + EXPECT_EQ("v1", node["k1"].as()); + EXPECT_EQ("v1", node["k2"].as()); + EXPECT_EQ(2, node.size()); + + node.force_insert(k2, v2); + EXPECT_EQ("v1", node["k1"].as()); + EXPECT_EQ("v2", node["k2"].as()); + EXPECT_EQ(2, node.size()); +} + TEST(NodeTest, UndefinedConstNodeWithFallback) { Node node; const Node& cn = node; From 97d56c3f3608331baaee26e17d2f116d799a7edc Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Sun, 22 Nov 2015 11:27:55 -0600 Subject: [PATCH 2/2] Remove 'const' modifier on return of Node::as. This enables the return value to be moved, rather than copied. --- include/yaml-cpp/node/impl.h | 6 +++--- include/yaml-cpp/node/node.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index 69db1b7..50af6b6 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -87,7 +87,7 @@ struct as_if { explicit as_if(const Node& node_) : node(node_) {} const Node& node; - const T operator()(const S& fallback) const { + T operator()(const S& fallback) const { if (!node.m_pNode) return fallback; @@ -140,14 +140,14 @@ struct as_if { // access functions template -inline const T Node::as() const { +inline T Node::as() const { if (!m_isValid) throw InvalidNode(); return as_if(*this)(); } template -inline const T Node::as(const S& fallback) const { +inline T Node::as(const S& fallback) const { if (!m_isValid) return fallback; return as_if(*this)(fallback); diff --git a/include/yaml-cpp/node/node.h b/include/yaml-cpp/node/node.h index 4ec3543..1207f6e 100644 --- a/include/yaml-cpp/node/node.h +++ b/include/yaml-cpp/node/node.h @@ -63,9 +63,9 @@ class YAML_CPP_API Node { // access template - const T as() const; + T as() const; template - const T as(const S& fallback) const; + T as(const S& fallback) const; const std::string& Scalar() const; const std::string& Tag() const;