diff --git a/include/yaml-cpp/node/detail/impl.h b/include/yaml-cpp/node/detail/impl.h index 88a51de..2be4f78 100644 --- a/include/yaml-cpp/node/detail/impl.h +++ b/include/yaml-cpp/node/detail/impl.h @@ -58,9 +58,7 @@ struct get_idx::value>::type> { template struct remove_idx { - static char remove(std::vector& /* sequence */, const Key& /* key */) { - return 0; - } + static bool remove(std::vector&, const Key&) { return false; } }; template @@ -68,13 +66,13 @@ struct remove_idx< Key, typename std::enable_if::value && !std::is_same::value>::type> { - static char remove(std::vector& sequence, const Key& key) { - if (key >= sequence.size()) { - return 0; - } else { - sequence.erase(sequence.begin() + key); - return 1; - } + static bool remove(std::vector& sequence, const Key& key) { + if (key >= sequence.size()) { + return false; + } else { + sequence.erase(sequence.begin() + key); + return true; + } } }; @@ -82,10 +80,10 @@ template struct remove_idx::value>::type> { - static char remove(std::vector& sequence, const Key& key) { + static bool remove(std::vector& sequence, const Key& key) { return key >= 0 ? remove_idx::remove( sequence, static_cast(key)) - : 0; + : false; } }; @@ -162,29 +160,25 @@ inline node& node_data::get(const Key& key, shared_memory_holder pMemory) { template inline bool node_data::remove(const Key& key, shared_memory_holder pMemory) { - if (m_type == NodeType::Sequence) { - char result = remove_idx::remove(m_sequence, key); - if (result == 0) return false; - if (result == 1) return true; - convert_to_map(pMemory); - } - if (m_type == NodeType::Map) { + if (m_type == NodeType::Sequence) { + return remove_idx::remove(m_sequence, key); + } else if (m_type == NodeType::Map) { + kv_pairs::iterator it = m_undefinedPairs.begin(); + while (it != m_undefinedPairs.end()) { + kv_pairs::iterator jt = std::next(it); + if (it->first->equals(key, pMemory)) { + m_undefinedPairs.erase(it); + } + it = jt; + } - kv_pairs::iterator it = m_undefinedPairs.begin(); - while (it != m_undefinedPairs.end()) { - kv_pairs::iterator jt = std::next(it); - if (it->first->equals(key, pMemory)) - m_undefinedPairs.erase(it); - it = jt; - } - - for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) { - if (it->first->equals(key, pMemory)) { - m_map.erase(it); - return true; - } - } - } + for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) { + if (it->first->equals(key, pMemory)) { + m_map.erase(it); + return true; + } + } + } return false; } diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index 44eb5fd..61ba3e6 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -1,10 +1,10 @@ -#include "yaml-cpp/emitter.h" -#include "yaml-cpp/node/emit.h" #include "yaml-cpp/node/node.h" -#include "yaml-cpp/node/impl.h" +#include "yaml-cpp/emitter.h" #include "yaml-cpp/node/convert.h" -#include "yaml-cpp/node/iterator.h" #include "yaml-cpp/node/detail/impl.h" +#include "yaml-cpp/node/emit.h" +#include "yaml-cpp/node/impl.h" +#include "yaml-cpp/node/iterator.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -78,6 +78,16 @@ TEST(NodeTest, MapElementRemoval) { EXPECT_TRUE(!node["foo"]); } +TEST(NodeTest, MapIntegerElementRemoval) { + Node node; + node[1] = "hello"; + node[2] = 'c'; + node["foo"] = "bar"; + EXPECT_TRUE(node.IsMap()); + node.remove(1); + EXPECT_TRUE(node.IsMap()); +} + TEST(NodeTest, SimpleAssignSequence) { Node node; node[0] = 10; diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index 0f6b500..931884f 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -14,8 +14,3 @@ add_sources(read.cpp) add_executable(read read.cpp) target_link_libraries(read yaml-cpp) set_target_properties(read PROPERTIES COMPILE_FLAGS "-std=c++11") - -add_sources(demo.cpp) -add_executable(demo demo.cpp) -target_link_libraries(demo yaml-cpp) -set_target_properties(demo PROPERTIES COMPILE_FLAGS "-std=c++11") diff --git a/util/demo.cpp b/util/demo.cpp deleted file mode 100644 index 0ece82b..0000000 --- a/util/demo.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "yaml-cpp/yaml.h" -#include -#include -#include -#include -#include - -using namespace std; -using namespace YAML; -int main(){ - Node root; - - //rooe[0] = 2; - //root[1] = 5; - //root[2] = 77; - //root[3] = 324; - - - root[0] = 'h'; - root[1] = 'e'; - root[2] = 'l'; - root[3] = 'o'; - - - if(root.IsSequence()){ - cout << "sequence" << endl; - cout << root << endl; - } - - if(root.remove(1)){ - cout << "remove success" << endl; - } - - if(root.IsMap()){ - cout << "map" << endl; - } - - if(root.IsSequence()){ - cout << "sequence" << endl; - } - - cout << root << endl; - - return 0; -}