BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslx_outstreamfunctions.h
Go to the documentation of this file.
1/// @file bslx_outstreamfunctions.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslx_outstreamfunctions.h -*-C++-*-
8#ifndef INCLUDED_BSLX_OUTSTREAMFUNCTIONS
9#define INCLUDED_BSLX_OUTSTREAMFUNCTIONS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslx_outstreamfunctions bslx_outstreamfunctions
15/// @brief Facilitate uniform externalization of user and fundamental types.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslx
19/// @{
20/// @addtogroup bslx_outstreamfunctions
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslx_outstreamfunctions-purpose"> Purpose</a>
25/// * <a href="#bslx_outstreamfunctions-classes"> Classes </a>
26/// * <a href="#bslx_outstreamfunctions-description"> Description </a>
27/// * <a href="#bslx_outstreamfunctions-component-design-anticipated-usage-and-the-bdex-contract"> Component Design, Anticipated Usage, and the BDEX Contract </a>
28/// * <a href="#bslx_outstreamfunctions-implementing-bdex-streaming-in-value-semantic-template-classes"> Implementing BDEX Streaming in Value-Semantic Template Classes </a>
29/// * <a href="#bslx_outstreamfunctions-usage"> Usage </a>
30/// * <a href="#bslx_outstreamfunctions-example-1-using-bslx-outstreamfunctions-to-externalize-data"> Example 1: Using bslx::OutStreamFunctions to Externalize Data </a>
31///
32/// # Purpose {#bslx_outstreamfunctions-purpose}
33/// Facilitate uniform externalization of user and fundamental types.
34///
35/// # Classes {#bslx_outstreamfunctions-classes}
36///
37/// - bslx::OutStreamFunctions: namespace for BDEX externalization functions
38///
39/// @see bslx_instreamfunctions, bslx_versionfunctions
40///
41/// # Description {#bslx_outstreamfunctions-description}
42/// This component provides a namespace,
43/// `bslx::OutStreamFunctions`, that facilitates uniform support for BDEX
44/// externalization across all BDEX-compliant user-defined types, including
45/// template types and containers, as well as those fundamental types (and
46/// `bsl::string` and `bsl::vector`) for which the BDEX protocol provides direct
47/// support.
48///
49/// The namespace `bslx::OutStreamFunctions` facilitates client externalization
50/// of objects in a uniform, type-independent manner. It contains the
51/// `bdexStreamOut` function that externalizes objects of all BDEX-compliant
52/// types. This function externalizes the specified `object` in the specified
53/// `version`. The `bdexStreamOut` function is overloaded for fundamental
54/// types, enumeration types, `bsl::string`, and `bsl::vector`. Note that,
55/// excluding `bsl::vector`, version information is never written to the stream
56/// while externalizing these types.
57///
58/// By default, objects of enumeration type are streamed out as 32-bit `int`
59/// values. Users can override this behavior by providing overloads of the
60/// `OutStreamFunctions::bdexStreamOut` function in the enumeration's namespace
61/// for their enumeration types. The general form of this overload is:
62/// @code
63/// template <class STREAM>
64/// STREAM& bdexStreamOut(STREAM& stream, const MyEnum& value, int version)
65/// {
66/// using bslx::OutStreamFunctions::bdexStreamOut;
67///
68/// // Code to stream out objects of 'MyEnum' type.
69///
70/// return stream;
71/// }
72/// @endcode
73/// For value-semantic types that support the BDEX protocol, the free function
74/// `bdexStreamOut` calls the `bdexStreamOut` member function for that type.
75///
76/// ## Component Design, Anticipated Usage, and the BDEX Contract {#bslx_outstreamfunctions-component-design-anticipated-usage-and-the-bdex-contract}
77///
78///
79/// @ref bslx_outstreamfunctions is an integral part of the BDEX externalization
80/// contract. The BDEX contract is at least in part "collaborative", which is
81/// to say that each developer of a given *kind* of component (e.g., a stream or
82/// a value-semantic container) must comply with the relevant portions of the
83/// contract to ensure that the "system as a whole" works for everybody.
84/// @ref bslx_outstreamfunctions plays several related but different roles in
85/// helping various developers to produce BDEX-compliant components. In this
86/// section we briefly highlight how and why @ref bslx_outstreamfunctions is
87/// helpful (or required) for these different developers. By discussing
88/// different aspects of usage, we convey the general design goals of this
89/// component, and, to a certain extent, the overall BDEX contract. See the
90/// `bslx` package-level documentation for a full specification of the BDEX
91/// contract.
92///
93/// ### Implementing BDEX Streaming in Value-Semantic Template Classes {#bslx_outstreamfunctions-implementing-bdex-streaming-in-value-semantic-template-classes}
94///
95///
96/// The author of a non-template value-semantic type has full knowledge of the
97/// details of the "value" of that type, and may choose to use the appropriate
98/// output stream `put` methods directly when implementing the required
99/// `bdexStreamOut` method for that type. However, if one or more aspects of
100/// the value are of template parameter type, then the author cannot in general
101/// know how to stream the value using the `put` methods. For example, if a
102/// type has as its value one `int` data member:
103/// @code
104/// int d_value;
105/// @endcode
106/// then the implementation of the `bdexStreamOut` method can contain:
107/// @code
108/// stream.putInt32(d_value);
109/// @endcode
110/// However, if the data member is of (template parameter) `TYPE`:
111/// @code
112/// TYPE d_value;
113/// @endcode
114/// then the implementation of the `bdexStreamOut` method must rely on the
115/// `bslx::OutStreamFunctions` implementation to output the value:
116/// @code
117/// using bslx::OutStreamFunctions::bdexStreamOut;
118/// bdexStreamOut(stream, d_value, 1);
119/// @endcode
120/// This call will resolve to the correct sequence of `put` calls no matter
121/// whether `TYPE` is a fundamental type, a BDEX-compliant `enum`, or a proper
122/// BDEX-compliant class. In the latter two cases, the explicit specification
123/// of the version format (in this case, 1) guarantees the stable operation of
124/// this method whether or not `TYPE` is provided additional version formats.
125///
126/// ## Usage {#bslx_outstreamfunctions-usage}
127///
128///
129/// This section illustrates intended use of this component.
130///
131/// ### Example 1: Using bslx::OutStreamFunctions to Externalize Data {#bslx_outstreamfunctions-example-1-using-bslx-outstreamfunctions-to-externalize-data}
132///
133///
134/// In this example we illustrate the primary intended use of the parameterized
135/// methods of this component, as well as a few trivial invocations just to show
136/// the syntax clearly. To accomplish this, we exhibit two separate example
137/// "components": a value-semantic point object, and an `enum`. In all cases,
138/// the component designs are very simple, with much of the implied
139/// functionality omitted, in order to focus attention on the key aspects of the
140/// functionality of *this* component.
141///
142/// First, consider an `enum` `Color` that enumerates a set of colors:
143/// @code
144/// enum Color {
145/// RED = 0,
146/// GREEN = 1,
147/// BLUE = 2
148/// };
149/// @endcode
150/// Next, we consider a very special-purpose point that has as a data member its
151/// color. Such a point provides an excellent opportunity for factoring, but
152/// since we are interested in highlighting BDEX streaming of various types, we
153/// will present a simple and unfactored design here. In a real-world problem,
154/// the `mypoint` component would be implemented differently.
155///
156/// Note that the `MyPoint` class in this example represents its coordinates as
157/// `short` integer values; this is done to make the BDEX stream output byte
158/// pattern somewhat easier for the reader of this example to recognize when the
159/// output buffer is printed.
160/// @code
161/// // mypoint.h
162///
163/// class MyPoint {
164/// // This class provides a geometric point having integer coordinates and
165/// // an enumerated color property.
166///
167/// short d_x; // x coordinate
168/// short d_y; // y coordinate
169/// Color d_color; // enumerated color property
170///
171/// public:
172/// // CLASS METHODS
173/// static int maxSupportedBdexVersion(int versionSelector);
174/// // Return the maximum valid BDEX format version, as indicated by
175/// // the specified 'versionSelector', to be passed to the
176/// // 'bdexStreamOut' method. Note that it is highly recommended that
177/// // versionSelector' be formatted as "YYYYMMDD", a date
178/// // representation. Also note that 'versionSelector' should be a
179/// // *compile*-time-chosen value that selects a format version
180/// // supported by both externalizer and unexternalizer. See the
181/// // 'bslx' package-level documentation for more information on BDEX
182/// // streaming of value-semantic types and containers.
183///
184/// // CREATORS
185/// MyPoint();
186/// // Create a default point.
187///
188/// MyPoint(short x, short y, Color color);
189/// // Create a point having the specified 'x' and 'y' coordinates
190/// // and the specified 'color'.
191///
192/// ~MyPoint();
193/// // Destroy this point.
194///
195/// // MANIPULATORS
196/// // ...
197///
198/// // ACCESSORS
199/// int x() const;
200/// // Return the x coordinate of this point.
201///
202/// int y() const;
203/// // Return the y coordinate of this point.
204///
205/// Color color() const;
206/// // Return the enumerated color of this point.
207///
208/// template <class STREAM>
209/// STREAM& bdexStreamOut(STREAM& stream, int version) const;
210/// // Write the value of this object, using the specified 'version'
211/// // format, to the specified output 'stream', and return a reference
212/// // to 'stream'. If 'stream' is initially invalid, this operation
213/// // has no effect. If 'version' is not supported, 'stream' is
214/// // invalidated, but otherwise unmodified. Note that 'version' is
215/// // not written to 'stream'. See the 'bslx' package-level
216/// // documentation for more information on BDEX streaming of
217/// // value-semantic types and containers.
218/// };
219///
220/// // FREE OPERATORS
221/// inline
222/// bool operator==(const MyPoint& lhs, const MyPoint& rhs);
223/// // Return 'true' if the specified 'lhs' and 'rhs' points have the same
224/// // value, and 'false' otherwise. Two points have the same value if
225/// // they have the same x and y coordinates and the same color.
226/// @endcode
227/// Representative (inline) implementations of these methods are shown below:
228/// @code
229/// // ========================================================================
230/// // INLINE FUNCTION DEFINITIONS
231/// // ========================================================================
232///
233/// // CLASS METHODS
234/// inline
235/// int MyPoint::maxSupportedBdexVersion(int versionSelector)
236/// {
237/// if (versionSelector >= 20131201) {
238/// return 2;
239/// }
240/// return 1;
241/// }
242///
243/// // CREATORS
244/// inline
245/// MyPoint::MyPoint(short x, short y, Color color)
246/// : d_x(x)
247/// , d_y(y)
248/// , d_color(color)
249/// {
250/// }
251///
252/// inline
253/// MyPoint::~MyPoint()
254/// {
255/// }
256///
257/// // ...
258///
259/// // MANIPULATORS
260/// // ...
261///
262/// // ACCESSORS
263/// inline
264/// int MyPoint::x() const
265/// {
266/// return d_x;
267/// }
268///
269/// // ...
270///
271/// template <class STREAM>
272/// STREAM& MyPoint::bdexStreamOut(STREAM& stream, int version) const
273/// {
274/// switch (version) {
275/// case 1: {
276/// stream.putInt16(d_x); // output the x coordinate
277/// stream.putInt16(d_y); // output the y coordinate
278/// stream.putInt8(static_cast<char>(d_color));
279/// // output the color enum as one byte
280/// } break;
281/// default: {
282/// stream.invalidate();
283/// } break;
284/// }
285/// return stream;
286/// }
287///
288/// // FREE OPERATORS
289/// inline
290/// bool operator==(const MyPoint& lhs, const MyPoint& rhs)
291/// {
292/// return lhs.x() == rhs.x()
293/// && lhs.y() == rhs.y()
294/// && lhs.color() == rhs.color();
295/// }
296/// @endcode
297/// Then, we will implement an extremely simple output stream that supports the
298/// BDEX documentation-only protocol. For simplicity, we will use a fixed-size
299/// buffer (usually a bad idea in any event, and more so here since the
300/// implementation knows the buffer size, but makes no effort to prevent
301/// overwriting that buffer), and will only show a few methods needed for this
302/// example. See other `bslx` stream components for examples of
303/// properly-designed BDEX streams.
304/// @code
305/// // myoutstream.h
306/// // ...
307///
308/// class MyOutStream {
309/// // This class implements a limited-size fixed-buffer output stream that
310/// // partially conforms to the BDEX protocol for output streams. This
311/// // class is suitable for demonstration purposes only.
312///
313/// char d_buffer[1000]; // externalized values stored as contiguous bytes
314///
315/// int d_length; // length of data in 'd_buffer' (in bytes)
316///
317/// bool d_validFlag; // stream validity flag; 'true' if stream is in
318/// // valid state, 'false' otherwise
319///
320/// public:
321/// // CREATORS
322/// MyOutStream();
323/// // Create an empty output stream of limited, fixed capacity. Note
324/// // that this object is suitable for demonstration purposes only.
325///
326/// ~MyOutStream();
327/// // Destroy this output stream.
328///
329/// // MANIPULATORS
330/// void invalidate();
331/// // Put this input stream in an invalid state. This function has no
332/// // effect if this stream is already invalid. Note that this
333/// // function should be called whenever a value extracted from this
334/// // stream is determined to be invalid, inconsistent, or otherwise
335/// // incorrect.
336///
337/// MyOutStream& putVersion(int version);
338/// // Write to this stream the one-byte, two's complement integer
339/// // comprised of the least-significant one byte of the specified
340/// // 'version', and return a reference to this stream.
341///
342/// MyOutStream& putInt32(int value);
343/// // Write to this stream the four-byte, two's complement integer (in
344/// // network byte order) comprised of the least-significant four
345/// // bytes of the specified 'value' (in host byte order), and return
346/// // a reference to this stream.
347///
348/// MyOutStream& putInt16(int value);
349/// // Write to this stream the two-byte, two's complement integer
350/// // (in network byte order) comprised of the least-significant two
351/// // bytes of the specified 'value' (in host byte order), and return
352/// // a reference to this stream.
353///
354/// MyOutStream& putInt8(int value);
355/// // Write to this stream the one-byte, two's complement integer
356/// // comprised of the least-significant one byte of the specified
357/// // 'value', and return a reference to this stream.
358///
359/// void removeAll();
360/// // Remove all content in this stream.
361///
362/// // ACCESSORS
363/// const char *data() const;
364/// // Return the address of the contiguous, non-modifiable internal
365/// // memory buffer of this stream. The address will remain valid as
366/// // long as this stream is not destroyed or modified. The behavior
367/// // of accessing elements outside the range
368/// // '[ data() .. data() + (length() - 1) ]' is undefined.
369///
370/// int length() const;
371/// // Return the number of bytes in this stream.
372/// };
373///
374/// // FREE OPERATORS
375/// inline
376/// bsl::ostream& operator<<(bsl::ostream& stream,
377/// const MyOutStream& object);
378/// // Write the specified 'object' to the specified output 'stream' in
379/// // some reasonable (multi-line) format, and return a reference to
380/// // 'stream'.
381/// @endcode
382/// The relevant (inline) implementations are as follows.
383/// @code
384/// // ========================================================================
385/// // INLINE FUNCTION DEFINITIONS
386/// // ========================================================================
387///
388/// // CREATORS
389/// inline
390/// MyOutStream::MyOutStream()
391/// : d_length(0)
392/// , d_validFlag(true)
393/// {
394/// }
395///
396/// inline
397/// MyOutStream::~MyOutStream()
398/// {
399/// }
400///
401/// // MANIPULATORS
402/// inline
403/// void MyOutStream::invalidate()
404/// {
405/// d_validFlag = false;
406/// }
407///
408/// inline
409/// MyOutStream& MyOutStream::putVersion(int value)
410/// {
411/// d_buffer[d_length] = static_cast<char>(value);
412/// ++d_length;
413/// return *this;
414/// }
415///
416/// inline
417/// MyOutStream& MyOutStream::putInt32(int value)
418/// {
419/// d_buffer[d_length + 0] = static_cast<char>((value >> 24) & 0xff);
420/// d_buffer[d_length + 1] = static_cast<char>((value >> 16) & 0xff);
421/// d_buffer[d_length + 2] = static_cast<char>((value >> 8) & 0xff);
422/// d_buffer[d_length + 3] = static_cast<char>((value >> 0) & 0xff);
423/// d_length += 4;
424/// return *this;
425/// }
426///
427/// inline
428/// MyOutStream& MyOutStream::putInt16(int value)
429/// {
430/// d_buffer[d_length + 0] = static_cast<char>((value >> 8) & 0xff);
431/// d_buffer[d_length + 1] = static_cast<char>((value >> 0) & 0xff);
432/// d_length += 2;
433/// return *this;
434/// }
435///
436/// inline
437/// MyOutStream& MyOutStream::putInt8(int value)
438/// {
439/// d_buffer[d_length] = static_cast<char>(value);
440/// d_length += 1;
441/// return *this;
442/// }
443///
444/// inline
445/// void MyOutStream::removeAll()
446/// {
447/// d_length = 0;
448/// }
449///
450/// // ACCESSORS
451/// inline
452/// const char *MyOutStream::data() const
453/// {
454/// return static_cast<const char *>(d_buffer);
455/// }
456///
457/// inline
458/// int MyOutStream::length() const
459/// {
460/// return d_length;
461/// }
462/// @endcode
463/// Finally, use the above `enum`, point class, and output stream to illustrate
464/// `bslx::OutStreamFunctions` functionality. This test code does not attempt
465/// to do anything more useful than writing known values to a stream and
466/// confirming that the expected byte pattern was in fact written.
467/// @code
468/// int i = 168496141; // byte pattern 0a 0b 0c 0d
469/// Color color = BLUE; // byte pattern 02
470/// MyPoint p(0, -1, color); // byte pattern 00 00 ff ff 02
471///
472/// using bslx::OutStreamFunctions::bdexStreamOut;
473///
474/// MyOutStream out;
475/// assert(0 == out.length());
476///
477/// bdexStreamOut(out, i, 1);
478/// assert(4 == out.length());
479/// assert(0 == bsl::memcmp(out.data(), "\x0a\x0b\x0c\x0d", out.length()));
480///
481/// out.removeAll();
482/// assert(0 == out.length());
483///
484/// bdexStreamOut(out, i, 0);
485/// assert(4 == out.length());
486/// assert(0 == bsl::memcmp(out.data(), "\x0a\x0b\x0c\x0d", out.length()));
487///
488/// out.removeAll();
489/// assert(0 == out.length());
490///
491/// bdexStreamOut(out, color, 1);
492/// assert(4 == out.length());
493/// assert(0 == bsl::memcmp(out.data(), "\x00\x00\x00\x02", out.length()));
494///
495/// out.removeAll();
496/// assert(0 == out.length());
497///
498/// bdexStreamOut(out, color, 0);
499/// assert(4 == out.length());
500/// assert(0 == bsl::memcmp(out.data(), "\x00\x00\x00\x02", out.length()));
501///
502/// out.removeAll();
503/// assert(0 == out.length());
504///
505/// bdexStreamOut(out, p, 1);
506/// assert(5 == out.length());
507/// assert(0 == bsl::memcmp(out.data(), "\x00\x00\xff\xff\x02", out.length()));
508/// @endcode
509/// @}
510/** @} */
511/** @} */
512
513/** @addtogroup bsl
514 * @{
515 */
516/** @addtogroup bslx
517 * @{
518 */
519/** @addtogroup bslx_outstreamfunctions
520 * @{
521 */
522
523#include <bslscm_version.h>
524
526
527#include <bslmf_assert.h>
528#include <bslmf_conditional.h>
529#include <bslmf_isenum.h>
530
531#include <bsls_types.h>
532
533#include <bsl_string.h>
534#include <bsl_vector.h>
535#include <bsl_cstdint.h>
536
537#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
538#include <bslmf_if.h>
539#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
540
541
542namespace bslx {
543
544 // ============================
545 // namespace OutStreamFunctions
546 // ============================
547
548namespace OutStreamFunctions {
549 // This namespace facilitates externalization of all BDEX-compliant types
550 // in a type-independent manner. The externalization functions are
551 // overloaded for fundamental types, enumeration types, 'bsl::string', and
552 // 'bsl::vector'. A compilation error will occur if the (template
553 // parameter) 'TYPE' of a non-overloaded method of
554 // 'bslx::OutStreamFunctions' does not support 'bdexStreamOut' and
555 // 'maxSupportedBdexVersion' (with the appropriate signatures).
556
557 // =================
558 // struct IsEnumType
559 // =================
560
561 /// This `struct`, together with `IsNotEnumType` (below), is used to
562 /// distinguish enumeration types from other types in function overload
563 /// resolution. This `struct` contains no interface or implementation
564 /// by design, and is meant for internal use only.
565 struct IsEnumType {
566 };
567
568 // ====================
569 // struct IsNotEnumType
570 // ====================
571
572 /// This `struct`, together with `IsEnumType` (above), is used to
573 /// distinguish enumeration types from other types in function overload
574 /// resolution. This `struct` contains no interface or implementation
575 /// by design, and is meant for internal use only.
577 };
578
579 // PRIVATE CLASS METHODS
580
581 /// Write the specified `value` to the specified output `stream` as a
582 /// 32-bit `int`, and return a reference to `stream`. The specified
583 /// `version` is ignored. If `stream` is initially invalid, this
584 /// operation has no effect. Note that this function is called only for
585 /// enumeration types and that if `value` is outside the range of an
586 /// `int32_t` the externalization will be lossy. Also note that this
587 /// function is for internal use only. See the `bslx` package-level
588 /// documentation for more information on BDEX streaming of
589 /// value-semantic types and containers.
590 template <class STREAM, class TYPE>
591 STREAM& bdexStreamOutImp(STREAM& stream,
592 const TYPE& value,
593 int version,
594 const IsEnumType&);
595
596 /// Write the specified `value`, using the specified `version` format,
597 /// to the specified output `stream`, and return a reference to
598 /// `stream`. If `stream` is initially invalid, this operation has no
599 /// effect. If `version` is not supported by `TYPE`, `stream` is
600 /// invalidated, but otherwise unmodified. Note that `version` is not
601 /// written to `stream`. Also note that this function is for internal
602 /// use only. See the `bslx` package-level documentation for more
603 /// information on BDEX streaming of value-semantic types and
604 /// containers.
605 template <class STREAM, class TYPE>
606 STREAM& bdexStreamOutImp(STREAM& stream,
607 const TYPE& value,
608 int version,
609 const IsNotEnumType&);
610
611 // CLASS METHODS
612
613 /// Write the specified `value` to the specified output `stream`, and
614 /// return a reference to `stream`. If `stream` is initially invalid,
615 /// this operation has no effect. If needed, first write the computed
616 /// version information to the `stream` and if this version is not
617 /// supported by `TYPE`, `stream` is invalidated. Note that the version
618 /// is only needed when the (template parameter) `TYPE` is a
619 /// `bsl::vector` or a user-defined type. See the `bslx` package-level
620 /// documentation for more information on BDEX streaming of
621 /// value-semantic types and containers.
622 template <class STREAM, class TYPE>
623 STREAM& bdexStreamOut(STREAM& stream, const TYPE& value);
624
625 /// Write the specified `value`, using the specified `version` format,
626 /// to the specified output `stream`, and return a reference to
627 /// `stream`. If `stream` is initially invalid, this operation has no
628 /// effect. If `version` is not supported by `TYPE`, `stream` is
629 /// invalidated, but otherwise unmodified. Note that `version` is not
630 /// written to `stream`. See the `bslx` package-level documentation for
631 /// more information on BDEX streaming of value-semantic types and
632 /// containers.
633 template <class STREAM, class TYPE>
634 STREAM& bdexStreamOut(STREAM& stream, const TYPE& value, int version);
635
636 /* overloads */
637
638 /// Write the specified `bool` `value` to the specified output `stream`,
639 /// and return a reference to `stream`. The optionally specified
640 /// `version` is ignored. If `stream` is initially invalid, this
641 /// operation has no effect. See the `bslx` package-level documentation
642 /// for more information on BDEX streaming of value-semantic types and
643 /// containers.
644 template <class STREAM>
645 STREAM& bdexStreamOut(STREAM& stream, const bool& value, int version = 0);
646
647 /// Write the specified `char` `value` to the specified output `stream`,
648 /// and return a reference to `stream`. The optionally specified
649 /// `version` is ignored. If `stream` is initially invalid, this
650 /// operation has no effect. See the `bslx` package-level documentation
651 /// for more information on BDEX streaming of value-semantic types and
652 /// containers.
653 template <class STREAM>
654 STREAM& bdexStreamOut(STREAM& stream, const char& value, int version = 0);
655
656 /// Write the specified `signed char` `value` to the specified output
657 /// `stream`, and return a reference to `stream`. The optionally
658 /// specified `version` is ignored. If `stream` is initially invalid,
659 /// this operation has no effect. See the `bslx` package-level
660 /// documentation for more information on BDEX streaming of
661 /// value-semantic types and containers.
662 template <class STREAM>
663 STREAM& bdexStreamOut(STREAM& stream,
664 const signed char& value,
665 int version = 0);
666
667 /// Write the specified `unsigned char` `value` to the specified output
668 /// `stream`, and return a reference to `stream`. The optionally
669 /// specified `version` is ignored. If `stream` is initially invalid,
670 /// this operation has no effect. See the `bslx` package-level
671 /// documentation for more information on BDEX streaming of
672 /// value-semantic types and containers.
673 template <class STREAM>
674 STREAM& bdexStreamOut(STREAM& stream,
675 const unsigned char& value,
676 int version = 0);
677
678 /// Write the specified `short` `value` to the specified output
679 /// `stream`, and return a reference to `stream`. The optionally
680 /// specified `version` is ignored. If `stream` is initially invalid,
681 /// this operation has no effect. See the `bslx` package-level
682 /// documentation for more information on BDEX streaming of
683 /// value-semantic types and containers.
684 template <class STREAM>
685 STREAM& bdexStreamOut(STREAM& stream,
686 const short& value,
687 int version = 0);
688
689 /// Write the specified `unsigned short` `value` to the specified output
690 /// `stream`, and return a reference to `stream`. The optionally
691 /// specified `version` is ignored. If `stream` is initially invalid,
692 /// this operation has no effect. See the `bslx` package-level
693 /// documentation for more information on BDEX streaming of
694 /// value-semantic types and containers.
695 template <class STREAM>
696 STREAM& bdexStreamOut(STREAM& stream,
697 const unsigned short& value,
698 int version = 0);
699
700 /// Write the specified `int` `value` to the specified output `stream`,
701 /// and return a reference to `stream`. The optionally specified
702 /// `version` is ignored. If `stream` is initially invalid, this
703 /// operation has no effect. See the `bslx` package-level
704 /// documentation for more information on BDEX streaming of
705 /// value-semantic types and containers.
706 template <class STREAM>
707 STREAM& bdexStreamOut(STREAM& stream, const int& value, int version = 0);
708
709 /// Write the specified `unsigned int` `value` to the specified output
710 /// `stream`, and return a reference to `stream`. The optionally
711 /// specified `version` is ignored. If `stream` is initially invalid,
712 /// this operation has no effect. See the `bslx` package-level
713 /// documentation for more information on BDEX streaming of
714 /// value-semantic types and containers.
715 template <class STREAM>
716 STREAM& bdexStreamOut(STREAM& stream,
717 const unsigned int& value,
718 int version = 0);
719
720 /// Write the specified `long` `value` to the specified output `stream`
721 /// as a 32-bit `int`, and return a reference to `stream`. The
722 /// optionally specified `version` is ignored. If `stream` is initially
723 /// invalid, this operation has no effect. Note that for platforms
724 /// where `long` is not equivalent to `int32_t`, this operation may be
725 /// lossy. See the `bslx` package-level documentation for more
726 /// information on BDEX streaming of value-semantic types and
727 /// containers.
728 template <class STREAM>
729 STREAM& bdexStreamOut(STREAM& stream, const long& value, int version = 0);
730
731 /// Write the specified `unsigned long` `value` to the specified output
732 /// `stream` as a 32-bit `unsigned int`, and return a reference to
733 /// `stream`. The optionally specified `version` is ignored. If
734 /// `stream` is initially invalid, this operation has no effect. Note
735 /// that for platforms where `unsigned long` is not equivalent to
736 /// `uint32_t`, this operation may be lossy. See the `bslx`
737 /// package-level documentation for more information on BDEX streaming
738 /// of value-semantic types and containers.
739 template <class STREAM>
740 STREAM& bdexStreamOut(STREAM& stream,
741 const unsigned long& value,
742 int version = 0);
743
744 /// Write the specified `bsls::Types::Int64` `value` to the specified
745 /// output `stream`, and return a reference to `stream`. The optionally
746 /// specified `version` is ignored. If `stream` is initially invalid,
747 /// this operation has no effect. See the `bslx` package-level
748 /// documentation for more information on BDEX streaming of
749 /// value-semantic types and containers.
750 template <class STREAM>
751 STREAM& bdexStreamOut(STREAM& stream,
752 const bsls::Types::Int64& value,
753 int version = 0);
754
755 /// Write the specified `bsls::Types::Uint64` `value` to the specified
756 /// output `stream`, and return a reference to `stream`. The optionally
757 /// specified `version` is ignored. If `stream` is initially invalid,
758 /// this operation has no effect. See the `bslx` package-level
759 /// documentation for more information on BDEX streaming of
760 /// value-semantic types and containers.
761 template <class STREAM>
762 STREAM& bdexStreamOut(STREAM& stream,
763 const bsls::Types::Uint64& value,
764 int version = 0);
765
766 /// Write the specified `float` `value` to the specified output
767 /// `stream`, and return a reference to `stream`. The optionally
768 /// specified `version` is ignored. If `stream` is initially invalid,
769 /// this operation has no effect. See the `bslx` package-level
770 /// documentation for more information on BDEX streaming of
771 /// value-semantic types and containers.
772 template <class STREAM>
773 STREAM& bdexStreamOut(STREAM& stream, const float& value, int version = 0);
774
775 /// Write the specified `double` `value` to the specified output
776 /// `stream`, and return a reference to `stream`. The optionally
777 /// specified `version` is ignored. If `stream` is initially invalid,
778 /// this operation has no effect. See the `bslx` package-level
779 /// documentation for more information on BDEX streaming of
780 /// value-semantic types and containers.
781 template <class STREAM>
782 STREAM& bdexStreamOut(STREAM& stream,
783 const double& value,
784 int version = 0);
785
786 /// Write the specified `bsl::string` `value` to the specified output
787 /// `stream`, and return a reference to `stream`. The optionally
788 /// specified `version` is ignored. If `stream` is initially invalid,
789 /// this operation has no effect. See the `bslx` package-level
790 /// documentation for more information on BDEX streaming of
791 /// value-semantic types and containers.
792 template <class STREAM>
793 STREAM& bdexStreamOut(STREAM& stream,
794 const bsl::string& value,
795 int version = 0);
796
797 /// Write the specified `bsl::vector<char, ALLOC>` `value` to the
798 /// specified output `stream`, and return a reference to `stream`. The
799 /// specified `version` is ignored. If `stream` is initially invalid,
800 /// this operation has no effect. See the `bslx` package-level
801 /// documentation for more information on BDEX streaming of
802 /// value-semantic types and containers.
803 template <class STREAM, class ALLOC>
804 STREAM& bdexStreamOut(STREAM& stream,
805 const bsl::vector<char, ALLOC>& value,
806 int version);
807
808 /// Write the specified `bsl::vector<signed char, ALLOC>` `value` to the
809 /// specified output `stream`, and return a reference to `stream`. The
810 /// specified `version` is ignored. If `stream` is initially invalid,
811 /// this operation has no effect. See the `bslx` package-level
812 /// documentation for more information on BDEX streaming of
813 /// value-semantic types and containers.
814 template <class STREAM, class ALLOC>
815 STREAM& bdexStreamOut(STREAM& stream,
817 int version);
818
819 /// Write the specified `bsl::vector<unsigned char, ALLOC>` `value` to
820 /// the specified output `stream`, and return a reference to `stream`.
821 /// The specified `version` is ignored. If `stream` is initially
822 /// invalid, this operation has no effect. See the `bslx` package-level
823 /// documentation for more information on BDEX streaming of
824 /// value-semantic types and containers.
825 template <class STREAM, class ALLOC>
826 STREAM& bdexStreamOut(STREAM& stream,
828 int version);
829
830 /// Write the specified `bsl::vector<short, ALLOC>` `value` to the
831 /// specified output `stream`, and return a reference to `stream`. The
832 /// specified `version` is ignored. If `stream` is initially invalid,
833 /// this operation has no effect. See the `bslx` package-level
834 /// documentation for more information on BDEX streaming of
835 /// value-semantic types and containers.
836 template <class STREAM, class ALLOC>
837 STREAM& bdexStreamOut(STREAM& stream,
838 const bsl::vector<short, ALLOC>& value,
839 int version);
840
841 /// Write the specified `bsl::vector<unsigned short, ALLOC>` `value` to
842 /// the specified output `stream`, and return a reference to `stream`.
843 /// The specified `version` is ignored. If `stream` is initially
844 /// invalid, this operation has no effect. See the `bslx` package-level
845 /// documentation for more information on BDEX streaming of
846 /// value-semantic types and containers.
847 template <class STREAM, class ALLOC>
848 STREAM& bdexStreamOut(STREAM& stream,
850 int version);
851
852 /// Write the specified `bsl::vector<int, ALLOC>` `value` to the
853 /// specified output `stream`, and return a reference to `stream`. The
854 /// specified `version` is ignored. If `stream` is initially invalid,
855 /// this operation has no effect. See the `bslx` package-level
856 /// documentation for more information on BDEX streaming of
857 /// value-semantic types and containers.
858 template <class STREAM, class ALLOC>
859 STREAM& bdexStreamOut(STREAM& stream,
860 const bsl::vector<int, ALLOC>& value,
861 int version);
862
863 /// Write the specified `bsl::vector<unsigned int, ALLOC>` `value` to
864 /// the specified output `stream`, and return a reference to `stream`.
865 /// The specified `version` is ignored. If `stream` is initially
866 /// invalid, this operation has no effect. See the `bslx` package-level
867 /// documentation for more information on BDEX streaming of
868 /// value-semantic types and containers.
869 template <class STREAM, class ALLOC>
870 STREAM& bdexStreamOut(STREAM& stream,
872 int version);
873
874 /// Write the specified `bsl::vector<bsls::Types::Int64, ALLOC>` `value`
875 /// to the specified output `stream`, and return a reference to
876 /// `stream`. The specified `version` is ignored. If `stream` is
877 /// initially invalid, this operation has no effect. See the `bslx`
878 /// package-level documentation for more information on BDEX streaming
879 /// of value-semantic types and containers.
880 template <class STREAM, class ALLOC>
881 STREAM& bdexStreamOut(
882 STREAM& stream,
884 int version);
885
886 /// Write the specified `bsl::vector<bsls::Types::Uint64, ALLOC>`
887 /// `value` to the specified output `stream`, and return a reference to
888 /// `stream`. The specified `version` is ignored. If `stream` is
889 /// initially invalid, this operation has no effect. See the `bslx`
890 /// package-level documentation for more information on BDEX streaming
891 /// of value-semantic types and containers.
892 template <class STREAM, class ALLOC>
893 STREAM& bdexStreamOut(
894 STREAM& stream,
896 int version);
897
898 /// Write the specified `bsl::vector<float, ALLOC>` `value` to the
899 /// specified output `stream`, and return a reference to `stream`. The
900 /// specified `version` is ignored. If `stream` is initially invalid,
901 /// this operation has no effect. See the `bslx` package-level
902 /// documentation for more information on BDEX streaming of
903 /// value-semantic types and containers.
904 template <class STREAM, class ALLOC>
905 STREAM& bdexStreamOut(STREAM& stream,
906 const bsl::vector<float, ALLOC>& value,
907 int version);
908
909 /// Write the specified `bsl::vector<double, ALLOC>` `value` to the
910 /// specified output `stream`, and return a reference to `stream`. The
911 /// specified `version` is ignored. If `stream` is initially invalid,
912 /// this operation has no effect. See the `bslx` package-level
913 /// documentation for more information on BDEX streaming of
914 /// value-semantic types and containers.
915 template <class STREAM, class ALLOC>
916 STREAM& bdexStreamOut(STREAM& stream,
917 const bsl::vector<double, ALLOC>& value,
918 int version);
919
920 /// Write the specified `bsl::vector<TYPE, ALLOC>` `value` to the
921 /// specified output `stream`, and return a reference to `stream`. If
922 /// `stream` is initially invalid, this operation has no effect. If
923 /// needed, first write the computed version information to the `stream`
924 /// and if this version is not supported by `TYPE` and the vector is not
925 /// empty, `stream` is invalidated. Note that the version is only
926 /// needed when the (template parameter) `TYPE` is a `bsl::vector` or a
927 /// user-defined type. See the `bslx` package-level documentation for
928 /// more information on BDEX streaming of value-semantic types and
929 /// containers.
930 template <class STREAM, class TYPE, class ALLOC>
931 STREAM& bdexStreamOut(STREAM& stream,
932 const bsl::vector<TYPE, ALLOC>& value);
933
934 /// Write the specified `bsl::vector<TYPE, ALLOC>` `value`, using the
935 /// specified `version` format, to the specified output `stream`, and
936 /// return a reference to `stream`. If `stream` is initially invalid,
937 /// this operation has no effect. If `version` is not supported by
938 /// `TYPE` and the vector is not empty, `stream` is invalidated, but
939 /// otherwise unmodified. Note that the specified `TYPE` might not
940 /// require a `version` to be serialized and that `version` is not
941 /// written to `stream`. See the `bslx` package-level documentation for
942 /// more information on BDEX streaming of value-semantic types and
943 /// containers.
944 template <class STREAM, class TYPE, class ALLOC>
945 STREAM& bdexStreamOut(STREAM& stream,
946 const bsl::vector<TYPE, ALLOC>& value,
947 int version);
948
949} // close namespace OutStreamFunctions
950
951// ============================================================================
952// INLINE FUNCTION DEFINITIONS
953// ============================================================================
954
955 // ----------------------------
956 // namespace OutStreamFunctions
957 // ----------------------------
958
959template <class STREAM, class TYPE>
960inline
962 const TYPE& value,
963 int /* version */,
964 const IsEnumType&)
965{
966 // A compilation error indicating the next line of code implies the 'TYPE'
967 // cannot be represented as a 32-bit 'int' and an overload for the
968 // 'OutStreamFunctions::bdexStreamOut' function, in the enumeration's
969 // namespace, should be provided.
970
971 BSLMF_ASSERT(sizeof(TYPE) <= sizeof(bsl::int32_t));
972
973 // Stream the 'enum' value as a 32-bit 'int'.
974 return stream.putInt32(static_cast<int>(value));
975}
976
977template <class STREAM, class TYPE>
978inline
980 const TYPE& value,
981 int version,
982 const IsNotEnumType&)
983{
984 // A compilation error indicating the next line of code implies the class
985 // of 'TYPE' does not support the 'bdexStreamOut' method.
986
987 return value.bdexStreamOut(stream, version);
988}
989
990template <class STREAM, class TYPE>
991inline
992STREAM& OutStreamFunctions::bdexStreamOut(STREAM& stream, const TYPE& value)
993{
995
996 const int version = maxSupportedBdexVersion(&value,
997 stream.bdexVersionSelector());
998 if (VersionFunctions::k_NO_VERSION != version) {
999 stream.putVersion(version);
1000 }
1001
1002 return bdexStreamOut(stream, value, version);
1003}
1004
1005template <class STREAM, class TYPE>
1006inline
1008 const TYPE& value,
1009 int version)
1010{
1012 IsEnumType,
1013 IsNotEnumType>::type dummyType;
1014 return bdexStreamOutImp(stream, value, version, dummyType());
1015}
1016
1017template <class STREAM>
1018inline
1020 const bool& value,
1021 int /* version */)
1022{
1023 return stream.putInt8(static_cast<char>(value));
1024}
1025
1026template <class STREAM>
1027inline
1029 const char& value,
1030 int /* version */)
1031{
1032 return stream.putInt8(value);
1033}
1034
1035template <class STREAM>
1036inline
1038 const signed char& value,
1039 int /* version */)
1040{
1041 return stream.putInt8(value);
1042}
1043
1044template <class STREAM>
1045inline
1047 const unsigned char& value,
1048 int /* version */)
1049{
1050 return stream.putUint8(value);
1051}
1052
1053template <class STREAM>
1054inline
1056 const short& value,
1057 int /* version */)
1058{
1059 return stream.putInt16(value);
1060}
1061
1062template <class STREAM>
1063inline
1065 const unsigned short& value,
1066 int /* version */)
1067{
1068 return stream.putUint16(value);
1069}
1070
1071template <class STREAM>
1072inline
1074 const int& value,
1075 int /* version */)
1076{
1077 return stream.putInt32(value);
1078}
1079
1080template <class STREAM>
1081inline
1083 const unsigned int& value,
1084 int /* version */)
1085{
1086 return stream.putUint32(value);
1087}
1088
1089template <class STREAM>
1090inline
1092 const long& value,
1093 int /* version */)
1094{
1095 return stream.putInt32(static_cast<int>(value));
1096}
1097
1098template <class STREAM>
1099inline
1101 const unsigned long& value,
1102 int /* version */)
1103{
1104 return stream.putUint32(static_cast<unsigned int>(value));
1105}
1106
1107template <class STREAM>
1108inline
1110 const bsls::Types::Int64& value,
1111 int /* version */)
1112{
1113 return stream.putInt64(value);
1114}
1115
1116template <class STREAM>
1117inline
1119 STREAM& stream,
1120 const bsls::Types::Uint64& value,
1121 int /* version */)
1122{
1123 return stream.putUint64(value);
1124}
1125
1126template <class STREAM>
1127inline
1129 const float& value,
1130 int /* version */)
1131{
1132 return stream.putFloat32(value);
1133}
1134
1135template <class STREAM>
1136inline
1138 const double& value,
1139 int /* version */)
1140{
1141 return stream.putFloat64(value);
1142}
1143
1144template <class STREAM>
1145inline
1147 const bsl::string& value,
1148 int /* version */)
1149{
1150 return stream.putString(value);
1151}
1152
1153template <class STREAM, class ALLOC>
1154inline
1156 STREAM& stream,
1157 const bsl::vector<char, ALLOC>& value,
1158 int /* version */)
1159{
1160 const int length = static_cast<int>(value.size());
1161 stream.putLength(length);
1162
1163 return 0 < length ? stream.putArrayInt8(&value[0], length) : stream;
1164}
1165
1166template <class STREAM, class ALLOC>
1167inline
1169 STREAM& stream,
1171 int /* version */)
1172{
1173 const int length = static_cast<int>(value.size());
1174 stream.putLength(length);
1175
1176 return 0 < length ? stream.putArrayInt8(&value[0], length) : stream;
1177}
1178
1179template <class STREAM, class ALLOC>
1180inline
1182 STREAM& stream,
1184 int /* version */)
1185{
1186 const int length = static_cast<int>(value.size());
1187 stream.putLength(length);
1188
1189 return 0 < length ? stream.putArrayUint8(&value[0], length) : stream;
1190}
1191
1192template <class STREAM, class ALLOC>
1193inline
1195 STREAM& stream,
1196 const bsl::vector<short, ALLOC>& value,
1197 int /* version */)
1198{
1199 const int length = static_cast<int>(value.size());
1200 stream.putLength(length);
1201
1202 return 0 < length ? stream.putArrayInt16(&value[0], length) : stream;
1203}
1204
1205template <class STREAM, class ALLOC>
1206inline
1208 STREAM& stream,
1210 int /* version */)
1211{
1212 const int length = static_cast<int>(value.size());
1213 stream.putLength(length);
1214
1215 return 0 < length ? stream.putArrayUint16(&value[0], length) : stream;
1216}
1217
1218template <class STREAM, class ALLOC>
1219inline
1221 STREAM& stream,
1222 const bsl::vector<int, ALLOC>& value,
1223 int /* version */)
1224{
1225 const int length = static_cast<int>(value.size());
1226 stream.putLength(length);
1227
1228 return 0 < length ? stream.putArrayInt32(&value[0], length) : stream;
1229}
1230
1231template <class STREAM, class ALLOC>
1232inline
1234 STREAM& stream,
1236 int /* version */)
1237{
1238 const int length = static_cast<int>(value.size());
1239 stream.putLength(length);
1240
1241 return 0 < length ? stream.putArrayUint32(&value[0], length) : stream;
1242}
1243
1244template <class STREAM, class ALLOC>
1245inline
1247 STREAM& stream,
1249 int /* version */)
1250{
1251 const int length = static_cast<int>(value.size());
1252 stream.putLength(length);
1253
1254 return 0 < length ? stream.putArrayInt64(&value[0], length) : stream;
1255}
1256
1257template <class STREAM, class ALLOC>
1258inline
1260 STREAM& stream,
1262 int /* version */)
1263{
1264 const int length = static_cast<int>(value.size());
1265 stream.putLength(length);
1266
1267 return 0 < length ? stream.putArrayUint64(&value[0], length) : stream;
1268}
1269
1270template <class STREAM, class ALLOC>
1271inline
1273 STREAM& stream,
1274 const bsl::vector<float, ALLOC>& value,
1275 int /* version */)
1276{
1277 const int length = static_cast<int>(value.size());
1278 stream.putLength(length);
1279
1280 return 0 < length ? stream.putArrayFloat32(&value[0], length) : stream;
1281}
1282
1283template <class STREAM, class ALLOC>
1284inline
1286 STREAM& stream,
1287 const bsl::vector<double, ALLOC>& value,
1288 int /* version */)
1289{
1290 const int length = static_cast<int>(value.size());
1291 stream.putLength(length);
1292
1293 return 0 < length ? stream.putArrayFloat64(&value[0], length) : stream;
1294}
1295
1296template <class STREAM, class TYPE, class ALLOC>
1297inline
1299 STREAM& stream,
1300 const bsl::vector<TYPE, ALLOC>& value)
1301{
1303
1304 const int version = maxSupportedBdexVersion(&value,
1305 stream.bdexVersionSelector());
1306
1307 stream.putVersion(version);
1308 return bdexStreamOut(stream, value, version);
1309}
1310
1311template <class STREAM, class TYPE, class ALLOC>
1313 STREAM& stream,
1314 const bsl::vector<TYPE, ALLOC>& value,
1315 int version)
1316{
1317 typedef typename bsl::vector<TYPE, ALLOC>::const_iterator Iterator;
1318
1319 const int length = static_cast<int>(value.size());
1320 stream.putLength(length);
1321
1322 for (Iterator it = value.begin(); it != value.end(); ++it) {
1323 bdexStreamOut(stream, *it, version);
1324 }
1325
1326 return stream;
1327}
1328
1329} // close package namespace
1330
1331
1332#endif
1333
1334// ----------------------------------------------------------------------------
1335// Copyright 2014 Bloomberg Finance L.P.
1336//
1337// Licensed under the Apache License, Version 2.0 (the "License");
1338// you may not use this file except in compliance with the License.
1339// You may obtain a copy of the License at
1340//
1341// http://www.apache.org/licenses/LICENSE-2.0
1342//
1343// Unless required by applicable law or agreed to in writing, software
1344// distributed under the License is distributed on an "AS IS" BASIS,
1345// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1346// See the License for the specific language governing permissions and
1347// limitations under the License.
1348// ----------------------------- END-OF-FILE ----------------------------------
1349
1350/** @} */
1351/** @} */
1352/** @} */
Definition bslstl_string.h:1281
size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the number of elements in this vector.
Definition bslstl_vector.h:2664
iterator begin() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2511
iterator end() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2519
Definition bslstl_vector.h:1025
VALUE_TYPE const * const_iterator
Definition bslstl_vector.h:1058
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:229
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
STREAM & bdexStreamOut(STREAM &stream, const TYPE &value)
Definition bslx_outstreamfunctions.h:992
STREAM & bdexStreamOutImp(STREAM &stream, const TYPE &value, int version, const IsEnumType &)
Definition bslx_outstreamfunctions.h:961
int maxSupportedBdexVersion(const TYPE *, int versionSelector)
Definition bslx_versionfunctions.h:519
@ k_NO_VERSION
Definition bslx_versionfunctions.h:334
Definition bslx_byteinstream.h:377
Definition bslmf_conditional.h:120
unsigned long long Uint64
Definition bsls_types.h:137
long long Int64
Definition bsls_types.h:132
Definition bslx_outstreamfunctions.h:565
Definition bslx_outstreamfunctions.h:576