BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslx_testoutstream.h
Go to the documentation of this file.
1/// @file bslx_testoutstream.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslx_testoutstream.h -*-C++-*-
8#ifndef INCLUDED_BSLX_TESTOUTSTREAM
9#define INCLUDED_BSLX_TESTOUTSTREAM
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslx_testoutstream bslx_testoutstream
15/// @brief Enable externalization of fundamental types with identification.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslx
19/// @{
20/// @addtogroup bslx_testoutstream
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslx_testoutstream-purpose"> Purpose</a>
25/// * <a href="#bslx_testoutstream-classes"> Classes </a>
26/// * <a href="#bslx_testoutstream-description"> Description </a>
27/// * <a href="#bslx_testoutstream-versioning"> Versioning </a>
28/// * <a href="#bslx_testoutstream-usage"> Usage </a>
29/// * <a href="#bslx_testoutstream-example-1-basic-externalization"> Example 1: Basic Externalization </a>
30///
31/// # Purpose {#bslx_testoutstream-purpose}
32/// Enable externalization of fundamental types with identification.
33///
34/// # Classes {#bslx_testoutstream-classes}
35///
36/// - bslx::TestOutStream: byte-array-based output stream for fundamental types
37///
38/// @see bslx_testinstream, bslx_byteoutstream
39///
40/// # Description {#bslx_testoutstream-description}
41/// This component implements a byte-array-based output stream
42/// class, `bslx::TestOutStream`, that provides platform-independent output
43/// methods ("externalization") on values, and arrays of values, of fundamental
44/// types, and on `bsl::string`. This component also externalizes information
45/// to the stream that can be used by the reader of the stream to verify, for
46/// these types, that the type of data requested from the input stream matches
47/// what was written by this output stream. This component is meant for testing
48/// only.
49///
50/// This component is intended to be used in conjunction with the
51/// @ref bslx_testinstream "unexternalization" component. Each output method of
52/// `bslx::TestOutStream` writes a value or a homogeneous array of values to an
53/// internally managed buffer. The values are formatted to be readable by the
54/// corresponding `bslx::TestInStream` method. In general, the user cannot rely
55/// on any other mechanism to read data written by `bslx::TestOutStream` unless
56/// that mechanism explicitly states its ability to do so.
57///
58/// The supported types and required content are listed in the `bslx`
59/// package-level documentation under "Supported Types".
60///
61/// Note that the values are stored in big-endian format (i.e., network byte
62/// order).
63///
64/// Note that output streams can be *invalidated* explicitly and queried for
65/// *validity*. Writing to an initially invalid stream has no effect. Whenever
66/// an output operation fails, the stream should be invalidated explicitly.
67///
68/// ## Versioning {#bslx_testoutstream-versioning}
69///
70///
71/// BDEX provides two concepts that support versioning the BDEX serialization
72/// format of a type: `version` and `versionSelector`. A `version` is a 1-based
73/// integer indicating one of the supported formats (e.g., format 1, format 2,
74/// etc.). A `versionSelector` is a value that is mapped to a `version` for a
75/// type by the type's implementation of `maxSupportedBdexVersion`.
76///
77/// Selecting a value for a `versionSelector` is required at two different
78/// points: (1) when implementing a new `version` format within the
79/// `bdexStreamIn` and `bdexStreamOut` methods of a type, and (2) when
80/// implementing code that constructs a BDEX `OutStream`. In both cases, the
81/// value should be a *compile*-time-selected value.
82///
83/// When a new `version` format is implemented within the `bdexStreamIn` and
84/// `bdexStreamOut` methods of a type, a new mapping in
85/// `maxSupportedBdexVersion` should be created to expose this new `version`
86/// with a `versionSelector`. A simple - and the recommended - approach is to
87/// use a value having the pattern "YYYYMMDD", where "YYYYMMDD" corresponds to
88/// the "go-live" date of the corresponding `version` format.
89///
90/// When constructing an `OutStream`, a simple approach is to use the current
91/// date as a *compile*-time constant value. In combination with the
92/// recommended selection of `versionSelector` values for
93/// `maxSupportedBdexVersion`, this will result in consistent and predictable
94/// behavior while externalizing types. Note that this recommendation is chosen
95/// for its simplicity: to ensure the largest possible audience for an
96/// externalized representation, clients can select the minimum date value that
97/// will result in the desired version of all types externalized with
98/// `operator<<` being selected.
99///
100/// See the `bslx` package-level documentation for more detailed information
101/// about versioning.
102///
103/// ## Usage {#bslx_testoutstream-usage}
104///
105///
106/// This section illustrates intended use of this component.
107///
108/// ### Example 1: Basic Externalization {#bslx_testoutstream-example-1-basic-externalization}
109///
110///
111/// A `bslx::TestOutStream` can be used to externalize values in a
112/// platform-neutral way. Writing out fundamental C++ types and `bsl::string`
113/// requires no additional work on the part of the client; the client can simply
114/// use the stream directly. The following code serializes a few representative
115/// values using a `bslx::TestOutStream`, compares the contents of this stream
116/// to the expected value, and then writes the contents of this stream's buffer
117/// to `stdout`.
118///
119/// First, we create a `bslx::TestOutStream` with an arbitrary value for its
120/// `versionSelector` and externalize some values:
121/// @code
122/// bslx::TestOutStream outStream(20131127);
123/// outStream.putInt32(1);
124/// outStream.putInt32(2);
125/// outStream.putInt8('c');
126/// outStream.putString(bsl::string("hello"));
127/// @endcode
128/// Then, we compare the contents of the stream to the expected value:
129/// @code
130/// const char *theChars = outStream.data();
131/// bsl::size_t length = outStream.length();
132/// assert(24 == length);
133/// assert( 0 == bsl::memcmp(theChars,
134/// "\xE6\x00\x00\x00\x01\xE6\x00\x00\x00\x02\xE0"
135/// "c\xE0\x05\xE1\x00\x00\x00\x05""hello",
136/// length));
137/// @endcode
138/// Finally, we print the stream's contents to `bsl::cout`.
139/// @code
140/// for (bsl::size_t i = 0; i < length; ++i) {
141/// if(bsl::isalnum(static_cast<unsigned char>(theChars[i]))) {
142/// bsl::cout << "nextByte (char): " << theChars[i] << bsl::endl;
143/// }
144/// else {
145/// bsl::cout << "nextByte (int): "
146/// << static_cast<int>(theChars[i])
147/// << bsl::endl;
148/// }
149/// }
150/// @endcode
151/// Executing the above code results in the following output:
152/// @code
153/// nextByte (int): -26
154/// nextByte (int): 0
155/// nextByte (int): 0
156/// nextByte (int): 0
157/// nextByte (int): 1
158/// nextByte (int): -26
159/// nextByte (int): 0
160/// nextByte (int): 0
161/// nextByte (int): 0
162/// nextByte (int): 2
163/// nextByte (int): -32
164/// nextByte (char): c
165/// nextByte (int): -32
166/// nextByte (int): 5
167/// nextByte (int): -31
168/// nextByte (char): h
169/// nextByte (char): e
170/// nextByte (char): l
171/// nextByte (char): l
172/// nextByte (char): o
173/// @endcode
174/// Note the negative numeric values indicate the "type" of the data that
175/// follows (see @ref bslx_typecode ).
176///
177/// See the @ref bslx_testinstream component usage example for a more practical
178/// example of using this test output stream.
179/// @}
180/** @} */
181/** @} */
182
183/** @addtogroup bsl
184 * @{
185 */
186/** @addtogroup bslx
187 * @{
188 */
189/** @addtogroup bslx_testoutstream
190 * @{
191 */
192
193#include <bslscm_version.h>
194
195#include <bslx_byteoutstream.h>
197
198#include <bsls_types.h>
199
200#include <bsl_cstddef.h>
201#include <bsl_iosfwd.h>
202#include <bsl_string.h>
203
204
205
206namespace bslma { class Allocator; }
207
208namespace bslx {
209
210 // ===================
211 // class TestOutStream
212 // ===================
213
214/// This class implements output methods to externalize fundamental types
215/// and their associated type identification data. It stores the
216/// accumulated result in network byte order. See the `bslx` package-level
217/// documentation for the definition of the BDEX `OutStream` protocol.
218///
219/// See @ref bslx_testoutstream
221
222 // DATA
223 ByteOutStream d_imp; // byte out stream implementation
224
225 bool d_makeNextInvalidFlag; // if 'true', next "put" operation
226 // outputs the invalid data indicator
227 // and resets this flag to 'false'
228
229 // FRIENDS
230 friend bsl::ostream& operator<<(bsl::ostream&, const TestOutStream&);
231
232 private:
233 // NOT IMPLEMENTED
235 TestOutStream& operator=(const TestOutStream&);
236
237 public:
238 // CREATORS
239
240 /// Create an empty output byte stream that will use the specified
241 /// (*compile*-time-defined) `versionSelector` as needed (see
242 /// {Versioning}). Optionally specify a `basicAllocator` used to supply
243 /// memory. If `basicAllocator` is 0, the currently installed default allocator is used.
244 ///
245 /// \note Note that the `versionSelector` is expected to
246 /// be formatted as "YYYYMMDD", a date representation.
247 explicit TestOutStream(int versionSelector,
248 bslma::Allocator *basicAllocator = 0);
249
250 /// Create an empty output byte stream having an initial buffer capacity
251 /// of at least the specified `initialCapacity` (in bytes) and that will
252 /// use the specified (*compile*-time-defined) `versionSelector` as
253 /// needed (see {Versioning}). Optionally specify a `basicAllocator`
254 /// used to supply memory. If `basicAllocator` is 0, the currently installed default allocator is used.
255 ///
256 /// \note Note that the
257 /// `versionSelector` is expected to be formatted as "YYYYMMDD", a date
258 /// representation.
259 TestOutStream(int versionSelector,
260 bsl::size_t initialCapacity,
261 bslma::Allocator *basicAllocator = 0);
262
263 /// Destroy this object.
265
266 // MANIPULATORS
267
268 /// Put this output stream in an invalid state. This function has no
269 /// effect if this stream is already invalid.
270 void invalidate();
271
272 /// Make the next output operation externalize the invalid data
273 /// indicator, as opposed to the actual type indicator, to this output
274 /// stream; the data associated with the next output operation is still externalized.
275 ///
276 /// \note Note that the invalid data indicator can be detected
277 /// by a corresponding `TestInStream` object.
278 void makeNextInvalid();
279
280 /// If the specified `length` is less than 128, write to this stream the
281 /// one-byte type indicator for a one-byte integer and the one-byte
282 /// integer comprised of the least-significant one byte of the `length`;
283 /// otherwise, write to this stream the one-byte type indicator for a
284 /// four-byte integer and the four-byte, two's complement integer (in
285 /// network byte order) comprised of the least-significant four bytes of
286 /// the `length` (in host byte order) with the most-significant bit set.
287 /// Return a reference to this stream. If this stream is initially
288 /// invalid, this operation has no effect. If the next output operation
289 /// has been set to be marked invalid (see `makeNextInvalid`), reset
290 /// this marking and emit the invalid indicator instead of the type indicator.
291 ///
292 /// \pre The behavior is undefined unless `0 <= length`.
294
295 /// Write to this stream the one-byte type indicator for a one-byte
296 /// unsigned integer and the one-byte, two's complement unsigned integer
297 /// comprised of the least-significant one byte of the specified
298 /// `version`, and return a reference to this stream. If this stream is
299 /// initially invalid, this operation has no effect. If the next output
300 /// operation has been set to be marked invalid (see `makeNextInvalid`),
301 /// reset this marking and emit the invalid indicator instead of the
302 /// type indicator.
304
305 /// Set the internal buffer size of this stream to be at least the
306 /// specified `newCapacity` (in bytes).
307 void reserveCapacity(bsl::size_t newCapacity);
308
309 /// Remove all content in this stream and validate this stream if it is
310 /// currently invalid.
311 void reset();
312
313 // *** scalar integer values ***
314
315 /// Write to this stream the one-byte type indicator for an eight-byte
316 /// integer and the eight-byte, two's complement integer (in network
317 /// byte order) comprised of the least-significant eight bytes of the
318 /// specified `value` (in host byte order), and return a reference to
319 /// this stream. If this stream is initially invalid, this operation
320 /// has no effect. If the next output operation has been set to be
321 /// marked invalid (see `makeNextInvalid`), reset this marking and emit
322 /// the invalid indicator instead of the type indicator.
324
325 /// Write to this stream the one-byte type indicator for an eight-byte
326 /// unsigned integer and the eight-byte, two's complement unsigned
327 /// integer (in network byte order) comprised of the least-significant
328 /// eight bytes of the specified `value` (in host byte order), and
329 /// return a reference to this stream. If this stream is initially
330 /// invalid, this operation has no effect. If the next output operation
331 /// has been set to be marked invalid (see `makeNextInvalid`), reset
332 /// this marking and emit the invalid indicator instead of the type
333 /// indicator.
335
336 /// Write to this stream the one-byte type indicator for a seven-byte
337 /// integer and the seven-byte, two's complement integer (in network
338 /// byte order) comprised of the least-significant seven bytes of the
339 /// specified `value` (in host byte order), and return a reference to
340 /// this stream. If this stream is initially invalid, this operation
341 /// has no effect. If the next output operation has been set to be
342 /// marked invalid (see `makeNextInvalid`), reset this marking and emit
343 /// the invalid indicator instead of the type indicator.
345
346 /// Write to this stream the one-byte type indicator for a seven-byte
347 /// unsigned integer and the seven-byte, two's complement unsigned
348 /// integer (in network byte order) comprised of the least-significant
349 /// seven bytes of the specified `value` (in host byte order), and
350 /// return a reference to this stream. If this stream is initially
351 /// invalid, this operation has no effect. If the next output operation
352 /// has been set to be marked invalid (see `makeNextInvalid`), reset
353 /// this marking and emit the invalid indicator instead of the type
354 /// indicator.
356
357 /// Write to this stream the one-byte type indicator for a six-byte
358 /// integer and the six-byte, two's complement integer (in network byte
359 /// order) comprised of the least-significant six bytes of the specified
360 /// `value` (in host byte order), and return a reference to this stream.
361 /// If this stream is initially invalid, this operation has no effect.
362 /// If the next output operation has been set to be marked invalid (see
363 /// `makeNextInvalid`), reset this marking and emit the invalid
364 /// indicator instead of the type indicator.
366
367 /// Write to this stream the one-byte type indicator for a six-byte
368 /// unsigned integer and the six-byte, two's complement unsigned integer
369 /// (in network byte order) comprised of the least-significant six bytes
370 /// of the specified `value` (in host byte order), and return a
371 /// reference to this stream. If this stream is initially invalid, this
372 /// operation has no effect. If the next output operation has been set
373 /// to be marked invalid (see `makeNextInvalid`), reset this marking and
374 /// emit the invalid indicator instead of the type indicator.
376
377 /// Write to this stream the one-byte type indicator for a five-byte
378 /// integer and the five-byte, two's complement integer (in network byte
379 /// order) comprised of the least-significant five bytes of the
380 /// specified `value` (in host byte order), and return a reference to
381 /// this stream. If this stream is initially invalid, this operation
382 /// has no effect. If the next output operation has been set to be
383 /// marked invalid (see `makeNextInvalid`), reset this marking and emit
384 /// the invalid indicator instead of the type indicator.
386
387 /// Write to this stream the one-byte type indicator for a five-byte
388 /// unsigned integer and the five-byte, two's complement unsigned
389 /// integer (in network byte order) comprised of the least-significant
390 /// five bytes of the specified `value` (in host byte order), and return
391 /// a reference to this stream. If this stream is initially invalid,
392 /// this operation has no effect. If the next output operation has been
393 /// set to be marked invalid (see `makeNextInvalid`), reset this marking
394 /// and emit the invalid indicator instead of the type indicator.
396
397 /// Write to this stream the one-byte type indicator for a four-byte
398 /// integer and the four-byte, two's complement integer (in network byte
399 /// order) comprised of the least-significant four bytes of the
400 /// specified `value` (in host byte order), and return a reference to
401 /// this stream. If this stream is initially invalid, this operation
402 /// has no effect. If the next output operation has been set to be
403 /// marked invalid (see `makeNextInvalid`), reset this marking and emit
404 /// the invalid indicator instead of the type indicator.
406
407 /// Write to this stream the one-byte type indicator for a four-byte
408 /// unsigned integer and the four-byte, two's complement unsigned
409 /// integer (in network byte order) comprised of the least-significant
410 /// four bytes of the specified `value` (in host byte order), and return
411 /// a reference to this stream. If this stream is initially invalid,
412 /// this operation has no effect. If the next output operation has been
413 /// set to be marked invalid (see `makeNextInvalid`), reset this marking
414 /// and emit the invalid indicator instead of the type indicator.
415 TestOutStream& putUint32(unsigned int value);
416
417 /// Write to this stream the one-byte type indicator for a three-byte
418 /// integer and the three-byte, two's complement integer (in network
419 /// byte order) comprised of the least-significant three bytes of the
420 /// specified `value` (in host byte order), and return a reference to
421 /// this stream. If this stream is initially invalid, this operation
422 /// has no effect. If the next output operation has been set to be
423 /// marked invalid (see `makeNextInvalid`), reset this marking and emit
424 /// the invalid indicator instead of the type indicator.
426
427 /// Write to this stream the one-byte type indicator for a three-byte
428 /// unsigned integer and the three-byte, two's complement unsigned
429 /// integer (in network byte order) comprised of the least-significant
430 /// three bytes of the specified `value` (in host byte order), and
431 /// return a reference to this stream. If this stream is initially
432 /// invalid, this operation has no effect. If the next output operation
433 /// has been set to be marked invalid (see `makeNextInvalid`), reset
434 /// this marking and emit the invalid indicator instead of the type
435 /// indicator.
436 TestOutStream& putUint24(unsigned int value);
437
438 /// Write to this stream the one-byte type indicator for a two-byte
439 /// integer and the two-byte, two's complement integer (in network byte
440 /// order) comprised of the least-significant two bytes of the specified
441 /// `value` (in host byte order), and return a reference to this stream.
442 /// If this stream is initially invalid, this operation has no effect.
443 /// If the next output operation has been set to be marked invalid (see
444 /// `makeNextInvalid`), reset this marking and emit the invalid
445 /// indicator instead of the type indicator.
447
448 /// Write to this stream the one-byte type indicator for a two-byte
449 /// unsigned integer and the two-byte, two's complement unsigned integer
450 /// (in network byte order) comprised of the least-significant two bytes
451 /// of the specified `value` (in host byte order), and return a
452 /// reference to this stream. If this stream is initially invalid, this
453 /// operation has no effect. If the next output operation has been set
454 /// to be marked invalid (see `makeNextInvalid`), reset this marking and
455 /// emit the invalid indicator instead of the type indicator.
456 TestOutStream& putUint16(unsigned int value);
457
458 /// Write to this stream the one-byte type indicator for a one-byte
459 /// integer and the one-byte, two's complement integer comprised of the
460 /// least-significant one byte of the specified `value`, and return a
461 /// reference to this stream. If this stream is initially invalid, this
462 /// operation has no effect. If the next output operation has been set
463 /// to be marked invalid (see `makeNextInvalid`), reset this marking and
464 /// emit the invalid indicator instead of the type indicator.
466
467 /// Write to this stream the one-byte type indicator for a one-byte
468 /// unsigned integer and the one-byte, two's complement unsigned integer
469 /// comprised of the least-significant one byte of the specified
470 /// `value`, and return a reference to this stream. If this stream is
471 /// initially invalid, this operation has no effect. If the next output
472 /// operation has been set to be marked invalid (see `makeNextInvalid`),
473 /// reset this marking and emit the invalid indicator instead of the
474 /// type indicator.
475 TestOutStream& putUint8(unsigned int value);
476
477 // *** scalar floating-point values ***
478
479 /// Write to this stream the one-byte type indicator for an eight-byte
480 /// double-precision floating-point number and the eight-byte IEEE
481 /// double-precision floating-point number (in network byte order)
482 /// comprised of the most-significant eight bytes of the specified
483 /// `value` (in host byte order), and return a reference to this stream.
484 /// If this stream is initially invalid, this operation has no effect.
485 /// If the next output operation has been set to be marked invalid (see
486 /// `makeNextInvalid`), reset this marking and emit the invalid indicator instead of the type indicator.
487 ///
488 /// \note Note that for
489 /// non-conforming platforms, this operation may be lossy.
490 TestOutStream& putFloat64(double value);
491
492 /// Write to this stream the one-byte type indicator for a four-byte
493 /// single-precision floating-point number and the four-byte IEEE
494 /// single-precision floating-point number (in network byte order)
495 /// comprised of the most-significant four bytes of the specified
496 /// `value` (in host byte order), and return a reference to this stream.
497 /// If this stream is initially invalid, this operation has no effect.
498 /// If the next output operation has been set to be marked invalid (see
499 /// `makeNextInvalid`), reset this marking and emit the invalid indicator instead of the type indicator.
500 ///
501 /// \note Note that for
502 /// non-conforming platforms, this operation may be lossy.
504
505 // *** string values ***
506
507 /// Write to this stream the one-byte type indicator for a length (see
508 /// `putLength`), the length of the specified `value` (see `putLength`),
509 /// the one-byte type indicator for an array of one-byte unsigned
510 /// integers, and an array of one-byte, two's complement unsigned
511 /// integers comprised of the least-significant one byte of each
512 /// character in the `value`, and return a reference to this stream. If
513 /// this stream is initially invalid, this operation has no effect. If
514 /// the next output operation has been set to be marked invalid (see
515 /// `makeNextInvalid`), reset this marking and emit the invalid
516 /// indicator instead of the type indicator.
517 TestOutStream& putString(const bsl::string& value);
518
519 // *** arrays of integer values ***
520
521 /// Write to this stream the one-byte type indicator for an eight-byte
522 /// integer, the four-byte, two's complement integer (in network byte
523 /// order) comprised of the least-significant four bytes of the
524 /// specified `numValues` (in host byte order), and the consecutive
525 /// eight-byte, two's complement integers (in network byte order)
526 /// comprised of the least-significant eight bytes of each of the
527 /// `numValues` leading entries in the specified `values` (in host byte
528 /// order), and return a reference to this stream. If this stream is
529 /// initially invalid, this operation has no effect. If the next output
530 /// operation has been set to be marked invalid (see `makeNextInvalid`),
531 /// reset this marking and emit the invalid indicator instead of the type indicator.
532 ///
533 /// \pre The behavior is undefined unless `0 <= numValues`
534 /// and `values` has sufficient contents.
536 int numValues);
537
538 /// Write to this stream the one-byte type indicator for an eight-byte
539 /// unsigned integer, the four-byte, two's complement integer (in
540 /// network byte order) comprised of the least-significant four bytes of
541 /// the specified `numValues` (in host byte order), and the consecutive
542 /// eight-byte, two's complement unsigned integers (in network byte
543 /// order) comprised of the least-significant eight bytes of each of the
544 /// `numValues` leading entries in the specified `values` (in host byte
545 /// order), and return a reference to this stream. If this stream is
546 /// initially invalid, this operation has no effect. If the next output
547 /// operation has been set to be marked invalid (see `makeNextInvalid`),
548 /// reset this marking and emit the invalid indicator instead of the type indicator.
549 ///
550 /// \pre The behavior is undefined unless `0 <= numValues`
551 /// and `values` has sufficient contents.
553 int numValues);
554
555 /// Write to this stream the one-byte type indicator for a seven-byte
556 /// integer, the four-byte, two's complement integer (in network byte
557 /// order) comprised of the least-significant four bytes of the
558 /// specified `numValues` (in host byte order), and the consecutive
559 /// seven-byte, two's complement integers (in network byte order)
560 /// comprised of the least-significant seven bytes of each of the
561 /// `numValues` leading entries in the specified `values` (in host byte
562 /// order), and return a reference to this stream. If this stream is
563 /// initially invalid, this operation has no effect. If the next output
564 /// operation has been set to be marked invalid (see `makeNextInvalid`),
565 /// reset this marking and emit the invalid indicator instead of the type indicator.
566 ///
567 /// \pre The behavior is undefined unless `0 <= numValues`
568 /// and `values` has sufficient contents.
570 int numValues);
571
572 /// Write to this stream the one-byte type indicator for a seven-byte
573 /// unsigned integer, the four-byte, two's complement integer (in
574 /// network byte order) comprised of the least-significant four bytes of
575 /// the specified `numValues` (in host byte order), and the consecutive
576 /// seven-byte, two's complement unsigned integers (in network byte
577 /// order) comprised of the least-significant seven bytes of each of the
578 /// `numValues` leading entries in the specified `values` (in host byte
579 /// order), and return a reference to this stream. If this stream is
580 /// initially invalid, this operation has no effect. If the next output
581 /// operation has been set to be marked invalid (see `makeNextInvalid`),
582 /// reset this marking and emit the invalid indicator instead of the type indicator.
583 ///
584 /// \pre The behavior is undefined unless `0 <= numValues`
585 /// and `values` has sufficient contents.
587 int numValues);
588
589 /// Write to this stream the one-byte type indicator for a six-byte
590 /// integer, the four-byte, two's complement integer (in network byte
591 /// order) comprised of the least-significant four bytes of the
592 /// specified `numValues` (in host byte order), and the consecutive
593 /// six-byte, two's complement integers (in network byte order)
594 /// comprised of the least-significant six bytes of each of the
595 /// `numValues` leading entries in the specified `values` (in host byte
596 /// order), and return a reference to this stream. If this stream is
597 /// initially invalid, this operation has no effect. If the next output
598 /// operation has been set to be marked invalid (see `makeNextInvalid`),
599 /// reset this marking and emit the invalid indicator instead of the type indicator.
600 ///
601 /// \pre The behavior is undefined unless `0 <= numValues`
602 /// and `values` has sufficient contents.
604 int numValues);
605
606 /// Write to this stream the one-byte type indicator for a six-byte
607 /// unsigned integer, the four-byte, two's complement integer (in
608 /// network byte order) comprised of the least-significant four bytes of
609 /// the specified `numValues` (in host byte order), and the consecutive
610 /// six-byte, two's complement unsigned integers (in network byte order)
611 /// comprised of the least-significant six bytes of each of the
612 /// `numValues` leading entries in the specified `values` (in host byte
613 /// order), and return a reference to this stream. If this stream is
614 /// initially invalid, this operation has no effect. If the next output
615 /// operation has been set to be marked invalid (see `makeNextInvalid`),
616 /// reset this marking and emit the invalid indicator instead of the type indicator.
617 ///
618 /// \pre The behavior is undefined unless `0 <= numValues`
619 /// and `values` has sufficient contents.
621 int numValues);
622
623 /// Write to this stream the one-byte type indicator for a five-byte
624 /// integer, the four-byte, two's complement integer (in network byte
625 /// order) comprised of the least-significant four bytes of the
626 /// specified `numValues` (in host byte order), and the consecutive
627 /// five-byte, two's complement integers (in network byte order)
628 /// comprised of the least-significant five bytes of each of the
629 /// `numValues` leading entries in the specified `values` (in host byte
630 /// order), and return a reference to this stream. If this stream is
631 /// initially invalid, this operation has no effect. If the next output
632 /// operation has been set to be marked invalid (see `makeNextInvalid`),
633 /// reset this marking and emit the invalid indicator instead of the type indicator.
634 ///
635 /// \pre The behavior is undefined unless `0 <= numValues`
636 /// and `values` has sufficient contents.
638 int numValues);
639
640 /// Write to this stream the one-byte type indicator for a five-byte
641 /// unsigned integer, the four-byte, two's complement integer (in
642 /// network byte order) comprised of the least-significant four bytes of
643 /// the specified `numValues` (in host byte order), and the consecutive
644 /// five-byte, two's complement unsigned integers (in network byte
645 /// order) comprised of the least-significant five bytes of each of the
646 /// `numValues` leading entries in the specified `values` (in host byte
647 /// order), and return a reference to this stream. If this stream is
648 /// initially invalid, this operation has no effect. If the next output
649 /// operation has been set to be marked invalid (see `makeNextInvalid`),
650 /// reset this marking and emit the invalid indicator instead of the type indicator.
651 ///
652 /// \pre The behavior is undefined unless `0 <= numValues`
653 /// and `values` has sufficient contents.
655 int numValues);
656
657 /// Write to this stream the one-byte type indicator for a four-byte
658 /// integer, the four-byte, two's complement integer (in network byte
659 /// order) comprised of the least-significant four bytes of the
660 /// specified `numValues` (in host byte order), and the consecutive
661 /// four-byte, two's complement integers (in network byte order)
662 /// comprised of the least-significant four bytes of each of the
663 /// `numValues` leading entries in the specified `values` (in host byte
664 /// order), and return a reference to this stream. If this stream is
665 /// initially invalid, this operation has no effect. If the next output
666 /// operation has been set to be marked invalid (see `makeNextInvalid`),
667 /// reset this marking and emit the invalid indicator instead of the type indicator.
668 ///
669 /// \pre The behavior is undefined unless `0 <= numValues`
670 /// and `values` has sufficient contents.
671 TestOutStream& putArrayInt32(const int *values, int numValues);
672
673 /// Write to this stream the one-byte type indicator for a four-byte
674 /// unsigned integer, the four-byte, two's complement integer (in
675 /// network byte order) comprised of the least-significant four bytes of
676 /// the specified `numValues` (in host byte order), and the consecutive
677 /// four-byte, two's complement unsigned integers (in network byte
678 /// order) comprised of the least-significant four bytes of each of the
679 /// `numValues` leading entries in the specified `values` (in host byte
680 /// order), and return a reference to this stream. If this stream is
681 /// initially invalid, this operation has no effect. If the next output
682 /// operation has been set to be marked invalid (see `makeNextInvalid`),
683 /// reset this marking and emit the invalid indicator instead of the type indicator.
684 ///
685 /// \pre The behavior is undefined unless `0 <= numValues`
686 /// and `values` has sufficient contents.
687 TestOutStream& putArrayUint32(const unsigned int *values, int numValues);
688
689 /// Write to this stream the one-byte type indicator for a three-byte
690 /// integer, the four-byte, two's complement integer (in network byte
691 /// order) comprised of the least-significant four bytes of the
692 /// specified `numValues` (in host byte order), and the consecutive
693 /// three-byte, two's complement integers (in network byte order)
694 /// comprised of the least-significant three bytes of each of the
695 /// `numValues` leading entries in the specified `values` (in host byte
696 /// order), and return a reference to this stream. If this stream is
697 /// initially invalid, this operation has no effect. If the next output
698 /// operation has been set to be marked invalid (see `makeNextInvalid`),
699 /// reset this marking and emit the invalid indicator instead of the type indicator.
700 ///
701 /// \pre The behavior is undefined unless `0 <= numValues`
702 /// and `values` has sufficient contents.
703 TestOutStream& putArrayInt24(const int *values, int numValues);
704
705 /// Write to this stream the one-byte type indicator for a three-byte
706 /// unsigned integer, the four-byte, two's complement integer (in
707 /// network byte order) comprised of the least-significant four bytes of
708 /// the specified `numValues` (in host byte order), and the consecutive
709 /// three-byte, two's complement unsigned integers (in network byte
710 /// order) comprised of the least-significant three bytes of each of the
711 /// `numValues` leading entries in the specified `values` (in host byte
712 /// order), and return a reference to this stream. If this stream is
713 /// initially invalid, this operation has no effect. If the next output
714 /// operation has been set to be marked invalid (see `makeNextInvalid`),
715 /// reset this marking and emit the invalid indicator instead of the type indicator.
716 ///
717 /// \pre The behavior is undefined unless `0 <= numValues`
718 /// and `values` has sufficient contents.
719 TestOutStream& putArrayUint24(const unsigned int *values, int numValues);
720
721 /// Write to this stream the one-byte type indicator for a two-byte
722 /// integer, the four-byte, two's complement integer (in network byte
723 /// order) comprised of the least-significant four bytes of the
724 /// specified `numValues` (in host byte order), and the consecutive
725 /// two-byte, two's complement integers (in network byte order)
726 /// comprised of the least-significant two bytes of each of the
727 /// `numValues` leading entries in the specified `values` (in host byte
728 /// order), and return a reference to this stream. If this stream is
729 /// initially invalid, this operation has no effect. If the next output
730 /// operation has been set to be marked invalid (see `makeNextInvalid`),
731 /// reset this marking and emit the invalid indicator instead of the type indicator.
732 ///
733 /// \pre The behavior is undefined unless `0 <= numValues`
734 /// and `values` has sufficient contents.
735 TestOutStream& putArrayInt16(const short *values, int numValues);
736
737 /// Write to this stream the one-byte type indicator for a two-byte
738 /// unsigned integer, the four-byte, two's complement integer (in
739 /// network byte order) comprised of the least-significant four bytes of
740 /// the specified `numValues` (in host byte order), and the consecutive
741 /// two-byte, two's complement unsigned integers (in network byte order)
742 /// comprised of the least-significant two bytes of each of the
743 /// `numValues` leading entries in the specified `values` (in host byte
744 /// order), and return a reference to this stream. If this stream is
745 /// initially invalid, this operation has no effect. If the next output
746 /// operation has been set to be marked invalid (see `makeNextInvalid`),
747 /// reset this marking and emit the invalid indicator instead of the type indicator.
748 ///
749 /// \pre The behavior is undefined unless `0 <= numValues`
750 /// and `values` has sufficient contents.
751 TestOutStream& putArrayUint16(const unsigned short *values, int numValues);
752
753 /// Write to this stream the one-byte type indicator for a one-byte
754 /// integer, the four-byte, two's complement integer (in network byte
755 /// order) comprised of the least-significant four bytes of the
756 /// specified `numValues` (in host byte order), and the consecutive
757 /// one-byte, two's complement integers comprised of the
758 /// least-significant one byte of each of the `numValues` leading
759 /// entries in the specified `values`, and return a reference to this
760 /// stream. If this stream is initially invalid, this operation has no
761 /// effect. If the next output operation has been set to be marked
762 /// invalid (see `makeNextInvalid`), reset this marking and emit the
763 /// invalid indicator instead of the type indicator.
764 ///
765 /// \pre The behavior is undefined unless `0 <= numValues` and `values` has sufficient
766 /// contents.
767 TestOutStream& putArrayInt8(const char *values, int numValues);
768 TestOutStream& putArrayInt8(const signed char *values, int numValues);
769
770 /// Write to this stream the one-byte type indicator for a one-byte
771 /// unsigned integer, the four-byte, two's complement integer (in
772 /// network byte order) comprised of the least-significant four bytes of
773 /// the specified `numValues` (in host byte order), and the consecutive
774 /// one-byte, two's complement unsigned integers comprised of the
775 /// least-significant one byte of each of the `numValues` leading
776 /// entries in the specified `values`, and return a reference to this
777 /// stream. If this stream is initially invalid, this operation has no
778 /// effect. If the next output operation has been set to be marked
779 /// invalid (see `makeNextInvalid`), reset this marking and emit the
780 /// invalid indicator instead of the type indicator.
781 ///
782 /// \pre The behavior is undefined unless `0 <= numValues` and `values` has sufficient
783 /// contents.
784 TestOutStream& putArrayUint8(const char *values, int numValues);
785 TestOutStream& putArrayUint8(const unsigned char *values, int numValues);
786
787 // *** arrays of floating-point values ***
788
789 /// Write to this stream the one-byte type indicator for an eight-byte
790 /// double-precision floating-point number, the four-byte, two's
791 /// complement integer (in network byte order) comprised of the
792 /// least-significant four bytes of the specified `numValues` (in host
793 /// byte order), and the consecutive eight-byte IEEE double-precision
794 /// floating-point numbers (in network byte order) comprised of the
795 /// most-significant eight bytes of each of the `numValues` leading
796 /// entries in the specified `values` (in host byte order), and return a
797 /// reference to this stream. If this stream is initially invalid, this
798 /// operation has no effect. If the next output operation has been set
799 /// to be marked invalid (see `makeNextInvalid`), reset this marking and
800 /// emit the invalid indicator instead of the type indicator.
801 ///
802 /// \pre The behavior is undefined unless `0 <= numValues` and `values` has sufficient contents.
803 ///
804 /// \note Note that for non-conforming platforms, this
805 /// operation may be lossy.
806 TestOutStream& putArrayFloat64(const double *values, int numValues);
807
808 /// Write to this stream the one-byte type indicator for a four-byte
809 /// single-precision floating-point number, the four-byte, two's
810 /// complement integer (in network byte order) comprised of the
811 /// least-significant four bytes of the specified `numValues` (in host
812 /// byte order), and the consecutive four-byte IEEE single-precision
813 /// floating-point numbers (in network byte order) comprised of the
814 /// most-significant four bytes of each of the `numValues` leading
815 /// entries in the specified `values` (in host byte order), and return a
816 /// reference to this stream. If this stream is initially invalid, this
817 /// operation has no effect. If the next output operation has been set
818 /// to be marked invalid (see `makeNextInvalid`), reset this marking and
819 /// emit the invalid indicator instead of the type indicator.
820 ///
821 /// \pre The behavior is undefined unless `0 <= numValues` and `values` has sufficient contents.
822 ///
823 /// \note Note that for non-conforming platforms, this
824 /// operation may be lossy.
825 TestOutStream& putArrayFloat32(const float *values, int numValues);
826
827 // ACCESSORS
828
829 /// Return a non-zero value if this stream is valid, and 0 otherwise.
830 /// An invalid stream is a stream for which an output operation was
831 /// detected to have failed or `invalidate` was called.
832 operator const void *() const;
833
834 /// Return the `versionSelector` to be used with `operator<<` for BDEX
835 /// streaming as per the `bslx` package-level documentation.
836 int bdexVersionSelector() const;
837
838 /// Return the address of the contiguous, non-modifiable internal memory
839 /// buffer of this stream. The address will remain valid as long as
840 /// this array is not destroyed or modified (i.e., the current capacity
841 /// is not exceeded). The behavior of accessing elements outside the
842 /// range `[ data() .. data() + (length() - 1) ]` is undefined.
843 const char *data() const;
844
845 /// Return `true` if this stream is valid, and `false` otherwise. An
846 /// invalid stream is a stream for which an output operation was
847 /// detected to have failed or `invalidate` was called.
848 bool isValid() const;
849
850 /// Return the number of bytes in this stream.
851 bsl::size_t length() const;
852};
853
854// FREE OPERATORS
855
856/// Write the specified `object` to the specified output `stream` in some
857/// reasonable (multi-line) format, and return a reference to `stream`.
858bsl::ostream& operator<<(bsl::ostream& stream,
859 const TestOutStream& object);
860
861/// Write the specified `value` to the specified output `stream` following
862/// the requirements of the BDEX protocol (see the `bslx` package-level
863/// documentation), and return a reference to `stream`.
864///
865/// \pre The behavior is undefined unless `TYPE` is BDEX-compliant.
866template <class TYPE>
867TestOutStream& operator<<(TestOutStream& stream, const TYPE& value);
868
869// ============================================================================
870// INLINE DEFINITIONS
871// ============================================================================
872
873 // -------------------
874 // class TestOutStream
875 // -------------------
876
877// MANIPULATORS
878inline
880{
881 d_imp.invalidate();
882}
883
884inline
886{
887 d_makeNextInvalidFlag = true;
888}
889
890inline
891void TestOutStream::reserveCapacity(bsl::size_t newCapacity)
892{
893 d_imp.reserveCapacity(newCapacity);
894}
895
896inline
898{
899 d_imp.reset();
900}
901
902 // *** string values ***
903
904inline
906{
907 putLength(static_cast<int>(value.length()));
908 return putArrayUint8(value.data(), static_cast<int>(value.length()));
909}
910
911// ACCESSORS
912inline
913TestOutStream::operator const void *() const
914{
915 return d_imp;
916}
917
918inline
920{
921 return d_imp.bdexVersionSelector();
922}
923
924inline
925const char *TestOutStream::data() const
926{
927 return d_imp.data();
928}
929
930inline
932{
933 return d_imp.isValid();
934}
935
936inline
937bsl::size_t TestOutStream::length() const
938{
939 return d_imp.length();
940}
941
942// FREE OPERATORS
943template <class TYPE>
944inline
945TestOutStream& operator<<(TestOutStream& stream, const TYPE& value)
946{
947 return OutStreamFunctions::bdexStreamOut(stream, value);
948}
949
950} // close package namespace
951
952
953// TRAITS
954
955namespace bslma {
956
957template <>
958struct UsesBslmaAllocator<bslx::TestOutStream> : bsl::true_type {};
959
960} // close namespace bslma
961
962
963#endif
964
965// ----------------------------------------------------------------------------
966// Copyright 2014 Bloomberg Finance L.P.
967//
968// Licensed under the Apache License, Version 2.0 (the "License");
969// you may not use this file except in compliance with the License.
970// You may obtain a copy of the License at
971//
972// http://www.apache.org/licenses/LICENSE-2.0
973//
974// Unless required by applicable law or agreed to in writing, software
975// distributed under the License is distributed on an "AS IS" BASIS,
976// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
977// See the License for the specific language governing permissions and
978// limitations under the License.
979// ----------------------------- END-OF-FILE ----------------------------------
980
981/** @} */
982/** @} */
983/** @} */
Definition bslstl_string.h:1252
size_type length() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:7301
CHAR_TYPE * data() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:7177
Definition bslma_allocator.h:545
Definition bslx_byteoutstream.h:212
void invalidate()
Definition bslx_byteoutstream.h:724
bool isValid() const
Definition bslx_byteoutstream.h:1610
void reserveCapacity(bsl::size_t newCapacity)
Definition bslx_byteoutstream.h:749
bsl::size_t length() const
Return the number of bytes in this stream.
Definition bslx_byteoutstream.h:1616
const char * data() const
Definition bslx_byteoutstream.h:1604
int bdexVersionSelector() const
Definition bslx_byteoutstream.h:1598
void reset()
Definition bslx_byteoutstream.h:755
Definition bslx_testoutstream.h:220
TestOutStream & putUint48(bsls::Types::Uint64 value)
TestOutStream & putArrayUint32(const unsigned int *values, int numValues)
TestOutStream & putUint16(unsigned int value)
TestOutStream & putInt24(int value)
TestOutStream(int versionSelector, bsl::size_t initialCapacity, bslma::Allocator *basicAllocator=0)
TestOutStream & putArrayUint56(const bsls::Types::Uint64 *values, int numValues)
TestOutStream & putInt56(bsls::Types::Int64 value)
TestOutStream & putLength(int length)
TestOutStream & putFloat64(double value)
TestOutStream & putArrayInt64(const bsls::Types::Int64 *values, int numValues)
TestOutStream & putArrayInt48(const bsls::Types::Int64 *values, int numValues)
TestOutStream & putUint8(unsigned int value)
TestOutStream & putArrayUint48(const bsls::Types::Uint64 *values, int numValues)
TestOutStream & putArrayInt8(const signed char *values, int numValues)
TestOutStream & putUint32(unsigned int value)
TestOutStream & putInt48(bsls::Types::Int64 value)
TestOutStream & putString(const bsl::string &value)
Definition bslx_testoutstream.h:905
TestOutStream & putInt16(int value)
TestOutStream & putArrayFloat32(const float *values, int numValues)
void invalidate()
Definition bslx_testoutstream.h:879
void makeNextInvalid()
Definition bslx_testoutstream.h:885
TestOutStream & putUint24(unsigned int value)
TestOutStream & putArrayUint16(const unsigned short *values, int numValues)
TestOutStream & putArrayInt16(const short *values, int numValues)
TestOutStream & putArrayInt56(const bsls::Types::Int64 *values, int numValues)
int bdexVersionSelector() const
Definition bslx_testoutstream.h:919
TestOutStream & putUint56(bsls::Types::Uint64 value)
friend bsl::ostream & operator<<(bsl::ostream &, const TestOutStream &)
TestOutStream & putArrayFloat64(const double *values, int numValues)
void reserveCapacity(bsl::size_t newCapacity)
Definition bslx_testoutstream.h:891
void reset()
Definition bslx_testoutstream.h:897
TestOutStream(int versionSelector, bslma::Allocator *basicAllocator=0)
bool isValid() const
Definition bslx_testoutstream.h:931
TestOutStream & putArrayUint40(const bsls::Types::Uint64 *values, int numValues)
TestOutStream & putArrayUint8(const char *values, int numValues)
TestOutStream & putVersion(int version)
bsl::size_t length() const
Return the number of bytes in this stream.
Definition bslx_testoutstream.h:937
~TestOutStream()
Destroy this object.
TestOutStream & putInt32(int value)
const char * data() const
Definition bslx_testoutstream.h:925
TestOutStream & putInt40(bsls::Types::Int64 value)
TestOutStream & putFloat32(float value)
TestOutStream & putArrayInt24(const int *values, int numValues)
TestOutStream & putArrayUint24(const unsigned int *values, int numValues)
TestOutStream & putInt8(int value)
TestOutStream & putUint64(bsls::Types::Uint64 value)
TestOutStream & putInt64(bsls::Types::Int64 value)
TestOutStream & putUint40(bsls::Types::Uint64 value)
TestOutStream & putArrayInt40(const bsls::Types::Int64 *values, int numValues)
TestOutStream & putArrayUint64(const bsls::Types::Uint64 *values, int numValues)
TestOutStream & putArrayInt8(const char *values, int numValues)
TestOutStream & putArrayInt32(const int *values, int numValues)
TestOutStream & putArrayUint8(const unsigned char *values, int numValues)
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition baljsn_encoder_testtypes.h:76
STREAM & bdexStreamOut(STREAM &stream, const TYPE &value)
Definition bslx_outstreamfunctions.h:1004
Definition bslx_byteinstream.h:377
bsl::ostream & operator<<(bsl::ostream &stream, const ByteInStream &object)
Definition bslma_usesbslmaallocator.h:344
unsigned long long Uint64
Definition bsls_types.h:139
long long Int64
Definition bsls_types.h:134