4#include "../Algorithms/AlgorithmBubbleSort.h"
16template <
typename SchemaBuilder>
21 using TypeBuildFunction =
typename Type::TypeBuildFunction;
22 using VirtualTablesType =
decltype(SchemaBuilder::vtables);
25 template <u
int32_t MAX_TOTAL_TYPES>
29 VirtualTablesType vtables;
33 template <u
int32_t NUM_TYPES>
34 struct FlatTrimmedResult
38 VirtualTablesType vtables;
41 template <u
int32_t MAX_TYPES>
46 const auto baseLinkID = types.size;
47 builder.currentLinkID = types.size;
48 builder.types = {types.values + types.size, MAX_TYPES - types.size};
53 const auto numberOfTypes = builder.currentLinkID - baseLinkID;
54 const auto numberOfChildren = numberOfTypes - 1;
57 if (not types.values[baseLinkID].typeInfo.setNumberOfChildren(numberOfChildren))
60 struct OrderByMemberOffset
62 constexpr bool operator()(
const Type& a,
const Type& b)
const
64 return a.typeInfo.memberInfo.offsetInBytes < b.typeInfo.memberInfo.offsetInBytes;
68 types.values[baseLinkID].typeInfo.structInfo.isPacked)
72 OrderByMemberOffset());
74 types.size += numberOfTypes;
80 template <u
int32_t MAX_LINK_BUFFER_SIZE, u
int32_t MAX_TOTAL_TYPES,
typename Func>
81 constexpr static FlatFullResult<MAX_TOTAL_TYPES> compileAllTypesFor(Func func)
84 FlatFullResult<MAX_TOTAL_TYPES> result;
86 SchemaBuilder container(result.types.values, MAX_TOTAL_TYPES);
88#if SC_COMPILER_GCC && __GNUC__ <= 13
94 if (not appendTypesTo(result.types, func, container))
101 while (typeIndex < result.types.size)
103 Type& type = result.types.values[typeIndex];
104 if (not type.typeInfo.isPrimitiveType() and type.typeInfo.needsLinking())
107#if SC_COMPILER_GCC && __GNUC__ <= 13
108 if (alreadyVisitedTypes.
contains(type.typeName, &outIndex))
110 if (alreadyVisitedTypes.
contains(type.typeBuild, &outIndex))
113 if (not type.typeInfo.setLinkIndex(alreadyVisitedLinkID.values[outIndex]))
118 if (not type.typeInfo.setLinkIndex(result.types.size))
120 if (not alreadyVisitedLinkID.
push_back(result.types.size))
122#if SC_COMPILER_GCC && __GNUC__ <= 13
123 if (not alreadyVisitedTypes.
push_back(type.typeName))
125 if (not alreadyVisitedTypes.
push_back(type.typeBuild))
128 if (not appendTypesTo(result.types, type.typeBuild, container))
134 result.vtables = container.vtables;
146 template <
typename T, u
int32_t MAX_LINK_BUFFER_SIZE = 20, u
int32_t MAX_TOTAL_TYPES = 100>
149 constexpr auto schema =
150 compileAllTypesFor<MAX_LINK_BUFFER_SIZE, MAX_TOTAL_TYPES>(&
Reflect<T>::template build<SchemaBuilder>);
151 static_assert(schema.types.size > 0,
"Something failed in compileAllTypesFor");
154 FlatTrimmedResult<schema.types.size> result;
155 for (
uint32_t i = 0; i < schema.types.size; ++i)
157 result.typeInfos.values[i] = schema.types.values[i].typeInfo;
158 result.typeNames.values[i] = schema.types.values[i].typeName;
160 result.typeInfos.size = schema.types.size;
161 result.typeNames.size = schema.types.size;
162 result.vtables = schema.vtables;
169template <
typename TypeVisitor>
172 using TypeBuildFunction = bool (*)(TypeVisitor& builder);
176 TypeBuildFunction typeBuild;
178 constexpr SchemaType() : typeBuild(
nullptr) {}
180 : typeInfo(typeInfo), typeName(typeName), typeBuild(typeBuild)
184 template <
typename T>
191 template <
typename T>
199 template <
typename R,
typename T,
int N>
208 template <
typename T>
217template <
typename TypeVisitor>
228 template <
typename R,
typename T,
int N>
229 [[nodiscard]]
constexpr bool operator()(
uint8_t memberTag, R T::*field,
const char (&name)[N],
size_t offset)
235 [[nodiscard]]
constexpr bool addType(
Type type)
238 return types.writeAndAdvance(type);
constexpr void bubbleSort(Iterator first, Iterator last, BinaryPredicate predicate=BinaryPredicate())
Sorts iterator range according to BinaryPredicate (bubble sort).
Definition: AlgorithmBubbleSort.h:34
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition: PrimitiveTypes.h:36
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition: PrimitiveTypes.h:38
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition: PrimitiveTypes.h:37
@ TypeStruct
Type is a struct type.
An object that can be converted to any primitive type providing its max value.
Definition: Limits.h:21
A constexpr array.
Definition: ReflectionFoundation.h:18
constexpr bool push_back(const T &value)
Append a single item to this array.
Definition: ReflectionFoundation.h:60
constexpr bool contains(T value, uint32_t *outIndex=nullptr) const
Check if array contains given value, and retrieve index where such item exists.
Definition: ReflectionFoundation.h:27
Class template used to check if a given type IsPacked property is true at compile time.
Definition: Reflection.h:334
Definition: ReflectionSchemaCompiler.h:246
A schema builder that doesn't build any virtual table.
Definition: ReflectionSchemaCompiler.h:244
Basic class template that must be partially specialized for each type.
Definition: Reflection.h:195
Common code for derived class to create a SchemaBuilder suitable for SC::Reflection::SchemaCompiler.
Definition: ReflectionSchemaCompiler.h:219
Creates a schema linking a series of SchemaType.
Definition: ReflectionSchemaCompiler.h:18
static constexpr auto compile()
Returns a constexpr compiled trimmed flat schema for type T.
Definition: ReflectionSchemaCompiler.h:147
Holds together a TypeInfo, a StringView and a type-erased builder function pointer.
Definition: ReflectionSchemaCompiler.h:171
static constexpr SchemaType createArray(TypeStringView name, uint8_t numChildren, TypeInfo::ArrayInfo arrayInfo)
Create from an array-like type.
Definition: ReflectionSchemaCompiler.h:209
static constexpr SchemaType createMember(uint8_t memberTag, R T::*, const char(&name)[N], size_t offset)
Create from a struct member with given name, memberTag and offset.
Definition: ReflectionSchemaCompiler.h:200
static constexpr SchemaType createGeneric()
Create from a generic type T.
Definition: ReflectionSchemaCompiler.h:185
static constexpr SchemaType createStruct(TypeStringView name=TypeToString< T >::get())
Create from a Struct type T.
Definition: ReflectionSchemaCompiler.h:192
A minimal ASCII StringView with shortened name to be used in TypeToString.
Definition: ReflectionFoundation.h:101
Holds extended type info for array-like types.
Definition: Reflection.h:98
Holds extended type info for members of struct.
Definition: Reflection.h:81
Holds extended type info for structs.
Definition: Reflection.h:91
[reflectionSnippet3]
Definition: Reflection.h:64
uint8_t numberOfChildren
Only valid when TypeInfo::hasLink == false
Definition: Reflection.h:69
Strips down class name produced by ClNm to reduce binary size (from C++17 going forward)
Definition: ReflectionFoundation.h:178
A writable span of objects.
Definition: ReflectionFoundation.h:79