text
stringlengths
0
2.2M
//..
int usageExample1()
{
bslma::TestAllocator testAlloc;
MyContainer<int> C1(123, &testAlloc);
ASSERT(C1.allocator() == &testAlloc);
ASSERT(C1.front() == 123);
MyContainer<int> C2(C1);
ASSERT(C2.allocator() == bslma::Default::defaultAllocator());
ASSERT(C2.front() == 123);
return 0;
}
//..
//
///Example 2: 'bslma' Allocator Propagation
///- - - - - - - - - - - - - - - - - - - -
// This example demonstrates that 'MyContainer' does indeed propagate the
// allocator to its contained element.
//
// First, we create a representative element class, 'MyType', that allocates
// memory using the 'bslma' allocator protocol:
//..
#include <bslma_default.h>
class MyType {
// DATA
// ...
bslma::Allocator *d_allocator_p;
public:
// TRAITS
BSLMF_NESTED_TRAIT_DECLARATION(MyType, bslma::UsesBslmaAllocator);
// CREATORS
explicit MyType(bslma::Allocator *basicAllocator = 0)
// Create a 'MyType' object having the default value. Optionally
// specify a 'basicAllocator' used to supply memory. If
// 'basicAllocator' is 0, the currently installed default allocator
// is used.
: d_allocator_p(bslma::Default::allocator(basicAllocator))
{
// ...
}
MyType(const MyType& original, bslma::Allocator *basicAllocator = 0)
// Create a 'MyType' object having the same value as the specified
// 'original' object. Optionally specify a 'basicAllocator' used
// to supply memory. If 'basicAllocator' is 0, the currently
// installed default allocator is used.
: d_allocator_p(bslma::Default::allocator(basicAllocator))
{
(void) original;
// ...
}
// ...
// ACCESSORS
bslma::Allocator *allocator() const
// Return the allocator used by this object to supply memory.
{
return d_allocator_p;
}
// ...
};
//..
// Finally, we instantiate 'MyContainer' using 'MyType' and verify that, when
// we provide the address of an allocator to the constructor of the container,
// the same address is passed to the constructor of the contained element. We
// also verify that, when the container is copy-constructed without supplying
// an allocator, the copy uses the default allocator, not the allocator from
// the original object. Moreover, we verify that the element stored in the
// copy also uses the default allocator:
//..
int usageExample2()
{
bslma::TestAllocator testAlloc;
MyContainer<MyType> C1(&testAlloc);
ASSERT(C1.allocator() == &testAlloc);
ASSERT(C1.front().allocator() == &testAlloc);
MyContainer<MyType> C2(C1);
ASSERT(C2.allocator() != C1.allocator());
ASSERT(C2.allocator() == bslma::Default::defaultAllocator());
ASSERT(C2.front().allocator() != &testAlloc);
ASSERT(C2.front().allocator() == bslma::Default::defaultAllocator());
return 0;
}
//..
//
#if defined(BSLS_COMPILERFEATURES_GUARANTEED_COPY_ELISION)
///Example 3: Constructing into Non-heap Memory
///- - - - - - - - - - - - - - - - - - - - - -
// This example demonstrates using 'bslma::ConstructionUtil::make' to
// implement a simple wrapper class that contains a single item that might or