From 0577e90fd1d73777e0c73995ac83bb2e0fc77db3 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Sun, 2 Aug 2015 18:33:39 +0200 Subject: Add reset method to tag_list Changes to doxygen --- include/tag_list.h | 21 ++++++++++++++++++--- src/tag_list.cpp | 6 ++++++ test/nbttest.cpp | 12 +++++++++++- 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({3, 4, 5}) @endcode + * @param init list of values from which the elements are constructed */ template static tag_list of(std::initializer_list 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(17); + ASSERT(list.el_type() == tag_type::Int); + + list.reset(tag_type::Float); + ASSERT(list.el_type() == tag_type::Float); + list.emplace_back(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)); -- cgit 0.0.5-2-1-g0f52