BDE 4.14.0 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.
115
116 // CLASS METHODS
117 static bool swapBytes(bool x);
118 static char swapBytes(char x);
119 static unsigned char swapBytes(unsigned char x);
120 static signed char swapBytes(signed char x);
121#ifdef BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY
122 static char8_t swapBytes(char8_t x);
123#endif
124 static wchar_t swapBytes(wchar_t x);
125 static short swapBytes(short x);
126 static unsigned short swapBytes(unsigned short x);
127#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
128 static char16_t swapBytes(char16_t x);
129 static char32_t swapBytes(char32_t x);
130#endif
131 static int swapBytes(int x);
132 static unsigned int swapBytes(unsigned int x);
133 static long swapBytes(long x);
134 /// Return the value that results from reversing the order of the bytes
135 /// in the specified `x`.
136 static unsigned long swapBytes(unsigned long x);
139
140 /// Return the value that results from reversing the order of the bytes
141 /// in the specified `x`.
142 static unsigned short swapBytes16(unsigned short x);
143
144 /// Return the value that results from reversing the order of the bytes
145 /// in the specified `x`.
146 static unsigned int swapBytes32(unsigned int x);
147
148 /// Return the value that results from reversing the order of the bytes
149 /// in the specified `x`.
151};
152
153// ============================================================================
154// INLINE FUNCTION DEFINITIONS
155// ============================================================================
156
157 // --------------------
158 // struct ByteOrderUtil
159 // --------------------
160
161// CLASS METHODS
162inline
163bool
165{
166 return x;
167}
168
169inline
170char
172{
173 return x;
174}
175
176inline
177unsigned char
179{
180 return x;
181}
182
183inline
184signed char
186{
187 return x;
188}
189
190#ifdef BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY
191inline
192char8_t
194{
195 return x;
196}
197#endif
198
199inline
200wchar_t
202{
203 // Size of 'wchar_t' varies depending on platform and compiler switches.
204 // We could not find any compiler-defined macros that would reliably
205 // indicate the size, so use 'ByteOrderUtil_Impl' to adjust automatically.
206
208}
209
210inline
211short
213{
214 // These macros all return a value of type 'short'.
215
216#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16)
217 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16( short, x);
218#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16)
219 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16(short, &x);
220#else
222#endif
223}
224
225inline
226unsigned short
228{
229 // These macros all return a value of type 'unsigned short'.
230
231#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16)
232 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16( unsigned short, x);
233#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16)
234 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16(unsigned short, &x);
235#else
237#endif
238}
239
240#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
241inline
242char16_t
244{
245 // These macros all return a value of type 'unsigned short'.
246
247#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16)
248 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16( char16_t, x);
249#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16)
250 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16(char16_t, &x);
251#else
253#endif
254}
255
256inline
257char32_t
259{
260 // These macros all return a value of type 'int'.
261
262#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32)
263 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32( char32_t, x);
264#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32)
265 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32(char32_t, &x);
266#else
268#endif
269}
270#endif
271
272inline
273int
275{
276 // These macros all return a value of type 'int'.
277
278#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32)
279 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32( int, x);
280#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32)
281 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32(int, &x);
282#else
284#endif
285}
286
287inline
288unsigned int
290{
291 // These macros all return a value of type 'unsigned int'.
292
293#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32)
294 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32( unsigned int, x);
295#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32)
296 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32(unsigned int, &x);
297#else
299#endif
300}
301
302inline
303long
305{
306 // Size of 'long' varies depending on platform and compiler switches. We
307 // could not find any compiler-defined macros that would reliably indicate
308 // the size, so use 'ByteOrderUtil_Impl' to adjust automatically.
309
311}
312
313inline
314unsigned long
316{
317 // Size of 'unsigned long' varies depending on platform and compiler
318 // switches. We could not find any compiler-defined macros that would
319 // reliably indicate the size, so use 'ByteOrderUtil_Impl' to adjust
320 // automatically.
321
323}
324
325inline
328{
329 // These macros all return a value of type 'bsls::Types::Uint64'.
330
331#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64)
332 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64( bsls::Types::Uint64, x);
333#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64)
334# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
335# pragma GCC diagnostic push
336# pragma GCC diagnostic ignored "-Wstrict-aliasing"
337# endif
338
339 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64(bsls::Types::Uint64, &x);
340
341# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
342# pragma GCC diagnostic pop
343# endif
344#else
346#endif
347}
348
349inline
352{
353 // These macros all return a value of type 'bsls::Types::Int64'.
354
355#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64)
356 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64( bsls::Types::Int64, x);
357#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64)
358# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
359# pragma GCC diagnostic push
360# pragma GCC diagnostic ignored "-Wstrict-aliasing"
361# endif
362
363 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64(bsls::Types::Int64, &x);
364
365# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
366# pragma GCC diagnostic pop
367# endif
368#else
370#endif
371}
372
373inline
374unsigned short
376{
377 // These macros all return a value of type 'unsigned short'.
378
379#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16)
380 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_16( unsigned short, x);
381#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16)
382 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P16(unsigned short, &x);
383#else
385#endif
386}
387
388inline
389unsigned int
391{
392 // These macros all return a value of type 'unsigned int'.
393
394#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32)
395 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_32( unsigned int, x);
396#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32)
397 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P32(unsigned int, &x);
398#else
400#endif
401}
402
403inline
406{
407 // These macros all return a value of type 'bsls::Types::Uint64'.
408
409#if defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64)
410 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_64( bsls::Types::Uint64, x);
411#elif defined(BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64)
412# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
413# pragma GCC diagnostic push
414# pragma GCC diagnostic ignored "-Wstrict-aliasing"
415# endif
416
417 BSLS_BYTEORDERUTIL_IMPL_CUSTOMSWAP_P64(bsls::Types::Uint64, &x);
418
419# ifdef BSLS_PLATFORM_HAS_PRAGMA_GCC_DIAGNOSTIC
420# pragma GCC diagnostic pop
421# endif
422#else
424#endif
425}
426
427} // close package namespace
428
429
430#endif
431
432// ----------------------------------------------------------------------------
433// Copyright 2014 Bloomberg Finance L.P.
434//
435// Licensed under the Apache License, Version 2.0 (the "License");
436// you may not use this file except in compliance with the License.
437// You may obtain a copy of the License at
438//
439// http://www.apache.org/licenses/LICENSE-2.0
440//
441// Unless required by applicable law or agreed to in writing, software
442// distributed under the License is distributed on an "AS IS" BASIS,
443// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
444// See the License for the specific language governing permissions and
445// limitations under the License.
446// ----------------------------- END-OF-FILE ----------------------------------
447
448/** @} */
449/** @} */
450/** @} */
#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)
Definition bsls_ident.h:195
Definition bdlt_iso8601util.h:691
Definition bsls_byteorderutil_impl.h:91
Definition bsls_byteorderutil.h:114
static Types::Uint64 swapBytes64(Types::Uint64 x)
Definition bsls_byteorderutil.h:405
static unsigned int swapBytes32(unsigned int x)
Definition bsls_byteorderutil.h:390
static bool swapBytes(bool x)
Definition bsls_byteorderutil.h:164
static unsigned short swapBytes16(unsigned short x)
Definition bsls_byteorderutil.h:375
unsigned long long Uint64
Definition bsls_types.h:137
long long Int64
Definition bsls_types.h:132