tag_primitive.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_PRIMITIVE_H_INCLUDED
  21. #define TAG_PRIMITIVE_H_INCLUDED
  22. #include "crtp_tag.h"
  23. #include "primitive_detail.h"
  24. #include "io/stream_reader.h"
  25. #include "io/stream_writer.h"
  26. #include <istream>
  27. #include <sstream>
  28. namespace nbt
  29. {
  30. /**
  31. * @brief Tag that contains an integral or floating-point value
  32. *
  33. * Common class for tag_byte, tag_short, tag_int, tag_long, tag_float and tag_double.
  34. */
  35. template<class T>
  36. class tag_primitive final : public detail::crtp_tag<tag_primitive<T>>
  37. {
  38. public:
  39. ///The type of the value
  40. typedef T value_type;
  41. ///The type of the tag
  42. static constexpr tag_type type = detail::get_primitive_type<T>::value;
  43. //Constructor
  44. constexpr tag_primitive(T val = 0) noexcept: value(val) {}
  45. //Getters
  46. operator T&() { return value; }
  47. constexpr operator T() const { return value; }
  48. constexpr T get() const { return value; }
  49. //Setters
  50. tag_primitive& operator=(T val) { value = val; return *this; }
  51. void set(T val) { value = val; }
  52. void read_payload(io::stream_reader& reader) override;
  53. void write_payload(io::stream_writer& writer) const override;
  54. private:
  55. T value;
  56. };
  57. template<class T> bool operator==(const tag_primitive<T>& lhs, const tag_primitive<T>& rhs)
  58. { return lhs.get() == rhs.get(); }
  59. template<class T> bool operator!=(const tag_primitive<T>& lhs, const tag_primitive<T>& rhs)
  60. { return !(lhs == rhs); }
  61. //Typedefs that should be used instead of the template tag_primitive.
  62. typedef tag_primitive<int8_t> tag_byte;
  63. typedef tag_primitive<int16_t> tag_short;
  64. typedef tag_primitive<int32_t> tag_int;
  65. typedef tag_primitive<int64_t> tag_long;
  66. typedef tag_primitive<float> tag_float;
  67. typedef tag_primitive<double> tag_double;
  68. //Explicit instantiations
  69. template class NBT_EXPORT tag_primitive<int8_t>;
  70. template class NBT_EXPORT tag_primitive<int16_t>;
  71. template class NBT_EXPORT tag_primitive<int32_t>;
  72. template class NBT_EXPORT tag_primitive<int64_t>;
  73. template class NBT_EXPORT tag_primitive<float>;
  74. template class NBT_EXPORT tag_primitive<double>;
  75. template<class T>
  76. void tag_primitive<T>::read_payload(io::stream_reader& reader)
  77. {
  78. reader.read_num(value);
  79. if(!reader.get_istr())
  80. {
  81. std::ostringstream str;
  82. str << "Error reading tag_" << type;
  83. throw io::input_error(str.str());
  84. }
  85. }
  86. template<class T>
  87. void tag_primitive<T>::write_payload(io::stream_writer& writer) const
  88. {
  89. writer.write_num(value);
  90. }
  91. }
  92. #endif // TAG_PRIMITIVE_H_INCLUDED