diff options
| author | ljfa-ag <ljfa-ag@web.de> | 2015-07-01 21:26:28 +0200 |
|---|---|---|
| committer | ljfa-ag <ljfa-ag@web.de> | 2015-07-01 21:26:28 +0200 |
| commit | ffaf121d24428a9d822c1e3de512b9593be7aa0e (patch) | |
| tree | af8f619f0ec448fcec88a91d36d90698742314e1 /include | |
| parent | e0ff3b56c05225dd7da7d973af4c708966379a12 (diff) | |
| download | Project-Tick-ffaf121d24428a9d822c1e3de512b9593be7aa0e.tar.gz Project-Tick-ffaf121d24428a9d822c1e3de512b9593be7aa0e.zip | |
Implement CRTP for tag
Diffstat (limited to 'include')
| -rw-r--r-- | include/crtp_tag.h | 66 | ||||
| -rw-r--r-- | include/tag_compound.h | 9 | ||||
| -rw-r--r-- | include/tag_primitive.h | 27 | ||||
| -rw-r--r-- | include/tag_string.h | 9 |
4 files changed, 72 insertions, 39 deletions
diff --git a/include/crtp_tag.h b/include/crtp_tag.h new file mode 100644 index 0000000000..f35b9d0c9a --- /dev/null +++ b/include/crtp_tag.h @@ -0,0 +1,66 @@ +/* + * libnbt++ - A library for the Minecraft Named Binary Tag format. + * Copyright (C) 2013, 2015 ljfa-ag + * + * This file is part of libnbt++. + * + * libnbt++ is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libnbt++ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libnbt++. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef CRTP_TAG_H_INCLUDED +#define CRTP_TAG_H_INCLUDED + +#include "tag.h" + +namespace nbt +{ + +namespace detail +{ + + template<class Sub> + class crtp_tag : public tag + { + public: + std::unique_ptr<tag> move_clone() && override final; + + private: + bool equals(const tag& rhs) const override final; + tag& assign(tag&& rhs) override final; + }; + + template<class Sub> + std::unique_ptr<tag> crtp_tag<Sub>::move_clone() && + { + return std::unique_ptr<tag>( + new Sub(static_cast<Sub&&>(*this)) + ); + } + + template<class Sub> + bool crtp_tag<Sub>::equals(const tag& rhs) const + { + return *this == static_cast<const Sub&>(rhs); + } + + template<class Sub> + tag& crtp_tag<Sub>::assign(tag&& rhs) + { + return *this = dynamic_cast<Sub&&>(rhs); + } + +} + +} + +#endif // CRTP_TAG_H_INCLUDED diff --git a/include/tag_compound.h b/include/tag_compound.h index 1b6e214b10..265479c8af 100644 --- a/include/tag_compound.h +++ b/include/tag_compound.h @@ -20,7 +20,7 @@ #ifndef TAG_COMPOUND_H_INCLUDED #define TAG_COMPOUND_H_INCLUDED -#include "tag.h" +#include "crtp_tag.h" #include "value.h" #include <map> #include <string> @@ -29,7 +29,7 @@ namespace nbt { ///Tag that contains multiple unordered named tags of arbitrary types -class tag_compound final : public tag +class tag_compound final : public detail::crtp_tag<tag_compound> { public: //Iterator types @@ -108,17 +108,12 @@ public: const_iterator cend() const; tag_type get_type() const noexcept override; - std::unique_ptr<tag> move_clone() && override; friend bool operator==(const tag_compound& lhs, const tag_compound& rhs); friend bool operator!=(const tag_compound& lhs, const tag_compound& rhs); private: std::map<std::string, value> tags; - - bool equals(const tag& rhs) const override; - - tag_compound& assign(tag&& rhs) override; }; template<class T, class... Args> diff --git a/include/tag_primitive.h b/include/tag_primitive.h index c58cb891ff..6087495048 100644 --- a/include/tag_primitive.h +++ b/include/tag_primitive.h @@ -20,7 +20,7 @@ #ifndef TAG_PRIMITIVE_H_INCLUDED #define TAG_PRIMITIVE_H_INCLUDED -#include "tag.h" +#include "crtp_tag.h" #include "primitive_detail.h" #include <type_traits> @@ -33,7 +33,7 @@ namespace nbt * Common class for tag_byte, tag_short, tag_int, tag_long, tag_float and tag_double. */ template<class T> -class tag_primitive final : public tag +class tag_primitive final : public detail::crtp_tag<tag_primitive<T>> { public: ///The type of the value @@ -52,14 +52,9 @@ public: void set(T value); tag_type get_type() const noexcept override; - std::unique_ptr<tag> move_clone() && override; private: T value; - - bool equals(const tag& rhs) const override; - - tag_primitive<T>& assign(tag&& rhs) override; }; template<class T> bool operator==(const tag_primitive<T>& lhs, const tag_primitive<T>& rhs); @@ -116,24 +111,6 @@ tag_type tag_primitive<T>::get_type() const noexcept } template<class T> -std::unique_ptr<tag> tag_primitive<T>::move_clone() && -{ - return std::unique_ptr<tag>(new tag_primitive<T>(std::move(*this))); -} - -template<class T> -bool tag_primitive<T>::equals(const tag& rhs) const -{ - return *this == static_cast<const tag_primitive<T>&>(rhs); -} - -template<class T> -tag_primitive<T>& tag_primitive<T>::assign(tag&& rhs) -{ - return *this = dynamic_cast<tag_primitive<T>&&>(rhs); -} - -template<class T> bool operator==(const tag_primitive<T>& lhs, const tag_primitive<T>& rhs) { return lhs.get() == rhs.get(); diff --git a/include/tag_string.h b/include/tag_string.h index b28476da2c..3c514bae96 100644 --- a/include/tag_string.h +++ b/include/tag_string.h @@ -20,14 +20,14 @@ #ifndef TAG_STRING_H_INCLUDED #define TAG_STRING_H_INCLUDED -#include "tag.h" +#include "crtp_tag.h" #include <string> namespace nbt { ///Tag that contains a UTF-8 string -class tag_string final : public tag +class tag_string final : public detail::crtp_tag<tag_string> { public: ///The type of the tag @@ -46,14 +46,9 @@ public: void set(std::string&& str); tag_type get_type() const noexcept override; - std::unique_ptr<tag> move_clone() && override; private: std::string value; - - bool equals(const tag& rhs) const override; - - tag_string& assign(tag&& rhs) override; }; bool operator==(const tag_string& lhs, const tag_string& rhs); |
