BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlma_factory.h
Go to the documentation of this file.
1/// @file bdlma_factory.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlma_factory.h -*-C++-*-
8#ifndef INCLUDED_BDLMA_FACTORY
9#define INCLUDED_BDLMA_FACTORY
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlma_factory bdlma_factory
15/// @brief Provide a protocol for creator/deleter of parameterized objects.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlma
19/// @{
20/// @addtogroup bdlma_factory
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlma_factory-purpose"> Purpose</a>
25/// * <a href="#bdlma_factory-classes"> Classes </a>
26/// * <a href="#bdlma_factory-description"> Description </a>
27/// * <a href="#bdlma_factory-usage"> Usage </a>
28/// * <a href="#bdlma_factory-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#bdlma_factory-purpose}
31/// Provide a protocol for creator/deleter of parameterized objects.
32///
33/// # Classes {#bdlma_factory-classes}
34///
35/// - bdlma::Factory: protocol class for creator/deleter of `TYPE` objects
36///
37/// @see bdlma_deleter
38///
39/// # Description {#bdlma_factory-description}
40/// This component defines the base-level protocol for a
41/// thread-safe and type-safe creator and deleter of objects of parameterized
42/// type. In particular, `bdlma::Factory` extends the `bdlma::Deleter` protocol
43/// with the addition of a `createObject` method:
44/// @code
45/// ( bdlma::Factory )
46/// | createObject
47/// v
48/// ( bdlma::Deleter )
49/// dtor
50/// deleteObject
51/// @endcode
52/// This class is extremely useful for transferring the ownership of objects
53/// between different entities. The following usage example demonstrates this
54/// point.
55///
56/// ## Usage {#bdlma_factory-usage}
57///
58///
59/// This section illustrates intended use of this component.
60///
61/// ### Example 1: Basic Usage {#bdlma_factory-example-1-basic-usage}
62///
63///
64/// Suppose that we would like to transfer an object between threads using
65/// `bsl::shared_ptr`. For the sake of discussion, the type of this object is
66/// `my_Obj` and we will suppose that it is created using a concrete
67/// implementation of `bdlma::Factory`, say, `my_Factory`, the implementation of
68/// which is assumed:
69/// @code
70/// my_Factory factory;
71/// my_Obj *object = factory.createObject();
72/// @endcode
73/// Next we create a shared pointer passing to it `object` and the `factory`
74/// that was used to create `object`:
75/// @code
76/// bsl::shared_ptr<my_Obj> handle(object, &factory);
77/// @endcode
78/// Now the `handle` can be passed to another thread or enqueued efficiently.
79/// Once the reference count of `handle` goes to 0, `object` is automatically
80/// deleted via the `deleteObject` method of `factory`, which in turn will
81/// invoke the destructor of `object`. Note that since the type of the factory
82/// used to both create the object under management and to instantiate `handle`
83/// is `bdlma::Factory<my_Obj>`, any kind of creator/deleter that implements
84/// this protocol can be passed. Also note, on the downside, that the lifetime
85/// of `factory` must be longer than the lifetime of all associated object
86/// instances.
87/// @}
88/** @} */
89/** @} */
90
91/** @addtogroup bdl
92 * @{
93 */
94/** @addtogroup bdlma
95 * @{
96 */
97/** @addtogroup bdlma_factory
98 * @{
99 */
100
101#include <bdlscm_version.h>
102
103#include <bdlma_deleter.h>
104
105
106namespace bdlma {
107
108 // =============
109 // class Factory
110 // =============
111
112/// This `class` provides a protocol (or pure interface) for a thread-safe
113/// object creator and deleter. It extends the `Deleter` protocol with the
114/// addition of a `createObject` method.
115///
116/// See @ref bdlma_factory
117template <class TYPE>
118class Factory : public Deleter<TYPE> {
119
120 public:
121 // CREATORS
122
123 /// Destroy this object factory.
124 virtual ~Factory();
125
126 /// Create an instance of the specified `TYPE` using default arguments.
127 /// Return the address of the newly-created object on success, and 0
128 /// otherwise. The instance must be destroyed using the `deleteObject`
129 /// method of this object factory.
130 virtual TYPE *createObject() = 0;
131
132 // MANIPULATORS
133
134 /// Destroy the specified `instance` based on its static type and return
135 /// its memory footprint to the appropriate memory manager. The
136 /// behavior is undefined unless `instance` was obtained from a call to
137 /// `createObject` on this object factory and has not yet been deleted.
138 virtual void deleteObject(TYPE *instance) = 0;
139};
140
141// ============================================================================
142// INLINE DEFINITIONS
143// ============================================================================
144
145// CREATORS
146template <class TYPE>
150
151} // close package namespace
152
153
154#endif
155
156// ----------------------------------------------------------------------------
157// Copyright 2015 Bloomberg Finance L.P.
158//
159// Licensed under the Apache License, Version 2.0 (the "License");
160// you may not use this file except in compliance with the License.
161// You may obtain a copy of the License at
162//
163// http://www.apache.org/licenses/LICENSE-2.0
164//
165// Unless required by applicable law or agreed to in writing, software
166// distributed under the License is distributed on an "AS IS" BASIS,
167// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
168// See the License for the specific language governing permissions and
169// limitations under the License.
170// ----------------------------- END-OF-FILE ----------------------------------
171
172/** @} */
173/** @} */
174/** @} */
Definition bdlma_deleter.h:109
Definition bdlma_factory.h:118
virtual ~Factory()
Destroy this object factory.
Definition bdlma_factory.h:147
virtual void deleteObject(TYPE *instance)=0
virtual TYPE * createObject()=0
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlma_alignedallocator.h:276