Derive-C
Loading...
Searching...
No Matches
default.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4#include <stddef.h>
5
8
9#include "id.h"
10#include "fnv1a.h"
11
14#if defined DC_GENERIC_KEYWORD_SUPPORTED
15 #define DC_DEFAULT_HASH(obj) \
16 _Generic(*(obj), \
17 int8_t: int8_t_hash_id, \
18 uint8_t: uint8_t_hash_id, \
19 int16_t: int16_t_hash_id, \
20 uint16_t: uint16_t_hash_id, \
21 int32_t: int32_t_hash_id, \
22 uint32_t: uint32_t_hash_id, \
23 int64_t: int64_t_hash_id, \
24 uint64_t: uint64_t_hash_id, \
25 char*: dc_fnv1a_str, \
26 const char*: dc_fnv1a_str_const)(obj)
27#else
28namespace dc::hash {
29
30 #include <type_traits>
31 #include <cstdint>
32
33template <class T> constexpr uint64_t default_hash_impl(T const* obj) {
34 using U = std::remove_cv_t<T>;
35
36 if constexpr (std::is_same_v<U, int8_t>)
37 return int8_t_hash_id(obj);
38 else if constexpr (std::is_same_v<U, uint8_t>)
39 return uint8_t_hash_id(obj);
40 else if constexpr (std::is_same_v<U, int16_t>)
41 return int16_t_hash_id(obj);
42 else if constexpr (std::is_same_v<U, uint16_t>)
43 return uint16_t_hash_id(obj);
44 else if constexpr (std::is_same_v<U, int32_t>)
45 return int32_t_hash_id(obj);
46 else if constexpr (std::is_same_v<U, uint32_t>)
47 return uint32_t_hash_id(obj);
48 else if constexpr (std::is_same_v<U, int64_t>)
49 return int64_t_hash_id(obj);
50 else if constexpr (std::is_same_v<U, uint64_t>)
51 return uint64_t_hash_id(obj);
52 else if constexpr (std::is_same_v<U, char*>)
53 return dc_fnv1a_str(obj);
54 else if constexpr (std::is_same_v<U, char const*>)
55 return dc_fnv1a_str_const(obj);
56 else {
57 // always-false, but dependent, without a helper variable
58 static_assert([]<class>() { return false; }.template operator()<U>(),
59 "DC_DEFAULT_HASH: unsupported type");
60 }
61}
62} // namespace dc::hash
63 #define DC_DEFAULT_HASH(obj) dc::hash::default_hash_impl(obj)
64#endif
static DC_PUBLIC uint64_t dc_fnv1a_str(char *const *s)
Definition fnv1a.h:24
static DC_PUBLIC uint64_t dc_fnv1a_str_const(const char *const *s)
Definition fnv1a.h:26