text
stringlengths
0
2.2M
// DATA
Link *d_head_p; // head of list
Link *d_tail_p; // tail of list
int d_length; // number of objects
bslma::Allocator *d_allocator_p; // allocator (held, not owned)
public:
// CREATORS
explicit my_List(bslma::Allocator *basicAllocator = 0);
// Create a 'my_List' object having an initial length of 0.
// Optionally specify a 'basicAllocator' used to supply memory. If
// 'basicAllocator' is 0, the currently installed default allocator
// is used.
// ...
~my_List();
// Destroy this 'my_List' object and all elements currently stored.
// MANIPULATORS
// ...
void append(const TYPE& object);
// Append (a copy of) the specified 'object' of parameterized
// 'TYPE' to (the end of) this list.
// ...
};
//..
// Note that the rest of the 'my_List' interface (above) and implementation
// (below) are omitted as the portion shown is sufficient to demonstrate the
// use of 'bslma::RawDeleterProctor'.
//..
// CREATORS
template <class TYPE>
inline
my_List<TYPE>::my_List(bslma::Allocator *basicAllocator)
: d_head_p(0)
, d_tail_p(0)
, d_length(0)
, d_allocator_p(bslma::Default::allocator(basicAllocator))
{
}
template <class TYPE>
my_List<TYPE>::~my_List()
{
while (d_head_p) {
Link *tmp = d_head_p;
d_head_p = d_head_p->d_next_p;
d_allocator_p->deleteObject(tmp->d_object_p);
d_allocator_p->deallocate(tmp);
}
}
// MANIPULATORS
template <class TYPE>
void my_List<TYPE>::append(const TYPE& object)
{
// !!!Warning: Modified since 'my_Class' does not take an allocator.
TYPE *tmp = (TYPE *)new(*d_allocator_p) TYPE(object);
// possibly throw
//************************************************************
// Note the use of the raw deleter proctor on 'tmp' (below). *
//************************************************************
bslma::RawDeleterProctor<TYPE, bslma::Allocator> proctor(tmp,
d_allocator_p);
if (!d_head_p) {
d_head_p = new(*d_allocator_p) Link; // possibly throw
d_tail_p = d_head_p;
}
else {
d_tail_p->d_next_p = new(*d_allocator_p) Link; // possibly throw
d_tail_p = d_tail_p->d_next_p;
}
d_tail_p->d_object_p = tmp;
d_tail_p->d_next_p = 0;
//*********************************************************
// Note that the raw deleter proctor is released (below). *
//*********************************************************
proctor.release();
}
//..
// The 'append' method defined above potentially throws in three places. If
// the memory allocator held in 'd_allocator_p' were to throw while attempting
// to create the object of parameterized 'TYPE', no memory would be leaked.
// But without subsequent use of the 'bslma::RawDeleterProctor', if the
// allocator subsequently throws while creating the link, all memory (and any
// other resources) acquired as a result of copying the (not-yet-managed)
// object would be leaked. Using the 'bslma::RawDeleterProctor' prevents the
// leaks by deleting the proctored object automatically should the proctor go
// out of scope before the 'release' method of the proctor is called (such as
// when the function exits prematurely due to an exception).
//
// Note that the 'append' method assumes the copy constructor of 'TYPE' takes