summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorljfa-ag <ljfa-ag@web.de>2015-08-11 11:16:10 +0200
committerljfa-ag <ljfa-ag@web.de>2015-08-11 11:19:53 +0200
commitb5f3570924b49eb8564b68b02e5dbd18f0ad9a06 (patch)
tree681efd2bb049d88d780352372d89c4c59caaeffa /include
parent7122475522a290e39fb32999efc5ed20aef0a315 (diff)
downloadProject-Tick-b5f3570924b49eb8564b68b02e5dbd18f0ad9a06.tar.gz
Project-Tick-b5f3570924b49eb8564b68b02e5dbd18f0ad9a06.zip
Inline crtp_tag and tag_primitive methods
Diffstat (limited to 'include')
-rw-r--r--include/crtp_tag.h56
-rw-r--r--include/tag_primitive.h33
2 files changed, 32 insertions, 57 deletions
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