1#ifndef FLATBUFFERS_BASE_H_
2#define FLATBUFFERS_BASE_H_
7#if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
8 defined(_MSC_VER) && defined(_DEBUG)
11 #define _CRTDBG_MAP_ALLOC
15 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
19#if !defined(FLATBUFFERS_ASSERT)
21#define FLATBUFFERS_ASSERT assert
22#elif defined(FLATBUFFERS_ASSERT_INCLUDE)
24#include FLATBUFFERS_ASSERT_INCLUDE
35#if defined(ARDUINO) && !defined(ARDUINOSTL_M_H) && defined(__AVR__)
50#if defined(__unix__) && !defined(FLATBUFFERS_LOCALE_INDEPENDENT)
55 #include <android/api-level.h>
58#if defined(__ICCARM__)
59#include <intrinsics.h>
77#if defined(__GNUC__) && !defined(__clang__)
78 #define FLATBUFFERS_GCC (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
80 #define FLATBUFFERS_GCC 0
84 #define FLATBUFFERS_CLANG (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
86 #define FLATBUFFERS_CLANG 0
90#if __cplusplus <= 199711L && \
91 (!defined(_MSC_VER) || _MSC_VER < 1600) && \
92 (!defined(__GNUC__) || \
93 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40400))
94 #error A C++11 compatible compiler with support for the auto typing is \
95 required for FlatBuffers.
96 #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
99#if !defined(__clang__) && \
100 defined(__GNUC__) && \
101 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600)
106 const class nullptr_t {
108 template<
class T>
inline operator T*()
const {
return 0; }
110 void operator&()
const;
114 #define constexpr const
120#if defined(__s390x__)
121 #define FLATBUFFERS_LITTLEENDIAN 0
123#if !defined(FLATBUFFERS_LITTLEENDIAN)
124 #if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
125 #if (defined(__BIG_ENDIAN__) || \
126 (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
127 #define FLATBUFFERS_LITTLEENDIAN 0
129 #define FLATBUFFERS_LITTLEENDIAN 1
131 #elif defined(_MSC_VER)
133 #define FLATBUFFERS_LITTLEENDIAN 0
135 #define FLATBUFFERS_LITTLEENDIAN 1
138 #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN.
142#define FLATBUFFERS_VERSION_MAJOR 23
143#define FLATBUFFERS_VERSION_MINOR 5
144#define FLATBUFFERS_VERSION_REVISION 26
145#define FLATBUFFERS_STRING_EXPAND(X) #X
146#define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
149 const char* FLATBUFFERS_VERSION();
152#if (!defined(_MSC_VER) || _MSC_VER > 1600) && \
153 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407)) || \
155 #define FLATBUFFERS_FINAL_CLASS final
156 #define FLATBUFFERS_OVERRIDE override
157 #define FLATBUFFERS_EXPLICIT_CPP11 explicit
158 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE : flatbuffers::voffset_t
160 #define FLATBUFFERS_FINAL_CLASS
161 #define FLATBUFFERS_OVERRIDE
162 #define FLATBUFFERS_EXPLICIT_CPP11
163 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE
166#if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
167 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \
168 (defined(__cpp_constexpr) && __cpp_constexpr >= 200704)
169 #define FLATBUFFERS_CONSTEXPR constexpr
170 #define FLATBUFFERS_CONSTEXPR_CPP11 constexpr
171 #define FLATBUFFERS_CONSTEXPR_DEFINED
173 #define FLATBUFFERS_CONSTEXPR const
174 #define FLATBUFFERS_CONSTEXPR_CPP11
177#if (defined(__cplusplus) && __cplusplus >= 201402L) || \
178 (defined(__cpp_constexpr) && __cpp_constexpr >= 201304)
179 #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR_CPP11
181 #define FLATBUFFERS_CONSTEXPR_CPP14
184#if (defined(__GXX_EXPERIMENTAL_CXX0X__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \
185 (defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190023026)) || \
187 #define FLATBUFFERS_NOEXCEPT noexcept
189 #define FLATBUFFERS_NOEXCEPT
194#if (!defined(_MSC_VER) || _MSC_FULL_VER >= 180020827) && \
195 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 404)) || \
197 #define FLATBUFFERS_DELETE_FUNC(func) func = delete
199 #define FLATBUFFERS_DELETE_FUNC(func) private: func
202#if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
203 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \
205 #define FLATBUFFERS_DEFAULT_DECLARATION
212#if (defined(_MSC_VER) && _MSC_VER > 1700 ) \
213 || (defined(__cpp_alias_templates) && __cpp_alias_templates >= 200704) \
214 || (defined(__cplusplus) && __cplusplus >= 201103L)
215 #define FLATBUFFERS_TEMPLATES_ALIASES
218#ifndef FLATBUFFERS_HAS_STRING_VIEW
221 #if defined(__has_include)
223 #if __has_include(<string_view>) && (__cplusplus >= 201606 || (defined(_HAS_CXX17) && _HAS_CXX17))
224 #include <string_view>
226 typedef std::string_view string_view;
228 #define FLATBUFFERS_HAS_STRING_VIEW 1
230 #elif __has_include(<experimental/string_view>) && (__cplusplus >= 201411)
231 #include <experimental/string_view>
233 typedef std::experimental::string_view string_view;
235 #define FLATBUFFERS_HAS_STRING_VIEW 1
237 #elif __has_include("absl/strings/string_view.h") && \
238 __has_include("absl/base/config.h") && \
239 (__cplusplus >= 201411)
240 #include "absl/base/config.h"
241 #if !defined(ABSL_USES_STD_STRING_VIEW)
242 #include "absl/strings/string_view.h"
244 typedef absl::string_view string_view;
246 #define FLATBUFFERS_HAS_STRING_VIEW 1
252#ifndef FLATBUFFERS_GENERAL_HEAP_ALLOC_OK
254 #define FLATBUFFERS_GENERAL_HEAP_ALLOC_OK 1
257#ifndef FLATBUFFERS_HAS_NEW_STRTOD
261 #if (defined(_MSC_VER) && _MSC_VER >= 1900) || \
262 (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \
264 #define FLATBUFFERS_HAS_NEW_STRTOD 1
268#ifndef FLATBUFFERS_LOCALE_INDEPENDENT
271 #if (defined(_MSC_VER) && _MSC_VER >= 1800) || \
272 (defined(__ANDROID_API__) && __ANDROID_API__>= 21) || \
273 (defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 700)) && \
274 (!defined(__Fuchsia__) && !defined(__ANDROID_API__))
275 #define FLATBUFFERS_LOCALE_INDEPENDENT 1
277 #define FLATBUFFERS_LOCALE_INDEPENDENT 0
284#if defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))
285 #define FLATBUFFERS_SUPPRESS_UBSAN(type) __attribute__((no_sanitize(type)))
286#elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)
287 #define FLATBUFFERS_SUPPRESS_UBSAN(type) __attribute__((no_sanitize_undefined))
289 #define FLATBUFFERS_SUPPRESS_UBSAN(type)
294template<
typename T> FLATBUFFERS_CONSTEXPR
inline bool IsConstTrue(T t) {
299#if ((__cplusplus >= 201703L) \
300 || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
302 #define FLATBUFFERS_ATTRIBUTE(attr) attr
304 #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]]
306 #define FLATBUFFERS_ATTRIBUTE(attr)
308 #if FLATBUFFERS_CLANG >= 30800
309 #define FLATBUFFERS_FALLTHROUGH() [[clang::fallthrough]]
310 #elif FLATBUFFERS_GCC >= 70300
311 #define FLATBUFFERS_FALLTHROUGH() [[gnu::fallthrough]]
313 #define FLATBUFFERS_FALLTHROUGH()
326typedef uint32_t uoffset_t;
327typedef uint64_t uoffset64_t;
330typedef int32_t soffset_t;
331typedef int64_t soffset64_t;
335typedef uint16_t voffset_t;
337typedef uintmax_t largest_scalar_t;
340#define FLATBUFFERS_MAX_BUFFER_SIZE std::numeric_limits<::flatbuffers::soffset_t>::max()
341#define FLATBUFFERS_MAX_64_BUFFER_SIZE std::numeric_limits<::flatbuffers::soffset64_t>::max()
347#define FLATBUFFERS_MIN_BUFFER_SIZE sizeof(uoffset_t) + sizeof(soffset_t) + \
348 sizeof(uint16_t) + sizeof(uint16_t)
351#ifndef FLATBUFFERS_MAX_ALIGNMENT
352 #define FLATBUFFERS_MAX_ALIGNMENT 32
356static const size_t kFileIdentifierLength = 4;
358inline bool VerifyAlignmentRequirements(
size_t align,
size_t min_align = 1) {
359 return (min_align <= align) && (align <= (FLATBUFFERS_MAX_ALIGNMENT)) &&
360 (align & (align - 1)) == 0;
364 #pragma warning(disable: 4351)
365 #pragma warning(push)
366 #pragma warning(disable: 4127)
369template<
typename T> T EndianSwap(T t) {
370 #if defined(_MSC_VER)
371 #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort
372 #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong
373 #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64
374 #elif defined(__ICCARM__)
375 #define FLATBUFFERS_BYTESWAP16 __REV16
376 #define FLATBUFFERS_BYTESWAP32 __REV
377 #define FLATBUFFERS_BYTESWAP64(x) \
378 ((__REV(static_cast<uint32_t>(x >> 32U))) | (static_cast<uint64_t>(__REV(static_cast<uint32_t>(x)))) << 32U)
380 #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__)
382 #define FLATBUFFERS_BYTESWAP16(x) \
383 static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16))
385 #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16
387 #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32
388 #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64
390 if (
sizeof(T) == 1) {
392 }
else if (
sizeof(T) == 2) {
393 union { T t; uint16_t i; } u = { t };
394 u.i = FLATBUFFERS_BYTESWAP16(u.i);
396 }
else if (
sizeof(T) == 4) {
397 union { T t; uint32_t i; } u = { t };
398 u.i = FLATBUFFERS_BYTESWAP32(u.i);
400 }
else if (
sizeof(T) == 8) {
401 union { T t; uint64_t i; } u = { t };
402 u.i = FLATBUFFERS_BYTESWAP64(u.i);
415template<
typename T> T EndianScalar(T t) {
416 #if FLATBUFFERS_LITTLEENDIAN
419 return EndianSwap(t);
425FLATBUFFERS_SUPPRESS_UBSAN(
"alignment")
426T ReadScalar(const
void *
p) {
427 return EndianScalar(*
reinterpret_cast<const T *
>(
p));
432#if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
433 #pragma GCC diagnostic push
434 #pragma GCC diagnostic ignored "-Wstringop-overflow"
439FLATBUFFERS_SUPPRESS_UBSAN(
"alignment")
440void WriteScalar(
void *
p, T t) {
441 *
reinterpret_cast<T *
>(
p) = EndianScalar(t);
444template<
typename T>
struct Offset;
445template<
typename T> FLATBUFFERS_SUPPRESS_UBSAN(
"alignment") void WriteScalar(
void *
p,
Offset<T> t) {
446 *
reinterpret_cast<uoffset_t *
>(
p) = EndianScalar(t.o);
449#if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
450 #pragma GCC diagnostic pop
456FLATBUFFERS_SUPPRESS_UBSAN(
"unsigned-integer-overflow")
457inline
size_t PaddingBytes(
size_t buf_size,
size_t scalar_size) {
458 return ((~buf_size) + 1) & (scalar_size - 1);
464template<
typename T>
inline bool IsTheSameAs(T e, T def) {
return e == def; }
466#if defined(FLATBUFFERS_NAN_DEFAULTS) && \
467 defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
469template<
typename T>
inline bool IsFloatTheSameAs(T e, T def) {
470 return (e == def) || ((def != def) && (e != e));
472template<>
inline bool IsTheSameAs<float>(
float e,
float def) {
473 return IsFloatTheSameAs(e, def);
475template<>
inline bool IsTheSameAs<double>(
double e,
double def) {
476 return IsFloatTheSameAs(e, def);
484inline bool IsOutRange(
const T &v,
const T &low,
const T &high) {
485 return (v < low) || (high < v);
490inline bool IsInRange(
const T &v,
const T &low,
const T &high) {
491 return !IsOutRange(v, low, high);
int Offset(const Dims< 4 > &dims, int i0, int i1, int i2, int i3)
#define FLATBUFFERS_ASSERT