BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlbb_blobstreambuf.h
Go to the documentation of this file.
1/// @file bdlbb_blobstreambuf.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlbb_blobstreambuf.h -*-C++-*-
8#ifndef INCLUDED_BDLBB_BLOBSTREAMBUF
9#define INCLUDED_BDLBB_BLOBSTREAMBUF
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlbb_blobstreambuf bdlbb_blobstreambuf
15/// @brief Provide blob implementing the `streambuf` interface.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlbb
19/// @{
20/// @addtogroup bdlbb_blobstreambuf
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlbb_blobstreambuf-purpose"> Purpose</a>
25/// * <a href="#bdlbb_blobstreambuf-classes"> Classes </a>
26/// * <a href="#bdlbb_blobstreambuf-description"> Description </a>
27///
28/// # Purpose {#bdlbb_blobstreambuf-purpose}
29/// Provide blob implementing the `streambuf` interface.
30///
31/// # Classes {#bdlbb_blobstreambuf-classes}
32///
33/// - bdlbb::InBlobStreamBuf: `bdlbb::Blob` input `streambuf`
34/// - bdlbb::OutBlobStreamBuf: `bdlbb::Blob` output `streambuf`
35///
36/// @see bdlbb_blob
37///
38/// # Description {#bdlbb_blobstreambuf-description}
39/// This component implements the input and output
40/// `bsl::basic_streambuf` protocol using a user-supplied `bdlbb::Blob`. Method
41/// names necessarily correspond to the protocol-specified method names. Refer
42/// to the C++ Standard, Section 27.5.2, for a full specification of the
43/// interface.
44///
45/// A `bdlbb::Blob` is an indexed sequence of `bdlbb::BlobBuffer` of potentially
46/// different sizes. The number of buffers in the sequence can increase or
47/// decrease, but the order of the buffers cannot change. Therefore, the blob
48/// behaves logically as a single indexed buffer. `bdlbb::InBlobStreamBuf` and
49/// `bdlbb::OutBlobStreamBuf` can therefore respectively read from and write to
50/// this buffer as if there were a single continuous index.
51/// @}
52/** @} */
53/** @} */
54
55/** @addtogroup bdl
56 * @{
57 */
58/** @addtogroup bdlbb
59 * @{
60 */
61/** @addtogroup bdlbb_blobstreambuf
62 * @{
63 */
64
65#include <bdlscm_version.h>
66
67#include <bdlbb_blob.h>
68
69#include <bsls_assert.h>
70#include <bsls_keyword.h>
71#include <bsls_review.h>
72
73#include <bsl_ios.h> // for 'bsl::streamsize'
74#include <bsl_streambuf.h>
75#include <bsl_cstddef.h> // bsl::size_t
76
77
78namespace bdlbb { class Blob; }
79namespace bdlbb {
80
81 // =====================
82 // class InBlobStreamBuf
83 // =====================
84
85/// This class implements the input functionality of the @ref basic_streambuf
86/// protocol, using a client-supplied `bdlbb::Blob`.
87///
88/// See @ref bdlbb_blobstreambuf
89class InBlobStreamBuf : public bsl::streambuf {
90
91 // PRIVATE TYPES
92 typedef bsl::ios_base ios_base;
93
94 // DATA
95 const bdlbb::Blob *d_blob_p; // "streamed" blob (held)
96 int d_getBufferIndex; // index of current buffer
97 int d_previousBuffersLength; // length of buffers before the
98 // current one
99
100 // NOT IMPLEMENTED
102 InBlobStreamBuf& operator=(const InBlobStreamBuf&);
103
104 private:
105 // PRIVATE MANIPULATORS
106
107 /// Set the current location to the specified `position`.
108 void setGetPosition(bsl::size_t position);
109
110 // PRIVATE ACCESSORS
111
112 /// Check this object's invariant.
113 int checkInvariant() const;
114
115 protected:
116 // PROTECTED VIRTUAL FUNCTIONS
117
118 /// Return `traits_type::eof()` unconditionally.
119 int_type overflow(int_type c = bsl::streambuf::traits_type::eof())
121
122 /// Adjust the underlying blob and put the optionally specified
123 /// character `c` at the newly valid `gptr()`. Return `c` (or
124 /// `~traits_type::eof` if `c == traits_type::eof`) on success, and
125 /// `traits_type::eof()` otherwise.
126 int_type pbackfail(int_type c = bsl::streambuf::traits_type::eof())
128
129 /// Set the location from which the next I/O operation indicated by the
130 /// optionally specified `which` mode will occur to the specified
131 /// `offset` position from the location indicated by the specified
132 /// `fixedPosition`. Return the new offset on success, and
133 /// `off_type(-1)` otherwise. `offset` may be negative. Note that this
134 /// method will fail if `bsl::ios_base::out` is set.
135 pos_type seekoff(
136 off_type offset,
137 bsl::ios_base::seekdir fixedPosition,
138 bsl::ios_base::openmode which = bsl::ios_base::in | bsl::ios_base::out)
140
141 /// Set the location from which the next I/O operation indicated by the
142 /// optionally specified `which` mode will occur to the specified
143 /// `position`. Return `position` on success, and `off_type(-1)`
144 /// otherwise. Note that this method will fail if `bsl::ios_base::out`
145 /// is set.
146 pos_type seekpos(
147 pos_type position,
148 bsl::ios_base::openmode which = bsl::ios_base::in | bsl::ios_base::out)
150
151 /// Return the number of characters currently available for reading from
152 /// this stream buffer, or 0 if there are none.
154
155 /// Return 0 unconditionally.
157
158 /// Adjust the underlying blob so that the next read position is valid.
159 /// Return the character at `gptr()` on success and `traits_type::eof()`
160 /// otherwise.
162
163 /// Read the specified `numChars` to the specified `destination`.
164 /// Return the number of characters successfully read. The behavior is
165 /// undefined unless 0 <= `numChars`.
166 bsl::streamsize xsgetn(char_type *destination,
167 bsl::streamsize numChars) BSLS_KEYWORD_OVERRIDE;
168
169 /// Return 0 unconditionally.
170 bsl::streamsize xsputn(const char_type *source,
171 bsl::streamsize numChars) BSLS_KEYWORD_OVERRIDE;
172
173 public:
174 // CREATORS
175
176 /// Create a `BlobStreamBuf` using the specified `blob`. The behavior
177 /// is undefined unless `blob` remains valid and externally unmodified
178 /// for the lifetime of this `streambuf`.
179 explicit InBlobStreamBuf(const bdlbb::Blob *blob);
180
181 /// Destroy this stream buffer.
183
184 // MANIPULATORS
185
186 /// Reset the get areas. Optionally set the underlying `bdlbb::Blob`
187 /// value to the optionally specified `blob` if `blob` is not 0. The
188 /// behavior is undefined unless `blob` remains valid and externally
189 /// unmodified for the lifetime of this `streambuf`.
190 void reset(const bdlbb::Blob *blob = 0);
191
192 // ACCESSORS
193
194 /// Return the index of the current buffer. The behavior is undefined
195 /// unless the "streamed" blob has at least one buffer.
196 int currentBufferIndex() const;
197
198 /// Return the address of the blob held by this stream buffer.
199 const bdlbb::Blob *data() const;
200
201 /// Return the number of bytes contained in the buffers located before
202 /// the current one. The behavior is undefined unless the "streamed"
203 /// blob has at least one buffer.
204 int previousBuffersLength() const;
205};
206
207 // ======================
208 // class OutBlobStreamBuf
209 // ======================
210
211/// This class implements the output functionality of the @ref basic_streambuf
212/// protocol, using a client-supplied `bdlbb::Blob`.
213///
214/// See @ref bdlbb_blobstreambuf
215class OutBlobStreamBuf : public bsl::streambuf {
216
217 // PRIVATE TYPES
218 typedef bsl::ios_base ios_base;
219
220 // DATA
221 bdlbb::Blob *d_blob_p; // "streamed" blob (held)
222 int d_putBufferIndex; // index of current buffer
223 int d_previousBuffersLength; // length of buffers before
224
225 // NOT IMPLEMENTED
227 OutBlobStreamBuf& operator=(const OutBlobStreamBuf&);
228
229 private:
230 // PRIVATE MANIPULATORS
231
232 /// Set the current location to the specified `position`.
233 void setPutPosition(bsl::size_t position);
234
235 // PRIVATE ACCESSORS
236
237 /// Check this object's invariants and return 0.
238 int checkInvariant() const;
239
240 protected:
241 // PROTECTED VIRTUAL FUNCTIONS
242
243 /// Append the optionally specified character `c` to this streambuf, and
244 /// return `c`. By default, `traits_type::eof()` is appended.
245 int_type overflow(int_type c = bsl::streambuf::traits_type::eof())
247
248 /// Return `traits_type::eof()` unconditionally.
249 int_type pbackfail(int_type c = bsl::streambuf::traits_type::eof())
251
252 /// Set the location from which the next I/O operation indicated by the
253 /// optionally specified `which` mode will occur to the specified
254 /// `offset` position from the location indicated by the specified
255 /// `fixedPosition`. Return the new offset on success, and
256 /// `off_type(-1)` otherwise. `offset` may be negative. Note that this
257 /// method will fail if `bsl::ios_base::in` is set.
258 pos_type seekoff(
259 off_type offset,
260 bsl::ios_base::seekdir fixedPosition,
261 bsl::ios_base::openmode which = bsl::ios_base::in | bsl::ios_base::out)
263
264 /// Set the location from which the next I/O operation indicated by the
265 /// optionally specified `which` mode will occur to the specified
266 /// `position`. Return `position` on success, and `off_type(-1)`
267 /// otherwise. Note that this method will fail if `bsl::ios_base::in`
268 /// is set.
269 pos_type seekpos(
270 pos_type position,
271 bsl::ios_base::openmode which = bsl::ios_base::in | bsl::ios_base::out)
273
274 /// Return 0 unconditionally.
276
277 /// Synchronize the put position in the blob of this stream buffer.
278 /// Return 0 unconditionally.
280
281 /// Return `traits_type::eof()` unconditionally.
283
284 /// Return 0 unconditionally.
285 bsl::streamsize xsgetn(char_type *destination,
286 bsl::streamsize numChars) BSLS_KEYWORD_OVERRIDE;
287
288 /// Copy the specified `numChars` from the specified `source` to the
289 /// blob held by this streambuf, starting at the current put area
290 /// location. The behavior is undefined unless 0 <= `numChars`.
291 bsl::streamsize xsputn(const char_type *source,
292 bsl::streamsize numChars) BSLS_KEYWORD_OVERRIDE;
293
294 public:
295 // CREATORS
296
297 /// Create a `OutBlobStreamBuf` using the specified `blob`, and set the
298 /// location at which the next write operation will occur to
299 /// `blob->length()`. The behavior is undefined unless `blob` remains
300 /// valid and externally unmodified for the lifetime of this
301 /// `streambuf`.
302 explicit OutBlobStreamBuf(bdlbb::Blob *blob);
303
304 /// Destroy this stream buffer.
306
307 // MANIPULATORS
308
309 /// Return the address of the blob held by this stream buffer.
310 bdlbb::Blob *data();
311
312 /// Reset the put position of this buffer to the first location,
313 /// available for writing in the underlying `bdlbb::Blob`. Optionally
314 /// specify a `blob` used to change current underlying `bdlbb::Blob`
315 /// value for. The behavior is undefined unless `blob` remains valid
316 /// and externally unmodified for the lifetime of this `streambuf`.
317 void reset(bdlbb::Blob *blob = 0);
318
319 // ACCESSORS
320
321 /// Return the index of the current buffer. The behavior is undefined
322 /// unless the "streamed" blob has at least one buffer.
323 int currentBufferIndex() const;
324
325 /// Return the address of the blob held by this stream buffer.
326 const bdlbb::Blob *data() const;
327
328 /// Return the number of bytes contained in the buffers located before
329 /// the current one. The behavior is undefined unless the "streamed"
330 /// blob has at least one buffer.
331 int previousBuffersLength() const;
332};
333
334// ============================================================================
335// INLINE DEFINITIONS
336// ============================================================================
337
338 // =====================
339 // class InBlobStreamBuf
340 // =====================
341
342// MANIPULATORS
343inline
344void InBlobStreamBuf::reset(const bdlbb::Blob *blob)
345{
346 if (blob) {
347 d_blob_p = blob;
348 d_getBufferIndex = 0;
349 d_previousBuffersLength = 0;
350 setg(0, 0, 0);
351 if (0 == d_blob_p->length()) {
352 return; // RETURN
353 }
354 }
355 setGetPosition(0);
356}
357
358// ACCESSORS
359inline
361{
362 BSLS_ASSERT(d_getBufferIndex < d_blob_p->numBuffers());
363 return d_getBufferIndex;
364}
365
366inline
368{
369 return d_blob_p;
370}
371
372inline
374{
375 return d_previousBuffersLength;
376}
377
378 // ======================
379 // class OutBlobStreamBuf
380 // ======================
381
382// MANIPULATORS
383inline
385{
386 return d_blob_p;
387}
388
389inline
391{
392 if (blob) {
393 d_blob_p = blob;
394 d_putBufferIndex = 0;
395 d_previousBuffersLength = 0;
396 setp(0, 0);
397 if (0 == d_blob_p->totalSize()) {
398 return; // RETURN
399 }
400 }
401 setPutPosition(d_blob_p->length());
402}
403
404// ACCESSORS
405inline
407{
408 BSLS_ASSERT(d_putBufferIndex < d_blob_p->numBuffers());
409 return d_putBufferIndex;
410}
411
412inline
414{
415 return d_blob_p;
416}
417
418inline
420{
421 return d_previousBuffersLength;
422}
423} // close package namespace
424
425
426
427#endif
428
429// ----------------------------------------------------------------------------
430// Copyright 2018 Bloomberg Finance L.P.
431//
432// Licensed under the Apache License, Version 2.0 (the "License");
433// you may not use this file except in compliance with the License.
434// You may obtain a copy of the License at
435//
436// http://www.apache.org/licenses/LICENSE-2.0
437//
438// Unless required by applicable law or agreed to in writing, software
439// distributed under the License is distributed on an "AS IS" BASIS,
440// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
441// See the License for the specific language governing permissions and
442// limitations under the License.
443// ----------------------------- END-OF-FILE ----------------------------------
444
445/** @} */
446/** @} */
447/** @} */
Definition bdlbb_blob.h:642
int length() const
Return the length of this blob.
Definition bdlbb_blob.h:1220
int totalSize() const
Definition bdlbb_blob.h:1238
Definition bdlbb_blobstreambuf.h:89
void reset(const bdlbb::Blob *blob=0)
Definition bdlbb_blobstreambuf.h:344
bsl::streamsize xsgetn(char_type *destination, bsl::streamsize numChars) BSLS_KEYWORD_OVERRIDE
int_type pbackfail(int_type c=bsl::streambuf::traits_type::eof()) BSLS_KEYWORD_OVERRIDE
const bdlbb::Blob * data() const
Return the address of the blob held by this stream buffer.
Definition bdlbb_blobstreambuf.h:367
int_type overflow(int_type c=bsl::streambuf::traits_type::eof()) BSLS_KEYWORD_OVERRIDE
Return traits_type::eof() unconditionally.
int sync() BSLS_KEYWORD_OVERRIDE
Return 0 unconditionally.
bsl::streamsize showmanyc() BSLS_KEYWORD_OVERRIDE
pos_type seekoff(off_type offset, bsl::ios_base::seekdir fixedPosition, bsl::ios_base::openmode which=bsl::ios_base::in|bsl::ios_base::out) BSLS_KEYWORD_OVERRIDE
pos_type seekpos(pos_type position, bsl::ios_base::openmode which=bsl::ios_base::in|bsl::ios_base::out) BSLS_KEYWORD_OVERRIDE
int_type underflow() BSLS_KEYWORD_OVERRIDE
int currentBufferIndex() const
Definition bdlbb_blobstreambuf.h:360
int previousBuffersLength() const
Definition bdlbb_blobstreambuf.h:373
bsl::streamsize xsputn(const char_type *source, bsl::streamsize numChars) BSLS_KEYWORD_OVERRIDE
Return 0 unconditionally.
Definition bdlbb_blobstreambuf.h:215
int currentBufferIndex() const
Definition bdlbb_blobstreambuf.h:406
int previousBuffersLength() const
Definition bdlbb_blobstreambuf.h:419
int_type overflow(int_type c=bsl::streambuf::traits_type::eof()) BSLS_KEYWORD_OVERRIDE
void reset(bdlbb::Blob *blob=0)
Definition bdlbb_blobstreambuf.h:390
bdlbb::Blob * data()
Return the address of the blob held by this stream buffer.
Definition bdlbb_blobstreambuf.h:384
#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 bdlbb_blob.h:442
Definition bdlb_printmethods.h:283