BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bsls_byteorderutil.h
Go to the documentation of this file.
1/// @file bsls_byteorderutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_byteorderutil.h -*-C++-*-
8#ifndef INCLUDED_BSLS_BYTEORDERUTIL
9#define INCLUDED_BSLS_BYTEORDERUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsls_byteorderutil bsls_byteorderutil
15/// @brief Provide byte-order swapping functions.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsls
19/// @{
20/// @addtogroup bsls_byteorderutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsls_byteorderutil-purpose"> Purpose</a>
25/// * <a href="#bsls_byteorderutil-classes"> Classes </a>
26/// * <a href="#bsls_byteorderutil-description"> Description </a>
27/// * <a href="#bsls_byteorderutil-usage"> Usage </a>
28///
29/// # Purpose {#bsls_byteorderutil-purpose}
30/// Provide byte-order swapping functions.
31///
32/// # Classes {#bsls_byteorderutil-classes}
33///
34/// - bsls::ByteOrderUtil: namespace for byte-order swapping functions
35///
36/// @see bsls_byteorder
37///
38/// # Description {#bsls_byteorderutil-description}
39/// This component provides a utility `class`,
40/// `bsls::ByteOrderUtil`, that contains a suite of static functions for
41/// reversing the byte order of integral types. The functions
42/// `swapByteOrder{16,32,64}` reverse the byte order of words having the
43/// indicated widths (in bits), while the overloaded function `swapBytes` will
44/// swap the bytes of any integral type passed to it, returning the same type it
45/// is passed.
46///
47/// ## Usage {#bsls_byteorderutil-usage}
48///
49///
50/// In this example we demonstrate the use of different overloads of the
51/// `swapBytes` function.
52///
53/// First we `typedef` a shorthand to the namespace `class`:
54/// @code
55/// typedef bsls::ByteOrderUtil Util;
56/// @endcode
57/// Then, we demonstrate reversing the bytes of an `unsigned short`:
58/// @code
59/// unsigned short us = 0x1234;
60/// assert(0x3412 == Util::swapBytes(us));
61/// @endcode
62/// Next, we do a signed 'short:
63/// @code
64/// short ss = 0x4321;
65/// assert(0x2143 == Util::swapBytes(ss));
66/// @endcode
67/// Then, we reverse an `unsigned int`:
68/// @code
69/// unsigned int ui = 0x01020304;
70/// assert(0x04030201 == Util::swapBytes(ui));
71/// @endcode
72/// Next, we reverse the bytes of a 32-bit signed integer:
73/// @code
74/// int si = 0x11223344;
75/// assert(0x44332211 == Util::swapBytes(si));
76/// @endcode
77/// Now, we perform the transform on a 64-bit unsigned:
78/// @code
79/// bsls::Types::Uint64 ui64 = 0x0102030405060708ULL;
80/// assert(0x0807060504030201ULL == Util::swapBytes(ui64));
81/// @endcode
82/// Finally, we do a 64-bit signed integer:
83/// @code
84/// bsls::Types::Int64 i64 = 0x0a0b0c0d0e0f0102LL;
85/// assert(0x02010f0e0d0c0b0aLL == Util::swapBytes(i64));
86/// @endcode
87/// @}
88/** @} */
89/** @} */
90
91/** @addtogroup bsl
92 * @{
93 */
94/** @addtogroup bsls
95 * @{
96 */
97/** @addtogroup bsls_byteorderutil
98 * @{
99 */
100
101#include <bsls_byteorderutil_impl.h>
102#include <bsls_platform.h>
103#include <bsls_types.h>
104
105
106namespace bsls {
107
108 // ====================
109 // struct ByteOrderUtil
110 // ====================
111
112/// This `class` provides a namespace for functions used for reversing the
113/// byte order of values having integral type.
114///
115/// See @ref bsls_byteorderutil
117
118 // CLASS METHODS
119 static bool swapBytes(bool x);
120 static char swapBytes(char x);
121 static unsigned char swapBytes(unsigned char x);
122 static signed char swapBytes(signed char x);
123#ifdef BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY
124 static char8_t swapBytes(char8_t x);
125#endif
126 static wchar_t swapBytes(wchar_t x);
127 static short swapBytes(short x);
128 static unsigned short swapBytes(unsigned short x);
129#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
130 static char16_t swapBytes(char16_t x);
131 static char32_t swapBytes(char32_t x);
132#endif
133 /// Return the value that results from reversing the order of the bytes
134 /// in the specified `x`.
135 static int swapBytes(int x);
136 static unsigned int swapBytes(unsigned int x);
137 static long swapBytes(long x);
138 static unsigned long swapBytes(unsigned long x);
141
142 /// Return the value that results from reversing the order of the bytes
143 /// in the specified `x`.
144 static unsigned short swapBytes16(unsigned short x);
145
146 /// Return the value that results from reversing the order of the bytes
147 /// in the specified `x`.
148 static unsigned int swapBytes32(unsigned int x);
149
150 /// Return the value that results from reversing the order of the bytes
151 /// in the specified `x`.
153};
154
155// ============================================================================
156// INLINE FUNCTION DEFINITIONS
157// ============================================================================
158
159 // --------------------
160 // struct ByteOrderUtil
161 // --------------------
162
163// CLASS METHODS
164inline
165bool
167{
168 return x;
169}
170
171inline
172char
174{
175 return x;
176}
177
178inline
179unsigned char
181{
182 return x;
183}
184
185inline
186signed char
188{
189 return x;
190}
191
192#ifdef BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY
193inline
194char8_t
196{
197 return x;
198}
199#endif
200
201inline
202wchar_t
204{
205 // Size of 'wchar_t' varies depending on platform and compiler switches.
206 // We could not find any compiler-defined macros that would reliably
207 // indicate the size, so use 'ByteOrderUtil_Impl' to adjust automatically.
208
210}
211
212inline
213short
215{
216 // These macros all return a value of type 'short'.
217
218#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16)
219 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16( short, x);
220#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16)
221 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16(short, &x);
222#else
224#endif
225}
226
227inline
228unsigned short
230{
231 // These macros all return a value of type 'unsigned short'.
232
233#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16)
234 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16( unsigned short, x);
235#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16)
236 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16(unsigned short, &x);
237#else
239#endif
240}
241
242#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
243inline
244char16_t
245ByteOrderUtil::swapBytes(char16_t x)
246{
247 // These macros all return a value of type 'unsigned short'.
248
249#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16)
250 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16( char16_t, x);
251#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16)
252 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16(char16_t, &x);
253#else
255#endif
256}
257
258inline
259char32_t
260ByteOrderUtil::swapBytes(char32_t x)
261{
262 // These macros all return a value of type 'int'.
263
264#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32)
265 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32( char32_t, x);
266#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32)
267 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32(char32_t, &x);
268#else
270#endif
271}
272#endif
273
274inline
275int
277{
278 // These macros all return a value of type 'int'.
279
280#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32)
281 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32( int, x);
282#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32)
283 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32(int, &x);
284#else
286#endif
287}
288
289inline
290unsigned int
292{
293 // These macros all return a value of type 'unsigned int'.
294
295#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32)
296 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32( unsigned int, x);
297#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32)
298 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32(unsigned int, &x);
299#else
301#endif
302}
303
304inline
305long
307{
308 // Size of 'long' varies depending on platform and compiler switches. We
309 // could not find any compiler-defined macros that would reliably indicate
310 // the size, so use 'ByteOrderUtil_Impl' to adjust automatically.
311
313}
314
315inline
316unsigned long
318{
319 // Size of 'unsigned long' varies depending on platform and compiler
320 // switches. We could not find any compiler-defined macros that would
321 // reliably indicate the size, so use 'ByteOrderUtil_Impl' to adjust
322 // automatically.
323
325}
326
327inline
330{
331 // These macros all return a value of type 'bsls::Types::Uint64'.
332
333#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64)
334 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64( bsls::Types::Uint64, x);
335#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64)
336# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
337# pragma GCC diagnostic push
338# pragma GCC diagnostic ignored "-Wstrict-aliasing"
339# endif
340
341 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64(bsls::Types::Uint64, &x);
342
343# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
344# pragma GCC diagnostic pop
345# endif
346#else
348#endif
349}
350
351inline
354{
355 // These macros all return a value of type 'bsls::Types::Int64'.
356
357#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64)
358 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64( bsls::Types::Int64, x);
359#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64)
360# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
361# pragma GCC diagnostic push
362# pragma GCC diagnostic ignored "-Wstrict-aliasing"
363# endif
364
365 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64(bsls::Types::Int64, &x);
366
367# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
368# pragma GCC diagnostic pop
369# endif
370#else
372#endif
373}
374
375inline
376unsigned short
378{
379 // These macros all return a value of type 'unsigned short'.
380
381#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16)
382 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16( unsigned short, x);
383#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16)
384 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16(unsigned short, &x);
385#else
387#endif
388}
389
390inline
391unsigned int
393{
394 // These macros all return a value of type 'unsigned int'.
395
396#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32)
397 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32( unsigned int, x);
398#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32)
399 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32(unsigned int, &x);
400#else
402#endif
403}
404
405inline
408{
409 // These macros all return a value of type 'bsls::Types::Uint64'.
410
411#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64)
412 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64( bsls::Types::Uint64, x);
413#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64)
414# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
415# pragma GCC diagnostic push
416# pragma GCC diagnostic ignored "-Wstrict-aliasing"
417# endif
418
419 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64(bsls::Types::Uint64, &x);
420
421# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
422# pragma GCC diagnostic pop
423# endif
424#else
426#endif
427}
428
429} // close package namespace
430
431
432#endif
433
434// ----------------------------------------------------------------------------
435// Copyright 2014 Bloomberg Finance L.P.
436//
437// Licensed under the Apache License, Version 2.0 (the "License");
438// you may not use this file except in compliance with the License.
439// You may obtain a copy of the License at
440//
441// http://www.apache.org/licenses/LICENSE-2.0
442//
443// Unless required by applicable law or agreed to in writing, software
444// distributed under the License is distributed on an "AS IS" BASIS,
445// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
446// See the License for the specific language governing permissions and
447// limitations under the License.
448// ----------------------------- END-OF-FILE ----------------------------------
449
450/** @} */
451/** @} */
452/** @} */
#define BSLS_BYTEORDERUTIL_IMPL_GENERICSWAP_32(dstType, x)
Definition bsls_byteorderutil_impl.h:537
#define BSLS_BYTEORDERUTIL_IMPL_GENERICSWAP_16(dstType, x)
Definition bsls_byteorderutil_impl.h:528
#define BSLS_BYTEORDERUTIL_IMPL_GENERICSWAP_64(dstType, x)
Definition bsls_byteorderutil_impl.h:548
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlt_iso8601util.h:707
Definition bsls_byteorderutil_impl.h:91
Definition bsls_byteorderutil.h:116
static Types::Uint64 swapBytes64(Types::Uint64 x)
Definition bsls_byteorderutil.h:407
static unsigned int swapBytes32(unsigned int x)
Definition bsls_byteorderutil.h:392
static bool swapBytes(bool x)
Definition bsls_byteorderutil.h:166
static unsigned short swapBytes16(unsigned short x)
Definition bsls_byteorderutil.h:377
unsigned long long Uint64
Definition bsls_types.h:139
long long Int64
Definition bsls_types.h:134