|
1 | 1 | /** @mainpage Binary Serialuze, Classes and Functions For Binary Data Serialization
|
| 2 | + * |
| 3 | + * ## Overview |
2 | 4 | *
|
3 | 5 | * Serialization transforms objects into a byte stream for transmission over a
|
4 | 6 | * network or for file IO. Deserialization is the converse, transforming a byte
|
5 | 7 | * stream into application level objects.
|
6 | 8 | *
|
7 | 9 | * This library differs from other binary serialization libraries in that the
|
8 |
| - * main interfaces is a "std::format" like |
| 10 | + * main interfaces is a "std::format" like interface. |
| 11 | + * |
9 | 12 | * These functions and classes provide a simple and light abstraction for binary big-endian serialization. There are no
|
10 | 13 | * message or element definitions, no embedded preprocesser syntax, and no extra
|
11 | 14 | * build steps.
|
|
113 | 116 | #ifndef BINARY_SERIALIZE_HPP_INCLUDED
|
114 | 117 | #define BINARY_SERIALIZE_HPP_INCLUDED
|
115 | 118 |
|
116 |
| -#include "utility/cast_ptr_to.hpp" |
117 | 119 | #include "buffer/shared_buffer.hpp"
|
118 | 120 | #include "serialize/extract_append.hpp"
|
119 | 121 |
|
|
127 | 129 | #include <iterator> // value type of iterator
|
128 | 130 | #include <array>
|
129 | 131 | #include <cassert>
|
| 132 | +#include <concepts> |
130 | 133 |
|
131 | 134 | #include <iostream> // debugging
|
132 | 135 |
|
133 | 136 | namespace chops {
|
134 | 137 |
|
| 138 | +template <typename Ctr> |
| 139 | +concept supports_expandable_buffer = |
| 140 | + std::same_as<Ctr::value_type, std::byte> && |
| 141 | + requires (Ctr ctr) { |
| 142 | + { ctr.size() } -> std::integral; |
| 143 | + ctr.resize(std::size_t{}); |
| 144 | + { ctr.data() } -> std::same_as<std::byte*>; |
| 145 | + } |
| 146 | + |
| 147 | +template <supports_expandable_buffer Ctr = chops::mutable_shared_buffer, |
| 148 | + std::endian Endian = std::endian::little> |
| 149 | +struct expandable_buffer : Ctr { |
| 150 | + using Endian; |
| 151 | +} |
135 | 152 | /**
|
| 153 | +
|
136 | 154 | * @brief Extract a sequence in network byte order from a @c std::byte buffer into the
|
137 | 155 | * provided container.
|
138 | 156 | *
|
@@ -188,6 +206,8 @@ Container extract_sequence(const std::byte* buf) noexcept(fill in) {
|
188 | 206 | * the elements in the sequence.
|
189 | 207 | *
|
190 | 208 | */
|
| 209 | + |
| 210 | + |
191 | 211 | /*
|
192 | 212 | template <typename Cnt, typename Iter>
|
193 | 213 | std::size_t append_sequence(std::byte* buf, Cnt cnt, Iter start, Iter end) noexcept {
|
@@ -278,6 +298,17 @@ Buf& marshall(Buf& buf, const T& val, adl_tag) {
|
278 | 298 | return buf;
|
279 | 299 | }
|
280 | 300 |
|
| 301 | +namespace detail { |
| 302 | + |
| 303 | +template <typename CastValType, typename T, typename Buf = expandable_buffer> |
| 304 | +constexpr Buf& serialize_val(Buf& buf, const T& val) { |
| 305 | + auto old_sz = buf.size(); |
| 306 | + buf.resize(old_sz + sizeof(CastValType)); |
| 307 | + append_val(buf.data()+old_sz, static_cast<CastValType>(val)); |
| 308 | + return buf; |
| 309 | +} |
| 310 | + |
| 311 | +} |
281 | 312 | /**
|
282 | 313 | * @brief Marshall a single arithmetic value or a @c std::byte into a buffer of bytes.
|
283 | 314 | *
|
|
0 commit comments