Skip to content

Commit f62450b

Browse files
Added byteswap header, redesigned extract_append with endian flexibility
1 parent fc2b94c commit f62450b

File tree

6 files changed

+269
-453
lines changed

6 files changed

+269
-453
lines changed

include/serialize/binary_serialize.hpp

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
/** @file
1+
/** @mainpage Binary Serialuze, Classes and Functions For Binary Data Serialization
22
*
3-
* @defgroup marshall_module Classes and functions for big-endian binary data
4-
* marshalling and unmarshalling (transform objects into and out of byte streams
5-
* for transmission over a network or for file IO).
3+
* Serialization transforms objects into a byte stream for transmission over a
4+
* network or for file IO. Deserialization is the converse, transforming a byte
5+
* stream into application level objects.
66
*
7-
* @brief Classes and functions to transform objects into a big-endian binary stream
8-
* of bytes (marshall) and the converse (unmarshall), transform a stream of bytes into
9-
* objects.
10-
*
11-
* The @c utility-rack @c marshall and @c unmarshall functions and classes provide a
12-
* simple and light abstraction for binary big-endian serialization. There are no
7+
* This library differs from other binary serialization libraries in that the
8+
* main interfaces is a "std::format" like
9+
* These functions and classes provide a simple and light abstraction for binary big-endian serialization. There are no
1310
* message or element definitions, no embedded preprocesser syntax, and no extra
1411
* build steps.
1512
*
@@ -113,8 +110,8 @@
113110
*
114111
*/
115112

116-
#ifndef MARSHALL_HPP_INCLUDED
117-
#define MARSHALL_HPP_INCLUDED
113+
#ifndef BINARY_SERIALIZE_HPP_INCLUDED
114+
#define BINARY_SERIALIZE_HPP_INCLUDED
118115

119116
#include "utility/cast_ptr_to.hpp"
120117
#include "marshall/shared_buffer.hpp"

include/serialize/byteswap.hpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/** @file
2+
*
3+
* @brief This is a direct implementation of the C++ 23 std::byteswap function,
4+
* for use in pre C++ 23 applications.
5+
*
6+
* This implementation is taken directly from the CPP Reference page:
7+
* https://en.cppreference.com/w/cpp/numeric/byteswap
8+
*
9+
* @author Cliff Green, CPP Reference
10+
*
11+
* @copyright (c) 2024 by Cliff Green
12+
*
13+
* Distributed under the Boost Software License, Version 1.0.
14+
* (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
15+
*
16+
*/
17+
18+
#ifndef BYTESWAP_HPP_INCLUDED
19+
#define BYTESWAP_HPP_INCLUDED
20+
21+
#include <concepts> // std::integral
22+
#include <bit> // std::bit_cast
23+
#include <array>
24+
#include <cstddef> // std::byte
25+
#include <algorithm> // std::ranges::reverse
26+
27+
namespace chops {
28+
29+
template<std::integral T>
30+
constexpr T byteswap(T value) noexcept {
31+
if constexpr (sizeof(T) == 1u) {
32+
return value;
33+
}
34+
static_assert(std::has_unique_object_representations_v<T>,
35+
"T may not have padding bits");
36+
auto value_representation = std::bit_cast<std::array<std::byte, sizeof(T)>>(value);
37+
std::ranges::reverse(value_representation);
38+
return std::bit_cast<T>(value_representation);
39+
}
40+
41+
} // end namespace
42+
43+
#endif
44+

0 commit comments

Comments
 (0)