BDE 4.14.0 Production release
|
Macros | |
#define | bslma_RawDeleterGuard bslma::RawDeleterGuard |
This alias is defined for backward compatibility. | |
Provide a guard to unconditionally manage an object.
This component provides a guard class template, bslma::RawDeleterGuard
, to unconditionally manage an (otherwise-unmanaged) object of parameterized TYPE
supplied at construction. The managed object is deleted automatically when the guard object goes out of scope by first calling the (managed) object's destructor, and then freeing the memory using the parameterized ALLOCATOR
(allocator or pool) also supplied at construction.
Note that this component should be used only if we are sure that the supplied pointer is not of a type that is a secondary base class – i.e., the (managed) object's address is (numerically) the same as when it was originally dispensed by ALLOCATOR
.
The parameterized ALLOCATOR
type of the bslma::RawDeleterGuard
class template must provide a (possibly virtual
) method:
to deallocate memory at the specified address
(originally supplied by the ALLOCATOR
object).
This example shows how one might use a bslma::RawDeleterGuard
to guard a dynamically-allocated object, deleting that object automatically when the guard goes out of scope.
Suppose we have a simple queue class that stores object values using an "out-of-place" representation (i.e., an array of dynamically-allocated object pointers):
Note that the popFront
method returns an object by value because (1) there may be no reasonable default object to pass in, (2) there may be no reasonable copy-assignment semantics, or (3) it is simply more syntactically convenient (e.g., if, say, the queued objects are themselves pointers):
Note that the pushBack
method should be implemented with a constructor proxy that determines whether TYPE
takes an allocator at construction (see bslalg_constructorproxy ). However, for the purpose of this example, the implementation is simplified by assuming TYPE
takes an allocator.
The pushBack
method defined above stores a copy of the provided object. The popFront
method returns the leading object by value, and the bslma::RawDeleterGuard
is used to automatically delete the copy the queue manages when the guard goes out of scope (i.e., when the function returns).
#define bslma_RawDeleterGuard bslma::RawDeleterGuard |