BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslma_exceptionguard.h
Go to the documentation of this file.
1/// @file bslma_exceptionguard.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslma_exceptionguard.h -*-C++-*-
8#ifndef INCLUDED_BSLMA_EXCEPTIONGUARD
9#define INCLUDED_BSLMA_EXCEPTIONGUARD
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslma_exceptionguard bslma_exceptionguard
15/// @brief Provide a check that objects throwing exceptions do not change.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslma
19/// @{
20/// @addtogroup bslma_exceptionguard
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslma_exceptionguard-purpose"> Purpose</a>
25/// * <a href="#bslma_exceptionguard-classes"> Classes </a>
26/// * <a href="#bslma_exceptionguard-description"> Description </a>
27/// * <a href="#bslma_exceptionguard-usage"> Usage </a>
28///
29/// # Purpose {#bslma_exceptionguard-purpose}
30/// Provide a check that objects throwing exceptions do not change.
31///
32/// # Classes {#bslma_exceptionguard-classes}
33///
34/// - bslma::ExceptionGuard: guard to check an object state has not changed
35///
36/// @see bslma_testallocator
37///
38/// # Description {#bslma_exceptionguard-description}
39/// This component provides a class, `bslma::ExceptionGuard`, that
40/// can be used to ASSERT if an object changes state when a method fails by
41/// throwing an exception. This is often used to validate the strong exception
42/// safety guarantee in a test driver, usually with the test support macros
43/// provided by the component @ref bslma_testallocator , such as
44/// `BSLMA_TESTALLOCATOR_EXCEPTION_TEST_BEGIN`. The object under test must be
45/// CopyConstructible, and support the extended copy constructor taking an
46/// allocator. Note that this may be a generalised STL allocator, conforming to
47/// the Allocator requirements of the C++ standard, rather than just a
48/// `bslma::Allocator`. This allows for testing standard library components
49/// such as those in `bsl`.
50///
51/// As the constructor must make a copy of the object under test, this class
52/// should not be used in a test driver until after the extended copy
53/// constructor has been proven tested. Similarly, the destructor asserts that
54/// the value has not changed using `operator==`, which should also be confirmed
55/// as correct before relying on this class in a test driver. Finally, the
56/// `resetvalue` method should not be used prior to validating the copy-
57/// assignment operator.
58///
59/// ## Usage {#bslma_exceptionguard-usage}
60///
61///
62/// TBD ...
63/// @}
64/** @} */
65/** @} */
66
67/** @addtogroup bsl
68 * @{
69 */
70/** @addtogroup bslma
71 * @{
72 */
73/** @addtogroup bslma_exceptionguard
74 * @{
75 */
76
77#include <bslscm_version.h>
78
79#include <bsls_assert.h>
80
81
82
83namespace bslma {
84
85class Allocator;
86
87 // ====================
88 // class ExceptionGuard
89 // ====================
90
91/// This class provide a mechanism to verify the strong exception guarantee
92/// in exception-throwing code. On construction, this class stores the a
93/// copy of an object of the parameterized type `OBJECT` and the address of
94/// that object. On destruction, if `release` was not invoked, it will
95/// verify the value of the object is the same as the value of the copy
96/// create on construction. This class requires the copy constructor and
97/// `operator ==` to be tested before use.
98///
99/// See @ref bslma_exceptionguard
100template <class OBJECT>
102
103 // DATA
104 int d_line; // the line number at construction
105 OBJECT d_copy; // copy of the object being tested
106 const OBJECT *d_object_p; // address of the original object
107
108 public:
109 // CREATORS
110
111 /// Create the exception guard for the specified `object` at the
112 /// specified `line` number. Optionally, specify `basicAllocator` used
113 /// to supply memory.
114 ExceptionGuard(const OBJECT *object,
115 int line,
116 Allocator *basicAllocator = 0);
117
118 /// Create the exception guard for the specified `object` at the
119 /// specified `line` number. Optionally, specify `basicAllocator` used
120 /// to supply memory.
121 template <class ALLOCATOR>
122 ExceptionGuard(const OBJECT *object,
123 int line,
124 const ALLOCATOR& basicAllocator);
125
126 /// Destroy the exception guard. If the guard was not released, verify
127 /// that the state of the object supplied at construction has not
128 /// change.
130
131 // MANIPULATORS
132
133 /// Release the guard from verifying the state of the object.
134 void release();
135
136 /// Reset the expected state of the guarded object, if an exception
137 /// should propagate past this guard, to the specified `value`, which is
138 /// set from the specified `line`.
139 void resetValue(const OBJECT& value, int line);
140};
141
142// ============================================================================
143// INLINE DEFINITIONS
144// ============================================================================
145
146
147 // --------------------
148 // class ExceptionGuard
149 // --------------------
150
151// CREATORS
152template <class OBJECT>
153inline
155 int line,
156 Allocator *basicAllocator)
157: d_line(line)
158, d_copy(*object, basicAllocator)
159, d_object_p(object)
160{
161}
162
163template <class OBJECT>
164template <class ALLOCATOR>
165inline
167 int line,
168 const ALLOCATOR& basicAllocator)
169: d_line(line)
170, d_copy(*object, basicAllocator)
171, d_object_p(object)
172{
173}
174
175template <class OBJECT>
177{
178 if (d_object_p) {
179 // This test is tricky, as it is typically triggered while an exception
180 // is active, and so should not have an assert handler that in turn
181 // throws an exception. Note that as this assertion is the whole
182 // purpose of the class, we use 'BSLS_ASSERT_OPT' so that it is active
183 // in most build modes.
184 BSLS_ASSERT_OPT(d_copy == *d_object_p);
185 }
186}
187
188// MANIPULATORS
189template <class OBJECT>
190inline
192{
193 d_object_p = 0;
194}
195
196template <class OBJECT>
197inline
198void ExceptionGuard<OBJECT>::resetValue(const OBJECT& value, int line)
199{
200 d_copy = value;
201 d_line = line;
202}
203
204} // close package namespace
205
206
207
208#endif
209
210// ----------------------------------------------------------------------------
211// Copyright 2013 Bloomberg Finance L.P.
212//
213// Licensed under the Apache License, Version 2.0 (the "License");
214// you may not use this file except in compliance with the License.
215// You may obtain a copy of the License at
216//
217// http://www.apache.org/licenses/LICENSE-2.0
218//
219// Unless required by applicable law or agreed to in writing, software
220// distributed under the License is distributed on an "AS IS" BASIS,
221// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
222// See the License for the specific language governing permissions and
223// limitations under the License.
224// ----------------------------- END-OF-FILE ----------------------------------
225
226/** @} */
227/** @} */
228/** @} */
Definition bslma_allocator.h:457
Definition bslma_exceptionguard.h:101
~ExceptionGuard()
Definition bslma_exceptionguard.h:176
void release()
Release the guard from verifying the state of the object.
Definition bslma_exceptionguard.h:191
void resetValue(const OBJECT &value, int line)
Definition bslma_exceptionguard.h:198
ExceptionGuard(const OBJECT *object, int line, Allocator *basicAllocator=0)
Definition bslma_exceptionguard.h:154
#define BSLS_ASSERT_OPT(X)
Definition bsls_assert.h:1856
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balxml_encoderoptions.h:68