diff options
| -rw-r--r-- | include/tag_compound.h | 12 | ||||
| -rw-r--r-- | src/tag_compound.cpp | 7 | ||||
| -rw-r--r-- | test/nbttest.cpp | 8 |
3 files changed, 14 insertions, 13 deletions
diff --git a/include/tag_compound.h b/include/tag_compound.h index acde3a1e2b..a0adf07282 100644 --- a/include/tag_compound.h +++ b/include/tag_compound.h @@ -68,19 +68,21 @@ public: * * If the given key already exists, assigns the tag to it. * Otherwise, it is inserted under the given key. - * @return true if the key did not exist + * @return a pair of the iterator to the value and a bool indicating + * whether the key did not exist */ - bool put(const std::string& key, std::unique_ptr<tag>&& t); + std::pair<iterator, bool> put(const std::string& key, std::unique_ptr<tag>&& t); /** * @brief Constructs and assigns or inserts a tag into the compound * * Constructs a new tag of type @c T with the given args and inserts * or assigns it to the given key. - * @return true if the key did not exist + * @return a pair of the iterator to the value and a bool indicating + * whether the key did not exist */ template<class T, class... Args> - bool emplace(const std::string& key, Args&&... args); + std::pair<iterator, bool> emplace(const std::string& key, Args&&... args); /** * @brief Erases a tag from the compound @@ -113,7 +115,7 @@ private: }; template<class T, class... Args> -bool tag_compound::emplace(const std::string& key, Args&&... args) +std::pair<tag_compound::iterator, bool> tag_compound::emplace(const std::string& key, Args&&... args) { return put(key, std::unique_ptr<tag>(new T(std::forward<Args>(args)...))); } diff --git a/src/tag_compound.cpp b/src/tag_compound.cpp index a2b91826b6..b0a36c30f4 100644 --- a/src/tag_compound.cpp +++ b/src/tag_compound.cpp @@ -43,18 +43,17 @@ value& tag_compound::operator[](const std::string& key) return tags[key]; } -bool tag_compound::put(const std::string& key, std::unique_ptr<tag>&& t) +std::pair<tag_compound::iterator, bool> tag_compound::put(const std::string& key, std::unique_ptr<tag>&& t) { auto it = tags.find(key); if(it != tags.end()) { it->second.set_ptr(std::move(t)); - return false; + return {it, false}; } else { - tags.emplace(key, value(std::move(t))); - return true; + return tags.emplace(key, value(std::move(t))); } } diff --git a/test/nbttest.cpp b/test/nbttest.cpp index 19a9ddc1b9..64db9efe91 100644 --- a/test/nbttest.cpp +++ b/test/nbttest.cpp @@ -143,10 +143,10 @@ void test_tag_compound() comp.clear(); ASSERT(comp == tag_compound{}); - ASSERT(comp.put("abc", std::unique_ptr<tag>(new tag_double(6.0))) == true); - ASSERT(comp.put("abc", std::unique_ptr<tag>(new tag_long(-28))) == false); - ASSERT(comp.emplace<tag_string>("def", "ghi") == true); - ASSERT(comp.emplace<tag_byte>("def", 4) == false); + ASSERT(comp.put("abc", std::unique_ptr<tag>(new tag_double(6.0))).second == true); + ASSERT(comp.put("abc", std::unique_ptr<tag>(new tag_long(-28))).second == false); + ASSERT(comp.emplace<tag_string>("def", "ghi").second == true); + ASSERT(comp.emplace<tag_byte>("def", 4).second == false); ASSERT((comp == tag_compound{{"abc", tag_long(-28)}, {"def", tag_byte(4)}})); std::clog << "test_tag_compound passed" << std::endl; |
