text stringlengths 0 2.2M |
|---|
}
|
bslma::Allocator *allocator() const
|
// Return the allocator used by this object to supply memory.
|
{
|
return d_allocator_p;
|
}
|
// etc.
|
};
|
//..
|
// Next, we implement the constructors that allocate memory and construct a
|
// 'TYPE' object in the allocated memory. We perform the allocation using the
|
// 'allocate' method of 'bslma::Allocator' and the construction using the
|
// 'construct' method of 'ConstructionUtil' that provides the correct semantics
|
// for passing the allocator to the constructed object when appropriate:
|
//..
|
template <class TYPE>
|
MyContainer<TYPE>::MyContainer(bslma::Allocator *basicAllocator)
|
: d_allocator_p(bslma::Default::allocator(basicAllocator))
|
{
|
d_value_p = static_cast<TYPE *>(d_allocator_p->allocate(sizeof(TYPE)));
|
MyContainerProctor<TYPE> proctor(d_allocator_p, d_value_p);
|
// Call 'construct' with no constructor arguments (aside from the
|
// allocator).
|
bslma::ConstructionUtil::construct(d_value_p, d_allocator_p);
|
proctor.release();
|
}
|
template <class TYPE>
|
template <class OTHER>
|
MyContainer<TYPE>::MyContainer(
|
BSLS_COMPILERFEATURES_FORWARD_REF(OTHER) value,
|
bslma::Allocator *basicAllocator)
|
: d_allocator_p(bslma::Default::allocator(basicAllocator))
|
{
|
d_value_p = static_cast<TYPE *>(d_allocator_p->allocate(sizeof(TYPE)));
|
MyContainerProctor<TYPE> proctor(d_allocator_p, d_value_p);
|
// Call 'construct' by forwarding 'value'.
|
bslma::ConstructionUtil::construct(
|
d_value_p,
|
d_allocator_p,
|
BSLS_COMPILERFEATURES_FORWARD(OTHER, value));
|
proctor.release();
|
}
|
//..
|
// Then, we define the copy constructor for 'MyContainer'. Note that we don't
|
// propagate the allocator from the 'original' container, but use
|
// 'basicAllocator' instead:
|
//..
|
template <class TYPE>
|
MyContainer<TYPE>::MyContainer(const MyContainer& original,
|
bslma::Allocator *basicAllocator)
|
: d_allocator_p(bslma::Default::allocator(basicAllocator))
|
{
|
d_value_p = static_cast<TYPE *>(d_allocator_p->allocate(sizeof(TYPE)));
|
MyContainerProctor<TYPE> proctor(d_allocator_p, d_value_p);
|
// Call 'construct' so as to copy-construct the element contained by
|
// 'original'.
|
bslma::ConstructionUtil::construct(d_value_p,
|
d_allocator_p,
|
*original.d_value_p);
|
proctor.release();
|
}
|
//..
|
// Now, the destructor destroys the object and deallocates the memory used to
|
// hold the element using the allocator:
|
//..
|
template <class TYPE>
|
MyContainer<TYPE>::~MyContainer()
|
{
|
d_value_p->~TYPE();
|
d_allocator_p->deallocate(d_value_p);
|
}
|
//..
|
// Next, the assignment operator needs to assign the value without modifying
|
// the allocator.
|
//..
|
template <class TYPE>
|
MyContainer<TYPE>& MyContainer<TYPE>::operator=(const TYPE& rhs)
|
{
|
*d_value_p = rhs;
|
return *this;
|
}
|
template <class TYPE>
|
MyContainer<TYPE>& MyContainer<TYPE>::operator=(const MyContainer& rhs)
|
{
|
*d_value_p = *rhs.d_value_p;
|
return *this;
|
}
|
//..
|
// Finally, we perform a simple test of 'MyContainer', instantiating it with
|
// element type 'int':
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.