roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
byte_order.h
Go to the documentation of this file.
1#pragma once
2
3// Support for endian-ness conversions between platform-specific and
4// platform-independent representations that can be used as template
5// parameters.
6
7// Note: a decent compiler will completely remove 'no-op' conversions,
8// and inline swaps using native builtins.
9
10#include <inttypes.h>
12
13namespace roo_io {
14
18
19#if defined(__BIG_ENDIAN__)
21#elif defined(__LITTLE_ENDIAN__)
23#endif
24};
25
26} // namespace roo_io
27
28// Determine the native byte order if possible.
29
30
31
32namespace roo_io {
33namespace byte_order {
34
35// Templated operator for swapping the byte order of any unsigned integer type.
36template <typename type>
37struct Swapper;
38
39template <>
41 constexpr uint8_t operator()(uint8_t in) const { return in; }
42};
43
44template <>
46 constexpr uint16_t operator()(uint16_t in) const {
47 return bswap16(in);
48 }
49};
50
51template <>
53 constexpr uint32_t operator()(uint32_t in) const {
54 return bswap32(in);
55 }
56};
57
58template <>
60 constexpr uint64_t operator()(uint64_t in) const {
61 return bswap64(in);
62 }
63};
64
65template <typename storage_type>
69
70// Operator for converting the byte order from src to dst. The default
71// implementation, that assumes that src != dst, and thus swapping is needed.
72template <typename storage_type, ByteOrder src, ByteOrder dst>
73struct Converter {
75 return Swap(in);
76 }
77};
78
79// No swapping if src == dst.
80template <typename storage_type, ByteOrder byte_order>
81struct Converter<storage_type, byte_order, byte_order> {
82 constexpr storage_type operator()(storage_type in) const { return in; }
83};
84
85// Utility to convert an integer type from the src to dst byte order.
86// If src == dst, compiles to no-op; otherwise, it compiles to
87// a built-in byte swap routine.
88template <typename storage_type, ByteOrder src, ByteOrder dst>
92
93} // namespace byte_order
94
95// A convenience function that swaps the bytes in the integer type.
96template <typename storage_type>
98 return byte_order::Swap(in);
99}
100
101// A convenience function to convert an integer type from
102// host-native byte order to the specified byte order.
103template <typename storage_type, ByteOrder dst>
105 return byte_order::Convert<storage_type, kNativeEndian, dst>(in);
106}
107
108// A convenience function to convert an integer type from
109// a specified byte order to the host-native byte order.
110template <typename storage_type, ByteOrder src>
112 return byte_order::Convert<storage_type, src, kNativeEndian>(in);
113}
114
115// A convenience function to convert an integer type from
116// host-native byte order to big endian.
117template <typename storage_type>
119 return byte_order::Convert<storage_type, kNativeEndian, kBigEndian>(in);
120}
121
122// A convenience function to convert an integer type from
123// host-native byte order to little endian.
124template <typename storage_type>
126 return byte_order::Convert<storage_type, kNativeEndian, kLittleEndian>(in);
127}
128
129// A convenience function to convert an integer type from
130// big endian to the host-native byte order.
131template <typename storage_type>
133 return byte_order::Convert<storage_type, kBigEndian, kNativeEndian>(in);
134}
135
136// A convenience function to convert an integer type from
137// little endian to the host-native byte order.
138template <typename storage_type>
140 return byte_order::Convert<storage_type, kLittleEndian, kNativeEndian>(in);
141}
142
143} // namespace roo_io
Convert Endianness of shorts, longs, long longs, regardless of architecture/OS.
static uint32_t bswap32(uint32_t x)
Definition endianness.h:95
static uint64_t bswap64(uint64_t x)
Definition endianness.h:101
static uint16_t bswap16(uint16_t x)
Definition endianness.h:92
constexpr storage_type Convert(storage_type in)
Definition byte_order.h:89
constexpr storage_type Swap(storage_type in)
Definition byte_order.h:66
Definition byte.h:6
constexpr storage_type bswap(storage_type in)
Definition byte_order.h:97
constexpr storage_type htole(storage_type in)
Definition byte_order.h:125
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8
constexpr storage_type toh(storage_type in)
Definition byte_order.h:111
constexpr storage_type hto(storage_type in)
Definition byte_order.h:104
constexpr storage_type htobe(storage_type in)
Definition byte_order.h:118
constexpr storage_type betoh(storage_type in)
Definition byte_order.h:132
@ kLittleEndian
Definition byte_order.h:16
@ kBigEndian
Definition byte_order.h:17
constexpr storage_type letoh(storage_type in)
Definition byte_order.h:139
constexpr storage_type operator()(storage_type in) const
Definition byte_order.h:82
constexpr storage_type operator()(storage_type in) const
Definition byte_order.h:74
constexpr uint16_t operator()(uint16_t in) const
Definition byte_order.h:46
constexpr uint32_t operator()(uint32_t in) const
Definition byte_order.h:53
constexpr uint64_t operator()(uint64_t in) const
Definition byte_order.h:60
constexpr uint8_t operator()(uint8_t in) const
Definition byte_order.h:41