Provide a basic output stream buffer using a client buffer.
More...
Namespaces |
namespace | bdlsb |
Detailed Description
- Outline
-
-
- Purpose:
- Provide a basic output stream buffer using a client buffer.
-
- Classes:
-
- See also:
- Component 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);
Then, we create an instance of bslx::GenericOutStream
using streamBuf
, with an arbitrary value for its versionSelector
, and externalize some values: 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));