BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_ownerhash.h
Go to the documentation of this file.
1/// @file bslstl_ownerhash.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_ownerhash.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_OWNERHASH
9#define INCLUDED_BSLSTL_OWNERHASH
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id$ $CSID$")
13
14/// @defgroup bslstl_ownerhash bslstl_ownerhash
15/// @brief Provide an ownership hash functor for shared and weak pointers.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_ownerhash
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_ownerhash-purpose"> Purpose</a>
25/// * <a href="#bslstl_ownerhash-classes"> Classes </a>
26/// * <a href="#bslstl_ownerhash-canonical-header"> Canonical Header </a>
27/// * <a href="#bslstl_ownerhash-description"> Description </a>
28/// * <a href="#bslstl_ownerhash-usage"> Usage </a>
29/// * <a href="#bslstl_ownerhash-example-1-basic-use-of-ref-owner_hash"> Example 1: Basic Use of @ref owner_hash </a>
30///
31/// # Purpose {#bslstl_ownerhash-purpose}
32/// Provide an ownership hash functor for shared and weak pointers.
33///
34/// # Classes {#bslstl_ownerhash-classes}
35///
36/// - bsl::owner_hash: owner hash functor for `shared_ptr` and `weak_ptr`
37///
38/// # Canonical Header {#bslstl_ownerhash-canonical-header}
39/// bsl_memory.h
40///
41/// @see bslstl_sharedptr
42///
43/// # Description {#bslstl_ownerhash-description}
44/// This component provides the C++26 standard functor,
45/// `bsl::owner_hash`, that calculates the hash of a smart pointer object by the
46/// address of their `bslma::SharedPtrRep` data. Note that this class is an
47/// empty POD type.
48///
49/// ## Usage {#bslstl_ownerhash-usage}
50///
51///
52/// This section illustrates intended use of this component.
53///
54/// ### Example 1: Basic Use of @ref owner_hash {#bslstl_ownerhash-example-1-basic-use-of-ref-owner_hash}
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 define an owner-based equality predicate, that is required by the
63/// `bsl::unordered_map` along with this owner-based hash.
64/// @code
65/// struct TestOwnerEqual {
66/// // TYPES
67/// typedef void is_transparent;
68///
69/// template <class T1, class T2>
70/// bool operator()(const bsl::shared_ptr<T1>& lhs,
71/// const bsl::shared_ptr<T2>& rhs) const
72/// // For the specified 'lhs' and 'rhs', return the result of
73/// // 'lhs.owner_equal(rhs)'
74/// {
75/// return lhs.owner_equal(rhs);
76/// }
77///
78/// template <class T1, class T2>
79/// bool operator()(const bsl::shared_ptr<T1>& lhs,
80/// const bsl::weak_ptr<T2>& rhs) const
81/// // For the specified 'lhs' and 'rhs', return the result of
82/// // 'lhs.owner_equal(rhs)'
83/// {
84/// return lhs.owner_equal(rhs);
85/// }
86///
87/// template <class T1, class T2>
88/// bool operator()(const bsl::weak_ptr<T1>& lhs,
89/// const bsl::shared_ptr<T2>& rhs) const
90/// // For the specified 'lhs' and 'rhs', return the result of
91/// // 'lhs.owner_equal(rhs)'
92/// {
93/// return lhs.owner_equal(rhs);
94/// }
95///
96/// template <class T1, class T2>
97/// bool operator()(const bsl::weak_ptr<T1>& lhs,
98/// const bsl::weak_ptr<T2>& rhs) const
99/// // For the specified 'lhs' and 'rhs', return the result of
100/// // 'lhs.owner_equal(rhs)'
101/// {
102/// return lhs.owner_equal(rhs);
103/// }
104/// };
105/// @endcode
106/// Note that this struct is defined only to avoid cycle dependencies between
107/// BDE components. In real code for these purposes it is recommended to use
108/// `bsl::owner_equal`.
109///
110/// Then, we create a container and populate it:
111/// @code
112/// typedef bsl::unordered_map<
113/// bsl::shared_ptr<int>,
114/// int,
115/// bsl::owner_hash,
116/// TestOwnerEqual> Map;
117///
118/// Map container;
119///
120/// bsl::shared_ptr<int> sharedPtr1 = bsl::make_shared<int>(1);
121/// bsl::shared_ptr<int> sharedPtr2 = bsl::make_shared<int>(2);
122/// bsl::weak_ptr<int> weakPtr1(sharedPtr1);
123///
124/// container[sharedPtr1] = 1;
125/// container[sharedPtr2] = 2;
126/// @endcode
127/// Now, we make sure, that shared pointers can be used to perform lookup:
128/// @code
129/// Map::const_iterator iter = container.find(sharedPtr1);
130/// assert(container.end() != iter );
131/// assert(1 == iter->second);
132///
133/// iter = container.find(sharedPtr2);
134/// assert(container.end() != iter);
135/// assert(2 == iter->second);
136/// @endcode
137/// Finally, we simulate the situation of accessing the container from another
138/// thread and perform lookup using weak pointers:
139/// @code
140/// iter = container.find(weakPtr1);
141/// assert(container.end() != iter );
142/// assert(1 == iter->second);
143///
144/// bsl::weak_ptr<int> weakPtr3(bsl::make_shared<int>(3));
145/// iter = container.find(weakPtr3);
146/// assert(container.end() == iter);
147/// @endcode
148/// @}
149/** @} */
150/** @} */
151
152/** @addtogroup bsl
153 * @{
154 */
155/** @addtogroup bslstl
156 * @{
157 */
158/** @addtogroup bslstl_ownerhash
159 * @{
160 */
161
162#include <bslscm_version.h>
163
164#include <bslstl_sharedptr.h>
165
166#include <bsls_keyword.h> // 'BSLS_KEYWORD_NOEXCEPT'
167
168#include <stddef.h> // 'size_t'
169
170namespace bsl {
171
172 // C++26 Compatibility
173
175
176 // TYPES
177
178 /// Type alias indicating this is a transparent hash functor.
179 typedef void is_transparent;
180
181 // CREATORS
182 owner_hash() = default;
183 // Create an @ref owner_hash object.
184
185 owner_hash(const owner_hash& original) = default;
186 // Create an @ref owner_hash object. Note that as @ref owner_hash is an
187 // empty (stateless) type, this operation has no observable effect.
188
189 ~owner_hash() = default;
190 // Destroy this object.
191
192 // MANIPULATORS
193 owner_hash& operator=(const owner_hash& rhs) = default;
194 // Assign to this object the value of the specified 'rhs' object, and
195 // return a reference providing modifiable access to this object. Note
196 // that as @ref owner_hash is an empty (stateless) type, this operation
197 // has no observable effect.
198
199 // ACCESSORS
200 template <class ELEMENT_TYPE>
201 size_t operator()(const shared_ptr<ELEMENT_TYPE>& x) const
203
204 /// Return the result of calling `owner_hash()` on the specified `x`.
205 ///
206 /// \note Note that for any object `y` where `x.owner_equal(y)` is true,
207 /// `x.owner_hash() == y.owner_hash()` is true.
208 template <class ELEMENT_TYPE>
209 size_t operator()(const weak_ptr< ELEMENT_TYPE>& x) const
211};
212
213// ============================================================================
214// INLINE DEFINITIONS
215// ============================================================================
216
217 // -----------------
218 // struct owner_hash
219 // -----------------
220
221template <class ELEMENT_TYPE>
222inline
225{
226 return x.owner_hash();
227}
228
229template <class ELEMENT_TYPE>
230inline
233{
234 return x.owner_hash();
235}
236
237// ============================================================================
238// TYPE TRAITS
239// ============================================================================
240
241// Type traits for @ref owner_hash
242//: o @ref owner_hash is a stateless POD, trivially constructible and copyable.
243
244template<>
248
249template<>
253
254} // close namespace bsl
255
256#endif
257
258// ----------------------------------------------------------------------------
259// Copyright 2024 Bloomberg Finance L.P.
260//
261// Licensed under the Apache License, Version 2.0 (the "License");
262// you may not use this file except in compliance with the License.
263// You may obtain a copy of the License at
264//
265// http://www.apache.org/licenses/LICENSE-2.0
266//
267// Unless required by applicable law or agreed to in writing, software
268// distributed under the License is distributed on an "AS IS" BASIS,
269// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
270// See the License for the specific language governing permissions and
271// limitations under the License.
272// ----------------------------- END-OF-FILE ----------------------------------
273
274/** @} */
275/** @} */
276/** @} */
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_ownerhash.h:174
owner_hash(const owner_hash &original)=default
void is_transparent
Type alias indicating this is a transparent hash functor.
Definition bslstl_ownerhash.h:179
~owner_hash()=default
size_t operator()(const shared_ptr< ELEMENT_TYPE > &x) const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_ownerhash.h:223
owner_hash()=default
owner_hash & operator=(const owner_hash &rhs)=default