summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorljfa-ag <ljfa-ag@web.de>2015-08-04 17:58:14 +0200
committerljfa-ag <ljfa-ag@web.de>2015-08-04 17:58:14 +0200
commit61b2560a524ad7fe7d5e34cfccd70a7e92e7b8b9 (patch)
treedb428cdef2aff070e8ace938f5f5264b4fd1d211
parent99f15267e1f6db299d87bfb9744dec11c40963e4 (diff)
downloadProject-Tick-61b2560a524ad7fe7d5e34cfccd70a7e92e7b8b9.tar.gz
Project-Tick-61b2560a524ad7fe7d5e34cfccd70a7e92e7b8b9.zip
Create is_valid_type function
-rw-r--r--include/tag.h6
-rw-r--r--src/tag.cpp5
-rw-r--r--test/nbttest.cpp9
3 files changed, 20 insertions, 0 deletions
diff --git a/include/tag.h b/include/tag.h
index 75a41f2e1f..ae84891c76 100644
--- a/include/tag.h
+++ b/include/tag.h
@@ -45,6 +45,12 @@ enum class tag_type : int8_t
Null = -1 ///< Used to denote empty @ref value s
};
+/**
+ * @brief Returns whether the given number falls within the range of valid tag types
+ * @param allow_end whether to consider tag_type::End (0) valid
+ */
+bool is_valid_type(int type, bool allow_end = false);
+
///Base class for all NBT tag classes
class tag
{
diff --git a/src/tag.cpp b/src/tag.cpp
index b695bd98bb..db4c09aa2e 100644
--- a/src/tag.cpp
+++ b/src/tag.cpp
@@ -26,6 +26,11 @@
namespace nbt
{
+bool is_valid_type(int type, bool allow_end)
+{
+ return (allow_end ? 0 : 1) <= type && type <= 11;
+}
+
std::unique_ptr<tag> tag::clone() &&
{
return std::move(*this).move_clone();
diff --git a/test/nbttest.cpp b/test/nbttest.cpp
index 12f80ed62e..15b0c67fba 100644
--- a/test/nbttest.cpp
+++ b/test/nbttest.cpp
@@ -26,6 +26,15 @@ using namespace nbt;
void test_tag()
{
+ ASSERT(!is_valid_type(-1));
+ ASSERT(!is_valid_type(0));
+ ASSERT(is_valid_type(0, true));
+ ASSERT(is_valid_type(1));
+ ASSERT(is_valid_type(5, false));
+ ASSERT(is_valid_type(7, true));
+ ASSERT(is_valid_type(11));
+ ASSERT(!is_valid_type(12));
+
ASSERT(*tag::create(tag_type::Byte) == tag_byte());
EXPECT_EXCEPTION(tag::create(tag_type::Null), std::invalid_argument);
EXPECT_EXCEPTION(tag::create(tag_type::End), std::invalid_argument);