From ea404fc070d15cf53fb6e0099f8e68771da529fd Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Wed, 15 Jul 2015 19:35:51 +0200 Subject: Implement tag_array --- include/primitive_detail.h | 4 +- include/tag_array.h | 95 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 7 deletions(-) diff --git a/include/primitive_detail.h b/include/primitive_detail.h index aaf33ff563..d124e738da 100644 --- a/include/primitive_detail.h +++ b/include/primitive_detail.h @@ -34,9 +34,7 @@ namespace detail { ///Meta-struct that holds the tag_type value for a specific primitive type template struct get_primitive_type - { - static_assert(sizeof(T) != sizeof(T), "Can only use primitive types used by NBT as parameter for tag_primitive"); - }; + { static_assert(sizeof(T) != sizeof(T), "Can only use primitive types used by NBT as parameter for tag_primitive"); }; template<> struct get_primitive_type : public std::integral_constant {}; template<> struct get_primitive_type : public std::integral_constant {}; diff --git a/include/tag_array.h b/include/tag_array.h index 4283ea4fac..159e432626 100644 --- a/include/tag_array.h +++ b/include/tag_array.h @@ -21,11 +21,23 @@ #define TAG_ARRAY_H_INCLUDED #include "crtp_tag.h" +#include #include namespace nbt { +namespace detail +{ + + template struct get_array_type + { static_assert(sizeof(T) != sizeof(T), "Can only use byte or int as parameter for tag_array"); }; + + template<> struct get_array_type : public std::integral_constant {}; + template<> struct get_array_type : public std::integral_constant {}; + +} + /** * @brief Tag that contains an array of byte or int values * @@ -43,10 +55,10 @@ public: typedef T value_type; ///The type of the tag - static constexpr tag_type type = tag_type::Byte_Array; + static constexpr tag_type type = detail::get_array_type::value; ///Constructs an empty array - tag_array(); + tag_array() {} ///Constructs an array with the given values tag_array(std::initializer_list init); @@ -86,14 +98,89 @@ public: const_iterator cbegin() const; const_iterator cend() const; -private: - std::vector values; + ///The vector that holds the values + std::vector data; }; +template bool operator==(const tag_array& lhs, const tag_array& rhs); +template bool operator!=(const tag_array& lhs, const tag_array& rhs); + //Typedefs that should be used instead of the template tag_array. typedef tag_array tag_byte_array; typedef tag_array tag_int_array; +template +tag_array::tag_array(std::initializer_list init): + data(init) +{} + +template +T& tag_array::at(size_t i) +{ + return data.at(i); +} + +template +T tag_array::at(size_t i) const +{ + return data.at(i); +} + +template +T& tag_array::operator[](size_t i) +{ + return data[i]; +} + +template +T tag_array::operator[](size_t i) const +{ + return data[i]; +} + +template +void tag_array::push_back(T val) +{ + data.push_back(val); +} + +template +void tag_array::pop_back() +{ + data.pop_back(); +} + +template +size_t tag_array::size() const +{ + return data.size(); +} + +template +void tag_array::clear() +{ + data.clear(); +} + +template auto tag_array::begin() -> iterator { return data.begin(); } +template auto tag_array::end() -> iterator { return data.end(); } +template auto tag_array::begin() const -> const_iterator { return data.begin(); } +template auto tag_array::end() const -> const_iterator { return data.end(); } +template auto tag_array::cbegin() const -> const_iterator { return data.cbegin(); } +template auto tag_array::cend() const -> const_iterator { return data.cend(); } + +template +bool operator==(const tag_array& lhs, const tag_array& rhs) +{ + return lhs.data == rhs.data; +} + +template +bool operator!=(const tag_array& lhs, const tag_array& rhs) +{ + return !(lhs == rhs); +} + } #endif // TAG_ARRAY_H_INCLUDED -- cgit 0.0.5-2-1-g0f52