MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
TypeInfo.h
Go to the documentation of this file.
1/** @noop License Comment
2 * @file
3 * @copyright
4 * This Source Code is subject to the terms of the Mozilla Public License, v2.0.
5 * If a copy of the MPL was not distributed with this file You can obtain one at
6 * https://mozilla.org/MPL/2.0/
7 *
8 * @author David Mórász
9 * @date 2025
10 */
11
12#pragma once
13
14#include "CoreMinimal.h"
15#include "Mcro/Inheritance.h"
16#include "Mcro/TextMacros.h"
17#include "Mcro/TypeName.h"
18
20{
21 using namespace Mcro::TypeName;
22 using namespace Mcro::Inheritance;
23
24 /**
25 * @brief
26 * Group together type info for identification. Can have an invalid state when no type is specified.
27 *
28 * If given type also explicitly list its inheritance (through `TInherit` for example) base types are also stored
29 * for type safety checks.
30 */
31 struct MCRO_API FType
32 {
33 static constexpr int32 MaxBaseCount = 64;
34
35 template <typename T>
36 struct TTag {};
37
38 template <typename T>
39 constexpr FType(TTag<T>&&)
40 : Name(
41 GetCompileTimeTypeName<std::decay_t<T>>().data(),
42 GetCompileTimeTypeName<std::decay_t<T>>().size()
43 )
44 , Hash(GetCompileTimeTypeHash<std::decay_t<T>>())
45 {
46 if constexpr (CHasBases<T>)
47 {
48 ForEachExplicitBase<T>([this] <typename Base> ()
49 {
50 if (BaseCount >= MaxBaseCount) return;
51 BaseTypeHashes[BaseCount] = TTypeHash<Base>;
52 ++BaseCount;
53 });
54 }
55 }
56
57 constexpr FType() {}
58
59 FStringView Name;
60 FTypeHash Hash = 0;
61
62 constexpr FStringView ToString() const { return Name; }
63 FORCEINLINE FString ToStringCopy() const { return FString(Name); }
64
65 constexpr bool IsValid() const { return Hash != 0; }
66 constexpr operator bool() const { return IsValid(); }
67
68 /** @brief check to see if pointers of this and the other types are safe to cast between */
69 constexpr bool IsCompatibleWith(FType const& other) const
70 {
71 if (Hash == other.Hash) return true;
72
73 for (const FTypeHash base : other)
74 if (base == Hash) return true;
75
76 for (const FTypeHash base : *this)
77 if (base == other.Hash) return true;
78
79 return false;
80 }
81
82 /** @brief check to see if pointers of this and the other types are safe to cast between */
83 template <typename Other>
84 constexpr bool IsCompatibleWith() const;
85
86 friend constexpr bool operator == (FType const& left, FType const& right) { return left.Hash == right.Hash; }
87 friend constexpr bool operator != (FType const& left, FType const& right) { return left.Hash != right.Hash; }
88
89 friend constexpr uint32 GetTypeHash(FType const& self)
90 {
91 return static_cast<uint32>(self.Hash) ^ static_cast<uint32>(self.Hash >> 32);
92 }
93
94 constexpr const FTypeHash* begin() const { return BaseTypeHashes; }
95 constexpr const FTypeHash* end() const { return BaseTypeHashes + BaseCount; }
96 constexpr size_t size() const { return BaseCount; }
97
98 private:
99 FTypeHash BaseTypeHashes[MaxBaseCount] {};
100 int32 BaseCount = 0;
101 };
102
103 template <typename T>
105
106 template <typename Other>
107 constexpr bool FType::IsCompatibleWith() const
108 {
110 }
111}
size_t size(TArray< T, A > const &r)
Definition Range.h:101
Use leading TEXT_ without parenthesis for Unreal compatible text literals.
Convert types to string.
consteval std::basic_string_view< TCHAR > GetCompileTimeTypeName()
Get a string view of the compile time typename.
Definition TypeName.h:58
constexpr FType TTypeOf
Definition TypeInfo.h:104
consteval FTypeHash GetCompileTimeTypeHash()
Definition TypeName.h:106
uint64 FTypeHash
Definition TypeName.h:103
Group together type info for identification. Can have an invalid state when no type is specified.
Definition TypeInfo.h:32
constexpr FType(TTag< T > &&)
Definition TypeInfo.h:39
constexpr size_t size() const
Definition TypeInfo.h:96
constexpr FStringView ToString() const
Definition TypeInfo.h:62
FORCEINLINE FString ToStringCopy() const
Definition TypeInfo.h:63
FStringView Name
Definition TypeInfo.h:59
constexpr FType()
Definition TypeInfo.h:57
constexpr const FTypeHash * begin() const
Definition TypeInfo.h:94
constexpr bool IsCompatibleWith() const
check to see if pointers of this and the other types are safe to cast between
Definition TypeInfo.h:107
friend constexpr uint32 GetTypeHash(FType const &self)
Definition TypeInfo.h:89
constexpr const FTypeHash * end() const
Definition TypeInfo.h:95
constexpr bool IsValid() const
Definition TypeInfo.h:65
constexpr bool IsCompatibleWith(FType const &other) const
check to see if pointers of this and the other types are safe to cast between
Definition TypeInfo.h:69