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