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

Detailed Description

Outline

Purpose

Provide a basic output stream buffer using a client buffer.

Classes

See also
bdlsb_fixedmemoutstreambuf

Description

This component implements the output portion of the bsl::basic_streambuf protocol using a client-supplied memory buffer. Method names 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. The only difference between this component and bdlsb_fixedmemoutstreambuf is that the class bdlsb::FixedMemOutput does not derive from a bsl::streambuf and does not support locales. This is advantageous for performance reasons, as the overhead of the initialization and virtual function calls of a bsl::streambuf can be undesirable. The bdlsb::FixedMemOutput is designed to be used by generic template code that must be instantiated on a type that matches the interface of bsl::streambuf, but does not require an actual bsl::streambuf, in particular bslx_genericoutstream .

Usage

This section illustrates intended use of this component.

Example 1: Basic Use of bdlsb::FixedMemOutput

This example demonstrates instantiating a template, bslx::GenericOutStream', on a bdlsb::FixedMemOutput object and using the bslx::GenericOutStream object to stream out some data.

First, we create an object of our stream buffer:

enum { k_STREAMBUF_CAPACITY = 30 };
char buffer[k_STREAMBUF_CAPACITY];
bdlsb::FixedMemOutput streamBuf(buffer, k_STREAMBUF_CAPACITY);
Definition bdlsb_fixedmemoutput.h:137

Then, we create an instance of bslx::GenericOutStream using streamBuf, with an arbitrary value for its versionSelector, and externalize some values:

20150707);
outStream.putInt32(1);
outStream.putInt32(2);
outStream.putInt8('c');
outStream.putString(bsl::string("hello"));
Definition bslstl_string.h:1281
Definition bslx_genericoutstream.h:363

Finally, we compare the contents of the buffer to the expected value:

assert(15 == streamBuf.length());
assert( 0 == bsl::memcmp(streamBuf.data(),
"\x00\x00\x00\x01\x00\x00\x00\x02""c\x05""hello",
15));