BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslstl_ownerless.h
Go to the documentation of this file.
1/// @file bslstl_ownerless.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_ownerless.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_OWNERLESS
9#define INCLUDED_BSLSTL_OWNERLESS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id$ $CSID$")
13
14/// @defgroup bslstl_ownerless bslstl_ownerless
15/// @brief Provide an ordering for shared and weak pointers.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_ownerless
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_ownerless-purpose"> Purpose</a>
25/// * <a href="#bslstl_ownerless-classes"> Classes </a>
26/// * <a href="#bslstl_ownerless-description"> Description </a>
27/// * <a href="#bslstl_ownerless-usage"> Usage </a>
28/// * <a href="#bslstl_ownerless-example-1-basic-use-of-owner_less<void>"> Example 1: Basic Use of owner_less<void> </a>
29///
30/// # Purpose {#bslstl_ownerless-purpose}
31/// Provide an ordering for shared and weak pointers.
32///
33/// # Classes {#bslstl_ownerless-classes}
34///
35/// - bsl::owner_less: ordering comparator for `shared_ptr` and `weak_ptr`
36///
37/// **Canonical header:** bsl_memory.h
38///
39/// @see bslstl_sharedptr
40///
41/// # Description {#bslstl_ownerless-description}
42/// This component provides the C+11 standard binary comparison
43/// functor, `bsl::owner_less`, that determines the order of two smart pointer
44/// objects by the relative order of the address of their `bslma::SharedPtrRep`
45/// data. Note that this class is an empty POD type.
46///
47/// ## Usage {#bslstl_ownerless-usage}
48///
49///
50/// This section illustrates intended use of this component.
51///
52/// ### Example 1: Basic Use of owner_less<void> {#bslstl_ownerless-example-1-basic-use-of-owner_less<void>}
53///
54///
55/// Suppose we need a map accepting shared pointers as keys. We also expect
56/// that this container will be accessible from multiple threads and some of
57/// them will store weak versions of smart pointers to break reference cycles.
58/// To avoid excessive conversions we can use a transparent comparator to
59/// enable heterogeneous lookup with `bsl::weak_ptr` objects as parameters for
60/// search functions.
61///
62/// First, we create a container and populate it:
63/// @code
64/// typedef bsl::map<bsl::shared_ptr<int>, int, bsl::owner_less<void> >
65/// Map;
66/// Map container;
67///
68/// bsl::shared_ptr<int> sharedPtr1 = bsl::make_shared<int>(1);
69/// bsl::shared_ptr<int> sharedPtr2 = bsl::make_shared<int>(2);
70/// bsl::weak_ptr<int> weakPtr1(sharedPtr1);
71///
72/// container[sharedPtr1] = 1;
73/// container[sharedPtr2] = 2;
74/// @endcode
75/// Now, we make sure, that shared pointers can be used to perform lookup:
76/// @code
77/// Map::const_iterator iter = container.find(sharedPtr1);
78/// assert(container.end() != iter );
79/// assert(1 == iter->second);
80///
81/// iter = container.find(sharedPtr2);
82/// assert(container.end() != iter);
83/// assert(2 == iter->second);
84/// @endcode
85/// Finally, we simulate the situation of accessing the container from another
86/// thread and perform lookup using weak pointers:
87/// @code
88/// iter = container.find(weakPtr1);
89/// assert(container.end() != iter );
90/// assert(1 == iter->second);
91///
92/// bsl::weak_ptr<int> weakPtr3(bsl::make_shared<int>(3));
93/// iter = container.find(weakPtr3);
94/// assert(container.end() == iter);
95/// @endcode
96/// @}
97/** @} */
98/** @} */
99
100/** @addtogroup bsl
101 * @{
102 */
103/** @addtogroup bslstl
104 * @{
105 */
106/** @addtogroup bslstl_ownerless
107 * @{
108 */
109
110#include <bslscm_version.h>
111
112#include <bslstl_sharedptr.h>
113
114#include <bsls_keyword.h> // 'BSLS_KEYWORD_NOEXCEPT'
115
116namespace bsl {
117
118template <class POINTER_TYPE = void>
120
121template <class ELEMENT_TYPE>
122struct owner_less<shared_ptr<ELEMENT_TYPE> >;
123
124template <class ELEMENT_TYPE>
125struct owner_less<weak_ptr<ELEMENT_TYPE> >;
126
127template <>
128struct owner_less<void>;
129
130 // C++11 Compatibility
131
132template <class ELEMENT_TYPE>
133struct owner_less<shared_ptr<ELEMENT_TYPE> > {
134
135 // TYPES
136 typedef bool result_type;
139
140 // CREATORS
141 owner_less() = default;
142 // Create an @ref owner_less object.
143
144 owner_less(const owner_less& original) = default;
145 // Create an @ref owner_less object. Note that as @ref owner_less is an
146 // empty (stateless) type, this operation has no observable effect.
147
148 ~owner_less() = default;
149 // Destroy this object.
150
151 // MANIPULATORS
152 owner_less& operator=(const owner_less& rhs) = default;
153 // Assign to this object the value of the specified 'rhs' object, and
154 // return a reference providing modifiable access to this object. Note
155 // that as @ref owner_less is an empty (stateless) type, this operation
156 // has no observable effect.
157
158 // ACCESSORS
159 bool operator()(const shared_ptr<ELEMENT_TYPE>& a,
160 const shared_ptr<ELEMENT_TYPE>& b) const
162 bool operator()(const shared_ptr<ELEMENT_TYPE>& a,
163 const weak_ptr<ELEMENT_TYPE>& b) const
165
166 /// Return `true` if the address of the
167 /// `BloombergLP::bslma::SharedPtrRep` object used by the specified `a`
168 /// is ordered before the address of the
169 /// `BloombergLP::bslma::SharedPtrRep` object used by the specified `b`
170 /// under the total ordering supplied by
171 /// `std::less<BloombergLP::bslma::SharedPtrRep *>`, and `false`
172 /// otherwise.
173 bool operator()(const weak_ptr<ELEMENT_TYPE>& a,
174 const shared_ptr<ELEMENT_TYPE>& b) const
176};
177
178template <class ELEMENT_TYPE>
179struct owner_less<weak_ptr<ELEMENT_TYPE> > {
180
181 // TYPES
182 typedef bool result_type;
185
186 // CREATORS
187 owner_less() = default;
188 // Create an @ref owner_less object.
189
190 owner_less(const owner_less& original) = default;
191 // Create an @ref owner_less object. Note that as @ref owner_less is an
192 // empty (stateless) type, this operation has no observable effect.
193
194 ~owner_less() = default;
195 // Destroy this object.
196
197 // MANIPULATORS
198 owner_less& operator=(const owner_less& rhs) = default;
199 // Assign to this object the value of the specified 'rhs' object, and
200 // return a reference providing modifiable access to this object. Note
201 // that as @ref owner_less is an empty (stateless) type, this operation
202 // has no observable effect.
203
204 // ACCESSORS
205 bool operator()(const weak_ptr<ELEMENT_TYPE>& a,
206 const weak_ptr<ELEMENT_TYPE>& b) const
208 bool operator()(const shared_ptr<ELEMENT_TYPE>& a,
209 const weak_ptr<ELEMENT_TYPE>& b) const
211
212 /// Return `true` if the address of the
213 /// `BloombergLP::bslma::SharedPtrRep` object used by the specified `a`
214 /// is ordered before the address of the
215 /// `BloombergLP::bslma::SharedPtrRep` object used by the specified `b`
216 /// under the total ordering supplied by
217 /// `std::less<BloombergLP::bslma::SharedPtrRep *>`, and `false`
218 /// otherwise.
219 bool operator()(const weak_ptr<ELEMENT_TYPE>& a,
220 const shared_ptr<ELEMENT_TYPE>& b) const
222};
223
224template<>
225struct owner_less<void> {
226
227 // TYPES
228
229 /// Type alias indicating this is a transparent comparator.
230 typedef void is_transparent;
231
232 // CREATORS
233 owner_less() = default;
234 // Create an @ref owner_less object.
235
236 owner_less(const owner_less& original) = default;
237 // Create an @ref owner_less object. Note that as @ref owner_less is an
238 // empty (stateless) type, this operation has no observable effect.
239
240 ~owner_less() = default;
241 // Destroy this object.
242
243 // MANIPULATORS
244 owner_less& operator=(const owner_less& rhs) = default;
245 // Assign to this object the value of the specified 'rhs' object, and
246 // return a reference providing modifiable access to this object. Note
247 // that as @ref owner_less is an empty (stateless) type, this operation
248 // has no observable effect.
249
250 // ACCESSORS
251 template<class ELEMENT_TYPE_A, class ELEMENT_TYPE_B>
252 bool operator()(const shared_ptr<ELEMENT_TYPE_A> &a,
253 const shared_ptr<ELEMENT_TYPE_B> &b) const
255 template<class ELEMENT_TYPE_A, class ELEMENT_TYPE_B>
256 bool operator()(const shared_ptr<ELEMENT_TYPE_A> &a,
257 const weak_ptr< ELEMENT_TYPE_B> &b) const
259 template<class ELEMENT_TYPE_A, class ELEMENT_TYPE_B>
260 bool operator()(const weak_ptr< ELEMENT_TYPE_A> &a,
261 const shared_ptr<ELEMENT_TYPE_B> &b) const
263
264 /// Return `true` if the address of the
265 /// `BloombergLP::bslma::SharedPtrRep` object used by the specified `a`
266 /// is ordered before the address of the
267 /// `BloombergLP::bslma::SharedPtrRep` object used by the specified `b`
268 /// under the total ordering supplied by
269 /// `std::less<BloombergLP::bslma::SharedPtrRep *>`, and `false`
270 /// otherwise.
271 template<class ELEMENT_TYPE_A, class ELEMENT_TYPE_B>
272 bool operator()(const weak_ptr<ELEMENT_TYPE_A> &a,
273 const weak_ptr<ELEMENT_TYPE_B> &b) const
275};
276
277// ============================================================================
278// INLINE DEFINITIONS
279// ============================================================================
280
281 // --------------------------------------------
282 // struct owner_less<shared_ptr<ELEMENT_TYPE> >
283 // --------------------------------------------
284
285template <class ELEMENT_TYPE>
286inline
290{
291 return a.owner_before(b);
292}
293
294template <class ELEMENT_TYPE>
295inline
299{
300 return a.owner_before(b);
301}
302
303template <class ELEMENT_TYPE>
304inline
306 const weak_ptr<ELEMENT_TYPE>& a,
308{
309 return a.owner_before(b);
310}
311
312 // ------------------------------------------
313 // struct owner_less<weak_ptr<ELEMENT_TYPE> >
314 // ------------------------------------------
315
316template <class ELEMENT_TYPE>
317inline
319 const weak_ptr<ELEMENT_TYPE>& a,
321{
322 return a.owner_before(b);
323}
324
325template <class ELEMENT_TYPE>
326inline
330{
331 return a.owner_before(b);
332}
333
334template <class ELEMENT_TYPE>
335inline
337 const weak_ptr<ELEMENT_TYPE>& a,
339{
340 return a.owner_before(b);
341}
342
343 // -----------------------
344 // struct owner_less<void>
345 // -----------------------
346
347template<class ELEMENT_TYPE_A, class ELEMENT_TYPE_B>
348inline
352{
353 return a.owner_before(b);
354}
355
356template<class ELEMENT_TYPE_A, class ELEMENT_TYPE_B>
357inline
361{
362 return a.owner_before(b);
363}
364
365template<class ELEMENT_TYPE_A, class ELEMENT_TYPE_B>
366inline
370{
371 return a.owner_before(b);
372}
373
374template<class ELEMENT_TYPE_A, class ELEMENT_TYPE_B>
375inline
379{
380 return a.owner_before(b);
381}
382
383// ============================================================================
384// TYPE TRAITS
385// ============================================================================
386
387// Type traits for @ref owner_less
388//: o @ref owner_less is a stateless POD, trivially constructible and copyable.
389
390template<class POINTER_TYPE>
394
395template<class POINTER_TYPE>
396struct is_trivially_copyable<owner_less<POINTER_TYPE> >
398{};
399
400} // close namespace bsl
401
402#endif
403
404// ----------------------------------------------------------------------------
405// Copyright 2014 Bloomberg Finance L.P.
406//
407// Licensed under the Apache License, Version 2.0 (the "License");
408// you may not use this file except in compliance with the License.
409// You may obtain a copy of the License at
410//
411// http://www.apache.org/licenses/LICENSE-2.0
412//
413// Unless required by applicable law or agreed to in writing, software
414// distributed under the License is distributed on an "AS IS" BASIS,
415// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
416// See the License for the specific language governing permissions and
417// limitations under the License.
418// ----------------------------- END-OF-FILE ----------------------------------
419
420/** @} */
421/** @} */
422/** @} */
Definition bslstl_sharedptr.h:1830
Definition bslstl_sharedptr.h:3705
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_NOEXCEPT
Definition bsls_keyword.h:632
Definition bdlb_printmethods.h:283
Definition bslmf_istriviallycopyable.h:329
Definition bslmf_istriviallydefaultconstructible.h:293
bool result_type
Definition bslstl_ownerless.h:136
shared_ptr< ELEMENT_TYPE > second_argument_type
Definition bslstl_ownerless.h:138
owner_less & operator=(const owner_less &rhs)=default
shared_ptr< ELEMENT_TYPE > first_argument_type
Definition bslstl_ownerless.h:137
owner_less(const owner_less &original)=default
owner_less(const owner_less &original)=default
void is_transparent
Type alias indicating this is a transparent comparator.
Definition bslstl_ownerless.h:230
owner_less & operator=(const owner_less &rhs)=default
weak_ptr< ELEMENT_TYPE > second_argument_type
Definition bslstl_ownerless.h:184
owner_less & operator=(const owner_less &rhs)=default
weak_ptr< ELEMENT_TYPE > first_argument_type
Definition bslstl_ownerless.h:183
bool result_type
Definition bslstl_ownerless.h:182
owner_less(const owner_less &original)=default
Definition bslstl_ownerless.h:119