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
6 changes: 6 additions & 0 deletions include/yaml-cpp/node/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,12 @@ inline void Node::force_insert(const Key& key, const Value& value) {
m_pNode->force_insert(key, value, m_pMemory);
}

template <typename Key>
inline bool Node::contains(const Key& key) const {
EnsureNodeExists();
return ((const detail::node*)m_pNode)->get(key, m_pMemory) != nullptr;
}

// free functions
inline bool operator==(const Node& lhs, const Node& rhs) { return lhs.is(rhs); }
} // namespace YAML
Expand Down
3 changes: 3 additions & 0 deletions include/yaml-cpp/node/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ class YAML_CPP_API Node {
template <typename Key, typename Value>
void force_insert(const Key& key, const Value& value);

template <typename Key>
bool contains(const Key& key) const;

private:
enum Zombie { ZombieNode };
explicit Node(Zombie);
Expand Down
25 changes: 25 additions & 0 deletions test/node/node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,31 @@ TEST(NodeTest, MissingKey) {
EXPECT_THROW(node["bar"].as<std::string>(), InvalidNode);
}

TEST(NodeTest, MapContains) {
Node node, key;
node["foo"] = "value";
node["bar"] = "eulav";
node[1] = "hello";
key["test"] = "asdf";
key["test2"] = "ghjkl";
node[key] = 123;
EXPECT_TRUE(node.contains("foo"));
EXPECT_TRUE(node.contains("bar"));
EXPECT_TRUE(node.contains(1));
EXPECT_TRUE(node.contains(key));
EXPECT_TRUE(!node.contains("baz"));
EXPECT_TRUE(!node.contains(2));

node.remove("foo");
node.remove(key);
EXPECT_TRUE(!node.contains("foo"));
EXPECT_TRUE(node.contains("bar"));
EXPECT_TRUE(node.contains(1));
EXPECT_TRUE(!node.contains(key));
EXPECT_TRUE(!node.contains("baz"));
EXPECT_TRUE(!node.contains(2));
}

TEST(NodeTest, MapIntegerElementRemoval) {
Node node;
node[1] = "hello";
Expand Down
Loading