BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslstl_ownerequal.h
Go to the documentation of this file.
1/// @file bslstl_ownerequal.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_ownerequal.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_OWNEREQUAL
9#define INCLUDED_BSLSTL_OWNEREQUAL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id$ $CSID$")
13
14/// @defgroup bslstl_ownerequal bslstl_ownerequal
15/// @brief Provide an ownership comparison for shared and weak pointers.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_ownerequal
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_ownerequal-purpose"> Purpose</a>
25/// * <a href="#bslstl_ownerequal-classes"> Classes </a>
26/// * <a href="#bslstl_ownerequal-description"> Description </a>
27/// * <a href="#bslstl_ownerequal-usage"> Usage </a>
28/// * <a href="#bslstl_ownerequal-example-1-basic-use-of-ref-owner_equal"> Example 1: Basic Use of @ref owner_equal </a>
29///
30/// # Purpose {#bslstl_ownerequal-purpose}
31/// Provide an ownership comparison for shared and weak pointers.
32///
33/// # Classes {#bslstl_ownerequal-classes}
34///
35/// - bsl::owner_equal: owner equality comparator for `shared_ptr` and `weak_ptr`
36///
37/// **Canonical header:** bsl_memory.h
38///
39/// @see bslstl_sharedptr
40///
41/// # Description {#bslstl_ownerequal-description}
42/// This component provides the C++26 standard binary comparison
43/// functor, `bsl::owner_equal`, that determines the equality of two smart
44/// pointer objects by the address of their `bslma::SharedPtrRep` data. Note
45/// that this class is an empty POD type.
46///
47/// ## Usage {#bslstl_ownerequal-usage}
48///
49///
50/// This section illustrates intended use of this component.
51///
52/// ### Example 1: Basic Use of @ref owner_equal {#bslstl_ownerequal-example-1-basic-use-of-ref-owner_equal}
53///
54///
55/// Suppose we need an unordered map accepting shared pointers as keys. We also
56/// expect that this container will be accessible from multiple threads and some
57/// of them will store weak versions of smart pointers to break reference
58/// cycles.
59///
60/// First, we create a container and populate it:
61/// @code
62/// typedef bsl::unordered_map<
63/// bsl::shared_ptr<int>,
64/// int,
65/// bsl::owner_hash,
66/// bsl::owner_equal> Map;
67///
68/// Map container;
69/// bsl::shared_ptr<int> sharedPtr1 = bsl::make_shared<int>(1);
70/// bsl::shared_ptr<int> sharedPtr2 = bsl::make_shared<int>(2);
71/// bsl::weak_ptr<int> weakPtr1(sharedPtr1);
72///
73/// container[sharedPtr1] = 1;
74/// container[sharedPtr2] = 2;
75/// @endcode
76/// Then we make sure that shared pointers can be used to perform lookup, and
77/// verify that the results are correct.
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 accessing the container from another thread and
88/// 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_ownerequal
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
120 // C++26 Compatibility
121
123
124 // TYPES
125
126 /// Type alias indicating this is a transparent comparator.
127 typedef void is_transparent;
128
129 // CREATORS
130 owner_equal() = default;
131 // Create an @ref owner_equal object.
132
133 owner_equal(const owner_equal& original) = default;
134 // Create an @ref owner_equal object. Note that as @ref owner_equal is an
135 // empty (stateless) type, this operation has no observable effect.
136
137 ~owner_equal() = default;
138 // Destroy this object.
139
140 // MANIPULATORS
141 owner_equal& operator=(const owner_equal& rhs) = default;
142 // Assign to this object the value of the specified 'rhs' object, and
143 // return a reference providing modifiable access to this object. Note
144 // that as @ref owner_equal is an empty (stateless) type, this operation
145 // has no observable effect.
146
147 // ACCESSORS
148 template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
150 const shared_ptr<ELEMENT_TYPE_Y>& y) const
152 template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
154 const weak_ptr< ELEMENT_TYPE_Y>& y) const
156 template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
158 const shared_ptr<ELEMENT_TYPE_Y>& y) const
160
161 /// Return `true` if the address of the
162 /// `BloombergLP::bslma::SharedPtrRep` object used by the specified `x`
163 /// is equal to the address of the `BloombergLP::bslma::SharedPtrRep`
164 /// object used by the specified `y`, and `false` otherwise.
165 template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
167 const weak_ptr<ELEMENT_TYPE_Y>& y) const
169};
170
171// ============================================================================
172// INLINE DEFINITIONS
173// ============================================================================
174
175 // ------------------
176 // struct owner_equal
177 // ------------------
178
179template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
180inline
187
188template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
189inline
193{
194 return x.owner_equal(y);
195}
196
197template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
198inline
202{
203 return x.owner_equal(y);
204}
205
206template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
207inline
211{
212 return x.owner_equal(y);
213}
214
215// ============================================================================
216// TYPE TRAITS
217// ============================================================================
218
219// Type traits for @ref owner_equal
220//: o @ref owner_equal is a stateless POD, trivially constructible and copyable.
221
222template <>
224
225template <>
227
228} // close namespace bsl
229
230#endif
231
232// ----------------------------------------------------------------------------
233// Copyright 2024 Bloomberg Finance L.P.
234//
235// Licensed under the Apache License, Version 2.0 (the "License");
236// you may not use this file except in compliance with the License.
237// You may obtain a copy of the License at
238//
239// http://www.apache.org/licenses/LICENSE-2.0
240//
241// Unless required by applicable law or agreed to in writing, software
242// distributed under the License is distributed on an "AS IS" BASIS,
243// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
244// See the License for the specific language governing permissions and
245// limitations under the License.
246// ----------------------------- END-OF-FILE ----------------------------------
247
248/** @} */
249/** @} */
250/** @} */
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
Definition bslstl_ownerequal.h:122
owner_equal & operator=(const owner_equal &rhs)=default
void is_transparent
Type alias indicating this is a transparent comparator.
Definition bslstl_ownerequal.h:127
owner_equal()=default
owner_equal(const owner_equal &original)=default
~owner_equal()=default
bool operator()(const shared_ptr< ELEMENT_TYPE_X > &x, const shared_ptr< ELEMENT_TYPE_Y > &y) const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_ownerequal.h:181