Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
type_info.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include "gaia/core/hashing_policy.h"
5#include "gaia/core/span.h"
6
7namespace gaia {
8 namespace meta {
9
11 template <typename...>
12 class type_group {
13 inline static uint32_t s_identifier{};
14
15 public:
16 template <typename... Type>
17 inline static const uint32_t id = s_identifier++;
18 };
19
20 template <>
21 class type_group<void>;
22
23 //----------------------------------------------------------------------
24 // Type meta data
25 //----------------------------------------------------------------------
26
27 struct type_info final {
28 private:
29 constexpr static size_t find_first_of(const char* data, size_t len, char toFind, size_t startPos = 0) {
30 for (size_t i = startPos; i < len; ++i) {
31 if (data[i] == toFind)
32 return i;
33 }
34 return size_t(-1);
35 }
36
37 constexpr static size_t find_last_of(const char* data, size_t len, char c, size_t startPos = size_t(-1)) {
38 const auto minValue = startPos <= len - 1 ? startPos : len - 1;
39 for (int64_t i = (int64_t)minValue; i >= 0; --i) {
40 if (data[i] == c)
41 return (size_t)i;
42 }
43 return size_t(-1);
44 }
45
46 public:
47 template <typename T>
48 static uint32_t id() noexcept {
50 }
51
52 template <typename T>
53 GAIA_NODISCARD static constexpr const char* full_name() noexcept {
54 return GAIA_PRETTY_FUNCTION;
55 }
56
57 template <typename T>
58 GAIA_NODISCARD static constexpr auto name() noexcept {
59 // MSVC:
60 // const char* __cdecl ecs::ComponentInfo::name<struct ecs::EnfEntity>(void)
61 // -> ecs::EnfEntity
62 // Clang/Clang-cl/GCC:
63 // const ecs::ComponentInfo::name() [T = ecs::EnfEntity]
64 // -> ecs::EnfEntity
65
66 // Note:
67 // We don't want to use std::string_view here because it would only make it harder on compile-times.
68 // In fact, even if we did, we need to be afraid of compiler issues.
69 // Clang 8 and older wouldn't compile because their string_view::find_last_of doesn't work
70 // in constexpr context. Tested with and without LIBCPP
71 // https://stackoverflow.com/questions/56484834/constexpr-stdstring-viewfind-last-of-doesnt-work-on-clang-8-with-libstdc
72 // As a workaround find_first_of and find_last_of were implemented
73
74 size_t strLen = 0;
75 while (GAIA_PRETTY_FUNCTION[strLen] != '\0')
76 ++strLen;
77
78 std::span<const char> name{GAIA_PRETTY_FUNCTION, strLen};
79 const auto prefixPos = find_first_of(name.data(), name.size(), GAIA_PRETTY_FUNCTION_PREFIX);
80 const auto start = find_first_of(name.data(), name.size(), ' ', prefixPos + 1);
81 const auto end = find_last_of(name.data(), name.size(), GAIA_PRETTY_FUNCTION_SUFFIX);
82 return name.subspan(start + 1, end - start - 1);
83 }
84
85 template <typename T>
86 GAIA_NODISCARD static constexpr auto hash() noexcept {
87#if GAIA_COMPILER_MSVC && _MSC_VER <= 1916
88 GAIA_MSVC_WARNING_PUSH()
89 GAIA_MSVC_WARNING_DISABLE(4307)
90#endif
91
92 auto n = name<T>();
93 return core::calculate_hash64(n.data(), n.size());
94
95#if GAIA_COMPILER_MSVC && _MSC_VER <= 1916
96 GAIA_MSVC_WARNING_PUSH()
97#endif
98 }
99 };
100
101 } // namespace meta
102} // namespace gaia
Definition span_impl.h:99
Provides statically generated unique identifier for a given group of types.
Definition type_info.h:12
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition type_info.h:27