diff options
Diffstat (limited to 'depends/libnbtplusplus/include')
18 files changed, 1855 insertions, 0 deletions
diff --git a/depends/libnbtplusplus/include/crtp_tag.h b/depends/libnbtplusplus/include/crtp_tag.h new file mode 100644 index 00000000..82b41907 --- /dev/null +++ b/depends/libnbtplusplus/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" +#include "nbt_visitor.h" +#include "make_unique.h" + +#include "nbt++_export.h" + +namespace nbt +{ + +namespace detail +{ + + template<class Sub> + class NBT___EXPORT crtp_tag : public tag + { + public: + //Pure virtual destructor to make the class abstract + virtual ~crtp_tag() noexcept = 0; + + tag_type get_type() const noexcept override final { return Sub::type; }; + + 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 { return sub_this() = dynamic_cast<Sub&&>(rhs); } + + 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 { 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); } + }; + + template<class Sub> + crtp_tag<Sub>::~crtp_tag() noexcept {} + +} + +} + +#endif // CRTP_TAG_H_INCLUDED diff --git a/depends/libnbtplusplus/include/endian_str.h b/depends/libnbtplusplus/include/endian_str.h new file mode 100644 index 00000000..ac8e8986 --- /dev/null +++ b/depends/libnbtplusplus/include/endian_str.h @@ -0,0 +1,113 @@ +/* + * 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 ENDIAN_STR_H_INCLUDED +#define ENDIAN_STR_H_INCLUDED + +#include <cstdint> +#include <iosfwd> + +#include "nbt++_export.h" + +/** + * @brief Reading and writing numbers from and to streams + * in binary format with different byte orders. + */ +namespace endian +{ + +enum endian { little, big }; + +///Reads number from stream in specified endian +template<class T> +NBT___EXPORT void read(std::istream& is, T& x, endian e); + +///Reads number from stream in little endian +NBT___EXPORT void read_little(std::istream& is, uint8_t& x); +NBT___EXPORT void read_little(std::istream& is, uint16_t& x); +NBT___EXPORT void read_little(std::istream& is, uint32_t& x); +NBT___EXPORT void read_little(std::istream& is, uint64_t& x); +NBT___EXPORT void read_little(std::istream& is, int8_t& x); +NBT___EXPORT void read_little(std::istream& is, int16_t& x); +NBT___EXPORT void read_little(std::istream& is, int32_t& x); +NBT___EXPORT void read_little(std::istream& is, int64_t& x); +NBT___EXPORT void read_little(std::istream& is, float& x); +NBT___EXPORT void read_little(std::istream& is, double& x); + +///Reads number from stream in big endian +NBT___EXPORT void read_big(std::istream& is, uint8_t& x); +NBT___EXPORT void read_big(std::istream& is, uint16_t& x); +NBT___EXPORT void read_big(std::istream& is, uint32_t& x); +NBT___EXPORT void read_big(std::istream& is, uint64_t& x); +NBT___EXPORT void read_big(std::istream& is, int8_t& x); +NBT___EXPORT void read_big(std::istream& is, int16_t& x); +NBT___EXPORT void read_big(std::istream& is, int32_t& x); +NBT___EXPORT void read_big(std::istream& is, int64_t& x); +NBT___EXPORT void read_big(std::istream& is, float& x); +NBT___EXPORT void read_big(std::istream& is, double& x); + +///Writes number to stream in specified endian +template<class T> +NBT___EXPORT void write(std::ostream& os, T x, endian e); + +///Writes number to stream in little endian +NBT___EXPORT void write_little(std::ostream& os, uint8_t x); +NBT___EXPORT void write_little(std::ostream& os, uint16_t x); +NBT___EXPORT void write_little(std::ostream& os, uint32_t x); +NBT___EXPORT void write_little(std::ostream& os, uint64_t x); +NBT___EXPORT void write_little(std::ostream& os, int8_t x); +NBT___EXPORT void write_little(std::ostream& os, int16_t x); +NBT___EXPORT void write_little(std::ostream& os, int32_t x); +NBT___EXPORT void write_little(std::ostream& os, int64_t x); +NBT___EXPORT void write_little(std::ostream& os, float x); +NBT___EXPORT void write_little(std::ostream& os, double x); + +///Writes number to stream in big endian +NBT___EXPORT void write_big(std::ostream& os, uint8_t x); +NBT___EXPORT void write_big(std::ostream& os, uint16_t x); +NBT___EXPORT void write_big(std::ostream& os, uint32_t x); +NBT___EXPORT void write_big(std::ostream& os, uint64_t x); +NBT___EXPORT void write_big(std::ostream& os, int8_t x); +NBT___EXPORT void write_big(std::ostream& os, int16_t x); +NBT___EXPORT void write_big(std::ostream& os, int32_t x); +NBT___EXPORT void write_big(std::ostream& os, int64_t x); +NBT___EXPORT void write_big(std::ostream& os, float x); +NBT___EXPORT void write_big(std::ostream& os, double x); + +template<class T> +NBT___EXPORT void read(std::istream& is, T& x, endian e) +{ + if(e == little) + read_little(is, x); + else + read_big(is, x); +} + +template<class T> +NBT___EXPORT void write(std::ostream& os, T x, endian e) +{ + if(e == little) + write_little(os, x); + else + write_big(os, x); +} + +} + +#endif // ENDIAN_STR_H_INCLUDED diff --git a/depends/libnbtplusplus/include/io/stream_reader.h b/depends/libnbtplusplus/include/io/stream_reader.h new file mode 100644 index 00000000..81255783 --- /dev/null +++ b/depends/libnbtplusplus/include/io/stream_reader.h @@ -0,0 +1,138 @@ +/* + * 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 STREAM_READER_H_INCLUDED +#define STREAM_READER_H_INCLUDED + +#include "endian_str.h" +#include "tag.h" +#include "tag_compound.h" +#include <iosfwd> +#include <memory> +#include <stdexcept> +#include <utility> + +#include "nbt++_export.h" + +namespace nbt +{ +namespace io +{ + +///Exception that gets thrown when reading is not successful +class NBT___EXPORT input_error : public std::runtime_error +{ + using std::runtime_error::runtime_error; +}; + +/** +* @brief Reads a named tag from the stream, making sure that it is a compound +* @param is the stream to read from +* @param e the byte order of the source data. The Java edition +* of Minecraft uses Big Endian, the Pocket edition uses Little Endian +* @throw input_error on failure, or if the tag in the stream is not a compound +*/ +NBT___EXPORT std::pair<std::string, std::unique_ptr<tag_compound>> read_compound(std::istream& is, endian::endian e = endian::big); + +/** +* @brief Reads a named tag from the stream +* @param is the stream to read from +* @param e the byte order of the source data. The Java edition +* of Minecraft uses Big Endian, the Pocket edition uses Little Endian +* @throw input_error on failure +*/ +NBT___EXPORT std::pair<std::string, std::unique_ptr<tag>> read_tag(std::istream& is, endian::endian e = endian::big); + +/** + * @brief Helper class for reading NBT tags from input streams + * + * Can be reused to read multiple tags + */ +class NBT___EXPORT stream_reader +{ +public: + /** + * @param is the stream to read from + * @param e the byte order of the source data. The Java edition + * of Minecraft uses Big Endian, the Pocket edition uses Little Endian + */ + explicit stream_reader(std::istream& is, endian::endian e = endian::big) noexcept; + + ///Returns the stream + std::istream& get_istr() const; + ///Returns the byte order + endian::endian get_endian() const; + + /** + * @brief Reads a named tag from the stream, making sure that it is a compound + * @throw input_error on failure, or if the tag in the stream is not a compound + */ + std::pair<std::string, std::unique_ptr<tag_compound>> read_compound(); + + /** + * @brief Reads a named tag from the stream + * @throw input_error on failure + */ + std::pair<std::string, std::unique_ptr<tag>> read_tag(); + + /** + * @brief Reads a tag of the given type without name from the stream + * @throw input_error on failure + */ + std::unique_ptr<tag> read_payload(tag_type type); + + /** + * @brief Reads a tag type from the stream + * @param allow_end whether to consider tag_type::End valid + * @throw input_error on failure + */ + tag_type read_type(bool allow_end = false); + + /** + * @brief Reads a binary number from the stream + * + * On failure, will set the failbit on the stream. + */ + template<class T> + void read_num(T& x); + + /** + * @brief Reads an NBT string from the stream + * + * An NBT string consists of two bytes indicating the length, followed by + * the characters encoded in modified UTF-8. + * @throw input_error on failure + */ + std::string read_string(); + +private: + std::istream& is; + const endian::endian endian; +}; + +template<class T> +void stream_reader::read_num(T& x) +{ + endian::read(is, x, endian); +} + +} +} + +#endif // STREAM_READER_H_INCLUDED diff --git a/depends/libnbtplusplus/include/io/stream_writer.h b/depends/libnbtplusplus/include/io/stream_writer.h new file mode 100644 index 00000000..8938b73b --- /dev/null +++ b/depends/libnbtplusplus/include/io/stream_writer.h @@ -0,0 +1,122 @@ +/* + * 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 STREAM_WRITER_H_INCLUDED +#define STREAM_WRITER_H_INCLUDED + +#include "tag.h" +#include "endian_str.h" +#include <iosfwd> + +#include "nbt++_export.h" + +namespace nbt +{ +namespace io +{ + +/* Not sure if that is even needed +///Exception that gets thrown when writing is not successful +class output_error : public std::runtime_error +{ + using std::runtime_error::runtime_error; +};*/ + +/** +* @brief Writes a named tag into the stream, including the tag type +* @param key the name of the tag +* @param t the tag +* @param os the stream to write to +* @param e the byte order of the written data. The Java edition +* of Minecraft uses Big Endian, the Pocket edition uses Little Endian +*/ +NBT___EXPORT void write_tag(const std::string& key, const tag& t, std::ostream& os, endian::endian e = endian::big); + +/** + * @brief Helper class for writing NBT tags to output streams + * + * Can be reused to write multiple tags + */ +class NBT___EXPORT stream_writer +{ +public: + ///Maximum length of an NBT string (16 bit unsigned) + static constexpr size_t max_string_len = UINT16_MAX; + ///Maximum length of an NBT list or array (32 bit signed) + static constexpr uint32_t max_array_len = INT32_MAX; + + /** + * @param os the stream to write to + * @param e the byte order of the written data. The Java edition + * of Minecraft uses Big Endian, the Pocket edition uses Little Endian + */ + explicit stream_writer(std::ostream& os, endian::endian e = endian::big) noexcept: + os(os), endian(e) + {} + + ///Returns the stream + std::ostream& get_ostr() const { return os; } + ///Returns the byte order + endian::endian get_endian() const { return endian; } + + /** + * @brief Writes a named tag into the stream, including the tag type + */ + void write_tag(const std::string& key, const tag& t); + + /** + * @brief Writes the given tag's payload into the stream + */ + void write_payload(const tag& t) { t.write_payload(*this); } + + /** + * @brief Writes a tag type to the stream + */ + void write_type(tag_type tt) { write_num(static_cast<int8_t>(tt)); } + + /** + * @brief Writes a binary number to the stream + */ + template<class T> + void write_num(T x); + + /** + * @brief Writes an NBT string to the stream + * + * An NBT string consists of two bytes indicating the length, followed by + * the characters encoded in modified UTF-8. + * @throw std::length_error if the string is too long for NBT + */ + void write_string(const std::string& str); + +private: + std::ostream& os; + const endian::endian endian; +}; + +template<class T> +void stream_writer::write_num(T x) +{ + endian::write(os, x, endian); +} + +} +} + +#endif // STREAM_WRITER_H_INCLUDED diff --git a/depends/libnbtplusplus/include/make_unique.h b/depends/libnbtplusplus/include/make_unique.h new file mode 100644 index 00000000..9a929543 --- /dev/null +++ b/depends/libnbtplusplus/include/make_unique.h @@ -0,0 +1,37 @@ +/* + * 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 MAKE_UNIQUE_H_INCLUDED +#define MAKE_UNIQUE_H_INCLUDED + +#include <memory> + +namespace nbt +{ + +///Creates a new object of type T and returns a std::unique_ptr to it +template<class T, class... Args> +std::unique_ptr<T> make_unique(Args&&... args) +{ + return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); +} + +} + +#endif // MAKE_UNIQUE_H_INCLUDED diff --git a/depends/libnbtplusplus/include/nbt_tags.h b/depends/libnbtplusplus/include/nbt_tags.h new file mode 100644 index 00000000..810bf0d6 --- /dev/null +++ b/depends/libnbtplusplus/include/nbt_tags.h @@ -0,0 +1,29 @@ +/* + * 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 NBT_TAGS_H_INCLUDED +#define NBT_TAGS_H_INCLUDED + +#include "tag_primitive.h" +#include "tag_string.h" +#include "tag_array.h" +#include "tag_list.h" +#include "tag_compound.h" + +#endif // NBT_TAGS_H_INCLUDED diff --git a/depends/libnbtplusplus/include/nbt_visitor.h b/depends/libnbtplusplus/include/nbt_visitor.h new file mode 100644 index 00000000..6cd51f65 --- /dev/null +++ b/depends/libnbtplusplus/include/nbt_visitor.h @@ -0,0 +1,82 @@ +/* + * 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 NBT_VISITOR_H_INCLUDED +#define NBT_VISITOR_H_INCLUDED + +#include "tagfwd.h" + +#include "nbt++_export.h" + +namespace nbt +{ + +/** + * @brief Base class for visitors of tags + * + * Implementing the Visitor pattern + */ +class NBT___EXPORT nbt_visitor +{ +public: + virtual ~nbt_visitor() noexcept = 0; //Abstract class + + virtual void visit(tag_byte&) {} + virtual void visit(tag_short&) {} + virtual void visit(tag_int&) {} + virtual void visit(tag_long&) {} + virtual void visit(tag_float&) {} + virtual void visit(tag_double&) {} + virtual void visit(tag_byte_array&) {} + virtual void visit(tag_string&) {} + virtual void visit(tag_list&) {} + virtual void visit(tag_compound&) {} + virtual void visit(tag_int_array&) {} +}; + +/** + * @brief Base class for visitors of constant tags + * + * Implementing the Visitor pattern + */ +class NBT___EXPORT const_nbt_visitor +{ +public: + virtual ~const_nbt_visitor() noexcept = 0; //Abstract class + + virtual void visit(const tag_byte&) {} + virtual void visit(const tag_short&) {} + virtual void visit(const tag_int&) {} + virtual void visit(const tag_long&) {} + virtual void visit(const tag_float&) {} + virtual void visit(const tag_double&) {} + virtual void visit(const tag_byte_array&) {} + virtual void visit(const tag_string&) {} + virtual void visit(const tag_list&) {} + virtual void visit(const tag_compound&) {} + virtual void visit(const tag_int_array&) {} +}; + +inline nbt_visitor::~nbt_visitor() noexcept {} + +inline const_nbt_visitor::~const_nbt_visitor() noexcept {} + +} + +#endif // NBT_VISITOR_H_INCLUDED diff --git a/depends/libnbtplusplus/include/primitive_detail.h b/depends/libnbtplusplus/include/primitive_detail.h new file mode 100644 index 00000000..4715ee7e --- /dev/null +++ b/depends/libnbtplusplus/include/primitive_detail.h @@ -0,0 +1,46 @@ +/* + * 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 PRIMITIVE_DETAIL_H_INCLUDED +#define PRIMITIVE_DETAIL_H_INCLUDED + +#include <type_traits> + +///@cond +namespace nbt +{ + +namespace detail +{ + ///Meta-struct that holds the tag_type value for a specific primitive type + template<class T> struct get_primitive_type + { static_assert(sizeof(T) != sizeof(T), "Invalid type paramter for tag_primitive, can only use types that NBT uses"); }; + + template<> struct get_primitive_type<int8_t> : public std::integral_constant<tag_type, tag_type::Byte> {}; + template<> struct get_primitive_type<int16_t> : public std::integral_constant<tag_type, tag_type::Short> {}; + template<> struct get_primitive_type<int32_t> : public std::integral_constant<tag_type, tag_type::Int> {}; + template<> struct get_primitive_type<int64_t> : public std::integral_constant<tag_type, tag_type::Long> {}; + template<> struct get_primitive_type<float> : public std::integral_constant<tag_type, tag_type::Float> {}; + template<> struct get_primitive_type<double> : public std::integral_constant<tag_type, tag_type::Double> {}; +} + +} +///@endcond + +#endif // PRIMITIVE_DETAIL_H_INCLUDED diff --git a/depends/libnbtplusplus/include/tag.h b/depends/libnbtplusplus/include/tag.h new file mode 100644 index 00000000..9a9f5858 --- /dev/null +++ b/depends/libnbtplusplus/include/tag.h @@ -0,0 +1,159 @@ +/* + * 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 TAG_H_INCLUDED +#define TAG_H_INCLUDED + +#include <cstdint> +#include <iosfwd> +#include <memory> + +#include "nbt++_export.h" + +namespace nbt +{ + +///Tag type values used in the binary format +enum class tag_type : int8_t +{ + End = 0, + Byte = 1, + Short = 2, + Int = 3, + Long = 4, + Float = 5, + Double = 6, + Byte_Array = 7, + String = 8, + List = 9, + Compound = 10, + Int_Array = 11, + Null = -1 ///< Used to denote empty @ref value s +}; + +/** + * @brief Returns whether the given number falls within the range of valid tag types + * @param allow_end whether to consider tag_type::End (0) valid + */ +NBT___EXPORT bool is_valid_type(int type, bool allow_end = false); + +//Forward declarations +class nbt_visitor; +class const_nbt_visitor; +namespace io +{ + class stream_reader; + class stream_writer; +} + +///Base class for all NBT tag classes +class NBT___EXPORT tag +{ +public: + //Virtual destructor + virtual ~tag() noexcept {} + + ///Returns the type of the tag + virtual tag_type get_type() const noexcept = 0; + + //Polymorphic clone methods + virtual std::unique_ptr<tag> clone() const& = 0; + virtual std::unique_ptr<tag> move_clone() && = 0; + std::unique_ptr<tag> clone() &&; + + /** + * @brief Returns a reference to the tag as an instance of T + * @throw std::bad_cast if the tag is not of type T + */ + template<class T> + T& as(); + template<class T> + const T& as() const; + + /** + * @brief Move-assigns the given tag if the class is the same + * @throw std::bad_cast if @c rhs is not the same type as @c *this + */ + virtual tag& assign(tag&& rhs) = 0; + + /** + * @brief Calls the appropriate overload of @c visit() on the visitor with + * @c *this as argument + * + * Implementing the Visitor pattern + */ + virtual void accept(nbt_visitor& visitor) = 0; + virtual void accept(const_nbt_visitor& visitor) const = 0; + + /** + * @brief Reads the tag's payload from the stream + * @throw io::stream_reader::input_error on failure + */ + virtual void read_payload(io::stream_reader& reader) = 0; + + /** + * @brief Writes the tag's payload into the stream + */ + virtual void write_payload(io::stream_writer& writer) const = 0; + + /** + * @brief Default-constructs a new tag of the given type + * @throw std::invalid_argument if the type is not valid (e.g. End or Null) + */ + static std::unique_ptr<tag> create(tag_type type); + + friend bool operator==(const tag& lhs, const tag& rhs); + friend bool operator!=(const tag& lhs, const tag& rhs); + +private: + /** + * @brief Checks for equality to a tag of the same type + * @param rhs an instance of the same class as @c *this + */ + virtual bool equals(const tag& rhs) const = 0; +}; + +///Output operator for tag types +NBT___EXPORT std::ostream& operator<<(std::ostream& os, tag_type tt); + +/** + * @brief Output operator for tags + * + * Uses @ref text::json_formatter + * @relates tag + */ +NBT___EXPORT std::ostream& operator<<(std::ostream& os, const tag& t); + +template<class T> +T& tag::as() +{ + static_assert(std::is_base_of<tag, T>::value, "T must be a subclass of tag"); + return dynamic_cast<T&>(*this); +} + +template<class T> +const T& tag::as() const +{ + static_assert(std::is_base_of<tag, T>::value, "T must be a subclass of tag"); + return dynamic_cast<const T&>(*this); +} + +} + +#endif // TAG_H_INCLUDED diff --git a/depends/libnbtplusplus/include/tag_array.h b/depends/libnbtplusplus/include/tag_array.h new file mode 100644 index 00000000..77b77d7d --- /dev/null +++ b/depends/libnbtplusplus/include/tag_array.h @@ -0,0 +1,131 @@ +/* + * 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 TAG_ARRAY_H_INCLUDED +#define TAG_ARRAY_H_INCLUDED + +#include "crtp_tag.h" +#include <type_traits> +#include <vector> + +#include "nbt++_export.h" + +namespace nbt +{ + +///@cond +namespace detail +{ + ///Meta-struct that holds the tag_type value for a specific array type + template<class T> struct get_array_type + { static_assert(sizeof(T) != sizeof(T), "Invalid type paramter for tag_primitive, can only use byte or int"); }; + + template<> struct get_array_type<int8_t> : public std::integral_constant<tag_type, tag_type::Byte_Array> {}; + template<> struct get_array_type<int32_t> : public std::integral_constant<tag_type, tag_type::Int_Array> {}; +} +///@cond + +/** + * @brief Tag that contains an array of byte or int values + * + * Common class for tag_byte_array and tag_int_array. + */ +template<class T> +class NBT___EXPORT tag_array final : public detail::crtp_tag<tag_array<T>> +{ +public: + //Iterator types + typedef typename std::vector<T>::iterator iterator; + typedef typename std::vector<T>::const_iterator const_iterator; + + ///The type of the contained values + typedef T value_type; + + ///The type of the tag + static constexpr tag_type type = detail::get_array_type<T>::value; + + ///Constructs an empty array + tag_array() {} + + ///Constructs an array with the given values + tag_array(std::initializer_list< |
