Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:30:25

0001 /*
0002  * Copyright 2014 Google Inc. All rights reserved.
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *     http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
0015  */
0016 
0017 #ifndef FLATBUFFERS_H_
0018 #define FLATBUFFERS_H_
0019 
0020 #include <algorithm>
0021 
0022 // TODO: These includes are for mitigating the pains of users editing their
0023 // source because they relied on flatbuffers.h to include everything for them.
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 /// @brief This can compute the start of a FlatBuffer from a root pointer, i.e.
0041 /// it is the opposite transformation of GetRoot().
0042 /// This may be useful if you want to pass on a root and have the recipient
0043 /// delete the buffer afterwards.
0044 inline const uint8_t *GetBufferStartFromRootPointer(const void *root) {
0045   auto table = reinterpret_cast<const Table *>(root);
0046   auto vtable = table->GetVTable();
0047   // Either the vtable is before the root or after the root.
0048   auto start = (std::min)(vtable, reinterpret_cast<const uint8_t *>(root));
0049   // Align to at least sizeof(uoffset_t).
0050   start = reinterpret_cast<const uint8_t *>(reinterpret_cast<uintptr_t>(start) &
0051                                             ~(sizeof(uoffset_t) - 1));
0052   // Additionally, there may be a file_identifier in the buffer, and the root
0053   // offset. The buffer may have been aligned to any size between
0054   // sizeof(uoffset_t) and FLATBUFFERS_MAX_ALIGNMENT (see "force_align").
0055   // Sadly, the exact alignment is only known when constructing the buffer,
0056   // since it depends on the presence of values with said alignment properties.
0057   // So instead, we simply look at the next uoffset_t values (root,
0058   // file_identifier, and alignment padding) to see which points to the root.
0059   // None of the other values can "impersonate" the root since they will either
0060   // be 0 or four ASCII characters.
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   // We didn't find the root, either the "root" passed isn't really a root,
0071   // or the buffer is corrupt.
0072   // Assert, because calling this function with bad data may cause reads
0073   // outside of buffer boundaries.
0074   FLATBUFFERS_ASSERT(false);
0075   return nullptr;
0076 }
0077 
0078 /// @brief This return the prefixed size of a FlatBuffer.
0079 template<typename SizeT = uoffset_t>
0080 inline SizeT GetPrefixedSize(const uint8_t *buf) {
0081   return ReadScalar<SizeT>(buf);
0082 }
0083 
0084 // Gets the total length of the buffer given a sized prefixed FlatBuffer.
0085 //
0086 // This includes the size of the prefix as well as the buffer:
0087 //
0088 //  [size prefix][flatbuffer]
0089 //  |---------length--------|
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 // Base class for native objects (FlatBuffer data de-serialized into native
0096 // C++ data structures).
0097 // Contains no functionality, purely documentative.
0098 struct NativeTable {};
0099 
0100 /// @brief Function types to be used with resolving hashes into objects and
0101 /// back again. The resolver gets a pointer to a field inside an object API
0102 /// object that is of the type specified in the schema using the attribute
0103 /// `cpp_type` (it is thus important whatever you write to this address
0104 /// matches that type). The value of this field is initially null, so you
0105 /// may choose to implement a delayed binding lookup using this function
0106 /// if you wish. The resolver does the opposite lookup, for when the object
0107 /// is being serialized again.
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 // Helper function to test if a field is present, using any of the field
0114 // enums in the generated code.
0115 // `table` must be a generated table type. Since this is a template parameter,
0116 // this is not typechecked to be a subclass of Table, so beware!
0117 // Note: this function will return false for fields equal to the default
0118 // value, since they're not stored in the buffer (unless force_defaults was
0119 // used).
0120 template<typename T>
0121 bool IsFieldPresent(const T *table, typename T::FlatBuffersVTableOffset field) {
0122   // Cast, since Table is a private baseclass of any table types.
0123   return reinterpret_cast<const Table *>(table)->CheckField(
0124       static_cast<voffset_t>(field));
0125 }
0126 
0127 // Utility function for reverse lookups on the EnumNames*() functions
0128 // (in the generated C++ code)
0129 // names must be NULL terminated.
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 // These macros allow us to layout a struct with a guarantee that they'll end
0137 // up looking the same on different compilers and platforms.
0138 // It does this by disallowing the compiler to do any padding, and then
0139 // does padding itself by inserting extra padding fields that make every
0140 // element aligned to its own size.
0141 // Additionally, it manually sets the alignment of the struct as a whole,
0142 // which is typically its largest element, or a custom size set in the schema
0143 // by the force_align attribute.
0144 // These are used in the generated code only.
0145 
0146 // clang-format off
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 // clang-format on
0165 
0166 // Minimal reflection via code generation.
0167 // Besides full-fat reflection (see reflection.h) and parsing/printing by
0168 // loading schemas (see idl.h), we can also have code generation for minimal
0169 // reflection data which allows pretty-printing and other uses without needing
0170 // a schema or a parser.
0171 // Generate code with --reflect-types (types only) or --reflect-names (names
0172 // also) to enable.
0173 // See minireflect.h for utilities using this functionality.
0174 
0175 // These types are organized slightly differently as the ones in idl.h.
0176 enum SequenceType { ST_TABLE, ST_STRUCT, ST_UNION, ST_ENUM };
0177 
0178 // Scalars have the same order as in idl.h
0179 // clang-format off
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)  // See SequenceType.
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 // clang-format on
0211 
0212 // Basic type info cost just 16bits per field!
0213 // We're explicitly defining the signedness since the signedness of integer
0214 // bitfields is otherwise implementation-defined and causes warnings on older
0215 // GCC compilers.
0216 struct TypeCode {
0217   // ElementaryType
0218   unsigned short base_type : 4;
0219   // Either vector (in table) or array (in struct)
0220   unsigned short is_repeating : 1;
0221   // Index into type_refs below, or -1 for none.
0222   signed short sequence_ref : 11;
0223 };
0224 
0225 static_assert(sizeof(TypeCode) == 2, "TypeCode");
0226 
0227 struct TypeTable;
0228 
0229 // Signature of the static method present in each type.
0230 typedef const TypeTable *(*TypeFunction)();
0231 
0232 struct TypeTable {
0233   SequenceType st;
0234   size_t num_elems;  // of type_codes, values, names (but not type_refs).
0235   const TypeCode *type_codes;     // num_elems count
0236   const TypeFunction *type_refs;  // less than num_elems entries (see TypeCode).
0237   const int16_t *array_sizes;     // less than num_elems entries (see TypeCode).
0238   const int64_t *values;  // Only set for non-consecutive enum/union or structs.
0239   const char *const *names;  // Only set if compiled with --reflect-names.
0240 };
0241 
0242 // String which identifies the current version of FlatBuffers.
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 // clang-format off
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 /// @endcond
0280 }  // namespace flatbuffers
0281 
0282 // clang-format on
0283 
0284 #endif  // FLATBUFFERS_H_