BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslma_deleterhelper

Typedefs

typedef bslma::DeleterHelper bslma_DeleterHelper
 This alias is defined for backward compatibility.
 

Detailed Description

Outline

Purpose

Provide namespace for functions used to delete objects.

Classes

See also
bslma_rawdeleterguard, bslmf_ispolymporphic

Description

This component provides non-primitive procedures used to delete objects of parameterized TYPE by first calling the destructor of the object, and then freeing the memory footprint of the object using a parameterized ALLOCATOR (allocator or pool) provided as a second argument. The "raw" method (deleteObjectRaw) should be used only if we are sure that the supplied object is not of a type that is a secondary base class – i.e., the object's address is (numerically) the same as when it was originally dispensed by ALLOCATOR. The non-"raw" deleteObject has no such restriction. Note that this component will fail to compile when instantiated for a class that gives a false-positive for the type trait bsl::is_polymorphic. See the bslmf_ispolymporphic component for more details.

Usage

The following my_RawDeleterGuard class defines a guard that unconditionally deletes a managed object upon destruction. Via the deleteObjectRaw method supplied by this component, the guard's destructor first destroys the managed object, then deallocates the footprint of the object. The declaration of my_RawDeleterGuard follows:

/// This class implements a guard that unconditionally deletes a managed
/// object upon destruction by first invoking the object's destructor,
/// and then invoking the `deallocate` method of an allocator (or pool)
/// of parameterized `ALLOCATOR` type supplied at construction.
template <class TYPE, class ALLOCATOR>
class my_RawDeleterGuard {
// DATA
TYPE *d_object_p; // managed object
ALLOCATOR *d_allocator_p; // allocator or pool (held, not owned)
// NOT IMPLEMENTED
my_RawDeleterGuard(const my_RawDeleterGuard&);
my_RawDeleterGuard& operator=(const my_RawDeleterGuard&);
public:
// CREATORS
/// Create a raw deleter guard that unconditionally manages the
/// specified `object`, and that uses the specified `allocator` to
/// delete `object` upon the destruction of this guard. The
/// behavior is undefined unless `object` and `allocator` are
/// non-zero, and `allocator` supplied the memory for `object`.
/// Note that `allocator` must remain valid throughout the lifetime
/// of this guard.
my_RawDeleterGuard(TYPE *object, ALLOCATOR *allocator);
/// Destroy this raw deleter guard and delete the object it manages
/// by first invoking the destructor of the (managed) object, and
/// then invoking the `deallocate` method of the allocator (or pool)
/// that was supplied with the object at construction.
~my_RawDeleterGuard();
};

The deleteObjectRaw method is used in the destructor as follows:

template <class TYPE, class ALLOCATOR>
inline
my_RawDeleterGuard<TYPE, ALLOCATOR>::~my_RawDeleterGuard()
{
bslma::DeleterHelper::deleteObjectRaw(d_object_p, d_allocator_p);
}
static void deleteObjectRaw(const TYPE *object, ALLOCATOR *allocator)
Definition bslma_deleterhelper.h:217

Note that we have denoted our guard to be a "raw" guard in keeping with this use of deleteObjectRaw (as opposed to deleteObject).

Typedef Documentation

◆ bslma_DeleterHelper