BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslma_destructorguard.h
Go to the documentation of this file.
1/// @file bslma_destructorguard.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslma_destructorguard.h -*-C++-*-
8#ifndef INCLUDED_BSLMA_DESTRUCTORGUARD
9#define INCLUDED_BSLMA_DESTRUCTORGUARD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslma_destructorguard bslma_destructorguard
15/// @brief Provide a guard to unconditionally manage an object.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslma
19/// @{
20/// @addtogroup bslma_destructorguard
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslma_destructorguard-purpose"> Purpose</a>
25/// * <a href="#bslma_destructorguard-classes"> Classes </a>
26/// * <a href="#bslma_destructorguard-description"> Description </a>
27/// * <a href="#bslma_destructorguard-usage"> Usage </a>
28///
29/// # Purpose {#bslma_destructorguard-purpose}
30/// Provide a guard to unconditionally manage an object.
31///
32/// # Classes {#bslma_destructorguard-classes}
33///
34/// - bslma::DestructorGuard: guard to unconditionally manage an object
35///
36/// @see bslma_destructorproctor, bslma_autodestructor
37///
38/// # Description {#bslma_destructorguard-description}
39/// This component provides a guard class template,
40/// `bslma::DestructorGuard`, to unconditionally manage an (otherwise-unmanaged)
41/// object of parameterized `TYPE` supplied at construction. The managed object
42/// is destroyed automatically when the guard object goes out of scope by
43/// calling the (managed) object's destructor.
44///
45/// ## Usage {#bslma_destructorguard-usage}
46///
47///
48/// Suppose we have a situation where one of the two constructors will be called
49/// to create an object on the stack for performance reasons. The construction
50/// thus occurs within either of the branches of an `if` statement, so the
51/// object itself, to survive the end of the "then" or "else" block, must be
52/// constructed in a `bsls::ObjectBuffer`. Once constructed, the object would
53/// not be destroyed automatically, so to make sure it will be destroyed, we
54/// place it under the management of a `bslma::DestructorGuard`. After that, we
55/// know that however the routine exits -- either by a return or as a result of
56/// an exception being thrown -- the object will be destroyed.
57/// @code
58/// double usageExample(double startValue)
59/// {
60/// bsls::ObjectBuffer<std::vector<double> > buffer;
61/// std::vector<double>& myVec = buffer.object();
62///
63/// if (startValue >= 0) {
64/// new (&myVec) std::vector<double>(100, startValue);
65/// }
66/// else {
67/// new (&myVec) std::vector<double>();
68/// }
69///
70/// //***********************************************************
71/// // Note the use of the destructor guard on 'myVec' (below). *
72/// //***********************************************************
73///
74/// bslma::DestructorGuard<std::vector<double> > guard(&myVec);
75/// @endcode
76/// Note that regardless of how this routine terminates, `myVec` will be
77/// destroyed.
78/// @code
79/// // ...
80///
81/// myVec.push_back(3.0);
82/// @endcode
83/// Note that `push_back` could allocate memory and therefore may throw.
84/// However, if it does, `myVec` will be destroyed automatically along with
85/// `guard`.
86/// @code
87/// if (myVec[0] >= 5.0) {
88/// return 5.0; // RETURN
89/// @endcode
90/// Note that `myVec` is automatically destroyed as the function returns.
91/// @code
92/// }
93///
94/// return myVec[myVec.size() / 2];
95/// @endcode
96/// Note that `myVec` is destroyed after the temporary containing the return
97/// value is created.
98/// @code
99/// }
100/// @endcode
101/// @}
102/** @} */
103/** @} */
104
105/** @addtogroup bsl
106 * @{
107 */
108/** @addtogroup bslma
109 * @{
110 */
111/** @addtogroup bslma_destructorguard
112 * @{
113 */
114
115#include <bslscm_version.h>
116
117#include <bsls_assert.h>
118
119
120
121namespace bslma {
122
123 // =====================
124 // class DestructorGuard
125 // =====================
126
127/// This class implements a guard that unconditionally destroys a managed
128/// object upon destruction by invoking the (managed) object's destructor.
129///
130/// See @ref bslma_destructorguard
131template <class TYPE>
133
134 // DATA
135 TYPE *d_object_p; // managed object
136
137 // NOT IMPLEMENTED
140
141 public:
142 // CREATORS
143
144 /// Create a destructor guard that unconditionally manages the specified
145 /// `object`, and invokes the destructor of `object` upon the
146 /// destruction of this guard. The behavior is undefined unless
147 /// `object` is non-zero.
148 explicit DestructorGuard(TYPE *object);
149
150 /// Destroy this destructor guard and the object it manages by invoking
151 /// the destructor of the (managed) object.
153};
154
155// ============================================================================
156// INLINE DEFINITIONS
157// ============================================================================
158
159 // ---------------------
160 // class DestructorGuard
161 // ---------------------
162
163// CREATORS
164template <class TYPE>
165inline
167: d_object_p(object)
168{
169 BSLS_ASSERT_SAFE(object);
170}
171
172template <class TYPE>
173inline
175{
176 BSLS_ASSERT_SAFE(d_object_p);
177
178 d_object_p->~TYPE();
179}
180
181} // close package namespace
182
183#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
184// ============================================================================
185// BACKWARD COMPATIBILITY
186// ============================================================================
187
188#ifdef bslma_DestructorGuard
189#undef bslma_DestructorGuard
190#endif
191/// This alias is defined for backward compatibility.
192#define bslma_DestructorGuard bslma::DestructorGuard
193#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
194
195
196
197#endif
198
199// ----------------------------------------------------------------------------
200// Copyright 2013 Bloomberg Finance L.P.
201//
202// Licensed under the Apache License, Version 2.0 (the "License");
203// you may not use this file except in compliance with the License.
204// You may obtain a copy of the License at
205//
206// http://www.apache.org/licenses/LICENSE-2.0
207//
208// Unless required by applicable law or agreed to in writing, software
209// distributed under the License is distributed on an "AS IS" BASIS,
210// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
211// See the License for the specific language governing permissions and
212// limitations under the License.
213// ----------------------------- END-OF-FILE ----------------------------------
214
215/** @} */
216/** @} */
217/** @} */
Definition bslma_destructorguard.h:132
~DestructorGuard()
Definition bslma_destructorguard.h:174
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balxml_encoderoptions.h:68