tag_compound.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_COMPOUND_H_INCLUDED
  21. #define TAG_COMPOUND_H_INCLUDED
  22. #include "crtp_tag.h"
  23. #include "value_initializer.h"
  24. #include <map>
  25. #include <string>
  26. namespace nbt
  27. {
  28. ///Tag that contains multiple unordered named tags of arbitrary types
  29. class NBT_EXPORT tag_compound final : public detail::crtp_tag<tag_compound>
  30. {
  31. typedef std::map<std::string, value> map_t_;
  32. public:
  33. //Iterator types
  34. typedef map_t_::iterator iterator;
  35. typedef map_t_::const_iterator const_iterator;
  36. ///The type of the tag
  37. static constexpr tag_type type = tag_type::Compound;
  38. ///Constructs an empty compound
  39. tag_compound() {}
  40. ///Constructs a compound with the given key-value pairs
  41. tag_compound(std::initializer_list<std::pair<std::string, value_initializer>> init);
  42. /**
  43. * @brief Accesses a tag by key with bounds checking
  44. *
  45. * Returns a value to the tag with the specified key, or throws an
  46. * exception if it does not exist.
  47. * @throw std::out_of_range if given key does not exist
  48. */
  49. value& at(const std::string& key);
  50. const value& at(const std::string& key) const;
  51. /**
  52. * @brief Accesses a tag by key
  53. *
  54. * Returns a value to the tag with the specified key. If it does not exist,
  55. * creates a new uninitialized entry under the key.
  56. */
  57. value& operator[](const std::string& key) { return tags[key]; }
  58. /**
  59. * @brief Inserts or assigns a tag
  60. *
  61. * If the given key already exists, assigns the tag to it.
  62. * Otherwise, it is inserted under the given key.
  63. * @return a pair of the iterator to the value and a bool indicating
  64. * whether the key did not exist
  65. */
  66. std::pair<iterator, bool> put(const std::string& key, value_initializer&& val);
  67. /**
  68. * @brief Inserts a tag if the key does not exist
  69. * @return a pair of the iterator to the value with the key and a bool
  70. * indicating whether the value was actually inserted
  71. */
  72. std::pair<iterator, bool> insert(const std::string& key, value_initializer&& val);
  73. /**
  74. * @brief Constructs and assigns or inserts a tag into the compound
  75. *
  76. * Constructs a new tag of type @c T with the given args and inserts
  77. * or assigns it to the given key.
  78. * @note Unlike std::map::emplace, this will overwrite existing values
  79. * @return a pair of the iterator to the value and a bool indicating
  80. * whether the key did not exist
  81. */
  82. template<class T, class... Args>
  83. std::pair<iterator, bool> emplace(const std::string& key, Args&&... args);
  84. /**
  85. * @brief Erases a tag from the compound
  86. * @return true if a tag was erased
  87. */
  88. bool erase(const std::string& key);
  89. ///Returns true if the given key exists in the compound
  90. bool has_key(const std::string& key) const;
  91. ///Returns true if the given key exists and the tag has the given type
  92. bool has_key(const std::string& key, tag_type type) const;
  93. ///Returns the number of tags in the compound
  94. size_t size() const { return tags.size(); }
  95. ///Erases all tags from the compound
  96. void clear() { tags.clear(); }
  97. //Iterators
  98. iterator begin() { return tags.begin(); }
  99. iterator end() { return tags.end(); }
  100. const_iterator begin() const { return tags.begin(); }
  101. const_iterator end() const { return tags.end(); }
  102. const_iterator cbegin() const { return tags.cbegin(); }
  103. const_iterator cend() const { return tags.cend(); }
  104. void read_payload(io::stream_reader& reader) override;
  105. void write_payload(io::stream_writer& writer) const override;
  106. friend bool operator==(const tag_compound& lhs, const tag_compound& rhs)
  107. { return lhs.tags == rhs.tags; }
  108. friend bool operator!=(const tag_compound& lhs, const tag_compound& rhs)
  109. { return !(lhs == rhs); }
  110. private:
  111. map_t_ tags;
  112. };
  113. template<class T, class... Args>
  114. std::pair<tag_compound::iterator, bool> tag_compound::emplace(const std::string& key, Args&&... args)
  115. {
  116. return put(key, value(make_unique<T>(std::forward<Args>(args)...)));
  117. }
  118. }
  119. #endif // TAG_COMPOUND_H_INCLUDED