BDE 4.14.0 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`). Note
134/// that this class is not designed to be derived from.
135///
136/// See @ref bdlsb_fixedmemoutput
138
139 public:
140 // TYPES
141 typedef char char_type;
142 typedef bsl::char_traits<char>::int_type int_type;
143 typedef bsl::char_traits<char>::pos_type pos_type;
144 typedef bsl::char_traits<char>::off_type off_type;
145 typedef bsl::char_traits<char> traits_type;
146
147 private:
148 // PRIVATE TYPE
149 typedef bsls::Types::IntPtr IntPtr;
150
151 // PRIVATE DATA MEMBERS
152 char *d_buffer_p; // output buffer
153 bsl::streamsize d_capacity; // length of output buffer
154 pos_type d_pos; // output cursor
155
156 // NOT IMPLEMENTED
158 FixedMemOutput& operator=(const FixedMemOutput&);
159
160 public:
161 // CREATORS
162
163 /// Create an empty stream buffer that uses the specified character
164 /// `buffer` of the specified `length`. The behavior is undefined
165 /// unless `length == 0` or `length > 0 && buffer != 0`.
166 /// Note that `buffer` is held but not owned.
167 FixedMemOutput(char *buffer, bsl::streamsize length);
168
169 /// Destroy this object.
170 ~FixedMemOutput() = default;
171
172 // MANIPULATORS
173
174 /// Return a pointer providing modifiable access to the character buffer
175 /// held by this stream buffer (supplied at construction).
176 char *data();
177
178 // *** 27.5.2.2.1 locales: ***
179
180 /// Associate the specified locale `loc` to this stream buffer. Operation
181 /// has no effect, because locales are not supported by this component.
182 /// Return default constructed bsl::locale object.
183 bsl::locale pubimbue(const bsl::locale& loc);
184
185 // *** 27.5.2.2.2 buffer and positioning: ***
186
187 /// Reset the internal buffer of this stream to the specified `buffer` of
188 /// the specified `length`. Note that the next write operation will start
189 /// at the beginning of `buffer`.
190 FixedMemOutput *pubsetbuf(char *buffer,
191 bsl::streamsize length);
192
193 /// Set the position indicator to the relative specified `offset` from the
194 /// base position indicated by the specified `fixedPosition` and return the
195 /// resulting absolute position on success or pos_type(-1) on failure.
196 /// Optionally specify `which` area of the stream buffer. The seek
197 /// operation will fail if `which` does not include the flag
198 /// `bsl::ios_base::out` or if the resulting absolute position is less than
199 /// zero or greater than the value returned by `length`.
201 bsl::ios_base::seekdir fixedPosition,
202 bsl::ios_base::openmode which =
203 bsl::ios_base::in | bsl::ios_base::out);
204
205 /// Set the position indicator to the specified `position` and return the
206 /// resulting absolute position on success or pos_type(-1) on failure.
207 /// Optionally specify `which` area of the stream buffer. The `seekpos`
208 /// operation will fail if `which` does not include the flag
209 /// `bsl::ios_base::out` or if position is less then zero or greater than
210 /// the value returned by `length`.
212 bsl::ios_base::openmode which =
213 bsl::ios_base::in | bsl::ios_base::out);
214
215 /// Synchronizes the controlled character sequence (the buffers) with the
216 /// associated character sequence. Operation has no effect, because the
217 /// stream is always kept in sync (no buffered output). Return 0.
218 int pubsync();
219
220 // *** 27.5.2.2.5 Put area: ***
221
222 /// Write the specified character `c` to this buffer. Return `c`, or
223 /// `traits_type::eof()` if the end of the write buffer is reached.
224 int_type sputc(char c);
225
226 /// Write the specified `length` characters at the specified address `s` to
227 /// this buffer. Return the number of characters written, which is either
228 /// `length` or the distance from the current write position to the end of
229 /// the write buffer, whichever is smaller, and move the write cursor
230 /// position by this amount.
231 bsl::streamsize sputn(const char *s, bsl::streamsize length);
232
233 // ACCESSORS
234
235 /// Return the size in bytes of the buffer held by this stream buffer.
236 bsl::streamsize capacity() const;
237
238 /// Return a pointer providing non-modifiable access to the character
239 /// buffer held by this stream buffer (supplied at construction).
240 const char *data() const;
241
242 /// Return the number of characters from the beginning of the buffer to the
243 /// current write position.
244 bsl::streamsize length() const;
245
246 // *** 27.5.2.2.1 locales: ***
247
248 /// Return the current default locale. Operation has no effect, because
249 /// locales are not supported by this component. Return default
250 /// constructed bsl::locale object.
251 bsl::locale getloc() const;
252
253};
254
255// ============================================================================
256// INLINE DEFINITIONS
257// ============================================================================
258
259 // --------------
260 // FixedMemOutput
261 // --------------
262
263// CREATORS
264inline
265FixedMemOutput::FixedMemOutput(char *buffer,
266 bsl::streamsize length)
267: d_buffer_p(buffer)
268, d_capacity(length)
269, d_pos(0)
270{
271 BSLS_ASSERT(buffer || 0 == length);
272 BSLS_ASSERT(0 <= length);
273}
274
275// MANIPULATORS
276inline
278{
279 return d_buffer_p;
280}
281
282inline
283bsl::locale FixedMemOutput::pubimbue(const bsl::locale&)
284{
285 return bsl::locale();
286}
287
288inline
290 bsl::streamsize length)
291{
292 BSLS_ASSERT(buffer || 0 == length);
293 BSLS_ASSERT(0 <= length);
294
295 d_buffer_p = buffer;
296 d_capacity = length;
297 d_pos = 0;
298
299 return this;
300}
301
302inline
304{
305 // Nothing to do, the buffer is always up to date.
306 return 0;
307}
308
309inline
311{
312 if (d_pos >= d_capacity) {
313 return traits_type::eof(); // RETURN
314 }
315 d_buffer_p[static_cast<IntPtr>(d_pos)] = c;
316 d_pos += 1;
317 return traits_type::to_int_type(c);
318}
319
320inline
321bsl::streamsize FixedMemOutput::sputn(const char *s,
322 bsl::streamsize length)
323{
324 BSLS_ASSERT(s);
325
326 length = bsl::min<bsl::streamsize>(
327 length,
328 d_capacity - static_cast<bsl::streamsize>(d_pos));
329 if (0 < length) {
330 bsl::memcpy(d_buffer_p + static_cast<IntPtr>(d_pos), s, length);
331 d_pos += length;
332 }
333 else {
334 BSLS_ASSERT(0 == length);
335 }
336
337 return length;
338}
339
340// ACCESSORS
341inline
342bsl::streamsize FixedMemOutput::capacity() const
343{
344 return d_capacity;
345}
346
347inline
348const char *FixedMemOutput::data() const
349{
350 return d_buffer_p;
351}
352
353inline
354bsl::locale FixedMemOutput::getloc() const
355{
356 return bsl::locale();
357}
358
359inline
360bsl::streamsize FixedMemOutput::length() const
361{
362 return bsl::streamsize(d_pos);
363}
364
365} // close package namespace
366
367
368#endif
369
370// ----------------------------------------------------------------------------
371// Copyright 2015 Bloomberg Finance L.P.
372//
373// Licensed under the Apache License, Version 2.0 (the "License");
374// you may not use this file except in compliance with the License.
375// You may obtain a copy of the License at
376//
377// http://www.apache.org/licenses/LICENSE-2.0
378//
379// Unless required by applicable law or agreed to in writing, software
380// distributed under the License is distributed on an "AS IS" BASIS,
381// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
382// See the License for the specific language governing permissions and
383// limitations under the License.
384// ----------------------------- END-OF-FILE ----------------------------------
385
386/** @} */
387/** @} */
388/** @} */
Definition bdlsb_fixedmemoutput.h:137
int_type sputc(char c)
Definition bdlsb_fixedmemoutput.h:310
bsl::locale pubimbue(const bsl::locale &loc)
Definition bdlsb_fixedmemoutput.h:283
bsl::locale getloc() const
Definition bdlsb_fixedmemoutput.h:354
bsl::streamsize capacity() const
Return the size in bytes of the buffer held by this stream buffer.
Definition bdlsb_fixedmemoutput.h:342
bsl::streamsize sputn(const char *s, bsl::streamsize length)
Definition bdlsb_fixedmemoutput.h:321
char char_type
Definition bdlsb_fixedmemoutput.h:141
bsl::char_traits< char > traits_type
Definition bdlsb_fixedmemoutput.h:145
FixedMemOutput * pubsetbuf(char *buffer, bsl::streamsize length)
Definition bdlsb_fixedmemoutput.h:289
bsl::char_traits< char >::int_type int_type
Definition bdlsb_fixedmemoutput.h:142
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:144
bsl::streamsize length() const
Definition bdlsb_fixedmemoutput.h:360
char * data()
Definition bdlsb_fixedmemoutput.h:277
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:143
int pubsync()
Definition bdlsb_fixedmemoutput.h:303
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlsb_fixedmeminput.h:145
std::ptrdiff_t IntPtr
Definition bsls_types.h:130