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
|
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
#pragma once
#include <cstddef>
#include <string>
#include <type_traits>
#include <vector>
#include "caf/byte.hpp"
#include "caf/byte_buffer.hpp"
#include "caf/detail/core_export.hpp"
#include "caf/detail/squashed_int.hpp"
#include "caf/fwd.hpp"
#include "caf/save_inspector_base.hpp"
#include "caf/span.hpp"
namespace caf {
/// Serializes C++ objects into a sequence of bytes.
/// @note The binary data format may change between CAF versions and does not
/// perform any type checking at run-time. Thus the output of this
/// serializer is unsuitable for persistence layers.
class CAF_CORE_EXPORT binary_serializer
: public save_inspector_base<binary_serializer> {
public:
// -- member types -----------------------------------------------------------
using super = save_inspector_base<binary_serializer>;
using container_type = byte_buffer;
using value_type = byte;
// -- constructors, destructors, and assignment operators --------------------
binary_serializer(actor_system& sys, byte_buffer& buf) noexcept;
binary_serializer(execution_unit* ctx, byte_buffer& buf) noexcept
: buf_(buf), write_pos_(buf.size()), context_(ctx) {
// nop
}
binary_serializer(const binary_serializer&) = delete;
binary_serializer& operator=(const binary_serializer&) = delete;
// -- properties -------------------------------------------------------------
/// Returns the current execution unit.
execution_unit* context() const noexcept {
return context_;
}
byte_buffer& buf() noexcept {
return buf_;
}
const byte_buffer& buf() const noexcept {
return buf_;
}
size_t write_pos() const noexcept {
return write_pos_;
}
static constexpr bool has_human_readable_format() noexcept {
return false;
}
// -- position management ----------------------------------------------------
/// Sets the write position to `offset`.
/// @pre `offset <= buf.size()`
void seek(size_t offset) noexcept {
write_pos_ = offset;
}
/// Jumps `num_bytes` forward. Resizes the buffer (filling it with zeros)
/// when skipping past the end.
void skip(size_t num_bytes);
// -- interface functions ----------------------------------------------------
constexpr bool begin_object(type_id_t, string_view) noexcept {
return true;
}
constexpr bool end_object() {
return true;
}
constexpr bool begin_field(string_view) noexcept {
return true;
}
bool begin_field(string_view, bool is_present);
bool begin_field(string_view, span<const type_id_t> types, size_t index);
bool begin_field(string_view, bool is_present, span<const type_id_t> types,
size_t index);
constexpr bool end_field() {
return true;
}
constexpr bool begin_tuple(size_t) {
return true;
}
constexpr bool end_tuple() {
return true;
}
constexpr bool begin_key_value_pair() {
return true;
}
constexpr bool end_key_value_pair() {
return true;
}
bool begin_sequence(size_t list_size);
constexpr bool end_sequence() {
return true;
}
bool begin_associative_array(size_t size) {
return begin_sequence(size);
}
bool end_associative_array() {
return end_sequence();
}
bool value(byte x);
bool value(bool x);
bool value(int8_t x);
bool value(uint8_t x);
bool value(int16_t x);
bool value(uint16_t x);
bool value(int32_t x);
bool value(uint32_t x);
bool value(int64_t x);
bool value(uint64_t x);
template <class T>
std::enable_if_t<std::is_integral<T>::value, bool> value(T x) {
return value(static_cast<detail::squashed_int_t<T>>(x));
}
bool value(float x);
bool value(double x);
bool value(long double x);
bool value(string_view x);
bool value(const std::u16string& x);
bool value(const std::u32string& x);
bool value(span<const byte> x);
bool value(const std::vector<bool>& x);
private:
/// Stores the serialized output.
byte_buffer& buf_;
/// Stores the current offset for writing.
size_t write_pos_;
/// Provides access to the ::proxy_registry and to the ::actor_system.
execution_unit* context_;
};
} // namespace caf
|