pabigot  0.1.1
C++ support classes
Data Structures | Namespaces | Enumerations | Functions | Variables
byteorder.hpp File Reference

Support for byte order manipulation and packed storage. More...

#include <algorithm>
#include <cinttypes>
#include <climits>
#include <cstring>
#include <type_traits>
#include <pabigot/common.hpp>
#include <pabigot/external/ccbysa30.hpp>

Go to the source code of this file.

Data Structures

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

Namespaces

 pabigot
 Root for all pabigot namespaces.
 
 pabigot::byteorder
 Functionality related to byte order and endianness.
 

Enumerations

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

Functions

constexpr byte_order_enum pabigot::byteorder::host_byte_order ()
 Return the byte_order_enum value for the host. More...
 
template<class T , class U = typename std::make_unsigned<T>::type>
constexpr U pabigot::byteorder::details::byteswap (T v)
 Return a byte-swapped representation of an integral value. More...
 
template<typename T >
constexpr std::enable_if< details::is_constexpr_swappable_v< T >, T >::type pabigot::byteorder::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 pabigot::byteorder::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 pabigot::byteorder::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 pabigot::byteorder::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 pabigot::byteorder::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 pabigot::byteorder::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 pabigot::byteorder::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 pabigot::byteorder::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 pabigot::byteorder::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 pabigot::byteorder::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 pabigot::byteorder::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 pabigot::byteorder::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 pabigot::byteorder::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 pabigot::byteorder::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 pabigot::byteorder::host_x_network (const T &v)
 Convert between host and network byte order.
 

Variables

static constexpr wchar_t pabigot::byteorder::BOM = u'\uFFFE'
 The Unicode byte order marker.
 
template<class T >
constexpr bool pabigot::byteorder::details::is_constexpr_swappable_v {std::is_integral<T>::value}
 Category identifying types that can be constexpr swapped. More...
 
template<class T >
constexpr bool pabigot::byteorder::details::is_alias_swappable_v
 Category identifying types that can swapped by alias with a byte array. More...
 
template<class T >
constexpr bool pabigot::byteorder::details::is_other_swappable_v
 Category identifying types that can be swapped by using std::reverse on a copy. More...
 

Detailed Description

Support for byte order manipulation and packed storage.

Function Documentation

◆ byteswap()

template<class T , class U = typename std::make_unsigned<T>::type>
constexpr U pabigot::byteorder::details::byteswap ( v)
constexpr

Return a byte-swapped representation of an integral value.

Template Parameters
Tany type that can be converted to an unsigned representation.
Parameters
vthe host representation of a value of type T.
Returns
The byte-swapped unsigned value of v.

Variable Documentation

◆ is_alias_swappable_v

template<class T >
constexpr bool pabigot::byteorder::details::is_alias_swappable_v
constexpr
Initial value:
{std::is_scalar_v<T>
&& !is_constexpr_swappable_v<T>}

Category identifying types that can swapped by alias with a byte array.

This is the fallback for things that fail is_constexpr_swappable_v(). The process used is to alias a copy of the value with a uint8_t array, byte-swap the array, then read the value out of the inactive tag.

Note
Technically the algorithm underlying this supports reversing any contiguous area of memory. However, byte-reversing arbitrary structures makes no sense, so this solution is limited to scalar values. Containers of octet data, for which reversal does make sense, are handled by a different solution.

◆ is_constexpr_swappable_v

template<class T >
constexpr bool pabigot::byteorder::details::is_constexpr_swappable_v {std::is_integral<T>::value}
constexpr

Category identifying types that can be constexpr swapped.

This is a variable template that identifies types for which byte swapping can be performed with a constant expression.

Specifically, these are the types that can be converted through static_cast to and from the unsigned integer type of the same size without changing the value.

◆ is_other_swappable_v

template<class T >
constexpr bool pabigot::byteorder::details::is_other_swappable_v
constexpr
Initial value:
{!is_constexpr_swappable_v<T>
&& !is_alias_swappable_v<T>}

Category identifying types that can be swapped by using std::reverse on a copy.

These are container classes with a bi-directional iterator and octet-sized values.