libcosmos
Linux C++ System Programming Library
All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cosmos::net::EndianNumber< T, endianness > Class Template Reference

An endianness aware unsigned integer. More...

#include <byte_order.hxx>

+ Inheritance diagram for cosmos::net::EndianNumber< T, endianness >:

Public Types

using RawInt = typename RawIntTraits<T, endianness>::Int
 

Public Member Functions

constexpr EndianNumber ()
 Construct a zero-initialized integer.
 
constexpr EndianNumber (const no_init_t)
 Leave member undefined e.g. for use with placement new or for performance reasons.
 
constexpr EndianNumber (const RawInt rint)
 Construct the number from a raw integer in the correct byte order.
 
constexpr EndianNumber (const T egg)
 Constructs the number from a native integer that will possibly be converted into the correct byte order.
 
constexpr EndianNumber (const EndianNumber< T, EndianTraits< endianness >::other > other)
 Constructs the number from an EndianNumber of differing Endian type.
 
void setFromHost (const T egg)
 
constexpr T toHost () const
 
 operator T () const
 
RawInt raw () const
 

Static Protected Member Functions

static constexpr T toTargetEndianness (const T egg)
 

Protected Attributes

m_egg
 

Detailed Description

template<typename T, Endian endianness>
class cosmos::net::EndianNumber< T, endianness >

An endianness aware unsigned integer.

This type stores an unsigned primitive integer in the given byte order endianness. The interface takes host byte order on input and returns host byte order on output. Only the raw() function returns the unmodified value, possibly in a foreign byte order, as a strong enum type obtained from RawIntTraits.

Definition at line 159 of file byte_order.hxx.

Member Typedef Documentation

◆ RawInt

template<typename T , Endian endianness>
using cosmos::net::EndianNumber< T, endianness >::RawInt = typename RawIntTraits<T, endianness>::Int

Definition at line 162 of file byte_order.hxx.

Constructor & Destructor Documentation

◆ EndianNumber() [1/5]

template<typename T , Endian endianness>
cosmos::net::EndianNumber< T, endianness >::EndianNumber ( )
inlineconstexpr

Construct a zero-initialized integer.

Definition at line 167 of file byte_order.hxx.

167: m_egg{} {}

◆ EndianNumber() [2/5]

template<typename T , Endian endianness>
cosmos::net::EndianNumber< T, endianness >::EndianNumber ( const no_init_t )
inlineconstexpr

Leave member undefined e.g. for use with placement new or for performance reasons.

Definition at line 170 of file byte_order.hxx.

170{}

◆ EndianNumber() [3/5]

template<typename T , Endian endianness>
cosmos::net::EndianNumber< T, endianness >::EndianNumber ( const RawInt rint)
inlineconstexpr

Construct the number from a raw integer in the correct byte order.

Definition at line 173 of file byte_order.hxx.

173 :
174 m_egg{to_integral(rint)} {
175 }

◆ EndianNumber() [4/5]

template<typename T , Endian endianness>
cosmos::net::EndianNumber< T, endianness >::EndianNumber ( const T egg)
inlineconstexpr

Constructs the number from a native integer that will possibly be converted into the correct byte order.

Definition at line 178 of file byte_order.hxx.

178 :
179 m_egg{toTargetEndianness(egg)} {
180 }

◆ EndianNumber() [5/5]

template<typename T , Endian endianness>
cosmos::net::EndianNumber< T, endianness >::EndianNumber ( const EndianNumber< T, EndianTraits< endianness >::other > other)
inlineconstexpr

Constructs the number from an EndianNumber of differing Endian type.

Definition at line 183 of file byte_order.hxx.

183 :
184 m_egg{toTargetEndianness(other.toHost())} {
185 }

Member Function Documentation

◆ operator T()

template<typename T , Endian endianness>
cosmos::net::EndianNumber< T, endianness >::operator T ( ) const
inline

Definition at line 199 of file byte_order.hxx.

199 {
200 return toHost();
201 }

◆ raw()

template<typename T , Endian endianness>
RawInt cosmos::net::EndianNumber< T, endianness >::raw ( ) const
inline

Definition at line 203 of file byte_order.hxx.

203 {
204 return RawInt{m_egg};
205 }

◆ setFromHost()

template<typename T , Endian endianness>
void cosmos::net::EndianNumber< T, endianness >::setFromHost ( const T egg)
inline

Definition at line 187 of file byte_order.hxx.

187 {
188 m_egg = toTargetEndianness(egg);
189 }

◆ toHost()

template<typename T , Endian endianness>
T cosmos::net::EndianNumber< T, endianness >::toHost ( ) const
inlineconstexpr

Definition at line 191 of file byte_order.hxx.

191 {
192 if (our_endian == endianness) {
193 return m_egg;
194 } else {
195 return swap_byte_order(m_egg);
196 }
197 };
uint16_t swap_byte_order(uint16_t value)
Return the reversed byte order for the given 16 bit value.

◆ toTargetEndianness()

template<typename T , Endian endianness>
static constexpr T cosmos::net::EndianNumber< T, endianness >::toTargetEndianness ( const T egg)
inlinestaticconstexprprotected

Definition at line 209 of file byte_order.hxx.

209 {
210 if (our_endian == endianness) {
211 return egg;
212 } else {
213 return swap_byte_order(egg);
214 }
215 }

Member Data Documentation

◆ m_egg

template<typename T , Endian endianness>
T cosmos::net::EndianNumber< T, endianness >::m_egg
protected

Definition at line 219 of file byte_order.hxx.


The documentation for this class was generated from the following file: