tagval 0.2.0
Modern C++23 header-only library of tagged values (open/closed enumerations with metadata)
Loading...
Searching...
No Matches
hash.hpp
Go to the documentation of this file.
1#pragma once
2
10
11#include <tagval/base.hpp>
12
13#include <concepts>
14#include <cstddef>
15#include <functional>
16#include <string_view>
17
18namespace tagval::detail {
19
20[[nodiscard]] inline std::size_t hash_combine(std::size_t seed, const std::size_t v) noexcept {
21 // boost::hash_combine constants — well-distributed mixing for 64-bit seeds.
22 seed ^= v + 0x9e3779b97f4a7c15ULL + (seed << 6) + (seed >> 2);
23 return seed;
24}
25
26} // namespace tagval::detail
27
28template <typename T>
29 requires std::derived_from<T, tagval::detail::TagValBaseTag>
30struct std::hash<T> { // NOLINT(cert-dcl58-cpp)
31 [[nodiscard]] std::size_t operator()(const T& v) const noexcept {
32 std::size_t s = std::hash<std::string_view>{}(T::kind_id());
33 s = tagval::detail::hash_combine(s, std::hash<std::string_view>{}(v.code()));
34 return s;
35 }
36};
CRTP HandleBase used by ClosedEnded and OpenEnded.
Definition base.hpp:26
std::size_t hash_combine(std::size_t seed, const std::size_t v) noexcept
Definition hash.hpp:20
constexpr TagValDescriptor compute_descriptor() noexcept
Definition base.hpp:38
std::size_t operator()(const T &v) const noexcept
Definition hash.hpp:31