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 --- include/crtp_tag.h | 56 +++++++------------------------------------------ include/tag_primitive.h | 33 ++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 57 deletions(-) (limited to 'include') 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 -- cgit 0.0.5-2-1-g0f52