Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions include/yaml-cpp/node/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,12 @@ inline void Node::Assign(char* rhs) {
}

inline Node& Node::operator=(const Node& rhs) {
if (!m_isValid || !rhs.m_isValid)
throw InvalidNode();
if (is(rhs))
return *this;
AssignNode(rhs);
AssignData(rhs);
return *this;
}

inline Node& Node::operator=(const NodeAlias& rhs) {
AssignNode(rhs.m_node);
return *this;
}

Expand Down
10 changes: 10 additions & 0 deletions include/yaml-cpp/node/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct iterator_value;
} // namespace YAML

namespace YAML {
class YAML_CPP_API NodeAlias;
class YAML_CPP_API Node {
public:
friend class NodeBuilder;
Expand Down Expand Up @@ -81,6 +82,7 @@ class YAML_CPP_API Node {
template <typename T>
Node& operator=(const T& rhs);
Node& operator=(const Node& rhs);
Node& operator=(const NodeAlias& rhs);
void reset(const Node& rhs = Node());

// size/iterator
Expand Down Expand Up @@ -134,6 +136,14 @@ class YAML_CPP_API Node {
mutable detail::node* m_pNode;
};

class NodeAlias {
friend class Node;
public:
NodeAlias(const Node& node) : m_node(node) {}
private:
const Node& m_node;
};

YAML_CPP_API bool operator==(const Node& lhs, const Node& rhs);

YAML_CPP_API Node Clone(const Node& node);
Expand Down
1 change: 0 additions & 1 deletion test/integration/handler_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "handler_test.h"
#include "specexamples.h" // IWYU pragma: keep
#include "yaml-cpp/yaml.h" // IWYU pragma: keep

#include "gmock/gmock.h"
Expand Down
16 changes: 16 additions & 0 deletions test/integration/load_node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ TEST(LoadNodeTest, IterateMap) {
EXPECT_EQ(3, i);
}

TEST(LoadNodeTest, AliasMultipleAssign) {
Node doc = Load("{A: &DEFAULT {str: string, int: 42, float: 3.1415}, B: *DEFAULT}");

for (YAML::const_iterator it = doc.begin(); it != doc.end(); ++it) {
SCOPED_TRACE("group " + it->first.as<std::string>());
Node value;

value = it->second["str"];
EXPECT_STREQ(value.as<std::string>().c_str(), "string");
value = it->second["float"];
EXPECT_EQ(value.as<float>(), 3.1415f);
value = it->second["int"];
EXPECT_EQ(value.as<int>(), 42);
}
}

#ifdef BOOST_FOREACH
TEST(LoadNodeTest, ForEach) {
Node node = Load("[1, 3, 5, 7]");
Expand Down
10 changes: 5 additions & 5 deletions test/node/node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ TEST(NodeTest, StdPair) {
TEST(NodeTest, SimpleAlias) {
Node node;
node["foo"] = "value";
node["bar"] = node["foo"];
node["bar"] = NodeAlias(node["foo"]);
EXPECT_EQ("value", node["foo"].as<std::string>());
EXPECT_EQ("value", node["bar"].as<std::string>());
EXPECT_EQ(node["bar"], node["foo"]);
Expand All @@ -274,7 +274,7 @@ TEST(NodeTest, AliasAsKey) {

TEST(NodeTest, SelfReferenceSequence) {
Node node;
node[0] = node;
node[0] = NodeAlias(node);
EXPECT_TRUE(node.IsSequence());
EXPECT_EQ(1, node.size());
EXPECT_EQ(node, node[0]);
Expand All @@ -284,7 +284,7 @@ TEST(NodeTest, SelfReferenceSequence) {

TEST(NodeTest, ValueSelfReferenceMap) {
Node node;
node["key"] = node;
node["key"] = NodeAlias(node);
EXPECT_TRUE(node.IsMap());
EXPECT_EQ(1, node.size());
EXPECT_EQ(node, node["key"]);
Expand All @@ -302,7 +302,7 @@ TEST(NodeTest, KeySelfReferenceMap) {

TEST(NodeTest, SelfReferenceMap) {
Node node;
node[node] = node;
node[node] = NodeAlias(node);
EXPECT_TRUE(node.IsMap());
EXPECT_EQ(1, node.size());
EXPECT_EQ(node, node[node]);
Expand All @@ -322,7 +322,7 @@ TEST(NodeTest, TempMapVariable) {
TEST(NodeTest, TempMapVariableAlias) {
Node node;
Node tmp = node["key"];
tmp = node["other"];
tmp = NodeAlias(node["other"]);
node["other"] = "value";
EXPECT_TRUE(node.IsMap());
EXPECT_EQ(2, node.size());
Expand Down