BDE 4.39.x 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-canonical-header"> Canonical Header </a>
27/// * <a href="#bslstl_ownerequal-description"> Description </a>
28/// * <a href="#bslstl_ownerequal-usage"> Usage </a>
29/// * <a href="#bslstl_ownerequal-example-1-basic-use-of-ref-owner_equal"> Example 1: Basic Use of @ref owner_equal </a>
30///
31/// # Purpose {#bslstl_ownerequal-purpose}
32/// Provide an ownership comparison for shared and weak pointers.
33///
34/// # Classes {#bslstl_ownerequal-classes}
35///
36/// - bsl::owner_equal: owner equality comparator for `shared_ptr` and `weak_ptr`
37///
38/// # Canonical Header {#bslstl_ownerequal-canonical-header}
39/// bsl_memory.h
40///
41/// @see bslstl_sharedptr
42///
43/// # Description {#bslstl_ownerequal-description}
44/// This component provides the C++26 standard binary comparison
45/// functor, `bsl::owner_equal`, that determines the equality of two smart
46/// pointer objects by the address of their `bslma::SharedPtrRep` data. Note
47/// that this class is an empty POD type.
48///
49/// ## Usage {#bslstl_ownerequal-usage}
50///
51///
52/// This section illustrates intended use of this component.
53///
54/// ### Example 1: Basic Use of @ref owner_equal {#bslstl_ownerequal-example-1-basic-use-of-ref-owner_equal}
55///
56///
57/// Suppose we need an unordered map accepting shared pointers as keys. We also
58/// expect that this container will be accessible from multiple threads and some
59/// of them will store weak versions of smart pointers to break reference
60/// cycles.
61///
62/// First, we create a container and populate it:
63/// @code
64/// typedef bsl::unordered_map<
65/// bsl::shared_ptr<int>,
66/// int,
67/// bsl::owner_hash,
68/// bsl::owner_equal> Map;
69///
70/// Map container;
71/// bsl::shared_ptr<int> sharedPtr1 = bsl::make_shared<int>(1);
72/// bsl::shared_ptr<int> sharedPtr2 = bsl::make_shared<int>(2);
73/// bsl::weak_ptr<int> weakPtr1(sharedPtr1);
74///
75/// container[sharedPtr1] = 1;
76/// container[sharedPtr2] = 2;
77/// @endcode
78/// Then we make sure that shared pointers can be used to perform lookup, and
79/// verify that the results are correct.
80/// @code
81/// Map::const_iterator iter = container.find(sharedPtr1);
82/// assert(container.end() != iter );
83/// assert(1 == iter->second);
84///
85/// iter = container.find(sharedPtr2);
86/// assert(container.end() != iter);
87/// assert(2 == iter->second);
88/// @endcode
89/// Finally, we simulate the accessing the container from another thread and
90/// perform lookup using weak pointers:
91/// @code
92/// iter = container.find(weakPtr1);
93/// assert(container.end() != iter );
94/// assert(1 == iter->second);
95///
96/// bsl::weak_ptr<int> weakPtr3(bsl::make_shared<int>(3));
97/// iter = container.find(weakPtr3);
98/// assert(container.end() == iter);
99/// @endcode
100/// @}
101/** @} */
102/** @} */
103
104/** @addtogroup bsl
105 * @{
106 */
107/** @addtogroup bslstl
108 * @{
109 */
110/** @addtogroup bslstl_ownerequal
111 * @{
112 */
113
114#include <bslscm_version.h>
115
116#include <bslstl_sharedptr.h>
117
118#include <bsls_keyword.h> // 'BSLS_KEYWORD_NOEXCEPT'
119
120namespace bsl {
121
122 // C++26 Compatibility
123
125
126 // TYPES
127
128 /// Type alias indicating this is a transparent comparator.
129 typedef void is_transparent;
130
131 // CREATORS
132 owner_equal() = default;
133 // Create an @ref owner_equal object.
134
135 owner_equal(const owner_equal& original) = default;
136 // Create an @ref owner_equal object. Note that as @ref owner_equal is an
137 // empty (stateless) type, this operation has no observable effect.
138
139 ~owner_equal() = default;
140 // Destroy this object.
141
142 // MANIPULATORS
144 // Assign to this object the value of the specified 'rhs' object, and
145 // return a reference providing modifiable access to this object. Note
146 // that as @ref owner_equal is an empty (stateless) type, this operation
147 // has no observable effect.
148
149 // ACCESSORS
150 template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
152 const shared_ptr<ELEMENT_TYPE_Y>& y) const
154 template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
156 const weak_ptr< ELEMENT_TYPE_Y>& y) const
158 template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
160 const shared_ptr<ELEMENT_TYPE_Y>& y) const
162
163 /// Return `true` if the address of the
164 /// `BloombergLP::bslma::SharedPtrRep` object used by the specified `x`
165 /// is equal to the address of the `BloombergLP::bslma::SharedPtrRep`
166 /// object used by the specified `y`, and `false` otherwise.
167 template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
169 const weak_ptr<ELEMENT_TYPE_Y>& y) const
171};
172
173// ============================================================================
174// INLINE DEFINITIONS
175// ============================================================================
176
177 // ------------------
178 // struct owner_equal
179 // ------------------
180
181template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
182inline
189
190template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
191inline
195{
196 return x.owner_equal(y);
197}
198
199template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
200inline
204{
205 return x.owner_equal(y);
206}
207
208template<class ELEMENT_TYPE_X, class ELEMENT_TYPE_Y>
209inline
213{
214 return x.owner_equal(y);
215}
216
217// ============================================================================
218// TYPE TRAITS
219// ============================================================================
220
221// Type traits for @ref owner_equal
222//: o @ref owner_equal is a stateless POD, trivially constructible and copyable.
223
224template <>
226
227template <>
229
230} // close namespace bsl
231
232#endif
233
234// ----------------------------------------------------------------------------
235// Copyright 2024 Bloomberg Finance L.P.
236//
237// Licensed under the Apache License, Version 2.0 (the "License");
238// you may not use this file except in compliance with the License.
239// You may obtain a copy of the License at
240//
241// http://www.apache.org/licenses/LICENSE-2.0
242//
243// Unless required by applicable law or agreed to in writing, software
244// distributed under the License is distributed on an "AS IS" BASIS,
245// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
246// See the License for the specific language governing permissions and
247// limitations under the License.
248// ----------------------------- END-OF-FILE ----------------------------------
249
250/** @} */
251/** @} */
252/** @} */
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
Definition bslstl_ownerequal.h:124
owner_equal & operator=(const owner_equal &rhs)=default
void is_transparent
Type alias indicating this is a transparent comparator.
Definition bslstl_ownerequal.h:129
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:183