1 #ifndef FLATBUFFERS_BASE_H_
2 #define FLATBUFFERS_BASE_H_
3
4 // clang-format off
5
6 // If activate should be declared and included first.
7 #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
8 defined(_MSC_VER) && defined(_DEBUG)
9 // The _CRTDBG_MAP_ALLOC inside <crtdbg.h> will replace
10 // calloc/free (etc) to its debug version using #define directives.
11 #define _CRTDBG_MAP_ALLOC
12 #include <stdlib.h>
13 #include <crtdbg.h>
14 // Replace operator new by trace-enabled version.
15 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
16 #define new DEBUG_NEW
17 #endif
18
19 #if !defined(FLATBUFFERS_ASSERT)
20 #include <assert.h>
21 #define FLATBUFFERS_ASSERT assert
22 #elif defined(FLATBUFFERS_ASSERT_INCLUDE)
23 // Include file with forward declaration
24 #include FLATBUFFERS_ASSERT_INCLUDE
25 #endif
26
27 #ifndef ARDUINO
28 #include <cstdint>
29 #endif
30
31 #include <cstddef>
32 #include <cstdlib>
33 #include <cstring>
34
35 #if defined(ARDUINO) && !defined(ARDUINOSTL_M_H)
36 #include <utility.h>
37 #else
38 #include <utility>
39 #endif
40
41 #include <string>
42 #include <type_traits>
43 #include <vector>
44 #include <set>
45 #include <algorithm>
46 #include <iterator>
47 #include <memory>
48
49 #if defined(__unix__) && !defined(FLATBUFFERS_LOCALE_INDEPENDENT)
50 #include <unistd.h>
51 #endif
52
53 #ifdef _STLPORT_VERSION
54 #define FLATBUFFERS_CPP98_STL
55 #endif
56
57 #ifdef __ANDROID__
58 #include <android/api-level.h>
59 #endif
60
61 #if defined(__ICCARM__)
62 #include <intrinsics.h>
63 #endif
64
65 // Note the __clang__ check is needed, because clang presents itself
66 // as an older GNUC compiler (4.2).
67 // Clang 3.3 and later implement all of the ISO C++ 2011 standard.
68 // Clang 3.4 and later implement all of the ISO C++ 2014 standard.
69 // http://clang.llvm.org/cxx_status.html
70
71 // Note the MSVC value '__cplusplus' may be incorrect:
72 // The '__cplusplus' predefined macro in the MSVC stuck at the value 199711L,
73 // indicating (erroneously!) that the compiler conformed to the C++98 Standard.
74 // This value should be correct starting from MSVC2017-15.7-Preview-3.
75 // The '__cplusplus' will be valid only if MSVC2017-15.7-P3 and the `/Zc:__cplusplus` switch is set.
76 // Workaround (for details see MSDN):
77 // Use the _MSC_VER and _MSVC_LANG definition instead of the __cplusplus for compatibility.
78 // The _MSVC_LANG macro reports the Standard version regardless of the '/Zc:__cplusplus' switch.
79
80 #if defined(__GNUC__) && !defined(__clang__)
81 #define FLATBUFFERS_GCC (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
82 #else
83 #define FLATBUFFERS_GCC 0
84 #endif
85
86 #if defined(__clang__)
87 #define FLATBUFFERS_CLANG (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
88 #else
89 #define FLATBUFFERS_CLANG 0
90 #endif
91
92 /// @cond FLATBUFFERS_INTERNAL
93 #if __cplusplus <= 199711L && \
94 (!defined(_MSC_VER) || _MSC_VER < 1600) && \
95 (!defined(__GNUC__) || \
96 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40400))
97 #error A C++11 compatible compiler with support for the auto typing is \
98 required for FlatBuffers.
99 #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
100 #endif
101
102 #if !defined(__clang__) && \
103 defined(__GNUC__) && \
104 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600)
105 // Backwards compatibility for g++ 4.4, and 4.5 which don't have the nullptr
106 // and constexpr keywords. Note the __clang__ check is needed, because clang
107 // presents itself as an older GNUC compiler.
108 #ifndef nullptr_t
109 const class nullptr_t {
110 public:
111 template<class T> inline operator T*() const { return 0; }
112 private:
113 void operator&() const;
114 } nullptr = {};
115 #endif
116 #ifndef constexpr
117 #define constexpr const
118 #endif
119 #endif
120
121 // The wire format uses a little endian encoding (since that's efficient for
122 // the common platforms).
123 #if defined(__s390x__)
124 #define FLATBUFFERS_LITTLEENDIAN 0
125 #endif // __s390x__
126 #if !defined(FLATBUFFERS_LITTLEENDIAN)
127 #if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
128 #if (defined(__BIG_ENDIAN__) || \
129 (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
130 #define FLATBUFFERS_LITTLEENDIAN 0
131 #else
132 #define FLATBUFFERS_LITTLEENDIAN 1
133 #endif // __BIG_ENDIAN__
134 #elif defined(_MSC_VER)
135 #if defined(_M_PPC)
136 #define FLATBUFFERS_LITTLEENDIAN 0
137 #else
138 #define FLATBUFFERS_LITTLEENDIAN 1
139 #endif
140 #else
141 #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN.
142 #endif
143 #endif // !defined(FLATBUFFERS_LITTLEENDIAN)
144
145 #define FLATBUFFERS_VERSION_MAJOR 2
146 #define FLATBUFFERS_VERSION_MINOR 0
147 #define FLATBUFFERS_VERSION_REVISION 0
148 #define FLATBUFFERS_STRING_EXPAND(X) #X
149 #define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
150 namespace flatbuffers {
151 // Returns version as string "MAJOR.MINOR.REVISION".
152 const char* FLATBUFFERS_VERSION();
153 }
154
155 #if (!defined(_MSC_VER) || _MSC_VER > 1600) && \
156 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407)) || \
157 defined(__clang__)
158 #define FLATBUFFERS_FINAL_CLASS final
159 #define FLATBUFFERS_OVERRIDE override
160 #define FLATBUFFERS_EXPLICIT_CPP11 explicit
161 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE : flatbuffers::voffset_t
162 #else
163 #define FLATBUFFERS_FINAL_CLASS
164 #define FLATBUFFERS_OVERRIDE
165 #define FLATBUFFERS_EXPLICIT_CPP11
166 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE
167 #endif
168
169 #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
170 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \
171 (defined(__cpp_constexpr) && __cpp_constexpr >= 200704)
172 #define FLATBUFFERS_CONSTEXPR constexpr
173 #define FLATBUFFERS_CONSTEXPR_CPP11 constexpr
174 #define FLATBUFFERS_CONSTEXPR_DEFINED
175 #else
176 #define FLATBUFFERS_CONSTEXPR const
177 #define FLATBUFFERS_CONSTEXPR_CPP11
178 #endif
179
180 #if (defined(__cplusplus) && __cplusplus >= 201402L) || \
181 (defined(__cpp_constexpr) && __cpp_constexpr >= 201304)
182 #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR_CPP11
183 #else
184 #define FLATBUFFERS_CONSTEXPR_CPP14
185 #endif
186
187 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \
188 (defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190023026)) || \
189 defined(__clang__)
190 #define FLATBUFFERS_NOEXCEPT noexcept
191 #else
192 #define FLATBUFFERS_NOEXCEPT
193 #endif
194
195 // NOTE: the FLATBUFFERS_DELETE_FUNC macro may change the access mode to
196 // private, so be sure to put it at the end or reset access mode explicitly.
197 #if (!defined(_MSC_VER) || _MSC_FULL_VER >= 180020827) && \
198 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 404)) || \
199 defined(__clang__)
200 #define FLATBUFFERS_DELETE_FUNC(func) func = delete
201 #else
202 #define FLATBUFFERS_DELETE_FUNC(func) private: func
203 #endif
204
205 #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
206 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \
207 defined(__clang__)
208 #define FLATBUFFERS_DEFAULT_DECLARATION
209 #endif
210
211 // Check if we can use template aliases
212 // Not possible if Microsoft Compiler before 2012
213 // Possible is the language feature __cpp_alias_templates is defined well
214 // Or possible if the C++ std is C+11 or newer
215 #if (defined(_MSC_VER) && _MSC_VER > 1700 /* MSVC2012 */) \
216 || (defined(__cpp_alias_templates) && __cpp_alias_templates >= 200704) \
217 || (defined(__cplusplus) && __cplusplus >= 201103L)
218 #define FLATBUFFERS_TEMPLATES_ALIASES
219 #endif
220
221 #ifndef FLATBUFFERS_HAS_STRING_VIEW
222 // Only provide flatbuffers::string_view if __has_include can be used
223 // to detect a header that provides an implementation
224 #if defined(__has_include)
225 // Check for std::string_view (in c++17)
226 #if __has_include(<string_view>) && (__cplusplus >= 201606 || (defined(_HAS_CXX17) && _HAS_CXX17))
227 #include <string_view>
228 namespace flatbuffers {
229 typedef std::string_view string_view;
230 }
231 #define FLATBUFFERS_HAS_STRING_VIEW 1
232 // Check for std::experimental::string_view (in c++14, compiler-dependent)
233 #elif __has_include(<experimental/string_view>) && (__cplusplus >= 201411)
234 #include <experimental/string_view>
235 namespace flatbuffers {
236 typedef std::experimental::string_view string_view;
237 }
238 #define FLATBUFFERS_HAS_STRING_VIEW 1
239 // Check for absl::string_view
240 #elif __has_include("absl/strings/string_view.h")
241 #include "absl/strings/string_view.h"
242 namespace flatbuffers {
243 typedef absl::string_view string_view;
244 }
245 #define FLATBUFFERS_HAS_STRING_VIEW 1
246 #endif
247 #endif // __has_include
248 #endif // !FLATBUFFERS_HAS_STRING_VIEW
249
250 #ifndef FLATBUFFERS_HAS_NEW_STRTOD
251 // Modern (C++11) strtod and strtof functions are available for use.
252 // 1) nan/inf strings as argument of strtod;
253 // 2) hex-float as argument of strtod/strtof.
254 #if (defined(_MSC_VER) && _MSC_VER >= 1900) || \
255 (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \
256 (defined(__clang__))
257 #define FLATBUFFERS_HAS_NEW_STRTOD 1
258 #endif
259 #endif // !FLATBUFFERS_HAS_NEW_STRTOD
260
261 #ifndef FLATBUFFERS_LOCALE_INDEPENDENT
262 // Enable locale independent functions {strtof_l, strtod_l,strtoll_l, strtoull_l}.
263 #if ((defined(_MSC_VER) && _MSC_VER >= 1800) || \
264 (defined(_XOPEN_VERSION) && (_XOPEN_VERSION>=700)) && (!defined(__ANDROID_API__) || (defined(__ANDROID_API__) && (__ANDROID_API__>=21))))
265 #define FLATBUFFERS_LOCALE_INDEPENDENT 1
266 #else
267 #define FLATBUFFERS_LOCALE_INDEPENDENT 0
268 #endif
269 #endif // !FLATBUFFERS_LOCALE_INDEPENDENT
270
271 // Suppress Undefined Behavior Sanitizer (recoverable only). Usage:
272 // - __supress_ubsan__("undefined")
273 // - __supress_ubsan__("signed-integer-overflow")
274 #if defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))
275 #define __supress_ubsan__(type) __attribute__((no_sanitize(type)))
276 #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)
277 #define __supress_ubsan__(type) __attribute__((no_sanitize_undefined))
278 #else
279 #define __supress_ubsan__(type)
280 #endif
281
282 // This is constexpr function used for checking compile-time constants.
283 // Avoid `#pragma warning(disable: 4127) // C4127: expression is constant`.
IsConstTrue(T t)284 template<typename T> FLATBUFFERS_CONSTEXPR inline bool IsConstTrue(T t) {
285 return !!t;
286 }
287
288 // Enable C++ attribute [[]] if std:c++17 or higher.
289 #if ((__cplusplus >= 201703L) \
290 || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
291 // All attributes unknown to an implementation are ignored without causing an error.
292 #define FLATBUFFERS_ATTRIBUTE(attr) [[attr]]
293
294 #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]]
295 #else
296 #define FLATBUFFERS_ATTRIBUTE(attr)
297
298 #if FLATBUFFERS_CLANG >= 30800
299 #define FLATBUFFERS_FALLTHROUGH() [[clang::fallthrough]]
300 #elif FLATBUFFERS_GCC >= 70300
301 #define FLATBUFFERS_FALLTHROUGH() [[gnu::fallthrough]]
302 #else
303 #define FLATBUFFERS_FALLTHROUGH()
304 #endif
305 #endif
306
307 /// @endcond
308
309 /// @file
310 namespace flatbuffers {
311
312 /// @cond FLATBUFFERS_INTERNAL
313 // Our default offset / size type, 32bit on purpose on 64bit systems.
314 // Also, using a consistent offset type maintains compatibility of serialized
315 // offset values between 32bit and 64bit systems.
316 typedef uint32_t uoffset_t;
317
318 // Signed offsets for references that can go in both directions.
319 typedef int32_t soffset_t;
320
321 // Offset/index used in v-tables, can be changed to uint8_t in
322 // format forks to save a bit of space if desired.
323 typedef uint16_t voffset_t;
324
325 typedef uintmax_t largest_scalar_t;
326
327 // In 32bits, this evaluates to 2GB - 1
328 #define FLATBUFFERS_MAX_BUFFER_SIZE ((1ULL << (sizeof(::flatbuffers::soffset_t) * 8 - 1)) - 1)
329
330 // We support aligning the contents of buffers up to this size.
331 #define FLATBUFFERS_MAX_ALIGNMENT 16
332
333 inline bool VerifyAlignmentRequirements(size_t align, size_t min_align = 1) {
334 return (min_align <= align) && (align <= (FLATBUFFERS_MAX_ALIGNMENT)) &&
335 (align & (align - 1)) == 0; // must be power of 2
336 }
337
338 #if defined(_MSC_VER)
339 #pragma warning(disable: 4351) // C4351: new behavior: elements of array ... will be default initialized
340 #pragma warning(push)
341 #pragma warning(disable: 4127) // C4127: conditional expression is constant
342 #endif
343
EndianSwap(T t)344 template<typename T> T EndianSwap(T t) {
345 #if defined(_MSC_VER)
346 #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort
347 #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong
348 #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64
349 #elif defined(__ICCARM__)
350 #define FLATBUFFERS_BYTESWAP16 __REV16
351 #define FLATBUFFERS_BYTESWAP32 __REV
352 #define FLATBUFFERS_BYTESWAP64(x) \
353 ((__REV(static_cast<uint32_t>(x >> 32U))) | (static_cast<uint64_t>(__REV(static_cast<uint32_t>(x)))) << 32U)
354 #else
355 #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__)
356 // __builtin_bswap16 was missing prior to GCC 4.8.
357 #define FLATBUFFERS_BYTESWAP16(x) \
358 static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16))
359 #else
360 #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16
361 #endif
362 #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32
363 #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64
364 #endif
365 if (sizeof(T) == 1) { // Compile-time if-then's.
366 return t;
367 } else if (sizeof(T) == 2) {
368 union { T t; uint16_t i; } u = { t };
369 u.i = FLATBUFFERS_BYTESWAP16(u.i);
370 return u.t;
371 } else if (sizeof(T) == 4) {
372 union { T t; uint32_t i; } u = { t };
373 u.i = FLATBUFFERS_BYTESWAP32(u.i);
374 return u.t;
375 } else if (sizeof(T) == 8) {
376 union { T t; uint64_t i; } u = { t };
377 u.i = FLATBUFFERS_BYTESWAP64(u.i);
378 return u.t;
379 } else {
380 FLATBUFFERS_ASSERT(0);
381 return t;
382 }
383 }
384
385 #if defined(_MSC_VER)
386 #pragma warning(pop)
387 #endif
388
389
EndianScalar(T t)390 template<typename T> T EndianScalar(T t) {
391 #if FLATBUFFERS_LITTLEENDIAN
392 return t;
393 #else
394 return EndianSwap(t);
395 #endif
396 }
397
398 template<typename T>
399 // UBSAN: C++ aliasing type rules, see std::bit_cast<> for details.
400 __supress_ubsan__("alignment")
ReadScalar(const void * p)401 T ReadScalar(const void *p) {
402 return EndianScalar(*reinterpret_cast<const T *>(p));
403 }
404
405 // See https://github.com/google/flatbuffers/issues/5950
406
407 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
408 #pragma GCC diagnostic push
409 #pragma GCC diagnostic ignored "-Wstringop-overflow"
410 #endif
411
412 template<typename T>
413 // UBSAN: C++ aliasing type rules, see std::bit_cast<> for details.
414 __supress_ubsan__("alignment")
WriteScalar(void * p,T t)415 void WriteScalar(void *p, T t) {
416 *reinterpret_cast<T *>(p) = EndianScalar(t);
417 }
418
419 template<typename T> struct Offset;
WriteScalar(void * p,Offset<T> t)420 template<typename T> __supress_ubsan__("alignment") void WriteScalar(void *p, Offset<T> t) {
421 *reinterpret_cast<uoffset_t *>(p) = EndianScalar(t.o);
422 }
423
424 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
425 #pragma GCC diagnostic pop
426 #endif
427
428 // Computes how many bytes you'd have to pad to be able to write an
429 // "scalar_size" scalar if the buffer had grown to "buf_size" (downwards in
430 // memory).
431 __supress_ubsan__("unsigned-integer-overflow")
PaddingBytes(size_t buf_size,size_t scalar_size)432 inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) {
433 return ((~buf_size) + 1) & (scalar_size - 1);
434 }
435
436 } // namespace flatbuffers
437 #endif // FLATBUFFERS_BASE_H_
438