diff options
| author | ljfa-ag <ljfa-ag@web.de> | 2015-09-05 13:00:31 +0200 |
|---|---|---|
| committer | ljfa-ag <ljfa-ag@web.de> | 2015-09-05 13:00:31 +0200 |
| commit | fd32c7c7d05bc7e823d7e129d16193be4635b0c7 (patch) | |
| tree | 2e35597b74189c0fbb67d213d9f68eb299fb1e37 | |
| parent | 5c6cb9eca80a7bb18beb0fae42426b97a9f99fe0 (diff) | |
| download | Project-Tick-fd32c7c7d05bc7e823d7e129d16193be4635b0c7.tar.gz Project-Tick-fd32c7c7d05bc7e823d7e129d16193be4635b0c7.zip | |
Change some exception types thrown by tag_list
| -rw-r--r-- | include/tag_list.h | 18 | ||||
| -rw-r--r-- | src/tag_list.cpp | 10 | ||||
| -rw-r--r-- | test/nbttest.h | 20 |
3 files changed, 23 insertions, 25 deletions
diff --git a/include/tag_list.h b/include/tag_list.h index c27da8697d..5ae505d4fc 100644 --- a/include/tag_list.h +++ b/include/tag_list.h @@ -23,7 +23,7 @@ #include "crtp_tag.h" #include "tagfwd.h" #include "value_initializer.h" -#include <typeinfo> +#include <stdexcept> #include <vector> namespace nbt @@ -33,14 +33,12 @@ 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(). + * with el_type(). The types of the values contained in the list should not + * be changed to mismatch the element 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. */ class tag_list final : public detail::crtp_tag<tag_list> { @@ -86,7 +84,7 @@ public: /** * @brief Constructs a list with the given contents - * @throw std::bad_cast if the tags are not all of the same type + * @throw std::invalid_argument if the tags are not all of the same type */ tag_list(std::initializer_list<value> init); @@ -111,7 +109,7 @@ public: /** * @brief Assigns a value at the given index - * @throw std::bad_cast if the type of the value does not match the list's + * @throw std::invalid_argument if the type of the value does not match the list's * content type * @throw std::out_of_range if the index is out of range */ @@ -119,14 +117,14 @@ public: /** * @brief Appends the tag to the end of the list - * @throw std::bad_cast if the type of the tag does not match the list's + * @throw std::invalid_argument if the type of the tag does not match the list's * content type */ void push_back(value_initializer&& val); /** * @brief Constructs and appends a tag to the end of the list - * @throw std::bad_cast if the type of the tag does not match the list's + * @throw std::invalid_argument if the type of the tag does not match the list's * content type */ template<class T, class... Args> @@ -206,7 +204,7 @@ void tag_list::emplace_back(Args&&... args) if(el_type_ == tag_type::Null) //set content type if undetermined el_type_ = T::type; else if(el_type_ != T::type) - throw std::bad_cast(); + throw std::invalid_argument("The tag type does not match the list's content type"); tags.emplace_back(make_unique<T>(std::forward<Args>(args)...)); } diff --git a/src/tag_list.cpp b/src/tag_list.cpp index a1a708a6a6..039bd9b77a 100644 --- a/src/tag_list.cpp +++ b/src/tag_list.cpp @@ -48,7 +48,7 @@ tag_list::tag_list(std::initializer_list<value> init) for(const value& val: init) { if(!val || val.get_type() != el_type_) - throw std::bad_cast(); + throw std::invalid_argument("The values are not all the same type"); } tags.assign(init.begin(), init.end()); } @@ -67,18 +67,18 @@ const value& tag_list::at(size_t i) const void tag_list::set(size_t i, value&& val) { if(val.get_type() != el_type_) - throw std::bad_cast(); + throw std::invalid_argument("The tag type does not match the list's content type"); tags.at(i) = std::move(val); } void tag_list::push_back(value_initializer&& val) { if(!val) //don't allow null values - throw std::bad_cast(); + throw std::invalid_argument("The value must not be null"); if(el_type_ == tag_type::Null) //set content type if undetermined el_type_ = val.get_type(); else if(el_type_ != val.get_type()) - throw std::bad_cast(); + throw std::invalid_argument("The tag type does not match the list's content type"); tags.push_back(std::move(val)); } @@ -131,7 +131,7 @@ void tag_list::write_payload(io::stream_writer& writer) const if(val.get_type() != el_type_) { writer.get_ostr().setstate(std::ios::failbit); - throw std::bad_cast(); + throw std::logic_error("The tags in the list do not all match the content type"); } writer.write_payload(val); } diff --git a/test/nbttest.h b/test/nbttest.h index 1eb53acc4f..53327af983 100644 --- a/test/nbttest.h +++ b/test/nbttest.h @@ -306,13 +306,13 @@ public: { tag_list list; TS_ASSERT_EQUALS(list.el_type(), tag_type::Null); - TS_ASSERT_THROWS(list.push_back(value(nullptr)), std::bad_cast); + TS_ASSERT_THROWS(list.push_back(value(nullptr)), std::invalid_argument); list.emplace_back<tag_string>("foo"); TS_ASSERT_EQUALS(list.el_type(), tag_type::String); list.push_back(tag_string("bar")); - TS_ASSERT_THROWS(list.push_back(tag_int(42)), std::bad_cast); - TS_ASSERT_THROWS(list.emplace_back<tag_compound>(), std::bad_cast); + TS_ASSERT_THROWS(list.push_back(tag_int(42)), std::invalid_argument); + TS_ASSERT_THROWS(list.emplace_back<tag_compound>(), std::invalid_argument); TS_ASSERT((list == tag_list{"foo", "bar"})); TS_ASSERT(list[0] == tag_string("foo")); @@ -323,8 +323,8 @@ public: TS_ASSERT_THROWS(list.at(-1), std::out_of_range); list.set(1, value(tag_string("baz"))); - TS_ASSERT_THROWS(list.set(1, value(nullptr)), std::bad_cast); - TS_ASSERT_THROWS(list.set(1, value(tag_int(-42))), std::bad_cast); + TS_ASSERT_THROWS(list.set(1, value(nullptr)), std::invalid_argument); + TS_ASSERT_THROWS(list.set(1, value(tag_int(-42))), std::invalid_argument); TS_ASSERT_EQUALS(static_cast<std::string>(list[1]), "baz"); TS_ASSERT_EQUALS(list.size(), 2u); @@ -341,8 +341,8 @@ public: list.clear(); TS_ASSERT_EQUALS(list.size(), 0u); TS_ASSERT_EQUALS(list.el_type(), tag_type::String) - TS_ASSERT_THROWS(list.push_back(tag_short(25)), std::bad_cast); - TS_ASSERT_THROWS(list.push_back(value(nullptr)), std::bad_cast); + TS_ASSERT_THROWS(list.push_back(tag_short(25)), std::invalid_argument); + TS_ASSERT_THROWS(list.push_back(value(nullptr)), std::invalid_argument); list.reset(); TS_ASSERT_EQUALS(list.el_type(), tag_type::Null); @@ -365,9 +365,9 @@ public: TS_ASSERT((short_list != tag_list{25, 36})); TS_ASSERT((short_list == tag_list{value(tag_short(25)), value(tag_short(36))})); - TS_ASSERT_THROWS((tag_list{value(tag_byte(4)), value(tag_int(5))}), std::bad_cast); - TS_ASSERT_THROWS((tag_list{value(nullptr), value(tag_int(6))}), std::bad_cast); - TS_ASSERT_THROWS((tag_list{value(tag_int(7)), value(tag_int(8)), value(nullptr)}), std::bad_cast); + TS_ASSERT_THROWS((tag_list{value(tag_byte(4)), value(tag_int(5))}), std::invalid_argument); + TS_ASSERT_THROWS((tag_list{value(nullptr), value(tag_int(6))}), std::invalid_argument); + TS_ASSERT_THROWS((tag_list{value(tag_int(7)), value(tag_int(8)), value(nullptr)}), std::invalid_argument); TS_ASSERT_EQUALS((tag_list(std::initializer_list<value>{})).el_type(), tag_type::Null); TS_ASSERT_EQUALS((tag_list{2, 3, 5, 7}).el_type(), tag_type::Int); } |
