6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ENDIAN_ENDIAN_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ENDIAN_ENDIAN_HPP_
21#include "arene/base/stdlib_choice/cstddef.hpp"
22#include "arene/base/stdlib_choice/cstdint.hpp"
23#include "arene/base/stdlib_choice/enable_if.hpp"
24#include "arene/base/stdlib_choice/ignore.hpp"
25#include "arene/base/stdlib_choice/is_arithmetic.hpp"
26#include "arene/base/stdlib_choice/is_enum.hpp"
27#include "arene/base/stdlib_choice/is_floating_point.hpp"
28#include "arene/base/stdlib_choice/is_integral.hpp"
29#include "arene/base/stdlib_choice/is_same.hpp"
30#include "arene/base/stdlib_choice/underlying_type.hpp"
35#include "arene/base/endian/detail/endian_windows_impl.hpp"
36#elif defined(__BYTE_ORDER__)
37#include "arene/base/endian/detail/endian_specified_byte_order_impl.hpp"
39#include "arene/base/endian/detail/endian_generic_impl.hpp"
72template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>> =
nullptr>
83template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>> =
nullptr>
97 std::enable_if_t<std::is_floating_point<T>::value>,
98 std::enable_if_t<!std::is_same<T, long double>::value>> =
nullptr>
122template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>> =
nullptr>
133template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>> =
nullptr>
147 std::enable_if_t<std::is_floating_point<T>::value>,
148 std::enable_if_t<!std::is_same<T, long double>::value>> =
nullptr>
170template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>> =
nullptr>
178template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>> =
nullptr>
189 std::enable_if_t<std::is_floating_point<T>::value>,
190 std::enable_if_t<!std::is_same<T, long double>::value>> =
nullptr>
208template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>> =
nullptr>
216template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>> =
nullptr>
227 std::enable_if_t<std::is_floating_point<T>::value>,
228 std::enable_if_t<!std::is_same<T, long double>::value>> =
nullptr>
240namespace endian_detail {
266template <std::
size_t Size>
271class endian_impl<sizeof(std::
uint8_t)> {
279 static constexpr auto read_little_endian(span<
byte const,
sizeof(type)>
const bytes)
noexcept -> type {
280 return ::arene::base::to_integer<type>(bytes[byte_0]);
286 static constexpr void write_little_endian(type
const value, span<
byte,
sizeof(type)>
const bytes)
noexcept {
287 bytes[byte_0] =
to_byte(value);
293 static constexpr auto read_big_endian(span<
byte const,
sizeof(type)>
const bytes)
noexcept -> type {
294 return ::arene::base::to_integer<type>(bytes[byte_0]);
300 static constexpr void write_big_endian(type
const value, span<
byte,
sizeof(type)>
const bytes)
noexcept {
301 bytes[byte_0] =
to_byte(value);
307class endian_impl<sizeof(std::
uint16_t)> {
315 static constexpr auto read_little_endian(span<
byte const,
sizeof(type)>
const bytes)
noexcept -> type {
323 return ::arene::base::to_integer<std::uint16_t>(bytes[byte_0]) |
330 static constexpr void write_little_endian(type
const value, span<
byte,
sizeof(type)>
const bytes)
noexcept {
339 bytes[byte_0] =
to_byte(value);
346 static constexpr auto read_big_endian(span<
byte const,
sizeof(type)>
const bytes)
noexcept -> type {
355 return ::arene::base::to_integer<std::uint16_t>(bytes[byte_1]) |
362 static constexpr void write_big_endian(type
const value, span<
byte,
sizeof(type)>
const bytes)
noexcept {
371 bytes[byte_1] =
to_byte(value);
378class endian_impl<sizeof(std::
uint32_t)> {
385 static constexpr auto read_little_endian(span<
byte const,
sizeof(type)>
const bytes)
noexcept -> type {
394 return ::arene::base::to_integer<std::uint32_t>(bytes[byte_0]) |
406 static constexpr void write_little_endian(type
const value, span<
byte,
sizeof(type)>
const bytes)
noexcept {
414 bytes[byte_0] =
to_byte(value);
415 bytes[byte_1] =
to_byte(value >> bits_8);
416 bytes[byte_2] =
to_byte(value >> bits_16);
417 bytes[byte_3] =
to_byte(value >> bits_24);
423 static constexpr auto read_big_endian(span<
byte const,
sizeof(type)>
const bytes)
noexcept -> type {
432 return ::arene::base::to_integer<std::uint32_t>(bytes[byte_3]) |
441 static constexpr void write_big_endian(type
const value, span<
byte,
sizeof(type)>
const bytes)
noexcept {
450 bytes[byte_3] =
to_byte(value);
451 bytes[byte_2] =
to_byte(value >> bits_8);
452 bytes[byte_1] =
to_byte(value >> bits_16);
453 bytes[byte_0] =
to_byte(value >> bits_24);
459class endian_impl<sizeof(std::
uint64_t)> {
466 static constexpr auto read_little_endian(span<
byte const,
sizeof(type)>
const bytes)
noexcept -> type {
475 return ::arene::base::to_integer<std::uint64_t>(bytes[byte_0]) |
488 static constexpr void write_little_endian(type
const value, span<
byte,
sizeof(type)>
const bytes)
noexcept {
497 bytes[byte_0] =
to_byte(value);
498 bytes[byte_1] =
to_byte(value >> bits_8);
499 bytes[byte_2] =
to_byte(value >> bits_16);
500 bytes[byte_3] =
to_byte(value >> bits_24);
501 bytes[byte_4] =
to_byte(value >> bits_32);
502 bytes[byte_5] =
to_byte(value >> bits_40);
503 bytes[byte_6] =
to_byte(value >> bits_48);
504 bytes[byte_7] =
to_byte(value >> bits_56);
510 static constexpr auto read_big_endian(span<
byte const,
sizeof(type)>
const bytes)
noexcept -> type {
519 return ::arene::base::to_integer<std::uint64_t>(bytes[byte_7]) |
532 static constexpr void write_big_endian(type
const value, span<
byte,
sizeof(type)>
const bytes)
noexcept {
541 bytes[byte_7] =
to_byte(value);
542 bytes[byte_6] =
to_byte(value >> bits_8);
543 bytes[byte_5] =
to_byte(value >> bits_16);
544 bytes[byte_4] =
to_byte(value >> bits_24);
545 bytes[byte_3] =
to_byte(value >> bits_32);
546 bytes[byte_2] =
to_byte(value >> bits_40);
547 bytes[byte_1] =
to_byte(value >> bits_48);
548 bytes[byte_0] =
to_byte(value >> bits_56);
557template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>>>
564template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>>>
565constexpr auto read_little_endian(span<
byte const,
sizeof(T)>
const bytes)
noexcept -> T {
571template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>>>
574 static_cast<typename endian_detail::endian_impl<sizeof(T)
>::type>(value),
579template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>>>
587template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>>>
589 return static_cast<T
>(endian_detail::endian_impl<
sizeof(T)>
::read_big_endian(bytes));
594template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>>>
595constexpr auto read_big_endian(span<
byte const,
sizeof(T)>
const bytes)
noexcept -> T {
601template <typename T, constraints<std::enable_if_t<std::is_integral<T>::value>>>
604 static_cast<typename endian_detail::endian_impl<sizeof(T)
>::type>(value),
609template <typename T, constraints<std::enable_if_t<std::is_enum<T>::value>>>
623 static_assert((endian::native == endian::big) || (endian::native == endian::little),
"Must have fixed endian");
625 switch (endian::native) {
643 static_assert((endian::native == endian::big) || (endian::native == endian::little),
"Must have fixed endian");
645 switch (endian::native) {
663 static_assert((endian::native == endian::big) || (endian::native == endian::little),
"Must have fixed endian");
666 switch (endian::native) {
685 static_assert((endian::native == endian::big) || (endian::native == endian::little),
"Must have fixed endian");
688 switch (endian::native) {
A backport of std::span for C++14. Provides a view over a contiguous range of elements of type T.
Definition span.hpp:315
Definition accumulate.hpp:37
void write_big_endian(base::type_identity_t< T > value, span< byte, sizeof(T)> bytes) noexcept=delete
Write a value to a buffer in big-endian binary representation.
Definition endian.hpp:622
void write_little_endian(base::type_identity_t< T > value, span< byte, sizeof(T)> bytes) noexcept=delete
Write a value to a buffer in little-endian binary representation.
Definition endian.hpp:642
::std::uint32_t uint32_t
A 32-bit unsigned integer type.
Definition integer_types.hpp:36
typename type_identity< T >::type type_identity_t
Polyfill of std::type_identity_t added in C++20.
Definition type_identity.hpp:40
constexpr auto copy(InputIterator first, InputIterator last, OutputIterator output) noexcept(noexcept(*(++output)) &&noexcept(*(++first)) &&noexcept(first !=last) &&std::is_nothrow_assignable<, typename std::iterator_traits< InputIterator >::reference >::value) -> OutputIterator
Copies the elements in the range, defined by [first, last), to another range beginning at output.
Definition copy.hpp:40
constexpr auto to_byte(ValueType const val) noexcept -> byte
Convert an integer to an arene::base::byte.
Definition byte.hpp:46
auto as_bytes(span< T, N > spn) noexcept -> span< byte const, span_detail::is_dynamic_extent(N) ? dynamic_extent :(N *sizeof(T))>
Obtain a const view to the object representation of the elements of the span spn.
Definition span.hpp:967
auto read_little_endian(span< byte const, sizeof(T)> bytes) noexcept -> T=delete
Read a value from a serialized binary representation stored in little-endian byte order.
Definition endian.hpp:684
::std::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition integer_types.hpp:46
::std::uint16_t uint16_t
A 16-bit unsigned integer type.
Definition integer_types.hpp:27
auto as_writable_bytes(span< T, N > spn) noexcept -> span< byte, span_detail::is_dynamic_extent(N) ? dynamic_extent :(N *sizeof(T))>
Obtain a non-const view to the object representation of the elements of the span spn.
Definition span.hpp:977
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
auto read_big_endian(span< byte const, sizeof(T)> bytes) noexcept -> T=delete
Read a value from a serialized binary representation stored in big-endian byte order.
Definition endian.hpp:662
::std::uint8_t uint8_t
An 8-bit unsigned integer type.
Definition integer_types.hpp:17
constexpr auto to_integer(byte const val) noexcept -> IntegerType
convert the byte value to an integral representation.
Definition byte.hpp:59
Definition accumulate.hpp:36
underlying_type_detail::underlying_type< T > underlying_type
Obtains the underlying integer type for a given enumeration type.
Definition underlying_type.hpp:52
::uint8_t uint8_t
An 8-bit unsigned integer type.
Definition cstdint.hpp:30
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
::uint16_t uint16_t
A 16-bit unsigned integer type.
Definition cstdint.hpp:36
::uint32_t uint32_t
A 32-bit unsigned integer type.
Definition cstdint.hpp:41
::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition cstdint.hpp:47
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72