BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslx_instreamfunctions.h
Go to the documentation of this file.
1/// @file bslx_instreamfunctions.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslx_instreamfunctions.h -*-C++-*-
8#ifndef INCLUDED_BSLX_INSTREAMFUNCTIONS
9#define INCLUDED_BSLX_INSTREAMFUNCTIONS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslx_instreamfunctions bslx_instreamfunctions
15/// @brief Facilitate uniform unexternalization of user and fundamental types.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslx
19/// @{
20/// @addtogroup bslx_instreamfunctions
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslx_instreamfunctions-purpose"> Purpose</a>
25/// * <a href="#bslx_instreamfunctions-classes"> Classes </a>
26/// * <a href="#bslx_instreamfunctions-description"> Description </a>
27/// * <a href="#bslx_instreamfunctions-component-design-anticipated-usage-and-the-bdex-contract"> Component Design, Anticipated Usage, and the BDEX Contract </a>
28/// * <a href="#bslx_instreamfunctions-implementing-bdex-streaming-in-value-semantic-template-classes"> Implementing BDEX Streaming in Value-Semantic Template Classes </a>
29/// * <a href="#bslx_instreamfunctions-usage"> Usage </a>
30/// * <a href="#bslx_instreamfunctions-example-1-using-bslx-instreamfunctions-to-unexternalize-data"> Example 1: Using bslx::InStreamFunctions to Unexternalize Data </a>
31///
32/// # Purpose {#bslx_instreamfunctions-purpose}
33/// Facilitate uniform unexternalization of user and fundamental types.
34///
35/// # Classes {#bslx_instreamfunctions-classes}
36///
37/// - bslx::InStreamFunctions: namespace for BDEX unexternalization functions
38///
39/// @see bslx_outstreamfunctions, bslx_versionfunctions
40///
41/// # Description {#bslx_instreamfunctions-description}
42/// This component provides a namespace, `bslx::InStreamFunctions`,
43/// that facilitates uniform support for BDEX unexternalization across all
44/// BDEX-compliant user-defined types, including template types and containers,
45/// as well as those fundamental types (and `bsl::string` and `bsl::vector`) for
46/// which the BDEX protocol provides direct support.
47///
48/// The namespace `bslx::InStreamFunctions` facilitates client unexternalization
49/// of objects in a uniform, type-independent manner. It contains the
50/// `bdexStreamIn` function that unexternalizes objects of all BDEX-compliant
51/// types. This function unexternalizes the specified `object` in the specified
52/// `version` or the `version` read from the input stream as required by the
53/// BDEX protocol. The `bdexStreamIn` function is overloaded for fundamental
54/// types, enumeration types, `bsl::string`, and `bsl::vector`. Note that,
55/// excluding `bsl::vector`, version information is never read from the stream
56/// while unexternalizing these types.
57///
58/// By default, objects of enumeration type are streamed in as 32-bit `int`
59/// values. Users can override this behavior by providing overloads of the
60/// `InStreamFunctions::bdexStreamIn` 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& bdexStreamIn(STREAM& stream, MyEnum& variable, int version)
65/// {
66/// using bslx::InStreamFunctions::bdexStreamIn;
67///
68/// // Code to stream in 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/// `bdexStreamIn` calls the `bdexStreamIn` member function for that type.
75///
76/// ## Component Design, Anticipated Usage, and the BDEX Contract {#bslx_instreamfunctions-component-design-anticipated-usage-and-the-bdex-contract}
77///
78///
79/// @ref bslx_instreamfunctions is an integral part of the BDEX unexternalization
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_instreamfunctions 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_instreamfunctions is helpful
87/// (or required) for these different developers. By discussing different
88/// aspects of usage, we convey the general design goals of this component, and,
89/// to a certain extent, the overall BDEX contract. See the `bslx`
90/// package-level documentation for a full specification of the BDEX contract.
91///
92/// ### Implementing BDEX Streaming in Value-Semantic Template Classes {#bslx_instreamfunctions-implementing-bdex-streaming-in-value-semantic-template-classes}
93///
94///
95/// The author of a non-template value-semantic type has full knowledge of the
96/// details of the "value" of that type, and may choose to use the appropriate
97/// input stream `get` methods directly when implementing the required
98/// `bdexStreamIn` method for that type. However, if one or more aspects of the
99/// value are of template parameter type, then the author cannot in general know
100/// how to stream the value using the `get` methods. For example, if a type has
101/// as its value one `int` data member:
102/// @code
103/// int d_value;
104/// @endcode
105/// then the implementation of the `bdexStreamIn` method can contain:
106/// @code
107/// stream.getInt32(d_value);
108/// @endcode
109/// However, if the data member is of (template parameter) `VALUE_TYPE`:
110/// @code
111/// VALUE_TYPE d_value;
112/// @endcode
113/// then the implementation of the `bdexStreamIn` method must rely on the
114/// `bslx::InStreamFunctions` implementation to input the value:
115/// @code
116/// using bslx::InStreamFunctions::bdexStreamIn;
117/// bdexStreamIn(stream, d_value, 1);
118/// @endcode
119/// This call will resolve to the correct sequence of `get` calls no matter
120/// whether `VALUE_TYPE` is a fundamental type, a BDEX-compliant `enum`, or a
121/// proper BDEX-compliant class. In the latter two cases, the explicit
122/// specification of the version format (in this case, 1) guarantees the stable
123/// operation of this method whether or not `VALUE_TYPE` is provided additional
124/// version formats.
125///
126/// ## Usage {#bslx_instreamfunctions-usage}
127///
128///
129/// This section illustrates intended use of this component.
130///
131/// ### Example 1: Using bslx::InStreamFunctions to Unexternalize Data {#bslx_instreamfunctions-example-1-using-bslx-instreamfunctions-to-unexternalize-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 three separate example
137/// "components": an `enum`, a value-semantic point object, and an input stream.
138/// In all cases, the component designs are very simple, with much of the
139/// implied functionality omitted, in order to focus attention on the key
140/// aspects of the 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 input byte
158/// pattern somewhat easier for the reader of this example to recognize when the
159/// input buffer is printed.
160/// @code
161/// // mypoint.h
162///
163/// /// This class provides a geometric point having integer coordinates and
164/// /// an enumerated color property.
165/// class MyPoint {
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/// // ...
174///
175/// // CREATORS
176///
177/// /// Create a default point.
178/// MyPoint();
179///
180/// /// Create a point having the specified `x` and `y` coordinates and the
181/// /// specified `color`.
182/// MyPoint(short x, short y, Color color);
183///
184/// /// Destroy this point.
185/// ~MyPoint();
186///
187/// // MANIPULATORS
188/// // ...
189///
190/// // ACCESSORS
191///
192/// /// Return the x coordinate of this point.
193/// short x() const;
194///
195/// /// Return the y coordinate of this point.
196/// short y() const;
197///
198/// /// Return the enumerated color of this point.
199/// Color color() const;
200///
201/// /// Assign to this object the value read from the specified input
202/// /// `stream` using the specified `version` format, and return a
203/// /// reference to `stream`. If `stream` is initially invalid, this
204/// /// operation has no effect. If `version` is not supported, this
205/// /// object is unaltered and `stream` is invalidated, but otherwise
206/// /// unmodified. If `version` is supported but `stream` becomes
207/// /// invalid during this operation, this object has an undefined, but
208/// /// valid, state. Note that no version is read from `stream`. See
209/// /// the `bslx` package-level documentation for more information on
210/// /// BDEX streaming of value-semantic types and containers.
211/// template <class STREAM>
212/// STREAM& bdexStreamIn(STREAM& stream, int version);
213/// };
214///
215/// // FREE OPERATORS
216///
217/// /// Return `true` if the specified `lhs` and `rhs` points have the same
218/// /// value, and `false` otherwise. Two points have the same value if
219/// /// they have the same x and y coordinates and the same color.
220/// inline
221/// bool operator==(const MyPoint& lhs, const MyPoint& rhs);
222/// @endcode
223/// Representative (inline) implementations of these methods are shown below:
224/// @code
225/// // ========================================================================
226/// // INLINE FUNCTION DEFINITIONS
227/// // ========================================================================
228///
229/// // CREATORS
230/// inline
231/// MyPoint::MyPoint()
232/// {
233/// }
234///
235/// inline
236/// MyPoint::MyPoint(short x, short y, Color color)
237/// : d_x(x)
238/// , d_y(y)
239/// , d_color(color)
240/// {
241/// }
242///
243/// inline
244/// MyPoint::~MyPoint()
245/// {
246/// }
247///
248/// // ...
249///
250/// // MANIPULATORS
251/// // ...
252///
253/// // ACCESSORS
254/// inline
255/// Color MyPoint::color() const
256/// {
257/// return d_color;
258/// }
259///
260/// inline
261/// short MyPoint::x() const
262/// {
263/// return d_x;
264/// }
265///
266/// inline
267/// short MyPoint::y() const
268/// {
269/// return d_y;
270/// }
271/// // ...
272///
273/// template <class STREAM>
274/// STREAM& MyPoint::bdexStreamIn(STREAM& stream, int version)
275/// {
276/// switch (version) {
277/// case 1: {
278/// stream.getInt16(d_x); // input the x coordinate
279/// stream.getInt16(d_y); // input the y coordinate
280/// char color;
281/// stream.getInt8(color); // input the color enum as one byte
282/// d_color = static_cast<Color>(color);
283/// } break;
284/// default: {
285/// stream.invalidate();
286/// } break;
287/// }
288/// return stream;
289/// }
290///
291/// // FREE OPERATORS
292/// inline
293/// bool operator==(const MyPoint& lhs, const MyPoint& rhs)
294/// {
295/// return lhs.x() == rhs.x()
296/// && lhs.y() == rhs.y()
297/// && lhs.color() == rhs.color();
298/// }
299/// @endcode
300/// Then, we will implement an extremely simple input stream that supports the
301/// BDEX documentation-only protocol. For simplicity, we will use an externally
302/// managed buffer, and will only show a few methods needed for this example.
303/// @code
304/// // myinstream.h
305/// // ...
306///
307/// /// This class implements a limited-size fixed-buffer input stream that
308/// /// partially conforms to the BDEX protocol for input streams. This
309/// /// class is suitable for demonstration purposes only.
310/// class MyInStream {
311///
312/// const char *d_buffer; // input buffer, held but not owned
313/// int d_length; // length of `d_buffer` (bytes)
314/// int d_cursor; // cursor (index into `d_buffer`)
315///
316/// public:
317/// // CREATORS
318///
319/// /// Create an input stream using the specified `buffer` having the
320/// /// specified `length` (in bytes).
321/// MyInStream(const char *buffer, int length);
322///
323/// /// Destroy this input byte stream.
324/// ~MyInStream();
325///
326/// // MANIPULATORS
327///
328/// /// Consume a version value from this input stream, store that value
329/// /// in the specified `version`, and return a reference to this
330/// /// stream. ...
331/// MyInStream& getVersion(int& version);
332///
333/// /// Consume a 32-bit signed integer value from this input stream,
334/// /// store that value in the specified `value`, and return a reference
335/// /// to this stream. ...
336/// MyInStream& getInt32(int& value);
337///
338/// /// Consume a 16-bit signed integer value from this input stream,
339/// /// store that value in the specified `value`, and return a reference
340/// /// to this stream. ...
341/// MyInStream& getInt16(short& value);
342///
343/// /// Consume an 8-bit signed integer value from this input stream,
344/// /// store that value in the specified `value`, and return a reference
345/// /// to this stream. ...
346/// MyInStream& getInt8(char& value);
347///
348/// /// Put this input stream in an invalid state. ...
349/// void invalidate();
350///
351/// // ACCESSORS
352///
353/// /// Return a non-zero value if this stream is valid, and 0
354/// /// otherwise. An invalid stream is a stream in which insufficient
355/// /// or invalid data was detected during an extraction operation.
356/// /// Note that an empty stream will be valid unless an extraction
357/// /// attempt or explicit invalidation causes it to be otherwise.
358/// operator const void *() const;
359///
360/// /// Return the index of the next byte to be extracted from this
361/// /// stream.
362/// int cursor() const;
363///
364/// /// Return `true` if this stream is empty, and `false` otherwise.
365/// /// Note that this function enables higher-level types to verify
366/// /// that, after successfully reading all expected data, no data
367/// /// remains.
368/// bool isEmpty() const;
369///
370/// /// Return the total number of bytes stored in this stream.
371/// int length() const;
372/// };
373///
374/// @endcode
375/// The relevant (inline) implementations are as follows.
376/// @code
377/// // ========================================================================
378/// // INLINE FUNCTION DEFINITIONS
379/// // ========================================================================
380///
381/// // CREATORS
382/// inline
383/// MyInStream::MyInStream(const char *buffer, int length)
384/// : d_buffer(buffer)
385/// , d_length(length)
386/// , d_cursor(0)
387/// {
388/// }
389///
390/// inline
391/// MyInStream::~MyInStream()
392/// {
393/// }
394///
395/// // MANIPULATORS
396///
397/// inline
398/// MyInStream& MyInStream::getVersion(int& value)
399/// {
400/// value = static_cast<unsigned char>(d_buffer[d_cursor++]);
401/// return *this;
402/// }
403///
404/// inline
405/// MyInStream& MyInStream::getInt32(int& value)
406/// {
407/// const unsigned char *buffer =
408/// reinterpret_cast<const unsigned char *>(d_buffer);
409/// value = static_cast<int>((buffer[d_cursor ] << 24U) |
410/// (buffer[d_cursor + 1] << 16U) |
411/// (buffer[d_cursor + 2] << 8U) |
412/// (buffer[d_cursor + 3] ));
413/// d_cursor += 4;
414/// return *this;
415/// }
416///
417/// inline
418/// MyInStream& MyInStream::getInt16(short& value)
419/// {
420/// const unsigned char *buffer =
421/// reinterpret_cast<const unsigned char *>(d_buffer);
422/// value = static_cast<short>((buffer[d_cursor ] << 8) |
423/// (buffer[d_cursor + 1] ));
424/// d_cursor += 2;
425/// return *this;
426/// }
427///
428/// inline
429/// MyInStream& MyInStream::getInt8(char& value)
430/// {
431/// value = d_buffer[d_cursor];
432/// d_cursor += 1;
433/// return *this;
434/// }
435///
436/// inline
437/// void MyInStream::invalidate()
438/// {
439/// d_buffer = 0;
440/// }
441///
442/// // ACCESSORS
443/// inline
444/// MyInStream::operator const void *() const
445/// {
446/// return d_cursor <= d_length ? d_buffer : 0;
447/// }
448///
449/// inline
450/// int MyInStream::cursor() const
451/// {
452/// return d_cursor;
453/// }
454///
455/// inline
456/// bool MyInStream::isEmpty() const
457/// {
458/// return d_cursor >= d_length;
459/// }
460///
461/// inline
462/// int MyInStream::length() const
463/// {
464/// return d_length;
465/// }
466/// @endcode
467/// Finally, use the above `enum`, point class, and input stream to illustrate
468/// `bslx::InStreamFunctions` functionality. This test code does not attempt to
469/// do anything more useful than reading values from a stream whose buffer was
470/// written "by hand" and confirming that the expected values were read
471/// correctly from the known byte pattern in the buffer.
472/// @code
473/// using bslx::InStreamFunctions::bdexStreamIn;
474///
475/// {
476/// const int EXP = 0x0A0B0C0D;
477/// const char buffer[4] = { 0xA, 0xB, 0xC, 0xD }; // 'int' (no version)
478/// int i = 0;
479///
480/// MyInStream in1(buffer, 4); // use the one buffer
481/// bdexStreamIn(in1, i, 1);
482/// assert(in1); assert(EXP == i);
483///
484/// i = 0; // reset 'i'
485/// MyInStream in2(buffer, 4); // re-use 'buffer (no version)
486/// bdexStreamIn(in2, i, 0);
487/// assert(in2); assert(EXP == i);
488/// }
489///
490/// {
491/// const MyPoint EXP(0, -1, BLUE);
492/// const char buffer1[5] = { 0, 0, -1, -1, 2 }; // 'MyPoint' (no ver)
493/// const char buffer2[6] = { 1, 0, 0, -1, -1, 2 }; // version, 'MyPoint'
494/// MyPoint p1, p2; // two default points
495///
496/// MyInStream in1(buffer1, 5); // 'buffer1' has no version byte
497/// bdexStreamIn(in1, p1, 1);
498/// assert(in1); assert(EXP == p1);
499///
500/// MyInStream in2(buffer2, 6); // 'buffer2' *has* a version
501/// int version;
502/// in2.getVersion(version);
503/// assert(1 == version);
504/// bdexStreamIn(in2, p2, version);
505/// assert(in2); assert(EXP == p2);
506/// }
507/// @endcode
508/// @}
509/** @} */
510/** @} */
511
512/** @addtogroup bsl
513 * @{
514 */
515/** @addtogroup bslx
516 * @{
517 */
518/** @addtogroup bslx_instreamfunctions
519 * @{
520 */
521
522#include <bslscm_version.h>
523
525
526#include <bslmf_assert.h>
527#include <bslmf_conditional.h>
528#include <bslmf_isenum.h>
529
530#include <bsls_performancehint.h>
531#include <bsls_types.h>
532
533#include <bsl_string.h>
534#include <bsl_vector.h>
535
536#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
537#include <bslmf_if.h>
538#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
539
540
541namespace bslx {
542 // ===========================
543 // namespace InStreamFunctions
544 // ===========================
545
546/// This namespace facilitates unexternalization of all BDEX-compliant types
547/// in a type-independent manner. The unexternalization functions are
548/// overloaded for fundamental types, enumeration types, `bsl::string`, and
549/// `bsl::vector`. A compilation error will occur if the (template
550/// parameter) `VALUE_TYPE` of a non-overloaded method of
551/// `bslx::InStreamFunctions` does not support `bdexStreamIn` (with the
552/// appropriate signature).
553namespace InStreamFunctions {
554
555
556 // ======================================
557 // class InStreamFunctions_AccessorHelper
558 // ======================================
559
560/// This `struct` provides a namespace for implementation helper functions
561/// for this component. They are not intended for use outside this
562/// component.
563///
564/// See @ref bslx_instreamfunctions
565template <class STREAM>
567
568 /// Load into the specified `result` the specified `length` values read
569 /// from the specified `stream`, and return a reference to `stream`. If
570 /// `stream` is initially invalid, `*result` is unchanged. If `stream`
571 /// becomes invalid during this operation, the contents of `result` have
572 /// an undefined, but valid, state.
573 static STREAM& getArray(STREAM& stream, bool *result, int length);
574 static STREAM& getArray(STREAM& stream, char *result, int length);
575 static STREAM& getArray(STREAM& stream, signed char *result, int length);
576 static STREAM& getArray(STREAM& stream, unsigned char *result, int length);
577 static STREAM& getArray(STREAM& stream, short *result, int length);
578 static STREAM& getArray(STREAM& stream,
579 unsigned short *result,
580 int length);
581 static STREAM& getArray(STREAM& stream, int *result, int length);
582 static STREAM& getArray(STREAM& stream, unsigned int *result, int length);
583 static STREAM& getArray(STREAM& stream,
584 bsls::Types::Int64 *result,
585 int length);
586 static STREAM& getArray(STREAM& stream,
587 bsls::Types::Uint64 *result,
588 int length);
589 static STREAM& getArray(STREAM& stream, float *result, int length);
590 static STREAM& getArray(STREAM& stream, double *result, int length);
591
592 /// Assign to the specified `variable` the
593 /// `bsl::vector<VALUE_TYPE, ALLOC>` value read from the specified input
594 /// `stream`, and return a reference to `stream`. If `stream` is
595 /// initially invalid, this operation has no effect. If `stream`
596 /// becomes invalid during this operation, `variable` has an undefined,
597 /// but valid, state.
598 template <class VALUE_TYPE, class ALLOC>
599 static STREAM& getArray(STREAM& stream,
601};
602
603 // =================
604 // struct IsEnumType
605 // =================
606
607/// This `struct`, together with `IsNotEnumType` (below), is used to
608/// distinguish enumeration types from other types in function overload
609/// resolution. This `struct` contains no interface or implementation by
610/// design, and is meant for internal use only.
611///
612/// See @ref bslx_instreamfunctions
614 };
615
616 // ====================
617 // struct IsNotEnumType
618 // ====================
619
620 /// This `struct`, together with `IsEnumType` (above), is used to
621 /// distinguish enumeration types from other types in function overload
622 /// resolution. This `struct` contains no interface or implementation
623 /// by design, and is meant for internal use only.
624 ///
625 /// See @ref bslx_instreamfunctions
627 };
628
629 // PRIVATE CLASS METHODS
630
631 /// Assign to the specified `variable` the `VALUE_TYPE` value read from
632 /// the specified input `stream`, and return a reference to `stream`.
633 /// The specified `version` is ignored. If `stream` is initially
634 /// invalid, this operation has no effect. If `version` is not
635 /// supported by `VALUE_TYPE`, `variable` is unaltered and `stream` is
636 /// invalidated, but otherwise unmodified. If `version` is supported
637 /// but `stream` becomes invalid during this operation, `variable` has an undefined, but valid, state.
638 ///
639 /// \note Note that this function is called
640 /// only for enumeration types and that this function is for internal
641 /// use only. See the `bslx` package-level documentation for more
642 /// information on BDEX streaming of value-semantic types and
643 /// containers.
644 template <class STREAM, class VALUE_TYPE>
645 STREAM& bdexStreamInImp(STREAM& stream,
646 VALUE_TYPE& variable,
647 int version,
648 const IsEnumType&);
649
650 /// Assign to the specified `variable` the `VALUE_TYPE` value read from
651 /// the specified input `stream` using the specified `version` format,
652 /// and return a reference to `stream`. If `stream` is initially
653 /// invalid, this operation has no effect. If `version` is not
654 /// supported by `VALUE_TYPE`, `variable` is unaltered and `stream` is
655 /// invalidated, but otherwise unmodified. If `version` is supported
656 /// but `stream` becomes invalid during this operation, `variable` has an undefined, but valid, state.
657 ///
658 /// \note Note that this function is for
659 /// internal use only. See the `bslx` package-level documentation for
660 /// more information on BDEX streaming of value-semantic types and
661 /// containers.
662 template <class STREAM, class VALUE_TYPE>
663 STREAM& bdexStreamInImp(STREAM& stream,
664 VALUE_TYPE& variable,
665 int version,
666 const IsNotEnumType&);
667
668 // CLASS METHODS
669
670 /// Assign to the specified `variable` the `VALUE_TYPE` value read from
671 /// the specified input `stream`, and return a reference to `stream`.
672 /// If `stream` is initially invalid, this operation has no effect. If
673 /// needed, first read the version information from the `stream` and if
674 /// this version is not supported by `VALUE_TYPE`, `stream` is
675 /// invalidated, but otherwise unmodified. If `stream` becomes invalid
676 /// during this operation, `variable` has an undefined, but valid, state.
677 ///
678 /// \note Note that the version is only needed when the (template
679 /// parameter) `VALUE_TYPE` is a `bsl::vector` or a user-defined type.
680 /// See the `bslx` package-level documentation for more information on
681 /// BDEX streaming of value-semantic types and containers.
682 template <class STREAM, class VALUE_TYPE>
683 STREAM& bdexStreamIn(STREAM& stream, VALUE_TYPE& variable);
684
685 /// Assign to the specified `variable` the `VALUE_TYPE` value read from
686 /// the specified input `stream` using the specified `version` format,
687 /// and return a reference to `stream`. If `stream` is initially
688 /// invalid, this operation has no effect. If `version` is not
689 /// supported by `VALUE_TYPE`, `variable` is unaltered and `stream` is
690 /// invalidated, but otherwise unmodified. If `version` is supported
691 /// but `stream` becomes invalid during this operation, `variable` has
692 /// an undefined, but valid, state. See the `bslx` package-level
693 /// documentation for more information on BDEX streaming of
694 /// value-semantic types and containers.
695 template <class STREAM, class VALUE_TYPE>
696 STREAM& bdexStreamIn(STREAM& stream, VALUE_TYPE& variable, int version);
697
698 /* overloads */
699
700 /// Assign to the specified `variable` the `bool` value read from the
701 /// specified input `stream`, and return a reference to `stream`. The
702 /// optionally specified `version` is ignored. If `stream` is initially
703 /// invalid, this operation has no effect. If `stream` becomes invalid
704 /// during this operation, `variable` has an undefined, but valid,
705 /// state. See the `bslx` package-level documentation for more
706 /// information on BDEX streaming of value-semantic types and
707 /// containers.
708 template <class STREAM>
709 STREAM& bdexStreamIn(STREAM& stream, bool& variable, int version = 0);
710
711 /// Assign to the specified `variable` the `char` value read from the
712 /// specified input `stream`, and return a reference to `stream`. The
713 /// optionally specified `version` is ignored. If `stream` is initially
714 /// invalid, this operation has no effect. If `stream` becomes invalid
715 /// during this operation, `variable` has an undefined, but valid,
716 /// state. See the `bslx` package-level documentation for more
717 /// information on BDEX streaming of value-semantic types and
718 /// containers.
719 template <class STREAM>
720 STREAM& bdexStreamIn(STREAM& stream, char& variable, int version = 0);
721
722 /// Assign to the specified `variable` the `signed char` value read from
723 /// the specified input `stream`, and return a reference to `stream`.
724 /// The optionally specified `version` is ignored. If `stream` is
725 /// initially invalid, this operation has no effect. If `stream`
726 /// becomes invalid during this operation, `variable` has an undefined,
727 /// but valid, state. See the `bslx` package-level documentation for
728 /// more information on BDEX streaming of value-semantic types and
729 /// containers.
730 template <class STREAM>
731 STREAM& bdexStreamIn(STREAM& stream,
732 signed char& variable,
733 int version = 0);
734
735 /// Assign to the specified `variable` the `unsigned char` value read
736 /// from the specified input `stream`, and return a reference to
737 /// `stream`. The optionally specified `version` is ignored. If
738 /// `stream` is initially invalid, this operation has no effect. If
739 /// `stream` becomes invalid during this operation, `variable` has an
740 /// undefined, but valid, state. See the `bslx` package-level
741 /// documentation for more information on BDEX streaming of
742 /// value-semantic types and containers.
743 template <class STREAM>
744 STREAM& bdexStreamIn(STREAM& stream,
745 unsigned char& variable,
746 int version = 0);
747
748 /// Assign to the specified `variable` the `short` value read from the
749 /// specified input `stream`, and return a reference to `stream`. The
750 /// optionally specified `version` is ignored. If `stream` is initially
751 /// invalid, this operation has no effect. If `stream` becomes invalid
752 /// during this operation, `variable` has an undefined, but valid,
753 /// state. See the `bslx` package-level documentation for more
754 /// information on BDEX streaming of value-semantic types and
755 /// containers.
756 template <class STREAM>
757 STREAM& bdexStreamIn(STREAM& stream, short& variable, int version = 0);
758
759 /// Assign to the specified `variable` the `unsigned short` value read
760 /// from the specified input `stream`, and return a reference to
761 /// `stream`. The optionally specified `version` is ignored. If
762 /// `stream` is initially invalid, this operation has no effect. If
763 /// `stream` becomes invalid during this operation, `variable` has an
764 /// undefined, but valid, state. See the `bslx` package-level
765 /// documentation for more information on BDEX streaming of
766 /// value-semantic types and containers.
767 template <class STREAM>
768 STREAM& bdexStreamIn(STREAM& stream,
769 unsigned short& variable,
770 int version = 0);
771
772 /// Assign to the specified `variable` the `int` value read from the
773 /// specified input `stream`, and return a reference to `stream`. The
774 /// optionally specified `version` is ignored. If `stream` is initially
775 /// invalid, this operation has no effect. If `stream` becomes invalid
776 /// during this operation, `variable` has an undefined, but valid,
777 /// state. See the `bslx` package-level documentation for more
778 /// information on BDEX streaming of value-semantic types and
779 /// containers.
780 template <class STREAM>
781 STREAM& bdexStreamIn(STREAM& stream, int& variable, int version = 0);
782
783 /// Assign to the specified `variable` the `unsigned int` value read
784 /// from the specified input `stream`, and return a reference to
785 /// `stream`. The optionally specified `version` is ignored. If
786 /// `stream` is initially invalid, this operation has no effect. If
787 /// `stream` becomes invalid during this operation, `variable` has an
788 /// undefined, but valid, state. See the `bslx` package-level
789 /// documentation for more information on BDEX streaming of
790 /// value-semantic types and containers.
791 template <class STREAM>
792 STREAM& bdexStreamIn(STREAM& stream,
793 unsigned int& variable,
794 int version = 0);
795
796 /// Assign to the specified `variable` the 32-bit `int` value read from
797 /// the specified input `stream`, and return a reference to `stream`.
798 /// The optionally specified `version` is ignored. If `stream` is
799 /// initially invalid, this operation has no effect. If `stream`
800 /// becomes invalid during this operation, `variable` has an undefined,
801 /// but valid, state. See the `bslx` package-level documentation for
802 /// more information on BDEX streaming of value-semantic types and
803 /// containers.
804 template <class STREAM>
805 STREAM& bdexStreamIn(STREAM& stream, long& variable, int version = 0);
806
807 /// Assign to the specified `variable` the 32-bit `unsigned int` value
808 /// read from the specified input `stream`, and return a reference to
809 /// `stream`. The optionally specified `version` is ignored. If
810 /// `stream` is initially invalid, this operation has no effect. If
811 /// `stream` becomes invalid during this operation, `variable` has an
812 /// undefined, but valid, state. See the `bslx` package-level
813 /// documentation for more information on BDEX streaming of
814 /// value-semantic types and containers.
815 template <class STREAM>
816 STREAM& bdexStreamIn(STREAM& stream,
817 unsigned long& variable,
818 int version = 0);
819
820 /// Assign to the specified `variable` the `bsls::Types::Int64` value
821 /// read from the specified input `stream`, and return a reference to
822 /// `stream`. The optionally specified `version` is ignored. If
823 /// `stream` is initially invalid, this operation has no effect. If
824 /// `stream` becomes invalid during this operation, `variable` has an
825 /// undefined, but valid, state. See the `bslx` package-level
826 /// documentation for more information on BDEX streaming of
827 /// value-semantic types and containers.
828 template <class STREAM>
829 STREAM& bdexStreamIn(STREAM& stream,
830 bsls::Types::Int64& variable,
831 int version = 0);
832
833 /// Assign to the specified `variable` the `bsls::Types::Uint64` value
834 /// read from the specified input `stream`, and return a reference to
835 /// `stream`. The optionally specified `version` is ignored. If
836 /// `stream` is initially invalid, this operation has no effect. If
837 /// `stream` becomes invalid during this operation, `variable` has an
838 /// undefined, but valid, state. See the `bslx` package-level
839 /// documentation for more information on BDEX streaming of
840 /// value-semantic types and containers.
841 template <class STREAM>
842 STREAM& bdexStreamIn(STREAM& stream,
843 bsls::Types::Uint64& variable,
844 int version = 0);
845
846 /// Assign to the specified `variable` the `float` value read from the
847 /// specified input `stream`, and return a reference to `stream`. The
848 /// optionally specified `version` is ignored. If `stream` is initially
849 /// invalid, this operation has no effect. If `stream` becomes invalid
850 /// during this operation, `variable` has an undefined, but valid,
851 /// state. See the `bslx` package-level documentation for more
852 /// information on BDEX streaming of value-semantic types and
853 /// containers.
854 template <class STREAM>
855 STREAM& bdexStreamIn(STREAM& stream, float& variable, int version = 0);
856
857 /// Assign to the specified `variable` the `double` value read from the
858 /// specified input `stream`, and return a reference to `stream`. The
859 /// optionally specified `version` is ignored. If `stream` is initially
860 /// invalid, this operation has no effect. If `stream` becomes invalid
861 /// during this operation, `variable` has an undefined, but valid,
862 /// state. See the `bslx` package-level documentation for more
863 /// information on BDEX streaming of value-semantic types and
864 /// containers.
865 template <class STREAM>
866 STREAM& bdexStreamIn(STREAM& stream, double& variable, int version = 0);
867
868 /// Assign to the specified `variable` the `bsl::string` value read from
869 /// the specified input `stream`, and return a reference to `stream`.
870 /// The optionally specified `version` is ignored. If `stream` is
871 /// initially invalid, this operation has no effect. If `stream`
872 /// becomes invalid during this operation, `variable` has an undefined,
873 /// but valid, state. See the `bslx` package-level documentation for
874 /// more information on BDEX streaming of value-semantic types and
875 /// containers.
876 template <class STREAM>
877 STREAM& bdexStreamIn(STREAM& stream,
878 bsl::string& variable,
879 int version = 0);
880
881 /// Assign to the specified `variable` the `bsl::vector<char, ALLOC>`
882 /// value read from the specified input `stream`, and return a reference
883 /// to `stream`. The specified `version` is ignored. If `stream` is
884 /// initially invalid, this operation has no effect. If `stream`
885 /// becomes invalid during this operation, `variable` has an undefined,
886 /// but valid, state. See the `bslx` package-level documentation for
887 /// more information on BDEX streaming of value-semantic types and
888 /// containers.
889 template <class STREAM, class ALLOC>
890 STREAM& bdexStreamIn(STREAM& stream,
891 bsl::vector<char, ALLOC>& variable,
892 int version);
893
894 /// Assign to the specified `variable` the
895 /// `bsl::vector<signed char, ALLOC>` value read from the specified
896 /// input `stream`, and return a reference to `stream`. The specified
897 /// `version` is ignored. If `stream` is initially invalid, this
898 /// operation has no effect. If `stream` becomes invalid during this
899 /// operation, `variable` has an undefined, but valid, state. See the
900 /// `bslx` package-level documentation for more information on BDEX
901 /// streaming of value-semantic types and containers.
902 template <class STREAM, class ALLOC>
903 STREAM& bdexStreamIn(STREAM& stream,
905 int version);
906
907 /// Assign to the specified `variable` the
908 /// `bsl::vector<unsigned char, ALLOC>` value read from the specified
909 /// input `stream`, and return a reference to `stream`. The specified
910 /// `version` is ignored. If `stream` is initially invalid, this
911 /// operation has no effect. If `stream` becomes invalid during this
912 /// operation, `variable` has an undefined, but valid, state. See the
913 /// `bslx` package-level documentation for more information on BDEX
914 /// streaming of value-semantic types and containers.
915 template <class STREAM, class ALLOC>
916 STREAM& bdexStreamIn(STREAM& stream,
918 int version);
919
920 /// Assign to the specified `variable` the `bsl::vector<short, ALLOC>`
921 /// value read from the specified input `stream`, and return a reference
922 /// to `stream`. The specified `version` is ignored. If `stream` is
923 /// initially invalid, this operation has no effect. If `stream`
924 /// becomes invalid during this operation, `variable` has an undefined,
925 /// but valid, state. See the `bslx` package-level documentation for
926 /// more information on BDEX streaming of value-semantic types and
927 /// containers.
928 template <class STREAM, class ALLOC>
929 STREAM& bdexStreamIn(STREAM& stream,
931 int version);
932
933 /// Assign to the specified `variable` the
934 /// `bsl::vector<unsigned short, ALLOC>` value read from the specified
935 /// input `stream`, and return a reference to `stream`. The specified
936 /// `version` is ignored. If `stream` is initially invalid, this
937 /// operation has no effect. If `stream` becomes invalid during this
938 /// operation, `variable` has an undefined, but valid, state. See the
939 /// `bslx` package-level documentation for more information on BDEX
940 /// streaming of value-semantic types and containers.
941 template <class STREAM, class ALLOC>
942 STREAM& bdexStreamIn(STREAM& stream,
944 int version);
945
946 /// Assign to the specified `variable` the `bsl::vector<int, ALLOC>`
947 /// value read from the specified input `stream`, and return a reference
948 /// to `stream`. The specified `version` is ignored. If `stream` is
949 /// initially invalid, this operation has no effect. If `stream`
950 /// becomes invalid during this operation, `variable` has an undefined,
951 /// but valid, state. See the `bslx` package-level documentation for
952 /// more information on BDEX streaming of value-semantic types and
953 /// containers.
954 template <class STREAM, class ALLOC>
955 STREAM& bdexStreamIn(STREAM& stream,
956 bsl::vector<int, ALLOC>& variable,
957 int version);
958
959 /// Assign to the specified `variable` the
960 /// `bsl::vector<unsigned int, ALLOC>` value read from the specified
961 /// input `stream`, and return a reference to `stream`. The specified
962 /// `version` is ignored. If `stream` is initially invalid, this
963 /// operation has no effect. If `stream` becomes invalid during this
964 /// operation, `variable` has an undefined, but valid, state. See the
965 /// `bslx` package-level documentation for more information on BDEX
966 /// streaming of value-semantic types and containers.
967 template <class STREAM, class ALLOC>
968 STREAM& bdexStreamIn(STREAM& stream,
970 int version);
971
972 /// Assign to the specified `variable` the
973 /// `bsl::vector<bsls::Types::Int64, ALLOC>` value read from the
974 /// specified input `stream`, and return a reference to `stream`. The
975 /// specified `version` is ignored. If `stream` is initially invalid,
976 /// this operation has no effect. If `stream` becomes invalid during
977 /// this operation, `variable` has an undefined, but valid, state. See
978 /// the `bslx` package-level documentation for more information on BDEX
979 /// streaming of value-semantic types and containers.
980 template <class STREAM, class ALLOC>
981 STREAM& bdexStreamIn(STREAM& stream,
983 int version);
984
985 /// Assign to the specified `variable` the
986 /// `bsl::vector<bsls::Types::Uint64, ALLOC>` value read from the
987 /// specified input `stream`, and return a reference to `stream`. The
988 /// specified `version` is ignored. If `stream` is initially invalid,
989 /// this operation has no effect. If `stream` becomes invalid during
990 /// this operation, `variable` has an undefined, but valid, state. See
991 /// the `bslx` package-level documentation for more information on BDEX
992 /// streaming of value-semantic types and containers.
993 template <class STREAM, class ALLOC>
994 STREAM& bdexStreamIn(STREAM& stream,
996 int version);
997
998 /// Assign to the specified `variable` the `bsl::vector<float, ALLOC>`
999 /// value read from the specified input `stream`, and return a reference
1000 /// to `stream`. The specified `version` is ignored. If `stream` is
1001 /// initially invalid, this operation has no effect. If `stream`
1002 /// becomes invalid during this operation, `variable` has an undefined,
1003 /// but valid, state. See the `bslx` package-level documentation for
1004 /// more information on BDEX streaming of value-semantic types and
1005 /// containers.
1006 template <class STREAM, class ALLOC>
1007 STREAM& bdexStreamIn(STREAM& stream,
1008 bsl::vector<float, ALLOC>& variable,
1009 int version);
1010
1011 /// Assign to the specified `variable` the `bsl::vector<double, ALLOC>`
1012 /// value read from the specified input `stream`, and return a reference
1013 /// to `stream`. The specified `version` is ignored. If `stream` is
1014 /// initially invalid, this operation has no effect. If `stream`
1015 /// becomes invalid during this operation, `variable` has an undefined,
1016 /// but valid, state. See the `bslx` package-level documentation for
1017 /// more information on BDEX streaming of value-semantic types and
1018 /// containers.
1019 template <class STREAM, class ALLOC>
1020 STREAM& bdexStreamIn(STREAM& stream,
1022 int version);
1023
1024 /// Assign to the specified `variable` the
1025 /// `bsl::vector<VALUE_TYPE, ALLOC>` value read from the specified input
1026 /// `stream`, and return a reference to `stream`. If `stream` is
1027 /// initially invalid, this operation has no effect. First read the
1028 /// version information from the `stream` and if this version is not
1029 /// supported by `VALUE_TYPE` and the vector is not empty, `stream` is
1030 /// invalidated, but otherwise unmodified. If `stream` becomes invalid
1031 /// during this operation, `variable` has an undefined, but valid,
1032 /// state. See the `bslx` package-level documentation for more
1033 /// information on BDEX streaming of value-semantic types and
1034 /// containers.
1035 template <class STREAM, class VALUE_TYPE, class ALLOC>
1036 STREAM& bdexStreamIn(STREAM& stream,
1038
1039 /// Assign to the specified `variable` the
1040 /// `bsl::vector<VALUE_TYPE, ALLOC>` value read from the specified input
1041 /// `stream` using the specified `version` format, and return a
1042 /// reference to `stream`. If `stream` is initially invalid, this
1043 /// operation has no effect. If `version` is not supported by
1044 /// `VALUE_TYPE` and the vector is not empty, `stream` is invalidated,
1045 /// but otherwise unmodified. If `stream` becomes invalid during this
1046 /// operation, `variable` has an undefined, but valid, state. See the
1047 /// `bslx` package-level documentation for more information on BDEX
1048 /// streaming of value-semantic types and containers.
1049 template <class STREAM, class VALUE_TYPE, class ALLOC>
1050 STREAM& bdexStreamIn(STREAM& stream,
1052 int version);
1053
1054} // close namespace InStreamFunctions
1055
1056// ============================================================================
1057// INLINE DEFINITIONS
1058// ============================================================================
1059
1060 // --------------------------------------
1061 // class InStreamFunctions_AccessorHelper
1062 // --------------------------------------
1063
1064template <class STREAM>
1065inline
1067 STREAM& stream,
1068 bool *result,
1069 int length)
1070{
1071 return stream.getArrayInt8(reinterpret_cast<char*>(result), length);
1072}
1073
1074template <class STREAM>
1075inline
1077 STREAM& stream,
1078 char *result,
1079 int length)
1080{
1081 return stream.getArrayInt8(result, length);
1082}
1083
1084template <class STREAM>
1085inline
1087 STREAM& stream,
1088 signed char *result,
1089 int length)
1090{
1091 return stream.getArrayInt8(result, length);
1092}
1093
1094template <class STREAM>
1095inline
1097 STREAM& stream,
1098 unsigned char *result,
1099 int length)
1100{
1101 return stream.getArrayUint8(result, length);
1102}
1103
1104template <class STREAM>
1105inline
1107 STREAM& stream,
1108 short *result,
1109 int length)
1110{
1111 return stream.getArrayInt16(result, length);
1112}
1113
1114template <class STREAM>
1115inline
1117 STREAM& stream,
1118 unsigned short *result,
1119 int length)
1120{
1121 return stream.getArrayUint16(result, length);
1122}
1123
1124template <class STREAM>
1125inline
1127 STREAM& stream,
1128 int *result,
1129 int length)
1130{
1131 return stream.getArrayInt32(result, length);
1132}
1133
1134template <class STREAM>
1135inline
1137 STREAM& stream,
1138 unsigned int *result,
1139 int length)
1140{
1141 return stream.getArrayUint32(result, length);
1142}
1143
1144template <class STREAM>
1145inline
1147 STREAM& stream,
1148 bsls::Types::Int64 *result,
1149 int length)
1150{
1151 return stream.getArrayInt64(result, length);
1152}
1153
1154template <class STREAM>
1155inline
1157 STREAM& stream,
1158 bsls::Types::Uint64 *result,
1159 int length)
1160{
1161 return stream.getArrayUint64(result, length);
1162}
1163
1164template <class STREAM>
1165inline
1167 STREAM& stream,
1168 float *result,
1169 int length)
1170{
1171 return stream.getArrayFloat32(result, length);
1172}
1173
1174template <class STREAM>
1175inline
1177 STREAM& stream,
1178 double *result,
1179 int length)
1180{
1181 return stream.getArrayFloat64(result, length);
1182}
1183
1184template <class STREAM>
1185template <class VALUE_TYPE, class ALLOC>
1187 STREAM& stream,
1189{
1190 int length = 0;
1191 stream.getLength(length);
1192
1193 if (!stream) {
1194 return stream; // RETURN
1195 }
1196
1197 // 'length' could be corrupt or invalid, so we limit the initial
1198 // 'resize' to something that can accommodate the preponderance of
1199 // vectors that will arise in practice. The remaining portion of a
1200 // vector longer than 16M is read in via a second pass.
1201 enum {
1202 k_INITIAL_ALLOCATION_COUNT = 16 * 1024 * 1024 / sizeof(VALUE_TYPE)
1203 };
1204
1205 const int initialLength = length < k_INITIAL_ALLOCATION_COUNT
1206 ? length
1207 : k_INITIAL_ALLOCATION_COUNT;
1208
1209 variable.resize(initialLength);
1210
1211 if (0 == length) {
1212 return stream; // RETURN
1213 }
1214
1215 STREAM& result = getArray(stream, &variable[0], initialLength);
1216
1217 if (!!stream && length > initialLength) {
1218 variable.resize(length);
1219 return getArray(stream,
1220 &variable[initialLength],
1221 length - initialLength); // RETURN
1222 }
1223
1224 return result;
1225}
1226
1227
1228 // ---------------------------
1229 // namespace InStreamFunctions
1230 // ---------------------------
1231
1232template <class STREAM, class VALUE_TYPE>
1233inline
1235 VALUE_TYPE& variable,
1236 int /* version */,
1237 const IsEnumType&)
1238{
1239 int enumVariable = 0;
1240 stream.getInt32(enumVariable);
1241
1242 if (stream) {
1243 variable = static_cast<VALUE_TYPE>(enumVariable);
1244 }
1245 return stream;
1246}
1247
1248template <class STREAM, class VALUE_TYPE>
1249inline
1251 VALUE_TYPE& variable,
1252 int version,
1253 const IsNotEnumType&)
1254{
1255 // A compilation error indicating the next line of code implies the class
1256 // of 'VALUE_TYPE' does not support the 'bdexStreamIn' method.
1257
1258 return variable.bdexStreamIn(stream, version);
1259}
1260
1261template <class STREAM, class VALUE_TYPE>
1262inline
1263STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream, VALUE_TYPE& variable)
1264{
1266
1267 // Determine if the 'VALUE_TYPE' requires a version to be externalized
1268 // using an arbitrary value for 'versionSelector'.
1269
1270 int version = maxSupportedBdexVersion(&variable, 0);
1271 if (VersionFunctions::k_NO_VERSION != version) {
1272 stream.getVersion(version);
1273
1274 if (!stream) {
1275 return stream; // RETURN
1276 }
1277 }
1278
1279 return bdexStreamIn(stream, variable, version);
1280}
1281
1282template <class STREAM, class VALUE_TYPE>
1283inline
1284STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1285 VALUE_TYPE& variable,
1286 int version)
1287{
1289 IsEnumType,
1290 IsNotEnumType>::type dummyType;
1291 return bdexStreamInImp(stream, variable, version, dummyType());
1292}
1293
1294template <class STREAM>
1295inline
1296STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1297 bool& variable,
1298 int /* version */)
1299{
1300 char temp = 0;
1301
1302 stream.getInt8(temp);
1303 variable = static_cast<bool>(temp);
1304
1305 return stream;
1306}
1307
1308template <class STREAM>
1309inline
1310STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1311 char& variable,
1312 int /* version */)
1313{
1314 return stream.getInt8(variable);
1315}
1316
1317template <class STREAM>
1318inline
1319STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1320 signed char& variable,
1321 int /* version */)
1322{
1323 return stream.getInt8(variable);
1324}
1325
1326template <class STREAM>
1327inline
1328STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1329 unsigned char& variable,
1330 int /* version */)
1331{
1332 return stream.getUint8(variable);
1333}
1334
1335template <class STREAM>
1336inline
1337STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1338 short& variable,
1339 int /* version */)
1340{
1341 return stream.getInt16(variable);
1342}
1343
1344template <class STREAM>
1345inline
1346STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1347 unsigned short& variable,
1348 int /* version */)
1349{
1350 return stream.getUint16(variable);
1351}
1352
1353template <class STREAM>
1354inline
1355STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1356 int& variable,
1357 int /* version */)
1358{
1359 return stream.getInt32(variable);
1360}
1361
1362template <class STREAM>
1363inline
1364STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1365 unsigned int& variable,
1366 int /* version */)
1367{
1368 return stream.getUint32(variable);
1369}
1370
1371template <class STREAM>
1372inline
1373STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1374 long& variable,
1375 int /* version */)
1376{
1377 int temp = 0; // 'long' and 'int' may not be the same size.
1378 stream.getInt32(temp);
1379 variable = temp;
1380 return stream;
1381}
1382
1383template <class STREAM>
1384inline
1385STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1386 unsigned long& variable,
1387 int /* version */)
1388{
1389 unsigned int temp = 0; // 'unsigned long' and 'unsigned int' may not be
1390 // the same size.
1391 stream.getUint32(temp);
1392 variable = temp;
1393 return stream;
1394}
1395
1396template <class STREAM>
1397inline
1398STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1399 bsls::Types::Int64& variable,
1400 int /* version */)
1401{
1402 return stream.getInt64(variable);
1403}
1404
1405template <class STREAM>
1406inline
1407STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1408 bsls::Types::Uint64& variable,
1409 int /* version */)
1410{
1411 return stream.getUint64(variable);
1412}
1413
1414template <class STREAM>
1415inline
1416STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1417 float& variable,
1418 int /* version */)
1419{
1420 return stream.getFloat32(variable);
1421}
1422
1423template <class STREAM>
1424inline
1425STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1426 double& variable,
1427 int /* version */)
1428{
1429 return stream.getFloat64(variable);
1430}
1431
1432template <class STREAM>
1433inline
1434STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1435 bsl::string& variable,
1436 int /* version */)
1437{
1438 return stream.getString(variable);
1439}
1440
1441template <class STREAM, class ALLOC>
1442inline
1443STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1444 bsl::vector<char, ALLOC>& variable,
1445 int /* version */)
1446{
1448 variable);
1449}
1450
1451template <class STREAM, class ALLOC>
1452inline
1454 STREAM& stream,
1456 int /* version */)
1457{
1459 variable);
1460}
1461
1462template <class STREAM, class ALLOC>
1463inline
1465 STREAM& stream,
1467 int /* version */)
1468{
1470 variable);
1471}
1472
1473template <class STREAM, class ALLOC>
1474inline
1475STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1476 bsl::vector<short, ALLOC>& variable,
1477 int /* version */)
1478{
1480 variable);
1481}
1482
1483template <class STREAM, class ALLOC>
1484inline
1486 STREAM& stream,
1488 int /* version */)
1489{
1491 variable);
1492}
1493
1494template <class STREAM, class ALLOC>
1495inline
1496STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1497 bsl::vector<int, ALLOC>& variable,
1498 int /* version */)
1499{
1501 variable);
1502}
1503
1504template <class STREAM, class ALLOC>
1505inline
1507 STREAM& stream,
1509 int /* version */)
1510{
1512 variable);
1513}
1514
1515template <class STREAM, class ALLOC>
1516inline
1518 STREAM& stream,
1520 int /* version */)
1521{
1523 variable);
1524}
1525
1526template <class STREAM, class ALLOC>
1527inline
1529 STREAM& stream,
1531 int /* version */)
1532{
1534 variable);
1535}
1536
1537template <class STREAM, class ALLOC>
1538inline
1539STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1540 bsl::vector<float, ALLOC>& variable,
1541 int /* version */)
1542{
1544 variable);
1545}
1546
1547template <class STREAM, class ALLOC>
1548inline
1549STREAM& InStreamFunctions::bdexStreamIn(STREAM& stream,
1551 int /* version */)
1552{
1554 variable);
1555}
1556
1557template <class STREAM, class VALUE_TYPE, class ALLOC>
1558inline
1560 STREAM& stream,
1562{
1563 int version = 0;
1564 stream.getVersion(version);
1565
1566 if (!stream) {
1567 return stream; // RETURN
1568 }
1569
1570 return bdexStreamIn(stream, variable, version);
1571}
1572
1573template <class STREAM, class VALUE_TYPE, class ALLOC>
1575 STREAM& stream,
1577 int version)
1578{
1579 typedef typename bsl::vector<VALUE_TYPE, ALLOC>::iterator Iterator;
1580
1581 int length = 0;
1582 stream.getLength(length);
1583
1584 if (!stream) {
1585 return stream; // RETURN
1586 }
1587
1588 // 'length' could be corrupt or invalid, so we limit the initial 'resize'
1589 // to something that can accommodate the preponderance of vectors that will
1590 // arise in practice. The remaining portion of a vector longer than 16M
1591 // bytes is read in via a second pass.
1592
1593 enum {
1594 k_INITIAL_ALLOCATION_COUNT =
1595 16 * 1024 * 1024 / sizeof(VALUE_TYPE)
1596 };
1597
1598 const int initialLength = length < k_INITIAL_ALLOCATION_COUNT
1599 ? length
1600 : k_INITIAL_ALLOCATION_COUNT;
1601
1602 variable.resize(initialLength);
1603
1604 if (0 == length) {
1605 return stream; // RETURN
1606 }
1607
1608 for (Iterator it = variable.begin(); it != variable.end(); ++it) {
1609 bdexStreamIn(stream, *it, version);
1610
1611
1614 return stream; // RETURN
1615 }
1616 }
1617
1618 if (length > initialLength) {
1619 variable.resize(length);
1620
1621 for (Iterator it = variable.begin() + initialLength;
1622 it != variable.end();
1623 ++it) {
1624 bdexStreamIn(stream, *it, version);
1625
1628 return stream; // RETURN
1629 }
1630 }
1631 }
1632
1633 return stream;
1634}
1635
1636} // close package namespace
1637
1638
1639#endif
1640
1641// ----------------------------------------------------------------------------
1642// Copyright 2014 Bloomberg Finance L.P.
1643//
1644// Licensed under the Apache License, Version 2.0 (the "License");
1645// you may not use this file except in compliance with the License.
1646// You may obtain a copy of the License at
1647//
1648// http://www.apache.org/licenses/LICENSE-2.0
1649//
1650// Unless required by applicable law or agreed to in writing, software
1651// distributed under the License is distributed on an "AS IS" BASIS,
1652// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1653// See the License for the specific language governing permissions and
1654// limitations under the License.
1655// ----------------------------- END-OF-FILE ----------------------------------
1656
1657/** @} */
1658/** @} */
1659/** @} */
Definition bslstl_string.h:1252
iterator begin() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2866
iterator end() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2874
Definition bslstl_vector.h:1120
VALUE_TYPE * iterator
Definition bslstl_vector.h:1152
void resize(size_type newSize)
Definition bslstl_vector.h:4189
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_PERFORMANCEHINT_UNLIKELY_HINT
Definition bsls_performancehint.h:484
#define BSLS_PERFORMANCEHINT_PREDICT_UNLIKELY(expr)
Definition bsls_performancehint.h:452
STREAM & bdexStreamIn(STREAM &stream, VALUE_TYPE &variable)
Definition bslx_instreamfunctions.h:1263
STREAM & bdexStreamInImp(STREAM &stream, VALUE_TYPE &variable, int version, const IsEnumType &)
Definition bslx_instreamfunctions.h:1234
int maxSupportedBdexVersion(const TYPE *, int versionSelector)
Definition bslx_versionfunctions.h:531
@ k_NO_VERSION
Definition bslx_versionfunctions.h:344
Definition bslx_byteinstream.h:377
Definition bslmf_conditional.h:123
unsigned long long Uint64
Definition bsls_types.h:139
long long Int64
Definition bsls_types.h:134
Definition bslx_instreamfunctions.h:566
static STREAM & getArray(STREAM &stream, bool *result, int length)
Definition bslx_instreamfunctions.h:1066
Definition bslx_instreamfunctions.h:613
Definition bslx_instreamfunctions.h:626