BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmf_referencewrapper.h
Go to the documentation of this file.
1/// @file bslmf_referencewrapper.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_referencewrapper.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_REFERENCEWRAPPER
9#define INCLUDED_BSLMF_REFERENCEWRAPPER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_referencewrapper bslmf_referencewrapper
15/// @brief Provide a copyable, assignable object wrapper for references.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_referencewrapper
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_referencewrapper-purpose"> Purpose</a>
25/// * <a href="#bslmf_referencewrapper-classes"> Classes </a>
26/// * <a href="#bslmf_referencewrapper-canonical-header"> Canonical Header </a>
27/// * <a href="#bslmf_referencewrapper-description"> Description </a>
28/// * <a href="#bslmf_referencewrapper-usage"> Usage </a>
29/// * <a href="#bslmf_referencewrapper-example-1-sorted-references"> Example 1: Sorted References </a>
30///
31/// # Purpose {#bslmf_referencewrapper-purpose}
32/// Provide a copyable, assignable object wrapper for references.
33///
34/// # Classes {#bslmf_referencewrapper-classes}
35///
36/// - bsl::reference_wrapper: copyable, rebindable reference proxy
37///
38/// # Canonical Header {#bslmf_referencewrapper-canonical-header}
39/// bsl_functional.h
40///
41/// # Description {#bslmf_referencewrapper-description}
42/// This component provides `bsl::reference_wrapper`, a copyable,
43/// rebindable proxy for a reference to an object or function. As a
44/// @ref reference_wrapper is an object it can be stored in a place that cannot
45/// normally hold a reference, such as an array or a Standard Library container.
46/// A @ref reference_wrapper is implicitly convertible to its contained reference
47/// type so that it can be passed to functions that take such a reference.
48/// Unlike a native C++ reference, which is immutably bound to its target at
49/// initialization, a @ref reference_wrapper may be rebound to refer to a different
50/// target by assignment from another @ref reference_wrapper object.
51///
52/// This component also provides the (free) helper functions `bsl::ref` and
53/// `bsl::cref` that may be used to generate @ref reference_wrapper objects more
54/// concisely than with the constructor.
55///
56/// NOTE: This component provides only a partial implementation of the standard
57/// class template before C++11, omitting support for use as a function object.
58/// Further, the C++ Standard Library uses `std::reference_wrapper` as a special
59/// vocabulary type for several factory functions such as `std::bind`,
60/// `std::make_pair`, and `std::make_tuple` to embed true references in the
61/// created objects. The `bsl` library does not implement those functions, but
62/// uses the `std` implementations directly. Hence, those features are not
63/// available with our C++03 implementation.
64///
65/// ## Usage {#bslmf_referencewrapper-usage}
66///
67///
68/// This section illustrates intended use of this component.
69///
70/// ### Example 1: Sorted References {#bslmf_referencewrapper-example-1-sorted-references}
71///
72///
73/// Let us suppose that we wish to handle objects that will be passed to a
74/// comparison function expecting references to the objects. Let us suppose
75/// further that these objects are large enough that we would not wish to move
76/// them around bodily as they are sorted.
77///
78/// First, let us define the large-object type:
79/// @code
80/// struct Canary {
81/// static const int s_size = 1000;
82/// Canary *d_values[s_size];
83/// Canary();
84/// };
85///
86/// Canary::Canary()
87/// {
88/// for (int i = 0; i < s_size; ++i) {
89/// d_values[i] = this;
90/// }
91/// }
92/// @endcode
93/// Next, we define the comparison function:
94/// @code
95/// bool operator<(Canary const& a, Canary const& b)
96/// {
97/// return a.d_values[0] < b.d_values[0];
98/// }
99/// @endcode
100/// Finally, we define a generic function to sort two items:
101/// @code
102/// template <class T>
103/// void sortTwoItems(T& a, T& b)
104/// {
105/// if (b < a) {
106/// T tmp(a);
107/// a = b;
108/// b = tmp;
109/// }
110/// }
111/// @endcode
112/// We can call `sortTwoItems` on wrappers representing `Canary` objects
113/// without need to move actual, large `Canary` objects about. In the call to
114/// `sortTwoItems`, below, the `operator=` used in it is that of
115/// `bsl::reference_wrapper<Canary>`, but the `operator<` used is the one
116/// declared for `Canary&` arguments. All of the conversions needed are
117/// applied implicitly:
118/// @code
119/// Canary canaries[2];
120/// bsl::reference_wrapper<Canary> canaryA = bsl::ref(canaries[1]);
121/// bsl::reference_wrapper<Canary> canaryB = bsl::ref(canaries[0]);
122/// sortTwoItems(canaryA, canaryB);
123///
124/// assert(&canaryA.get() == canaries);
125/// assert(&canaryB.get() == canaries + 1);
126/// @endcode
127/// @}
128/** @} */
129/** @} */
130
131/** @addtogroup bsl
132 * @{
133 */
134/** @addtogroup bslmf
135 * @{
136 */
137/** @addtogroup bslmf_referencewrapper
138 * @{
139 */
140
141#include <bslscm_version.h>
142
146
148#include <bsls_util.h> // for BloombergLP::bsls::Util::addressOf
149
150#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
152#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
153
154#ifdef BSLS_COMPILERFEATURES_FULL_CPP11
155# include <functional>
156# define BSLMF_REFERENCEWRAPPER_IS_ALIASED
157
158namespace bsl {
159using std::cref;
160using std::ref;
161using std::reference_wrapper;
162
163
164#else // C++03 implementation
165
166namespace bsl {
167
168 // =======================
169 // class reference_wrapper
170 // =======================
171
172/// This class is a copyable, rebindable proxy for a reference to an object
173/// or function. It encapsulates a reference into a value that can be
174/// reassigned to refer to a different target and can be stored where a
175/// native reference cannot, such as in a Standard Library container. A
176/// @ref reference_wrapper is implicitly convertible to a reference to the
177/// represented type, so it can be passed to functions expecting such a
178/// reference.
179///
180/// See @ref bslmf_referencewrapper
181template <class T>
183
184 private:
185 // DATA
186 T *d_represented_p; // the represented object (not owned)
187
188 public:
189 // TYPES
190 typedef T type;
191
192 // TRAITS
195
196 // CREATORS
197
198 /// Create a reference wrapper representing the specified `object`.
199 reference_wrapper(T& object); // IMPLICIT
200
201 /// Create a reference wrapper referring to the same object as the
202 /// specified `original`.
203 reference_wrapper(const reference_wrapper& original) = default;
204
205 /// Destroy this object.
207
208 // MANIPULATORS
209
210 /// Assign this object to refer to the same object as the specified
211 /// `rhs`, and return `*this`.
213
214 // ACCESSORS
215
216 /// Return a reference to the object that `*this` represents.
217 T& get() const;
218
219 /// Return a reference to the object that `*this` represents.
220 operator T&() const;
221};
222
223// FREE FUNCTIONS
224
225/// Return a reference wrapper representing a `const` view of the specified
226/// `object`.
227template <class T>
229
230/// Return a reference wrapper representing a `const` view of the same
231/// object as the specified `original`.
232template <class T>
234
235/// Return a reference wrapper that represents the specified `object`.
236template <class T>
238
239/// Return a reference wrapper that represents the same object as the
240/// specified `original`.
241template <class T>
243
244} // close namespace bsl
245
246// ============================================================================
247// INLINE DEFINITIONS
248// ============================================================================
249
250 // -----------------------
251 // class reference_wrapper
252 // -----------------------
253
254// CREATORS
255template <class T>
256inline
258 : d_represented_p(BloombergLP::bsls::Util::addressOf(object))
259{
260}
261
262// ACCESSORS
263template <class T>
264inline
266{
267 return *d_represented_p;
268}
269
270template <class T>
271inline
273{
274 return *d_represented_p;
275}
276
277// FREE FUNCTIONS
278template <class T>
279inline
281{
282 return reference_wrapper<const T>(object);
283}
284
285template <class T>
286inline
288{
289 return cref(original.get());
290}
291
292template <class T>
293inline
295{
296 return reference_wrapper<T>(object);
297}
298
299template <class T>
300inline
302{
303 return ref(original.get());
304}
305
306#endif // BSLMF_REFERENCEWRAPPER_IS_ALIASED
307
308
309namespace bslmf {
310
311template <class T>
312struct IsReferenceWrapper<bsl::reference_wrapper<T> > : bsl::true_type { };
313
314} // close namespace bslmf
315
316
317#endif
318
319// ----------------------------------------------------------------------------
320// Copyright 2013 Bloomberg Finance L.P.
321//
322// Licensed under the Apache License, Version 2.0 (the "License");
323// you may not use this file except in compliance with the License.
324// You may obtain a copy of the License at
325//
326// http://www.apache.org/licenses/LICENSE-2.0
327//
328// Unless required by applicable law or agreed to in writing, software
329// distributed under the License is distributed on an "AS IS" BASIS,
330// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
331// See the License for the specific language governing permissions and
332// limitations under the License.
333// ----------------------------- END-OF-FILE ----------------------------------
334
335/** @} */
336/** @} */
337/** @} */
Definition bslmf_referencewrapper.h:182
~reference_wrapper()=default
Destroy this object.
T type
Definition bslmf_referencewrapper.h:190
reference_wrapper & operator=(const reference_wrapper &rhs)=default
reference_wrapper(const reference_wrapper &original)=default
BSLMF_NESTED_TRAIT_DECLARATION(reference_wrapper, bsl::is_trivially_copyable)
T & get() const
Return a reference to the object that *this represents.
Definition bslmf_referencewrapper.h:265
reference_wrapper(T &object)
Create a reference wrapper representing the specified object.
Definition bslmf_referencewrapper.h:257
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlat_valuetypefunctions.h:939
reference_wrapper< const T > cref(const T &object)
reference_wrapper< T > ref(T &object)
Return a reference wrapper that represents the specified object.
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
Definition bdlbb_blob.h:579
Definition bdlt_iso8601util.h:707
Definition bslmf_istriviallycopyable.h:324
Definition bslmf_isreferencewrapper.h:167