WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
SwapByteOrder.h
Go to the documentation of this file.
1//===- SwapByteOrder.h - Generic and optimized byte swaps -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares generic and optimized functions to swap the byte order of
10// an integral type.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef WPIUTIL_WPI_SWAPBYTEORDER_H
15#define WPIUTIL_WPI_SWAPBYTEORDER_H
16
18#include "wpi/bit.h"
19#include <cstdint>
20#include <type_traits>
21
22namespace wpi {
23
24namespace sys {
25
26constexpr bool IsBigEndianHost =
28
30
31inline unsigned char getSwappedBytes(unsigned char C) { return wpi::byteswap(C); }
32inline signed char getSwappedBytes( signed char C) { return wpi::byteswap(C); }
33inline char getSwappedBytes( char C) { return wpi::byteswap(C); }
34
35inline unsigned short getSwappedBytes(unsigned short C) { return wpi::byteswap(C); }
36inline signed short getSwappedBytes( signed short C) { return wpi::byteswap(C); }
37
38inline unsigned int getSwappedBytes(unsigned int C) { return wpi::byteswap(C); }
39inline signed int getSwappedBytes( signed int C) { return wpi::byteswap(C); }
40
41inline unsigned long getSwappedBytes(unsigned long C) { return wpi::byteswap(C); }
42inline signed long getSwappedBytes( signed long C) { return wpi::byteswap(C); }
43
44inline unsigned long long getSwappedBytes(unsigned long long C) { return wpi::byteswap(C); }
45inline signed long long getSwappedBytes( signed long long C) { return wpi::byteswap(C); }
46
50
51inline double getSwappedBytes(double C) {
53}
54
55template <typename T>
56inline std::enable_if_t<std::is_enum_v<T>, T> getSwappedBytes(T C) {
57 return static_cast<T>(wpi::byteswap(wpi::to_underlying(C)));
58}
59
60template<typename T>
61inline void swapByteOrder(T &Value) {
62 Value = getSwappedBytes(Value);
63}
64
65} // end namespace sys
66} // end namespace wpi
67
68#endif
This file contains library features backported from future STL versions.
This file implements the C++20 <bit> header.
void swapByteOrder(T &Value)
Definition SwapByteOrder.h:61
constexpr bool IsBigEndianHost
Definition SwapByteOrder.h:26
unsigned char getSwappedBytes(unsigned char C)
Definition SwapByteOrder.h:31
static const bool IsLittleEndianHost
Definition SwapByteOrder.h:29
Foonathan namespace.
Definition ntcore_cpp.h:26
constexpr T byteswap(T V) noexcept
Reverses the bytes in the given integer value V.
Definition bit.h:63
To bit_cast(const From &from) noexcept
Definition bit.h:51
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
Definition STLForwardCompat.h:66