summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorljfa-ag <ljfa-ag@web.de>2015-08-05 17:06:40 +0200
committerljfa-ag <ljfa-ag@web.de>2015-08-05 17:06:40 +0200
commit7626425b2c6299b38865aa10c8d912e946f5f88f (patch)
tree9dfdc531294bc7b6933a459307941f779e662b21
parentb79ebd5096321cf8051f483bc20ac11fab9f9d6c (diff)
downloadProject-Tick-7626425b2c6299b38865aa10c8d912e946f5f88f.tar.gz
Project-Tick-7626425b2c6299b38865aa10c8d912e946f5f88f.zip
Add tag::as method
-rw-r--r--include/tag.h23
-rw-r--r--include/value.h8
-rw-r--r--test/io/read_test.cpp2
-rw-r--r--test/nbttest.cpp3
4 files changed, 30 insertions, 6 deletions
diff --git a/include/tag.h b/include/tag.h
index 58b99c082c..569da446d6 100644
--- a/include/tag.h
+++ b/include/tag.h
@@ -71,6 +71,15 @@ public:
std::unique_ptr<tag> clone() &&;
/**
+ * @brief Returns a reference to the tag as an instance of T
+ * @throw std::bad_cast if the tag is not of type T
+ */
+ template<class T>
+ T& as();
+ template<class T>
+ const T& as() const;
+
+ /**
* @brief Move-assigns the given tag if the class is the same
* @throw std::bad_cast if @c rhs is not the same type as @c *this
*/
@@ -101,6 +110,20 @@ private:
std::ostream& operator<<(std::ostream& os, tag_type tt);
+template<class T>
+T& tag::as()
+{
+ static_assert(std::is_base_of<tag, T>::value, "T must be a subclass of tag");
+ return dynamic_cast<T&>(*this);
+}
+
+template<class T>
+const T& tag::as() const
+{
+ static_assert(std::is_base_of<tag, T>::value, "T must be a subclass of tag");
+ return dynamic_cast<const T&>(*this);
+}
+
}
#endif // TAG_H_INCLUDED
diff --git a/include/value.h b/include/value.h
index c017bcbd27..4865c8fd1c 100644
--- a/include/value.h
+++ b/include/value.h
@@ -93,7 +93,7 @@ public:
const tag& get() const;
/**
- * @brief Returns the contained tag as an instance of T
+ * @brief Returns a reference to the contained tag as an instance of T
* @throw std::bad_cast if the tag is not of type T
*/
template<class T>
@@ -207,15 +207,13 @@ private:
template<class T>
T& value::as()
{
- static_assert(std::is_base_of<tag, T>::value, "T must be a subclass of tag");
- return dynamic_cast<T&>(get());
+ return tag_->as<T>();
}
template<class T>
const T& value::as() const
{
- static_assert(std::is_base_of<tag, T>::value, "T must be a subclass of tag");
- return dynamic_cast<T&>(get());
+ return tag_->as<T>();;
}
}
diff --git a/test/io/read_test.cpp b/test/io/read_test.cpp
index aa731e1e55..d3ea8b7765 100644
--- a/test/io/read_test.cpp
+++ b/test/io/read_test.cpp
@@ -109,7 +109,7 @@ void test_read_bigtest()
auto tagptr = reader.read_payload(tag_type::Compound);
ASSERT(tagptr->get_type() == tag_type::Compound);
- const tag_compound& comp = static_cast<const tag_compound&>(*tagptr);
+ const tag_compound& comp = tagptr->as<tag_compound>();
ASSERT(comp.size() == 13);
diff --git a/test/nbttest.cpp b/test/nbttest.cpp
index 15b0c67fba..b041d44aa8 100644
--- a/test/nbttest.cpp
+++ b/test/nbttest.cpp
@@ -54,6 +54,9 @@ void test_tag()
tstr.assign(tag_string("bar"));
EXPECT_EXCEPTION(tstr.assign(tag_int(6)), std::bad_cast);
ASSERT(tstr.get() == "bar");
+
+ ASSERT(&tstr.as<tag_string>() == &tstr);
+ EXPECT_EXCEPTION(tstr.as<tag_byte_array>(), std::bad_cast);
std::clog << "test_tag passed" << std::endl;
}