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
10 //----------------------------------------------------------------------
11 // Type meta data
12 //----------------------------------------------------------------------
13
14 struct type_info final {
15 private:
16 constexpr static size_t find_first_of(const char* data, size_t len, char toFind, size_t startPos = 0) {
17 for (size_t i = startPos; i < len; ++i) {
18 if (data[i] == toFind)
19 return i;
20 }
21 return size_t(-1);
22 }
23
24 constexpr static size_t find_last_of(const char* data, size_t len, char c, size_t startPos = size_t(-1)) {
25 const auto minValue = startPos <= len - 1 ? startPos : len - 1;
26 for (int64_t i = (int64_t)minValue; i >= 0; --i) {
27 if (data[i] == c)
28 return (size_t)i;
29 }
30 return size_t(-1);
31 }
32
33 public:
34 template <typename T>
35 GAIA_NODISCARD static constexpr const char* full_name() noexcept {
36 return GAIA_PRETTY_FUNCTION;
37 }
38
39 template <typename T>
40 GAIA_NODISCARD static constexpr auto name() noexcept {
41 // MSVC:
42 // const char* __cdecl ecs::type_info::name<struct ecs::EnfEntity>(void)
43 // -> ecs::EnfEntity
44 // Clang/Clang-cl/GCC:
45 // const ecs::type_info::name() [T = ecs::EnfEntity]
46 // -> ecs::EnfEntity
47
48 // Note:
49 // We don't want to use std::string_view here because it would only make it harder on compile-times.
50 // In fact, even if we did, we need to be afraid of compiler issues.
51 // Clang 8 and older wouldn't compile because their string_view::find_last_of doesn't work
52 // in constexpr context. Tested with and without LIBCPP
53 // https://stackoverflow.com/questions/56484834/constexpr-stdstring-viewfind-last-of-doesnt-work-on-clang-8-with-libstdc
54 // As a workaround find_first_of and find_last_of were implemented
55
56 size_t strLen = 0;
57 while (GAIA_PRETTY_FUNCTION[strLen] != '\0')
58 ++strLen;
59
60 std::span<const char> name{GAIA_PRETTY_FUNCTION, strLen};
61 const auto prefixPos = find_first_of(name.data(), name.size(), GAIA_PRETTY_FUNCTION_PREFIX);
62 const auto start = find_first_of(name.data(), name.size(), ' ', prefixPos + 1);
63 const auto end = find_last_of(name.data(), name.size(), GAIA_PRETTY_FUNCTION_SUFFIX);
64 return name.subspan(start + 1, end - start - 1);
65 }
66
67 template <typename T>
68 GAIA_NODISCARD static constexpr auto hash() noexcept {
69#if GAIA_COMPILER_MSVC && _MSC_VER <= 1916
70 GAIA_MSVC_WARNING_PUSH()
71 GAIA_MSVC_WARNING_DISABLE(4307)
72#endif
73
74 auto n = name<T>();
75 return core::calculate_hash64(n.data(), n.size());
76
77#if GAIA_COMPILER_MSVC && _MSC_VER <= 1916
78 GAIA_MSVC_WARNING_PUSH()
79#endif
80 }
81 };
82
83 } // namespace meta
84} // namespace gaia
Definition span_impl.h:99
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition type_info.h:14