summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/tag_list.h21
-rw-r--r--src/tag_list.cpp6
-rw-r--r--test/nbttest.cpp12
3 files changed, 35 insertions, 4 deletions
diff --git a/include/tag_list.h b/include/tag_list.h
index 87b2742e92..af5a87d5ee 100644
--- a/include/tag_list.h
+++ b/include/tag_list.h
@@ -32,6 +32,13 @@ namespace nbt
/**
* @brief Tag that contains multiple unnamed tags of the same type
*
+ * All the tags contained in the list have the same type, which can be queried
+ * with el_type().
+ *
+ * If the list is empty, the type can be undetermined, in which case el_type()
+ * will return tag_type::Null. The type will then be set when the first tag
+ * is added to the list.
+ *
* The list's behavior is undefined if the contained values are changed in a
* way that their type differs from the list's content type.
*/
@@ -46,8 +53,10 @@ public:
static constexpr tag_type type = tag_type::List;
/**
- * @brief Constructs a list with the given contents of type T
- * @param init list of values that are, one by one, given to a constructor of T
+ * @brief Constructs a list of type T with the given values
+ *
+ * Example: @code tag_list::of<tag_byte>({3, 4, 5}) @endcode
+ * @param init list of values from which the elements are constructed
*/
template<class T>
static tag_list of(std::initializer_list<T> init);
@@ -135,6 +144,12 @@ public:
///Erases all tags from the list. Preserves the content type.
void clear();
+ /**
+ * @brief Erases all tags from the list and changes the content type.
+ * @param type the new content type. Can be tag_type::Null to leave it undetermined.
+ */
+ void reset(tag_type type = tag_type::Null);
+
//Iterators
iterator begin();
iterator end();
@@ -147,7 +162,7 @@ public:
* @brief Equality comparison for lists
*
* Lists are considered equal if they contain equal tags. Empty lists
- * are always considered equal.
+ * are always considered equal to each other.
*/
friend bool operator==(const tag_list& lhs, const tag_list& rhs);
friend bool operator!=(const tag_list& lhs, const tag_list& rhs);
diff --git a/src/tag_list.cpp b/src/tag_list.cpp
index 7c7176835b..34112b3eca 100644
--- a/src/tag_list.cpp
+++ b/src/tag_list.cpp
@@ -117,6 +117,12 @@ void tag_list::clear()
tags.clear();
}
+void tag_list::reset(tag_type type)
+{
+ clear();
+ el_type_ = type;
+}
+
auto tag_list::begin() -> iterator { return tags.begin(); }
auto tag_list::end() -> iterator { return tags.end(); }
auto tag_list::begin() const -> const_iterator { return tags.begin(); }
diff --git a/test/nbttest.cpp b/test/nbttest.cpp
index 4bd9cd005d..347dad3e3a 100644
--- a/test/nbttest.cpp
+++ b/test/nbttest.cpp
@@ -294,9 +294,19 @@ void test_tag_list()
ASSERT((list != tag_list{2, 3, 5, 7}));
list.clear();
- ASSERT(list.size() == 0);
+ ASSERT(list.size() == 0 && list.el_type() == tag_type::String);
EXPECT_EXCEPTION(list.push_back(tag_short(25)), std::bad_cast);
EXPECT_EXCEPTION(list.push_back(value(nullptr)), std::bad_cast);
+
+ list.reset();
+ ASSERT(list.el_type() == tag_type::Null);
+ list.emplace_back<tag_int>(17);
+ ASSERT(list.el_type() == tag_type::Int);
+
+ list.reset(tag_type::Float);
+ ASSERT(list.el_type() == tag_type::Float);
+ list.emplace_back<tag_float>(17.0f);
+ ASSERT(list == tag_list({17.0f}));
ASSERT(tag_list() == tag_list(tag_type::Int));
ASSERT(tag_list(tag_type::Short) == tag_list(tag_type::Int));