diff options
| author | ljfa-ag <ljfa-ag@web.de> | 2015-07-12 18:44:52 +0200 |
|---|---|---|
| committer | ljfa-ag <ljfa-ag@web.de> | 2015-07-12 18:44:52 +0200 |
| commit | 857bdd55e92b5910852d1215bf5cef43e78b5ecb (patch) | |
| tree | 5d8b5e773aa09a81f8207799afab0bdb762b4db0 | |
| parent | 65750c0c2941c7c71640ad1b9bc8ff43b45889a9 (diff) | |
| download | Project-Tick-857bdd55e92b5910852d1215bf5cef43e78b5ecb.tar.gz Project-Tick-857bdd55e92b5910852d1215bf5cef43e78b5ecb.zip | |
Add initializer list constructors for tag_list
| -rw-r--r-- | include/tag_list.h | 25 | ||||
| -rw-r--r-- | 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<tag_list> { @@ -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<int8_t> init); + tag_list(std::initializer_list<int16_t> init); + tag_list(std::initializer_list<int32_t> init); + tag_list(std::initializer_list<int64_t> init); + tag_list(std::initializer_list<float> init); + tag_list(std::initializer_list<double> init); + tag_list(std::initializer_list<std::string> init); + tag_list(std::initializer_list<tag_byte_array> init); + tag_list(std::initializer_list<tag_list> init); + tag_list(std::initializer_list<tag_compound> init); + tag_list(std::initializer_list<tag_int_array> 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<value> 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<tag_compound>(), 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; } |
