BDE 4.14.0 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 .
130struct ByteOrder {
131
132 public:
133 // TYPES
134 enum Enum {
135 e_LITTLE_ENDIAN, // base enumeration
136 e_BIG_ENDIAN, // base enumeration
137
138 e_NETWORK = e_BIG_ENDIAN, // Network byte order is always
139 // aliased to big-endian.
140
141#ifdef BSLS_PLATFORM_IS_LITTLE_ENDIAN
142 e_HOST = e_LITTLE_ENDIAN // Host byte order is always aliased to
143#else // a base enumeration, varying
144 e_HOST = e_BIG_ENDIAN // depending on the platform.
145#endif
146 };
147
148 public:
149 // CLASS METHODS
150
151 /// Write the string representation of the specified enumeration `value`
152 /// to the specified output `stream`, and return a reference to
153 /// `stream`. Optionally specify an initial indentation `level`, whose
154 /// absolute value is incremented recursively for nested objects. If
155 /// `level` is specified, optionally specify `spacesPerLevel`, whose
156 /// absolute value indicates the number of spaces per indentation level
157 /// for this and all of its nested objects. If `level` is negative,
158 /// suppress indentation of the first line. If `spacesPerLevel` is
159 /// negative, format the entire output on one line, suppressing all but
160 /// the initial indentation (as governed by `level`). See `toAscii` for
161 /// what constitutes the string representation of a
162 /// `ByteOrder::Enum` value.
163 static bsl::ostream& print(bsl::ostream& stream,
164 Enum value,
165 int level = 0,
166 int spacesPerLevel = 4);
167
168 /// Return the non-modifiable string representation corresponding to the
169 /// specified enumeration `value`, if it exists, and a unique (error)
170 /// string otherwise. The string representation of `value` matches the
171 /// name of its corresponding base enumeration with the "e_" prefix
172 /// elided. For example:
173 /// @code
174 /// bsl::cout << ByteOrder::toAscii(ByteOrder::e_NETWORK);
175 /// @endcode
176 /// will print the following on standard output:
177 /// @code
178 /// BIG_ENDIAN
179 /// @endcode
180 /// Note that specifying a `value` that does not match any of the
181 /// enumerators will result in a string representation that is distinct
182 /// from any of those corresponding to the enumerators, but is otherwise
183 /// unspecified.
184 static const char *toAscii(Enum value);
185};
186
187// FREE OPERATORS
188
189/// Write the string representation of the specified enumeration `value` to
190/// the specified output `stream` in a single-line format, and return a
191/// reference to `stream`. See `toAscii` for what constitutes the string
192/// representation of a `bdlde::ByteOrder::Enum` value. Note that this
193/// method has the same behavior as
194/// @code
195/// bdlde::ByteOrder::print(stream, value, 0, -1);
196/// @endcode
197bsl::ostream& operator<<(bsl::ostream& stream, ByteOrder::Enum value);
198
199// ============================================================================
200// INLINE FUNCTION DEFINITIONS
201// ============================================================================
202
203 // -----------------------
204 // struct bdlde::ByteOrder
205 // -----------------------
206
207} // close package namespace
208
209// FREE OPERATORS
210inline
211bsl::ostream& bdlde::operator<<(bsl::ostream& stream,
213{
214 return ByteOrder::print(stream, value, 0, -1);
215}
216
217
218
219#endif
220
221// ----------------------------------------------------------------------------
222// Copyright 2015 Bloomberg Finance L.P.
223//
224// Licensed under the Apache License, Version 2.0 (the "License");
225// you may not use this file except in compliance with the License.
226// You may obtain a copy of the License at
227//
228// http://www.apache.org/licenses/LICENSE-2.0
229//
230// Unless required by applicable law or agreed to in writing, software
231// distributed under the License is distributed on an "AS IS" BASIS,
232// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
233// See the License for the specific language governing permissions and
234// limitations under the License.
235// ----------------------------- END-OF-FILE ----------------------------------
236
237/** @} */
238/** @} */
239/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlde_base64alphabet.h:118
bsl::ostream & operator<<(bsl::ostream &stream, Base64Alphabet::Enum value)
Definition bdlde_byteorder.h:130
static const char * toAscii(Enum value)
Enum
Definition bdlde_byteorder.h:134
@ e_NETWORK
Definition bdlde_byteorder.h:138
@ e_HOST
Definition bdlde_byteorder.h:144
@ e_BIG_ENDIAN
Definition bdlde_byteorder.h:136
@ e_LITTLE_ENDIAN
Definition bdlde_byteorder.h:135
static bsl::ostream & print(bsl::ostream &stream, Enum value, int level=0, int spacesPerLevel=4)