1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
/*
* Copyright 2017 The Native Object Protocols Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LIBNOP_INCLUDE_NOP_BASE_SERIALIZER_H_
#define LIBNOP_INCLUDE_NOP_BASE_SERIALIZER_H_
#include <memory>
#include <nop/base/encoding.h>
#include <nop/status.h>
namespace nop {
//
// Serializer and Deserializer template types provide the basic interface for
// writing and reading C++ types to a writer or reader class. There are several
// types of specializations of Serializer and Deserializer: those that contain
// an internal instance of the writer or reader and those that wrap a pointer or
// unique pointer to an external writer or reader.
//
// Example of instantiating a Serializer with an internal Writer:
//
// Serializer<StreamWriter<std::stringstream>> serializer;
// auto status = serializer.Write(data_type);
//
// Example of instantiating a Serializer with an external Writer:
//
// using Writer = StreamWriter<std::stringstream>;
// Writer stream_writer;
// Serializer<Writer> serializer{&stream_writer};
// auto status = serializer.Write(data_type);
//
// Which specialization to use depends on the situation and whether the writer
// or reader will be used in different contexts or only for serialization /
// deserialization tasks.
//
// Implementation of Write method common to all Serializer specializations.
struct SerializerCommon {
template <typename T, typename Writer>
static constexpr Status<void> Write(const T& value, Writer* writer) {
// Determine how much space to prepare the writer for.
const std::size_t size_bytes = Encoding<T>::Size(value);
// Prepare the writer for the serialized data.
auto status = writer->Prepare(size_bytes);
if (!status)
return status;
// Serialize the data to the writer.
return Encoding<T>::Write(value, writer);
}
};
// Serializer with internal instance of Writer.
template <typename Writer>
class Serializer {
public:
template <typename... Args>
constexpr Serializer(Args&&... args) : writer_{std::forward<Args>(args)...} {}
constexpr Serializer(Serializer&&) = default;
constexpr Serializer& operator=(Serializer&&) = default;
// Returns the encoded size of |value| in bytes. This may be an over estimate
// but must never be an under esitmate.
template <typename T>
constexpr std::size_t GetSize(const T& value) {
return Encoding<T>::Size(value);
}
// Serializes |value| to the Writer.
template <typename T>
constexpr Status<void> Write(const T& value) {
return SerializerCommon::Write(value, &writer_);
}
constexpr const Writer& writer() const { return writer_; }
constexpr Writer& writer() { return writer_; }
constexpr Writer&& take() { return std::move(writer_); }
private:
Writer writer_;
Serializer(const Serializer&) = delete;
Serializer& operator=(const Serializer&) = delete;
};
// Serializer that wraps a pointer to Writer.
template <typename Writer>
class Serializer<Writer*> {
public:
constexpr Serializer() : writer_{nullptr} {}
constexpr Serializer(Writer* writer) : writer_{writer} {}
constexpr Serializer(const Serializer&) = default;
constexpr Serializer& operator=(const Serializer&) = default;
// Returns the encoded size of |value| in bytes. This may be an over estimate
// but must never be an under esitmate.
template <typename T>
constexpr std::size_t GetSize(const T& value) {
return Encoding<T>::Size(value);
}
// Serializes |value| to the Writer.
template <typename T>
constexpr Status<void> Write(const T& value) {
return SerializerCommon::Write(value, writer_);
}
constexpr const Writer& writer() const { return *writer_; }
constexpr Writer& writer() { return *writer_; }
private:
Writer* writer_;
};
// Serializer that wraps a unique pointer to Writer.
template <typename Writer>
class Serializer<std::unique_ptr<Writer>> {
public:
constexpr Serializer() = default;
constexpr Serializer(std::unique_ptr<Writer> writer)
: writer_{std::move(writer)} {}
constexpr Serializer(Serializer&&) = default;
constexpr Serializer& operator=(Serializer&&) = default;
// Returns the encoded size of |value| in bytes. This may be an over estimate
// but must never be an under esitmate.
template <typename T>
constexpr std::size_t GetSize(const T& value) {
return Encoding<T>::Size(value);
}
// Serializes |value| to the Writer.
template <typename T>
constexpr Status<void> Write(const T& value) {
return SerializerCommon::Write(value, writer_.get());
}
constexpr const Writer& writer() const { return *writer_; }
constexpr Writer& writer() { return *writer_; }
private:
std::unique_ptr<Writer> writer_;
Serializer(const Serializer&) = delete;
Serializer& operator=(const Serializer&) = delete;
};
// Deserializer that wraps an internal instance of Reader.
template <typename Reader>
class Deserializer {
public:
template <typename... Args>
constexpr Deserializer(Args&&... args)
: reader_{std::forward<Args>(args)...} {}
constexpr Deserializer(Deserializer&&) = default;
constexpr Deserializer& operator=(Deserializer&&) = default;
// Deserializes the data from the reader.
template <typename T>
constexpr Status<void> Read(T* value) {
return Encoding<T>::Read(value, &reader_);
}
constexpr const Reader& reader() const { return reader_; }
constexpr Reader& reader() { return reader_; }
constexpr Reader&& take() { return std::move(reader_); }
private:
Reader reader_;
Deserializer(const Deserializer&) = delete;
Deserializer& operator=(const Deserializer&) = delete;
};
// Deserializer that wraps a pointer to Reader.
template <typename Reader>
class Deserializer<Reader*> {
public:
constexpr Deserializer() : reader_{nullptr} {}
constexpr Deserializer(Reader* reader) : reader_{reader} {}
constexpr Deserializer(const Deserializer&) = default;
constexpr Deserializer& operator=(const Deserializer&) = default;
// Deserializes the data from the reader.
template <typename T>
constexpr Status<void> Read(T* value) {
return Encoding<T>::Read(value, reader_);
}
constexpr const Reader& reader() const { return *reader_; }
constexpr Reader& reader() { return *reader_; }
private:
Reader* reader_;
};
// Deserializer that wraps a unique pointer to reader.
template <typename Reader>
class Deserializer<std::unique_ptr<Reader>> {
public:
constexpr Deserializer() = default;
constexpr Deserializer(std::unique_ptr<Reader> reader)
: reader_{std::move(reader)} {}
constexpr Deserializer(Deserializer&&) = default;
constexpr Deserializer& operator=(Deserializer&&) = default;
// Deserializes the data from the reader.
template <typename T>
constexpr Status<void> Read(T* value) {
return Encoding<T>::Read(value, reader_.get());
}
constexpr const Reader& reader() const { return *reader_; }
constexpr Reader& reader() { return *reader_; }
private:
std::unique_ptr<Reader> reader_;
Deserializer(const Deserializer&) = delete;
Deserializer& operator=(const Deserializer&) = delete;
};
} // namespace nop
#endif // LIBNOP_INCLUDE_NOP_BASE_SERIALIZER_H_
|