BDE 4.14.0 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 template <class ALLOCATOR, int IS_BSLMA_ALLOC>
84 struct AllocatorUtil {
85
86 /// Return the appropriate allocator for use when copy-constructing
87 /// a container. `rhsAlloc` is intended to be the allocator from
88 /// the container being copied. If `isBslmaAlloc` is of type
89 /// `bsl::true_type` then ignore `rhsAlloc` and return the
90 /// default allocator. Otherwise, return `rhsAlloc` unchanged.
91 static ALLOCATOR copyContainerAllocator(const ALLOCATOR& rhsAlloc);
92 };
93
94 /// Specialization for non-`bslma` allocators.
95 template <class ALLOCATOR>
96 struct AllocatorUtil<ALLOCATOR, 0> {
97
98 /// Return the appropriate allocator for use when copy-constructing
99 /// a container. `rhsAlloc` is intended to be the allocator from
100 /// the container being copied. If `isBslmaAlloc` is of type
101 /// `bsl::true_type` then ignore `rhsAlloc` and return the
102 /// default allocator. Otherwise, return `rhsAlloc` unchanged.
103 static ALLOCATOR copyContainerAllocator(const ALLOCATOR& rhsAlloc);
104 };
105
106 public:
107 // CLASS METHODS
108
109 /// Return the appropriate allocator for use when copy-constructing a
110 /// container. `rhsAlloc` is intended to be the allocator from the
111 /// container being copied. If the `ALLOCATOR` type uses `bslma`
112 /// allocator semantics, then ignore `rhsAlloc` and return the default
113 /// allocator. Otherwise, return `rhsAlloc` unchanged.
114 template <class ALLOCATOR>
115 static ALLOCATOR copyContainerAllocator(const ALLOCATOR& rhsAlloc);
116
117 /// Swap the contents of containers `c1` and `c2`, correctly handling
118 /// the case where the two containers have different allocator values.
119 /// The `quickswap` argument is a binary function object that is called
120 /// to quickly swap two `CONTAINER` objects with matching allocators.
121 /// (The `quickswap` object does not need to check that the allocator
122 /// matched -- it is guaranteed by the caller.) Strong exception
123 /// guarantee: if the allocator or copy constructor of either container
124 /// throws an exception, then the values of `c1` and `c2` are left
125 /// unchanged. The `quickswap` function must not throw an exception.
126 template <class CONTAINER, class QUICKSWAP_FUNC>
127 static void swapContainers(CONTAINER& c1,
128 CONTAINER& c2,
129 const QUICKSWAP_FUNC& quickswap);
130};
131
132} // close package namespace
133
134// ============================================================================
135// INLINE FUNCTION DEFINITIONS
136// ============================================================================
137
138 //------------------------------------------------
139 // struct AllocatorUtil<ALLOCATOR, IS_BSLMA_ALLOC>
140 //------------------------------------------------
141
142// PRIVATE CLASS METHODS
143template <class ALLOCATOR, int IS_BSLMA_ALLOC>
144inline
146copyContainerAllocator(const ALLOCATOR&)
147{
148 return ALLOCATOR();
149}
150
151 //-----------------------------------
152 // struct AllocatorUtil<ALLOCATOR, 0>
153 //-----------------------------------
154
155// PRIVATE CLASS METHODS
156template <class ALLOCATOR>
157inline
159copyContainerAllocator(const ALLOCATOR& rhsAlloc)
160{
161 return rhsAlloc;
162}
163
164namespace bslstp {
165
166 //-----------
167 // class Util
168 //-----------
169
170// CLASS METHODS
171template <class ALLOCATOR>
172inline
173ALLOCATOR Util::copyContainerAllocator(const ALLOCATOR& rhsAlloc)
174{
175 typedef typename
177
179 rhsAlloc);
180}
181
182template <class CONTAINER, class QUICKSWAP_FUNC>
183void Util::swapContainers(CONTAINER& c1,
184 CONTAINER& c2,
185 const QUICKSWAP_FUNC& quickswap)
186{
187 typedef typename CONTAINER::allocator_type allocator_type;
188 allocator_type alloc1 = c1.get_allocator();
189 allocator_type alloc2 = c2.get_allocator();
190
191 if (alloc1 == alloc2) {
192 quickswap(c1, c2);
193 }
194 else {
195 // Create copies of c1 and c2 using each-other's allocators Exception
196 // leaves originals untouched.
197
198 CONTAINER c1copy(c1, alloc2);
199 CONTAINER c2copy(c2, alloc1);
200
201 // Now use bit-wise swap (no exceptions thrown).
202
203 quickswap(c1, c2copy);
204 quickswap(c2, c1copy);
205 }
206}
207
208} // close package namespace
209
210#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
211// ============================================================================
212// BACKWARD COMPATIBILITY
213// ============================================================================
214
215/// This alias is defined for backward compatibility.
217#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
218
219
220
221#endif
222
223// ----------------------------------------------------------------------------
224// Copyright 2013 Bloomberg Finance L.P.
225//
226// Licensed under the Apache License, Version 2.0 (the "License");
227// you may not use this file except in compliance with the License.
228// You may obtain a copy of the License at
229//
230// http://www.apache.org/licenses/LICENSE-2.0
231//
232// Unless required by applicable law or agreed to in writing, software
233// distributed under the License is distributed on an "AS IS" BASIS,
234// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
235// See the License for the specific language governing permissions and
236// limitations under the License.
237// ----------------------------- END-OF-FILE ----------------------------------
238
239/** @} */
240/** @} */
241/** @} */
Definition bslstp_util.h:78
static ALLOCATOR copyContainerAllocator(const ALLOCATOR &rhsAlloc)
Definition bslstp_util.h:173
static void swapContainers(CONTAINER &c1, CONTAINER &c2, const QUICKSWAP_FUNC &quickswap)
Definition bslstp_util.h:183
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
bslstp::Util bslstp_Util
This alias is defined for backward compatibility.
Definition bslstp_util.h:216
static ALLOCATOR copyContainerAllocator(const ALLOCATOR &rhsAlloc)
Definition bslstp_util.h:146
Definition bslstp_exfunctional.h:323
Definition bslmf_isconvertible.h:867