value.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_REF_PROXY_H_INCLUDED
  21. #define TAG_REF_PROXY_H_INCLUDED
  22. #include "tag.h"
  23. #include <string>
  24. #include <type_traits>
  25. namespace nbt
  26. {
  27. /**
  28. * @brief Contains an NBT value of fixed type
  29. *
  30. * This class is a convenience wrapper for @c std::unique_ptr<tag>.
  31. * A value can contain any kind of tag or no tag (nullptr) and provides
  32. * operations for handling tags of which the type is not known at compile time.
  33. * Assignment or the set method on a value with no tag will fill in the value.
  34. *
  35. * The rationale for the existance of this class is to provide a type-erasured
  36. * means of storing tags, especially when they are contained in tag_compound
  37. * or tag_list. The alternative would be directly using @c std::unique_ptr<tag>
  38. * and @c tag&, which is how it was done in libnbt++1. The main drawback is that
  39. * it becomes very cumbersome to deal with tags of unknown type.
  40. *
  41. * For example, in this case it would not be possible to allow a syntax like
  42. * <tt>compound["foo"] = 42</tt>. If the key "foo" does not exist beforehand,
  43. * the left hand side could not have any sensible value if it was of type
  44. * @c tag&.
  45. * Firstly, the compound tag would have to create a new tag_int there, but it
  46. * cannot know that the new tag is going to be assigned an integer.
  47. * Also, if the type was @c tag& and it allowed assignment of integers, that
  48. * would mean the tag base class has assignments and conversions like this.
  49. * Which means that all other tag classes would inherit them from the base
  50. * class, even though it does not make any sense to allow converting a
  51. * tag_compound into an integer. Attempts like this should be caught at
  52. * compile time.
  53. *
  54. * This is why all the syntactic sugar for tags is contained in the value class
  55. * while the tag class only contains common operations for all tag types.
  56. */
  57. class NBT_EXPORT value
  58. {
  59. public:
  60. //Constructors
  61. value() noexcept {}
  62. explicit value(std::unique_ptr<tag>&& t) noexcept: tag_(std::move(t)) {}
  63. explicit value(tag&& t);
  64. //Moving
  65. value(value&&) noexcept = default;
  66. value& operator=(value&&) noexcept = default;
  67. //Copying
  68. explicit value(const value& rhs);
  69. value& operator=(const value& rhs);
  70. /**
  71. * @brief Assigns the given value to the tag if the type matches
  72. * @throw std::bad_cast if the type of @c t is not the same as the type
  73. * of this value
  74. */
  75. value& operator=(tag&& t);
  76. void set(tag&& t);
  77. //Conversion to tag
  78. /**
  79. * @brief Returns the contained tag
  80. *
  81. * If the value is uninitialized, the behavior is undefined.
  82. */
  83. operator tag&() { return get(); }
  84. operator const tag&() const { return get(); }
  85. tag& get() { return *tag_; }
  86. const tag& get() const { return *tag_; }
  87. /**
  88. * @brief Returns a reference to the contained tag as an instance of T
  89. * @throw std::bad_cast if the tag is not of type T
  90. */
  91. template<class T>
  92. T& as();
  93. template<class T>
  94. const T& as() const;
  95. //Assignment of primitives and string
  96. /**
  97. * @brief Assigns the given value to the tag if the type is compatible
  98. * @throw std::bad_cast if the value is not convertible to the tag type
  99. * via a widening conversion
  100. */
  101. value& operator=(int8_t val);
  102. value& operator=(int16_t val);
  103. value& operator=(int32_t val);
  104. value& operator=(int64_t val);
  105. value& operator=(float val);
  106. value& operator=(double val);
  107. /**
  108. * @brief Assigns the given string to the tag if it is a tag_string
  109. * @throw std::bad_cast if the contained tag is not a tag_string
  110. */
  111. value& operator=(const std::string& str);
  112. value& operator=(std::string&& str);
  113. //Conversions to primitives and string
  114. /**
  115. * @brief Returns the contained value if the type is compatible
  116. * @throw std::bad_cast if the tag type is not convertible to the desired
  117. * type via a widening conversion
  118. */
  119. explicit operator int8_t() const;
  120. explicit operator int16_t() const;
  121. explicit operator int32_t() const;
  122. explicit operator int64_t() const;
  123. explicit operator float() const;
  124. explicit operator double() const;
  125. /**
  126. * @brief Returns the contained string if the type is tag_string
  127. *
  128. * If the value is uninitialized, the behavior is undefined.
  129. * @throw std::bad_cast if the tag type is not tag_string
  130. */
  131. explicit operator const std::string&() const;
  132. ///Returns true if the value is not uninitialized
  133. explicit operator bool() const { return tag_ != nullptr; }
  134. /**
  135. * @brief In case of a tag_compound, accesses a tag by key with bounds checking
  136. *
  137. * If the value is uninitialized, the behavior is undefined.
  138. * @throw std::bad_cast if the tag type is not tag_compound
  139. * @throw std::out_of_range if given key does not exist
  140. * @sa tag_compound::at
  141. */
  142. value& at(const std::string& key);
  143. const value& at(const std::string& key) const;
  144. /**
  145. * @brief In case of a tag_compound, accesses a tag by key
  146. *
  147. * If the value is uninitialized, the behavior is undefined.
  148. * @throw std::bad_cast if the tag type is not tag_compound
  149. * @sa tag_compound::operator[]
  150. */
  151. value& operator[](const std::string& key);
  152. value& operator[](const char* key); //need this overload because of conflict with built-in operator[]
  153. /**
  154. * @brief In case of a tag_list, accesses a tag by index with bounds checking
  155. *
  156. * If the value is uninitialized, the behavior is undefined.
  157. * @throw std::bad_cast if the tag type is not tag_list
  158. * @throw std::out_of_range if the index is out of range
  159. * @sa tag_list::at
  160. */
  161. value& at(size_t i);
  162. const value& at(size_t i) const;
  163. /**
  164. * @brief In case of a tag_list, accesses a tag by index
  165. *
  166. * No bounds checking is performed. If the value is uninitialized, the
  167. * behavior is undefined.
  168. * @throw std::bad_cast if the tag type is not tag_list
  169. * @sa tag_list::operator[]
  170. */
  171. value& operator[](size_t i);
  172. const value& operator[](size_t i) const;
  173. ///Returns a reference to the underlying std::unique_ptr<tag>
  174. std::unique_ptr<tag>& get_ptr() { return tag_; }
  175. const std::unique_ptr<tag>& get_ptr() const { return tag_; }
  176. ///Resets the underlying std::unique_ptr<tag> to a different value
  177. void set_ptr(std::unique_ptr<tag>&& t) { tag_ = std::move(t); }
  178. ///@sa tag::get_type
  179. tag_type get_type() const;
  180. friend NBT_EXPORT bool operator==(const value& lhs, const value& rhs);
  181. friend NBT_EXPORT bool operator!=(const value& lhs, const value& rhs);
  182. private:
  183. std::unique_ptr<tag> tag_;
  184. };
  185. template<class T>
  186. T& value::as()
  187. {
  188. return tag_->as<T>();
  189. }
  190. template<class T>
  191. const T& value::as() const
  192. {
  193. return tag_->as<T>();
  194. }
  195. }
  196. #endif // TAG_REF_PROXY_H_INCLUDED