From b67a3f9b0aa06984d68aee6754bd74bd7004fd97 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Wed, 12 Aug 2015 18:06:58 +0200 Subject: Add write_payload methods to stream_writer and tags --- include/io/stream_writer.h | 15 +++++++++++++-- include/tag.h | 10 +++++++++- include/tag_array.h | 1 + include/tag_compound.h | 1 + include/tag_list.h | 5 +++++ include/tag_primitive.h | 8 ++++++++ include/tag_string.h | 5 +++++ 7 files changed, 42 insertions(+), 3 deletions(-) diff --git a/include/io/stream_writer.h b/include/io/stream_writer.h index 78d02cb2cc..8c354cceba 100644 --- a/include/io/stream_writer.h +++ b/include/io/stream_writer.h @@ -23,18 +23,19 @@ #include "tag.h" #include "endian_str.h" #include -#include +//#include 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 Helper class for writing NBT tags to output streams @@ -59,6 +60,16 @@ public: ///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); + /** * @brief Writes a tag type to the stream */ diff --git a/include/tag.h b/include/tag.h index 3bf23dae8f..a2ae49628e 100644 --- a/include/tag.h +++ b/include/tag.h @@ -55,7 +55,10 @@ bool is_valid_type(int type, bool allow_end = false); class nbt_visitor; class const_nbt_visitor; namespace io -{ class stream_reader; } +{ + class stream_reader; + class stream_writer; +} ///Base class for all NBT tag classes class tag @@ -102,6 +105,11 @@ public: */ 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) diff --git a/include/tag_array.h b/include/tag_array.h index 5ce7dd366e..f2163f6486 100644 --- a/include/tag_array.h +++ b/include/tag_array.h @@ -105,6 +105,7 @@ public: const_iterator cend() const; void read_payload(io::stream_reader& reader) override; + void write_payload(io::stream_writer& writer) const override; private: std::vector data; diff --git a/include/tag_compound.h b/include/tag_compound.h index 7ec53e4f80..bcb0a6cec2 100644 --- a/include/tag_compound.h +++ b/include/tag_compound.h @@ -120,6 +120,7 @@ public: const_iterator cend() const; void read_payload(io::stream_reader& reader) override; + void write_payload(io::stream_writer& writer) const override; friend bool operator==(const tag_compound& lhs, const tag_compound& rhs); friend bool operator!=(const tag_compound& lhs, const tag_compound& rhs); diff --git a/include/tag_list.h b/include/tag_list.h index dff89c4fcc..61ada4a5b9 100644 --- a/include/tag_list.h +++ b/include/tag_list.h @@ -163,6 +163,11 @@ public: * In case of a list of tag_end, the content type will be undetermined. */ void read_payload(io::stream_reader& reader) override; + /** + * @inheritdoc + * In case of a list of undetermined content type, the written type will be tag_end. + */ + void write_payload(io::stream_writer& writer) const override; /** * @brief Equality comparison for lists diff --git a/include/tag_primitive.h b/include/tag_primitive.h index 740f98a539..07ae985559 100644 --- a/include/tag_primitive.h +++ b/include/tag_primitive.h @@ -23,6 +23,7 @@ #include "crtp_tag.h" #include "primitive_detail.h" #include "io/stream_reader.h" +#include "io/stream_writer.h" #include #include @@ -57,6 +58,7 @@ public: void set(T val) { value = val; } void read_payload(io::stream_reader& reader) override; + void write_payload(io::stream_writer& writer) const override; private: T value; @@ -87,6 +89,12 @@ void tag_primitive::read_payload(io::stream_reader& reader) } } +template +void tag_primitive::write_payload(io::stream_writer& writer) const +{ + writer.write_num(value); +} + } #endif // TAG_PRIMITIVE_H_INCLUDED diff --git a/include/tag_string.h b/include/tag_string.h index 2a8a3d3f4e..ab084c44aa 100644 --- a/include/tag_string.h +++ b/include/tag_string.h @@ -52,6 +52,11 @@ public: void set(std::string&& str); void read_payload(io::stream_reader& reader) override; + /** + * @inheritdoc + * @throw std::length_error if the string is too long for NBT + */ + void write_payload(io::stream_writer& writer) const override; private: std::string value; -- cgit 0.0.5-2-1-g0f52