TRLC Platform Library  1.0.0
Header-only C++ library for compile-time platform detection and abstraction
endianness.hpp File Reference

Byte order detection and utilities for cross-platform development. More...

#include <cstdint>
#include <type_traits>

Go to the source code of this file.

Classes

struct  trlc::platform::EndiannessInfo
 Comprehensive endianness information structure. More...
 

Namespaces

 trlc
 
 trlc::platform
 
 trlc::platform::detail
 

Macros

#define TRLC_ENDIAN_LITTLE   (trlc::platform::isLittleEndian())
 Check if system is little-endian at compile time. More...
 
#define TRLC_ENDIAN_BIG   (trlc::platform::isBigEndian())
 Check if system is big-endian at compile time. More...
 
#define TRLC_BYTE_SWAP(x)   (trlc::platform::byteSwap(x))
 Swap bytes in a value. More...
 
#define TRLC_HTON(x)   (trlc::platform::hostToNetwork(x))
 Convert value from host to network byte order. More...
 
#define TRLC_NTOH(x)   (trlc::platform::networkToHost(x))
 Convert value from network to host byte order. More...
 
#define TRLC_HTONS(x)   (trlc::platform::hostToNetworkShort(x))
 Host to network byte order for 16-bit values. More...
 
#define TRLC_HTONL(x)   (trlc::platform::hostToNetworkLong(x))
 Host to network byte order for 32-bit values. More...
 
#define TRLC_NTOHS(x)   (trlc::platform::networkToHostShort(x))
 Network to host byte order for 16-bit values. More...
 
#define TRLC_NTOHL(x)   (trlc::platform::networkToHostLong(x))
 Network to host byte order for 32-bit values. More...
 
#define TRLC_ENDIANNESS_INCLUDED
 

Enumerations

enum class  trlc::platform::ByteOrder : int { trlc::platform::unknown = 0 , trlc::platform::little_endian , trlc::platform::big_endian , trlc::platform::mixed_endian }
 Byte order enumeration. More...
 

Functions

static ByteOrder trlc::platform::detail::detectByteOrderCompileTime ()
 Compile-time endianness detection using preprocessor macros. More...
 
ByteOrder trlc::platform::detail::detectByteOrderRuntime ()
 Runtime endianness detection using union method. More...
 
static ByteOrder trlc::platform::getByteOrder ()
 Get the system's byte order. More...
 
static EndiannessInfo trlc::platform::getEndiannessInfo ()
 Get comprehensive endianness information. More...
 
static bool trlc::platform::isLittleEndian ()
 Check if system is little-endian. More...
 
static bool trlc::platform::isBigEndian ()
 Check if system is big-endian. More...
 
static uint16_t trlc::platform::detail::byteSwap16Impl (uint16_t value)
 16-bit byte swap implementation More...
 
static uint32_t trlc::platform::detail::byteSwap32Impl (uint32_t value)
 32-bit byte swap implementation More...
 
static uint64_t trlc::platform::detail::byteSwap64Impl (uint64_t value)
 64-bit byte swap implementation More...
 
static uint16_t trlc::platform::byteSwap16 (uint16_t value)
 Swap bytes in a 16-bit value. More...
 
static uint32_t trlc::platform::byteSwap32 (uint32_t value)
 Swap bytes in a 32-bit value. More...
 
static uint64_t trlc::platform::byteSwap64 (uint64_t value)
 Swap bytes in a 64-bit value. More...
 
template<typename Type >
static Type trlc::platform::byteSwap (Type value)
 Generic byte swap function for integral types. More...
 
template<typename Type >
static Type trlc::platform::hostToNetwork (Type value)
 Convert from host byte order to network byte order (big-endian) More...
 
template<typename Type >
static Type trlc::platform::networkToHost (Type value)
 Convert from network byte order (big-endian) to host byte order. More...
 
static uint16_t trlc::platform::hostToNetworkShort (uint16_t hostshort)
 Convert 16-bit value from host to network byte order. More...
 
static uint32_t trlc::platform::hostToNetworkLong (uint32_t hostlong)
 Convert 32-bit value from host to network byte order. More...
 
static uint16_t trlc::platform::networkToHostShort (uint16_t netshort)
 Convert 16-bit value from network to host byte order. More...
 
