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