tag_string.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_STRING_H_INCLUDED
  21. #define TAG_STRING_H_INCLUDED
  22. #include "crtp_tag.h"
  23. #include <string>
  24. namespace nbt
  25. {
  26. ///Tag that contains a UTF-8 string
  27. class NBT_EXPORT tag_string final : public detail::crtp_tag<tag_string>
  28. {
  29. public:
  30. ///The type of the tag
  31. static constexpr tag_type type = tag_type::String;
  32. //Constructors
  33. tag_string() {}
  34. tag_string(const std::string& str): value(str) {}
  35. tag_string(std::string&& str) noexcept: value(std::move(str)) {}
  36. tag_string(const char* str): value(str) {}
  37. //Getters
  38. operator std::string&() { return value; }
  39. operator const std::string&() const { return value; }
  40. const std::string& get() const { return value; }
  41. //Setters
  42. tag_string& operator=(const std::string& str) { value = str; return *this; }
  43. tag_string& operator=(std::string&& str) { value = std::move(str); return *this; }
  44. tag_string& operator=(const char* str) { value = str; return *this; }
  45. void set(const std::string& str) { value = str; }
  46. void set(std::string&& str) { value = std::move(str); }
  47. void read_payload(io::stream_reader& reader) override;
  48. /**
  49. * @inheritdoc
  50. * @throw std::length_error if the string is too long for NBT
  51. */
  52. void write_payload(io::stream_writer& writer) const override;
  53. private:
  54. std::string value;
  55. };
  56. inline bool operator==(const tag_string& lhs, const tag_string& rhs)
  57. { return lhs.get() == rhs.get(); }
  58. inline bool operator!=(const tag_string& lhs, const tag_string& rhs)
  59. { return !(lhs == rhs); }
  60. }
  61. #endif // TAG_STRING_H_INCLUDED