BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlsb_fixedmeminput

Detailed Description

Outline

Purpose

Provide a basic input stream buffer using a client buffer.

Classes

See also
bdlsb_fixedmeminstreambuf

Description

This component provides a mechanism, bdlsb::FixedMemInput, that implements the input portion of the bsl::basic_streambuf protocol using a client-supplied memory buffer. Method names necessarily correspond to the protocol-specified method names. Clients supply the character buffer at stream buffer construction, and can later reinitialize the stream buffer with a different character buffer by calling the pubsetbuf method. This component provides none of the output-related functionality of bsl::basic_streambuf nor does it use locales in any way. The only difference between this component and bdlsb::FixedMemInStreamBuf is that the class bdlsb::FixedMemInput does not derive from a bsl::streambuf, and is generally more efficient (at initialization and due to the lack of virtual functions). It is especially designed for streaming a very small amount of information from a fixed-length buffer using a bslx_genericinstream .

Usage

This section illustrates intended use of this component.

Example 1: Basic Use of bdlsb::FixedMemInput

The bdlsb::FixedMemInput class is intended to be used as a template parameter to the bslx::GenericInStream class. Such specialization provides user with performance efficient way to unexternalize BDEX encoded data from an existing character buffer.

See the bslx_genericinstream component usage example for a more practical example of using bslx streams.

This example demonstrates instantiating a template, bslx::GenericInStream, on a bdlsb::FixedMemInput object and using the bslx::GenericInStream object to stream in some data.

First, create bslx::ByteOutStream outStream and externalize some user data to it. Note that this code only prepares the character buffer that is used to illustrate the purpose of the bdlsb::FixedMemInput class.

bslx::ByteOutStream outStream(20131127);
unsigned int MAGIC = 0x1812;
outStream.putUint32(MAGIC);
outStream.putInt32(83);
outStream.putString(bsl::string("test"));
assert(outStream.isValid());
Definition bslstl_string.h:1281
Definition bslx_byteoutstream.h:212

Next, create a bdlsb::FixedMemInput stream buffer initialized with the buffer from the bslx::ByteOutStream object outStream:

bdlsb::FixedMemInput streamBuffer(outStream.data(), outStream.length());
Definition bdlsb_fixedmeminput.h:164

Then, create the bslx::GenericInStream stream parameterized with bdlsb::FixedMemInput:

Definition bslx_genericinstream.h:590

Now, use resulting inStream to unexternalize user data:

unsigned int magic = 0;
int key;
inStream.getUint32(magic);
inStream.getInt32(key);
inStream.getString(value);
assert(inStream.isValid());

Finally, verify that the data from the supplied buffer was unexternalized correctly:

assert(MAGIC == magic);
assert(83 == key);
assert("test" == value);