1/*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef FLATBUFFERS_H_
18#define FLATBUFFERS_H_
19
20#include <algorithm>
21
22// TODO: These includes are for mitigating the pains of users editing their
23// source because they relied on flatbuffers.h to include everything for them.
24#include "flatbuffers/array.h"
25#include "flatbuffers/base.h"
26#include "flatbuffers/buffer.h"
27#include "flatbuffers/buffer_ref.h"
28#include "flatbuffers/detached_buffer.h"
29#include "flatbuffers/flatbuffer_builder.h"
30#include "flatbuffers/stl_emulation.h"
31#include "flatbuffers/string.h"
32#include "flatbuffers/struct.h"
33#include "flatbuffers/table.h"
34#include "flatbuffers/vector.h"
35#include "flatbuffers/vector_downward.h"
36#include "flatbuffers/verifier.h"
37
38namespace flatbuffers {
39
40/// @brief This can compute the start of a FlatBuffer from a root pointer, i.e.
41/// it is the opposite transformation of GetRoot().
42/// This may be useful if you want to pass on a root and have the recipient
43/// delete the buffer afterwards.
44inline const uint8_t *GetBufferStartFromRootPointer(const void *root) {
45 auto table = reinterpret_cast<const Table *>(root);
46 auto vtable = table->GetVTable();
47 // Either the vtable is before the root or after the root.
48 auto start = (std::min)(a: vtable, b: reinterpret_cast<const uint8_t *>(root));
49 // Align to at least sizeof(uoffset_t).
50 start = reinterpret_cast<const uint8_t *>(reinterpret_cast<uintptr_t>(start) &
51 ~(sizeof(uoffset_t) - 1));
52 // Additionally, there may be a file_identifier in the buffer, and the root
53 // offset. The buffer may have been aligned to any size between
54 // sizeof(uoffset_t) and FLATBUFFERS_MAX_ALIGNMENT (see "force_align").
55 // Sadly, the exact alignment is only known when constructing the buffer,
56 // since it depends on the presence of values with said alignment properties.
57 // So instead, we simply look at the next uoffset_t values (root,
58 // file_identifier, and alignment padding) to see which points to the root.
59 // None of the other values can "impersonate" the root since they will either
60 // be 0 or four ASCII characters.
61 static_assert(flatbuffers::kFileIdentifierLength == sizeof(uoffset_t),
62 "file_identifier is assumed to be the same size as uoffset_t");
63 for (auto possible_roots = FLATBUFFERS_MAX_ALIGNMENT / sizeof(uoffset_t) + 1;
64 possible_roots; possible_roots--) {
65 start -= sizeof(uoffset_t);
66 if (ReadScalar<uoffset_t>(p: start) + start ==
67 reinterpret_cast<const uint8_t *>(root))
68 return start;
69 }
70 // We didn't find the root, either the "root" passed isn't really a root,
71 // or the buffer is corrupt.
72 // Assert, because calling this function with bad data may cause reads
73 // outside of buffer boundaries.
74 FLATBUFFERS_ASSERT(false);
75 return nullptr;
76}
77
78/// @brief This return the prefixed size of a FlatBuffer.
79template<typename SizeT = uoffset_t>
80inline SizeT GetPrefixedSize(const uint8_t *buf) {
81 return ReadScalar<SizeT>(buf);
82}
83
84// Base class for native objects (FlatBuffer data de-serialized into native
85// C++ data structures).
86// Contains no functionality, purely documentative.
87struct NativeTable {};
88
89/// @brief Function types to be used with resolving hashes into objects and
90/// back again. The resolver gets a pointer to a field inside an object API
91/// object that is of the type specified in the schema using the attribute
92/// `cpp_type` (it is thus important whatever you write to this address
93/// matches that type). The value of this field is initially null, so you
94/// may choose to implement a delayed binding lookup using this function
95/// if you wish. The resolver does the opposite lookup, for when the object
96/// is being serialized again.
97typedef uint64_t hash_value_t;
98typedef std::function<void(void **pointer_adr, hash_value_t hash)>
99 resolver_function_t;
100typedef std::function<hash_value_t(void *pointer)> rehasher_function_t;
101
102// Helper function to test if a field is present, using any of the field
103// enums in the generated code.
104// `table` must be a generated table type. Since this is a template parameter,
105// this is not typechecked to be a subclass of Table, so beware!
106// Note: this function will return false for fields equal to the default
107// value, since they're not stored in the buffer (unless force_defaults was
108// used).
109template<typename T>
110bool IsFieldPresent(const T *table, typename T::FlatBuffersVTableOffset field) {
111 // Cast, since Table is a private baseclass of any table types.
112 return reinterpret_cast<const Table *>(table)->CheckField(
113 field: static_cast<voffset_t>(field));
114}
115
116// Utility function for reverse lookups on the EnumNames*() functions
117// (in the generated C++ code)
118// names must be NULL terminated.
119inline int LookupEnum(const char **names, const char *name) {
120 for (const char **p = names; *p; p++)
121 if (!strcmp(s1: *p, s2: name)) return static_cast<int>(p - names);
122 return -1;
123}
124
125// These macros allow us to layout a struct with a guarantee that they'll end
126// up looking the same on different compilers and platforms.
127// It does this by disallowing the compiler to do any padding, and then
128// does padding itself by inserting extra padding fields that make every
129// element aligned to its own size.
130// Additionally, it manually sets the alignment of the struct as a whole,
131// which is typically its largest element, or a custom size set in the schema
132// by the force_align attribute.
133// These are used in the generated code only.
134
135// clang-format off
136#if defined(_MSC_VER)
137 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \
138 __pragma(pack(1)) \
139 struct __declspec(align(alignment))
140 #define FLATBUFFERS_STRUCT_END(name, size) \
141 __pragma(pack()) \
142 static_assert(sizeof(name) == size, "compiler breaks packing rules")
143#elif defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
144 #define FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(alignment) \
145 _Pragma("pack(1)") \
146 struct __attribute__((aligned(alignment)))
147 #define FLATBUFFERS_STRUCT_END(name, size) \
148 _Pragma("pack()") \
149 static_assert(sizeof(name) == size, "compiler breaks packing rules")
150#else
151 #error Unknown compiler, please define structure alignment macros
152#endif
153// clang-format on
154
155// Minimal reflection via code generation.
156// Besides full-fat reflection (see reflection.h) and parsing/printing by
157// loading schemas (see idl.h), we can also have code generation for minimal
158// reflection data which allows pretty-printing and other uses without needing
159// a schema or a parser.
160// Generate code with --reflect-types (types only) or --reflect-names (names
161// also) to enable.
162// See minireflect.h for utilities using this functionality.
163
164// These types are organized slightly differently as the ones in idl.h.
165enum SequenceType { ST_TABLE, ST_STRUCT, ST_UNION, ST_ENUM };
166
167// Scalars have the same order as in idl.h
168// clang-format off
169#define FLATBUFFERS_GEN_ELEMENTARY_TYPES(ET) \
170 ET(ET_UTYPE) \
171 ET(ET_BOOL) \
172 ET(ET_CHAR) \
173 ET(ET_UCHAR) \
174 ET(ET_SHORT) \
175 ET(ET_USHORT) \
176 ET(ET_INT) \
177 ET(ET_UINT) \
178 ET(ET_LONG) \
179 ET(ET_ULONG) \
180 ET(ET_FLOAT) \
181 ET(ET_DOUBLE) \
182 ET(ET_STRING) \
183 ET(ET_SEQUENCE) // See SequenceType.
184
185enum ElementaryType {
186 #define FLATBUFFERS_ET(E) E,
187 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
188 #undef FLATBUFFERS_ET
189};
190
191inline const char * const *ElementaryTypeNames() {
192 static const char * const names[] = {
193 #define FLATBUFFERS_ET(E) #E,
194 FLATBUFFERS_GEN_ELEMENTARY_TYPES(FLATBUFFERS_ET)
195 #undef FLATBUFFERS_ET
196 };
197 return names;
198}
199// clang-format on
200
201// Basic type info cost just 16bits per field!
202// We're explicitly defining the signedness since the signedness of integer
203// bitfields is otherwise implementation-defined and causes warnings on older
204// GCC compilers.
205struct TypeCode {
206 // ElementaryType
207 unsigned short base_type : 4;
208 // Either vector (in table) or array (in struct)
209 unsigned short is_repeating : 1;
210 // Index into type_refs below, or -1 for none.
211 signed short sequence_ref : 11;
212};
213
214static_assert(sizeof(TypeCode) == 2, "TypeCode");
215
216struct TypeTable;
217
218// Signature of the static method present in each type.
219typedef const TypeTable *(*TypeFunction)();
220
221struct TypeTable {
222 SequenceType st;
223 size_t num_elems; // of type_codes, values, names (but not type_refs).
224 const TypeCode *type_codes; // num_elems count
225 const TypeFunction *type_refs; // less than num_elems entries (see TypeCode).
226 const int16_t *array_sizes; // less than num_elems entries (see TypeCode).
227 const int64_t *values; // Only set for non-consecutive enum/union or structs.
228 const char *const *names; // Only set if compiled with --reflect-names.
229};
230
231// String which identifies the current version of FlatBuffers.
232inline const char *flatbuffers_version_string() {
233 return "FlatBuffers " FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
234 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
235 FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
236}
237
238// clang-format off
239#define FLATBUFFERS_DEFINE_BITMASK_OPERATORS(E, T)\
240 inline E operator | (E lhs, E rhs){\
241 return E(T(lhs) | T(rhs));\
242 }\
243 inline E operator & (E lhs, E rhs){\
244 return E(T(lhs) & T(rhs));\
245 }\
246 inline E operator ^ (E lhs, E rhs){\
247 return E(T(lhs) ^ T(rhs));\
248 }\
249 inline E operator ~ (E lhs){\
250 return E(~T(lhs));\
251 }\
252 inline E operator |= (E &lhs, E rhs){\
253 lhs = lhs | rhs;\
254 return lhs;\
255 }\
256 inline E operator &= (E &lhs, E rhs){\
257 lhs = lhs & rhs;\
258 return lhs;\
259 }\
260 inline E operator ^= (E &lhs, E rhs){\
261 lhs = lhs ^ rhs;\
262 return lhs;\
263 }\
264 inline bool operator !(E rhs) \
265 {\
266 return !bool(T(rhs)); \
267 }
268/// @endcond
269} // namespace flatbuffers
270
271// clang-format on
272
273#endif // FLATBUFFERS_H_
274

source code of opencv/3rdparty/flatbuffers/include/flatbuffers/flatbuffers.h