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