diff options
| author | ljfa-ag <ljfa-ag@web.de> | 2015-08-12 18:06:58 +0200 |
|---|---|---|
| committer | ljfa-ag <ljfa-ag@web.de> | 2015-08-12 18:06:58 +0200 |
| commit | b67a3f9b0aa06984d68aee6754bd74bd7004fd97 (patch) | |
| tree | 43461ea6cd7bf984e93521145fbae1593c72d23b /include | |
| parent | 1178dbef6290d0a87d478ea06b11cf19e2e7b1d7 (diff) | |
| download | Project-Tick-b67a3f9b0aa06984d68aee6754bd74bd7004fd97.tar.gz Project-Tick-b67a3f9b0aa06984d68aee6754bd74bd7004fd97.zip | |
Add write_payload methods to stream_writer and tags
Diffstat (limited to 'include')
| -rw-r--r-- | include/io/stream_writer.h | 15 | ||||
| -rw-r--r-- | include/tag.h | 10 | ||||
| -rw-r--r-- | include/tag_array.h | 1 | ||||
| -rw-r--r-- | include/tag_compound.h | 1 | ||||
| -rw-r--r-- | include/tag_list.h | 5 | ||||
| -rw-r--r-- | include/tag_primitive.h | 8 | ||||
| -rw-r--r-- | 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 <iosfwd> -#include <stdexcept> +//#include <stdexcept> 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 @@ -60,6 +61,16 @@ public: 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 */ void write_type(tag_type tt); 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 @@ -103,6 +106,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<T> 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 <istream> #include <sstream> @@ -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<T>::read_payload(io::stream_reader& reader) } } +template<class T> +void tag_primitive<T>::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; |
