diff options
| author | ljfa-ag <ljfa-ag@web.de> | 2015-08-11 11:16:10 +0200 |
|---|---|---|
| committer | ljfa-ag <ljfa-ag@web.de> | 2015-08-11 11:19:53 +0200 |
| commit | b5f3570924b49eb8564b68b02e5dbd18f0ad9a06 (patch) | |
| tree | 681efd2bb049d88d780352372d89c4c59caaeffa | |
| parent | 7122475522a290e39fb32999efc5ed20aef0a315 (diff) | |
| download | Project-Tick-b5f3570924b49eb8564b68b02e5dbd18f0ad9a06.tar.gz Project-Tick-b5f3570924b49eb8564b68b02e5dbd18f0ad9a06.zip | |
Inline crtp_tag and tag_primitive methods
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | include/crtp_tag.h | 56 | ||||
| -rw-r--r-- | include/tag_primitive.h | 33 | ||||
| -rw-r--r-- | src/tag.cpp | 4 | ||||
| -rw-r--r-- | src/tag_primitive.cpp | 111 |
5 files changed, 36 insertions, 169 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 3baaf4fd66..8524bcdef4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,6 @@ add_library(nbt++ STATIC src/tag_array.cpp src/tag_compound.cpp src/tag_list.cpp - src/tag_primitive.cpp src/tag_string.cpp src/value.cpp src/value_initializer.cpp diff --git a/include/crtp_tag.h b/include/crtp_tag.h index 3f3a528b21..7b802979a9 100644 --- a/include/crtp_tag.h +++ b/include/crtp_tag.h @@ -37,18 +37,18 @@ namespace detail //Pure virtual destructor to make the class abstract virtual ~crtp_tag() noexcept = 0; - tag_type get_type() const noexcept override final; + tag_type get_type() const noexcept override final { return Sub::type; }; - std::unique_ptr<tag> clone() const& override final; - std::unique_ptr<tag> move_clone() && override final; + std::unique_ptr<tag> clone() const& override final { return make_unique<Sub>(sub_this()); } + std::unique_ptr<tag> move_clone() && override final { return make_unique<Sub>(std::move(sub_this())); } - tag& assign(tag&& rhs) override final; + tag& assign(tag&& rhs) override final { return sub_this() = dynamic_cast<Sub&&>(rhs); } - void accept(nbt_visitor& visitor) override final; - void accept(const_nbt_visitor& visitor) const override final; + void accept(nbt_visitor& visitor) override final { visitor.visit(sub_this()); } + void accept(const_nbt_visitor& visitor) const override final { visitor.visit(sub_this()); } private: - bool equals(const tag& rhs) const override final; + bool equals(const tag& rhs) const override final { return sub_this() == static_cast<const Sub&>(rhs); } Sub& sub_this() { return static_cast<Sub&>(*this); } const Sub& sub_this() const { return static_cast<const Sub&>(*this); } @@ -57,48 +57,6 @@ namespace detail template<class Sub> crtp_tag<Sub>::~crtp_tag() noexcept {} - template<class Sub> - tag_type crtp_tag<Sub>::get_type() const noexcept - { - return Sub::type; - } - - template<class Sub> - std::unique_ptr<tag> crtp_tag<Sub>::clone() const& - { - return make_unique<Sub>(sub_this()); - } - - template<class Sub> - std::unique_ptr<tag> crtp_tag<Sub>::move_clone() && - { - return make_unique<Sub>(std::move(sub_this())); - } - - template<class Sub> - tag& crtp_tag<Sub>::assign(tag&& rhs) - { - return sub_this() = dynamic_cast<Sub&&>(rhs); - } - - template<class Sub> - void crtp_tag<Sub>::accept(nbt_visitor& visitor) - { - visitor.visit(sub_this()); - } - - template<class Sub> - void crtp_tag<Sub>::accept(const_nbt_visitor& visitor) const - { - visitor.visit(sub_this()); - } - - template<class Sub> - bool crtp_tag<Sub>::equals(const tag& rhs) const - { - return sub_this() == static_cast<const Sub&>(rhs); - } - } } diff --git a/include/tag_primitive.h b/include/tag_primitive.h index b1bb9920c0..740f98a539 100644 --- a/include/tag_primitive.h +++ b/include/tag_primitive.h @@ -22,6 +22,9 @@ #include "crtp_tag.h" #include "primitive_detail.h" +#include "io/stream_reader.h" +#include <istream> +#include <sstream> namespace nbt { @@ -42,16 +45,16 @@ public: static constexpr tag_type type = detail::get_primitive_type<T>::value; //Constructor - tag_primitive(T value = 0) noexcept; + constexpr tag_primitive(T val = 0) noexcept: value(val) {} //Getters - operator T&(); - operator T() const; - T get() const; + operator T&() { return value; } + constexpr operator T() const { return value; } + constexpr T get() const { return value; } //Setters - tag_primitive& operator=(T value); - void set(T value); + tag_primitive& operator=(T val) { value = val; return *this; } + void set(T val) { value = val; } void read_payload(io::stream_reader& reader) override; @@ -59,8 +62,10 @@ private: T value; }; -template<class T> bool operator==(const tag_primitive<T>& lhs, const tag_primitive<T>& rhs); -template<class T> bool operator!=(const tag_primitive<T>& lhs, const tag_primitive<T>& rhs); +template<class T> bool operator==(const tag_primitive<T>& lhs, const tag_primitive<T>& rhs) +{ return lhs.get() == rhs.get(); } +template<class T> bool operator!=(const tag_primitive<T>& lhs, const tag_primitive<T>& rhs) +{ return !(lhs == rhs); } //Typedefs that should be used instead of the template tag_primitive. typedef tag_primitive<int8_t> tag_byte; @@ -70,6 +75,18 @@ typedef tag_primitive<int64_t> tag_long; typedef tag_primitive<float> tag_float; typedef tag_primitive<double> tag_double; +template<class T> +void tag_primitive<T>::read_payload(io::stream_reader& reader) +{ + reader.read_num(value); + if(!reader.get_istr()) + { + std::ostringstream str; + str << "Error reading tag_" << type; + throw io::input_error(str.str()); + } +} + } #endif // TAG_PRIMITIVE_H_INCLUDED diff --git a/src/tag.cpp b/src/tag.cpp index 5b6f1142e2..b0825b52e6 100644 --- a/src/tag.cpp +++ b/src/tag.cpp @@ -20,6 +20,7 @@ #include "tag.h" #include "nbt_tags.h" #include "text/json_formatter.h" +#include <limits> #include <ostream> #include <stdexcept> #include <typeinfo> @@ -27,6 +28,9 @@ namespace nbt { +static_assert(std::numeric_limits<float>::is_iec559 && std::numeric_limits<double>::is_iec559, + "The floating point values for NBT must conform to IEC 559/IEEE 754"); + bool is_valid_type(int type, bool allow_end) { return (allow_end ? 0 : 1) <= type && type <= 11; diff --git a/src/tag_primitive.cpp b/src/tag_primitive.cpp deleted file mode 100644 index dd07fea662..0000000000 --- a/src/tag_primitive.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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/>. - */ -#include "tag_primitive.h" -#include "io/stream_reader.h" -#include <limits> -#include <sstream> - -namespace nbt -{ - -static_assert(std::numeric_limits<float>::is_iec559 && std::numeric_limits<double>::is_iec559, - "The floating point values for NBT must conform to IEC 559/IEEE 754"); - -template<class T> -tag_primitive<T>::tag_primitive(T val) noexcept: - value(val) -{} - -template<class T> -tag_primitive<T>& tag_primitive<T>::operator=(T val) -{ - value = val; - return *this; -} - -template<class T> -void tag_primitive<T>::set(T val) -{ - value = val; -} - -template<class T> -tag_primitive<T>::operator T&() -{ - return value; -} - -template<class T> -tag_primitive<T>::operator T() const -{ - return value; -} - -template<class T> -T tag_primitive<T>::get() const -{ - return value; -} - -template<class T> -void tag_primitive<T>::read_payload(io::stream_reader& reader) -{ - reader.read_num(value); - if(!reader.get_istr()) - { - std::ostringstream str; - str << "Error reading tag_" << type; - throw io::input_error(str.str()); - } -} - -template<class T> -bool operator==(const tag_primitive<T>& lhs, const tag_primitive<T>& rhs) -{ - return lhs.get() == rhs.get(); -} - -template<class T> -bool operator!=(const tag_primitive<T>& lhs, const tag_primitive<T>& rhs) -{ - return !(lhs == rhs); -} - -//Enforce template instantiations -template class tag_primitive<int8_t>; -template class tag_primitive<int16_t>; -template class tag_primitive<int32_t>; -template class tag_primitive<int64_t>; -template class tag_primitive<float>; -template class tag_primitive<double>; -template bool operator==<int8_t> (const tag_primitive<int8_t>& , const tag_primitive<int8_t>&); -template bool operator==<int16_t>(const tag_primitive<int16_t>&, const tag_primitive<int16_t>&); -template bool operator==<int32_t>(const tag_primitive<int32_t>&, const tag_primitive<int32_t>&); -template bool operator==<int64_t>(const tag_primitive<int64_t>&, const tag_primitive<int64_t>&); -template bool operator==<float> (const tag_primitive<float>& , const tag_primitive<float>&); -template bool operator==<double> (const tag_primitive<double>& , const tag_primitive<double>&); -template bool operator!=<int8_t> (const tag_primitive<int8_t>& , const tag_primitive<int8_t>&); -template bool operator!=<int16_t>(const tag_primitive<int16_t>&, const tag_primitive<int16_t>&); -template bool operator!=<int32_t>(const tag_primitive<int32_t>&, const tag_primitive<int32_t>&); -template bool operator!=<int64_t>(const tag_primitive<int64_t>&, const tag_primitive<int64_t>&); -template bool operator!=<float> (const tag_primitive<float>& , const tag_primitive<float>&); -template bool operator!=<double> (const tag_primitive<double>& , const tag_primitive<double>&); - -} |
