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

Detailed Description

Outline

Purpose

Provide a protocol for creator/deleter of parameterized objects.

Classes

See also
bdlma_deleter

Description

This component defines the base-level protocol for a thread-safe and type-safe creator and deleter of objects of parameterized type. In particular, bdlma::Factory extends the bdlma::Deleter protocol with the addition of a createObject method:

| createObject
v
dtor
deleteObject
Definition bdlma_deleter.h:109
Definition bdlma_factory.h:118

This class is extremely useful for transferring the ownership of objects between different entities. The following usage example demonstrates this point.

Usage

This section illustrates intended use of this component.

Example 1: Basic Usage

Suppose that we would like to transfer 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 concrete implementation of bdlma::Factory, say, my_Factory, the implementation of which is assumed:

my_Factory factory;
my_Obj *object = factory.createObject();

Next we create a shared pointer passing to it object and the factory that was used to create object:

bsl::shared_ptr<my_Obj> handle(object, &factory);
Definition bslstl_sharedptr.h:1830

Now the handle can be passed to another thread or enqueued efficiently. Once the reference count of handle goes to 0, object is automatically deleted via the deleteObject method of factory, which in turn will invoke the destructor of object. Note that since the type of the factory used to both create the object under management and to instantiate handle is bdlma::Factory<my_Obj>, any kind of creator/deleter that implements this protocol can be passed. Also note, on the downside, that the lifetime of factory must be longer than the lifetime of all associated object instances.