From b5f3570924b49eb8564b68b02e5dbd18f0ad9a06 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Tue, 11 Aug 2015 11:16:10 +0200 Subject: Inline crtp_tag and tag_primitive methods --- CMakeLists.txt | 1 - include/crtp_tag.h | 56 +++--------------------- include/tag_primitive.h | 33 ++++++++++---- src/tag.cpp | 4 ++ src/tag_primitive.cpp | 111 ------------------------------------------------ 5 files changed, 36 insertions(+), 169 deletions(-) delete mode 100644 src/tag_primitive.cpp 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 clone() const& override final; - std::unique_ptr move_clone() && override final; + std::unique_ptr clone() const& override final { return make_unique(sub_this()); } + std::unique_ptr move_clone() && override final { return make_unique(std::move(sub_this())); } - tag& assign(tag&& rhs) override final; + tag& assign(tag&& rhs) override final { return sub_this() = dynamic_cast(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(rhs); } Sub& sub_this() { return static_cast(*this); } const Sub& sub_this() const { return static_cast(*this); } @@ -57,48 +57,6 @@ namespace detail template crtp_tag::~crtp_tag() noexcept {} - template - tag_type crtp_tag::get_type() const noexcept - { - return Sub::type; - } - - template - std::unique_ptr crtp_tag::clone() const& - { - return make_unique(sub_this()); - } - - template - std::unique_ptr crtp_tag::move_clone() && - { - return make_unique(std::move(sub_this())); - } - - template - tag& crtp_tag::assign(tag&& rhs) - { - return sub_this() = dynamic_cast(rhs); - } - - template - void crtp_tag::accept(nbt_visitor& visitor) - { - visitor.visit(sub_this()); - } - - template - void crtp_tag::accept(const_nbt_visitor& visitor) const - { - visitor.visit(sub_this()); - } - - template - bool crtp_tag::equals(const tag& rhs) const - { - return sub_this() == static_cast(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 +#include namespace nbt { @@ -42,16 +45,16 @@ public: static constexpr tag_type type = detail::get_primitive_type::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 bool operator==(const tag_primitive& lhs, const tag_primitive& rhs); -template bool operator!=(const tag_primitive& lhs, const tag_primitive& rhs); +template bool operator==(const tag_primitive& lhs, const tag_primitive& rhs) +{ return lhs.get() == rhs.get(); } +template bool operator!=(const tag_primitive& lhs, const tag_primitive& rhs) +{ return !(lhs == rhs); } //Typedefs that should be used instead of the template tag_primitive. typedef tag_primitive tag_byte; @@ -70,6 +75,18 @@ typedef tag_primitive tag_long; typedef tag_primitive tag_float; typedef tag_primitive tag_double; +template +void tag_primitive::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 #include #include #include @@ -27,6 +28,9 @@ namespace nbt { +static_assert(std::numeric_limits::is_iec559 && std::numeric_limits::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 . - */ -#include "tag_primitive.h" -#include "io/stream_reader.h" -#include -#include - -namespace nbt -{ - -static_assert(std::numeric_limits::is_iec559 && std::numeric_limits::is_iec559, - "The floating point values for NBT must conform to IEC 559/IEEE 754"); - -template -tag_primitive::tag_primitive(T val) noexcept: - value(val) -{} - -template -tag_primitive& tag_primitive::operator=(T val) -{ - value = val; - return *this; -} - -template -void tag_primitive::set(T val) -{ - value = val; -} - -template -tag_primitive::operator T&() -{ - return value; -} - -template -tag_primitive::operator T() const -{ - return value; -} - -template -T tag_primitive::get() const -{ - return value; -} - -template -void tag_primitive::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 -bool operator==(const tag_primitive& lhs, const tag_primitive& rhs) -{ - return lhs.get() == rhs.get(); -} - -template -bool operator!=(const tag_primitive& lhs, const tag_primitive& rhs) -{ - return !(lhs == rhs); -} - -//Enforce template instantiations -template class tag_primitive; -template class tag_primitive; -template class tag_primitive; -template class tag_primitive; -template class tag_primitive; -template class tag_primitive; -template bool operator== (const tag_primitive& , const tag_primitive&); -template bool operator==(const tag_primitive&, const tag_primitive&); -template bool operator==(const tag_primitive&, const tag_primitive&); -template bool operator==(const tag_primitive&, const tag_primitive&); -template bool operator== (const tag_primitive& , const tag_primitive&); -template bool operator== (const tag_primitive& , const tag_primitive&); -template bool operator!= (const tag_primitive& , const tag_primitive&); -template bool operator!=(const tag_primitive&, const tag_primitive&); -template bool operator!=(const tag_primitive&, const tag_primitive&); -template bool operator!=(const tag_primitive&, const tag_primitive&); -template bool operator!= (const tag_primitive& , const tag_primitive&); -template bool operator!= (const tag_primitive& , const tag_primitive&); - -} -- cgit 0.0.5-2-1-g0f52