|
BDE 4.14.0 Production release
|
Provide a protocol for creator/deleter of parameterized objects.
TYPE objectsThis 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:
This class is extremely useful for transferring the ownership of objects between different entities. The following usage example demonstrates this point.
This section illustrates intended use of this component.
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:
Next we create a shared pointer passing to it object and the factory that was used to create object:
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.