tag_array.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * libnbt++ - A library for the Minecraft Named Binary Tag format.
  3. * Copyright (C) 2013, 2015 ljfa-ag
  4. *
  5. * This file is part of libnbt++.
  6. *
  7. * libnbt++ is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * libnbt++ is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with libnbt++. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef TAG_ARRAY_H_INCLUDED
  21. #define TAG_ARRAY_H_INCLUDED
  22. #include "crtp_tag.h"
  23. #include "io/stream_reader.h"
  24. #include "io/stream_writer.h"
  25. #include <type_traits>
  26. #include <vector>
  27. #include <istream>
  28. namespace nbt
  29. {
  30. ///@cond
  31. namespace detail
  32. {
  33. ///Meta-struct that holds the tag_type value for a specific array type
  34. template<class T> struct get_array_type
  35. { static_assert(sizeof(T) != sizeof(T), "Invalid type paramter for tag_array, can only use byte or int"); };
  36. template<> struct get_array_type<int8_t> : public std::integral_constant<tag_type, tag_type::Byte_Array> {};
  37. template<> struct get_array_type<int32_t> : public std::integral_constant<tag_type, tag_type::Int_Array> {};
  38. template<> struct get_array_type<int64_t> : public std::integral_constant<tag_type, tag_type::Long_Array> {};
  39. }
  40. ///@cond
  41. /**
  42. * @brief Tag that contains an array of byte or int values
  43. *
  44. * Common class for tag_byte_array, tag_int_array and tag_long_array.
  45. */
  46. template<class T>
  47. class tag_array final : public detail::crtp_tag<tag_array<T>>
  48. {
  49. public:
  50. //Iterator types
  51. typedef typename std::vector<T>::iterator iterator;
  52. typedef typename std::vector<T>::const_iterator const_iterator;
  53. ///The type of the contained values
  54. typedef T value_type;
  55. ///The type of the tag
  56. static constexpr tag_type type = detail::get_array_type<T>::value;
  57. ///Constructs an empty array
  58. tag_array() {}
  59. ///Constructs an array with the given values
  60. tag_array(std::initializer_list<T> init): data(init) {}
  61. tag_array(std::vector<T>&& vec) noexcept: data(std::move(vec)) {}
  62. ///Returns a reference to the vector that contains the values
  63. std::vector<T>& get() { return data; }
  64. const std::vector<T>& get() const { return data; }
  65. /**
  66. * @brief Accesses a value by index with bounds checking
  67. * @throw std::out_of_range if the index is out of range
  68. */
  69. T& at(size_t i) { return data.at(i); }
  70. T at(size_t i) const { return data.at(i); }
  71. /**
  72. * @brief Accesses a value by index
  73. *
  74. * No bounds checking is performed.
  75. */
  76. T& operator[](size_t i) { return data[i]; }
  77. T operator[](size_t i) const { return data[i]; }
  78. ///Appends a value at the end of the array
  79. void push_back(T val) { data.push_back(val); }
  80. ///Removes the last element from the array
  81. void pop_back() { data.pop_back(); }
  82. ///Returns the number of values in the array
  83. size_t size() const { return data.size(); }
  84. ///Erases all values from the array.
  85. void clear() { data.clear(); }
  86. //Iterators
  87. iterator begin() { return data.begin(); }
  88. iterator end() { return data.end(); }
  89. const_iterator begin() const { return data.begin(); }
  90. const_iterator end() const { return data.end(); }
  91. const_iterator cbegin() const { return data.cbegin(); }
  92. const_iterator cend() const { return data.cend(); }
  93. void read_payload(io::stream_reader& reader) override;
  94. /**
  95. * @inheritdoc
  96. * @throw std::length_error if the array is too large for NBT
  97. */
  98. void write_payload(io::stream_writer& writer) const override;
  99. private:
  100. std::vector<T> data;
  101. };
  102. template<class T> bool operator==(const tag_array<T>& lhs, const tag_array<T>& rhs)
  103. { return lhs.get() == rhs.get(); }
  104. template<class T> bool operator!=(const tag_array<T>& lhs, const tag_array<T>& rhs)
  105. { return !(lhs == rhs); }
  106. //Slightly different between byte_array and int_array
  107. //Reading
  108. template<>
  109. inline void tag_array<int8_t>::read_payload(io::stream_reader& reader)
  110. {
  111. int32_t length;
  112. reader.read_num(length);
  113. if(length < 0)
  114. reader.get_istr().setstate(std::ios::failbit);
  115. if(!reader.get_istr())
  116. throw io::input_error("Error reading length of tag_byte_array");
  117. data.resize(length);
  118. reader.get_istr().read(reinterpret_cast<char*>(data.data()), length);
  119. if(!reader.get_istr())
  120. throw io::input_error("Error reading contents of tag_byte_array");
  121. }
  122. template<typename T>
  123. inline void tag_array<T>::read_payload(io::stream_reader& reader)
  124. {
  125. int32_t length;
  126. reader.read_num(length);
  127. if(length < 0)
  128. reader.get_istr().setstate(std::ios::failbit);
  129. if(!reader.get_istr())
  130. throw io::input_error("Error reading length of generic array tag");
  131. data.clear();
  132. data.reserve(length);
  133. for(T i = 0; i < length; ++i)
  134. {
  135. T val;
  136. reader.read_num(val);
  137. data.push_back(val);
  138. }
  139. if(!reader.get_istr())
  140. throw io::input_error("Error reading contents of generic array tag");
  141. }
  142. template<>
  143. inline void tag_array<int64_t>::read_payload(io::stream_reader& reader)
  144. {
  145. int32_t length;
  146. reader.read_num(length);
  147. if(length < 0)
  148. reader.get_istr().setstate(std::ios::failbit);
  149. if(!reader.get_istr())
  150. throw io::input_error("Error reading length of tag_long_array");
  151. data.clear();
  152. data.reserve(length);
  153. for(int32_t i = 0; i < length; ++i)
  154. {
  155. int64_t val;
  156. reader.read_num(val);
  157. data.push_back(val);
  158. }
  159. if(!reader.get_istr())
  160. throw io::input_error("Error reading contents of tag_long_array");
  161. }
  162. //Writing
  163. template<>
  164. inline void tag_array<int8_t>::write_payload(io::stream_writer& writer) const
  165. {
  166. if(size() > io::stream_writer::max_array_len)
  167. {
  168. writer.get_ostr().setstate(std::ios::failbit);
  169. throw std::length_error("Byte array is too large for NBT");
  170. }
  171. writer.write_num(static_cast<int32_t>(size()));
  172. writer.get_ostr().write(reinterpret_cast<const char*>(data.data()), data.size());
  173. }
  174. template<typename T>
  175. inline void tag_array<T>::write_payload(io::stream_writer& writer) const
  176. {
  177. if(size() > io::stream_writer::max_array_len)
  178. {
  179. writer.get_ostr().setstate(std::ios::failbit);
  180. throw std::length_error("Generic array is too large for NBT");
  181. }
  182. writer.write_num(static_cast<int32_t>(size()));
  183. for(T i: data)
  184. writer.write_num(i);
  185. }
  186. template<>
  187. inline void tag_array<int64_t>::write_payload(io::stream_writer& writer) const
  188. {
  189. if(size() > io::stream_writer::max_array_len)
  190. {
  191. writer.get_ostr().setstate(std::ios::failbit);
  192. throw std::length_error("Long array is too large for NBT");
  193. }
  194. writer.write_num(static_cast<int32_t>(size()));
  195. for(int64_t i: data)
  196. writer.write_num(i);
  197. }
  198. //Typedefs that should be used instead of the template tag_array.
  199. typedef tag_array<int8_t> tag_byte_array;
  200. typedef tag_array<int32_t> tag_int_array;
  201. typedef tag_array<int64_t> tag_long_array;
  202. }
  203. #endif // TAG_ARRAY_H_INCLUDED