BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsltf_stdstatefulallocator.h
Go to the documentation of this file.
1/// @file bsltf_stdstatefulallocator.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bsltf_stdstatefulallocator.h -*-C++-*-
8#ifndef INCLUDED_BSLTF_STDSTATEFULALLOCATOR
9#define INCLUDED_BSLTF_STDSTATEFULALLOCATOR
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bsltf_stdstatefulallocator bsltf_stdstatefulallocator
15/// @brief Provide a minimal standard compliant allocator.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bsltf
19/// @{
20/// @addtogroup bsltf_stdstatefulallocator
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bsltf_stdstatefulallocator-purpose"> Purpose</a>
25/// * <a href="#bsltf_stdstatefulallocator-classes"> Classes </a>
26/// * <a href="#bsltf_stdstatefulallocator-description"> Description </a>
27/// * <a href="#bsltf_stdstatefulallocator-usage"> Usage </a>
28/// * <a href="#bsltf_stdstatefulallocator-example-1-testing-the-support-for-stl-compliant-allocator"> Example 1: Testing The Support for STL-Compliant Allocator </a>
29///
30/// # Purpose {#bsltf_stdstatefulallocator-purpose}
31/// Provide a minimal standard compliant allocator.
32///
33/// # Classes {#bsltf_stdstatefulallocator-classes}
34///
35/// - bsltf::StdStatefulAllocator: standard compliant allocator managing state
36///
37/// @see bsltf_stdtestallocator
38///
39/// # Description {#bsltf_stdstatefulallocator-description}
40/// This component provides an allocator,
41/// `bsltf::StdStatefulAllocator`, that defines the minimal interface to comply
42/// with section 17.6.3.5 ([allocator.requirements]) of the C++11 standard,
43/// while still providing an externally visible and potentially distinct state
44/// for each allocator object. This type can be used to verify that constructs
45/// designed to support a standard-compliant allocator access the allocator only
46/// through the standard-defined interface.
47///
48/// `StdStatefulAllocator` delegates its operations to the allocator passed at
49/// construction (or the default allocator if no allocator is passed) that is
50/// also the sole attribute of this class. In most tests, a
51/// `bslma::TestAllocator` will be passed.
52///
53/// The key differences between this test allocator and a regular BDE allocator
54/// are:
55///
56/// * This allocator does not support the `scoped` allocation model, so that
57/// elements in a container will often have a different allocator to the
58/// container object itself.
59/// * This allocator may propagate through copy operations, move operations
60/// and `swap` operations, depending on how the template is configured as
61/// it is instantiated.
62///
63/// ## Usage {#bsltf_stdstatefulallocator-usage}
64///
65///
66/// This section illustrates intended use of this component.
67///
68/// ### Example 1: Testing The Support for STL-Compliant Allocator {#bsltf_stdstatefulallocator-example-1-testing-the-support-for-stl-compliant-allocator}
69///
70///
71/// In this example we will verify that a type supports the use of a
72/// STL-compliant allocator.
73///
74/// First, we define a simple container type intended to be used with a C++11
75/// standard compliant allocator:
76/// @code
77/// template <class TYPE, class ALLOCATOR>
78/// class MyContainer {
79/// // This container type is parameterized on a standard allocator type
80/// // and contains a single object, always initialized, which can be
81/// // replaced and accessed.
82///
83/// // DATA MEMBERS
84/// ALLOCATOR d_allocator; // allocator used to supply memory (held, not
85/// // owned)
86///
87/// TYPE *d_object_p; // pointer to the contained object
88///
89/// public:
90/// // CREATORS
91/// MyContainer(const TYPE& object, const ALLOCATOR& allocator);
92/// // Create an container containing the specified 'object', using the
93/// // specified 'allocator' to supply memory.
94///
95/// ~MyContainer();
96/// // Destroy this container.
97///
98/// // MANIPULATORS
99/// TYPE& object();
100/// // Return a reference providing modifiable access to the object
101/// // contained in this container.
102///
103/// // ACCESSORS
104/// const TYPE& object() const;
105/// // Return a reference providing non-modifiable access to the object
106/// // contained in this container.
107/// };
108/// @endcode
109/// Then, we define the member functions of `MyContainer`:
110/// @code
111/// // CREATORS
112/// template <class TYPE, class ALLOCATOR>
113/// MyContainer<TYPE, ALLOCATOR>::MyContainer(const TYPE& object,
114/// const ALLOCATOR& allocator)
115/// : d_allocator(allocator)
116/// {
117/// d_object_p = d_allocator.allocate(1);
118/// new (static_cast<void *>(d_object_p)) TYPE(object);
119/// }
120///
121/// template <class TYPE, class ALLOCATOR>
122/// MyContainer<TYPE, ALLOCATOR>::~MyContainer()
123/// {
124/// d_object_p->~TYPE();
125/// d_allocator.deallocate(d_object_p, 1);
126/// }
127///
128/// // MANIPULATORS
129/// template <class TYPE, class ALLOCATOR>
130/// TYPE& MyContainer<TYPE, ALLOCATOR>::object()
131/// {
132/// return *d_object_p;
133/// }
134///
135/// // ACCESSORS
136/// template <class TYPE, class ALLOCATOR>
137/// const TYPE& MyContainer<TYPE, ALLOCATOR>::object() const
138/// {
139/// return *d_object_p;
140/// }
141/// @endcode
142/// Now, we use `bsltf::StdStatefulAllocator` to implement a simple test for
143/// `MyContainer` to verify it correctly uses a parameterized allocator using
144/// only the C++11 standard methods:
145/// @code
146/// bslma::TestAllocator oa("object", veryVeryVeryVerbose);
147/// {
148/// typedef MyContainer<int, bsltf::StdStatefulAllocator<int> > Obj;
149///
150/// Obj mX(2, bsltf::StdStatefulAllocator<int>(&oa));
151/// const Obj& X = mX;
152/// assert(sizeof(int) == oa.numBytesInUse());
153///
154/// assert(X.object() == 2);
155///
156/// mX.object() = -10;
157/// assert(X.object() == -10);
158/// }
159///
160/// assert(0 == oa.numBytesInUse());
161/// @endcode
162/// @}
163/** @} */
164/** @} */
165
166/** @addtogroup bsl
167 * @{
168 */
169/** @addtogroup bsltf
170 * @{
171 */
172/** @addtogroup bsltf_stdstatefulallocator
173 * @{
174 */
175
176#include <bslscm_version.h>
177
178#include <bslma_allocator.h>
179#include <bslma_default.h>
180#include <bslma_isstdallocator.h>
181
182#include <bslmf_assert.h>
184#include <bslmf_issame.h>
185#include <bslmf_util.h> // 'forward(V)'
186
189#include <bsls_util.h> // 'forward<T>(V)'
190
191#include <new>
192
193#if BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
194// Include version that can be compiled with C++03
195// Generated on Thu Oct 21 10:11:37 2021
196// Command line: sim_cpp11_features.pl bsltf_stdstatefulallocator.h
197# define COMPILING_BSLTF_STDSTATEFULALLOCATOR_H
199# undef COMPILING_BSLTF_STDSTATEFULALLOCATOR_H
200#else
201
202
203namespace bsltf {
204
205 // ==========================
206 // class StdStatefulAllocator
207 // ==========================
208
209/// This allocator implements the minimal interface to comply with section
210/// 17.6.3.5 ([allocator.requirements]) of the C++11 standard, while
211/// maintaining a distinct object state - in this case a wrapped pointer to
212/// a `bslma::Allocator`. The template is configurable to control its
213/// allocator propagation properties, but does not support the BDE "scoped"
214/// allocator model, as scoped allocators should never propagate. Instances
215/// of this allocator delegate their operations to the wrapped allocator
216/// that constitutes its state. Note that while we define the various
217/// traits used by the C++11 allocator traits facility, they actually mean
218/// very little for this component, as it is the consumer of the allocator's
219/// responsibility to check and apply the traits correctly, typically by
220/// using `bsl::allocator_traits` to perform all memory allocation tasks
221/// rather than using the allocator directly. The
222/// `PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION` flag is consumed directly
223/// though, in the static member function
224/// `select_on_container_copy_construction`.
225///
226/// See @ref bsltf_stdstatefulallocator
227template <class TYPE,
228 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION = true,
229 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT = true,
230 bool PROPAGATE_ON_CONTAINER_SWAP = true,
231 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT = true,
232 bool IS_ALWAYS_EQUAL = false>
234
235 private:
236 // DATA
237 bslma::Allocator *d_allocator_p; // the wrapped allocator (held, not
238 // owned)
239
240 public:
241 // TRAITS
244
245 // PUBLIC TYPES
246 typedef TYPE value_type;
247
248 // For a minimal allocator, these should all be deducible for a C++11
249 // container implementation. Unfortunately, the C++03 implementation of
250 // 'allocator_traits' supported by BDE does not try the leaps of template
251 // metaprogramming necessary to deduce these types. That is left for a
252 // future C++11 implementation, where language makes such metaprograms
253 // much simpler to write.
254
255#if !defined(BSLSTL_ALLOCATOR_TRAITS_SUPPORTS_ALL_CPP11_DEDUCTIONS)
256 typedef std::size_t size_type;
257 typedef std::ptrdiff_t difference_type;
258 typedef TYPE *pointer;
259 typedef const TYPE *const_pointer;
260#endif
261
262 typedef bsl::integral_constant<bool,
263 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT>
265
268
269 typedef bsl::integral_constant<bool,
270 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT>
272
273 typedef bsl::integral_constant<bool,
274 IS_ALWAYS_EQUAL>
276
277 /// This nested `struct` template, parameterized by some
278 /// `BDE_OTHER_TYPE`, provides a namespace for an `other` type alias,
279 /// which is an allocator type following the same template as this one
280 /// but that allocates elements of `BDE_OTHER_TYPE`. Note that this
281 /// allocator type is convertible to and from `other` for any
282 /// `BDE_OTHER_TYPE` including `void`.
283 template <class BDE_OTHER_TYPE>
284 struct rebind
285 {
286
287 typedef StdStatefulAllocator<
288 BDE_OTHER_TYPE,
289 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
290 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
291 PROPAGATE_ON_CONTAINER_SWAP,
292 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
293 IS_ALWAYS_EQUAL> other;
294 };
295
296 // CREATORS
297
298 /// Create a `StdStatefulAllocator` object wrapping the specified
299 /// `allocator`.
301
302 StdStatefulAllocator(const StdStatefulAllocator& original) = default;
303 // Create an allocator having the same value as the specified
304 // 'original' object.
305
306 /// Create a `StdStatefulAllocator` object wrapping the same test
307 /// allocator as the specified `original`.
308 template <class BDE_OTHER_TYPE>
310 BDE_OTHER_TYPE,
311 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
312 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
313 PROPAGATE_ON_CONTAINER_SWAP,
314 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
315 IS_ALWAYS_EQUAL>& original);
316
318 // Destroy this object.
319
320 // MANIPULATORS
322 operator=(const StdStatefulAllocator& rhs) = default;
323 // Assign to this object the value of the specified 'rhs' object, and
324 // return a reference providing modifiable access to this object.
325
326 /// Allocate enough (properly aligned) space for the specified
327 /// `numElements` of the (template parameter) type `TYPE`. If the
328 /// underlying `bslma::Allocator` is unable to fulfill the allocation
329 /// request, an exception (typically `bsl::bad_alloc`) will be thrown.
330 TYPE *allocate(bslma::Allocator::size_type numElements);
331
332#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES // $var-args=14
333
334 /// Construct an object of the (template parameter) `ELEMENT_TYPE`, by
335 /// forwarding the specified (variable number of) `arguments` to the
336 /// corresponding constructor of `ELEMENT_TYPE`, at the specified
337 /// uninitialized memory `address`. The behavior is undefined unless
338 /// `address` is properly aligned for objects of `ELEMENT_TYPE`.
339 template <class ELEMENT_TYPE, class... Args>
340 void construct(ELEMENT_TYPE *address, Args&&... arguments);
341#endif
342
343 /// Return memory previously allocated at the specified `address` for
344 /// `numElements` back to this allocator. The `numElements` argument is
345 /// ignored by this allocator type. The behavior is undefined unless
346 /// `address` was allocated using this allocator object and has not
347 /// already been deallocated.
348 void deallocate(TYPE *address, bslma::Allocator::size_type numElements);
349
350 /// Invoke the `ELEMENT_TYPE` destructor for the object at the specified
351 /// `address`.
352 template <class ELEMENT_TYPE>
353 void destroy(ELEMENT_TYPE *address);
354
355 // ACCESSORS
356#if !defined(BSLSTL_ALLOCATOR_TRAITS_SUPPORTS_ALL_CPP11_DEDUCTIONS)
357 /// Return the maximum number of elements of type `TYPE` that can be
358 /// allocated using this allocator in a single call to the `allocate`
359 /// method. Note that there is no guarantee that attempts at allocating
360 /// less elements than the value returned by @ref max_size will not throw.
361 /// *** DO NOT RELY ON THE CONTINUING PRESENT OF THIS METHOD *** THIS
362 /// METHOD WILL BE REMOVED ONCE `bslstl::allocator_traits` PROPERLY
363 /// DEDUCES AN IMPLEMENTATION FOR THIS FUNCTION WHEN NOT SUPPLIED BY THE
364 /// ALLOCATOR DIRECTLY.
365 size_type max_size() const;
366#endif
367
368 /// Return a copy of this object if the `bool` template parameter
369 /// `PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION` is true, and a copy of a
370 /// `StdStatefulAllocator` object wrapping the default allocator
371 /// otherwise.
373
374 /// Return the address of the allocator wrapped by this object.
376};
377
378// FREE OPERATORS
379
380/// Return `true` if the specified `lhs` and `rhs` have the same underlying
381/// test allocator, and `false` otherwise.
382template <class TYPE1,
383 class TYPE2,
384 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
385 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
386 bool PROPAGATE_ON_CONTAINER_SWAP,
387 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
388 bool IS_ALWAYS_EQUAL>
389bool operator==(const StdStatefulAllocator<
390 TYPE1,
391 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
392 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
393 PROPAGATE_ON_CONTAINER_SWAP,
394 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
395 IS_ALWAYS_EQUAL>& lhs,
397 TYPE2,
398 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
399 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
400 PROPAGATE_ON_CONTAINER_SWAP,
401 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
402 IS_ALWAYS_EQUAL>& rhs);
403
404/// Return `true` if the specified `lhs` and `rhs` have different underlying
405/// test allocators, and `false` otherwise.
406template <class TYPE1,
407 class TYPE2,
408 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
409 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
410 bool PROPAGATE_ON_CONTAINER_SWAP,
411 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
412 bool IS_ALWAYS_EQUAL>
413bool operator!=(const StdStatefulAllocator<
414 TYPE1,
415 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
416 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
417 PROPAGATE_ON_CONTAINER_SWAP,
418 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
419 IS_ALWAYS_EQUAL>& lhs,
421 TYPE2,
422 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
423 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
424 PROPAGATE_ON_CONTAINER_SWAP,
425 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
426 IS_ALWAYS_EQUAL>& rhs);
427
428
429// ============================================================================
430// INLINE DEFINITIONS
431// ============================================================================
432
433 // --------------------------
434 // class StdStatefulAllocator
435 // --------------------------
436
437// CREATORS
438template <class TYPE,
439 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
440 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
441 bool PROPAGATE_ON_CONTAINER_SWAP,
442 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
443 bool IS_ALWAYS_EQUAL>
444inline
446 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
447 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
448 PROPAGATE_ON_CONTAINER_SWAP,
449 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
450 IS_ALWAYS_EQUAL>::
451StdStatefulAllocator(bslma::Allocator *allocator)
452: d_allocator_p(bslma::Default::allocator(allocator))
453{
454}
455
456template <class TYPE,
457 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
458 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
459 bool PROPAGATE_ON_CONTAINER_SWAP,
460 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
461 bool IS_ALWAYS_EQUAL>
462template <class BDE_OTHER_TYPE>
463inline
465 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
466 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
467 PROPAGATE_ON_CONTAINER_SWAP,
468 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
469 IS_ALWAYS_EQUAL>::
470StdStatefulAllocator(const StdStatefulAllocator<
471 BDE_OTHER_TYPE,
472 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
473 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
474 PROPAGATE_ON_CONTAINER_SWAP,
475 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
476 IS_ALWAYS_EQUAL>& original)
477: d_allocator_p(original.allocator())
478{
479}
480
481// MANIPULATORS
482template <class TYPE,
483 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
484 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
485 bool PROPAGATE_ON_CONTAINER_SWAP,
486 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
487 bool IS_ALWAYS_EQUAL>
488inline
489TYPE *
491 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
492 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
493 PROPAGATE_ON_CONTAINER_SWAP,
494 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
495 IS_ALWAYS_EQUAL>::allocate(
496 bslma::Allocator::size_type numElements)
497{
498 if (numElements > this->max_size()) {
499 BloombergLP::bsls::BslExceptionUtil::throwBadAlloc();
500 }
501
502 return static_cast<TYPE *>(d_allocator_p->allocate(
503 bslma::Allocator::size_type(numElements * sizeof(TYPE))));
504}
505
506#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES // $var-args=14
507template <class TYPE,
508 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
509 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
510 bool PROPAGATE_ON_CONTAINER_SWAP,
511 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
512 bool IS_ALWAYS_EQUAL>
513template <class ELEMENT_TYPE, class... Args>
514inline
515void
517 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
518 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
519 PROPAGATE_ON_CONTAINER_SWAP,
520 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
521 IS_ALWAYS_EQUAL>::construct(
522 ELEMENT_TYPE *address, Args&&... arguments)
523{
524 ::new (static_cast<void*>(address)) ELEMENT_TYPE(
525 BSLS_COMPILERFEATURES_FORWARD(Args,arguments)...);
526}
527#endif
528
529template <class TYPE,
530 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
531 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
532 bool PROPAGATE_ON_CONTAINER_SWAP,
533 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
534 bool IS_ALWAYS_EQUAL>
535inline
536void StdStatefulAllocator<TYPE,
537 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
538 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
539 PROPAGATE_ON_CONTAINER_SWAP,
540 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
541 IS_ALWAYS_EQUAL>::
542deallocate(TYPE *address, bslma::Allocator::size_type)
543{
544 d_allocator_p->deallocate(address);
545}
546
547template <class TYPE,
548 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
549 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
550 bool PROPAGATE_ON_CONTAINER_SWAP,
551 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
552 bool IS_ALWAYS_EQUAL>
553template <class ELEMENT_TYPE>
554inline
555void StdStatefulAllocator<TYPE,
556 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
557 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
558 PROPAGATE_ON_CONTAINER_SWAP,
559 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
560 IS_ALWAYS_EQUAL>::destroy(
561 ELEMENT_TYPE * address)
562{
563 address->~ELEMENT_TYPE();
564}
565
566template <class TYPE,
567 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
568 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
569 bool PROPAGATE_ON_CONTAINER_SWAP,
570 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
571 bool IS_ALWAYS_EQUAL>
572inline
573typename StdStatefulAllocator<
574 TYPE,
575 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
576 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
577 PROPAGATE_ON_CONTAINER_SWAP,
578 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
579 IS_ALWAYS_EQUAL>::size_type
581 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
582 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
583 PROPAGATE_ON_CONTAINER_SWAP,
584 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
585 IS_ALWAYS_EQUAL>::
586max_size() const
587{
588 // Return the largest value, 'v', such that 'v * sizeof(T)' fits in a
589 // 'size_type' (copied from bslstl_allocator).
590
591 // We will calculate MAX_NUM_BYTES based on our knowledge that
592 // 'bslma::Allocator::size_type' is just an alias for 'std::size_t'. First
593 // demonstrate that is true:
594
595 BSLMF_ASSERT((bsl::is_same<BloombergLP::bslma::Allocator::size_type,
596 std::size_t>::value));
597
598 static const std::size_t MAX_NUM_BYTES = ~std::size_t(0);
599 static const std::size_t MAX_NUM_ELEMENTS =
600 std::size_t(MAX_NUM_BYTES) / sizeof(TYPE);
601 return MAX_NUM_ELEMENTS;
602}
603
604template <class TYPE,
605 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
606 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
607 bool PROPAGATE_ON_CONTAINER_SWAP,
608 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
609 bool IS_ALWAYS_EQUAL>
610inline
612 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
613 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
614 PROPAGATE_ON_CONTAINER_SWAP,
615 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
616 IS_ALWAYS_EQUAL>
618 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
619 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
620 PROPAGATE_ON_CONTAINER_SWAP,
621 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
622 IS_ALWAYS_EQUAL>::
623select_on_container_copy_construction() const
624{
625 if (PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION) {
626 return *this; // RETURN
627 }
628
629 // else
630
632}
633
634template <class TYPE,
635 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
636 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
637 bool PROPAGATE_ON_CONTAINER_SWAP,
638 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
639 bool IS_ALWAYS_EQUAL>
640inline
643 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
644 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
645 PROPAGATE_ON_CONTAINER_SWAP,
646 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
647 IS_ALWAYS_EQUAL>::
648allocator() const
649{
650 return d_allocator_p;
651}
652
653} // close package namespace
654
655// FREE OPERATORS
656template <class TYPE1,
657 class TYPE2,
658 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
659 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
660 bool PROPAGATE_ON_CONTAINER_SWAP,
661 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
662 bool IS_ALWAYS_EQUAL>
663inline
664bool bsltf::operator==(const StdStatefulAllocator<
665 TYPE1,
666 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
667 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
668 PROPAGATE_ON_CONTAINER_SWAP,
669 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
670 IS_ALWAYS_EQUAL>& lhs,
671 const StdStatefulAllocator<
672 TYPE2,
673 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
674 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
675 PROPAGATE_ON_CONTAINER_SWAP,
676 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
677 IS_ALWAYS_EQUAL>& rhs)
678{
679 return IS_ALWAYS_EQUAL || (lhs.allocator() == rhs.allocator());
680}
681
682template <class TYPE1,
683 class TYPE2,
684 bool PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
685 bool PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
686 bool PROPAGATE_ON_CONTAINER_SWAP,
687 bool PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
688 bool IS_ALWAYS_EQUAL>
689inline
690bool bsltf::operator!=(const StdStatefulAllocator<
691 TYPE1,
692 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
693 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
694 PROPAGATE_ON_CONTAINER_SWAP,
695 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
696 IS_ALWAYS_EQUAL>& lhs,
697 const StdStatefulAllocator<
698 TYPE2,
699 PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION,
700 PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT,
701 PROPAGATE_ON_CONTAINER_SWAP,
702 PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT,
703 IS_ALWAYS_EQUAL>& rhs)
704{
705 return !IS_ALWAYS_EQUAL && (lhs.allocator() != rhs.allocator());
706}
707
708
709
710#endif // End C++11 code
711
712#endif
713
714// ----------------------------------------------------------------------------
715// Copyright 2013 Bloomberg Finance L.P.
716//
717// Licensed under the Apache License, Version 2.0 (the "License");
718// you may not use this file except in compliance with the License.
719// You may obtain a copy of the License at
720//
721// http://www.apache.org/licenses/LICENSE-2.0
722//
723// Unless required by applicable law or agreed to in writing, software
724// distributed under the License is distributed on an "AS IS" BASIS,
725// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
726// See the License for the specific language governing permissions and
727// limitations under the License.
728// ----------------------------- END-OF-FILE ----------------------------------
729
730/** @} */
731/** @} */
732/** @} */
Definition bslma_allocator.h:457
std::size_t size_type
Definition bslma_allocator.h:499
Definition bsltf_stdstatefulallocator.h:233
std::ptrdiff_t difference_type
Definition bsltf_stdstatefulallocator.h:257
bslma::Allocator * allocator() const
Return the address of the allocator wrapped by this object.
Definition bsltf_stdstatefulallocator.h:648
bsl::integral_constant< bool, PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT > propagate_on_container_move_assignment
Definition bsltf_stdstatefulallocator.h:271
StdStatefulAllocator select_on_container_copy_construction() const
Definition bsltf_stdstatefulallocator.h:623
TYPE * allocate(bslma::Allocator::size_type numElements)
Definition bsltf_stdstatefulallocator.h:495
bsl::integral_constant< bool, PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT > propagate_on_container_copy_assignment
Definition bsltf_stdstatefulallocator.h:264
const TYPE * const_pointer
Definition bsltf_stdstatefulallocator.h:259
StdStatefulAllocator(const StdStatefulAllocator &original)=default
std::size_t size_type
Definition bsltf_stdstatefulallocator.h:256
TYPE value_type
Definition bsltf_stdstatefulallocator.h:246
bsl::integral_constant< bool, PROPAGATE_ON_CONTAINER_SWAP > propagate_on_container_swap
Definition bsltf_stdstatefulallocator.h:267
void destroy(ELEMENT_TYPE *address)
Definition bsltf_stdstatefulallocator.h:560
void construct(ELEMENT_TYPE *address, Args &&... arguments)
Definition bsltf_stdstatefulallocator.h:521
BSLMF_NESTED_TRAIT_DECLARATION(StdStatefulAllocator, bslma::IsStdAllocator)
StdStatefulAllocator & operator=(const StdStatefulAllocator &rhs)=default
bsl::integral_constant< bool, IS_ALWAYS_EQUAL > is_always_equal
Definition bsltf_stdstatefulallocator.h:275
void deallocate(TYPE *address, bslma::Allocator::size_type numElements)
Definition bsltf_stdstatefulallocator.h:542
size_type max_size() const
Definition bsltf_stdstatefulallocator.h:586
TYPE * pointer
Definition bsltf_stdstatefulallocator.h:258
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:229
#define BSLS_COMPILERFEATURES_FORWARD(T, V)
Definition bsls_compilerfeatures.h:2018
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balxml_encoderoptions.h:68
Definition bsltf_allocargumenttype.h:92
bool operator!=(const AllocBitwiseMoveableTestType &lhs, const AllocBitwiseMoveableTestType &rhs)
bool operator==(const AllocBitwiseMoveableTestType &lhs, const AllocBitwiseMoveableTestType &rhs)
Definition bdldfp_decimal.h:5188
Definition bslmf_integralconstant.h:244
Definition bslmf_issame.h:146
static Allocator * defaultAllocator()
Definition bslma_default.h:889
Definition bslma_isstdallocator.h:201
Definition bsltf_stdstatefulallocator.h:285
StdStatefulAllocator< BDE_OTHER_TYPE, PROPAGATE_ON_CONTAINER_COPY_CONSTRUCTION, PROPAGATE_ON_CONTAINER_COPY_ASSIGNMENT, PROPAGATE_ON_CONTAINER_SWAP, PROPAGATE_ON_CONTAINER_MOVE_ASSIGNMENT, IS_ALWAYS_EQUAL > other
Definition bsltf_stdstatefulallocator.h:293