Provide a minimal standard compliant allocator.
More...
Namespaces |
namespace | bsltf |
Detailed Description
- Outline
-
-
- Purpose:
- Provide a minimal standard compliant allocator.
-
- Classes:
-
-
- Description:
- This component provides an allocator,
StdTestAllocator
, that defines the minimal interface to comply with section 20.1.5 ([lib.allocator.requirements]) of the C++03 standard. This type can be used to verify that constructs designed to support a standard-compliant allocator access the allocator only through the standard-defined interface.
StdTestAllocator
delegates its operations to a static bslma::Allocator
(delegate allocator) that can be configured by the utilities provided in the namespace StdTestAllocatorConfiguration
. StdTestAllocatorConfigurationGuard
provides a scoped guard to enable temporary replacement of the delegate allocator.
-
- Usage:
- This section illustrates intended use of this component.
-
- Example 1: Testing The Support for STL-Compliant Allocator:
- In this example we will verify that a type supports the use of a STL-compliant allocator.
- First we define a simple container type intended to be used with a C++03 standard compliant allocator:
template <class TYPE, class ALLOCATOR>
class MyContainer {
ALLOCATOR d_allocator;
TYPE *d_object_p;
public:
MyContainer(const TYPE& object);
~MyContainer();
TYPE& object();
const TYPE& object() const;
};
Then, we define the member functions of MyContainer
:
template <class TYPE, class ALLOCATOR>
MyContainer<TYPE, ALLOCATOR>::MyContainer(const TYPE& object)
{
d_object_p = d_allocator.allocate(1);
d_allocator.construct(d_object_p, object);
}
template <class TYPE, class ALLOCATOR>
MyContainer<TYPE, ALLOCATOR>::~MyContainer()
{
d_allocator.destroy(d_object_p);
d_allocator.deallocate(d_object_p);
}
template <class TYPE, class ALLOCATOR>
TYPE& MyContainer<TYPE, ALLOCATOR>::object()
{
return *d_object_p;
}
template <class TYPE, class ALLOCATOR>
const TYPE& MyContainer<TYPE, ALLOCATOR>::object() const
{
return *d_object_p;
}
Now, we use StdTestAllocator
to implement a simple test for MyContainer
to verify it correctly uses a parameterized allocator using only the C++03 standard methods: bslma_TestAllocator oa("object", veryVeryVeryVerbose);
StdTestAllocatorConfigurationGuard stag(&oa);
{
typedef MyContainer<int, StdTestAllocator<int> > Obj;
Obj mX(2); const Obj& X = mX;
assert(sizeof(int) == oa.numBytesInUse());
assert(X.object() == 2);
mX.object() = -10;
assert(X.object() == -10);
}
assert(0 == oa.numBytesInUse());