BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslma_stdtestallocator.h
Go to the documentation of this file.
1/// @file bslma_stdtestallocator.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslma_stdtestallocator.h -*-C++-*-
8#ifndef INCLUDED_BSLMA_STDTESTALLOCATOR
9#define INCLUDED_BSLMA_STDTESTALLOCATOR
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslma_stdtestallocator bslma_stdtestallocator
15/// @brief Provide stl-compatible, `bslma`-style allocator to track usage.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslma
19/// @{
20/// @addtogroup bslma_stdtestallocator
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslma_stdtestallocator-purpose"> Purpose</a>
25/// * <a href="#bslma_stdtestallocator-classes"> Classes </a>
26/// * <a href="#bslma_stdtestallocator-macros"> Macros </a>
27/// * <a href="#bslma_stdtestallocator-description"> Description </a>
28///
29/// # Purpose {#bslma_stdtestallocator-purpose}
30/// Provide stl-compatible, `bslma`-style allocator to track usage.
31///
32/// # Classes {#bslma_stdtestallocator-classes}
33///
34/// - bslma::StdTestAllocator: instrumented `bslma-style` stl allocator template
35///
36/// # Macros {#bslma_stdtestallocator-macros}
37///
38///
39/// @see TBD
40///
41/// # Description {#bslma_stdtestallocator-description}
42/// TBD
43/// @}
44/** @} */
45/** @} */
46
47/** @addtogroup bsl
48 * @{
49 */
50/** @addtogroup bslma
51 * @{
52 */
53/** @addtogroup bslma_stdtestallocator
54 * @{
55 */
56
57#include <bslscm_version.h>
58
62#include <bslma_testallocator.h>
63#include <bslma_default.h>
64
68#include <bslmf_issame.h>
70
71
72namespace bslma {
73
74 // ======================
75 // class StdTestAllocator
76 // ======================
77
78/// An STL-compatible test allocator that forwards allocation calls to an
79/// underlying mechanism object of a type derived from
80/// `bslma::TestAllocator`. This class template adheres to the allocator
81/// requirements defined in section 20.1.5 [lib.allocator.requirements] of
82/// the C++ standard and may be used to instantiate any [container] class
83/// template that follows the STL allocator protocol. The allocation
84/// mechanism is chosen at run-time, giving the programmer run-time control
85/// over how a container allocates and frees memory.
86///
87/// See @ref bslma_stdtestallocator
88template <class TYPE>
90
91 // DATA
92 TestAllocator *d_mechanism;
93
94 public:
95 // TRAITS
103 // Declare nested type traits for this class.
104
105 // PUBLIC TYPES
106 typedef std::size_t size_type;
107 typedef std::ptrdiff_t difference_type;
108 typedef TYPE *pointer;
109 typedef const TYPE *const_pointer;
111 void,
112 TYPE&>::type reference;
114 void,
115 const TYPE&>::type const_reference;
116 // typedef TYPE& reference;
117 // typedef const TYPE& const_reference;
118 typedef TYPE value_type;
119
120 /// This nested `struct` template, parameterized by `ANY_TYPE`, provides
121 /// a namespace for an `other` type alias, which is a `StdTestAllocator`
122 /// type following the same template as this one but that allocates elements of `ANY_TYPE`.
123 ///
124 /// \note Note that this `StdTestAllocator` type is
125 /// convertible to and from `other` for any type, including `void`.
126 ///
127 /// See @ref bslma_stdtestallocator
128 template <class ANY_TYPE>
129 struct rebind {
130
132 };
133
134 // CREATORS
135
136 /// TBD: fix comment to mention that the default allocator has to be
137 /// a test allocator.
138 /// Create a proxy object which will forward allocation calls to the
139 /// object pointed to by `bslma::Default::defaultAllocator()`.
140 /// Postcondition:
141 /// @code
142 /// this->mechanism() == bslma::Default::defaultAllocator();
143 /// @endcode
145
146 /// TBD: fix comment to mention that `mechanism` has to be a test
147 /// allocator.
148 /// Convert a `bslma::Allocator` pointer to an test allocator object
149 /// which forwards allocation calls to the object pointed to by the
150 /// specified `mechanism`. If `mechanism` is 0, then the currently
151 /// installed default allocator is used instead. Postcondition:
152 /// `0 == mechanism || this->mechanism() == mechanism`.
154
155 /// Create a proxy object using the same mechanism as the specified
156 /// `original`. Postcondition: `this->mechanism() == rhs.mechanism()`.
157 StdTestAllocator(const StdTestAllocator& original);
158
159 /// Create a proxy object sharing the same mechanism object as the
160 /// specified `rhs`. The newly constructed test allocator will compare
161 /// equal to `rhs`, even though they are instantiated on different
162 /// types. Postcondition: `this->mechanism() == rhs.mechanism()`.
163 template <class ANY_TYPE>
165
167 // Destroy this object. Note that this does not delete the object
168 // pointed to by 'mechanism()'. Also note that this method's
169 // definition is compiler generated.
170
172 // Assign to this object the value of the specified 'rhs'.
173 // Postcondition: 'this->mechanism() == rhs->mechanism()'. Note that
174 // this does not delete the object pointed to by the previous value of
175 // 'mechanism()'. Also note that this method's definition is compiler
176 // generated.
177
178 // MANIPULATORS
179
180 /// Allocate enough (properly aligned) space for the specified `n`
181 /// objects of (template parameter) `TYPE` by calling `allocate` on the
182 /// mechanism object. The optionally specified `hint` argument is
183 /// ignored by this test allocator type.
184 ///
185 /// \pre The behavior is undefined unless `n <= max_size()`.
186 pointer allocate(size_type n, const void *hint = 0);
187
188 /// Return memory previously allocated with `allocate` to the underlying
189 /// mechanism object by calling `deallocate` on the mechanism object
190 /// with the specified `p`. The optionally specified `n` argument is
191 /// ignored by this test allocator type.
192 void deallocate(pointer p, size_type n = 1);
193
194 /// Copy-construct an object of (template parameter) `TYPE` from the
195 /// specified `val` at the memory address specified by `p`. Do not directly allocate memory.
196 ///
197 /// \pre The behavior is undefined unless `p` is
198 /// not properly aligned for objects of the given `TYPE`.
199 template <class ELEMENT_TYPE>
200 void construct(ELEMENT_TYPE *address, const TYPE& val);
201
202 /// Call the `TYPE` destructor for the object pointed to by the
203 /// specified `p`. Do not directly deallocate any memory.
204 template <class ELEMENT_TYPE>
205 void destroy(ELEMENT_TYPE *address);
206
207 // ACCESSORS
208
209 /// Return the address of the object referred to by the specified `x`,
210 /// even if the (template parameter) `TYPE` overloads the unary
211 /// `operator&`.
212 pointer address(reference x) const;
213
214 /// Return the address of the object referred to by the specified `x`,
215 /// even if the (template parameter) `TYPE` overloads the unary
216 /// `operator&`.
218
219 /// Return the maximum number of elements of (template parameter) `TYPE` that can be allocated using this test allocator.
220 ///
221 /// \note Note that there is
222 /// no guarantee that attempts at allocating fewer elements than the
223 /// value returned by @ref max_size will not throw.
224 size_type max_size() const;
225
226 /// Return a pointer to the mechanism object to which this proxy
227 /// forwards allocation and deallocation calls.
228 TestAllocator *mechanism() const;
229};
230
231// FREE OPERATORS
232
233/// Return `true` if the specified `lhs` and `rhs` are proxies for the same
234/// `bslma::TestAllocator` object. This is a practical implementation of
235/// the STL requirement that two allocators compare equal if and only if
236/// memory allocated from one can be deallocated from the other.
237///
238/// \note Note that the two allocators need not be instantiated on the same type in order to
239/// compare equal.
240template <class T1, class T2>
241inline
242bool operator==(const StdTestAllocator<T1>& lhs,
243 const StdTestAllocator<T2>& rhs);
244
245/// Return `true` unless the specified `lhs` and `rhs` are proxies for the
246/// same `bslma::TestAllocator` object, in which case return `false`. This
247/// is a practical implementation of the STL requirement that two allocators
248/// compare equal if and only if memory allocated from one can be deallocated from the other.
249///
250/// \note Note that the two allocators need not be
251/// instantiated on the same type in order to compare equal.
252template <class T1, class T2>
253inline
254bool operator!=(const StdTestAllocator<T1>& lhs,
255 const StdTestAllocator<T2>& rhs);
256
257/// Return `true` if the specified `lhs` is a proxy for the specified `rhs`,
258/// and `false` otherwise.
259template <class TYPE>
260inline
261bool operator==(const StdTestAllocator<TYPE>& lhs,
262 const TestAllocator *rhs);
263
264/// Return `true` unless the specified `lhs` is a proxy for the specified
265/// `rhs`, in which case return `false`.
266template <class TYPE>
267inline
268bool operator!=(const StdTestAllocator<TYPE>& lhs,
269 const TestAllocator *rhs);
270
271/// Return `true` if the specified `rhs` is a proxy for the specified `lhs`,
272/// and `false` otherwise.
273template <class TYPE>
274inline
275bool operator==(const TestAllocator *lhs,
276 const StdTestAllocator<TYPE>& rhs);
277
278/// Return `true` unless the specified `rhs` is a proxy for the specified
279/// `lhs`, in which case return `false`.
280template <class TYPE>
281inline
282bool operator!=(const TestAllocator *lhs,
283 const StdTestAllocator<TYPE>& rhs);
284
285
286// ============================================================================
287// INLINE FUNCTION DEFINITIONS
288// ============================================================================
289
290 // ----------------------
291 // class StdTestAllocator
292 // ----------------------
293
294// LOW-LEVEL ACCESSORS
295template <class TYPE>
296inline
298{
299 return d_mechanism;
300}
301
302// CREATORS
303template <class TYPE>
304inline
306: d_mechanism(dynamic_cast<TestAllocator *>(Default::defaultAllocator()))
307{
308 BSLS_ASSERT_SAFE(d_mechanism);
309}
310
311template <class TYPE>
312inline
314: d_mechanism(dynamic_cast<TestAllocator *>(Default::allocator(mechanism)))
315{
316 BSLS_ASSERT_SAFE(d_mechanism);
317}
318
319template <class TYPE>
320inline
322: d_mechanism(original.mechanism())
323{
324 BSLS_ASSERT_SAFE(d_mechanism);
325}
326
327template <class TYPE>
328template <class ANY_TYPE>
329inline
331: d_mechanism(rhs.mechanism())
332{
333 BSLS_ASSERT_SAFE(d_mechanism);
334}
335
336// MANIPULATORS
337template <class TYPE>
338inline
341 const void *hint)
342{
343 BSLS_ASSERT_SAFE(n <= this->max_size());
344
345 (void) hint; // suppress unused parameter warning
346 return static_cast<pointer>(d_mechanism->allocate(n * sizeof(TYPE)));
347}
348
349template <class TYPE>
350inline
353{
354 (void) n; // suppress unused parameter warning
355 d_mechanism->deallocate(p);
356}
357
358template <class TYPE>
359template <class ELEMENT_TYPE>
360inline
361void StdTestAllocator<TYPE>::construct(ELEMENT_TYPE *address, const TYPE& val)
362{
363 new (static_cast<void*>(address)) ELEMENT_TYPE(val);
364}
365
366template <class TYPE>
367template <class ELEMENT_TYPE>
368inline
369void StdTestAllocator<TYPE>::destroy(ELEMENT_TYPE *address)
370{
371 DestructionUtil::destroy(address);
372}
373
374// ACCESSORS
375template <class TYPE>
376inline
382
383template <class TYPE>
384inline
386StdTestAllocator<TYPE>::address(reference x) const
387{
388 return BSLS_UTIL_ADDRESSOF(x);
389}
390
391template <class TYPE>
392inline
395{
396 // Return the largest value, 'v', such that 'v * sizeof(T)' fits in a
397 // 'size_type'.
398
399 // We will calculate MAX_NUM_BYTES based on our knowledge that
400 // 'bslma::Allocator::size_type' is just an alias for 'std::size_t'. First
401 // demonstrate that is true:
402
404
405 static const std::size_t MAX_NUM_BYTES = ~std::size_t(0);
406 static const std::size_t MAX_NUM_ELEMENTS = MAX_NUM_BYTES / sizeof(TYPE);
407
408 return MAX_NUM_ELEMENTS;
409}
410
411// FREE OPERATORS
412template <class T1, class T2>
413inline
414bool operator==(const StdTestAllocator<T1>& lhs,
415 const StdTestAllocator<T2>& rhs)
416{
417 return lhs.mechanism() == rhs.mechanism();
418}
419
420template <class T1, class T2>
421inline
422bool operator!=(const StdTestAllocator<T1>& lhs,
423 const StdTestAllocator<T2>& rhs)
424{
425 return ! (lhs == rhs);
426}
427
428template <class TYPE>
429inline
430bool operator==(const StdTestAllocator<TYPE>& lhs,
431 const TestAllocator *rhs)
432{
433 return lhs.mechanism() == rhs;
434}
435
436template <class TYPE>
437inline
438bool operator!=(const StdTestAllocator<TYPE>& lhs,
439 const TestAllocator *rhs)
440{
441 return ! (lhs == rhs);
442}
443
444template <class TYPE>
445inline
446bool operator==(const TestAllocator *lhs,
447 const StdTestAllocator<TYPE>& rhs)
448{
449 return lhs == rhs.mechanism();
450}
451
452template <class TYPE>
453inline
454bool operator!=(const TestAllocator *lhs,
455 const StdTestAllocator<TYPE>& rhs)
456{
457 return ! (lhs == rhs);
458}
459
460} // close package namespace
461
462
463#endif
464
465// ----------------------------------------------------------------------------
466// Copyright 2013 Bloomberg Finance L.P.
467//
468// Licensed under the Apache License, Version 2.0 (the "License");
469// you may not use this file except in compliance with the License.
470// You may obtain a copy of the License at
471//
472// http://www.apache.org/licenses/LICENSE-2.0
473//
474// Unless required by applicable law or agreed to in writing, software
475// distributed under the License is distributed on an "AS IS" BASIS,
476// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
477// See the License for the specific language governing permissions and
478// limitations under the License.
479// ----------------------------- END-OF-FILE ----------------------------------
480
481/** @} */
482/** @} */
483/** @} */
Definition bslma_allocator.h:545
Definition bslma_stdtestallocator.h:89
std::size_t size_type
Definition bslma_stdtestallocator.h:106
StdTestAllocator()
Definition bslma_stdtestallocator.h:305
BSLMF_NESTED_TRAIT_DECLARATION(StdTestAllocator, bslmf::IsBitwiseEqualityComparable)
void construct(ELEMENT_TYPE *address, const TYPE &val)
Definition bslma_stdtestallocator.h:361
pointer address(reference x) const
Definition bslma_stdtestallocator.h:378
bsl::conditional< bsl::is_void< TYPE >::value, void, TYPE & >::type reference
Definition bslma_stdtestallocator.h:112
BSLMF_NESTED_TRAIT_DECLARATION(StdTestAllocator, bslmf::IsBitwiseMoveable)
TestAllocator * mechanism() const
Definition bslma_stdtestallocator.h:297
bsl::conditional< bsl::is_void< TYPE >::value, void, constTYPE & >::type const_reference
Definition bslma_stdtestallocator.h:115
BSLMF_NESTED_TRAIT_DECLARATION(StdTestAllocator, bslmf::IsBitwiseCopyable)
pointer allocate(size_type n, const void *hint=0)
Definition bslma_stdtestallocator.h:340
void deallocate(pointer p, size_type n=1)
Definition bslma_stdtestallocator.h:351
const TYPE * const_pointer
Definition bslma_stdtestallocator.h:109
const_pointer address(const_reference x) const
void destroy(ELEMENT_TYPE *address)
Definition bslma_stdtestallocator.h:369
TYPE value_type
Definition bslma_stdtestallocator.h:118
StdTestAllocator & operator=(const StdTestAllocator &rhs)
std::ptrdiff_t difference_type
Definition bslma_stdtestallocator.h:107
size_type max_size() const
Definition bslma_stdtestallocator.h:394
TYPE * pointer
Definition bslma_stdtestallocator.h:108
BSLMF_NESTED_TRAIT_DECLARATION(StdTestAllocator, bslma::IsStdAllocator)
Definition bslma_testallocator.h:402
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:231
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_UTIL_ADDRESSOF(OBJ)
Definition bsls_util.h:296
Definition baljsn_encoder_testtypes.h:76
Definition bdldfp_decimal.h:5549
Definition bslmf_conditional.h:123
Definition bslmf_integralconstant.h:261
Definition bslmf_issame.h:146
Definition bslma_default.h:745
Definition bslma_isstdallocator.h:202
Definition bslma_stdtestallocator.h:129
StdTestAllocator< ANY_TYPE > other
Definition bslma_stdtestallocator.h:131
Definition bslmf_isbitwisecopyable.h:298
Definition bslmf_isbitwiseequalitycomparable.h:500
Definition bslmf_isbitwisemoveable.h:718