BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_utility.h
Go to the documentation of this file.
1/// @file bslstl_utility.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_utility.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_UTILITY
9#define INCLUDED_BSLSTL_UTILITY
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_utility bslstl_utility
15/// @brief Provide implementations for utilities not in the system library.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_utility
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_utility-purpose"> Purpose</a>
25/// * <a href="#bslstl_utility-classes"> Classes </a>
26/// * <a href="#bslstl_utility-canonical-header"> Canonical Header </a>
27/// * <a href="#bslstl_utility-description"> Description </a>
28/// * <a href="#bslstl_utility-usage"> Usage </a>
29///
30/// # Purpose {#bslstl_utility-purpose}
31/// Provide implementations for utilities not in the system library.
32///
33/// # Classes {#bslstl_utility-classes}
34///
35///
36/// # Canonical Header {#bslstl_utility-canonical-header}
37/// bsl_utility.h
38///
39/// @see bsl+bslhdrs
40///
41/// # Description {#bslstl_utility-description}
42/// This component is for internal use only. Please include
43/// `<bsl_utility.h>` instead. This component provides a namespace for free
44/// functions implementing standard utilities that are not provided by the
45/// underlying standard library implementation. For example, `as_const` is a
46/// C++17 utility, and it is provided here for code using C++03.
47///
48/// ## Usage {#bslstl_utility-usage}
49///
50///
51/// This component is for use by the `bsl+bslhdrs` package. Use
52/// `bsl_utility.h` directly.
53/// @}
54/** @} */
55/** @} */
56
57/** @addtogroup bsl
58 * @{
59 */
60/** @addtogroup bslstl
61 * @{
62 */
63/** @addtogroup bslstl_utility
64 * @{
65 */
66
67#include <bslscm_version.h>
68
69#include <bslmf_addconst.h>
70#include <bslmf_movableref.h>
71
73#include <bsls_keyword.h>
75
76#include <utility>
77
78#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
79#include <bsls_nativestd.h>
80#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
81
82namespace bsl {
83
84#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
85using std::as_const;
86#else
87/// Return a reference offering non-modifiable access to the specified `t`.
88template <class TYPE>
91
92# ifdef BSLS_COMPILERFEATURES_FULL_CPP11
93template <class TYPE>
94void as_const(const TYPE&&) = delete;
95# endif // C++11
96#endif // !BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
97
98#ifdef BSLS_LIBRARYFEATURES_HAS_CPP14_BASELINE_LIBRARY
99using std::exchange;
100#else
101# ifdef BSLS_COMPILERFEATURES_FULL_CPP11
102/// Assign to the specified `obj` the specified `newValue`, forwarded to the
103/// assignment operator of `obj`, and return the value held by `obj` prior to
104/// that assignment.
105template <class t_TYPE, class t_OTHER_TYPE = t_TYPE>
106t_TYPE exchange(t_TYPE& obj, t_OTHER_TYPE&& newValue);
107# else
108/// Assign to the specified `obj` the specified `newValue`, and return the value held by `obj` prior to that assignment.
109///
110/// \note Note that these functions
111/// support move semantics, simulated using `bslmf::MovableRef`:
112/// * The return value is move-constructed from `obj`.
113/// * If `t_OTHER_TYPE` is a specialization of `bslmf::MovableRef`, then move
114/// assignment to `obj` will occur.
115template <class t_TYPE, class t_OTHER_TYPE>
116t_TYPE exchange(t_TYPE& obj, const t_OTHER_TYPE& newValue);
117template <class t_TYPE, class t_OTHER_TYPE>
118t_TYPE exchange(t_TYPE& obj, t_OTHER_TYPE& newValue);
119# endif // C++11
120#endif // C++14
121
122// ============================================================================
123// INLINE DEFINITIONS
124// ============================================================================
125
126#ifndef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
127template <class TYPE>
130{
131 return t;
132}
133#endif
134
135#ifndef BSLS_LIBRARYFEATURES_HAS_CPP14_BASELINE_LIBRARY
136# ifdef BSLS_COMPILERFEATURES_FULL_CPP11
137template <class t_TYPE, class t_OTHER_TYPE>
138t_TYPE exchange(t_TYPE& obj, t_OTHER_TYPE&& newValue)
139{
140 t_TYPE oldValue = static_cast<t_TYPE&&>(obj);
141 obj = static_cast<t_OTHER_TYPE&&>(newValue);
142 return oldValue;
143}
144# else
145} // close namespace bsl
146
147namespace bslstl {
148/// This component-private class template is used to implement `bsl::exchange`
149/// in C++03. It performs an assignment in its destructor. See the
150/// implementation notes in the .cpp file for more information.
151///
152/// See @ref bslstl_utility
153template <class t_TYPE, class t_OTHER_TYPE>
155 // DATA
156
157 t_TYPE& d_obj; // left side for assignment
158 t_OTHER_TYPE& d_newValue; // right side for assignment
159
160 // CREATORS
161
162 /// Create a `Utility_ExchangeAssignOnDestruction` object that, on
163 /// destruction, will assign the specified `newValue` to the specified
164 /// `obj`.
165 Utility_ExchangeAssignOnDestruction(t_TYPE& obj, t_OTHER_TYPE& newValue)
166 : d_obj(obj), d_newValue(newValue) {}
167
168 /// Destroy this object, performing the promised assignment.
174};
175} // close package bslstl
176
177namespace bsl {
178template <class t_TYPE, class t_OTHER_TYPE>
179t_TYPE exchange(t_TYPE& obj, const t_OTHER_TYPE& newValue)
180{
181 BloombergLP::bslstl::
182 Utility_ExchangeAssignOnDestruction<t_TYPE, const t_OTHER_TYPE>
183 a(obj, newValue);
184 return t_TYPE(BloombergLP::bslmf::MovableRefUtil::move(obj));
185}
186
187template <class t_TYPE, class t_OTHER_TYPE>
188t_TYPE exchange(t_TYPE& obj, t_OTHER_TYPE& newValue)
189{
190 BloombergLP::bslstl::Utility_ExchangeAssignOnDestruction<t_TYPE,
191 t_OTHER_TYPE>
192 a(obj, newValue);
193 return t_TYPE(BloombergLP::bslmf::MovableRefUtil::move(obj));
194}
195# endif // C++11
196#endif // C++14
197} // close namespace bsl
198
199#endif // INCLUDED_BSLSTL_UTILITY
200
201// ----------------------------------------------------------------------------
202// Copyright 2022 Bloomberg Finance L.P.
203//
204// Licensed under the Apache License, Version 2.0 (the "License");
205// you may not use this file except in compliance with the License.
206// You may obtain a copy of the License at
207//
208// http://www.apache.org/licenses/LICENSE-2.0
209//
210// Unless required by applicable law or agreed to in writing, software
211// distributed under the License is distributed on an "AS IS" BASIS,
212// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
213// See the License for the specific language governing permissions and
214// limitations under the License.
215// ----------------------------- END-OF-FILE ----------------------------------
216
217/** @} */
218/** @} */
219/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_CONSTEXPR
Definition bsls_keyword.h:624
#define BSLS_KEYWORD_NOEXCEPT
Definition bsls_keyword.h:674
#define BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(...)
Definition bsls_keyword.h:676
Definition bdlat_valuetypefunctions.h:939
t_TYPE exchange(t_TYPE &obj, const t_OTHER_TYPE &newValue)
Definition bslstl_utility.h:179
BSLS_KEYWORD_CONSTEXPR bsl::add_const< TYPE >::type & as_const(TYPE &t) BSLS_KEYWORD_NOEXCEPT
Return a reference offering non-modifiable access to the specified t.
Definition bslstl_utility.h:129
Definition bslstl_algorithm.h:84
BloombergLP::bslmf::AddConst_Imp< t_TYPE,!is_reference< t_TYPE >::value &&!is_function< t_TYPE >::value &&!is_const< t_TYPE >::value >::Type type
Definition bslmf_addconst.h:176
Definition bslstl_utility.h:154
~Utility_ExchangeAssignOnDestruction() BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(false)
Destroy this object, performing the promised assignment.
Definition bslstl_utility.h:169
Utility_ExchangeAssignOnDestruction(t_TYPE &obj, t_OTHER_TYPE &newValue)
Definition bslstl_utility.h:165
t_OTHER_TYPE & d_newValue
Definition bslstl_utility.h:158
t_TYPE & d_obj
Definition bslstl_utility.h:157