From 05311238fa2c2b1bad8ad5af136b43a9ddd789fb Mon Sep 17 00:00:00 2001 From: Fedor Osetrov <33493672+fdr400@users.noreply.github.com> Date: Fri, 13 Mar 2026 11:57:57 +0300 Subject: [PATCH 1/3] Update reflection.h allow to use reflection in constant time evaluation --- include/flatbuffers/reflection.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/flatbuffers/reflection.h b/include/flatbuffers/reflection.h index 371df72526..f6f7c17b9c 100644 --- a/include/flatbuffers/reflection.h +++ b/include/flatbuffers/reflection.h @@ -30,16 +30,16 @@ namespace flatbuffers { // ------------------------- GETTERS ------------------------- -inline bool IsScalar(reflection::BaseType t) { +constexpr bool IsScalar(reflection::BaseType t) { 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; } -inline bool IsFloat(reflection::BaseType t) { +constexpr bool IsFloat(reflection::BaseType t) { 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; } From db5cb7f11bfab67fa2ed1b8b2380e8bd9beca336 Mon Sep 17 00:00:00 2001 From: Fedor Osetrov Date: Thu, 19 Mar 2026 17:40:36 +0000 Subject: [PATCH 2/3] make GetTypeSize constexpr --- include/flatbuffers/reflection.h | 63 ++++++++++++++++---------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/include/flatbuffers/reflection.h b/include/flatbuffers/reflection.h index f6f7c17b9c..8de87e6997 100644 --- a/include/flatbuffers/reflection.h +++ b/include/flatbuffers/reflection.h @@ -43,37 +43,38 @@ constexpr bool IsLong(reflection::BaseType t) { return t == reflection::Long || t == reflection::ULong; } +// This needs to correspond to the BaseType enum. +constexpr size_t kBaseTypeSize[] = { + 0, // None + 1, // UType + 1, // Bool + 1, // Byte + 1, // UByte + 2, // Short + 2, // UShort + 4, // Int + 4, // UInt + 8, // Long + 8, // ULong + 4, // Float + 8, // Double + 4, // String + 4, // Vector + 4, // Obj + 4, // Union + 0, // Array. Only used in structs. 0 was chosen to prevent out-of-bounds + // errors. + 8, // Vector64 + + 0 // MaxBaseType. This must be kept the last entry in this array. +}; +static_assert(sizeof(kBaseTypeSize) / sizeof(size_t) == reflection::MaxBaseType + 1, + "Size of sizes[] array does not match the count of BaseType " + "enum values."); + // Size of a basic type, don't use with structs. -inline size_t GetTypeSize(reflection::BaseType base_type) { - // This needs to correspond to the BaseType enum. - static size_t sizes[] = { - 0, // None - 1, // UType - 1, // Bool - 1, // Byte - 1, // UByte - 2, // Short - 2, // UShort - 4, // Int - 4, // UInt - 8, // Long - 8, // ULong - 4, // Float - 8, // Double - 4, // String - 4, // Vector - 4, // Obj - 4, // Union - 0, // Array. Only used in structs. 0 was chosen to prevent out-of-bounds - // errors. - 8, // Vector64 - - 0 // MaxBaseType. This must be kept the last entry in this array. - }; - static_assert(sizeof(sizes) / sizeof(size_t) == reflection::MaxBaseType + 1, - "Size of sizes[] array does not match the count of BaseType " - "enum values."); - return sizes[base_type]; +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 @@ -420,7 +421,7 @@ pointer_inside_vector piv(T* ptr, std::vector& vec) { return pointer_inside_vector(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. inline const reflection::Object& GetUnionType( From a2c4a70d60b17bfaacd42d7e7fc3a1f5d4d64b87 Mon Sep 17 00:00:00 2001 From: Fedor Osetrov Date: Thu, 19 Mar 2026 17:41:39 +0000 Subject: [PATCH 3/3] fix clang-format --- include/flatbuffers/reflection.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/flatbuffers/reflection.h b/include/flatbuffers/reflection.h index 8de87e6997..bc8c2a9288 100644 --- a/include/flatbuffers/reflection.h +++ b/include/flatbuffers/reflection.h @@ -68,7 +68,8 @@ constexpr size_t kBaseTypeSize[] = { 0 // MaxBaseType. This must be kept the last entry in this array. }; -static_assert(sizeof(kBaseTypeSize) / sizeof(size_t) == reflection::MaxBaseType + 1, +static_assert(sizeof(kBaseTypeSize) / sizeof(size_t) == + reflection::MaxBaseType + 1, "Size of sizes[] array does not match the count of BaseType " "enum values.");