BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslh_hashoptional.h
Go to the documentation of this file.
1/// @file bslh_hashoptional.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslh_hashoptional.h -*-C++-*-
8#ifndef INCLUDED_BSLH_HASHOPTIONAL
9#define INCLUDED_BSLH_HASHOPTIONAL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslh_hashoptional bslh_hashoptional
15/// @brief Provide `hashAppend` for `std::optional`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslh
19/// @{
20/// @addtogroup bslh_hashoptional
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslh_hashoptional-purpose"> Purpose</a>
25/// * <a href="#bslh_hashoptional-description"> Description </a>
26/// * <a href="#bslh_hashoptional-usage"> Usage </a>
27/// * <a href="#bslh_hashoptional-example-1-hashing-an-optional-boolean-value"> Example 1: Hashing an optional Boolean value </a>
28///
29/// # Purpose {#bslh_hashoptional-purpose}
30/// Provide `hashAppend` for `std::optional`.
31///
32/// # Description {#bslh_hashoptional-description}
33/// This component provides a free function template,
34/// `bslh::hashAppend`, overloaded for the `std::optional` class template.
35/// Including this function allows for `std::optional` types (and types that
36/// contain them) to be used as keys in BDE hashed containers.
37///
38/// Note that use of this component requires that the language standard be 2017
39/// or later, as that is when `std::optional` first appears.
40///
41/// ## Usage {#bslh_hashoptional-usage}
42///
43///
44/// This section illustrates intended usage of this component.
45///
46/// ### Example 1: Hashing an optional Boolean value {#bslh_hashoptional-example-1-hashing-an-optional-boolean-value}
47///
48///
49/// Suppose we want to maintain a boolean condition as either true, false, or
50/// unspecified, and have it fit within the BDE hash framework. We can use
51/// `std::optional<bool>` for this, and demonstrate that such a value can be
52/// correctly hashed.
53///
54/// First, we set up three such optional values to represent the three possible
55/// states we wish to represent.
56/// @code
57/// std::optional<bool> optionalTrue = true;
58/// std::optional<bool> optionalFalse = false;
59/// std::optional<bool> optionalUnset;
60/// @endcode
61/// Then, we create a hashing object.
62/// @code
63/// bslh::Hash<> hasher;
64/// @endcode
65/// Next, we hash each of our values.
66/// @code
67/// size_t optionalTrueHash = hasher(optionalTrue);
68/// size_t optionalFalseHash = hasher(optionalFalse);
69/// size_t optionalUnsetHash = hasher(optionalUnset);
70/// @endcode
71/// Then we hash the underlying values.
72/// @code
73/// size_t expectedTrueHash = hasher(true);
74/// size_t expectedFalseHash = hasher(false);
75/// @endcode
76/// Finally, we verify that the `std::optional` hasher produces the same results
77/// as the underlying hashers. For the disengaged hash, we will just check that
78/// the value differs from either engaged value.
79/// @code
80/// assert(expectedTrueHash == optionalTrueHash);
81/// assert(expectedFalseHash == optionalFalseHash);
82/// assert(expectedTrueHash != optionalUnsetHash);
83/// assert(expectedFalseHash != optionalUnsetHash);
84/// @endcode
85/// @}
86/** @} */
87/** @} */
88
89/** @addtogroup bsl
90 * @{
91 */
92/** @addtogroup bslh
93 * @{
94 */
95/** @addtogroup bslh_hashoptional
96 * @{
97 */
98
99#include <bslscm_version.h>
100
101#include <bslh_hash.h>
102
103#include <bslmf_removeconst.h>
104
105#include <bsls_libraryfeatures.h>
106#include <bsls_platform.h>
107
108#include <stddef.h> // for 'size_t'
109#include <string.h> // for 'strlen'
110
111#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
112
113#include <optional>
114
115
116namespace bslh {
117
118// FREE FUNCTIONS
119
120/// If the specified `input` is engaged, pass its contained value into the
121/// specified `algorithm` to be combined into the internal state of the
122/// algorithm that is used to produce the resulting hash value. Otherwise,
123/// pass an arbitrary `size_t` value instead. Note that this behavior (for
124/// engaged values) is required by the C++ Standard.
125template <class HASH_ALGORITHM, class TYPE>
126inline
127void
128hashAppend(HASH_ALGORITHM& algorithm, const std::optional<TYPE> &input)
129{
130 if (input) {
131 hashAppend(algorithm, input.value());
132 } else {
133 size_t disengaged = 0xB01DFACE;
134 hashAppend(algorithm, disengaged);
135 }
136}
137
138} // close package namespace
139
140
141#endif // BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
142
143#endif
144
145// ----------------------------------------------------------------------------
146// Copyright 2020 Bloomberg Finance L.P.
147//
148// Licensed under the Apache License, Version 2.0 (the "License");
149// you may not use this file except in compliance with the License.
150// You may obtain a copy of the License at
151//
152// http://www.apache.org/licenses/LICENSE-2.0
153//
154// Unless required by applicable law or agreed to in writing, software
155// distributed under the License is distributed on an "AS IS" BASIS,
156// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
157// See the License for the specific language governing permissions and
158// limitations under the License.
159// ----------------------------- END-OF-FILE ----------------------------------
160
161/** @} */
162/** @} */
163/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
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:638