BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslalg_swaputil.h
Go to the documentation of this file.
1/// @file bslalg_swaputil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslalg_swaputil.h -*-C++-*-
8#ifndef INCLUDED_BSLALG_SWAPUTIL
9#define INCLUDED_BSLALG_SWAPUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslalg_swaputil bslalg_swaputil
15/// @brief Provide a simple to use `swap` algorithm.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslalg
19/// @{
20/// @addtogroup bslalg_swaputil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslalg_swaputil-purpose"> Purpose</a>
25/// * <a href="#bslalg_swaputil-classes"> Classes </a>
26/// * <a href="#bslalg_swaputil-description"> Description </a>
27/// * <a href="#bslalg_swaputil-usage"> Usage </a>
28/// * <a href="#bslalg_swaputil-example-1-using-bslalg-swaputil-swap"> Example 1: Using bslalg::SwapUtil::swap </a>
29///
30/// # Purpose {#bslalg_swaputil-purpose}
31/// Provide a simple to use `swap` algorithm.
32///
33/// # Classes {#bslalg_swaputil-classes}
34///
35/// - bslalg::SwapUtil: namespace for the `swap` utility function.
36///
37/// @see bsl_algorithm
38///
39/// # Description {#bslalg_swaputil-description}
40/// This component provides a namespace for a utility function that
41/// swaps the value of two objects of the same type. Using this utility is
42/// intended to be a simpler alternative to using the standard `swap` algorithm
43/// directly. The standard `swap` algorithm is provided in the `bsl` namespace
44/// in a generic form and overloaded for specific classes in the namespaces of
45/// those classes. When the `swap` algorithm is used, its specific
46/// implementation is supposed to be found by Argument Dependent Lookup (ADL).
47/// Finding the proper `swap` function with ADL requires bringing the
48/// `bsl::swap` into the current scope with the `using` directive and then
49/// calling a `swap` function without the namespace qualification. The `swap`
50/// utility static function provided by this component relieves the end-user
51/// from a need to remember those details of the proper usage of the `swap`
52/// algorithm.
53///
54/// ## Usage {#bslalg_swaputil-usage}
55///
56///
57/// This section illustrates intended use of this component.
58///
59/// ### Example 1: Using bslalg::SwapUtil::swap {#bslalg_swaputil-example-1-using-bslalg-swaputil-swap}
60///
61///
62/// In this example we define a type `Container` and use `bslalg::SwapUtil` to
63/// both implement a user-defined `swap` for `Container`, and swap two container
64/// objects.
65///
66/// We start by defining a class `Container` in the `xyz` namespace. Further we
67/// assume that `Container` has some expensive-to-copy data, so we provide a
68/// custom `swap` algorithm to efficiently swap the data between a two objects
69/// this class by defining a `swap` method and a `swap` free function.
70/// @code
71/// namespace xyz {
72///
73/// class Container {
74/// private:
75/// int d_expensiveData;
76///
77/// public:
78/// void swap(Container& other);
79/// // Swap the value of 'this' object with the value of the specified
80/// // 'other' object. This method provides the no-throw
81/// // exception-safety guarantee.
82/// };
83///
84/// void swap(Container& a, Container& b);
85/// // Swap the values of the specified 'a' and 'b' objects. This function
86/// // provides the no-throw exception-safety guarantee.
87/// @endcode
88/// Note that the free function `swap` is overloaded in the namespace of the
89/// class `Container`, which is `xyz`.
90///
91/// Next, we implemented the `swap` method using the `bslalg::SwapUtil::swap` to
92/// swap the individual data elements:
93/// @code
94/// inline
95/// void Container::swap(Container& other)
96/// {
97/// bslalg::SwapUtil::swap(&d_expensiveData, &other.d_expensiveData);
98///
99/// // Equivalent to:
100/// // using bsl::swap;
101/// // swap(d_expensiveData, other.d_expensiveData);
102/// }
103/// @endcode
104/// Notice that calling `bslalg::SwapUtil::swap` is equivalent to making the
105/// `bsl::swap` available in the current scope by doing `using bsl::swap` and
106/// making a subsequent call to an unqualified `swap` function.
107///
108/// Then, we implement the `swap` free function:
109/// @code
110/// inline
111/// void swap(Container& a, Container& b)
112/// {
113/// a.swap(b);
114/// }
115///
116/// } // close namespace xyz
117/// @endcode
118/// Finally we can use `bslalg::SwapUtil::swap` to swap two objects of class
119/// `xyz::Container`:
120/// @code
121/// xyz::Container c1, c2;
122///
123/// bslalg::SwapUtil::swap(&c1, &c2);
124/// @endcode
125/// The above code correctly calls the `xyz::swap` overload for the `Container`
126/// class.
127/// @}
128/** @} */
129/** @} */
130
131/** @addtogroup bsl
132 * @{
133 */
134/** @addtogroup bslalg
135 * @{
136 */
137/** @addtogroup bslalg_swaputil
138 * @{
139 */
140
141#include <bslscm_version.h>
142
143#include <bsls_assert.h>
144#include <bsls_platform.h>
145
146#include <algorithm>
147
148
149
150// A workaround for GCC which before version 4.0 had some problems with ADL.
151#if defined(BSLS_PLATFORM_CMP_GNU) && BSLS_PLATFORM_CMP_VER_MAJOR < 40000
152
153class bslalg_SwapUtil_Dummy;
154
155void swap(bslalg_SwapUtil_Dummy);
156 // Introduce 'swap' to the 'BloombergLP' namespace. The dummy argument of
157 // a private type prevents this declaration from interfering with anything
158 // else.
159#endif
160
161namespace bslalg {
162
163 // ===============
164 // struct SwapUtil
165 // ===============
166
167/// This class provides a namespace for the `swap` utility method.
168///
169/// See @ref bslalg_swaputil
170class SwapUtil {
171
172 public:
173 // CLASS METHODS
174
175 /// Exchange the values of the specified `a` and `b` objects using
176 /// either a `swap` free function overloaded for type `T`, in the
177 /// namespace of type `T` if it's available, and the default generic
178 /// `bsl::swap` otherwise.
179 template <class T>
180 static
181 void swap(T *a, T *b);
182};
183
184// ============================================================================
185// INLINE FUNCTION DEFINITIONS
186// ============================================================================
187
188 // ---------------
189 // struct SwapUtil
190 // ---------------
191
192// CLASS METHODS
193template <class T>
194void SwapUtil::swap(T *a, T *b)
195{
196 BSLS_ASSERT_SAFE(a != NULL);
197 BSLS_ASSERT_SAFE(b != NULL);
198
199 using std::swap;
200
201// A workaround for GCC, which had some problems with ADL before version 4.0.
202#if defined(BSLS_PLATFORM_CMP_GNU) && BSLS_PLATFORM_CMP_VER_MAJOR < 40000
203 using BloombergLP::swap;
204#endif
205
206 swap(*a, *b);
207}
208
209} // close package namespace
210
211#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
212// ============================================================================
213// BACKWARD COMPATIBILITY
214// ============================================================================
215
216/// This alias is defined for backward compatibility.
218#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
219
220
221
222#endif
223
224// ----------------------------------------------------------------------------
225// Copyright 2013 Bloomberg Finance L.P.
226//
227// Licensed under the Apache License, Version 2.0 (the "License");
228// you may not use this file except in compliance with the License.
229// You may obtain a copy of the License at
230//
231// http://www.apache.org/licenses/LICENSE-2.0
232//
233// Unless required by applicable law or agreed to in writing, software
234// distributed under the License is distributed on an "AS IS" BASIS,
235// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
236// See the License for the specific language governing permissions and
237// limitations under the License.
238// ----------------------------- END-OF-FILE ----------------------------------
239
240/** @} */
241/** @} */
242/** @} */
Definition bslalg_swaputil.h:170
static void swap(T *a, T *b)
Definition bslalg_swaputil.h:194
bslalg::SwapUtil bslalg_SwapUtil
This alias is defined for backward compatibility.
Definition bslalg_swaputil.h:217
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
void swap(OptionValue &a, OptionValue &b)
Definition bdlc_flathashmap.h:1805
void swap(TYPE &a, TYPE &b)