qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
endian.h File Reference

Endianness detection and byte swapping utilities. More...

#include <cstdint>
#include <cstring>
#include <type_traits>
Include dependency graph for endian.h:

Go to the source code of this file.

Enumerations

enum class  qb::endian::order { little , big , unknown }
 Enumeration of byte order types. More...

Functions

constexpr order qb::endian::native_order ()
 Determines the system's native byte order at runtime.
constexpr bool qb::endian::is_little_endian ()
 Checks if the system is little-endian at compile time when possible.
constexpr bool qb::endian::is_big_endian ()
 Checks if the system is big-endian.
template<typename T>
qb::endian::byteswap (T value)
 Swaps the byte order of a value.
template<typename T>
qb::endian::to_big_endian (T value)
 Converts a value from native endianness to big-endian.
template<typename T>
qb::endian::from_big_endian (T value)
 Converts a value from big-endian to native endianness.
template<typename T>
qb::endian::to_little_endian (T value)
 Converts a value from native endianness to little-endian.
template<typename T>
qb::endian::from_little_endian (T value)
 Converts a value from little-endian to native endianness.

Detailed Description

Endianness detection and byte swapping utilities.

This file provides utilities for detecting the system's native endianness and converting values between different byte orders (little endian and big endian). It includes functions for runtime and compile-time endianness detection, as well as safe byte-swapping operations that work with arithmetic and enum types.

Author
qb - C++ Actor Framework

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Enumeration Type Documentation

◆ order

enum class qb::endian::order
strong

Enumeration of byte order types.

Enumerator
little 

Little-endian (least significant byte first)

big 

Big-endian (most significant byte first)

unknown 

Unknown endianness.

Function Documentation

◆ native_order()

order qb::endian::native_order ( )
constexpr

Determines the system's native byte order at runtime.

Uses compiler macros for detection when available, with a fallback to runtime detection using a union.

Returns
The native byte order of the system

◆ is_little_endian()

bool qb::endian::is_little_endian ( )
constexpr

Checks if the system is little-endian at compile time when possible.

Uses compiler macros for detection when available, with a fallback to runtime detection.

Returns
true if system is little-endian, false otherwise

◆ is_big_endian()

bool qb::endian::is_big_endian ( )
constexpr

Checks if the system is big-endian.

Returns
true if system is big-endian, false otherwise

◆ byteswap()

template<typename T>
T qb::endian::byteswap ( T value)
inline

Swaps the byte order of a value.

Reverses the bytes of any trivially copyable type. This function is safe to use with any arithmetic or enum type.

Template Parameters
TThe type of value to byte-swap (must be arithmetic or enum and trivially copyable)
Parameters
valueThe value to byte-swap
Returns
The byte-swapped value

◆ to_big_endian()

template<typename T>
T qb::endian::to_big_endian ( T value)
inline

Converts a value from native endianness to big-endian.

Template Parameters
TThe type of value to convert
Parameters
valueThe value to convert
Returns
The value in big-endian byte order

◆ from_big_endian()

template<typename T>
T qb::endian::from_big_endian ( T value)
inline

Converts a value from big-endian to native endianness.

Template Parameters
TThe type of value to convert
Parameters
valueThe big-endian value to convert
Returns
The value in native byte order

◆ to_little_endian()

template<typename T>
T qb::endian::to_little_endian ( T value)
inline

Converts a value from native endianness to little-endian.

Template Parameters
TThe type of value to convert
Parameters
valueThe value to convert
Returns
The value in little-endian byte order

◆ from_little_endian()

template<typename T>
T qb::endian::from_little_endian ( T value)
inline

Converts a value from little-endian to native endianness.

Template Parameters
TThe type of value to convert
Parameters
valueThe little-endian value to convert
Returns
The value in native byte order