tag_compound.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_COMPOUND_H_INCLUDED
  17. #define TAG_COMPOUND_H_INCLUDED
  18. #include "tag_base.h"
  19. #include <map>
  20. #include <memory>
  21. #include <boost/iterator/transform_iterator.hpp>
  22. namespace nbt
  23. {
  24. ///Contains multiple named tags of arbitrary types.
  25. class tag_compound : public tag
  26. {
  27. private:
  28. typedef std::map<std::string, std::unique_ptr<tag>> _map_t;
  29. //Functors for the boost transform_iterator.
  30. struct _it_trans
  31. {
  32. std::pair<const std::string&, tag&> operator()(const _map_t::value_type& it) const noexcept
  33. { return {it.first, *it.second}; }
  34. };
  35. struct _const_it_trans
  36. {
  37. std::pair<const std::string&, const tag&> operator()(const _map_t::value_type& it) const noexcept
  38. { return {it.first, *it.second}; }
  39. };
  40. public:
  41. ///Exception thrown when the access by key fails.
  42. struct key_not_found : public std::out_of_range
  43. {
  44. explicit key_not_found(const std::string& key):
  45. std::out_of_range("Key \"" + key + "\" not found in compound.") {}
  46. };
  47. /* Iterator typedefs
  48. Boost will never let me down <3 */
  49. typedef boost::transform_iterator<_it_trans, _map_t::iterator, std::pair<const std::string&, tag&>> iterator;
  50. typedef boost::transform_iterator<_const_it_trans, _map_t::const_iterator, std::pair<const std::string&, const tag&>> const_iterator;
  51. ///The type of the tag.
  52. static constexpr tag_type type = tag_type::_compound;
  53. ///Constructs an empty compound.
  54. tag_compound() {}
  55. //tag_compound is not copyable.
  56. tag_compound(const tag_compound&) = delete;
  57. tag_compound& operator=(const tag_compound&) = delete;
  58. //The move constructor and assignment operator must explicitly be defaulted,
  59. //otherwise the compiler attempts to use the deleted copy constructor and assignment.
  60. tag_compound(tag_compound&&) = default;
  61. tag_compound& operator=(tag_compound&&) = default;
  62. /**
  63. * \brief Accesses a tag by key.
  64. * \throw key_not_found if the key does not exist.
  65. */
  66. tag& at(const std::string& key);
  67. const tag& at(const std::string& key) const;
  68. /**
  69. * \brief Accesses a nested compound tag by a sequence of keys.
  70. *
  71. * Example:
  72. * \code x.at({"foo", "bar"}) \endcode
  73. * is equivalent to
  74. * \code x.at("foo").as<tag_compound>().at("bar") \endcode
  75. * \throw key_not_found if a key in the sequence does not exist.
  76. * \throw bad_as if a key in the sequence does not refer to a tag_compound.
  77. */
  78. tag& at(std::initializer_list<std::string> keys);
  79. const tag& at(std::initializer_list<std::string> keys) const;
  80. ///\sa at
  81. tag& operator[](const std::string& key) { return at(key); }
  82. const tag& operator[](const std::string& key) const { return at(key); }
  83. tag& operator[](std::initializer_list<std::string> keys) { return at(keys); }
  84. const tag& operator[](std::initializer_list<std::string> keys) const { return at(keys); }
  85. /**
  86. * \brief Inserts a tag into the container.
  87. *
  88. * The tag pointed to by \c tp is moved into the container.
  89. * If the given key already exists, no insertion is performed.
  90. * \return true if the tag was inserted.
  91. */
  92. bool insert(std::string key, std::unique_ptr<tag>&& tp);
  93. /**
  94. * \brief Allocates and constructs a new tag and inserts it into the container.
  95. *
  96. * Allocates space and constructs a new tag of type \c T using the given constructor arguments.
  97. * and inserts it into the container.
  98. * If the given key already exists, no construction and no insertion is performed.
  99. * \return true if the tag was inserted.
  100. */
  101. template<class T, class... Args> bool emplace(std::string key, Args&&... args);
  102. /**
  103. * \brief Erases an element from the container.
  104. * \return true if an element was erased.
  105. */
  106. bool erase(const std::string& key) { return m_data.erase(key); }
  107. ///Returns the number of elements in the container.
  108. uint32_t size() const noexcept { return m_data.size(); }
  109. ///Erases all elements from the container.
  110. void clear() { m_data.clear(); }
  111. iterator begin() noexcept;
  112. iterator end() noexcept;
  113. const_iterator begin() const noexcept;
  114. const_iterator end() const noexcept;
  115. const_iterator cbegin() const noexcept { return begin(); }
  116. const_iterator cend() const noexcept { return end(); }
  117. //Implementation of the tag interface.
  118. tag_type get_type() const noexcept { return type; }
  119. void print(std::ostream& os) const;
  120. ///Returns a constant reference to the internally used \c std::map object.
  121. const _map_t& data() const noexcept { return m_data; }
  122. protected:
  123. void write_payload(std::ostream& os) const;
  124. private:
  125. _map_t m_data;
  126. };
  127. inline auto tag_compound::begin() noexcept -> iterator { return {m_data.begin(), _it_trans()}; }
  128. inline auto tag_compound::end() noexcept -> iterator { return {m_data.end(), _it_trans()}; }
  129. inline auto tag_compound::begin() const noexcept -> const_iterator { return {m_data.cbegin(), _const_it_trans()}; }
  130. inline auto tag_compound::end() const noexcept -> const_iterator { return {m_data.cend(), _const_it_trans()}; }
  131. inline bool tag_compound::insert(std::string key, std::unique_ptr<tag>&& tp)
  132. { return m_data.insert(std::make_pair(std::move(key), std::move(tp))).second; }
  133. template<class T, class... Args> inline bool tag_compound::emplace(std::string key, Args&&... args)
  134. {
  135. if(m_data.find(key) != m_data.end())
  136. return false;
  137. insert(std::move(key), std::unique_ptr<T>(new T(std::forward<Args>(args)...)));
  138. return true;
  139. }
  140. }
  141. #endif // TAG_COMPOUND_H_INCLUDED