Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdlbb_simpleblobbufferfactory
[Package bdlbb]

Provide a simple implementation of bdlbb::BlobBufferFactory. More...

Namespaces

namespace  bdlbb

Detailed Description

Outline
Purpose:
Provide a simple implementation of bdlbb::BlobBufferFactory.
Classes:
bdlbb::SimpleBlobBufferFactory a simple bdlbb::BlobBufferFactory
Description:
This component provides a mechanism, bdlbb::SimpleBlobBufferFactory, that implements the bdlbb::BlobBufferFactory protocol and creates bdlbb::BlobBuffer objects of a fixed sized specified at the time the factory is constructed. The blob buffers created by this factory refer the a (shared) buffer allocated by the allocator supplied at construction, or the default allocator if no allocator is explicitly supplied.
Usage:
In this section we show intended usage of this component.
Example 1: Simple Blob Buffer Factory:
Suppose we want to make a blob that can be grown via calls to setLength, meaning that it must have a factory, and suppose you want all the memory for the blob buffers created for the factory to be allocated directly from a certain test allocator for test purposes. We use a SimpleBlobBufferFactory.
First, we create our allocator:
  bslma::TestAllocator testAllocator;
Then, we create our factor using that allocator:
  bdlbb::SimpleBlobBufferFactory factory(1024, &testAllocator);
  assert(factory.bufferSize() == 1024);
Next, we create our blob using that factory:
  bdlbb::Blob blob(&factory);
Next, we set the length big enough to require 20 blob buffers:
  blob.setLength(1024 * 20);
Then, we verify that the memory came from testAllocator. Note that since the blob buffers contain shared pointers, additional memory other than the writable areas of the blob buffers is allocated:
  assert(1024 * 20 < testAllocator.numBytesInUse());
Now, we examine the state of the blob:
  assert(20 == blob.numDataBuffers());
  assert(20 == blob.numBuffers());
  assert(1024 * 20 == blob.length());
Finally, we examine the blob buffers:
  for (int ii = 0; ii < blob.numDataBuffers(); ++ii) {
      assert(1024 == blob.buffer(ii).size());
  }