forked from microsoft/cppwinrt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_std_hash.h
More file actions
56 lines (49 loc) · 1.62 KB
/
base_std_hash.h
File metadata and controls
56 lines (49 loc) · 1.62 KB
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
namespace winrt::impl
{
inline size_t hash_data(void const* ptr, size_t const bytes) noexcept
{
#ifdef _WIN64
constexpr size_t fnv_offset_basis = 14695981039346656037ULL;
constexpr size_t fnv_prime = 1099511628211ULL;
#else
constexpr size_t fnv_offset_basis = 2166136261U;
constexpr size_t fnv_prime = 16777619U;
#endif
size_t result = fnv_offset_basis;
uint8_t const* const buffer = static_cast<uint8_t const*>(ptr);
for (size_t next = 0; next < bytes; ++next)
{
result ^= buffer[next];
result *= fnv_prime;
}
return result;
}
struct hash_base
{
size_t operator()(Windows::Foundation::IUnknown const& value) const noexcept
{
void* const abi_value = get_abi(value.try_as<Windows::Foundation::IUnknown>());
return std::hash<void*>{}(abi_value);
}
};
}
namespace std
{
template<> struct hash<winrt::hstring>
{
size_t operator()(winrt::hstring const& value) const noexcept
{
return std::hash<std::wstring_view>{}(value);
}
};
template<> struct hash<winrt::Windows::Foundation::IUnknown> : winrt::impl::hash_base {};
template<> struct hash<winrt::Windows::Foundation::IInspectable> : winrt::impl::hash_base {};
template<> struct hash<winrt::Windows::Foundation::IActivationFactory> : winrt::impl::hash_base {};
template<> struct hash<winrt::guid>
{
size_t operator()(winrt::guid const& value) const noexcept
{
return winrt::impl::hash_data(&value, sizeof(value));
}
};
}