BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlde_byteorder.h
Go to the documentation of this file.
1/// @file bdlde_byteorder.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlde_byteorder.h -*-C++-*-
8#ifndef INCLUDED_BDLDE_BYTEORDER
9#define INCLUDED_BDLDE_BYTEORDER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlde_byteorder bdlde_byteorder
15/// @brief Provide an enumeration of the set of possible byte orders.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlde
19/// @{
20/// @addtogroup bdlde_byteorder
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlde_byteorder-purpose"> Purpose</a>
25/// * <a href="#bdlde_byteorder-classes"> Classes </a>
26/// * <a href="#bdlde_byteorder-description"> Description </a>
27/// * <a href="#bdlde_byteorder-enumerators"> Enumerators </a>
28/// * <a href="#bdlde_byteorder-usage"> Usage </a>
29/// * <a href="#bdlde_byteorder-example-1-basic-syntax"> Example 1: Basic Syntax </a>
30///
31/// # Purpose {#bdlde_byteorder-purpose}
32/// Provide an enumeration of the set of possible byte orders.
33///
34/// # Classes {#bdlde_byteorder-classes}
35///
36/// - bdlde::ByteOrder: namespace for a byte order `enum`
37///
38/// @see bdlde_charconvertutf16
39///
40/// # Description {#bdlde_byteorder-description}
41/// This component provides a namespace for the `enum` type
42/// `bdlde::ByteOrder::Enum`, which enumerates the set of possible byte orders.
43///
44/// ## Enumerators {#bdlde_byteorder-enumerators}
45///
46///
47/// @code
48/// Name Description
49/// --------------- ---------------------------------------------------
50/// e_LITTLE_ENDIAN little-endian
51/// e_BIG_ENDIAN big-endian
52/// e_NETWORK network byte order (aliased to 'e_BIG_ENDIAN')
53/// e_HOST aliased to 'e_LITTLE_ENDIAN' or 'e_BIG_ENDIAN' (depending
54/// on platform)
55/// @endcode
56///
57/// ## Usage {#bdlde_byteorder-usage}
58///
59///
60/// This section illustrates intended use of this component.
61///
62/// ### Example 1: Basic Syntax {#bdlde_byteorder-example-1-basic-syntax}
63///
64///
65/// The following snippets of code provide a simple illustration of
66/// `bdlde::ByteOrder` usage.
67///
68/// First, we create a variable `value` of type `bdlde::ByteOrder::Enum` and
69/// initialize it with the enumerator value
70/// `bdlde::ByteOrder::e_LITTLE_ENDIAN`:
71/// @code
72/// bdlde::ByteOrder::Enum value = bdlde::ByteOrder::e_LITTLE_ENDIAN;
73/// @endcode
74/// Next, we store a pointer to its ASCII representation in a variable
75/// `asciiValue` of type `const char *`:
76/// @code
77/// const char *asciiValue = bdlde::ByteOrder::toAscii(value);
78/// assert(0 == bsl::strcmp(asciiValue, "LITTLE_ENDIAN"));
79/// @endcode
80/// Then, we try one of the aliased identifiers, and we get a string
81/// corresponding to the value it is aliased to:
82/// @code
83/// value = bdlde::ByteOrder::e_NETWORK;
84/// asciiValue = bdlde::ByteOrder::toAscii(value);
85///
86/// assert(0 == bsl::strcmp(asciiValue, "BIG_ENDIAN"));
87/// @endcode
88/// Finally, we print `value` to `bsl::cout`:
89/// @code
90/// bsl::cout << value << bsl::endl;
91/// @endcode
92/// This statement produces the following output on `stdout`:
93/// @code
94/// BIG_ENDIAN
95/// @endcode
96/// @}
97/** @} */
98/** @} */
99
100/** @addtogroup bdl
101 * @{
102 */
103/** @addtogroup bdlde
104 * @{
105 */
106/** @addtogroup bdlde_byteorder
107 * @{
108 */
109
110#include <bdlscm_version.h>
111
112#include <bsls_platform.h>
113
114#include <bsl_iosfwd.h>
115
116
117
118namespace bdlde {
119 // ================
120 // struct ByteOrder
121 // ================
122
123/// This `struct` provides a namespace for enumerating the set of byte
124/// orders. See `Enum` in the TYPES sub-section for details.
125///
126/// This class:
127/// * supports a complete set of *enumeration* operations
128/// - except for `bdex` serialization
129/// For terminology see @ref bsldoc_glossary .
130///
131/// See @ref bdlde_byteorder
132struct ByteOrder {
133
134 public:
135 // TYPES
136 enum Enum {
137 e_LITTLE_ENDIAN, // base enumeration
138 e_BIG_ENDIAN, // base enumeration
139
140 e_NETWORK = e_BIG_ENDIAN, // Network byte order is always
141 // aliased to big-endian.
142
143#ifdef BSLS_PLATFORM_IS_LITTLE_ENDIAN
144 e_HOST = e_LITTLE_ENDIAN // Host byte order is always aliased to
145#else // a base enumeration, varying
146 e_HOST = e_BIG_ENDIAN // depending on the platform.
147#endif
148 };
149
150 public:
151 // CLASS METHODS
152
153 /// Write the string representation of the specified enumeration `value`
154 /// to the specified output `stream`, and return a reference to
155 /// `stream`. Optionally specify an initial indentation `level`, whose
156 /// absolute value is incremented recursively for nested objects. If
157 /// `level` is specified, optionally specify `spacesPerLevel`, whose
158 /// absolute value indicates the number of spaces per indentation level
159 /// for this and all of its nested objects. If `level` is negative,
160 /// suppress indentation of the first line. If `spacesPerLevel` is
161 /// negative, format the entire output on one line, suppressing all but
162 /// the initial indentation (as governed by `level`). See `toAscii` for
163 /// what constitutes the string representation of a
164 /// `ByteOrder::Enum` value.
165 static bsl::ostream& print(bsl::ostream& stream,
166 Enum value,
167 int level = 0,
168 int spacesPerLevel = 4);
169
170 /// Return the non-modifiable string representation corresponding to the
171 /// specified enumeration `value`, if it exists, and a unique (error)
172 /// string otherwise. The string representation of `value` matches the
173 /// name of its corresponding base enumeration with the "e_" prefix
174 /// elided. For example:
175 /// @code
176 /// bsl::cout << ByteOrder::toAscii(ByteOrder::e_NETWORK);
177 /// @endcode
178 /// will print the following on standard output:
179 /// @code
180 /// BIG_ENDIAN
181 /// @endcode
182 ///
183 /// \note Note that specifying a `value` that does not match any of the
184 /// enumerators will result in a string representation that is distinct
185 /// from any of those corresponding to the enumerators, but is otherwise
186 /// unspecified.
187 static const char *toAscii(Enum value);
188};
189
190// FREE OPERATORS
191
192/// Write the string representation of the specified enumeration `value` to
193/// the specified output `stream` in a single-line format, and return a
194/// reference to `stream`. See `toAscii` for what constitutes the string representation of a `bdlde::ByteOrder::Enum` value.
195///
196/// \note Note that this
197/// method has the same behavior as
198/// @code
199/// bdlde::ByteOrder::print(stream, value, 0, -1);
200/// @endcode
201bsl::ostream& operator<<(bsl::ostream& stream, ByteOrder::Enum value);
202
203// ============================================================================
204// INLINE FUNCTION DEFINITIONS
205// ============================================================================
206
207 // -----------------------
208 // struct bdlde::ByteOrder
209 // -----------------------
210
211} // close package namespace
212
213// FREE OPERATORS
214inline
215bsl::ostream& bdlde::operator<<(bsl::ostream& stream,
217{
218 return ByteOrder::print(stream, value, 0, -1);
219}
220
221
222
223#endif
224
225// ----------------------------------------------------------------------------
226// Copyright 2015 Bloomberg Finance L.P.
227//
228// Licensed under the Apache License, Version 2.0 (the "License");
229// you may not use this file except in compliance with the License.
230// You may obtain a copy of the License at
231//
232// http://www.apache.org/licenses/LICENSE-2.0
233//
234// Unless required by applicable law or agreed to in writing, software
235// distributed under the License is distributed on an "AS IS" BASIS,
236// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
237// See the License for the specific language governing permissions and
238// limitations under the License.
239// ----------------------------- END-OF-FILE ----------------------------------
240
241/** @} */
242/** @} */
243/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlde_base64alphabet.h:118
bsl::ostream & operator<<(bsl::ostream &stream, Base64Alphabet::Enum value)
Definition bdlde_byteorder.h:132
static const char * toAscii(Enum value)
Enum
Definition bdlde_byteorder.h:136
@ e_NETWORK
Definition bdlde_byteorder.h:140
@ e_HOST
Definition bdlde_byteorder.h:146
@ e_BIG_ENDIAN
Definition bdlde_byteorder.h:138
@ e_LITTLE_ENDIAN
Definition bdlde_byteorder.h:137
static bsl::ostream & print(bsl::ostream &stream, Enum value, int level=0, int spacesPerLevel=4)