BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlsb_fixedmemoutput.h
Go to the documentation of this file.
1/// @file bdlsb_fixedmemoutput.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlsb_fixedmemoutput.h -*-C++-*-
8#ifndef INCLUDED_BDLSB_FIXEDMEMOUTPUT
9#define INCLUDED_BDLSB_FIXEDMEMOUTPUT
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlsb_fixedmemoutput bdlsb_fixedmemoutput
15/// @brief Provide a basic output stream buffer using a client buffer.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlsb
19/// @{
20/// @addtogroup bdlsb_fixedmemoutput
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlsb_fixedmemoutput-purpose"> Purpose</a>
25/// * <a href="#bdlsb_fixedmemoutput-classes"> Classes </a>
26/// * <a href="#bdlsb_fixedmemoutput-description"> Description </a>
27/// * <a href="#bdlsb_fixedmemoutput-usage"> Usage </a>
28/// * <a href="#bdlsb_fixedmemoutput-example-1-basic-use-of-bdlsb-fixedmemoutput"> Example 1: Basic Use of bdlsb::FixedMemOutput </a>
29///
30/// # Purpose {#bdlsb_fixedmemoutput-purpose}
31/// Provide a basic output stream buffer using a client buffer.
32///
33/// # Classes {#bdlsb_fixedmemoutput-classes}
34///
35/// - bdlsb::FixedMemOutput: basic output stream buffer using client memory
36///
37/// @see bdlsb_fixedmemoutstreambuf
38///
39/// # Description {#bdlsb_fixedmemoutput-description}
40/// This component implements the output portion of the
41/// `bsl::basic_streambuf` protocol using a client-supplied memory buffer.
42/// Method names correspond to the protocol-specified method names. Clients
43/// supply the character buffer at stream buffer construction, and can later
44/// reinitialize the stream buffer with a different character buffer by calling
45/// the `pubsetbuf` method. The only difference between this component and
46/// @ref bdlsb_fixedmemoutstreambuf is that the class `bdlsb::FixedMemOutput` does
47/// *not* derive from a `bsl::streambuf` and does not support locales. This is
48/// advantageous for performance reasons, as the overhead of the initialization
49/// and virtual function calls of a `bsl::streambuf` can be undesirable. The
50/// `bdlsb::FixedMemOutput` is designed to be used by generic template code that
51/// must be instantiated on a type that matches the interface of
52/// `bsl::streambuf`, but does not require an actual `bsl::streambuf`, in
53/// particular @ref bslx_genericoutstream .
54///
55/// ## Usage {#bdlsb_fixedmemoutput-usage}
56///
57///
58/// This section illustrates intended use of this component.
59///
60/// ### Example 1: Basic Use of bdlsb::FixedMemOutput {#bdlsb_fixedmemoutput-example-1-basic-use-of-bdlsb-fixedmemoutput}
61///
62///
63/// This example demonstrates instantiating a template, bslx::GenericOutStream',
64/// on a `bdlsb::FixedMemOutput` object and using the `bslx::GenericOutStream`
65/// object to stream out some data.
66///
67/// First, we create an object of our stream buffer:
68/// @code
69/// enum { k_STREAMBUF_CAPACITY = 30 };
70///
71/// char buffer[k_STREAMBUF_CAPACITY];
72/// bdlsb::FixedMemOutput streamBuf(buffer, k_STREAMBUF_CAPACITY);
73/// @endcode
74/// Then, we create an instance of `bslx::GenericOutStream` using `streamBuf`,
75/// with an arbitrary value for its `versionSelector`, and externalize some
76/// values:
77/// @code
78/// bslx::GenericOutStream<bdlsb::FixedMemOutput> outStream(&streamBuf,
79/// 20150707);
80/// outStream.putInt32(1);
81/// outStream.putInt32(2);
82/// outStream.putInt8('c');
83/// outStream.putString(bsl::string("hello"));
84/// @endcode
85/// Finally, we compare the contents of the buffer to the expected value:
86/// @code
87/// assert(15 == streamBuf.length());
88/// assert( 0 == bsl::memcmp(streamBuf.data(),
89/// "\x00\x00\x00\x01\x00\x00\x00\x02""c\x05""hello",
90/// 15));
91/// @endcode
92/// @}
93/** @} */
94/** @} */
95
96/** @addtogroup bdl
97 * @{
98 */
99/** @addtogroup bdlsb
100 * @{
101 */
102/** @addtogroup bdlsb_fixedmemoutput
103 * @{
104 */
105
106#include <bdlscm_version.h>
107
108#include <bsls_assert.h>
109#include <bsls_platform.h>
110#include <bsls_review.h>
111#include <bsls_types.h>
112
113#include <bsl_algorithm.h>
114#include <bsl_cstdlib.h>
115#include <bsl_cstring.h>
116#include <bsl_ios.h> // 'bsl::streamsize'
117#include <bsl_iosfwd.h>
118#include <bsl_locale.h>
119#include <bsl_streambuf.h>
120
121
122namespace bdlsb {
123
124 // ==============
125 // FixedMemOutput
126 // ==============
127
128/// This class, like `bdlsb::FixedMemOutStreamBuf`, implements the output
129/// functionality of the @ref basic_streambuf interface, using client-supplied
130/// `char *` memory. It has an identical interface to
131/// `bdlsb::FixedMemOutStreamBuf` but does *not* inherit from `bsl::streambuf`.
132/// Thus, it is suitable for use as template parameter to
133/// `bslx::GenericOutStream` (but not to `bslx::StreambufOutStream`).
134///
135/// \note Note that this class is not designed to be derived from.
136///
137/// See @ref bdlsb_fixedmemoutput
139
140 public:
141 // TYPES
142 typedef char char_type;
143 typedef bsl::char_traits<char>::int_type int_type;
144 typedef bsl::char_traits<char>::pos_type pos_type;
145 typedef bsl::char_traits<char>::off_type off_type;
146 typedef bsl::char_traits<char> traits_type;
147
148 private:
149 // PRIVATE TYPE
150 typedef bsls::Types::IntPtr IntPtr;
151
152 // PRIVATE DATA MEMBERS
153 char *d_buffer_p; // output buffer
154 bsl::streamsize d_capacity; // length of output buffer
155 pos_type d_pos; // output cursor
156
157 private:
158 // NOT IMPLEMENTED
160 FixedMemOutput& operator=(const FixedMemOutput&);
161
162 public:
163 // CREATORS
164
165 /// Create an empty stream buffer that uses the specified character
166 /// `buffer` of the specified `length`.
167 ///
168 /// \pre The behavior is undefined unless `length == 0` or `length > 0 && buffer != 0`.
169 ///
170 /// \note Note that `buffer` is held but not owned.
171 FixedMemOutput(char *buffer, bsl::streamsize length);
172
173 /// Destroy this object.
174 ~FixedMemOutput() = default;
175
176 // MANIPULATORS
177
178 /// Return a pointer providing modifiable access to the character buffer
179 /// held by this stream buffer (supplied at construction).
180 char *data();
181
182 // *** 27.5.2.2.1 locales: ***
183
184 /// Associate the specified locale `loc` to this stream buffer. Operation
185 /// has no effect, because locales are not supported by this component.
186 /// Return default constructed bsl::locale object.
187 bsl::locale pubimbue(const bsl::locale& loc);
188
189 // *** 27.5.2.2.2 buffer and positioning: ***
190
191 /// Reset the internal buffer of this stream to the specified `buffer` of the specified `length`.
192 ///
193 /// \note Note that the next write operation will start
194 /// at the beginning of `buffer`.
195 FixedMemOutput *pubsetbuf(char *buffer,
196 bsl::streamsize length);
197
198 /// Set the position indicator to the relative specified `offset` from the
199 /// base position indicated by the specified `fixedPosition` and return the
200 /// resulting absolute position on success or pos_type(-1) on failure.
201 /// Optionally specify `which` area of the stream buffer. The seek
202 /// operation will fail if `which` does not include the flag
203 /// `bsl::ios_base::out` or if the resulting absolute position is less than
204 /// zero or greater than the value returned by `length`.
206 bsl::ios_base::seekdir fixedPosition,
207 bsl::ios_base::openmode which =
208 bsl::ios_base::in | bsl::ios_base::out);
209
210 /// Set the position indicator to the specified `position` and return the
211 /// resulting absolute position on success or pos_type(-1) on failure.
212 /// Optionally specify `which` area of the stream buffer. The `seekpos`
213 /// operation will fail if `which` does not include the flag
214 /// `bsl::ios_base::out` or if position is less then zero or greater than
215 /// the value returned by `length`.
217 bsl::ios_base::openmode which =
218 bsl::ios_base::in | bsl::ios_base::out);
219
220 /// Synchronizes the controlled character sequence (the buffers) with the
221 /// associated character sequence. Operation has no effect, because the
222 /// stream is always kept in sync (no buffered output). Return 0.
223 int pubsync();
224
225 // *** 27.5.2.2.5 Put area: ***
226
227 /// Write the specified character `c` to this buffer. Return `c`, or
228 /// `traits_type::eof()` if the end of the write buffer is reached.
229 int_type sputc(char c);
230
231 /// Write the specified `length` characters at the specified address `s` to
232 /// this buffer. Return the number of characters written, which is either
233 /// `length` or the distance from the current write position to the end of
234 /// the write buffer, whichever is smaller, and move the write cursor
235 /// position by this amount.
236 bsl::streamsize sputn(const char *s, bsl::streamsize length);
237
238 // ACCESSORS
239
240 /// Return the size in bytes of the buffer held by this stream buffer.
241 bsl::streamsize capacity() const;
242
243 /// Return a pointer providing non-modifiable access to the character
244 /// buffer held by this stream buffer (supplied at construction).
245 const char *data() const;
246
247 /// Return the number of characters from the beginning of the buffer to the
248 /// current write position.
249 bsl::streamsize length() const;
250
251 // *** 27.5.2.2.1 locales: ***
252
253 /// Return the current default locale. Operation has no effect, because
254 /// locales are not supported by this component. Return default
255 /// constructed bsl::locale object.
256 bsl::locale getloc() const;
257
258};
259
260// ============================================================================
261// INLINE DEFINITIONS
262// ============================================================================
263
264 // --------------
265 // FixedMemOutput
266 // --------------
267
268// CREATORS
269inline
270FixedMemOutput::FixedMemOutput(char *buffer,
271 bsl::streamsize length)
272: d_buffer_p(buffer)
273, d_capacity(length)
274, d_pos(0)
275{
276 BSLS_ASSERT(buffer || 0 == length);
277 BSLS_ASSERT(0 <= length);
278}
279
280// MANIPULATORS
281inline
283{
284 return d_buffer_p;
285}
286
287inline
288bsl::locale FixedMemOutput::pubimbue(const bsl::locale&)
289{
290 return bsl::locale();
291}
292
293inline
295 bsl::streamsize length)
296{
297 BSLS_ASSERT(buffer || 0 == length);
298 BSLS_ASSERT(0 <= length);
299
300 d_buffer_p = buffer;
301 d_capacity = length;
302 d_pos = 0;
303
304 return this;
305}
306
307inline
309{
310 // Nothing to do, the buffer is always up to date.
311 return 0;
312}
313
314inline
316{
317 if (d_pos >= d_capacity) {
318 return traits_type::eof(); // RETURN
319 }
320 d_buffer_p[static_cast<IntPtr>(d_pos)] = c;
321 d_pos += 1;
322 return traits_type::to_int_type(c);
323}
324
325inline
326bsl::streamsize FixedMemOutput::sputn(const char *s,
327 bsl::streamsize length)
328{
329 BSLS_ASSERT(s);
330
331 length = bsl::min<bsl::streamsize>(
332 length,
333 d_capacity - static_cast<bsl::streamsize>(d_pos));
334 if (0 < length) {
335 bsl::memcpy(d_buffer_p + static_cast<IntPtr>(d_pos), s, length);
336 d_pos += length;
337 }
338 else {
339 BSLS_ASSERT(0 == length);
340 }
341
342 return length;
343}
344
345// ACCESSORS
346inline
347bsl::streamsize FixedMemOutput::capacity() const
348{
349 return d_capacity;
350}
351
352inline
353const char *FixedMemOutput::data() const
354{
355 return d_buffer_p;
356}
357
358inline
359bsl::locale FixedMemOutput::getloc() const
360{
361 return bsl::locale();
362}
363
364inline
365bsl::streamsize FixedMemOutput::length() const
366{
367 return bsl::streamsize(d_pos);
368}
369
370} // close package namespace
371
372
373#endif
374
375// ----------------------------------------------------------------------------
376// Copyright 2015 Bloomberg Finance L.P.
377//
378// Licensed under the Apache License, Version 2.0 (the "License");
379// you may not use this file except in compliance with the License.
380// You may obtain a copy of the License at
381//
382// http://www.apache.org/licenses/LICENSE-2.0
383//
384// Unless required by applicable law or agreed to in writing, software
385// distributed under the License is distributed on an "AS IS" BASIS,
386// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
387// See the License for the specific language governing permissions and
388// limitations under the License.
389// ----------------------------- END-OF-FILE ----------------------------------
390
391/** @} */
392/** @} */
393/** @} */
Definition bdlsb_fixedmemoutput.h:138
int_type sputc(char c)
Definition bdlsb_fixedmemoutput.h:315
bsl::locale pubimbue(const bsl::locale &loc)
Definition bdlsb_fixedmemoutput.h:288
bsl::locale getloc() const
Definition bdlsb_fixedmemoutput.h:359
bsl::streamsize capacity() const
Return the size in bytes of the buffer held by this stream buffer.
Definition bdlsb_fixedmemoutput.h:347
bsl::streamsize sputn(const char *s, bsl::streamsize length)
Definition bdlsb_fixedmemoutput.h:326
char char_type
Definition bdlsb_fixedmemoutput.h:142
bsl::char_traits< char > traits_type
Definition bdlsb_fixedmemoutput.h:146
FixedMemOutput * pubsetbuf(char *buffer, bsl::streamsize length)
Definition bdlsb_fixedmemoutput.h:294
bsl::char_traits< char >::int_type int_type
Definition bdlsb_fixedmemoutput.h:143
pos_type pubseekoff(off_type offset, bsl::ios_base::seekdir fixedPosition, bsl::ios_base::openmode which=bsl::ios_base::in|bsl::ios_base::out)
~FixedMemOutput()=default
Destroy this object.
bsl::char_traits< char >::off_type off_type
Definition bdlsb_fixedmemoutput.h:145
bsl::streamsize length() const
Definition bdlsb_fixedmemoutput.h:365
char * data()
Definition bdlsb_fixedmemoutput.h:282
pos_type pubseekpos(pos_type position, bsl::ios_base::openmode which=bsl::ios_base::in|bsl::ios_base::out)
bsl::char_traits< char >::pos_type pos_type
Definition bdlsb_fixedmemoutput.h:144
int pubsync()
Definition bdlsb_fixedmemoutput.h:308
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlsb_fixedmeminput.h:145
std::ptrdiff_t IntPtr
Definition bsls_types.h:132