BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslh_hashthreadid.h
Go to the documentation of this file.
1/// @file bslh_hashthreadid.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslh_hashthreadid.h -*-C++-*-
8#ifndef INCLUDED_BSLH_HASHTHREADID
9#define INCLUDED_BSLH_HASHTHREADID
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslh_hashthreadid bslh_hashthreadid
15/// @brief Provide `hashAppend` for `std::thread::id`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslh
19/// @{
20/// @addtogroup bslh_hashthreadid
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslh_hashthreadid-purpose"> Purpose</a>
25/// * <a href="#bslh_hashthreadid-description"> Description </a>
26/// * <a href="#bslh_hashthreadid-usage"> Usage </a>
27/// * <a href="#bslh_hashthreadid-example-1-hashing-a-thread-id"> Example 1: Hashing a Thread ID </a>
28///
29/// # Purpose {#bslh_hashthreadid-purpose}
30/// Provide `hashAppend` for `std::thread::id`.
31///
32/// # Description {#bslh_hashthreadid-description}
33/// This component provides a free function template,
34/// `bslh::hashAppend`, overloaded for the `std::thread::id` type.
35/// Including this function allows for `std::thread::id` (and types that contain
36/// members of that type) to be used as keys in BDE hashed containers.
37///
38/// ## Usage {#bslh_hashthreadid-usage}
39///
40///
41/// This section illustrates intended usage of this component.
42///
43/// ### Example 1: Hashing a Thread ID {#bslh_hashthreadid-example-1-hashing-a-thread-id}
44///
45///
46/// Suppose we have a struct with an `int` member and a `std::thread::id`
47/// member:
48/// @code
49/// struct S {
50/// int d_x;
51/// std::thread::id d_tid;
52/// };
53/// @endcode
54/// To implement `hashAppend` for this struct, we would feed its first member,
55/// then its second, to `hashAppend`. This is made possible by the fact that
56/// this component provides an overload that accepts `std::thread::id`.
57/// @code
58/// template <class t_HASH_ALG>
59/// void hashAppend(t_HASH_ALG& algorithm, const S& s)
60/// {
61/// using bslh::hashAppend;
62/// hashAppend(algorithm, s.d_x);
63/// hashAppend(algorithm, s.d_tid);
64/// }
65/// @endcode
66/// We can now use our new `hashAppend` overload:
67/// @code
68/// void usageExample()
69/// {
70/// using bslh::hashAppend;
71///
72/// bslh::DefaultHashAlgorithm h1;
73/// bslh::DefaultHashAlgorithm h2;
74///
75/// S s1;
76/// s.d_x = 1;
77/// s.d_tid = std::this_thread::get_id();
78/// hashAppend(h1, s1);
79///
80/// S s2 = s1;
81/// hashAppend(h2, s2);
82///
83/// assert(h1.computeHash() == h2.computeHash());
84/// }
85/// @endcode
86/// @}
87/** @} */
88/** @} */
89
90/** @addtogroup bsl
91 * @{
92 */
93/** @addtogroup bslh
94 * @{
95 */
96/** @addtogroup bslh_hashthreadid
97 * @{
98 */
99
100#include <bslscm_version.h>
101
102#include <bslh_hash.h>
103
104#include <bsls_libraryfeatures.h>
105
106#include <cstddef>
107#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
108# include <thread>
109#endif
110
111
112namespace bslh {
113#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
114// FREE FUNCTIONS
115
116/// Invoke `hashAppend` with the specified `algorithm` as the first argument
117/// and the result of applying an unspecified function to the specified `tid`
118/// as the second argument.
119template <class t_HASH_ALG>
120void hashAppend(t_HASH_ALG& algorithm, const std::thread::id& tid);
121
122/// This component-private struct is used to implement `hashAppend`.
123///
124/// See @ref bslh_hashthreadid
125struct HashThreadId_ImpUtil {
126 static std::size_t getHashInput(const std::thread::id& tid);
127};
128
129// ============================================================================
130// INLINE DEFINITIONS
131// ============================================================================
132
133// FREE FUNCTIONS
134
135template <class t_HASH_ALG>
136void hashAppend(t_HASH_ALG& algorithm, const std::thread::id& tid)
137{
138 hashAppend(algorithm, HashThreadId_ImpUtil::getHashInput(tid));
139}
140#endif // C++11
141} // close package namespace
142
143
144#endif
145
146// ----------------------------------------------------------------------------
147// Copyright 2025 Bloomberg Finance L.P.
148//
149// Licensed under the Apache License, Version 2.0 (the "License");
150// you may not use this file except in compliance with the License.
151// You may obtain a copy of the License at
152//
153// http://www.apache.org/licenses/LICENSE-2.0
154//
155// Unless required by applicable law or agreed to in writing, software
156// distributed under the License is distributed on an "AS IS" BASIS,
157// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
158// See the License for the specific language governing permissions and
159// limitations under the License.
160// ----------------------------- END-OF-FILE ----------------------------------
161
162/** @} */
163/** @} */
164/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslh_defaulthashalgorithm.h:339
bsl::enable_if<(bsl::is_integral< TYPE >::value||bsl::is_pointer< TYPE >::value||bsl::is_enum< TYPE >::value)&&!bsl::is_same< TYPE, bool >::value >::type hashAppend(HASH_ALGORITHM &hashAlg, TYPE input)
Definition bslh_hash.h:643