BDE 4.39.x 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
149namespace bslalg {
150
151 // ===============
152 // struct SwapUtil
153 // ===============
154
155/// This class provides a namespace for the `swap` utility method.
156///
157/// See @ref bslalg_swaputil
158class SwapUtil {
159
160 public:
161 // CLASS METHODS
162
163 /// Exchange the values of the specified `a` and `b` objects using
164 /// either a `swap` free function overloaded for type `T`, in the
165 /// namespace of type `T` if it's available, and the default generic
166 /// `bsl::swap` otherwise.
167 template <class T>
168 static
169 void swap(T *a, T *b);
170};
171
172// ============================================================================
173// INLINE FUNCTION DEFINITIONS
174// ============================================================================
175
176 // ---------------
177 // struct SwapUtil
178 // ---------------
179
180// CLASS METHODS
181template <class T>
182void SwapUtil::swap(T *a, T *b)
183{
184 BSLS_ASSERT_SAFE(a != NULL);
185 BSLS_ASSERT_SAFE(b != NULL);
186
187 using std::swap;
188
189 swap(*a, *b);
190}
191
192} // close package namespace
193
194#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
195// ============================================================================
196// BACKWARD COMPATIBILITY
197// ============================================================================
198
199/// This alias is defined for backward compatibility.
201#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
202
203
204
205#endif
206
207// ----------------------------------------------------------------------------
208// Copyright 2013 Bloomberg Finance L.P.
209//
210// Licensed under the Apache License, Version 2.0 (the "License");
211// you may not use this file except in compliance with the License.
212// You may obtain a copy of the License at
213//
214// http://www.apache.org/licenses/LICENSE-2.0
215//
216// Unless required by applicable law or agreed to in writing, software
217// distributed under the License is distributed on an "AS IS" BASIS,
218// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
219// See the License for the specific language governing permissions and
220// limitations under the License.
221// ----------------------------- END-OF-FILE ----------------------------------
222
223/** @} */
224/** @} */
225/** @} */
Definition bslalg_swaputil.h:158
static void swap(T *a, T *b)
Definition bslalg_swaputil.h:182
bslalg::SwapUtil bslalg_SwapUtil
This alias is defined for backward compatibility.
Definition bslalg_swaputil.h:200
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlc_flathashmap.h:2218