BDE 4.39.x 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// clang-format off
181// Include version that can be compiled with C++03
182// Generated on Mon Jan 13 08:31:33 2025
183// Command line: sim_cpp11_features.pl bsltf_stdtestallocator.h
184
185# define COMPILING_BSLTF_STDTESTALLOCATOR_H
187# undef COMPILING_BSLTF_STDTESTALLOCATOR_H
188
189// clang-format on
190#else
191
192
193namespace bsltf {
194
195 // ===================================
196 // class StdTestAllocatorConfiguration
197 // ===================================
198
199/// This `struct` provides a namespace for functions that manipulate and
200/// access the *delegate allocator* for `StdTestAllocator`. The delegate
201/// allocator is the allocator to which `StdTestAllocator` objects delegate
202/// their operations. The provided operations are *not* thread-safe.
203///
204/// \note Note that this allocator is configured globally as C++03 standard compliant
205/// allocators cannot have individually identifiable state.
206///
207/// See @ref bsltf_stdtestallocator
209
210 public:
211 // CLASS METHODS
212
213 /// Set the address of the delegate allocator to the specified
214 /// `basicAllocator`.
215 static void setDelegateAllocatorRaw(bslma::Allocator *basicAllocator);
216
217 /// Return the address of the delegate allocator.
218 /// \note Note that, this
219 /// method will initially return
220 /// `&bslma_NewDeleteAllocator::singleton()` if the
221 /// `setDelegatingAllocator` class method has not been called.
223};
224
225 // ========================================
226 // class StdTestAllocatorConfigurationGuard
227 // ========================================
228
229/// Upon construction, an object of this class saves the current *delegate
230/// allocator* for `StdTestAllocator` and and installs the user-specified
231/// allocator as the delegate allocator. The delegate allocator is the
232/// globally configured allocator to which an `StdTestAllocator` objects
233/// delegate their operations. On destruction, the original delegate
234/// allocator is restored.
235///
236/// See @ref bsltf_stdtestallocator
238
239 bslma::Allocator *d_original_p; // original (restore at destruction)
240
241 private:
242 // NOT IMPLEMENTED
247
248 public:
249 // CREATORS
250
251 /// Create a scoped guard that installs the specified
252 /// `temporaryAllocator` as the delegate allocator.
253 explicit
255
256 /// Restore the delegate allocator that was in place when this scoped
257 /// guard was created and destroy this guard.
259};
260
261
262 // ======================
263 // class StdTestAllocator
264 // ======================
265
266/// This allocator implements the minimal interface to comply with section
267/// 20.1.5 ([lib.allocator.requirements]) of the C++03 standard. Instances
268/// of this allocator delegate their operations to a globally configured
269/// delegate allocator as C++03 compliant allocators cannot have
270/// individually identifiable state (see `StdTestAllocatorConfiguration` and
271/// 'StdTestAllocatorConfigurationGuard).
272///
273/// See @ref bsltf_stdtestallocator
274template <class TYPE>
276
277 public:
278 // TRAITS
280
281 // PUBLIC TYPES
282 // Deliberately use types that will *not* have the same representation as
283 // the default 'size_t/ptrdiff_t' on most 64-bit platforms, yet will be
284 // wide enough to support our regular testing, as verified on 32-bit
285 // platforms.
288 typedef TYPE *pointer;
289 typedef const TYPE *const_pointer;
290 typedef TYPE& reference;
291 typedef const TYPE& const_reference;
292 typedef TYPE value_type;
293
294 /// This nested `struct` template, parameterized by some
295 /// `BDE_OTHER_TYPE`, provides a namespace for an `other` type alias,
296 /// which is an allocator type following the same template as this one but that allocates elements of `BDE_OTHER_TYPE`.
297 ///
298 /// \note Note that this
299 /// allocator type is convertible to and from `other` for any
300 /// `BDE_OTHER_TYPE` including `void`.
301 template <class BDE_OTHER_TYPE>
302 struct rebind
303 {
304
306 };
307
308 // CREATORS
309
310 /// Create a `StdTestAllocator` object.
312
313 // StdTestAllocator(const StdTestAllocator& original) = default;
314 // Create a 'StdTestAllocator' object. Note that this object will
315 // compare equal to the default constructed object, because this type
316 // has no state.
317
318 /// Create a `StdTestAllocator` object.
319 /// \note Note that this object will
320 /// compare equal to the default constructed object, because this type
321 /// has no state.
322 template <class BDE_OTHER_TYPE>
324
325 // ~StdTestAllocator() = default;
326 // Destroy this object.
327
328 // MANIPULATORS
329 // StdTestAllocator& operator=(const StdTestAllocator& rhs) = default;
330 // Assign to this object the value of the specified 'rhs' object, and
331 // return a reference providing modifiable access to this object.
332
333 /// Allocate enough (properly aligned) space for the specified
334 /// `numElements` of type `T`. If the configured delegate allocator is
335 /// unable to fulfill the allocation request, an exception (typically `bsl::bad_alloc`) will be thrown.
336 ///
337 /// \pre The behavior is undefined unless
338 /// `numElements <= max_size()`.
340
341 /// Return memory previously at the specified `address` for
342 /// `numElements` back to this allocator. The `numElements` argument is ignored by this allocator type.
343 ///
344 /// \pre The behavior is undefined unless
345 /// `address` was allocated using this allocator object and has not
346 /// already been deallocated.
347 void deallocate(pointer address, size_type numElements = 1);
348
349#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES // $var-args=14
350
351 /// Create an object of (template parameter) `ELEMENT_TYPE` at the
352 /// specified `address`, constructed by forwarding the specified
353 /// `argument1` and the (variable number of) additional specified
354 /// `arguments` to the corresponding constructor of `ELEMENT_TYPE`.
355 ///
356 /// \pre The behavior is undefined unless `address` refers to a block of memory
357 /// having sufficient size and alignment for an object of
358 /// `ELEMENT_TYPE`.
359 template <class ELEMENT_TYPE, class... Args>
360 void construct(ELEMENT_TYPE *address, Args&&... arguments);
361#endif
362
363 /// Call the `ELEMENT_TYPE` destructor for the object at the specified
364 /// `address` but do not deallocate the memory at `address`.
365 template <class ELEMENT_TYPE>
366 void destroy(ELEMENT_TYPE *address);
367
368 // ACCESSORS
369
370 /// Return the address providing modifiable access to `object`.
371 pointer address(reference object) const;
372
373 /// Return the address providing non-modifiable access to `object`.
375
376 /// Return the maximum number of elements of type `TYPE` that can be
377 /// allocated using this allocator in a single call to the `allocate` method.
378 ///
379 /// \note Note that there is no guarantee that attempts at allocating
380 /// less elements than the value returned by @ref max_size will not throw.
381 size_type max_size() const;
382};
383
384 // ============================
385 // class StdTestAllocator<void>
386 // ============================
387
388/// This specialization of `StdTestAllocator` for `void` type as the
389/// parameterized `TYPE` does not contain members that are unrepresentable
390/// for `void`.
391template <>
392class StdTestAllocator<void> {
393
394 public:
395 // PUBLIC TYPES
396
397 // 'size_type' and @ref difference_type were deliberately changed from fixed
398 // 32 bit types to being the size of a pointer, to avoid a cascade of
399 // warnings on 64-bit builds.
400
403 typedef void *pointer;
404 typedef const void *const_pointer;
405 typedef void value_type;
406
407 /// This nested `struct` template, parameterized by some
408 /// `BDE_OTHER_TYPE`, provides a namespace for an `other` type alias,
409 /// which is an allocator type following the same template as this one but that allocates elements of `BDE_OTHER_TYPE`.
410 ///
411 /// \note Note that this
412 /// allocator type is convertible to and from `other` for any
413 /// `BDE_OTHER_TYPE` including `void`.
414 template <class BDE_OTHER_TYPE>
415 struct rebind
416 {
417
419 };
420
421 // CREATORS
422
423 /// Create a `StdTestAllocator` object.
425
426 // StdTestAllocator(const StdTestAllocator& original) = default;
427 // Create a 'StdTestAllocator' object. Note that this object will
428 // compare equal to the default constructed object because, because
429 // this type has no state.
430
431 /// Create a `StdTestAllocator` object.
432 /// \note Note that this object will
433 /// compare equal to the default constructed object because, because
434 /// this type has no state.
435 template <class BDE_OTHER_TYPE>
437
438 // ~StdTestAllocator() = default;
439 // Destroy this object.
440
441 // MANIPULATORS
442 // StdTestAllocator& operator=(
443 // const StdTestAllocator& rhs) = default;
444 // Assign to this object the value of the specified 'rhs' object, and
445 // return a reference providing modifiable access to this object.
446};
447
448// FREE OPERATORS
449
450/// Return `true` because `StdTestAllocator` does not hold a state.
451template <class TYPE1, class TYPE2>
452bool operator==(const StdTestAllocator<TYPE1>& lhs,
453 const StdTestAllocator<TYPE2>& rhs);
454
455/// Return `false` because `StdTestAllocator` does not hold a state.
456template <class TYPE1, class TYPE2>
457bool operator!=(const StdTestAllocator<TYPE1>& lhs,
458 const StdTestAllocator<TYPE2>& rhs);
459
460
461 // ======================
462 // class StdTestAllocator
463 // ======================
464
465/// This `struct` provides a namespace for utilities that are common to
466/// all instantiations of the `StdTestAllocator` class template.
467///
468/// See @ref bsltf_stdtestallocator
470
471 // CLASS METHODS
472
473 /// Return the maximum number of objects, each taking the specified
474 /// `elementSize` bytes of storage, that can potentially be allocated by a `StdTestAllocator`.
475 ///
476 /// \note Note that this function is mostly about
477 /// insulating consumers of this component from a standard header, so
478 /// that this test component does not hide missing header dependencies
479 /// in testing scenarios.
480 static size_t maxSize(size_t elementSize);
481};
482
483// ============================================================================
484// INLINE AND TEMPLATE FUNCTION IMPLEMENTATIONS
485// ============================================================================
486
487 // ----------------------------------------
488 // class StdTestAllocatorConfigurationGuard
489 // ----------------------------------------
490
491// CREATORS
492inline
493StdTestAllocatorConfigurationGuard::StdTestAllocatorConfigurationGuard(
494 bslma::Allocator *temporaryAllocator)
495: d_original_p(StdTestAllocatorConfiguration::delegateAllocator())
496{
497 BSLS_ASSERT(temporaryAllocator);
498
500}
501
502inline
509
510 // ----------------------
511 // class StdTestAllocator
512 // ----------------------
513
514// CREATORS
515template <class TYPE>
516inline
520
521template <class TYPE>
522template <class BDE_OTHER_TYPE>
527
528// MANIPULATORS
529template <class TYPE>
530inline
533 numElements)
534{
535 if (numElements > this->max_size()) {
536 BloombergLP::bsls::BslExceptionUtil::throwBadAlloc();
537 }
538
539 return
541 allocate(bslma::Allocator::size_type(numElements * sizeof(TYPE))));
542}
543
544template <class TYPE>
545inline
550
551#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES // $var-args=14
552template <class TYPE>
553template <class ELEMENT_TYPE, class... Args>
554inline void
555StdTestAllocator<TYPE>::construct(ELEMENT_TYPE *address, Args&&... arguments)
556{
557 ::new (static_cast<void*>(address)) ELEMENT_TYPE(
558 BSLS_COMPILERFEATURES_FORWARD(Args,arguments)...);
559}
560#endif
561
562template <class TYPE>
563template <class ELEMENT_TYPE>
564inline
565void StdTestAllocator<TYPE>::destroy(ELEMENT_TYPE *address)
566{
567 address->~ELEMENT_TYPE();
568}
569
570template <class TYPE>
571inline
574{
575 return bsls::Util::addressOf(object);
576}
577
578template <class TYPE>
579inline
585
586template <class TYPE>
587inline
593
594 // ----------------------------
595 // class StdTestAllocator<void>
596 // ----------------------------
597
598// CREATORS
599inline
603
604template <class BDE_OTHER_TYPE>
609
610// FREE OPERATORS
611template <class TYPE1, class TYPE2>
612inline
613bool operator==(const bsltf::StdTestAllocator<TYPE1>&,
615{
616 return true;
617}
618
619template <class TYPE1, class TYPE2>
620inline
621bool operator!=(const bsltf::StdTestAllocator<TYPE1>&,
623{
624 return false;
625}
626
627} // close package namespace
628
629
630#endif // End C++11 code
631
632#endif
633
634// ----------------------------------------------------------------------------
635// Copyright 2013 Bloomberg Finance L.P.
636//
637// Licensed under the Apache License, Version 2.0 (the "License");
638// you may not use this file except in compliance with the License.
639// You may obtain a copy of the License at
640//
641// http://www.apache.org/licenses/LICENSE-2.0
642//
643// Unless required by applicable law or agreed to in writing, software
644// distributed under the License is distributed on an "AS IS" BASIS,
645// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
646// See the License for the specific language governing permissions and
647// limitations under the License.
648// ----------------------------- END-OF-FILE ----------------------------------
649
650/** @} */
651/** @} */
652/** @} */
Definition bslma_allocator.h:545
virtual void deallocate(void *address)=0
std::size_t size_type
Definition bslma_allocator.h:593
Definition bsltf_stdtestallocator.h:237
~StdTestAllocatorConfigurationGuard()
Definition bsltf_stdtestallocator.h:503
const void * const_pointer
Definition bsltf_stdtestallocator.h:404
bsls::Types::IntPtr difference_type
Definition bsltf_stdtestallocator.h:402
bsls::Types::UintPtr size_type
Definition bsltf_stdtestallocator.h:401
void value_type
Definition bsltf_stdtestallocator.h:405
void * pointer
Definition bsltf_stdtestallocator.h:403
Definition bsltf_stdtestallocator.h:275
const TYPE * const_pointer
Definition bsltf_stdtestallocator.h:289
BSLMF_NESTED_TRAIT_DECLARATION(StdTestAllocator, bslma::IsStdAllocator)
pointer address(reference object) const
Return the address providing modifiable access to object.
Definition bsltf_stdtestallocator.h:573
pointer allocate(size_type numElements)
bsls::Types::UintPtr size_type
Definition bsltf_stdtestallocator.h:286
StdTestAllocator()
Create a StdTestAllocator object.
Definition bsltf_stdtestallocator.h:517
bsls::Types::IntPtr difference_type
Definition bsltf_stdtestallocator.h:287
const TYPE & const_reference
Definition bsltf_stdtestallocator.h:291
TYPE * pointer
Definition bsltf_stdtestallocator.h:288
void construct(ELEMENT_TYPE *address, Args &&... arguments)
Definition bsltf_stdtestallocator.h:555
size_type max_size() const
Definition bsltf_stdtestallocator.h:589
TYPE value_type
Definition bsltf_stdtestallocator.h:292
void deallocate(pointer address, size_type numElements=1)
Definition bsltf_stdtestallocator.h:546
TYPE & reference
Definition bsltf_stdtestallocator.h:290
void destroy(ELEMENT_TYPE *address)
Definition bsltf_stdtestallocator.h:565
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_COMPILERFEATURES_FORWARD(T, V)
Definition bsls_compilerfeatures.h:2349
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bsltf_allocargumenttype.h:92
Definition bslma_isstdallocator.h:202
std::size_t UintPtr
Definition bsls_types.h:128
std::ptrdiff_t IntPtr
Definition bsls_types.h:132
static TYPE * addressOf(TYPE &obj)
Definition bsls_util.h:312
Definition bsltf_stdtestallocator.h:208
static void setDelegateAllocatorRaw(bslma::Allocator *basicAllocator)
static bslma::Allocator * delegateAllocator()
Definition bsltf_stdtestallocator.h:303
StdTestAllocator< BDE_OTHER_TYPE > other
Definition bsltf_stdtestallocator.h:305
StdTestAllocator< BDE_OTHER_TYPE > other
Definition bsltf_stdtestallocator.h:418
Definition bsltf_stdtestallocator.h:469
static size_t maxSize(size_t elementSize)