-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUnrealTypeTraits.h
151 lines (134 loc) · 4.57 KB
/
UnrealTypeTraits.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#pragma once
#include "IsEnum.h"
#include "IsPointer.h"
#include "IsArithmetic.h"
#include "AndOrNot.h"
#include "IsPODType.h"
#include "IsTriviallyCopyConstructible.h"
template <typename T, bool TypeIsSmall>
struct TCallTraitsParamTypeHelper
{
typedef const T& ParamType;
typedef const T& ConstParamType;
};
template <typename T>
struct TCallTraitsParamTypeHelper<T, true>
{
typedef const T ParamType;
typedef const T ConstParamType;
};
template <typename T>
struct TCallTraitsParamTypeHelper<T*, true>
{
typedef T* ParamType;
typedef const T* ConstParamType;
};
template <typename T>
struct TCallTraitsBase
{
private:
enum { PassByValue = TOr<TAndValue<(sizeof(T) <= sizeof(void*)), TIsPODType<T>>, TIsArithmetic<T>, TIsPointer<T>>::Value };
public:
typedef T ValueType;
typedef T& Reference;
typedef const T& ConstReference;
typedef typename TCallTraitsParamTypeHelper<T, PassByValue>::ParamType ParamType;
typedef typename TCallTraitsParamTypeHelper<T, PassByValue>::ConstParamType ConstPointerType;
};
/**
* TCallTraits
*/
template <typename T>
struct TCallTraits : public TCallTraitsBase<T> {};
// Fix reference-to-reference problems.
template <typename T>
struct TCallTraits<T&>
{
typedef T& ValueType;
typedef T& Reference;
typedef const T& ConstReference;
typedef T& ParamType;
typedef T& ConstPointerType;
};
// Array types
template <typename T, size_t N>
struct TCallTraits<T[N]>
{
private:
typedef T ArrayType[N];
public:
typedef const T* ValueType;
typedef ArrayType& Reference;
typedef const ArrayType& ConstReference;
typedef const T* const ParamType;
typedef const T* const ConstPointerType;
};
// const array types
template <typename T, size_t N>
struct TCallTraits<const T[N]>
{
private:
typedef const T ArrayType[N];
public:
typedef const T* ValueType;
typedef ArrayType& Reference;
typedef const ArrayType& ConstReference;
typedef const T* const ParamType;
typedef const T* const ConstPointerType;
};
/*-----------------------------------------------------------------------------
Traits for our particular container classes
-----------------------------------------------------------------------------*/
/**
* Helper for array traits. Provides a common base to more easily refine a portion of the traits
* when specializing. Mainly used by MemoryOps.h which is used by the contiguous storage containers like TArray.
*/
template<typename T>
struct TTypeTraitsBase
{
typedef typename TCallTraits<T>::ParamType ConstInitType;
typedef typename TCallTraits<T>::ConstPointerType ConstPointerType;
// There's no good way of detecting this so we'll just assume it to be true for certain known types and expect
// users to customize it for their custom types.
enum { IsBytewiseComparable = TOr<TIsEnum<T>, TIsArithmetic<T>, TIsPointer<T>>::Value };
};
/**
* Traits for types.
*/
template<typename T> struct TTypeTraits : public TTypeTraitsBase<T> {};
template <typename T, typename Arg>
struct TIsBitwiseConstructible
{
// Assume no bitwise construction in general
enum { Value = false };
};
template <typename T>
struct TIsBitwiseConstructible<T, T>
{
// Ts can always be bitwise constructed from itself if it is trivially copyable.
enum { Value = TIsTriviallyCopyConstructible<T>::Value };
};
template <typename T, typename U>
struct TIsBitwiseConstructible<const T, U> : TIsBitwiseConstructible<T, U>
{
// Constructing a const T is the same as constructing a T
};
// Const pointers can be bitwise constructed from non-const pointers.
// This is not true for pointer conversions in general, e.g. where an offset may need to be applied in the case
// of multiple inheritance, but there is no way of detecting that at compile-time.
template <typename T>
struct TIsBitwiseConstructible<const T*, T*>
{
// Constructing a const T is the same as constructing a T
enum { Value = true };
};
// Unsigned types can be bitwise converted to their signed equivalents, and vice versa.
// (assuming two's-complement, which we are)
template <> struct TIsBitwiseConstructible<uint8, char> { enum { Value = true }; };
template <> struct TIsBitwiseConstructible< char, uint8> { enum { Value = true }; };
template <> struct TIsBitwiseConstructible<uint16, short> { enum { Value = true }; };
template <> struct TIsBitwiseConstructible< short, uint16> { enum { Value = true }; };
template <> struct TIsBitwiseConstructible<uint32, int32> { enum { Value = true }; };
template <> struct TIsBitwiseConstructible< int32, uint32> { enum { Value = true }; };
template <> struct TIsBitwiseConstructible<uint64, int64> { enum { Value = true }; };
template <> struct TIsBitwiseConstructible< int64, uint64> { enum { Value = true }; };