allow to use reflection in constant time evaluation (#8978)

* Update reflection.h

allow to use reflection in constant time evaluation

* make GetTypeSize constexpr

* fix clang-format
This commit is contained in:
Fedor Osetrov
2026-03-20 05:01:45 +03:00
committed by GitHub
parent 48babd417d
commit 8396e00dd8

View File

@@ -30,50 +30,52 @@ namespace flatbuffers {
// ------------------------- GETTERS ------------------------- // ------------------------- GETTERS -------------------------
inline bool IsScalar(reflection::BaseType t) { constexpr bool IsScalar(reflection::BaseType t) {
return t >= reflection::UType && t <= reflection::Double; return t >= reflection::UType && t <= reflection::Double;
} }
inline bool IsInteger(reflection::BaseType t) { constexpr bool IsInteger(reflection::BaseType t) {
return t >= reflection::UType && t <= reflection::ULong; return t >= reflection::UType && t <= reflection::ULong;
} }
inline bool IsFloat(reflection::BaseType t) { constexpr bool IsFloat(reflection::BaseType t) {
return t == reflection::Float || t == reflection::Double; return t == reflection::Float || t == reflection::Double;
} }
inline bool IsLong(reflection::BaseType t) { constexpr bool IsLong(reflection::BaseType t) {
return t == reflection::Long || t == reflection::ULong; return t == reflection::Long || t == reflection::ULong;
} }
// Size of a basic type, don't use with structs. // This needs to correspond to the BaseType enum.
inline size_t GetTypeSize(reflection::BaseType base_type) { constexpr size_t kBaseTypeSize[] = {
// This needs to correspond to the BaseType enum. 0, // None
static size_t sizes[] = { 1, // UType
0, // None 1, // Bool
1, // UType 1, // Byte
1, // Bool 1, // UByte
1, // Byte 2, // Short
1, // UByte 2, // UShort
2, // Short 4, // Int
2, // UShort 4, // UInt
4, // Int 8, // Long
4, // UInt 8, // ULong
8, // Long 4, // Float
8, // ULong 8, // Double
4, // Float 4, // String
8, // Double 4, // Vector
4, // String 4, // Obj
4, // Vector 4, // Union
4, // Obj 0, // Array. Only used in structs. 0 was chosen to prevent out-of-bounds
4, // Union // errors.
0, // Array. Only used in structs. 0 was chosen to prevent out-of-bounds 8, // Vector64
// errors.
8, // Vector64
0 // MaxBaseType. This must be kept the last entry in this array. 0 // MaxBaseType. This must be kept the last entry in this array.
}; };
static_assert(sizeof(sizes) / sizeof(size_t) == reflection::MaxBaseType + 1, static_assert(sizeof(kBaseTypeSize) / sizeof(size_t) ==
"Size of sizes[] array does not match the count of BaseType " reflection::MaxBaseType + 1,
"enum values."); "Size of sizes[] array does not match the count of BaseType "
return sizes[base_type]; "enum values.");
// Size of a basic type, don't use with structs.
constexpr size_t GetTypeSize(reflection::BaseType base_type) {
return kBaseTypeSize[base_type];
} }
// Same as above, but now correctly returns the size of a struct if // Same as above, but now correctly returns the size of a struct if
@@ -420,7 +422,7 @@ pointer_inside_vector<T, U> piv(T* ptr, std::vector<U>& vec) {
return pointer_inside_vector<T, U>(ptr, vec); return pointer_inside_vector<T, U>(ptr, vec);
} }
inline const char* UnionTypeFieldSuffix() { return "_type"; } constexpr const char* UnionTypeFieldSuffix() { return "_type"; }
// Helper to figure out the actual table type a union refers to. // Helper to figure out the actual table type a union refers to.
inline const reflection::Object& GetUnionType( inline const reflection::Object& GetUnionType(