|
| 1 | +// |
| 2 | +// Created by manu343726 on 4/08/15. |
| 3 | +// |
| 4 | + |
| 5 | +#ifndef CTTI_HASH_HPP |
| 6 | +#define CTTI_HASH_HPP |
| 7 | + |
| 8 | +#include <functional> |
| 9 | +#include "detail/hash.hpp" |
| 10 | +#include "detail/pretty_function.hpp" |
| 11 | +#include "detail/string.hpp" |
| 12 | + |
| 13 | +namespace ctti |
| 14 | +{ |
| 15 | + struct type_id_t |
| 16 | + { |
| 17 | + constexpr type_id_t(const detail::string& name) : |
| 18 | + name_{name} |
| 19 | + {} |
| 20 | + |
| 21 | + constexpr detail::hash_t hash() const |
| 22 | + { |
| 23 | + return name_.hash(); |
| 24 | + } |
| 25 | + |
| 26 | + constexpr detail::string name() const |
| 27 | + { |
| 28 | + return name_; |
| 29 | + } |
| 30 | + |
| 31 | + friend constexpr bool operator==(const type_id_t& lhs, const type_id_t& rhs) |
| 32 | + { |
| 33 | + return lhs.hash() == rhs.hash(); |
| 34 | + } |
| 35 | + |
| 36 | + friend constexpr bool operator!=(const type_id_t& lhs, const type_id_t& rhs) |
| 37 | + { |
| 38 | + return !(lhs == rhs); |
| 39 | + } |
| 40 | + |
| 41 | + private: |
| 42 | + detail::string name_; |
| 43 | + }; |
| 44 | + |
| 45 | + namespace detail |
| 46 | + { |
| 47 | + template<char... Chars> |
| 48 | + struct ct_string |
| 49 | + { |
| 50 | + static constexpr const char value[] = {Chars..., '\0'}; |
| 51 | + }; |
| 52 | + |
| 53 | + template<char... Chars> |
| 54 | + constexpr const char ct_string<Chars...>::value[]; |
| 55 | + |
| 56 | + // The trick is to be able to discard both the preffix and suffix of |
| 57 | + // __PRETTY_FUNCTION__ string, getting T name only. |
| 58 | + // This is done in two steps: |
| 59 | + // |
| 60 | + // - type_id() function bellow gets T name length through CTTI_PRETTY_PRINT_LENGTH macro. |
| 61 | + // - Then __PRETTY_FUNCTION__ is computed again, using it to fill a variadic char string |
| 62 | + // template which will hold the T name. |
| 63 | + template<typename T, std::size_t... Is, std::size_t N> |
| 64 | + constexpr const char* parse_name(const char (&pretty_function)[N], std::index_sequence<Is...>) |
| 65 | + { |
| 66 | + constexpr std::size_t fn_length = 4 + sizeof("const char* parse_name(const char (&pretty_function)[N], std::index_sequence<Is...>)"); |
| 67 | + using str = ct_string<CTTI_PRETTY_FUNCTION[fn_length + Is]...>; |
| 68 | + |
| 69 | + return str::value; |
| 70 | + } |
| 71 | + |
| 72 | + // Since _() is a template, __PRETTY_FUNCTION__ string varies with template parameter, |
| 73 | + // pretty function is usually something like "hash_t _<T>() [with T = *type here*]". |
| 74 | + template<typename T> |
| 75 | + constexpr ctti::type_id_t type_id() |
| 76 | + { |
| 77 | + const auto str = detail::make_string(CTTI_PRETTY_FUNCTION); |
| 78 | + const auto name = str.template substr<CTTI_PRETTY_FUNCTION_BEGIN, CTTI_PRETTY_FUNCTION_END>(); |
| 79 | + |
| 80 | + return {name}; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + template<typename T> |
| 85 | + constexpr type_id_t type_id(T&&) |
| 86 | + { |
| 87 | + return detail::type_id<typename std::decay<T>::type>(); |
| 88 | + } |
| 89 | + |
| 90 | + template<typename T> |
| 91 | + constexpr type_id_t type_id() |
| 92 | + { |
| 93 | + return detail::type_id<typename std::decay<T>::type>(); |
| 94 | + } |
| 95 | + |
| 96 | + //static_assert(type_id<void>() == type_id<void>(), "ctti::type_id_t instances must be constant expressions"); |
| 97 | +} |
| 98 | + |
| 99 | +namespace std |
| 100 | +{ |
| 101 | + template<> |
| 102 | + struct hash<ctti::type_id_t> |
| 103 | + { |
| 104 | + std::size_t operator()(const ctti::type_id_t& id) const |
| 105 | + { |
| 106 | + return id.hash(); |
| 107 | + } |
| 108 | + }; |
| 109 | +} |
| 110 | + |
| 111 | +#endif //CTTI_HASH_HPP |
0 commit comments