forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigester.hh
63 lines (49 loc) · 1.61 KB
/
digester.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* Copyright (C) 2018-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include "utils/digest_algorithm.hh"
#include "utils/hashing.hh"
#include "utils/xx_hasher.hh"
#include <type_traits>
#include <variant>
namespace query {
struct noop_hasher {
void update(const char* ptr, size_t length) noexcept { }
std::array<uint8_t, 16> finalize_array() { return std::array<uint8_t, 16>(); };
};
class digester final {
std::variant<noop_hasher, xx_hasher> _impl;
public:
explicit digester(digest_algorithm algo) {
switch (algo) {
case digest_algorithm::xxHash:
_impl = xx_hasher();
break;
case digest_algorithm ::none:
_impl = noop_hasher();
break;
}
}
template<typename T, typename... Args>
void feed_hash(const T& value, Args&&... args) {
// FIXME uncomment the noexcept marking once clang bug 50994 is fixed or gcc compilation is turned on
std::visit([&] (auto& hasher) /* noexcept(noexcept(::feed_hash(hasher, value, args...))) */ -> void {
::feed_hash(hasher, value, std::forward<Args>(args)...);
}, _impl);
};
std::array<uint8_t, 16> finalize_array() {
return std::visit([&] (auto& hasher) {
return hasher.finalize_array();
}, _impl);
}
};
using default_hasher = xx_hasher;
template<typename Hasher>
using using_hash_of_hash = std::negation<std::is_same<Hasher, noop_hasher>>;
template<typename Hasher>
inline constexpr bool using_hash_of_hash_v = using_hash_of_hash<Hasher>::value;
}