BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsls_platformutil.h
Go to the documentation of this file.
1/// @file bsls_platformutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsls_platformutil.h -*-C++-*-
8#ifndef INCLUDED_BSLS_PLATFORMUTIL
9#define INCLUDED_BSLS_PLATFORMUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsls_platformutil bsls_platformutil
15/// @brief Provide consistent interface for platform-dependent functionality.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsls
19/// @{
20/// @addtogroup bsls_platformutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsls_platformutil-purpose"> Purpose</a>
25/// * <a href="#bsls_platformutil-classes"> Classes </a>
26/// * <a href="#bsls_platformutil-macros"> Macros </a>
27/// * <a href="#bsls_platformutil-description"> Description </a>
28/// * <a href="#bsls_platformutil-usage"> Usage </a>
29/// * <a href="#bsls_platformutil-types"> Types </a>
30/// * <a href="#bsls_platformutil-functions-and-macros"> Functions and Macros </a>
31///
32/// # Purpose {#bsls_platformutil-purpose}
33/// Provide consistent interface for platform-dependent functionality.
34///
35/// @deprecated Use @ref bsls_alignmentutil , @ref bsls_byteorder , @ref bsls_platform , and
36/// @ref bsls_types instead.
37///
38/// # Classes {#bsls_platformutil-classes}
39///
40/// - bsls::PlatformUtil: namespace for platform-neutral type names and API
41///
42/// # Macros {#bsls_platformutil-macros}
43///
44/// - BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN: flag set on little-endian platforms
45/// - BSLS_PLATFORMUTIL_IS_BIG_ENDIAN: flag set on big-endian platforms
46/// - BSLS_PLATFORMUTIL_HTONS(x): convert 16-bit from host to network byte order
47/// - BSLS_PLATFORMUTIL_HTONL(x): convert 32-bit from host to network byte order
48/// - BSLS_PLATFORMUTIL_NTOHS(x): convert 16-bit from network to host byte order
49/// - BSLS_PLATFORMUTIL_NTOHL(x): convert 32-bit from network to host byte order
50///
51/// @see bsls_platform, bsls_types
52///
53/// # Description {#bsls_platformutil-description}
54/// This component provides a namespace for a set of `typedef`s and
55/// functions that provide a stable, portable interface to platform-dependent
56/// functionality. In particular, this component supplies portable typenames
57/// for signed and unsigned 64-bit integers, and provides compile-time
58/// preprocessor macros that characterize the "endian-ness" of the underlying
59/// processor on the current platform. Runtime functionality to return the
60/// "endian-ness", and to round up to a multiple of the maximum required
61/// alignment for the processor, are also provided.
62///
63/// ## Usage {#bsls_platformutil-usage}
64///
65///
66/// The following illustrates how some of the types and functions supplied by
67/// this component might be used.
68///
69/// ### Types {#bsls_platformutil-types}
70///
71///
72/// `bsls::PlatformUtil::Int64` and `bsls::PlatformUtil::Uint64` identify the
73/// preferred fundamental types denoting signed and unsigned 64-bit integers,
74/// respectively:
75/// @code
76/// bsls::PlatformUtil::Uint64 stimulus = 787000000000ULL;
77/// @endcode
78/// Clients can use these types in the same way as an `int`. Clients can also
79/// mix usage with other fundamental integral types:
80/// @code
81/// bsls::PlatformUtil::Uint64 nationalDebt = 1000000000000ULL;
82/// nationalDebt += stimulus;
83///
84/// unsigned int deficitReduction = 1000000000;
85/// nationalDebt -= deficitReduction;
86///
87/// std::cout << "National Debt Level: " << nationalDebt << std::endl;
88/// @endcode
89/// `bsls::PlatformUtil::size_type` identifies the preferred integral type
90/// denoting the number of elements in a container, and the number of bytes in a
91/// single block of memory supplied by an allocator. For example, a typical use
92/// is as a `typedef` in an STL container:
93/// @code
94/// class vector {
95///
96/// // ...
97///
98/// public:
99/// typedef bsls::PlatformUtil::size_type size_type;
100///
101/// // ...
102/// };
103/// @endcode
104///
105/// ### Functions and Macros {#bsls_platformutil-functions-and-macros}
106///
107///
108/// The functions:
109/// @code
110/// bool bsls::PlatformUtil::isLittleEndian();
111/// bool bsls::PlatformUtil::isBigEndian();
112/// @endcode
113/// encapsulate the capability of determining whether a machine is big- or
114/// little-endian across all supported platforms. In addition, certain
115/// compile-time constants are also provided as preprocessor macros to
116/// facilitate conditional compilation:
117/// @code
118/// BSLS_PLATFORMUTIL_IS_BIG_ENDIAN
119/// BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN
120/// @endcode
121/// These functions and macros are useful for writing platform-independent code,
122/// such as a function that converts the bytes in a `short` to network byte
123/// order (which is consistent with big-endian):
124/// @code
125/// short convertToNetworkByteOrder(short input)
126/// // Return the specified 'input' in network byte order.
127/// {
128/// #ifdef BSLS_PLATFORMUTIL_IS_BIG_ENDIAN
129/// return input;
130/// #else
131/// return static_cast<short>(
132/// ((input >> 8) & 0xFF) | ((input & 0xFF) << 8));
133/// #endif
134/// }
135/// @endcode
136/// Note that in the above usage example, either the macros or the functions can
137/// be used to test whether a platform is big- or little-endian.
138/// @}
139/** @} */
140/** @} */
141
142/** @addtogroup bsl
143 * @{
144 */
145/** @addtogroup bsls
146 * @{
147 */
148/** @addtogroup bsls_platformutil
149 * @{
150 */
151
152#ifdef BDE_OPENSOURCE_PUBLICATION // DEPRECATED
153#error "bsls_platformutil is deprecated"
154#endif
155#include <bsls_alignmentutil.h>
156#include <bsls_platform.h>
157#include <bsls_types.h>
158
159#include <cstddef>
160
161
162
163namespace bsls {
164
165 // ===================
166 // struct PlatformUtil
167 // ===================
168
169/// Provide a namespace for a suite of `typedef`s and pure procedures that
170/// encapsulate, platform-dependent types and APIs.
172
173 // TYPES
174
175 /// The alias `size_type` refers to the preferred type for denoting a
176 /// number of elements in either `bslma` allocators or container types.
177 /// Note that this type is signed, as negative values may make sense in
178 /// certain contexts. Also note that the standard-compliant allocators
179 /// (e.g., `bsl::allocator` and `std::allocator`) use an *unsigned*
180 /// size type, but that is fine because they also have a mechanism
181 /// (@ref max_size ) to determine overflows resulting from converting from
182 /// one size type to the other.
183 ///
184 /// @deprecated Use @ref Types::size_type instead.
186
188
189 /// The aliases `UintPtr` and `IntPtr` are guaranteed to have the same
190 /// size as pointers.
191 ///
192 /// @deprecated Use @ref Types::UintPtr` and `Types::IntPtr instead.
194
196
197 /// The aliases `Int64` and `Uint64` stand for whatever type `Types`
198 /// implements for the appropriate supported platforms.
199 ///
200 /// @deprecated Use @ref Types::Int64` and `Types::Uint64 instead.
202
203#ifndef BDE_OMIT_INTERNAL_DEPRECATED
204 /// The alias `MaxAlign` refers to a type that is maximally-aligned on
205 /// the current platform.
206 ///
207 /// @deprecated Use @ref AlignmentUtil::MaxAlignedType instead.
209#endif // BDE_OMIT_INTERNAL_DEPRECATED
210
211 // CLASS METHODS
212
213 /// Return `true` if this platform is "big-endian", and `false`
214 /// otherwise. Note that "big-endian" (i.e., the most significant byte
215 /// of data at the lowest byte address) is consistent with network byte
216 /// order.
217 ///
218 /// @deprecated Use preprocessor macro `BSLS_PLATFORM_IS_BIG_ENDIAN`
219 /// defined in @ref bsls_platform instead.
220 static bool isBigEndian();
221
222 /// Return `true` if this platform is "little-endian", and `false`
223 /// otherwise. Note that "little-endian" (i.e., the least significant
224 /// byte of data at the lowest byte address) is inconsistent with
225 /// network byte order.
226 ///
227 /// @deprecated Use preprocessor macro
228 /// `BSLS_PLATFORMUTIL_IS_BIG_ENDIAN` defined in @ref bsls_platform
229 /// instead.
230 static bool isLittleEndian();
231
232 /// Return the specified `size` (in bytes) rounded up to the smallest
233 /// integral multiple of the maximum alignment. The behavior is
234 /// undefined unless `0 <= size`.
235 ///
236 /// @deprecated Use @ref AlignmentUtil::roundUpToMaximalAlignment instead.
237 static int roundUpToMaximalAlignment(int size);
238};
239
240} // close package namespace
241
242// ============================================================================
243// COMPILE-TIME CONSTANT PRE-PROCESSOR MACROS
244// ============================================================================
245
246// The following preprocessor macros are **DEPRECATED**. Please use their
247// replacements in @ref bsls_byteorder and @ref bsls_platform instead.
248
249#if defined(BSLS_PLATFORM_CPU_X86_64)
250 /// @deprecated Use preprocessor macro `BSLS_PLATFORM_IS_LITTLE_ENDIAN`
251 /// defined in @ref bsls_platform instead.
252 #define BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN BSLS_PLATFORM_IS_LITTLE_ENDIAN
253#endif
254
255#if defined(BSLS_PLATFORM_CPU_X86)
256 /// @deprecated Use preprocessor macro `BSLS_PLATFORM_IS_LITTLE_ENDIAN`
257 /// defined in @ref bsls_platform instead.
258 #define BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN \
259 BSLS_PLATFORM_IS_LITTLE_ENDIAN
260#endif
261
262#if defined(BSLS_PLATFORM_CPU_ARM)
263 /// @deprecated Use preprocessor macro `BSLS_PLATFORM_IS_LITTLE_ENDIAN`
264 /// defined in @ref bsls_platform instead.
265 #define BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN \
266 BSLS_PLATFORM_IS_LITTLE_ENDIAN
267#endif
268
269#if !defined(BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN)
270 /// @deprecated Use preprocessor macro `BSLS_PLATFORM_IS_BIG_ENDIAN`
271 /// defined in @ref bsls_platform instead.
272 #define BSLS_PLATFORMUTIL_IS_BIG_ENDIAN BSLS_PLATFORM_IS_BIG_ENDIAN
273#endif
274
275#ifndef BDE_OMIT_INTERNAL_DEPRECATED
276
277#if defined(BSLS_PLATFORMUTIL_IS_BIG_ENDIAN)
278#define BSLS_PLATFORMUTIL_HTONL(x) (x)
279#define BSLS_PLATFORMUTIL_HTONS(x) (x)
280#define BSLS_PLATFORMUTIL_NTOHL(x) (x)
281#define BSLS_PLATFORMUTIL_NTOHS(x) (x)
282 /// @deprecated Use preprocessor macros `BSLS_BYTEORDER_*TO*` defined in
283 /// @ref bsls_byteorder instead.
284#else
285 /// Use built-in if using gcc 4.3.
286
287#if (defined(BSLS_PLATFORM_CMP_GNU) && BSLS_PLATFORM_CMP_VER_MAJOR >= 40300) \
288 || defined(BSLS_PLATFORM_CMP_CLANG)
289#define BSLS_PLATFORMUTIL_NTOHL(x) (unsigned) __builtin_bswap32(x)
290#else
291#define BSLS_PLATFORMUTIL_NTOHL(x) \
292 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
293 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
294#endif
295#define BSLS_PLATFORMUTIL_HTONL(x) BSLS_PLATFORMUTIL_NTOHL(x)
296#define BSLS_PLATFORMUTIL_NTOHS(x) \
297 ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
298#define BSLS_PLATFORMUTIL_HTONS(x) BSLS_PLATFORMUTIL_NTOHS(x)
299#endif
300
301#endif // BDE_OMIT_INTERNAL_DEPRECATED
302
303namespace bsls {
304
305// ============================================================================
306// INLINE FUNCTION DEFINITIONS
307// ============================================================================
308
309 // -------------------
310 // struct PlatformUtil
311 // -------------------
312
313// CLASS METHODS
314inline
316{
317#if defined(BSLS_PLATFORM_IS_LITTLE_ENDIAN)
318 return BSLS_PLATFORM_IS_LITTLE_ENDIAN;
319#else
320 return false;
321#endif
322}
323
324inline
326{
327#if defined(BSLS_PLATFORM_IS_BIG_ENDIAN)
328 return BSLS_PLATFORM_IS_BIG_ENDIAN;
329#else
330 return false;
331#endif
332}
333
334inline
336{
337 enum { BSLS_MAX_ALIGN = AlignmentUtil::BSLS_MAX_ALIGNMENT };
338 return ((size + BSLS_MAX_ALIGN - 1) / BSLS_MAX_ALIGN) * BSLS_MAX_ALIGN;
339}
340
341} // close package namespace
342
343#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
344// ============================================================================
345// BACKWARD COMPATIBILITY
346// ============================================================================
347
348/// This alias is defined for backward compatibility.
350#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
351
352
353
354#if !defined(BSL_DOUBLE_UNDERSCORE_XLAT) || 1 == BSL_DOUBLE_UNDERSCORE_XLAT
355
356#ifdef BSLS_PLATFORMUTIL_IS_BIG_ENDIAN
357# define BSLS_PLATFORMUTIL__IS_BIG_ENDIAN BSLS_PLATFORMUTIL_IS_BIG_ENDIAN
358#endif
359#ifdef BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN
360# define BSLS_PLATFORMUTIL__IS_LITTLE_ENDIAN BSLS_PLATFORMUTIL_IS_LITTLE_ENDIAN
361#endif
362#define BSLS_PLATFORMUTIL__HTONL(x) BSLS_PLATFORMUTIL_HTONL(x)
363#define BSLS_PLATFORMUTIL__HTONS(x) BSLS_PLATFORMUTIL_HTONS(x)
364#define BSLS_PLATFORMUTIL__NTOHL(x) BSLS_PLATFORMUTIL_NTOHL(x)
365#define BSLS_PLATFORMUTIL__NTOHS(x) BSLS_PLATFORMUTIL_NTOHS(x)
366
367#endif
368
369#endif
370
371// ----------------------------------------------------------------------------
372// Copyright 2013 Bloomberg Finance L.P.
373//
374// Licensed under the Apache License, Version 2.0 (the "License");
375// you may not use this file except in compliance with the License.
376// You may obtain a copy of the License at
377//
378// http://www.apache.org/licenses/LICENSE-2.0
379//
380// Unless required by applicable law or agreed to in writing, software
381// distributed under the License is distributed on an "AS IS" BASIS,
382// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
383// See the License for the specific language governing permissions and
384// limitations under the License.
385// ----------------------------- END-OF-FILE ----------------------------------
386
387/** @} */
388/** @} */
389/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
bsls::PlatformUtil bsls_PlatformUtil
This alias is defined for backward compatibility.
Definition bsls_platformutil.h:349
Definition bdlt_iso8601util.h:691
AlignmentToType< BSLS_MAX_ALIGNMENT >::Type MaxAlignedType
Definition bsls_alignmentutil.h:282
@ BSLS_MAX_ALIGNMENT
Definition bsls_alignmentutil.h:275
Definition bsls_platformutil.h:171
Types::Uint64 Uint64
Definition bsls_platformutil.h:201
static bool isLittleEndian()
Definition bsls_platformutil.h:315
static bool isBigEndian()
Definition bsls_platformutil.h:325
AlignmentUtil::MaxAlignedType MaxAlign
Definition bsls_platformutil.h:208
Types::Int64 Int64
Definition bsls_platformutil.h:195
Types::UintPtr UintPtr
Definition bsls_platformutil.h:187
static int roundUpToMaximalAlignment(int size)
Definition bsls_platformutil.h:335
Types::size_type size_type
Definition bsls_platformutil.h:185
Types::IntPtr IntPtr
Definition bsls_platformutil.h:193
std::size_t UintPtr
Definition bsls_types.h:126
std::size_t size_type
Definition bsls_types.h:124
unsigned long long Uint64
Definition bsls_types.h:137
std::ptrdiff_t IntPtr
Definition bsls_types.h:130
long long Int64
Definition bsls_types.h:132