BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslx_streambufoutstream.h
Go to the documentation of this file.
1/// @file bslx_streambufoutstream.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslx_streambufoutstream.h -*-C++-*-
8#ifndef INCLUDED_BSLX_STREAMBUFOUTSTREAM
9#define INCLUDED_BSLX_STREAMBUFOUTSTREAM
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslx_streambufoutstream bslx_streambufoutstream
15/// @brief Externalization of fundamental types to a `bsl::streambuf`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslx
19/// @{
20/// @addtogroup bslx_streambufoutstream
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslx_streambufoutstream-purpose"> Purpose</a>
25/// * <a href="#bslx_streambufoutstream-classes"> Classes </a>
26/// * <a href="#bslx_streambufoutstream-description"> Description </a>
27/// * <a href="#bslx_streambufoutstream-versioning"> Versioning </a>
28/// * <a href="#bslx_streambufoutstream-usage"> Usage </a>
29/// * <a href="#bslx_streambufoutstream-example-1-basic-externalization"> Example 1: Basic Externalization </a>
30///
31/// # Purpose {#bslx_streambufoutstream-purpose}
32/// Externalization of fundamental types to a `bsl::streambuf`.
33///
34/// # Classes {#bslx_streambufoutstream-classes}
35///
36/// - bslx::StreambufOutStream: `bsl::streambuf` output for fundamentals
37///
38/// @see bslx_streambufinstream, bslx_genericoutstream
39///
40/// # Description {#bslx_streambufoutstream-description}
41/// This component implements a `bsl::streambuf` output stream
42/// class, `bslx::StreambufOutStream`, that provides platform-independent
43/// output methods ("externalization") on values, and arrays of values, of
44/// fundamental types, and on `bsl::string`.
45///
46/// This component is intended to be used in conjunction with the
47/// @ref bslx_streambufinstream "unexternalization" component. Each output method
48/// of `bslx::StreambufOutStream` writes a value or a homogeneous array of
49/// values to a `bsl::streambuf`. The values are formatted to be readable by
50/// the corresponding `bslx::StreambufInStream` method. In general, the user
51/// cannot rely on any other mechanism to read data written by
52/// `bslx::StreambufOutStream` unless that mechanism explicitly states its
53/// ability to do so.
54///
55/// The supported types and required content are listed in the `bslx`
56/// package-level documentation under "Supported Types".
57///
58/// Note that the values are stored in big-endian (i.e., network byte order)
59/// format.
60///
61/// Note that output streams can be *invalidated* explicitly and queried for
62/// *validity*. Writing to an initially invalid stream has no effect. Whenever
63/// an output operation fails, the stream should be invalidated explicitly.
64///
65/// ## Versioning {#bslx_streambufoutstream-versioning}
66///
67///
68/// BDEX provides two concepts that support versioning the BDEX serialization
69/// format of a type: `version` and `versionSelector`. A `version` is a 1-based
70/// integer indicating one of the supported formats (e.g., format 1, format 2,
71/// etc.). A `versionSelector` is a value that is mapped to a `version` for a
72/// type by the type's implementation of `maxSupportedBdexVersion`.
73///
74/// Selecting a value for a `versionSelector` is required at two different
75/// points: (1) when implementing a new `version` format within the
76/// `bdexStreamIn` and `bdexStreamOut` methods of a type, and (2) when
77/// implementing code that constructs a BDEX `OutStream`. In both cases, the
78/// value should be a *compile*-time-selected value.
79///
80/// When a new `version` format is implemented within the `bdexStreamIn` and
81/// `bdexStreamOut` methods of a type, a new mapping in
82/// `maxSupportedBdexVersion` should be created to expose this new `version`
83/// with a `versionSelector`. A simple - and the recommended - approach is to
84/// use a value having the pattern "YYYYMMDD", where "YYYYMMDD" corresponds to
85/// the "go-live" date of the corresponding `version` format.
86///
87/// When constructing an `OutStream`, a simple approach is to use the current
88/// date as a *compile*-time constant value. In combination with the
89/// recommended selection of `versionSelector` values for
90/// `maxSupportedBdexVersion`, this will result in consistent and predictable
91/// behavior while externalizing types. Note that this recommendation is chosen
92/// for its simplicity: to ensure the largest possible audience for an
93/// externalized representation, clients can select the minimum date value that
94/// will result in the desired version of all types externalized with
95/// `operator<<` being selected.
96///
97/// See the `bslx` package-level documentation for more detailed information
98/// about versioning.
99///
100/// ## Usage {#bslx_streambufoutstream-usage}
101///
102///
103/// This section illustrates intended use of this component.
104///
105/// ### Example 1: Basic Externalization {#bslx_streambufoutstream-example-1-basic-externalization}
106///
107///
108/// A `bslx::StreambufOutStream` can be used to externalize values in a
109/// platform-neutral way. Writing out fundamental C++ types and `bsl::string`
110/// requires no additional work on the part of the client; the client can simply
111/// use the stream directly. The following code serializes a few representative
112/// values using a `bslx::StreambufOutStream`, compares the contents of this
113/// stream to the expected value, and then writes the contents of this stream's
114/// buffer to `stdout`.
115///
116/// First, we create a `bslx::StreambufOutStream` with an arbitrary value for
117/// its `versionSelector` and externalize some values:
118/// @code
119/// bsl::stringbuf buffer;
120/// bslx::StreambufOutStream outStream(&buffer, 20131127);
121/// outStream.putInt32(1);
122/// outStream.putInt32(2);
123/// outStream.putInt8('c');
124/// outStream.putString(bsl::string("hello"));
125/// @endcode
126/// Then, we compare the contents of the stream to the expected value:
127/// @code
128/// bsl::string theChars = buffer.str();
129/// ASSERT(15 == theChars.size());
130/// ASSERT( 0 == bsl::memcmp(theChars.data(),
131/// "\x00\x00\x00\x01\x00\x00\x00\x02""c\x05""hello",
132/// 15));
133/// @endcode
134/// Finally, we print the stream's contents to `bsl::cout`.
135/// @code
136/// for (bsl::size_t i = 0; i < theChars.size(); ++i) {
137/// if (bsl::isalnum(static_cast<unsigned char>(theChars[i]))) {
138/// bsl::cout << "nextByte (char): " << theChars[i] << bsl::endl;
139/// }
140/// else {
141/// bsl::cout << "nextByte (int): "
142/// << static_cast<int>(theChars[i])
143/// << bsl::endl;
144/// }
145/// }
146/// @endcode
147/// Executing the above code results in the following output:
148/// @code
149/// nextByte (int): 0
150/// nextByte (int): 0
151/// nextByte (int): 0
152/// nextByte (int): 1
153/// nextByte (int): 0
154/// nextByte (int): 0
155/// nextByte (int): 0
156/// nextByte (int): 2
157/// nextByte (char): c
158/// nextByte (int): 5
159/// nextByte (char): h
160/// nextByte (char): e
161/// nextByte (char): l
162/// nextByte (char): l
163/// nextByte (char): o
164/// @endcode
165/// See the @ref bslx_streambufinstream component usage example for a more
166/// practical example of using `bslx` streams.
167/// @}
168/** @} */
169/** @} */
170
171/** @addtogroup bsl
172 * @{
173 */
174/** @addtogroup bslx
175 * @{
176 */
177/** @addtogroup bslx_streambufoutstream
178 * @{
179 */
180
181#include <bslscm_version.h>
182
184
185#include <bsl_streambuf.h>
186
187
188namespace bslx {
189
190 // ========================
191 // class StreambufOutStream
192 // ========================
193
194/// This class facilitates the externalization of values (and C-style arrays
195/// of values) of the fundamental integral and floating-point types in a
196/// data-independent, platform-neutral representation. It is currently a
197/// `typedef` for `bslx::GenericOutStream<bsl::streambuf>`.
199
200} // close package namespace
201
202
203#endif
204
205// ----------------------------------------------------------------------------
206// Copyright 2014 Bloomberg Finance L.P.
207//
208// Licensed under the Apache License, Version 2.0 (the "License");
209// you may not use this file except in compliance with the License.
210// You may obtain a copy of the License at
211//
212// http://www.apache.org/licenses/LICENSE-2.0
213//
214// Unless required by applicable law or agreed to in writing, software
215// distributed under the License is distributed on an "AS IS" BASIS,
216// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
217// See the License for the specific language governing permissions and
218// limitations under the License.
219// ----------------------------- END-OF-FILE ----------------------------------
220
221/** @} */
222/** @} */
223/** @} */
Definition bslx_genericoutstream.h:363
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bslx_byteinstream.h:377
GenericOutStream< bsl::streambuf > StreambufOutStream
Definition bslx_streambufoutstream.h:198