CoriEngine
Loading...
Searching...
No Matches
AggregateStructUID.hpp
Go to the documentation of this file.
1#pragma once
2#include "StringHash.hpp"
3#include <boost/pfr.hpp>
4
5namespace Cori {
6 namespace Utility {
7 namespace Internal {
8 template<typename T>
9 consteval std::string_view GetTypeHelper() {
10 return __PRETTY_FUNCTION__;
11 }
12
13 template<typename T, typename Tuple, std::size_t... Indexes>
14 consteval auto GenerateTypeSignature(std::index_sequence<Indexes...>) {
15 constexpr size_t finalSize =
16 GetTypeHelper<T>().size() + 1 +
18 (sizeof...(Indexes) > 0 ? -1 : 0) + 1 + 1;
19
20 std::array<char, finalSize> signature{};
21
22 char* cursor = signature.data();
23 auto append = [&](const std::string_view sv) {
24 for (const char c : sv) {
25 *cursor++ = c;
26 }
27 };
28
29 append(GetTypeHelper<T>());
30 append("{");
31 ((append(GetTypeHelper<std::tuple_element_t<Indexes, Tuple>>()), append(Indexes == sizeof...(Indexes) - 1 ? "" : ",")), ...);
32 append("}");
33 *cursor = '\0';
34 return signature;
35 }
36
37 template<typename T>
38 consteval auto GetTypeSignature() {
39 static_assert(std::is_aggregate_v<T>, "Type must be an aggregate.");
40
41 using MemberTuple = decltype(boost::pfr::structure_to_tuple(std::declval<T>()));
42 constexpr size_t memberCount = std::tuple_size_v<MemberTuple>;
43
44 return Internal::GenerateTypeSignature<T, MemberTuple>(std::make_index_sequence<memberCount>());
45 }
46 }
47
55 template<typename T>
56 consteval uint64_t GetAggregateStructUID() {
57 constexpr auto signature = Internal::GetTypeSignature<T>();
58 const auto view = std::string_view(signature.data(), signature.size() - 1);
59 return fnv1a64(view.data(), view.size());
60 }
61
62 }
63}
consteval std::string_view GetTypeHelper()
consteval auto GenerateTypeSignature(std::index_sequence< Indexes... >)
consteval auto GetTypeSignature()
A namespace for utilities of different kinds.
Definition AABB.hpp:7
constexpr std::uint64_t fnv1a64(const char *str, const std::size_t len)
Computes the 64bit FNV-1a string hash.
Definition StringHash.hpp:9
consteval uint64_t GetAggregateStructUID()
Generates a stable, unique ID for an aggregate struct at compile-time.
Global engine namespace.