5#include "../Foundation/TypeTraits.h"
6#include "ReflectionFoundation.h"
84 : memberTag(memberTag), offsetInBytes(offsetInBytes)
92 constexpr StructInfo(
bool isPacked) : isPacked(isPacked) {}
100 constexpr ArrayInfo(
bool isPacked,
uint32_t numElements) : isPacked(isPacked), numElements(numElements) {}
105 MemberInfo memberInfo;
106 StructInfo structInfo;
115 static_assert(
sizeof(TypeInfo) == 8,
"Size must be 8 bytes");
120 : hasLink(false), type(type), numberOfChildren(0), sizeInBytes(sizeInBytes), structInfo(structInfo)
125 : hasLink(true), type(type), linkIndex(0), sizeInBytes(sizeInBytes), memberInfo(member)
130 : hasLink(false), type(type), numberOfChildren(numberOfChildren), sizeInBytes(sizeInBytes), arrayInfo(arrayInfo)
135 : hasLink(true), type(type), linkIndex(0), sizeInBytes(sizeInBytes), emptyInfo()
139 [[nodiscard]]
constexpr auto getNumberOfChildren()
const {
return numberOfChildren; }
144 [[nodiscard]]
constexpr bool setNumberOfChildren(
size_t numChildren)
146 if (numChildren >
static_cast<decltype(numberOfChildren)
>(~0ull))
148 numberOfChildren =
static_cast<decltype(numberOfChildren)
>(numChildren);
153 [[nodiscard]]
constexpr bool hasValidLinkIndex()
const {
return hasLink and linkIndex > 0; }
156 [[nodiscard]]
constexpr bool needsLinking()
const {
return hasLink and linkIndex == 0; }
160 [[nodiscard]]
constexpr auto getLinkIndex()
const {
return linkIndex; }
165 [[nodiscard]]
constexpr bool setLinkIndex(
ssize_t newLinkIndex)
167 if (newLinkIndex >
static_cast<decltype(linkIndex)
>(~0ull))
169 linkIndex =
static_cast<decltype(linkIndex)
>(newLinkIndex);
174 [[nodiscard]]
constexpr bool isPrimitiveType()
const {
return isPrimitiveCategory(type); }
177 [[nodiscard]]
constexpr bool isPrimitiveOrPackedStruct()
const
179 if (isPrimitiveType())
184 [[nodiscard]]
static constexpr bool isPrimitiveCategory(
TypeCategory category)
197template <
typename T,
typename SFINAESelector =
void>
198struct ExtendedTypeInfo;
205struct ReflectPrimitive
207 template <
typename TypeVisitor>
208 [[nodiscard]]
static constexpr bool build(TypeVisitor&)
215template <>
struct Reflect<char> :
public ReflectPrimitive {
static constexpr auto getCategory(){
return TypeCategory::TypeINT8;}};
224template <>
struct Reflect<float> :
public ReflectPrimitive {
static constexpr auto getCategory(){
return TypeCategory::TypeFLOAT32;}};
225template <>
struct Reflect<double> :
public ReflectPrimitive {
static constexpr auto getCategory(){
return TypeCategory::TypeDOUBLE64;}};
226template <>
struct Reflect<bool> :
public ReflectPrimitive {
static constexpr auto getCategory(){
return TypeCategory::TypeBOOL;}};
229template <
typename T>
struct IsPrimitive {
static constexpr bool value = TypeInfo::isPrimitiveCategory(Reflect<T>::getCategory()); };
230template <
typename T>
struct IsStruct {
static constexpr bool value = Reflect<T>::getCategory() ==
TypeCategory::TypeStruct; };
235struct ExtendedTypeInfo<T, typename SC::TypeTraits::EnableIf<IsPrimitive<T>::value>::type>
238 static constexpr bool IsPacked =
true;
244template <
typename T,
size_t N>
249 template <
typename TypeVisitor>
250 [[nodiscard]]
static constexpr bool build(TypeVisitor& builder)
252 using Type =
typename TypeVisitor::Type;
255 constexpr bool isPacked = ExtendedTypeInfo<T>::IsPacked;
256 if (not builder.addType(Type::template createArray<T[N]>(
"Array", 1, TypeInfo::ArrayInfo{isPacked, N})))
260 if (not builder.addType(Type::template createGeneric<T>()))
267template <
typename T,
int N>
268struct ExtendedTypeInfo<T[N]>
271 static constexpr bool IsPacked = ExtendedTypeInfo<T>::IsPacked;
277template <
typename Type>
284 template <
typename TypeVisitor>
285 [[nodiscard]]
static constexpr bool build(TypeVisitor& builder)
288 if (not builder.addType(TypeVisitor::Type::template createStruct<T>()))
292 if (not Reflect<Type>::visit(builder))
300struct ExtendedStructTypeInfo
302 size_t memberSizeSum = 0;
303 bool IsPacked =
false;
305 constexpr ExtendedStructTypeInfo()
308 if (Reflect<T>::visit(*
this))
312 IsPacked = memberSizeSum ==
sizeof(T);
316 template <
typename R,
int N>
317 constexpr bool operator()(
int memberTag, R T::* member,
const char (&name)[N],
size_t offset)
323 if (not ExtendedTypeInfo<R>().IsPacked)
327 memberSizeSum +=
sizeof(R);
332template <
typename T,
typename SFINAESelector>
333struct ExtendedTypeInfo
336 static constexpr bool IsPacked = ExtendedStructTypeInfo<T>().IsPacked;
339template <
typename MemberVisitor,
typename Container,
typename ItemType,
int N>
340struct VectorArrayVTable
342 static constexpr bool build(MemberVisitor&) {
return true; }
350#define SC_REFLECT_STRUCT_VISIT(StructName) \
352 struct SC::Reflection::Reflect<StructName> : SC::Reflection::ReflectStruct<StructName> \
354 template <typename TypeVisitor> \
355 static constexpr bool visit(TypeVisitor&& builder) \
357 SC_COMPILER_WARNING_PUSH_OFFSETOF \
362#define SC_REFLECT_STRUCT_FIELD(MEMBER_TAG, MEMBER) \
363 and builder(MEMBER_TAG, &T::MEMBER, #MEMBER, SC_COMPILER_OFFSETOF(T, MEMBER))
366#define SC_REFLECT_STRUCT_LEAVE() \
368 SC_COMPILER_WARNING_POP \
#define SC_COMPILER_UNUSED(param)
Silence an unused variable or unused parameter warning.
Definition Compiler.h:131
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition PrimitiveTypes.h:37
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition PrimitiveTypes.h:36
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:42
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:38
short int16_t
Platform independent (2) bytes signed int.
Definition PrimitiveTypes.h:45
long long int64_t
Platform independent (8) bytes signed int.
Definition PrimitiveTypes.h:50
signed char int8_t
Platform independent (1) byte signed int.
Definition PrimitiveTypes.h:44
signed long ssize_t
Platform independent signed size type.
Definition PrimitiveTypes.h:57
int int32_t
Platform independent (4) bytes signed int.
Definition PrimitiveTypes.h:46
TypeCategory
Enumeration of possible category types recognized by Reflection.
Definition Reflection.h:31
@ TypeUINT32
Type is uint32_t
@ TypeUINT16
Type is uint16_t
@ TypeUINT64
Type is uint64_t
@ TypeArray
Type is an array type.
@ TypeINT16
Type is int16_t
@ TypeINT64
Type is int64_t
@ TypeINT32
Type is int32_t
@ TypeVector
Type is a vector type.
@ TypeFLOAT32
Type is float
@ TypeUINT8
Type is uint8_t
@ TypeStruct
Type is a struct type.
@ TypeDOUBLE64
Type is double
@ TypeInvalid
Invalid type sentinel.