summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorljfa-ag <ljfa-ag@web.de>2015-07-09 18:09:06 +0200
committerljfa-ag <ljfa-ag@web.de>2015-07-09 18:09:06 +0200
commit4945e533c37d261e82d34ea129f441be06385cce (patch)
treea8c36c3f35a524f54c2964616722bed1934b3763
parentea71042f79738bcdc08fe3bfa0a2f9cd1c0c03ac (diff)
downloadProject-Tick-4945e533c37d261e82d34ea129f441be06385cce.tar.gz
Project-Tick-4945e533c37d261e82d34ea129f441be06385cce.zip
Create tag_compound::insert method
-rw-r--r--include/tag_compound.h8
-rw-r--r--src/tag_compound.cpp5
-rw-r--r--test/nbttest.cpp7
3 files changed, 19 insertions, 1 deletions
diff --git a/include/tag_compound.h b/include/tag_compound.h
index 80be2f609e..0789bed295 100644
--- a/include/tag_compound.h
+++ b/include/tag_compound.h
@@ -73,10 +73,18 @@ public:
std::pair<iterator, bool> put(const std::string& key, value&& val);
/**
+ * @brief Inserts a tag if the key does not exist
+ * @return a pair of the iterator to the value with the key and a bool
+ * indicating whether the value was actually inserted
+ */
+ std::pair<iterator, bool> insert(const std::string& key, value&& val);
+
+ /**
* @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.
+ * @note Unlike std::map::emplace, this will overwrite existing values
* @return a pair of the iterator to the value and a bool indicating
* whether the key did not exist
*/
diff --git a/src/tag_compound.cpp b/src/tag_compound.cpp
index 4c6c03264e..4ee38c4412 100644
--- a/src/tag_compound.cpp
+++ b/src/tag_compound.cpp
@@ -57,6 +57,11 @@ std::pair<tag_compound::iterator, bool> tag_compound::put(const std::string& key
}
}
+std::pair<tag_compound::iterator, bool> tag_compound::insert(const std::string& key, value&& val)
+{
+ return tags.emplace(key, std::move(val));
+}
+
bool tag_compound::erase(const std::string& key)
{
return tags.erase(key) != 0;
diff --git a/test/nbttest.cpp b/test/nbttest.cpp
index 42f8bd25f4..f4d0405273 100644
--- a/test/nbttest.cpp
+++ b/test/nbttest.cpp
@@ -144,9 +144,14 @@ void test_tag_compound()
ASSERT(comp.put("abc", value(tag_double(6.0))).second == true);
ASSERT(comp.put("abc", value(tag_long(-28))).second == false);
+ ASSERT(comp.insert("ghi", value(tag_string("world"))).second == true);
+ ASSERT(comp.insert("abc", value(tag_string("hello"))).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)}}));
+ ASSERT((comp == tag_compound{
+ {"abc", tag_long(-28)},
+ {"def", tag_byte(4)},
+ {"ghi", tag_string("world")}}));
std::clog << "test_tag_compound passed" << std::endl;
}