mdds
Loading...
Searching...
No Matches
cref_wrapper.hpp
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3// SPDX-FileCopyrightText: 2024 - 2025 Kohei Yoshida
4//
5// SPDX-License-Identifier: MIT
6
7#pragma once
8
9#include <functional>
10#include <type_traits>
11
12namespace mdds { namespace detail {
13
18template<typename T>
19class cref_wrapper
20{
21 std::reference_wrapper<std::add_const_t<T>> m_value;
22
23public:
24 cref_wrapper(const T& v) noexcept : m_value(std::cref(v))
25 {}
26
27 const T& get() const noexcept
28 {
29 return m_value;
30 }
31
32 bool operator==(const cref_wrapper& r) const
33 {
34 return get() == r.get();
35 }
36
37 bool operator!=(const cref_wrapper& r) const
38 {
39 return !operator==(r);
40 }
41
42 struct hash
43 {
44 std::size_t operator()(const cref_wrapper& v) const noexcept(noexcept(std::hash<T>{}(v.get())))
45 {
46 // NB: hash value must be based on the wrapped value, else two
47 // identical values located in different memory locations may end up
48 // in different buckets, and the lookup may fail.
49 return std::hash<T>{}(v.get());
50 }
51 };
52};
53
54}} // namespace mdds::detail
55
56/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition cref_wrapper.hpp:43