BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsltf_stdtestallocator.h
Go to the documentation of this file.
1/// @file bsltf_stdtestallocator.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsltf_stdtestallocator.h -*-C++-*-
8#ifndef INCLUDED_BSLTF_STDTESTALLOCATOR
9#define INCLUDED_BSLTF_STDTESTALLOCATOR
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsltf_stdtestallocator bsltf_stdtestallocator
15/// @brief Provide a minimal standard compliant allocator.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsltf
19/// @{
20/// @addtogroup bsltf_stdtestallocator
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsltf_stdtestallocator-purpose"> Purpose</a>
25/// * <a href="#bsltf_stdtestallocator-classes"> Classes </a>
26/// * <a href="#bsltf_stdtestallocator-description"> Description </a>
27/// * <a href="#bsltf_stdtestallocator-usage"> Usage </a>
28/// * <a href="#bsltf_stdtestallocator-example-1-testing-the-support-for-stl-compliant-allocator"> Example 1: Testing The Support for STL-Compliant Allocator </a>
29///
30/// # Purpose {#bsltf_stdtestallocator-purpose}
31/// Provide a minimal standard compliant allocator.
32///
33/// # Classes {#bsltf_stdtestallocator-classes}
34///
35/// - bsltf::StdTestAllocatorConfiguration: namespace to configure allocator
36/// - bsltf::StdTestAllocatorConfigurationGuard: configuration scoped guard
37/// - bsltf::StdTestAllocator: standard compliant allocator
38///
39/// # Description {#bsltf_stdtestallocator-description}
40/// This component provides an allocator, `StdTestAllocator`, that
41/// defines the minimal interface to comply with section 20.1.5
42/// ([lib.allocator.requirements]) of the C++03 standard. This type can be used
43/// to verify that constructs designed to support a standard-compliant allocator
44/// access the allocator only through the standard-defined interface.
45///
46/// `StdTestAllocator` delegates its operations to a static `bslma::Allocator`
47/// (delegate allocator) that can be configured by the utilities provided in the
48/// namespace `StdTestAllocatorConfiguration`.
49/// `StdTestAllocatorConfigurationGuard` provides a scoped guard to enable
50/// temporary replacement of the delegate allocator.
51///
52/// ## Usage {#bsltf_stdtestallocator-usage}
53///
54///
55/// This section illustrates intended use of this component.
56///
57/// ### Example 1: Testing The Support for STL-Compliant Allocator {#bsltf_stdtestallocator-example-1-testing-the-support-for-stl-compliant-allocator}
58///
59///
60/// In this example we will verify that a type supports the use of a
61/// STL-compliant allocator.
62///
63/// First we define a simple container type intended to be used with a C++03
64/// standard compliant allocator:
65/// @code
66/// template <class TYPE, class ALLOCATOR>
67/// class MyContainer {
68/// // This container type is parameterized on a standard allocator type
69/// // and contains a single object, always initialized, which can be
70/// // replaced and accessed.
71///
72/// // DATA MEMBERS
73/// ALLOCATOR d_allocator; // allocator used to supply memory (held, not
74/// // owned)
75///
76/// TYPE *d_object_p; // pointer to the contained object
77///
78/// public:
79/// // CONSTRUCTORS
80/// MyContainer(const TYPE& object);
81/// // Create an container containing the specified 'object', using the
82/// // parameterized 'ALLOCATOR' to supply memory.
83///
84/// ~MyContainer();
85/// // Destroy this container.
86///
87/// // MANIPULATORS
88/// TYPE& object();
89/// // Return a reference providing modifiable access to the object
90/// // contained in this container.
91///
92/// // ACCESSORS
93/// const TYPE& object() const;
94/// // Return a reference providing non-modifiable access to the object
95/// // contained in this container.
96/// };
97/// @endcode
98/// Then, we define the member functions of `MyContainer`:
99/// @code
100/// // CREATORS
101/// template <class TYPE, class ALLOCATOR>
102/// MyContainer<TYPE, ALLOCATOR>::MyContainer(const TYPE& object)
103/// {
104/// d_object_p = d_allocator.allocate(1);
105/// d_allocator.construct(d_object_p, object);
106/// }
107///
108/// template <class TYPE, class ALLOCATOR>
109/// MyContainer<TYPE, ALLOCATOR>::~MyContainer()
110/// {
111/// d_allocator.destroy(d_object_p);
112/// d_allocator.deallocate(d_object_p);
113/// }
114///
115/// // MANIPULATORS
116/// template <class TYPE, class ALLOCATOR>
117/// TYPE& MyContainer<TYPE, ALLOCATOR>::object()
118/// {
119/// return *d_object_p;
120/// }
121///
122/// // ACCESSORS
123/// template <class TYPE, class ALLOCATOR>
124/// const TYPE& MyContainer<TYPE, ALLOCATOR>::object() const
125/// {
126/// return *d_object_p;
127/// }
128/// @endcode
129/// Now, we use `StdTestAllocator` to implement a simple test for `MyContainer`
130/// to verify it correctly uses a parameterized allocator using only the C++03
131/// standard methods:
132/// @code
133/// bslma_TestAllocator oa("object", veryVeryVeryVerbose);
134/// StdTestAllocatorConfigurationGuard stag(&oa);
135/// {
136/// typedef MyContainer<int, StdTestAllocator<int> > Obj;
137///
138/// Obj mX(2); const Obj& X = mX;
139/// assert(sizeof(int) == oa.numBytesInUse());
140///
141/// assert(X.object() == 2);
142///
143/// mX.object() = -10;
144/// assert(X.object() == -10);
145/// }
146///
147/// assert(0 == oa.numBytesInUse());
148/// @endcode
149/// @}
150/** @} */
151/** @} */
152
153/** @addtogroup bsl
154 * @{
155 */
156/** @addtogroup bsltf
157 * @{
158 */
159/** @addtogroup bsltf_stdtestallocator
160 * @{
161 */
162
163#include <bslscm_version.h>
164
165#include <bslma_allocator.h>
166#include <bslma_isstdallocator.h>
167
168#include <bslmf_util.h> // 'forward(V)'
169
170#include <bsls_assert.h>
173#include <bsls_types.h>
174#include <bsls_util.h>
175
176#include <new>
177#include <stddef.h>
178
179#if BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
180// Include version that can be compiled with C++03
181// Generated on Thu Oct 21 10:11:37 2021
182// Command line: sim_cpp11_features.pl bsltf_stdtestallocator.h
183# define COMPILING_BSLTF_STDTESTALLOCATOR_H
185# undef COMPILING_BSLTF_STDTESTALLOCATOR_H
186#else
187
188
189namespace bsltf {
190
191 // ===================================
192 // class StdTestAllocatorConfiguration
193 // ===================================
194
195/// This `struct` provides a namespace for functions that manipulate and
196/// access the *delegate allocator* for `StdTestAllocator`. The delegate
197/// allocator is the allocator to which `StdTestAllocator` objects delegate
198/// their operations. The provided operations are *not* thread-safe. Note
199/// that this allocator is configured globally as C++03 standard compliant
200/// allocators cannot have individually identifiable state.
202
203 public:
204 // CLASS METHODS
205
206 /// Set the address of the delegate allocator to the specified
207 /// `basicAllocator`.
208 static void setDelegateAllocatorRaw(bslma::Allocator *basicAllocator);
209
210 /// Return the address of the delegate allocator. Note that, this
211 /// method will initially return
212 /// `&bslma_NewDeleteAllocator::singleton()` if the
213 /// `setDelegatingAllocator` class method has not been called.
215};
216
217 // ========================================
218 // class StdTestAllocatorConfigurationGuard
219 // ========================================
220
221/// Upon construction, an object of this class saves the current *delegate
222/// allocator* for `StdTestAllocator` and and installs the user-specified
223/// allocator as the delegate allocator. The delegate allocator is the
224/// globally configured allocator to which an `StdTestAllocator` objects
225/// delegate their operations. On destruction, the original delegate
226/// allocator is restored.
227///
228/// See @ref bsltf_stdtestallocator
230
231 bslma::Allocator *d_original_p; // original (restore at destruction)
232
233 private:
234 // NOT IMPLEMENTED
239
240 public:
241 // CREATORS
242
243 /// Create a scoped guard that installs the specified
244 /// `temporaryAllocator` as the delegate allocator.
245 explicit
247
248 /// Restore the delegate allocator that was in place when this scoped
249 /// guard was created and destroy this guard.
251};
252
253
254 // ======================
255 // class StdTestAllocator
256 // ======================
257
258/// This allocator implements the minimal interface to comply with section
259/// 20.1.5 ([lib.allocator.requirements]) of the C++03 standard. Instances
260/// of this allocator delegate their operations to a globally configured
261/// delegate allocator as C++03 compliant allocators cannot have
262/// individually identifiable state (see `StdTestAllocatorConfiguration` and
263/// 'StdTestAllocatorConfigurationGuard).
264///
265/// See @ref bsltf_stdtestallocator
266template <class TYPE>
268
269 public:
270 // TRAITS
272
273 // PUBLIC TYPES
274 // Deliberately use types that will *not* have the same representation as
275 // the default 'size_t/ptrdiff_t' on most 64-bit platforms, yet will be
276 // wide enough to support our regular testing, as verified on 32-bit
277 // platforms.
280 typedef TYPE *pointer;
281 typedef const TYPE *const_pointer;
282 typedef TYPE& reference;
283 typedef const TYPE& const_reference;
284 typedef TYPE value_type;
285
286 /// This nested `struct` template, parameterized by some
287 /// `BDE_OTHER_TYPE`, provides a namespace for an `other` type alias,
288 /// which is an allocator type following the same template as this one
289 /// but that allocates elements of `BDE_OTHER_TYPE`. Note that this
290 /// allocator type is convertible to and from `other` for any
291 /// `BDE_OTHER_TYPE` including `void`.
292 template <class BDE_OTHER_TYPE>
293 struct rebind
294 {
295
297 };
298
299 // CREATORS
300
301 /// Create a `StdTestAllocator` object.
303
304 // StdTestAllocator(const StdTestAllocator& original) = default;
305 // Create a 'StdTestAllocator' object. Note that this object will
306 // compare equal to the default constructed object, because this type
307 // has no state.
308
309 /// Create a `StdTestAllocator` object. Note that this object will
310 /// compare equal to the default constructed object, because this type
311 /// has no state.
312 template <class BDE_OTHER_TYPE>
314
315 // ~StdTestAllocator() = default;
316 // Destroy this object.
317
318 // MANIPULATORS
319 // StdTestAllocator& operator=(const StdTestAllocator& rhs) = default;
320 // Assign to this object the value of the specified 'rhs' object, and
321 // return a reference providing modifiable access to this object.
322
323 /// Allocate enough (properly aligned) space for the specified
324 /// `numElements` of type `T`. If the configured delegate allocator is
325 /// unable to fulfill the allocation request, an exception (typically
326 /// `bsl::bad_alloc`) will be thrown. The behavior is undefined unless
327 /// `numElements <= max_size()`.
329
330 /// Return memory previously at the specified `address` for
331 /// `numElements` back to this allocator. The `numElements` argument is
332 /// ignored by this allocator type. The behavior is undefined unless
333 /// `address` was allocated using this allocator object and has not
334 /// already been deallocated.
335 void deallocate(pointer address, size_type numElements = 1);
336
337#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES // $var-args=14
338
339 /// Create an object of (template parameter) `ELEMENT_TYPE` at the
340 /// specified `address`, constructed by forwarding the specified
341 /// `argument1` and the (variable number of) additional specified
342 /// `arguments` to the corresponding constructor of `ELEMENT_TYPE`. The
343 /// behavior is undefined unless `address` refers to a block of memory
344 /// having sufficient size and alignment for an object of
345 /// `ELEMENT_TYPE`.
346 template <class ELEMENT_TYPE, class... Args>
347 void construct(ELEMENT_TYPE *address, Args&&... arguments);
348#endif
349
350 /// Call the `ELEMENT_TYPE` destructor for the object at the specified
351 /// `address` but do not deallocate the memory at `address`.
352 template <class ELEMENT_TYPE>
353 void destroy(ELEMENT_TYPE *address);
354
355 // ACCESSORS
356
357 /// Return the address providing modifiable access to `object`.
358 pointer address(reference object) const;
359
360 /// Return the address providing non-modifiable access to `object`.
362
363 /// Return the maximum number of elements of type `TYPE` that can be
364 /// allocated using this allocator in a single call to the `allocate`
365 /// method. Note that there is no guarantee that attempts at allocating
366 /// less elements than the value returned by @ref max_size will not throw.
367 size_type max_size() const;
368};
369
370 // ============================
371 // class StdTestAllocator<void>
372 // ============================
373
374/// This specialization of `StdTestAllocator` for `void` type as the
375/// parameterized `TYPE` does not contain members that are unrepresentable
376/// for `void`.
377template <>
378class StdTestAllocator<void> {
379
380 public:
381 // PUBLIC TYPES
382
383 // 'size_type' and @ref difference_type were deliberately changed from fixed
384 // 32 bit types to being the size of a pointer, to avoid a cascade of
385 // warnings on 64-bit builds.
386
389 typedef void *pointer;
390 typedef const void *const_pointer;
391 typedef void value_type;
392
393 /// This nested `struct` template, parameterized by some
394 /// `BDE_OTHER_TYPE`, provides a namespace for an `other` type alias,
395 /// which is an allocator type following the same template as this one
396 /// but that allocates elements of `BDE_OTHER_TYPE`. Note that this
397 /// allocator type is convertible to and from `other` for any
398 /// `BDE_OTHER_TYPE` including `void`.
399 template <class BDE_OTHER_TYPE>
400 struct rebind
401 {
402
404 };
405
406 // CREATORS
407
408 /// Create a `StdTestAllocator` object.
410
411 // StdTestAllocator(const StdTestAllocator& original) = default;
412 // Create a 'StdTestAllocator' object. Note that this object will
413 // compare equal to the default constructed object because, because
414 // this type has no state.
415
416 /// Create a `StdTestAllocator` object. Note that this object will
417 /// compare equal to the default constructed object because, because
418 /// this type has no state.
419 template <class BDE_OTHER_TYPE>
421
422 // ~StdTestAllocator() = default;
423 // Destroy this object.
424
425 // MANIPULATORS
426 // StdTestAllocator& operator=(
427 // const StdTestAllocator& rhs) = default;
428 // Assign to this object the value of the specified 'rhs' object, and
429 // return a reference providing modifiable access to this object.
430};
431
432// FREE OPERATORS
433
434/// Return `true` because `StdTestAllocator` does not hold a state.
435template <class TYPE1, class TYPE2>
436bool operator==(const StdTestAllocator<TYPE1>& lhs,
437 const StdTestAllocator<TYPE2>& rhs);
438
439/// Return `false` because `StdTestAllocator` does not hold a state.
440template <class TYPE1, class TYPE2>
441bool operator!=(const StdTestAllocator<TYPE1>& lhs,
442 const StdTestAllocator<TYPE2>& rhs);
443
444
445 // ======================
446 // class StdTestAllocator
447 // ======================
448
449/// This `struct` provides a namespace for utilities that are common to
450/// all instantiations of the `StdTestAllocator` class template.
452
453 // CLASS METHODS
454
455 /// Return the maximum number of objects, each taking the specified
456 /// `elementSize` bytes of storage, that can potentially be allocated by
457 /// a `StdTestAllocator`. Note that this function is mostly about
458 /// insulating consumers of this component from a standard header, so
459 /// that this test component does not hide missing header dependencies
460 /// in testing scenarios.
461 static size_t maxSize(size_t elementSize);
462};
463
464// ============================================================================
465// INLINE AND TEMPLATE FUNCTION IMPLEMENTATIONS
466// ============================================================================
467
468 // ----------------------------------------
469 // class StdTestAllocatorConfigurationGuard
470 // ----------------------------------------
471
472// CREATORS
473inline
474StdTestAllocatorConfigurationGuard::StdTestAllocatorConfigurationGuard(
475 bslma::Allocator *temporaryAllocator)
476: d_original_p(StdTestAllocatorConfiguration::delegateAllocator())
477{
478 BSLS_ASSERT(temporaryAllocator);
479
481}
482
483inline
490
491 // ----------------------
492 // class StdTestAllocator
493 // ----------------------
494
495// CREATORS
496template <class TYPE>
497inline
501
502template <class TYPE>
503template <class BDE_OTHER_TYPE>
508
509// MANIPULATORS
510template <class TYPE>
511inline
514 numElements)
515{
516 if (numElements > this->max_size()) {
517 BloombergLP::bsls::BslExceptionUtil::throwBadAlloc();
518 }
519
520 return
522 allocate(bslma::Allocator::size_type(numElements * sizeof(TYPE))));
523}
524
525template <class TYPE>
526inline
531
532#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES // $var-args=14
533template <class TYPE>
534template <class ELEMENT_TYPE, class... Args>
535inline void
536StdTestAllocator<TYPE>::construct(ELEMENT_TYPE *address, Args&&... arguments)
537{
538 ::new (static_cast<void*>(address)) ELEMENT_TYPE(
539 BSLS_COMPILERFEATURES_FORWARD(Args,arguments)...);
540}
541#endif
542
543template <class TYPE>
544template <class ELEMENT_TYPE>
545inline
546void StdTestAllocator<TYPE>::destroy(ELEMENT_TYPE *address)
547{
548 address->~ELEMENT_TYPE();
549}
550
551template <class TYPE>
552inline
555{
556 return bsls::Util::addressOf(object);
557}
558
559template <class TYPE>
560inline
566
567template <class TYPE>
568inline
574
575 // ----------------------------
576 // class StdTestAllocator<void>
577 // ----------------------------
578
579// CREATORS
580inline
584
585template <class BDE_OTHER_TYPE>
590
591// FREE OPERATORS
592template <class TYPE1, class TYPE2>
593inline
594bool operator==(const bsltf::StdTestAllocator<TYPE1>&,
596{
597 return true;
598}
599
600template <class TYPE1, class TYPE2>
601inline
602bool operator!=(const bsltf::StdTestAllocator<TYPE1>&,
604{
605 return false;
606}
607
608} // close package namespace
609
610
611#endif // End C++11 code
612
613#endif
614
615// ----------------------------------------------------------------------------
616// Copyright 2013 Bloomberg Finance L.P.
617//
618// Licensed under the Apache License, Version 2.0 (the "License");
619// you may not use this file except in compliance with the License.
620// You may obtain a copy of the License at
621//
622// http://www.apache.org/licenses/LICENSE-2.0
623//
624// Unless required by applicable law or agreed to in writing, software
625// distributed under the License is distributed on an "AS IS" BASIS,
626// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
627// See the License for the specific language governing permissions and
628// limitations under the License.
629// ----------------------------- END-OF-FILE ----------------------------------
630
631/** @} */
632/** @} */
633/** @} */
Definition bslma_allocator.h:457
virtual void deallocate(void *address)=0
std::size_t size_type
Definition bslma_allocator.h:499
Definition bsltf_stdtestallocator.h:229
~StdTestAllocatorConfigurationGuard()
Definition bsltf_stdtestallocator.h:484
const void * const_pointer
Definition bsltf_stdtestallocator.h:390
bsls::Types::IntPtr difference_type
Definition bsltf_stdtestallocator.h:388
bsls::Types::UintPtr size_type
Definition bsltf_stdtestallocator.h:387
void value_type
Definition bsltf_stdtestallocator.h:391
void * pointer
Definition bsltf_stdtestallocator.h:389
Definition bsltf_stdtestallocator.h:267
const TYPE * const_pointer
Definition bsltf_stdtestallocator.h:281
BSLMF_NESTED_TRAIT_DECLARATION(StdTestAllocator, bslma::IsStdAllocator)
pointer address(reference object) const
Return the address providing modifiable access to object.
Definition bsltf_stdtestallocator.h:554
pointer allocate(size_type numElements)
bsls::Types::UintPtr size_type
Definition bsltf_stdtestallocator.h:278
StdTestAllocator()
Create a StdTestAllocator object.
Definition bsltf_stdtestallocator.h:498
bsls::Types::IntPtr difference_type
Definition bsltf_stdtestallocator.h:279
const TYPE & const_reference
Definition bsltf_stdtestallocator.h:283
TYPE * pointer
Definition bsltf_stdtestallocator.h:280
void construct(ELEMENT_TYPE *address, Args &&... arguments)
Definition bsltf_stdtestallocator.h:536
size_type max_size() const
Definition bsltf_stdtestallocator.h:570
TYPE value_type
Definition bsltf_stdtestallocator.h:284
void deallocate(pointer address, size_type numElements=1)
Definition bsltf_stdtestallocator.h:527
TYPE & reference
Definition bsltf_stdtestallocator.h:282
void destroy(ELEMENT_TYPE *address)
Definition bsltf_stdtestallocator.h:546
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_COMPILERFEATURES_FORWARD(T, V)
Definition bsls_compilerfeatures.h:2018
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bsltf_allocargumenttype.h:92
Definition bslma_isstdallocator.h:201
std::size_t UintPtr
Definition bsls_types.h:126
std::ptrdiff_t IntPtr
Definition bsls_types.h:130
static TYPE * addressOf(TYPE &obj)
Definition bsls_util.h:305
Definition bsltf_stdtestallocator.h:201
static void setDelegateAllocatorRaw(bslma::Allocator *basicAllocator)
static bslma::Allocator * delegateAllocator()
Definition bsltf_stdtestallocator.h:294
StdTestAllocator< BDE_OTHER_TYPE > other
Definition bsltf_stdtestallocator.h:296
StdTestAllocator< BDE_OTHER_TYPE > other
Definition bsltf_stdtestallocator.h:403
Definition bsltf_stdtestallocator.h:451
static size_t maxSize(size_t elementSize)