From 4539d1884e9fb7556a8ec91e83b7ef8f91bb3ff6 Mon Sep 17 00:00:00 2001 From: sxrzh Date: Sun, 5 Jul 2026 01:06:17 +0800 Subject: [PATCH 1/2] feat: add thread-safe contains() function for Node. --- include/yaml-cpp/node/impl.h | 6 ++++++ include/yaml-cpp/node/node.h | 3 +++ test/node/node_test.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index 773847eab..4190b5343 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -394,6 +394,12 @@ inline void Node::force_insert(const Key& key, const Value& value) { m_pNode->force_insert(key, value, m_pMemory); } +template +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 diff --git a/include/yaml-cpp/node/node.h b/include/yaml-cpp/node/node.h index df55f5846..93661209d 100644 --- a/include/yaml-cpp/node/node.h +++ b/include/yaml-cpp/node/node.h @@ -114,6 +114,9 @@ class YAML_CPP_API Node { template void force_insert(const Key& key, const Value& value); + template + bool contains(const Key& key) const; + private: enum Zombie { ZombieNode }; explicit Node(Zombie); diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index 6df7df1c9..f2cf62f90 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -10,6 +10,7 @@ #include "gtest/gtest.h" #include +#include namespace { @@ -199,6 +200,31 @@ TEST(NodeTest, MissingKey) { EXPECT_THROW(node["bar"].as(), 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"; From 470d5e9a0c78fe8efab8634b0958231c23355b9d Mon Sep 17 00:00:00 2001 From: rzh789ABC <124703704+sxrzh@users.noreply.github.com> Date: Sun, 5 Jul 2026 01:18:18 +0800 Subject: [PATCH 2/2] Remove unused include for yaml-cpp/null.h Removed unused yaml-cpp/null.h include. --- test/node/node_test.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index f2cf62f90..b53c28566 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -10,7 +10,6 @@ #include "gtest/gtest.h" #include -#include namespace {