crtp_tag.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 CRTP_TAG_H_INCLUDED
  21. #define CRTP_TAG_H_INCLUDED
  22. #include "tag.h"
  23. #include "nbt_visitor.h"
  24. #include "make_unique.h"
  25. namespace nbt
  26. {
  27. namespace detail
  28. {
  29. template<class Sub>
  30. class crtp_tag : public tag
  31. {
  32. public:
  33. //Pure virtual destructor to make the class abstract
  34. virtual ~crtp_tag() noexcept = 0;
  35. tag_type get_type() const noexcept override final { return Sub::type; };
  36. std::unique_ptr<tag> clone() const& override final { return make_unique<Sub>(sub_this()); }
  37. std::unique_ptr<tag> move_clone() && override final { return make_unique<Sub>(std::move(sub_this())); }
  38. tag& assign(tag&& rhs) override final { return sub_this() = dynamic_cast<Sub&&>(rhs); }
  39. void accept(nbt_visitor& visitor) override final { visitor.visit(sub_this()); }
  40. void accept(const_nbt_visitor& visitor) const override final { visitor.visit(sub_this()); }
  41. private:
  42. bool equals(const tag& rhs) const override final { return sub_this() == static_cast<const Sub&>(rhs); }
  43. Sub& sub_this() { return static_cast<Sub&>(*this); }
  44. const Sub& sub_this() const { return static_cast<const Sub&>(*this); }
  45. };
  46. template<class Sub>
  47. crtp_tag<Sub>::~crtp_tag() noexcept {}
  48. }
  49. }
  50. #endif // CRTP_TAG_H_INCLUDED