BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlsb_fixedmemoutstreambuf.h
Go to the documentation of this file.
1/// @file bdlsb_fixedmemoutstreambuf.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlsb_fixedmemoutstreambuf.h -*-C++-*-
8#ifndef INCLUDED_BDLSB_FIXEDMEMOUTSTREAMBUF
9#define INCLUDED_BDLSB_FIXEDMEMOUTSTREAMBUF
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlsb_fixedmemoutstreambuf bdlsb_fixedmemoutstreambuf
15/// @brief Provide an output `basic_streambuf` using a client buffer.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlsb
19/// @{
20/// @addtogroup bdlsb_fixedmemoutstreambuf
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlsb_fixedmemoutstreambuf-purpose"> Purpose</a>
25/// * <a href="#bdlsb_fixedmemoutstreambuf-classes"> Classes </a>
26/// * <a href="#bdlsb_fixedmemoutstreambuf-description"> Description </a>
27/// * <a href="#bdlsb_fixedmemoutstreambuf-streaming-architecture"> Streaming Architecture </a>
28/// * <a href="#bdlsb_fixedmemoutstreambuf-usage"> Usage </a>
29/// * <a href="#bdlsb_fixedmemoutstreambuf-example-1-directly-observing-stream-buffer-contents"> Example 1: Directly Observing Stream Buffer Contents </a>
30/// * <a href="#bdlsb_fixedmemoutstreambuf-example-2-fixed-buffer-size"> Example 2: Fixed Buffer Size </a>
31///
32/// # Purpose {#bdlsb_fixedmemoutstreambuf-purpose}
33/// Provide an output @ref basic_streambuf using a client buffer.
34///
35/// # Classes {#bdlsb_fixedmemoutstreambuf-classes}
36///
37/// - bdlsb::FixedMemOutStreamBuf: output stream buffer using client memory
38///
39/// @see bdlsb_memoutstreambuf, bdlsb_fixedmeminstreambuf
40///
41/// # Description {#bdlsb_fixedmemoutstreambuf-description}
42/// This component defines a class `bdlsb::FixedMemOutStreamBuf`
43/// that implements the output portion of the `bsl::basic_streambuf` protocol
44/// using a client-supplied memory buffer. Method names necessarily correspond
45/// to the protocol-specified method names. Clients supply the character buffer
46/// at stream buffer construction, and can later reinitialize the stream buffer
47/// with a different character buffer by calling the `pubsetbuf` method.
48///
49/// This component provides none of the input-related functionality of
50/// @ref basic_streambuf (see Streaming Architecture, below), nor does it use
51/// locales in any way.
52///
53/// ## Streaming Architecture {#bdlsb_fixedmemoutstreambuf-streaming-architecture}
54///
55///
56/// Stream buffers are designed to decouple device handling from content
57/// formatting, providing the requisite device handling and possible buffering
58/// services, and leaving the formatting to the client stream. The standard
59/// C++ IOStreams library further partitions streaming into input streaming and
60/// output streaming, separating responsibilities for each at both the stream
61/// layer and the stream buffer layer. The BDE streaming library for `bdex`,
62/// including all of `bdesb`, follows this model.
63///
64/// ## Usage {#bdlsb_fixedmemoutstreambuf-usage}
65///
66///
67/// This section illustrates intended use of this component.
68///
69/// ### Example 1: Directly Observing Stream Buffer Contents {#bdlsb_fixedmemoutstreambuf-example-1-directly-observing-stream-buffer-contents}
70///
71///
72/// Unlike most implementations of the `bsl::basic_streambuf` concept,
73/// `bdlsb::FixedMemOutStreamBuf` gives the user direct access to the stream's
74/// storage, both through the `data` accessor and through the buffer originally
75/// supplied to the constructor. Note that this can be useful in many contexts,
76/// such as when we need to perform extra security validation on buffer during
77/// the streaming process.
78///
79/// First, we create an array to provide storage for the stream buffer, and
80/// construct a `bdlsb::FixedMemOutStreamBuf` on that array:
81/// @code
82/// const int STORAGE_SIZE = 64;
83/// char storage[STORAGE_SIZE];
84/// bdlsb::FixedMemOutStreamBuf buffer(storage, STORAGE_SIZE);
85/// @endcode
86/// Notice that `storage` is on the stack. `bdlsb::FixedMemOutStreamBuf` can be
87/// easily used without resorting to dynamic memory allocation.
88///
89/// Then, we observe that `buffer` already has a capacity of 64. Note that this
90/// capacity is fixed at construction:
91/// @code
92/// assert(STORAGE_SIZE == buffer.capacity());
93/// assert( 0 == buffer.length());
94/// assert(buffer.data() == storage);
95/// @endcode
96/// Next, we use `buffer` to construct a `bsl::ostream`:
97/// @code
98/// bsl::ostream stream(&buffer);
99/// @endcode
100/// Now, we output some data to the `stream`:
101/// @code
102/// stream << "The answer is " << 42 << ".";
103/// @endcode
104/// Finally, we observe that the data is present in the storage array that we
105/// supplied to `buffer`:
106/// @code
107/// assert(17 == buffer.length());
108/// assert(buffer.length() < STORAGE_SIZE);
109/// assert(0 == strncmp("The answer is 42.", storage, 17));
110/// @endcode
111///
112/// ### Example 2: Fixed Buffer Size {#bdlsb_fixedmemoutstreambuf-example-2-fixed-buffer-size}
113///
114///
115/// Unlike most implementations of the `bsl::basic_streambuf` concept,
116/// `bdlsb::FixedMemOutStreamBuf` uses a buffer of limited size, provided to the
117/// constructor together with the address of the storage buffer. That limit
118/// will not be exceeded even in case of superfluous data. Symbols beyond this
119/// limit will be ignored. Note that this can be useful if memory allocation
120/// should be strictly controlled.
121///
122/// First, we create an array to provide storage for the stream buffer, fill it
123/// with some data and construct a `bdlsb::FixedMemOutStreamBuf` on the part of
124/// that array:
125/// @code
126/// const unsigned int SMALL_STORAGE_SIZE = 16;
127/// const unsigned int SMALL_BUFFER_CAPACITY = SMALL_STORAGE_SIZE/2;
128/// char smallStorage[SMALL_STORAGE_SIZE];
129/// memset(smallStorage, 'Z', SMALL_STORAGE_SIZE);
130///
131/// bdlsb::FixedMemOutStreamBuf smallBuffer(smallStorage,
132/// SMALL_BUFFER_CAPACITY);
133/// @endcode
134/// Next, we write some characters to the buffer and check that it handles them
135/// correctly and superfluous data is ignored:
136/// @code
137/// bsl::streamsize returnedSize = smallBuffer.sputn("The answer is 42.", 17);
138/// assert(SMALL_BUFFER_CAPACITY == returnedSize);
139/// assert(SMALL_BUFFER_CAPACITY == smallBuffer.length());
140/// assert('Z' == smallStorage[smallBuffer.length()]);
141/// @endcode
142/// Then, we reset position indicator to the beginning of storage:
143/// @code
144/// smallBuffer.pubseekpos(0,bsl::ios_base::out);
145/// assert(0 == smallBuffer.length());
146/// @endcode
147/// Now, we write another string, containing fewer characters than the storage
148/// capacity:
149/// @code
150/// returnedSize = smallBuffer.sputn("Truth.", 6);
151/// @endcode
152/// Finally, we observe that given string has been successfully placed to
153/// buffer:
154/// @code
155/// assert(6 == returnedSize);
156/// assert(6 == smallBuffer.length());
157/// assert(0 == strncmp("Truth.", smallStorage, 6));
158/// @endcode
159/// @}
160/** @} */
161/** @} */
162
163/** @addtogroup bdl
164 * @{
165 */
166/** @addtogroup bdlsb
167 * @{
168 */
169/** @addtogroup bdlsb_fixedmemoutstreambuf
170 * @{
171 */
172
173#include <bdlscm_version.h>
174
175#include <bsls_assert.h>
176#include <bsls_keyword.h>
177#include <bsls_platform.h>
178#include <bsls_review.h>
179
180#include <bsl_cstdlib.h>
181#include <bsl_cstring.h>
182#include <bsl_ios.h>
183#include <bsl_streambuf.h>
184
185#if defined(BSLS_PLATFORM_CMP_MSVC) && defined(min)
186 // Note: on Windows -> WinDef.h:#define min(a,b) ...
187#undef min
188#endif
189
190
191namespace bdlsb {
192
193 // ==========================
194 // class FixedMemOutStreamBuf
195 // ==========================
196
197/// This class implements the output functionality of the @ref basic_streambuf
198/// protocol for client-supplied memory.
199///
200/// See @ref bdlsb_fixedmemoutstreambuf
201class FixedMemOutStreamBuf : public bsl::streambuf {
202
203 private:
204 // NOT IMPLEMENTED
207
208 protected:
209 // PROTECTED MANIPULATORS
210
211 /// Set the position indicator to the relative specified `offset` from
212 /// the base position indicated by the specified `fixedPosition` and
213 /// return the resulting absolute position on success or pos_type(-1)
214 /// on failure. Optionally specify `which` area of the stream buffer.
215 /// The seek operation will fail if `which` does not include the flag
216 /// `bsl::ios_base::out` or if the resulting absolute position is less
217 /// than zero or greater than the value returned by `length`.
218 pos_type seekoff(off_type offset,
219 bsl::ios_base::seekdir fixedPosition,
220 bsl::ios_base::openmode which = bsl::ios_base::out)
222
223 /// Set the position indicator to the specified `position` and return
224 /// the resulting absolute position on success or pos_type(-1) on
225 /// failure. Optionally specify `which` area of the stream buffer. The
226 /// `seekpos` operation will fail if `which` does not include the flag
227 /// `bsl::ios_base::out` or if position is less then zero or greater
228 /// than the value returned by `length`.
229 pos_type seekpos(pos_type position,
230 bsl::ios_base::openmode which = bsl::ios_base::out)
232
233 FixedMemOutStreamBuf *setbuf(char_type *buffer,
234 bsl::streamsize length)
236 // Reinitialize this stream buffer to use the specified character
237 // 'buffer' having the specified 'length'. Return the address of this
238 // modifiable stream buffer. The behavior is undefined unless
239 // 'length == 0' or 'length > 0 && buffer != 0'. Upon
240 // re-initialization for use of the new buffer, the length and next
241 // output location are reset to zero. Note that 'buffer' is held but
242 // not owned.
243
244 public:
245 // CREATORS
246
247 /// Create an empty stream buffer that uses the specified character
248 /// `buffer` of the specified `length`. The behavior is undefined
249 /// unless `length == 0` or `length > 0 && buffer != 0`.
250 /// Note that `buffer` is held but not owned.
251 FixedMemOutStreamBuf(char *buffer,
252 bsl::streamsize length);
253
254 /// Destroy this stream buffer.
256
257 // MANIPULATORS
258
259 /// Return a pointer providing modifiable access to the character buffer
260 /// held by this stream buffer (supplied at construction).
261 char *data();
262
263 // ACCESSORS
264
265 /// Return the number of characters in the buffer held by this stream
266 /// buffer. See `length`, below, for the span of bytes actually
267 /// written.
268 bsl::streamsize capacity() const;
269
270 /// Return a pointer providing non-modifiable access to the character
271 /// buffer held by this stream buffer (supplied at construction).
272 const char *data() const;
273
274 /// Return the number of characters from the beginning of the buffer to
275 /// the current write position. This function returns the same value
276 /// as `seekoff(0, bsl::ios_base::end)`. The length is modified by a
277 /// call to `seekpos` or `seekoff` and reset to zero by a call to
278 /// `pubsetbuf`.
279 bsl::streamsize length() const;
280};
281
282// ============================================================================
283// INLINE DEFINITIONS
284// ============================================================================
285
286 // --------------------------
287 // class FixedMemOutStreamBuf
288 // --------------------------
289
290// PROTECTED MANIPULATORS
291inline
293FixedMemOutStreamBuf::setbuf(char_type *buffer, bsl::streamsize length)
294{
295 BSLS_ASSERT(buffer || 0 == length);
296 BSLS_ASSERT(0 <= length);
297
298 // Reset pointers and length.
299 setp(buffer, buffer + length);
300 return this;
301}
302
303// CREATORS
304inline
305FixedMemOutStreamBuf::FixedMemOutStreamBuf(char *buffer,
306 bsl::streamsize length)
307{
308 BSLS_ASSERT(buffer || 0 == length);
309 BSLS_ASSERT(0 <= length);
310
311 setp(buffer, buffer + length);
312}
313
314inline
318
319// MANIPULATORS
320inline
322{
323 return pbase();
324}
325
326// ACCESSORS
327inline
328bsl::streamsize FixedMemOutStreamBuf::capacity() const
329{
330 return epptr() - pbase();
331}
332
333inline
334const char *FixedMemOutStreamBuf::data() const
335{
336 return pbase();
337}
338
339inline
340bsl::streamsize FixedMemOutStreamBuf::length() const
341{
342 return pptr() - pbase();
343}
344
345} // close package namespace
346
347
348#endif
349
350// ----------------------------------------------------------------------------
351// Copyright 2015 Bloomberg Finance L.P.
352//
353// Licensed under the Apache License, Version 2.0 (the "License");
354// you may not use this file except in compliance with the License.
355// You may obtain a copy of the License at
356//
357// http://www.apache.org/licenses/LICENSE-2.0
358//
359// Unless required by applicable law or agreed to in writing, software
360// distributed under the License is distributed on an "AS IS" BASIS,
361// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
362// See the License for the specific language governing permissions and
363// limitations under the License.
364// ----------------------------- END-OF-FILE ----------------------------------
365
366/** @} */
367/** @} */
368/** @} */
Definition bdlsb_fixedmemoutstreambuf.h:201
bsl::streamsize capacity() const
Definition bdlsb_fixedmemoutstreambuf.h:328
bsl::streamsize length() const
Definition bdlsb_fixedmemoutstreambuf.h:340
char * data()
Definition bdlsb_fixedmemoutstreambuf.h:321
~FixedMemOutStreamBuf() BSLS_KEYWORD_OVERRIDE
Destroy this stream buffer.
Definition bdlsb_fixedmemoutstreambuf.h:315
pos_type seekoff(off_type offset, bsl::ios_base::seekdir fixedPosition, bsl::ios_base::openmode which=bsl::ios_base::out) BSLS_KEYWORD_OVERRIDE
pos_type seekpos(pos_type position, bsl::ios_base::openmode which=bsl::ios_base::out) BSLS_KEYWORD_OVERRIDE
FixedMemOutStreamBuf * setbuf(char_type *buffer, bsl::streamsize length) BSLS_KEYWORD_OVERRIDE
Definition bdlsb_fixedmemoutstreambuf.h:293
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:653
Definition bdlsb_fixedmeminput.h:145
Definition bdlb_printmethods.h:283