pabigot  0.1.1
C++ support classes
Data Structures | Enumerations | Functions | Variables
pabigot::byteorder Namespace Reference

Functionality related to byte order and endianness. More...

Data Structures

class  octets_helper
 Infrastructure to fill an octet buffer with data. More...
 

Enumerations

enum  byte_order_enum : uint32_t { byte_order_enum::little_endian = 0x01020304U, byte_order_enum::big_endian = 0x04030201U, byte_order_enum::network = big_endian, byte_order_enum::pdp_endian = 0x03040102U }
 Enumeration of constants representing various known byte orders. More...
 

Functions

constexpr byte_order_enum host_byte_order ()
 Return the byte_order_enum value for the host. More...
 
template<typename T >
constexpr std::enable_if< details::is_constexpr_swappable_v< T >, T >::type byteswap (const T &t)
 Byte-swap values at compile-time. More...
 
template<typename T >
std::enable_if< details::is_alias_swappable_v< T >, T >::type byteswap (const T &t)
 Byte-swap values by aliasing a copy of the value to a uint8_t sequence.
 
template<typename T >
std::enable_if< details::is_other_swappable_v< T >, T >::type byteswap (const T &t)
 Byte-swap values by creating a copy and using std::reverse. More...
 
template<typename T , byte_order_enum endian>
constexpr std::enable_if< details::is_constexpr_swappable_v< T > &&(host_byte_order()==endian), T >::type hostswap (const T &t)
 constexpr-selected byte swap between host and endian. More...
 
template<typename T , byte_order_enum endian>
std::enable_if<!details::is_constexpr_swappable_v< T > &&(host_byte_order()==endian), T >::type hostswap (const T &t)
 constexpr-selected byte swap between host and endian. More...
 
template<typename T , byte_order_enum endian>
constexpr std::enable_if< details::is_constexpr_swappable_v< T > &&(host_byte_order() !=endian), T >::type hostswap (const T &t)
 constexpr-selected byte swap between host and endian. More...
 
template<typename T , byte_order_enum endian>
std::enable_if<!details::is_constexpr_swappable_v< T > &&(host_byte_order() !=endian), T >::type hostswap (const T &t)
 constexpr-selected byte swap between host and endian. More...
 
template<typename T >
constexpr std::enable_if< details::is_constexpr_swappable_v< T >, T >::type host_x_le (const T &v)
 Convert between host and little-endian byte order. More...
 
template<typename T >
std::enable_if<!details::is_constexpr_swappable_v< T >, T >::type host_x_le (const T &v)
 Convert between host and little-endian byte order.
 
template<typename T >
constexpr std::enable_if< details::is_constexpr_swappable_v< T >, T >::type host_x_be (const T &v)
 Convert between host and big-endian byte order. More...
 
template<typename T >
std::enable_if<!details::is_constexpr_swappable_v< T >, T >::type host_x_be (const T &v)
 Convert between host and big-endian byte order.
 
template<typename T >
constexpr std::enable_if< details::is_constexpr_swappable_v< T >, T >::type be_x_le (const T &v)
 Convert between big-endian and little-endian byte order. More...
 
template<typename T >
std::enable_if<!details::is_constexpr_swappable_v< T >, T >::type be_x_le (const T &v)
 Convert between big-endian and little-endian byte order.
 
template<typename T >
constexpr std::enable_if< details::is_constexpr_swappable_v< T >, T >::type host_x_network (const T &v)
 Convert between host and network byte order. More...
 
template<typename T >
std::enable_if<!details::is_constexpr_swappable_v< T >, T >::type host_x_network (const T &v)
 Convert between host and network byte order.
 

Variables

static constexpr wchar_t BOM = u'\uFFFE'
 The Unicode byte order marker.
 

Detailed Description

Functionality related to byte order and endianness.

Enumeration Type Documentation

◆ byte_order_enum

enum pabigot::byteorder::byte_order_enum : uint32_t
strong

Enumeration of constants representing various known byte orders.

The constants are the host interpretation of a 4-byte sequence {4, 3, 2, 1} as a 32-bit unsigned integer.

Enumerator
little_endian 

Representation for a host using little-endian order.

big_endian 

