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
19
namespace
Mcro::TypeInfo
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>
104
constexpr
FType
TTypeOf
=
FType
(
FType::TTag<T>
());
105
106
template
<
typename
Other>
107
constexpr
bool
FType::IsCompatibleWith
()
const
108
{
109
return
IsCompatibleWith
(
TTypeOf<Other>
);
110
}
111
}
Inheritance.h
size
size_t size(TArray< T, A > const &r)
Definition
Range.h:101
TextMacros.h
Use leading TEXT_ without parenthesis for Unreal compatible text literals.
TypeName.h
Convert types to string.
GetCompileTimeTypeName
consteval std::basic_string_view< TCHAR > GetCompileTimeTypeName()
Get a string view of the compile time typename.
Definition
TypeName.h:58
Mcro::Inheritance
Definition
Inheritance.h:19
Mcro::TypeInfo
Definition
TypeInfo.h:20
Mcro::TypeInfo::TTypeOf
constexpr FType TTypeOf
Definition
TypeInfo.h:104
Mcro::TypeName
Definition
TypeName.h:99
Mcro::TypeName::GetCompileTimeTypeHash
consteval FTypeHash GetCompileTimeTypeHash()
Definition
TypeName.h:106
Mcro::TypeName::FTypeHash
uint64 FTypeHash
Definition
TypeName.h:103
Mcro::TypeInfo::FType::TTag
Definition
TypeInfo.h:36
Mcro::TypeInfo::FType
Group together type info for identification. Can have an invalid state when no type is specified.
Definition
TypeInfo.h:32
Mcro::TypeInfo::FType::Hash
FTypeHash Hash
Definition
TypeInfo.h:60
Mcro::TypeInfo::FType::FType
constexpr FType(TTag< T > &&)
Definition
TypeInfo.h:39
Mcro::TypeInfo::FType::size
constexpr size_t size() const
Definition
TypeInfo.h:96
Mcro::TypeInfo::FType::ToString
constexpr FStringView ToString() const
Definition
TypeInfo.h:62
Mcro::TypeInfo::FType::ToStringCopy
FORCEINLINE FString ToStringCopy() const
Definition
TypeInfo.h:63
Mcro::TypeInfo::FType::Name
FStringView Name
Definition
TypeInfo.h:59
Mcro::TypeInfo::FType::FType
constexpr FType()
Definition
TypeInfo.h:57
Mcro::TypeInfo::FType::begin
constexpr const FTypeHash * begin() const
Definition
TypeInfo.h:94
Mcro::TypeInfo::FType::IsCompatibleWith
constexpr bool IsCompatibleWith() const
check to see if pointers of this and the other types are safe to cast between
Definition
TypeInfo.h:107
Mcro::TypeInfo::FType::GetTypeHash
friend constexpr uint32 GetTypeHash(FType const &self)
Definition
TypeInfo.h:89
Mcro::TypeInfo::FType::end
constexpr const FTypeHash * end() const
Definition
TypeInfo.h:95
Mcro::TypeInfo::FType::IsValid
constexpr bool IsValid() const
Definition
TypeInfo.h:65
Mcro::TypeInfo::FType::IsCompatibleWith
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
Mcro
Public
Mcro
TypeInfo.h
Generated by
1.12.0