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) 42 #include <type_traits> 49 #if defined(__unix__) && !defined(FLATBUFFERS_LOCALE_INDEPENDENT) 53 #ifdef _STLPORT_VERSION 54 #define FLATBUFFERS_CPP98_STL 58 #include <android/api-level.h> 61 #if defined(__ICCARM__) 62 #include <intrinsics.h> 80 #if defined(__GNUC__) && !defined(__clang__) 81 #define FLATBUFFERS_GCC (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) 83 #define FLATBUFFERS_GCC 0 86 #if defined(__clang__) 87 #define FLATBUFFERS_CLANG (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) 89 #define FLATBUFFERS_CLANG 0 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__ 102 #if !defined(__clang__) && \ 103 defined(__GNUC__) && \ 104 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600) 109 const class nullptr_t {
111 template<
class T>
inline operator T*()
const {
return 0; }
113 void operator&()
const;
117 #define constexpr const 123 #if defined(__s390x__) 124 #define FLATBUFFERS_LITTLEENDIAN 0 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 132 #define FLATBUFFERS_LITTLEENDIAN 1 133 #endif // __BIG_ENDIAN__ 134 #elif defined(_MSC_VER) 136 #define FLATBUFFERS_LITTLEENDIAN 0 138 #define FLATBUFFERS_LITTLEENDIAN 1 141 #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN. 143 #endif // !defined(FLATBUFFERS_LITTLEENDIAN) 145 #define FLATBUFFERS_VERSION_MAJOR 1 146 #define FLATBUFFERS_VERSION_MINOR 12 147 #define FLATBUFFERS_VERSION_REVISION 0 148 #define FLATBUFFERS_STRING_EXPAND(X) #X 149 #define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X) 152 const char* FLATBUFFERS_VERSION();
155 #if (!defined(_MSC_VER) || _MSC_VER > 1600) && \ 156 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407)) || \ 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 163 #define FLATBUFFERS_FINAL_CLASS 164 #define FLATBUFFERS_OVERRIDE 165 #define FLATBUFFERS_EXPLICIT_CPP11 166 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE 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 176 #define FLATBUFFERS_CONSTEXPR const 177 #define FLATBUFFERS_CONSTEXPR_CPP11 180 #if (defined(__cplusplus) && __cplusplus >= 201402L) || \ 181 (defined(__cpp_constexpr) && __cpp_constexpr >= 201304) 182 #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR_CPP11 184 #define FLATBUFFERS_CONSTEXPR_CPP14 187 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \ 188 (defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190023026)) || \ 190 #define FLATBUFFERS_NOEXCEPT noexcept 192 #define FLATBUFFERS_NOEXCEPT 197 #if (!defined(_MSC_VER) || _MSC_FULL_VER >= 180020827) && \ 198 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 404)) || \ 200 #define FLATBUFFERS_DELETE_FUNC(func) func = delete 202 #define FLATBUFFERS_DELETE_FUNC(func) private: func 205 #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \ 206 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \ 208 #define FLATBUFFERS_DEFAULT_DECLARATION 215 #if (defined(_MSC_VER) && _MSC_VER > 1700 ) \ 216 || (defined(__cpp_alias_templates) && __cpp_alias_templates >= 200704) \ 217 || (defined(__cplusplus) && __cplusplus >= 201103L) 218 #define FLATBUFFERS_TEMPLATES_ALIASES 221 #ifndef FLATBUFFERS_HAS_STRING_VIEW 224 #if defined(__has_include) 226 #if __has_include(<string_view>) && (__cplusplus >= 201606 || (defined(_HAS_CXX17) && _HAS_CXX17)) 227 #include <string_view> 229 typedef std::string_view string_view;
231 #define FLATBUFFERS_HAS_STRING_VIEW 1 233 #elif __has_include(<experimental/string_view>) && (__cplusplus >= 201411) 234 #include <experimental/string_view> 236 typedef std::experimental::string_view string_view;
238 #define FLATBUFFERS_HAS_STRING_VIEW 1 240 #elif __has_include("absl/strings/string_view.h") 241 #include "absl/strings/string_view.h" 243 typedef absl::string_view string_view;
245 #define FLATBUFFERS_HAS_STRING_VIEW 1 247 #endif // __has_include 248 #endif // !FLATBUFFERS_HAS_STRING_VIEW 250 #ifndef FLATBUFFERS_HAS_NEW_STRTOD 254 #if (defined(_MSC_VER) && _MSC_VER >= 1900) || \ 255 (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \ 257 #define FLATBUFFERS_HAS_NEW_STRTOD 1 259 #endif // !FLATBUFFERS_HAS_NEW_STRTOD 261 #ifndef FLATBUFFERS_LOCALE_INDEPENDENT 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 267 #define FLATBUFFERS_LOCALE_INDEPENDENT 0 269 #endif // !FLATBUFFERS_LOCALE_INDEPENDENT 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)) 279 #define __supress_ubsan__(type) 284 template<
typename T> FLATBUFFERS_CONSTEXPR
inline bool IsConstTrue(T t) {
289 #if ((__cplusplus >= 201703L) \ 290 || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))) 292 #define FLATBUFFERS_ATTRIBUTE(attr) [[attr]] 294 #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]] 296 #define FLATBUFFERS_ATTRIBUTE(attr) 298 #if FLATBUFFERS_CLANG >= 30800 299 #define FLATBUFFERS_FALLTHROUGH() [[clang::fallthrough]] 300 #elif FLATBUFFERS_GCC >= 70300 301 #define FLATBUFFERS_FALLTHROUGH() [[gnu::fallthrough]] 303 #define FLATBUFFERS_FALLTHROUGH() 316 typedef uint32_t uoffset_t;
319 typedef int32_t soffset_t;
323 typedef uint16_t voffset_t;
325 typedef uintmax_t largest_scalar_t;
328 #define FLATBUFFERS_MAX_BUFFER_SIZE ((1ULL << (sizeof(::flatbuffers::soffset_t) * 8 - 1)) - 1) 331 #define FLATBUFFERS_MAX_ALIGNMENT 16 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;
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 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) 355 #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__) 357 #define FLATBUFFERS_BYTESWAP16(x) \ 358 static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16)) 360 #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16 362 #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32 363 #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64 365 if (
sizeof(T) == 1) {
367 }
else if (
sizeof(T) == 2) {
368 union { T t; uint16_t i; } u = { t };
369 u.i = FLATBUFFERS_BYTESWAP16(u.i);
371 }
else if (
sizeof(T) == 4) {
372 union { T t; uint32_t i; } u = { t };
373 u.i = FLATBUFFERS_BYTESWAP32(u.i);
375 }
else if (
sizeof(T) == 8) {
376 union { T t; uint64_t i; } u = { t };
377 u.i = FLATBUFFERS_BYTESWAP64(u.i);
385 #if defined(_MSC_VER) 390 template<
typename T> T EndianScalar(T t) {
391 #if FLATBUFFERS_LITTLEENDIAN 394 return EndianSwap(t);
400 __supress_ubsan__(
"alignment")
401 T ReadScalar(
const void *p) {
402 return EndianScalar(*reinterpret_cast<const T *>(p));
407 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000) 408 #pragma GCC diagnostic push 409 #pragma GCC diagnostic ignored "-Wstringop-overflow" 414 __supress_ubsan__(
"alignment")
415 void WriteScalar(
void *p, T t) {
416 *
reinterpret_cast<T *
>(p) = EndianScalar(t);
419 template<
typename T>
struct Offset;
420 template<
typename T> __supress_ubsan__(
"alignment")
void WriteScalar(
void *p, Offset<T> t) {
421 *
reinterpret_cast<uoffset_t *
>(p) = EndianScalar(t.o);
424 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000) 425 #pragma GCC diagnostic pop 431 __supress_ubsan__(
"unsigned-integer-overflow")
432 inline size_t PaddingBytes(
size_t buf_size,
size_t scalar_size) {
433 return ((~buf_size) + 1) & (scalar_size - 1);
437 #endif // FLATBUFFERS_BASE_H_
#define FLATBUFFERS_ASSERT