Representation for a host using big-endian order.

network 

Representation for network byte order, which is big-endian.

pdp_endian 

Representation for PDP byte order.

This is identified for completeness but will almost certainly fail to work correctly and isn't really supported. Use it as a sign that byte order could not be identified.

Function Documentation

◆ be_x_le()

template<typename T >
constexpr std::enable_if<details::is_constexpr_swappable_v<T>, T>::type pabigot::byteorder::be_x_le ( const T &  v)
constexpr

Convert between big-endian and little-endian byte order.

Note
This constexpr overload is only available for types where a constexpr byte swap is supported, i.e. integral scalars.

◆ byteswap() [1/2]

template<typename T >
constexpr std::enable_if<details::is_constexpr_swappable_v<T>, T>::type pabigot::byteorder::byteswap ( const T &  t)
constexpr

Byte-swap values at compile-time.

Note
This constexpr overload is only available for types where a compile-time byte swap is supported, i.e. integral scalars.

◆ byteswap() [2/2]

template<typename T >
std::enable_if<details::is_other_swappable_v<T>, T>::type pabigot::byteorder::byteswap ( const T &  t)

Byte-swap values by creating a copy and using std::reverse.

Note
This is really intended only for things like std::array<uint8_t, 6>. It happens to work on std::vector<uint8_t>, but it'll fail to compile with some types, including ones acceptable to details::is_other_swappable_v().

◆ host_byte_order()

constexpr byte_order_enum pabigot::byteorder::host_byte_order ( )
constexpr

Return the byte_order_enum value for the host.

The implementation of this relies on compiler support, as there is no portable C++17 solution to determining runtime byte order at compile-time.

◆ host_x_be()

template<typename T >
constexpr std::enable_if<details::is_constexpr_swappable_v<T>, T>::type pabigot::byteorder::host_x_be ( const T &  v)
constexpr

Convert between host and big-endian byte order.

Note
This constexpr overload is only available for types where a constexpr byte swap is supported, i.e. integral scalars.

◆ host_x_le()

template<typename T >
constexpr std::enable_if<details::is_constexpr_swappable_v<T>, T>::type pabigot::byteorder::host_x_le ( const T &  v)
constexpr

Convert between host and little-endian byte order.

Note
This constexpr overload is only available for types where a constexpr byte swap is supported, i.e. integral scalars.

◆ host_x_network()

template<typename T >
constexpr std::enable_if<details::is_constexpr_swappable_v<T>, T>::type pabigot::byteorder::host_x_network ( const T &  v)
constexpr

Convert between host and network byte order.

Note
This constexpr overload is only available for types where a constexpr byte swap is supported, i.e. integral scalars.

◆ hostswap() [1/4]

template<typename T , byte_order_enum endian>
constexpr std::enable_if<details::is_constexpr_swappable_v<T> && (host_byte_order() == endian), T>::type pabigot::byteorder::hostswap ( const T &  t)
constexpr

constexpr-selected byte swap between host and endian.

This variant is available when host uses the desired endian and constexpr byte swap is supported for the type.

◆ hostswap() [2/4]

template<typename T , byte_order_enum endian>
std::enable_if<!details::is_constexpr_swappable_v<T> && (host_byte_order() == endian), T>::type pabigot::byteorder::hostswap ( const T &  t)

constexpr-selected byte swap between host and endian.

This variant is available when host uses the desired endian and constexpr byte swap is not supported for the type.

◆ hostswap() [3/4]

template<typename T , byte_order_enum endian>
constexpr std::enable_if<details::is_constexpr_swappable_v<T> && (host_byte_order() != endian), T>::type pabigot::byteorder::hostswap ( const T &  t)
constexpr

constexpr-selected byte swap between host and endian.

This variant is available when host does not use the desired endian and constexpr byte swap is supported for the type.

◆ hostswap() [4/4]

template<typename T , byte_order_enum endian>
std::enable_if<!details::is_constexpr_swappable_v<T> && (host_byte_order() != endian), T>::type pabigot::byteorder::hostswap ( const T &  t)

constexpr-selected byte swap between host and endian.

This variant is available when host does not use the desired endian and constexpr byte swap is not supported for the type.