diff options
Diffstat (limited to 'src/serialization/binary_archive.h')
-rw-r--r-- | src/serialization/binary_archive.h | 66 |
1 files changed, 51 insertions, 15 deletions
diff --git a/src/serialization/binary_archive.h b/src/serialization/binary_archive.h index baa6707db..56c94684e 100644 --- a/src/serialization/binary_archive.h +++ b/src/serialization/binary_archive.h @@ -28,7 +28,7 @@ // // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers -/* binary_archive.h +/*! \file binary_archive.h * * Portable (low-endian) binary archive */ #pragma once @@ -41,11 +41,20 @@ #include "common/varint.h" #include "warnings.h" -PUSH_WARNINGS -DISABLE_VS_WARNINGS(4244) +/* I have no clue what these lines means */ +PUSH_WARNINGS; +DISABLE_VS_WARNINGS(4244); //TODO: fix size_t warning in x32 platform +/*! \struct binary_archive_base + * + * \brief base for the binary archive type + * + * \detailed It isn't used outside of this file, which its only + * purpse is to define the functions used for the binary_archive. Its + * a header, basically. I think it was declared simply to save typing... + */ template <class Stream, bool IsSaving> struct binary_archive_base { @@ -56,23 +65,39 @@ struct binary_archive_base typedef uint8_t variant_tag_type; explicit binary_archive_base(stream_type &s) : stream_(s) { } - + + /* definition of standard API functions */ void tag(const char *) { } void begin_object() { } void end_object() { } void begin_variant() { } void end_variant() { } - stream_type &stream() { return stream_; } + /* I just want to leave a comment saying how this line really shows + flaws in the ownership model of many OOP languages, that is all. */ + stream_type &stream() { return stream_; } + protected: stream_type &stream_; }; +/* \struct binary_archive + * + * \brief the actualy binary archive type + * + * \detailed The boolean template argument /a W is the is_saving + * parameter for binary_archive_base. + * + * The is_saving parameter says whether the archive is being read from + * (false) or written to (true) + */ template <bool W> struct binary_archive; + template <> struct binary_archive<false> : public binary_archive_base<std::istream, false> { + explicit binary_archive(stream_type &s) : base_type(s) { stream_type::streampos pos = stream_.tellg(); stream_.seekg(0, std::ios_base::end); @@ -86,6 +111,10 @@ struct binary_archive<false> : public binary_archive_base<std::istream, false> serialize_uint(*(typename boost::make_unsigned<T>::type *)&v); } + /*! \fn serialize_uint + * + * \brief serializes an unsigned integer + */ template <class T> void serialize_uint(T &v, size_t width = sizeof(T)) { @@ -96,13 +125,17 @@ struct binary_archive<false> : public binary_archive_base<std::istream, false> char c; stream_.get(c); T b = (unsigned char)c; - ret += (b << shift); + ret += (b << shift); // can this be changed to OR, i think it can. shift += 8; } v = ret; } - void serialize_blob(void *buf, size_t len, const char *delimiter="") { stream_.read((char *)buf, len); } - + + void serialize_blob(void *buf, size_t len, const char *delimiter="") + { + stream_.read((char *)buf, len); + } + template <class T> void serialize_varint(T &v) { @@ -115,17 +148,18 @@ struct binary_archive<false> : public binary_archive_base<std::istream, false> typedef std::istreambuf_iterator<char> it; tools::read_varint(it(stream_), it(), v); // XXX handle failure } + void begin_array(size_t &s) { serialize_varint(s); } - void begin_array() { } + void begin_array() { } void delimit_array() { } void end_array() { } - void begin_string(const char *delimiter="\"") { } - void end_string(const char *delimiter="\"") { } + void begin_string(const char *delimiter /*="\""*/) { } + void end_string(const char *delimiter /*="\""*/) { } void read_variant_tag(variant_tag_type &t) { serialize_int(t); @@ -157,12 +191,14 @@ struct binary_archive<true> : public binary_archive_base<std::ostream, true> { for (size_t i = 0; i < sizeof(T); i++) { stream_.put((char)(v & 0xff)); - if (1 < sizeof(T)) { - v >>= 8; - } + if (1 < sizeof(T)) v >>= 8; } } - void serialize_blob(void *buf, size_t len, const char *delimiter="") { stream_.write((char *)buf, len); } + + void serialize_blob(void *buf, size_t len, const char *delimiter="") + { + stream_.write((char *)buf, len); + } template <class T> void serialize_varint(T &v) |