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

Detailed Description

Outline

Purpose

Provide routines that destroy objects efficiently.

Classes

See also
bslma_constructionutil

Description

This component provides utilities to destroy scalars with a uniform interface, but select a different implementation according to the traits possessed by the underlying type.

The trait under consideration by this component is:

Trait Note
------------------------------- -------------------------------------
bslmf::IsBitwiseCopyable Expressed in English as "TYPE has the
bit-wise copyable trait", or "TYPE is
bit-wise copyable", this trait also
implies that destructor calls can be
elided with no effect on observable
behavior.
Definition bslmf_isbitwisecopyable.h:298

Usage

In this section we show intended use of this component. Note that this component is for use by the bslstl package. Other clients should use the STL algorithms (in header <algorithm> and <memory>).

Example 1: Destroy int and an Integer Wrapper

In this example, we will use bslma::DestructionUtil to destroy both a scalar integer and a MyInteger type object. Calling the destroy method on a scalar integer is a no-op while calling the destroy method on an object of MyInteger class invokes the destructor of the object.

First, we define a MyInteger class that represents an integer value:

/// This class represents an integer value.
class MyInteger {
// DATA
int d_intValue; // integer value
public:
// CREATORS
/// Create a `MyInteger` object having integer value `0`.
MyInteger();
/// Create a `MyInteger` object having the specified `value`.
explicit MyInteger(int value);
/// Destroy this object.
~MyInteger();
// ACCESSORS
int getValue() const;
};

Then, we create an object, myInteger, of type MyInteger:

MyInteger *myInteger = &buffer.object();
new (myInteger) MyInteger(1);
Definition bsls_objectbuffer.h:276
TYPE & object()
Definition bsls_objectbuffer.h:351

Notice that we use an ObjectBuffer to allow us to safely invoke the destructor explicitly.

Now, we define a primitive integer:

int scalarInteger = 2;

Finally, we use the uniform bslma::DestructionUtil::destroy method to destroy both myInteger and scalarInteger:

bslma::DestructionUtil::destroy(myInteger);
bslma::DestructionUtil::destroy(&scalarInteger);