Skip to content

Commit 9ec6329

Browse files
working on new API and details
1 parent 93a61d6 commit 9ec6329

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

include/serialize/binary_serialize.hpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/** @mainpage Binary Serialuze, Classes and Functions For Binary Data Serialization
2+
*
3+
* ## Overview
24
*
35
* Serialization transforms objects into a byte stream for transmission over a
46
* network or for file IO. Deserialization is the converse, transforming a byte
57
* stream into application level objects.
68
*
79
* 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+
*
912
* These functions and classes provide a simple and light abstraction for binary big-endian serialization. There are no
1013
* message or element definitions, no embedded preprocesser syntax, and no extra
1114
* build steps.
@@ -113,7 +116,6 @@
113116
#ifndef BINARY_SERIALIZE_HPP_INCLUDED
114117
#define BINARY_SERIALIZE_HPP_INCLUDED
115118

116-
#include "utility/cast_ptr_to.hpp"
117119
#include "buffer/shared_buffer.hpp"
118120
#include "serialize/extract_append.hpp"
119121

@@ -127,12 +129,28 @@
127129
#include <iterator> // value type of iterator
128130
#include <array>
129131
#include <cassert>
132+
#include <concepts>
130133

131134
#include <iostream> // debugging
132135

133136
namespace chops {
134137

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+
}
135152
/**
153+
136154
* @brief Extract a sequence in network byte order from a @c std::byte buffer into the
137155
* provided container.
138156
*
@@ -188,6 +206,8 @@ Container extract_sequence(const std::byte* buf) noexcept(fill in) {
188206
* the elements in the sequence.
189207
*
190208
*/
209+
210+
191211
/*
192212
template <typename Cnt, typename Iter>
193213
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) {
278298
return buf;
279299
}
280300

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+
}
281312
/**
282313
* @brief Marshall a single arithmetic value or a @c std::byte into a buffer of bytes.
283314
*

include/serialize/byteswap.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @file
22
*
3-
* @brief This is a direct implementation of the C++ 23 std::byteswap function,
3+
* @brief This is an implementation of the C++ 23 @c std::byteswap function,
44
* for use in pre C++ 23 applications.
55
*
66
* This implementation is taken directly from the CPP Reference page:
@@ -18,14 +18,25 @@
1818
#ifndef BYTESWAP_HPP_INCLUDED
1919
#define BYTESWAP_HPP_INCLUDED
2020

21-
#include <concepts> // std::integral
21+
#include <concepts> // std::integral concept
2222
#include <bit> // std::bit_cast
2323
#include <array>
2424
#include <cstddef> // std::byte
2525
#include <algorithm> // std::ranges::reverse
2626

2727
namespace chops {
2828

29+
/**
30+
* @brief Perform an in-place byte swap on an integral type (if size of the
31+
* type is greater than one).
32+
*
33+
* @tparam T Type of value where swapping will occur.
34+
*
35+
* @param value Integral value to be byte swapped.
36+
*
37+
* @pre There must not be any padding bits in the type.
38+
*
39+
*/
2940
template<std::integral T>
3041
constexpr T byteswap(T value) noexcept {
3142
if constexpr (sizeof(T) == 1u) {

include/serialize/extract_append.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* endian order) to native format; conversely, given an arithmetic binary value, append
55
* it to a buffer of bytes in the specified endian order.
66
*
7-
* The functions in this file are low-level. The handle fundamental arithmetic types and
7+
* The functions in this file are low-level. They handle fundamental arithmetic types and
88
* extracting or appending to @c std::byte buffers. It is meant to be the lower layer
9-
* of serializing utilities, where the next layer up provides buffer management,
9+
* of serializing utilities, where the next higher layer provides buffer management,
1010
* sequences, and overloads for specific types such as @c std::string, @c bool, and
1111
* @c std::optional.
1212
*
@@ -78,7 +78,7 @@ concept integral_or_byte = std::integral<T> || std::is_same_v<std::remove_cv_t<T
7878
* @brief Extract a value from a @c std::byte buffer into a fundamental integral
7979
* or @c std::byte type in native endianness, swapping bytes as needed.
8080
*
81-
* @tparam BufEndian The endianness of the buffer.
81+
* @tparam BufEndian The endianness of the @c std::byte buffer.
8282
*
8383
* @tparam T Type of return value.
8484
*
@@ -166,7 +166,7 @@ constexpr std::size_t append_val(std::byte* buf, const T& val) noexcept {
166166
* will result in the native endianness of the platform. I.e. this works whether serialization
167167
* is big-endian or little-endian.
168168
*
169-
* @note Unsigned types are not supported.
169+
* @note Signed types are not supported.
170170
*
171171
* @param val The input value. Any standard unsigned integer type is allowed.
172172
*
@@ -204,6 +204,8 @@ constexpr std::size_t append_var_int(std::byte* output, T val) {
204204
* For consistency with the @c append_var_int function, only unsigned integers are
205205
* supported for the output type of this function.
206206
*
207+
* @note Signed types are not supported.
208+
*
207209
* @param input A variable-length encoded integer stored in a buffer of @c std::bytes.
208210
*
209211
* @param input_size Number of bytes representing the integer.

0 commit comments

Comments
 (0)