tag_base.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. libnbt++ - A library for the Minecraft Named Binary Tag format.
  3. Copyright (C) 2013 ljfa-ag
  4. This file is part of libnbt++.
  5. libnbt++ is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. libnbt++ is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with libnbt++. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef TAG_BASE_H_INCLUDED
  17. #define TAG_BASE_H_INCLUDED
  18. #include <cstdint>
  19. #include <iostream>
  20. #include <stdexcept>
  21. #include <utility>
  22. namespace nbt
  23. {
  24. //Forward declarations:
  25. class tag;
  26. namespace io { void write(std::ostream&, const std::string&, const tag&); }
  27. ///Base class for all tag classes.
  28. class tag
  29. {
  30. public:
  31. ///Tag type values used in the binary NBT format.
  32. enum class tag_type : int8_t
  33. {
  34. _end = 0,
  35. _byte = 1,
  36. _short = 2,
  37. _int = 3,
  38. _long = 4,
  39. _float = 5,
  40. _double = 6,
  41. _byte_array = 7,
  42. _string = 8,
  43. _list = 9,
  44. _compound = 10,
  45. _int_array = 11
  46. };
  47. ///Exception thrown when the cast with #as fails.
  48. struct bad_as : public std::runtime_error
  49. { explicit bad_as(tag_type from, tag_type to); };
  50. /**
  51. * \brief Exception thrown when reading a tag from stream fails.
  52. * \sa read
  53. */
  54. struct input_error : public std::runtime_error
  55. { explicit input_error(std::string str): std::runtime_error(std::move(str)) {} };
  56. //Virtual destructor
  57. virtual ~tag() noexcept {}
  58. /**
  59. * \brief Attempts to cast the tag reference to the given type.
  60. * \throw bad_as if the tag is not of type T.
  61. * \sa static_as
  62. */
  63. template<class T> T& as();
  64. template<class T> const T& as() const;
  65. /**
  66. * \brief `static_cast`s the tag reference to the given type.
  67. * \sa as
  68. */
  69. template<class T> T& static_as();
  70. template<class T> const T& static_as() const;
  71. ///Returns the type of the tag.
  72. virtual tag_type get_type() const noexcept = 0;
  73. ///Prints a human-readable representation of the tag into the stream.
  74. virtual void print(std::ostream& os) const = 0;
  75. protected:
  76. ///Writes the binary content of the tag into the stream.
  77. virtual void write_payload(std::ostream& os) const = 0;
  78. //The container classes need access to the protected read and write functions.
  79. friend class tag_list;
  80. friend class tag_compound;
  81. //The write function needs access to wite_payload.
  82. friend void io::write(std::ostream&, const std::string&, const tag&);
  83. };
  84. template<class T> inline T& tag::as()
  85. {
  86. if(get_type() != T::type)
  87. throw bad_as(get_type(), T::type);
  88. return static_as<T>();
  89. }
  90. template<class T> inline const T& tag::as() const
  91. {
  92. if(get_type() != T::type)
  93. throw bad_as(get_type(), T::type);
  94. return static_as<T>();
  95. }
  96. template<class T> inline T& tag::static_as()
  97. { return static_cast<T&>(*this); }
  98. template<class T> inline const T& tag::static_as() const
  99. { return static_cast<const T&>(*this); }
  100. ///\relates tag
  101. inline std::ostream& operator<<(std::ostream& os, const tag& t)
  102. { t.print(os); return os; }
  103. ///\relates tag
  104. std::ostream& operator<<(std::ostream& os, tag::tag_type tt);
  105. }
  106. #endif // TAG_BASE_H_INCLUDED