File indexing completed on 2025-08-27 09:30:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef FLATBUFFERS_H_
0018 #define FLATBUFFERS_H_
0019
0020 #include <algorithm>
0021
0022
0023
0024 #include "flatbuffers/array.h"
0025 #include "flatbuffers/base.h"
0026 #include "flatbuffers/buffer.h"
0027 #include "flatbuffers/buffer_ref.h"
0028 #include "flatbuffers/detached_buffer.h"
0029 #include "flatbuffers/flatbuffer_builder.h"
0030 #include "flatbuffers/stl_emulation.h"
0031 #include "flatbuffers/string.h"
0032 #include "flatbuffers/struct.h"
0033 #include "flatbuffers/table.h"
0034 #include "flatbuffers/vector.h"
0035 #include "flatbuffers/vector_downward.h"
0036 #include "flatbuffers/verifier.h"
0037
0038 namespace flatbuffers {
0039
0040
0041
0042
0043
0044 inline const uint8_t *GetBufferStartFromRootPointer(const void *root) {
0045 auto table = reinterpret_cast<const Table *>(root);
0046 auto vtable = table->GetVTable();
0047
0048 auto start = (std::min)(vtable, reinterpret_cast<const uint8_t *>(root));
0049
0050 start = reinterpret_cast<const uint8_t *>(reinterpret_cast<uintptr_t>(start) &
0051 ~(sizeof(uoffset_t) - 1));
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 static_assert(flatbuffers::kFileIdentifierLength == sizeof(uoffset_t),
0062 "file_identifier is assumed to be the same size as uoffset_t");
0063 for (auto possible_roots = FLATBUFFERS_MAX_ALIGNMENT / sizeof(uoffset_t) + 1;
0064 possible_roots; possible_roots--) {
0065 start -= sizeof(uoffset_t);
0066 if (ReadScalar<uoffset_t>(start) + start ==
0067 reinterpret_cast<const uint8_t *>(root))
0068 return start;
0069 }
0070
0071
0072
0073
0074 FLATBUFFERS_ASSERT(false);
0075 return nullptr;
0076 }
0077
0078
0079 template<typename SizeT = uoffset_t>
0080 inline SizeT GetPrefixedSize(const uint8_t *buf) {
0081 return ReadScalar<SizeT>(buf);
0082 }
0083
0084
0085
0086
0087
0088
0089
0090 template<typename SizeT = uoffset_t>
0091 inline SizeT GetSizePrefixedBufferLength(const uint8_t *const buf) {
0092 return ReadScalar<SizeT>(buf) + sizeof(SizeT);
0093 }
0094
0095
0096
0097
0098 struct NativeTable {};
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108 typedef uint64_t hash_value_t;
0109 typedef std::function<void(void **pointer_adr, hash_value_t hash)>
0110 resolver_function_t;
0111 typedef std::function<hash_value_t(void *pointer)> rehasher_function_t;
0112
0113
0114
0115
0116
0117
0118
0119
0120 template<typename T>
0121 bool IsFieldPresent(const T *table, typename T::FlatBuffersVTableOffset field) {
0122
0123 return reinterpret_cast<const Table *>(table)->CheckField(
0124 static_cast<voffset_t>(field));
0125 }
0126
0127
0128
0129
0130 inline int LookupEnum(const char **names, const char *name) {
0131 for (const char **p = names; *p; p++)
0132 if (!strcmp(*p, name)) return static_cast<int>(p - names);
0133 return -1;
0134 }
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147 #if defined(_MSC_VER)
0148 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \
0149 __pragma(pack(1)) \
0150 struct __declspec(align(alignment))
0151 #define FLATBUFFERS_STRUCT_END(name, size) \
0152 __pragma(pack()) \
0153 static_assert(sizeof(name) == size, "compiler breaks packing rules")
0154 #elif defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
0155 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \
0156 _Pragma("pack(1)") \
0157 struct __attribute__((aligned(alignment)))
0158 #define FLATBUFFERS_STRUCT_END(name, size) \
0159 _Pragma("pack()") \
0160 static_assert(sizeof(name) == size, "compiler breaks packing rules")
0161 #else
0162 #error Unknown compiler, please define structure alignment macros
0163 #endif
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176 enum SequenceType { ST_TABLE, ST_STRUCT, ST_UNION, ST_ENUM };
0177
0178
0179
0180 #define FLATBUFFERS_GEN_ELEMENTARY_TYPES(ET) \
0181 ET(ET_UTYPE) \
0182 ET(ET_BOOL) \
0183 ET(ET_CHAR) \
0184 ET(ET_UCHAR) \
0185 ET(ET_SHORT) \
0186 ET(ET_USHORT) \
0187 ET(ET_INT) \
0188 ET(ET_UINT) \
0189 ET(ET_LONG) \
0190 ET(ET_ULONG) \
0191 ET(ET_FLOAT) \
0192 ET(ET_DOUBLE) \
0193 ET(ET_STRING) \
0194 ET(ET_SEQUENCE)
0195
0196 enum ElementaryType {
0197 #define FLATBUFFERS_ET(E) E,
0198 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
0199 #undef FLATBUFFERS_ET
0200 };
0201
0202 inline const char * const *ElementaryTypeNames() {
0203 static const char * const names[] = {
0204 #define FLATBUFFERS_ET(E) #E,
0205 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
0206 #undef FLATBUFFERS_ET
0207 };
0208 return names;
0209 }
0210
0211
0212
0213
0214
0215
0216 struct TypeCode {
0217
0218 unsigned short base_type : 4;
0219
0220 unsigned short is_repeating : 1;
0221
0222 signed short sequence_ref : 11;
0223 };
0224
0225 static_assert(sizeof(TypeCode) == 2, "TypeCode");
0226
0227 struct TypeTable;
0228
0229
0230 typedef const TypeTable *(*TypeFunction)();
0231
0232 struct TypeTable {
0233 SequenceType st;
0234 size_t num_elems;
0235 const TypeCode *type_codes;
0236 const TypeFunction *type_refs;
0237 const int16_t *array_sizes;
0238 const int64_t *values;
0239 const char *const *names;
0240 };
0241
0242
0243 inline const char *flatbuffers_version_string() {
0244 return "FlatBuffers " FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
0245 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
0246 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
0247 }
0248
0249
0250 #define FLATBUFFERS_DEFINE_BITMASK_OPERATORS(E, T)\
0251 inline FLATBUFFERS_CONSTEXPR_CPP11 E operator | (E lhs, E rhs){\
0252 return E(T(lhs) | T(rhs));\
0253 }\
0254 inline FLATBUFFERS_CONSTEXPR_CPP11 E operator & (E lhs, E rhs){\
0255 return E(T(lhs) & T(rhs));\
0256 }\
0257 inline FLATBUFFERS_CONSTEXPR_CPP11 E operator ^ (E lhs, E rhs){\
0258 return E(T(lhs) ^ T(rhs));\
0259 }\
0260 inline FLATBUFFERS_CONSTEXPR_CPP11 E operator ~ (E lhs){\
0261 return E(~T(lhs));\
0262 }\
0263 inline FLATBUFFERS_CONSTEXPR_CPP11 E operator |= (E &lhs, E rhs){\
0264 lhs = lhs | rhs;\
0265 return lhs;\
0266 }\
0267 inline FLATBUFFERS_CONSTEXPR_CPP11 E operator &= (E &lhs, E rhs){\
0268 lhs = lhs & rhs;\
0269 return lhs;\
0270 }\
0271 inline FLATBUFFERS_CONSTEXPR_CPP11 E operator ^= (E &lhs, E rhs){\
0272 lhs = lhs ^ rhs;\
0273 return lhs;\
0274 }\
0275 inline FLATBUFFERS_CONSTEXPR_CPP11 bool operator !(E rhs) \
0276 {\
0277 return !bool(T(rhs)); \
0278 }
0279
0280 }
0281
0282
0283
0284 #endif