You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
1.2 KiB

#pragma once
#include <sha256_literal.h>
#include "util.hh"
namespace types {
// A very basic typeid: Using the constexpr __PRETTY_FUNCTION__ array slicing trick we used for `exopt::util::type_name<T>()`, we can extract the unmangled, de-aliased type name T, we can then hash that at comptime, and give it static storage: Therefore __PRETTY_FUNCTION__ will not be given storage, but the resulting (far smaller, but still *almost* unique to each type name) hash, will be.
using util::type_name;
template<typename T>
struct type_hash_of {
// Give *only* the computed hash static storage duration, not the type name.
constexpr static inline auto value = sha256::compute(util::type_name_literal<T>());
};
/// Returns a (semi) unique SHA256 hash representing the type `T`.
///
/// NOTE: This hash is given static storage duration, but nothing else used to calculate it is. Therefore executable bloat is not a concern for values obtained from this function.
template<typename T>
constexpr const auto& type_hash() noexcept {
constexpr const auto& value = type_hash_of<T>::value;
return value;
}
template<typename T>
constexpr inline auto& type_hash_v = type_hash_of<T>::value;
}