Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdlma_defaultdeleter
[Package bdlma]

Provide a concrete default deleter w/optionally-supplied allocator. More...

Namespaces

namespace  bdlma

Detailed Description

Outline
Purpose:
Provide a concrete default deleter w/optionally-supplied allocator.
Classes:
bdlma::DefaultDeleter concrete default bdema-based deleter
See also:
Component bslma_allocator, Component bdlma_deleter
Description:
This component provides a default concrete implementation of the bdlma::Deleter protocol:
                      ( bdlma::DefaultDeleter )
                                 |       ctor
                                 v
                         ( bdlma::Deleter )
                                         dtor
                                         deleteObject
Upon construction, a bdlma::DefaultDeleter is optionally supplied with a bdema-style allocator. If an allocator is not specified, the currently installed default allocator is used instead. The memory footprint of objects that are subsequently deleted via calls to the deleteObject method of the deleter will be returned to the allocator that was established at construction. Note that the allocator used to create the footprint of objects passed to deleteObject must be the same allocator that is used by the deleter.
Usage:
Suppose that we would like to transfer ownership of an object between threads using bsl::shared_ptr. For the sake of discussion, the type of this object is my_Obj and we will suppose that it is created using a given basicAllocator. Note that we assume that my_Obj does not require an allocator for any of its members:
  bslma::NewDeleteAllocator basicAllocator;
  my_Obj *object = new(basicAllocator) my_Obj;
Next, create a concrete deleter for object using the same allocator as was used to allocate its footprint:
  bdlma::DefaultDeleter<my_Obj> deleter(&basicAllocator);
Finally, create a shared pointer passing to it object and the address of deleter:
  bsl::shared_ptr<my_Obj> handle(object, &deleter, &basicAllocator);
Now the handle can be passed to another thread or enqueued efficiently. When the reference count of handle goes to 0, object is automatically deleted via the deleteObject method of deleter, which in turn will invoke the destructor of object. Note that since the type of the deleter used to instantiate handle is bdlma::Deleter<my_Obj>, any kind of deleter that implements this protocol can be passed. Also note, on the downside, that the lifetime of deleter must be longer than the lifetime of all associated instances.