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__)
42 #include <type_traits>
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
83 #if defined(__clang__)
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; }
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
130 #endif // __BIG_ENDIAN__
131 #elif defined(_MSC_VER)
133 #define FLATBUFFERS_LITTLEENDIAN 0
135 #define FLATBUFFERS_LITTLEENDIAN 1
138 #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN.
140 #endif // !defined(FLATBUFFERS_LITTLEENDIAN)
142 #define FLATBUFFERS_VERSION_MAJOR 24
143 #define FLATBUFFERS_VERSION_MINOR 3
144 #define FLATBUFFERS_VERSION_REVISION 25
145 #define FLATBUFFERS_STRING_EXPAND(X) #X
146 #define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
147 namespace flatbuffers {
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>
225 namespace flatbuffers {
228 #define FLATBUFFERS_HAS_STRING_VIEW 1
230 #elif __has_include(<experimental/string_view>) && (__cplusplus >= 201411)
231 #include <experimental/string_view>
232 namespace flatbuffers {
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"
243 namespace flatbuffers {
246 #define FLATBUFFERS_HAS_STRING_VIEW 1
249 #endif // __has_include
250 #endif // !FLATBUFFERS_HAS_STRING_VIEW
252 #ifndef FLATBUFFERS_GENERAL_HEAP_ALLOC_OK
254 #define FLATBUFFERS_GENERAL_HEAP_ALLOC_OK 1
255 #endif // !FLATBUFFERS_GENERAL_HEAP_ALLOC_OK
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
266 #endif // !FLATBUFFERS_HAS_NEW_STRTOD
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
279 #endif // !FLATBUFFERS_LOCALE_INDEPENDENT
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)
292 namespace flatbuffers {
295 template<
typename T> FLATBUFFERS_CONSTEXPR
inline bool IsConstTrue(T t) {
301 #if ((__cplusplus >= 201703L) \
302 || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
304 #define FLATBUFFERS_ATTRIBUTE(attr) attr
306 #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]]
308 #define FLATBUFFERS_ATTRIBUTE(attr)
310 #if FLATBUFFERS_CLANG >= 30800
311 #define FLATBUFFERS_FALLTHROUGH() [[clang::fallthrough]]
312 #elif FLATBUFFERS_GCC >= 70300
313 #define FLATBUFFERS_FALLTHROUGH() [[gnu::fallthrough]]
315 #define FLATBUFFERS_FALLTHROUGH()
322 namespace flatbuffers {
328 typedef uint32_t uoffset_t;
329 typedef uint64_t uoffset64_t;
332 typedef int32_t soffset_t;
333 typedef int64_t soffset64_t;
337 typedef uint16_t voffset_t;
339 typedef uintmax_t largest_scalar_t;
342 #define FLATBUFFERS_MAX_BUFFER_SIZE std::numeric_limits<::flatbuffers::soffset_t>::max()
343 #define FLATBUFFERS_MAX_64_BUFFER_SIZE std::numeric_limits<::flatbuffers::soffset64_t>::max()
349 #define FLATBUFFERS_MIN_BUFFER_SIZE sizeof(uoffset_t) + sizeof(soffset_t) + \
350 sizeof(uint16_t) + sizeof(uint16_t)
353 #ifndef FLATBUFFERS_MAX_ALIGNMENT
354 #define FLATBUFFERS_MAX_ALIGNMENT 32
358 static const size_t kFileIdentifierLength = 4;
360 inline bool VerifyAlignmentRequirements(
size_t align,
size_t min_align = 1) {
361 return (min_align <= align) && (align <= (FLATBUFFERS_MAX_ALIGNMENT)) &&
362 (align & (align - 1)) == 0;
365 #if defined(_MSC_VER)
366 #pragma warning(push)
367 #pragma warning(disable: 4127) // C4127: conditional expression is constant
370 template<
typename T> T EndianSwap(T t) {
371 #if defined(_MSC_VER)
372 #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort
373 #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong
374 #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64
375 #elif defined(__ICCARM__)
376 #define FLATBUFFERS_BYTESWAP16 __REV16
377 #define FLATBUFFERS_BYTESWAP32 __REV
378 #define FLATBUFFERS_BYTESWAP64(x) \
379 ((__REV(static_cast<uint32_t>(x >> 32U))) | (static_cast<uint64_t>(__REV(static_cast<uint32_t>(x)))) << 32U)
381 #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__)
383 #define FLATBUFFERS_BYTESWAP16(x) \
384 static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16))
386 #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16
388 #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32
389 #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64
391 if (
sizeof(T) == 1) {
393 }
else if (
sizeof(T) == 2) {
394 union { T t; uint16_t i; } u = { t };
395 u.i = FLATBUFFERS_BYTESWAP16(u.i);
397 }
else if (
sizeof(T) == 4) {
398 union { T t; uint32_t i; } u = { t };
399 u.i = FLATBUFFERS_BYTESWAP32(u.i);
401 }
else if (
sizeof(T) == 8) {
402 union { T t; uint64_t i; } u = { t };
403 u.i = FLATBUFFERS_BYTESWAP64(u.i);
411 #if defined(_MSC_VER)
416 template<
typename T> T EndianScalar(T t) {
417 #if FLATBUFFERS_LITTLEENDIAN
420 return EndianSwap(t);
426 FLATBUFFERS_SUPPRESS_UBSAN(
"alignment")
427 T ReadScalar(const
void *
p) {
428 return EndianScalar(*
reinterpret_cast<const T *
>(
p));
433 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
434 #pragma GCC diagnostic push
435 #pragma GCC diagnostic ignored "-Wstringop-overflow"
440 FLATBUFFERS_SUPPRESS_UBSAN(
"alignment")
441 void WriteScalar(
void *
p, T t) {
442 *
reinterpret_cast<T *
>(
p) = EndianScalar(t);
445 template<
typename T>
struct Offset;
446 template<
typename T> FLATBUFFERS_SUPPRESS_UBSAN(
"alignment")
void WriteScalar(
void *
p, Offset<T> t) {
447 *
reinterpret_cast<uoffset_t *
>(
p) = EndianScalar(t.o);
450 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
451 #pragma GCC diagnostic pop
457 FLATBUFFERS_SUPPRESS_UBSAN(
"unsigned-integer-overflow")
458 inline
size_t PaddingBytes(
size_t buf_size,
size_t scalar_size) {
459 return ((~buf_size) + 1) & (scalar_size - 1);
465 template<
typename T>
inline bool IsTheSameAs(T e, T def) {
return e == def; }
467 #if defined(FLATBUFFERS_NAN_DEFAULTS) && \
468 defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
470 template<
typename T>
inline bool IsFloatTheSameAs(T e, T def) {
471 return (e == def) || ((def != def) && (e != e));
473 template<>
inline bool IsTheSameAs<float>(
float e,
float def) {
474 return IsFloatTheSameAs(e, def);
476 template<>
inline bool IsTheSameAs<double>(
double e,
double def) {
477 return IsFloatTheSameAs(e, def);
485 inline bool IsOutRange(
const T &v,
const T &low,
const T &high) {
486 return (v < low) || (high < v);
491 inline bool IsInRange(
const T &v,
const T &low,
const T &high) {
492 return !IsOutRange(v, low, high);
496 #endif // FLATBUFFERS_BASE_H_