BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstp_util.h
Go to the documentation of this file.
1/// @file bslstp_util.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstp_util.h -*-C++-*-
8#ifndef INCLUDED_BSLSTP_UTIL
9#define INCLUDED_BSLSTP_UTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstp_util bslstp_util
15/// @brief Provide a namespace for utility functions for STL functionality.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstp
19/// @{
20/// @addtogroup bslstp_util
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstp_util-purpose"> Purpose</a>
25/// * <a href="#bslstp_util-classes"> Classes </a>
26/// * <a href="#bslstp_util-description"> Description </a>
27/// * <a href="#bslstp_util-usage"> Usage </a>
28///
29/// # Purpose {#bslstp_util-purpose}
30/// Provide a namespace for utility functions for STL functionality.
31///
32/// @deprecated Do not use directly.
33///
34/// # Classes {#bslstp_util-classes}
35///
36/// - bslstp::Util: namespace for utility functions
37///
38/// # Description {#bslstp_util-description}
39/// This component defines a class, `bslstp::Util`, that provides a
40/// namespace for utility functions used to implement STL functionality in the
41/// `bslstp` package.
42///
43/// ## Usage {#bslstp_util-usage}
44///
45///
46/// This component is for internal use only.
47/// @}
48/** @} */
49/** @} */
50
51/** @addtogroup bsl
52 * @{
53 */
54/** @addtogroup bslstp
55 * @{
56 */
57/** @addtogroup bslstp_util
58 * @{
59 */
60
61#include <bslscm_version.h>
62
63#include <bslmf_isconvertible.h>
64
65#include <bslma_allocator.h>
66
67
68
69namespace bslstp {
70
71 // ==========
72 // class Util
73 // ==========
74
75/// Namespace for utility functions used to implement STL functionality.
76///
77/// See @ref bslstp_util
78class Util {
79
80 // PRIVATE TYPES
81
82 /// Simplify template specializations and overloading.
83 ///
84 /// See @ref bslstp_util
85 template <class ALLOCATOR, int IS_BSLMA_ALLOC>
86 struct AllocatorUtil {
87
88 /// Return the appropriate allocator for use when copy-constructing
89 /// a container. `rhsAlloc` is intended to be the allocator from
90 /// the container being copied. If `isBslmaAlloc` is of type
91 /// `bsl::true_type` then ignore `rhsAlloc` and return the
92 /// default allocator. Otherwise, return `rhsAlloc` unchanged.
93 static ALLOCATOR copyContainerAllocator(const ALLOCATOR& rhsAlloc);
94 };
95
96 /// Specialization for non-`bslma` allocators.
97 template <class ALLOCATOR>
98 struct AllocatorUtil<ALLOCATOR, 0> {
99
100 /// Return the appropriate allocator for use when copy-constructing
101 /// a container. `rhsAlloc` is intended to be the allocator from
102 /// the container being copied. If `isBslmaAlloc` is of type
103 /// `bsl::true_type` then ignore `rhsAlloc` and return the
104 /// default allocator. Otherwise, return `rhsAlloc` unchanged.
105 static ALLOCATOR copyContainerAllocator(const ALLOCATOR& rhsAlloc);
106 };
107
108 public:
109 // CLASS METHODS
110
111 /// Return the appropriate allocator for use when copy-constructing a
112 /// container. `rhsAlloc` is intended to be the allocator from the
113 /// container being copied. If the `ALLOCATOR` type uses `bslma`
114 /// allocator semantics, then ignore `rhsAlloc` and return the default
115 /// allocator. Otherwise, return `rhsAlloc` unchanged.
116 template <class ALLOCATOR>
117 static ALLOCATOR copyContainerAllocator(const ALLOCATOR& rhsAlloc);
118
119 /// Swap the contents of containers `c1` and `c2`, correctly handling
120 /// the case where the two containers have different allocator values.
121 /// The `quickswap` argument is a binary function object that is called
122 /// to quickly swap two `CONTAINER` objects with matching allocators.
123 /// (The `quickswap` object does not need to check that the allocator
124 /// matched -- it is guaranteed by the caller.) Strong exception
125 /// guarantee: if the allocator or copy constructor of either container
126 /// throws an exception, then the values of `c1` and `c2` are left
127 /// unchanged. The `quickswap` function must not throw an exception.
128 template <class CONTAINER, class QUICKSWAP_FUNC>
129 static void swapContainers(CONTAINER& c1,
130 CONTAINER& c2,
131 const QUICKSWAP_FUNC& quickswap);
132};
133
134} // close package namespace
135
136// ============================================================================
137// INLINE FUNCTION DEFINITIONS
138// ============================================================================
139
140 //------------------------------------------------
141 // struct AllocatorUtil<ALLOCATOR, IS_BSLMA_ALLOC>
142 //------------------------------------------------
143
144// PRIVATE CLASS METHODS
145template <class ALLOCATOR, int IS_BSLMA_ALLOC>
146inline
148copyContainerAllocator(const ALLOCATOR&)
149{
150 return ALLOCATOR();
151}
152
153 //-----------------------------------
154 // struct AllocatorUtil<ALLOCATOR, 0>
155 //-----------------------------------
156
157// PRIVATE CLASS METHODS
158template <class ALLOCATOR>
159inline
161copyContainerAllocator(const ALLOCATOR& rhsAlloc)
162{
163 return rhsAlloc;
164}
165
166namespace bslstp {
167
168 //-----------
169 // class Util
170 //-----------
171
172// CLASS METHODS
173template <class ALLOCATOR>
174inline
175ALLOCATOR Util::copyContainerAllocator(const ALLOCATOR& rhsAlloc)
176{
177 typedef typename
179
181 rhsAlloc);
182}
183
184template <class CONTAINER, class QUICKSWAP_FUNC>
185void Util::swapContainers(CONTAINER& c1,
186 CONTAINER& c2,
187 const QUICKSWAP_FUNC& quickswap)
188{
189 typedef typename CONTAINER::allocator_type allocator_type;
190 allocator_type alloc1 = c1.get_allocator();
191 allocator_type alloc2 = c2.get_allocator();
192
193 if (alloc1 == alloc2) {
194 quickswap(c1, c2);
195 }
196 else {
197 // Create copies of c1 and c2 using each-other's allocators Exception
198 // leaves originals untouched.
199
200 CONTAINER c1copy(c1, alloc2);
201 CONTAINER c2copy(c2, alloc1);
202
203 // Now use bit-wise swap (no exceptions thrown).
204
205 quickswap(c1, c2copy);
206 quickswap(c2, c1copy);
207 }
208}
209
210} // close package namespace
211
212#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
213// ============================================================================
214// BACKWARD COMPATIBILITY
215// ============================================================================
216
217/// This alias is defined for backward compatibility.
219#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
220
221
222
223#endif
224
225// ----------------------------------------------------------------------------
226// Copyright 2013 Bloomberg Finance L.P.
227//
228// Licensed under the Apache License, Version 2.0 (the "License");
229// you may not use this file except in compliance with the License.
230// You may obtain a copy of the License at
231//
232// http://www.apache.org/licenses/LICENSE-2.0
233//
234// Unless required by applicable law or agreed to in writing, software
235// distributed under the License is distributed on an "AS IS" BASIS,
236// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
237// See the License for the specific language governing permissions and
238// limitations under the License.
239// ----------------------------- END-OF-FILE ----------------------------------
240
241/** @} */
242/** @} */
243/** @} */
Definition bslstp_util.h:78
static ALLOCATOR copyContainerAllocator(const ALLOCATOR &rhsAlloc)
Definition bslstp_util.h:175
static void swapContainers(CONTAINER &c1, CONTAINER &c2, const QUICKSWAP_FUNC &quickswap)
Definition bslstp_util.h:185
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
bslstp::Util bslstp_Util
This alias is defined for backward compatibility.
Definition bslstp_util.h:218
static ALLOCATOR copyContainerAllocator(const ALLOCATOR &rhsAlloc)
Definition bslstp_util.h:148
Definition bslstp_exfunctional.h:325
Definition bslmf_isconvertible.h:875