From 857bdd55e92b5910852d1215bf5cef43e78b5ecb Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Sun, 12 Jul 2015 18:44:52 +0200 Subject: Add initializer list constructors for tag_list --- include/tag_list.h | 25 +++++++++++++++++++++++++ test/nbttest.cpp | 13 +++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/include/tag_list.h b/include/tag_list.h index 2603e4a5ca..594d630547 100644 --- a/include/tag_list.h +++ b/include/tag_list.h @@ -28,6 +28,12 @@ namespace nbt { +//Forward declarations +class tag_list; +class tag_compound; +class tag_byte_array; +class tag_int_array; + ///Tag that contains multiple unnamed tags of the same type class tag_list : public detail::crtp_tag { @@ -49,6 +55,25 @@ public: ///Constructs an empty list with the given content type tag_list(tag_type type); + ///Constructs a list with the given contents + tag_list(std::initializer_list init); + tag_list(std::initializer_list init); + tag_list(std::initializer_list init); + tag_list(std::initializer_list init); + tag_list(std::initializer_list init); + tag_list(std::initializer_list init); + tag_list(std::initializer_list init); + tag_list(std::initializer_list init); + tag_list(std::initializer_list init); + tag_list(std::initializer_list init); + tag_list(std::initializer_list init); + + /** + * @brief Constructs a list with the given contents + * @throw std::bad_cast if the tags are not all of the same type + */ + tag_list(std::initializer_list init); + /** * @brief Accesses a tag by index with bounds checking * diff --git a/test/nbttest.cpp b/test/nbttest.cpp index 4c2fa548c5..932bdd2301 100644 --- a/test/nbttest.cpp +++ b/test/nbttest.cpp @@ -224,7 +224,7 @@ void test_tag_list() EXPECT_EXCEPTION(list.push_back(value(tag_int(42))), std::bad_cast); EXPECT_EXCEPTION(list.emplace_back(), std::bad_cast); - //ASSERT(list == tag_list{"foo", "bar"}); + ASSERT((list == tag_list{"foo", "bar"})); ASSERT(list[0] == tag_string("foo")); ASSERT(std::string(list.at(1)) == "bar"); @@ -245,7 +245,9 @@ void test_tag_list() } list.pop_back(); - //ASSERT(list == tag_list{"foo"}); + ASSERT(list == tag_list{"foo"}); + ASSERT(list == tag_list{std::string("foo")}); + ASSERT((list != tag_list{2, 3, 5, 7})); list.clear(); ASSERT(list.size() == 0); @@ -254,6 +256,13 @@ void test_tag_list() ASSERT(tag_list() == tag_list(tag_type::Int)); ASSERT(tag_list(tag_type::Short) == tag_list(tag_type::Int)); + + tag_list short_list{int16_t(25), int16_t(36)}; + ASSERT(short_list.el_type() == tag_type::Short); + ASSERT((short_list != tag_list{25, 36})); + ASSERT((short_list == tag_list{value(tag_short(25)), value(tag_short(36))})); + + EXPECT_EXCEPTION((tag_list{value(tag_byte(4)), value(tag_int(5))}), std::bad_cast); std::clog << "test_tag_list passed" << std::endl; } -- cgit 0.0.5-2-1-g0f52