Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdlma_factory
[Package bdlma]

Provide a protocol for creator/deleter of parameterized objects. More...

Namespaces

namespace  bdlma

Detailed Description

Outline
Purpose:
Provide a protocol for creator/deleter of parameterized objects.
Classes:
bdlma::Factory protocol class for creator/deleter of TYPE objects
See also:
Component 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:
                         ( bdlma::Factory )
                                 |       createObject
                                 v
                         ( bdlma::Deleter )
                                         dtor
                                         deleteObject
This class is extremely useful for transferring the ownership of objects between different entities. The following usage example demonstrates this point.
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);
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.