5#include "../Foundation/Limits.h"
6#include "../Foundation/TypeList.h"
7#include "ReflectionFoundation.h"
106 MemberInfo memberInfo;
107 StructInfo structInfo;
116 static_assert(
sizeof(
TypeInfo) == 8,
"Size must be 8 bytes");
185 [[nodiscard]]
static constexpr bool isPrimitiveCategory(
TypeCategory category)
198template <
typename T,
typename SFINAESelector =
void>
208 template <
typename TypeVisitor>
209 [[nodiscard]]
static constexpr bool build(TypeVisitor&)
225template <>
struct Reflect<float> :
public ReflectPrimitive {
static constexpr auto getCategory(){
return TypeCategory::TypeFLOAT32;}};
226template <>
struct Reflect<double> :
public ReflectPrimitive {
static constexpr auto getCategory(){
return TypeCategory::TypeDOUBLE64;}};
227template <>
struct Reflect<bool> :
public ReflectPrimitive {
static constexpr auto getCategory(){
return TypeCategory::TypeBOOL;}};
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>()))
302 size_t memberSizeSum = 0;
303 bool IsPacked =
false;
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)
327 memberSizeSum +=
sizeof(R);
332template <
typename T,
typename SFINAESelector>
344#define SC_REFLECT_STRUCT_VISIT(StructName) \
346 struct SC::Reflection::Reflect<StructName> : SC::Reflection::ReflectStruct<StructName> \
348 template <typename TypeVisitor> \
349 static constexpr bool visit(TypeVisitor&& builder) \
351 SC_COMPILER_WARNING_PUSH_OFFSETOF \
356#define SC_REFLECT_STRUCT_FIELD(MEMBER_TAG, MEMBER) \
357 and builder(MEMBER_TAG, &T::MEMBER, #MEMBER, SC_COMPILER_OFFSETOF(T, MEMBER))
360#define SC_REFLECT_STRUCT_LEAVE() \
362 SC_COMPILER_WARNING_POP \
#define SC_COMPILER_UNUSED(param)
Silence an unused variable or unused parameter warning.
Definition: Compiler.h:139
int int32_t
Platform independent (4) bytes signed int.
Definition: PrimitiveTypes.h:46
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
signed char int8_t
Platform independent (1) byte signed int.
Definition: PrimitiveTypes.h:44
long long int64_t
Platform independent (8) bytes signed int.
Definition: PrimitiveTypes.h:50
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
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition: PrimitiveTypes.h:37
signed long ssize_t
Platform independent signed size type.
Definition: PrimitiveTypes.h:57
TypeCategory
Enumeration of possible category types recognized by Reflection.
Definition: Reflection.h:32
@ 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.
An object that can be converted to any primitive type providing its max value.
Definition: Limits.h:21
Visit all struct members to gather sum of their sizes (helper for IsPacked).
Definition: Reflection.h:301
Class template used to check if a given type IsPacked property is true at compile time.
Definition: Reflection.h:334
Checks if a given type is primitive.
Definition: Reflection.h:230
Basic class template that must be partially specialized for each type.
Definition: Reflection.h:195
Base struct for all primitive types.
Definition: Reflection.h:207
Definition: Reflection.h:279
Holds extended type info for array-like types.
Definition: Reflection.h:98
uint32_t isPacked
Ensures no padding (recursively) for the entire span of the struct.
Definition: Reflection.h:99
uint32_t numElements
Number of elements in the array.
Definition: Reflection.h:100
Holds no extended type info.
Definition: Reflection.h:76
Holds extended type info for members of struct.
Definition: Reflection.h:81
uint16_t offsetInBytes
Used for signature uniqueness and by SerializationBinaryTypeErased.
Definition: Reflection.h:83
uint16_t memberTag
Used for versioned serialization.
Definition: Reflection.h:82
Holds extended type info for structs.
Definition: Reflection.h:91
bool isPacked
Ensures no padding (recursively) for the entire span of the struct.
Definition: Reflection.h:92
[reflectionSnippet3]
Definition: Reflection.h:64
uint8_t numberOfChildren
Only valid when TypeInfo::hasLink == false
Definition: Reflection.h:69
constexpr bool needsLinking() const
Check if this type info needs to be linked.
Definition: Reflection.h:157
constexpr auto getLinkIndex() const
Obtains link valid index (assuming hasLink == true and needsLinking() == false)
Definition: Reflection.h:161
constexpr bool hasValidLinkIndex() const
Check if this type info has a valid link index.
Definition: Reflection.h:154
constexpr TypeInfo()
[reflectionSnippet4]
Definition: Reflection.h:113
constexpr TypeInfo(TypeCategory type, uint16_t sizeInBytes)
Constructs a TypeInfo of given type and size.
Definition: Reflection.h:135
constexpr bool isPrimitiveOrPackedStruct() const
Check if type is primitive or it's a struct with isPacked property == true
Definition: Reflection.h:178
TypeCategory type
Type of typeinfo.
Definition: Reflection.h:66
constexpr TypeInfo(TypeCategory type, uint16_t sizeInBytes, MemberInfo member)
Constructs a TypeInfo used by Struct Members (children of Struct Type)
Definition: Reflection.h:125
bool hasLink
Contains a link to another type.
Definition: Reflection.h:65
constexpr bool setLinkIndex(ssize_t newLinkIndex)
Change Link index for this type.
Definition: Reflection.h:166
constexpr TypeInfo(TypeCategory type, uint16_t sizeInBytes, StructInfo structInfo)
Constructs a TypeInfo used by Struct Types.
Definition: Reflection.h:120
uint8_t linkIndex
Only valid when TypeInfo::hasLink == true
Definition: Reflection.h:70
uint16_t sizeInBytes
Size in bytes of the described type.
Definition: Reflection.h:72
constexpr bool setNumberOfChildren(size_t numChildren)
Sets the number of children of this typeinfo.
Definition: Reflection.h:145
constexpr TypeInfo(TypeCategory type, uint16_t sizeInBytes, uint8_t numberOfChildren, ArrayInfo arrayInfo)
Constructs a TypeInfo used by Array-like Types (T[N], Array<T, N> and Vector<T>)
Definition: Reflection.h:130
constexpr auto getNumberOfChildren() const
Get number of children (if any) of this info. Only valid when hasLink == false
Definition: Reflection.h:140
constexpr bool isPrimitiveType() const
Check if type is primitive.
Definition: Reflection.h:175
EnableIf conditionally defines a type if a boolean template parameter is true.
Definition: TypeTraits.h:25