BDE 4.14.0 Production release
|
Macros | |
#define | bslma_AutoDestructor bslma::AutoDestructor |
This alias is defined for backward compatibility. | |
Provide a range proctor to manage an array of objects.
This component provides a range proctor class template, bslma::AutoDestructor
, to manage an array of (otherwise-unmanaged) objects of parameterized TYPE
supplied at construction. Unless explicitly released, the contiguous managed objects are destroyed automatically when the range proctor goes out of scope by calling each (managed) object's destructor. Note that after a proctor object releases its managed objects, the same proctor can be reused to conditionally manage another contiguous sequence of objects by invoking the reset
method.
bslma::AutoDestructor
is normally used to achieve exception safety in an exception neutral way by automatically destroying (otherwise-unmanaged) orphaned objects for an "in-place" array should an exception occur. The following example illustrates the insertion operation for a generic array. Assume that the array initially contains the following five elements:
To insert an element "F" at index position 2, the existing elements at index positions 2, 3, and 4 (i.e., "C", "D", and "E") are first shifted right to create an empty spot at the specified insert destination (we assume here and below that the array has sufficient capacity). The elements have to be shifted one by one by invoking the copy constructor (immediately followed by destroying the original elements). However, should any of the copy construction operations throw, all allocated resources from every previous copy construction would be leaked. Using the bslma::AutoDestructor
prevents the leak by invoking the destructor of each of the previously copied elements 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):
As each of the elements at index positions beyond the insertion position is shifted up by one index position, the proctor (i.e., the proctor's length) is decremented, thereby extending by one the sequence of elements it manages below its origin (note that when the proctor's length is non- positive, the element at the origin is not managed). At the same time, the array's length is decremented to ensure that each array element is always being managed (during an allocation attempt) either by the proctor or the array itself, but not both:
When all elements are shifted, the bslma::AutoDestructor
will protect the entire range of shifted objects:
Next, a new copy of element "F" must be created. If, during creation, an allocation fails and an exception is thrown, the array (now of length 2) is in a valid state, while the proctor is responsible for destroying the orphaned elements at index positions 3, 4, and 5. If no exception is thrown, the proctor's release
method is called, releasing its control over the temporarily-managed contents:
Note that the rest of the my_Array
interface (above) and implementation (below) is omitted as the portion shown is sufficient to demonstrate the use of bslma::AutoDestructor
.
The elided implementation of the following insert
function (which shows code for the case above, i.e., there is sufficient capacity) is sufficient to illustrate the use of bslma::AutoDestructor
:
Note that the insert
method assumes the copy constructor of TYPE
takes an allocator as a second argument. In production code, a constructor proxy that checks the traits of TYPE
(to determine whether TYPE
indeed uses bslma::Allocator
) should be used (see bslalg_constructorproxy ).
#define bslma_AutoDestructor bslma::AutoDestructor |