static uint32_t trlc::platform::networkToHostLong (uint32_t netlong)
 Convert 32-bit value from network to host byte order. More...
 
static bool trlc::platform::areByteOrdersCompatible (ByteOrder order1, ByteOrder order2)
 Check if two byte orders are compatible (no swapping needed) More...
 
static ByteOrder trlc::platform::getOppositeByteOrder (ByteOrder order)
 Get the opposite byte order. More...
 
template<typename Type >
static Type trlc::platform::convertByteOrder (Type value, ByteOrder from_order, ByteOrder to_order)
 Convert value between specified byte orders. More...
 

Detailed Description

Byte order detection and utilities for cross-platform development.

This header provides comprehensive endianness detection and byte manipulation utilities. It includes compile-time byte order detection, efficient byte swapping functions with compiler intrinsics, and network byte order conversion utilities.

Features:

  • Compile-time endianness detection with runtime fallbacks
  • Efficient byte swapping using compiler intrinsics
  • Network/host byte order conversion functions
  • Template-based generic byte manipulation utilities
  • Comprehensive macro interface for easy usage
Author
TRLC Platform Team
Version
1.0.0

Definition in file endianness.hpp.

Macro Definition Documentation

◆ TRLC_BYTE_SWAP

#define TRLC_BYTE_SWAP (   x)    (trlc::platform::byteSwap(x))

Swap bytes in a value.

Generic macro for byte swapping that works with any integral type.

Parameters
xValue to byte swap
Returns
Byte-swapped value

Definition at line 538 of file endianness.hpp.

◆ TRLC_ENDIAN_BIG

#define TRLC_ENDIAN_BIG   (trlc::platform::isBigEndian())

Check if system is big-endian at compile time.

Evaluates to true if the system uses big-endian byte order.

Definition at line 528 of file endianness.hpp.

◆ TRLC_ENDIAN_LITTLE

#define TRLC_ENDIAN_LITTLE   (trlc::platform::isLittleEndian())

Check if system is little-endian at compile time.

Evaluates to true if the system uses little-endian byte order.

Definition at line 521 of file endianness.hpp.

◆ TRLC_ENDIANNESS_INCLUDED

#define TRLC_ENDIANNESS_INCLUDED

Definition at line 597 of file endianness.hpp.

◆ TRLC_HTON

#define TRLC_HTON (   x)    (trlc::platform::hostToNetwork(x))

Convert value from host to network byte order.

Parameters
xValue in host byte order
Returns
Value in network byte order

Definition at line 546 of file endianness.hpp.

◆ TRLC_HTONL

#define TRLC_HTONL (   x)    (trlc::platform::hostToNetworkLong(x))

Host to network byte order for 32-bit values.

Equivalent to POSIX htonl().

Parameters
x32-bit value in host byte order
Returns
32-bit value in network byte order

Definition at line 574 of file endianness.hpp.

◆ TRLC_HTONS

#define TRLC_HTONS (   x)    (trlc::platform::hostToNetworkShort(x))

Host to network byte order for 16-bit values.

Equivalent to POSIX htons().

Parameters
x16-bit value in host byte order
Returns
16-bit value in network byte order

Definition at line 564 of file endianness.hpp.

◆ TRLC_NTOH

#define TRLC_NTOH (   x)    (trlc::platform::networkToHost(x))

Convert value from network to host byte order.

Parameters
xValue in network byte order
Returns
Value in host byte order

Definition at line 554 of file endianness.hpp.

◆ TRLC_NTOHL

#define TRLC_NTOHL (   x)    (trlc::platform::networkToHostLong(x))

Network to host byte order for 32-bit values.

Equivalent to POSIX ntohl().

Parameters
x32-bit value in network byte order
Returns
32-bit value in host byte order

Definition at line 594 of file endianness.hpp.

◆ TRLC_NTOHS

#define TRLC_NTOHS (   x)    (trlc::platform::networkToHostShort(x))

Network to host byte order for 16-bit values.

Equivalent to POSIX ntohs().

Parameters
x16-bit value in network byte order
Returns
16-bit value in host byte order

Definition at line 584 of file endianness.hpp.