BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslma_testallocatorexception.h
Go to the documentation of this file.
1/// @file bslma_testallocatorexception.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslma_testallocatorexception.h -*-C++-*-
8#ifndef INCLUDED_BSLMA_TESTALLOCATOREXCEPTION
9#define INCLUDED_BSLMA_TESTALLOCATOREXCEPTION
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslma_testallocatorexception bslma_testallocatorexception
15/// @brief Provide an exception class for memory allocation operations.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslma
19/// @{
20/// @addtogroup bslma_testallocatorexception
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslma_testallocatorexception-purpose"> Purpose</a>
25/// * <a href="#bslma_testallocatorexception-classes"> Classes </a>
26/// * <a href="#bslma_testallocatorexception-description"> Description </a>
27/// * <a href="#bslma_testallocatorexception-usage"> Usage </a>
28///
29/// # Purpose {#bslma_testallocatorexception-purpose}
30/// Provide an exception class for memory allocation operations.
31///
32/// # Classes {#bslma_testallocatorexception-classes}
33///
34/// - bslma::TestAllocatorException: exception containing allocation information
35///
36/// # Description {#bslma_testallocatorexception-description}
37/// This component defines a simple exception object for testing
38/// exceptions during memory allocation operations. The exception object
39/// `bslma::TestAllocatorException` contains information about the allocation
40/// request, which can be queried for by the "catcher" of this exception.
41///
42/// ## Usage {#bslma_testallocatorexception-usage}
43///
44///
45/// In the following example, the `bslma::TestAllocatorException` object is
46/// thrown by the `allocate` method of the `my_Allocator` object after the
47/// number of allocation requests exceeds the allocator's allocation limit.
48/// This example demonstrates how to use a user-defined allocator (e.g.,
49/// `my_Allocator`) and `bslma::TestAllocatorException` to verify that an object
50/// (e.g., `my_ShortArray`) under test is exception-neutral:
51/// @code
52/// // my_allocator.h
53/// #include <bslma_allocator.h>
54///
55/// class my_Allocator : public bslma::Allocator {
56/// int d_allocationLimit;
57/// // ...
58///
59/// private:
60/// // NOT IMPLEMENTED
61/// my_Allocator(const my_Allocator&);
62/// my_Allocator& operator=(const my_Allocator&);
63///
64/// public:
65/// // CREATORS
66/// my_Allocator() : d_allocationLimit(-1) {}
67/// ~my_Allocator() {}
68///
69/// void *allocate(int size);
70/// void deallocate(void *address) { free(address); }
71/// void setAllocationLimit(int limit){ d_allocationLimit = limit; }
72/// int allocationLimit() const { return d_allocationLimit; }
73/// // ...
74/// };
75///
76/// // my_allocator.cpp
77/// #include <my_allocator.h>
78///
79/// void *my_Allocator::allocate(int size)
80/// {
81/// #ifdef BDE_BUILD_TARGET_EXC
82/// if (0 <= d_allocationLimit) {
83/// --d_allocationLimit;
84/// if (0 > d_allocationLimit) {
85/// throw bslma::TestAllocatorException(size);
86/// }
87/// }
88/// #endif
89/// return (void *)malloc(size);
90/// }
91/// @endcode
92/// Note that the macro `BDE_BUILD_TARGET_EXC` is defined at compile-time to
93/// indicate whether exceptions are enabled. In the above code, if exceptions
94/// are not enabled, the code that throws `bslma::TestAllocatorException` is
95/// never executed. The following is the test driver for `my_ShortArray`.
96///
97/// Note that "\$" must be replaced by "\" in the preprocessor macro definitions
98/// that follow. The "$" symbols are present in this header file to avoid a
99/// diagnostic elicited by some compilers (e.g., "warning: multi-line comment").
100/// @code
101/// // my_shortarray.t.cpp
102/// #include <my_shortarray.h>
103/// #include <my_testallocator.h>
104/// #include <bslma_testallocatorexception.h>
105///
106/// // ...
107///
108/// #ifdef BDE_BUILD_TARGET_EXC
109/// #define BSLMA_TESTALLOCATOR_EXCEPTION_TEST_BEGIN { \$
110/// { \$
111/// static int firstTime = 1; \$
112/// if (veryVerbose && firstTime) std::cout << \$
113/// "### BSLMA EXCEPTION TEST -- (ENABLED) --" << std::endl; \$
114/// firstTime = 0; \$
115/// } \$
116/// if (veryVeryVerbose) std::cout << \$
117/// "### Begin bslma exception test." << std::endl; \$
118/// int bslmaExceptionCounter = 0; \$
119/// static int bslmaExceptionLimit = 100; \$
120/// testAllocator.setAllocationLimit(bslmaExceptionCounter); \$
121/// do { \$
122/// try {
123///
124/// #define BSLMA_TESTALLOCATOR_EXCEPTION_TEST_END \$
125/// } catch (bslma::TestAllocatorException& e) { \$
126/// if (veryVerbose && bslmaExceptionLimit || veryVeryVerbose) { \$
127/// --bslmaExceptionLimit; \$
128/// std::cout << "(*** " << bslmaExceptionCounter << ')'; \$
129/// if (veryVeryVerbose) { std::cout << " BSLMA_EXCEPTION: " \$
130/// << "alloc limit = " << bslmaExceptionCounter << ", " \$
131/// << "last alloc size = " << e.numBytes(); \$
132/// } \$
133/// else if (0 == bslmaExceptionLimit) { \$
134/// std::cout << " [ Note: 'bslmaExceptionLimit' reached. ]";\$
135/// } \$
136/// std::cout << std::endl; \$
137/// } \$
138/// testAllocator.setAllocationLimit(++bslmaExceptionCounter); \$
139/// continue; \$
140/// } \$
141/// testAllocator.setAllocationLimit(-1); \$
142/// break; \$
143/// } while (1); \$
144/// if (veryVeryVerbose) std::cout << \$
145/// "### End bslma exception test." << std::endl; \$
146/// }
147/// #else
148/// #define BSLMA_TESTALLOCATOR_EXCEPTION_TEST_BEGIN \$
149/// { \$
150/// static int firstTime = 1; \$
151/// if (verbose && firstTime) { std::cout << \$
152/// "### BSLMA EXCEPTION TEST -- (NOT ENABLED) --" << std::endl; \$
153/// firstTime = 0; \$
154/// } \$
155/// }
156/// #define BSLMA_TESTALLOCATOR_EXCEPTION_TEST_END
157/// #endif
158///
159/// // ...
160///
161/// static
162/// bool areEqual(const short *array1, const short *array2, int numElement)
163/// // Return 'true' if the specified initial 'numElement' in the specified
164/// // 'array1' and 'array2' have the same values, and 'false' otherwise.
165/// {
166/// for (int i = 0; i < numElement; ++i) {
167/// if (array1[i] != array2[i]) return false;
168/// }
169/// return true;
170/// }
171///
172/// int main(int argc, char *argv[]) {
173/// int test = argc > 1 ? atoi(argv[1]) : 0;
174/// int verbose = argc > 2;
175/// int veryVerbose = argc > 3;
176/// int veryVeryVerbose = argc > 4;
177///
178/// my_Allocator testAllocator;
179///
180/// switch (test) { case 0:
181///
182/// // ...
183///
184/// case 6: {
185/// struct {
186/// int d_line;
187/// int d_numElem;
188/// short d_exp[NUM_VALUES];
189/// } DATA[] = {
190/// { L_, 0, {} },
191/// { L_, 1, { V0 } },
192/// { L_, 5, { V0, V1, V2, V3, V4 } }
193/// };
194///
195/// const int NUM_TEST = sizeof DATA / sizeof *DATA;
196///
197/// for (int ti = 0; ti < NUM_TEST; ++ti) {
198/// const int LINE = DATA[ti].d_line;
199/// const int NUM_ELEM = DATA[ti].d_numElem;
200/// const short *EXP = DATA[ti].d_exp;
201///
202/// BSLMA_TESTALLOCATOR_EXCEPTION_TEST_BEGIN {
203/// my_ShortArray mA(&testAllocator);
204/// const my_ShortArray& A = mA;
205/// for (int ei = 0; ei < NUM_ELEM; ++ei) {
206/// mA.append(VALUES[ei]);
207/// }
208/// if (veryVerbose) { P_(ti); P_(NUM_ELEM); P(A); }
209/// LOOP2_ASSERT(LINE, ti, areEqual(EXP, A, NUM_ELEM));
210/// } BSLMA_TESTALLOCATOR_EXCEPTION_TEST_END
211/// }
212///
213/// if (veryVerbose) std::cout << testAllocator << std::endl;
214/// } break;
215///
216/// // ...
217///
218/// }
219///
220/// // ...
221/// }
222/// @endcode
223/// @}
224/** @} */
225/** @} */
226
227/** @addtogroup bsl
228 * @{
229 */
230/** @addtogroup bslma
231 * @{
232 */
233/** @addtogroup bslma_testallocatorexception
234 * @{
235 */
236
237#include <bslscm_version.h>
238
239#include <bslma_allocator.h>
240
241#include <bsls_exceptionutil.h>
242#include <bsls_keyword.h>
243
244#include <new> // For 'std::bad_alloc'
245
246
247
248namespace bslma {
249
250 // ============================
251 // class TestAllocatorException
252 // ============================
253
254/// This class defines an exception object for memory allocation operations.
255/// Objects of this class contain information about an allocation request.
256///
257/// See @ref bslma_testallocatorexception
258class TestAllocatorException : public std::bad_alloc {
259
260 public:
261 // PUBLIC TYPES
262
263 /// Alias for the type used by the `Allocator` protocol to request a
264 /// memory allocation of a given size.
266
267 private:
268 // DATA
269 size_type d_numBytes; // number of bytes requested in an allocation
270 // operation
271
272 public:
273 // CREATORS
274
275 /// Create an exception object initialized with the specified `numBytes`
276 /// that indicates an allocation request size.
278
279 /// Destroy this object. Note that this method's definition is compiler
280 /// generated.
282
283 // ACCESSORS
284
285 /// Return the number of bytes (supplied at construction) that indicates
286 /// an allocation request size.
287 size_type numBytes() const;
288
289 /// Return a pointer to the string literal "bslma::TestAllocatorException"
290 /// having a storage duration of the lifetime of the program. Note that
291 /// the caller should *not* attempt to free this memory.
292 const char *what() const BSLS_EXCEPTION_VIRTUAL_NOTHROW
294};
295
296// ============================================================================
297// INLINE FUNCTION DEFINITIONS
298// ============================================================================
299
300 // ----------------------------
301 // class TestAllocatorException
302 // ----------------------------
303
304// CREATORS
305inline
310
311// ACCESSORS
312inline
315{
316 return d_numBytes;
317}
318
319inline
320const char *
322{
323 return "bslma::TestAllocatorException";
324}
325
326} // close package namespace
327
328#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
329// ============================================================================
330// BACKWARD COMPATIBILITY
331// ============================================================================
332
333/// This alias is defined for backward compatibility.
335#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
336
337
338
339#endif
340
341// ----------------------------------------------------------------------------
342// Copyright 2013 Bloomberg Finance L.P.
343//
344// Licensed under the Apache License, Version 2.0 (the "License");
345// you may not use this file except in compliance with the License.
346// You may obtain a copy of the License at
347//
348// http://www.apache.org/licenses/LICENSE-2.0
349//
350// Unless required by applicable law or agreed to in writing, software
351// distributed under the License is distributed on an "AS IS" BASIS,
352// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
353// See the License for the specific language governing permissions and
354// limitations under the License.
355// ----------------------------- END-OF-FILE ----------------------------------
356
357/** @} */
358/** @} */
359/** @} */
std::size_t size_type
Definition bslma_allocator.h:499
Definition bslma_testallocatorexception.h:258
Allocator::size_type size_type
Definition bslma_testallocatorexception.h:265
size_type numBytes() const
Definition bslma_testallocatorexception.h:314
const char * what() const BSLS_EXCEPTION_VIRTUAL_NOTHROW BSLS_KEYWORD_OVERRIDE
Definition bslma_testallocatorexception.h:321
bslma::TestAllocatorException bslma_TestAllocatorException
This alias is defined for backward compatibility.
Definition bslma_testallocatorexception.h:334
#define BSLS_EXCEPTION_VIRTUAL_NOTHROW
Definition bsls_exceptionutil.h:402
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:653
Definition balxml_encoderoptions.h:68