StormByte C++ Library 1.2.0
StormByte is a comprehensive, cross-platform C++ library aimed at easing system programming, configuration management, logging, and database handling tasks. This library provides a unified API that abstracts away the complexities and inconsistencies of different platforms (Windows, Linux).
Loading...
Searching...
No Matches
detail.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte.
5 *
6 * StormByte is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 3
8 * or later, as published by the Free Software Foundation.
9 *
10 * StormByte is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with StormByte. If not, see
17 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
18 */
19
20#pragma once
21
22// TriviallyCopyable / Integral: swap_endian's requires-clause and dispatch.
25
26#include <array>
27#include <bit>
28#include <cstddef>
29
34namespace StormByte {
35 namespace Type {
43 namespace Detail {
58 template<typename U>
59 requires TriviallyCopyable<U> && (sizeof(U) > 0)
60 constexpr U swap_endian(U val) noexcept {
61 if constexpr (sizeof(U) == 1) {
62 return val;
63 } else if constexpr (Integral<U>) {
64 return std::byteswap(val);
65 } else {
66 auto bytes = std::bit_cast<std::array<std::byte, sizeof(U)>>(val);
67 for (std::size_t i = 0, j = sizeof(U); i < j; ++i) {
68 --j;
69 const std::byte tmp = bytes[i];
70 bytes[i] = bytes[j];
71 bytes[j] = tmp;
72 }
73 return std::bit_cast<U>(bytes);
74 }
75 }
76 }
77 }
78}
Integral type (bool, char, int, long, …), including cv.
Definition categories.hxx:139
constexpr U swap_endian(U val) noexcept
Reverses the object-representation byte order of val.
Definition detail.hxx:60
Root namespace of the StormByte suite.