File indexing completed on 2025-08-27 09:30:24
0001 #ifndef FLATBUFFERS_BASE_H_
0002 #define FLATBUFFERS_BASE_H_
0003
0004
0005
0006
0007 #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
0008 defined(_MSC_VER) && defined(_DEBUG)
0009
0010
0011 #define _CRTDBG_MAP_ALLOC
0012 #include <stdlib.h>
0013 #include <crtdbg.h>
0014
0015 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
0016 #define new DEBUG_NEW
0017 #endif
0018
0019 #if !defined(FLATBUFFERS_ASSERT)
0020 #include <assert.h>
0021 #define FLATBUFFERS_ASSERT assert
0022 #elif defined(FLATBUFFERS_ASSERT_INCLUDE)
0023
0024 #include FLATBUFFERS_ASSERT_INCLUDE
0025 #endif
0026
0027 #ifndef ARDUINO
0028 #include <cstdint>
0029 #endif
0030
0031 #include <cstddef>
0032 #include <cstdlib>
0033 #include <cstring>
0034
0035 #if defined(ARDUINO) && !defined(ARDUINOSTL_M_H) && defined(__AVR__)
0036 #include <utility.h>
0037 #else
0038 #include <utility>
0039 #endif
0040
0041 #include <string>
0042 #include <type_traits>
0043 #include <vector>
0044 #include <set>
0045 #include <algorithm>
0046 #include <limits>
0047 #include <iterator>
0048 #include <memory>
0049
0050 #if defined(__unix__) && !defined(FLATBUFFERS_LOCALE_INDEPENDENT)
0051 #include <unistd.h>
0052 #endif
0053
0054 #ifdef __ANDROID__
0055 #include <android/api-level.h>
0056 #endif
0057
0058 #if defined(__ICCARM__)
0059 #include <intrinsics.h>
0060 #endif
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077 #if defined(__GNUC__) && !defined(__clang__)
0078 #define FLATBUFFERS_GCC (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
0079 #else
0080 #define FLATBUFFERS_GCC 0
0081 #endif
0082
0083 #if defined(__clang__)
0084 #define FLATBUFFERS_CLANG (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
0085 #else
0086 #define FLATBUFFERS_CLANG 0
0087 #endif
0088
0089
0090 #if __cplusplus <= 199711L && \
0091 (!defined(_MSC_VER) || _MSC_VER < 1600) && \
0092 (!defined(__GNUC__) || \
0093 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40400))
0094 #error A C++11 compatible compiler with support for the auto typing is \
0095 required for FlatBuffers.
0096 #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
0097 #endif
0098
0099 #if !defined(__clang__) && \
0100 defined(__GNUC__) && \
0101 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600)
0102
0103
0104
0105 #ifndef nullptr_t
0106 const class nullptr_t {
0107 public:
0108 template<class T> inline operator T*() const { return 0; }
0109 private:
0110 void operator&() const;
0111 } nullptr = {};
0112 #endif
0113 #ifndef constexpr
0114 #define constexpr const
0115 #endif
0116 #endif
0117
0118
0119
0120 #if defined(__s390x__)
0121 #define FLATBUFFERS_LITTLEENDIAN 0
0122 #endif
0123 #if !defined(FLATBUFFERS_LITTLEENDIAN)
0124 #if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
0125 #if (defined(__BIG_ENDIAN__) || \
0126 (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
0127 #define FLATBUFFERS_LITTLEENDIAN 0
0128 #else
0129 #define FLATBUFFERS_LITTLEENDIAN 1
0130 #endif
0131 #elif defined(_MSC_VER)
0132 #if defined(_M_PPC)
0133 #define FLATBUFFERS_LITTLEENDIAN 0
0134 #else
0135 #define FLATBUFFERS_LITTLEENDIAN 1
0136 #endif
0137 #else
0138 #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN.
0139 #endif
0140 #endif
0141
0142 #define FLATBUFFERS_VERSION_MAJOR 24
0143 #define FLATBUFFERS_VERSION_MINOR 12
0144 #define FLATBUFFERS_VERSION_REVISION 23
0145 #define FLATBUFFERS_STRING_EXPAND(X) #X
0146 #define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
0147 namespace flatbuffers {
0148
0149 const char* FLATBUFFERS_VERSION();
0150 }
0151
0152 #if (!defined(_MSC_VER) || _MSC_VER > 1600) && \
0153 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407)) || \
0154 defined(__clang__)
0155 #define FLATBUFFERS_FINAL_CLASS final
0156 #define FLATBUFFERS_OVERRIDE override
0157 #define FLATBUFFERS_EXPLICIT_CPP11 explicit
0158 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE : ::flatbuffers::voffset_t
0159 #else
0160 #define FLATBUFFERS_FINAL_CLASS
0161 #define FLATBUFFERS_OVERRIDE
0162 #define FLATBUFFERS_EXPLICIT_CPP11
0163 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE
0164 #endif
0165
0166 #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
0167 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \
0168 (defined(__cpp_constexpr) && __cpp_constexpr >= 200704)
0169 #define FLATBUFFERS_CONSTEXPR constexpr
0170 #define FLATBUFFERS_CONSTEXPR_CPP11 constexpr
0171 #define FLATBUFFERS_CONSTEXPR_DEFINED
0172 #else
0173 #define FLATBUFFERS_CONSTEXPR const
0174 #define FLATBUFFERS_CONSTEXPR_CPP11
0175 #endif
0176
0177 #if (defined(__cplusplus) && __cplusplus >= 201402L) || \
0178 (defined(__cpp_constexpr) && __cpp_constexpr >= 201304)
0179 #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR_CPP11
0180 #else
0181 #define FLATBUFFERS_CONSTEXPR_CPP14
0182 #endif
0183
0184 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \
0185 (defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190023026)) || \
0186 defined(__clang__)
0187 #define FLATBUFFERS_NOEXCEPT noexcept
0188 #else
0189 #define FLATBUFFERS_NOEXCEPT
0190 #endif
0191
0192
0193
0194 #if (!defined(_MSC_VER) || _MSC_FULL_VER >= 180020827) && \
0195 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 404)) || \
0196 defined(__clang__)
0197 #define FLATBUFFERS_DELETE_FUNC(func) func = delete
0198 #else
0199 #define FLATBUFFERS_DELETE_FUNC(func) private: func
0200 #endif
0201
0202 #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
0203 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \
0204 defined(__clang__)
0205 #define FLATBUFFERS_DEFAULT_DECLARATION
0206 #endif
0207
0208
0209
0210
0211
0212 #if (defined(_MSC_VER) && _MSC_VER > 1700 ) \
0213 || (defined(__cpp_alias_templates) && __cpp_alias_templates >= 200704) \
0214 || (defined(__cplusplus) && __cplusplus >= 201103L)
0215 #define FLATBUFFERS_TEMPLATES_ALIASES
0216 #endif
0217
0218 #ifndef FLATBUFFERS_HAS_STRING_VIEW
0219
0220
0221 #if defined(__has_include)
0222
0223 #if __has_include(<string_view>) && (__cplusplus >= 201606 || (defined(_HAS_CXX17) && _HAS_CXX17))
0224 #include <string_view>
0225 namespace flatbuffers {
0226 typedef std::string_view string_view;
0227 }
0228 #define FLATBUFFERS_HAS_STRING_VIEW 1
0229
0230 #elif __has_include(<experimental/string_view>) && (__cplusplus >= 201411)
0231 #include <experimental/string_view>
0232 namespace flatbuffers {
0233 typedef std::experimental::string_view string_view;
0234 }
0235 #define FLATBUFFERS_HAS_STRING_VIEW 1
0236
0237 #elif __has_include("absl/strings/string_view.h") && \
0238 __has_include("absl/base/config.h") && \
0239 (__cplusplus >= 201411)
0240 #include "absl/base/config.h"
0241 #if !defined(ABSL_USES_STD_STRING_VIEW)
0242 #include "absl/strings/string_view.h"
0243 namespace flatbuffers {
0244 typedef absl::string_view string_view;
0245 }
0246 #define FLATBUFFERS_HAS_STRING_VIEW 1
0247 #endif
0248 #endif
0249 #endif
0250 #endif
0251
0252 #ifndef FLATBUFFERS_GENERAL_HEAP_ALLOC_OK
0253
0254 #define FLATBUFFERS_GENERAL_HEAP_ALLOC_OK 1
0255 #endif
0256
0257 #ifndef FLATBUFFERS_HAS_NEW_STRTOD
0258
0259
0260
0261 #if (defined(_MSC_VER) && _MSC_VER >= 1900) || \
0262 (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \
0263 (defined(__clang__))
0264 #define FLATBUFFERS_HAS_NEW_STRTOD 1
0265 #endif
0266 #endif
0267
0268 #ifndef FLATBUFFERS_LOCALE_INDEPENDENT
0269
0270
0271 #if (defined(_MSC_VER) && _MSC_VER >= 1800) || \
0272 (defined(__ANDROID_API__) && __ANDROID_API__>= 21) || \
0273 (defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 700)) && \
0274 (!defined(__Fuchsia__) && !defined(__ANDROID_API__))
0275 #define FLATBUFFERS_LOCALE_INDEPENDENT 1
0276 #else
0277 #define FLATBUFFERS_LOCALE_INDEPENDENT 0
0278 #endif
0279 #endif
0280
0281
0282
0283
0284 #if defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))
0285 #define FLATBUFFERS_SUPPRESS_UBSAN(type) __attribute__((no_sanitize(type)))
0286 #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)
0287 #define FLATBUFFERS_SUPPRESS_UBSAN(type) __attribute__((no_sanitize_undefined))
0288 #else
0289 #define FLATBUFFERS_SUPPRESS_UBSAN(type)
0290 #endif
0291
0292 namespace flatbuffers {
0293
0294
0295 template<typename T> FLATBUFFERS_CONSTEXPR inline bool IsConstTrue(T t) {
0296 return !!t;
0297 }
0298 }
0299
0300
0301 #if ((__cplusplus >= 201703L) \
0302 || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
0303
0304 #define FLATBUFFERS_ATTRIBUTE(attr) attr
0305
0306 #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]]
0307 #else
0308 #define FLATBUFFERS_ATTRIBUTE(attr)
0309
0310 #if FLATBUFFERS_CLANG >= 30800
0311 #define FLATBUFFERS_FALLTHROUGH() [[clang::fallthrough]]
0312 #elif FLATBUFFERS_GCC >= 70300
0313 #define FLATBUFFERS_FALLTHROUGH() [[gnu::fallthrough]]
0314 #else
0315 #define FLATBUFFERS_FALLTHROUGH()
0316 #endif
0317 #endif
0318
0319
0320
0321
0322 namespace flatbuffers {
0323
0324
0325
0326
0327
0328 typedef uint32_t uoffset_t;
0329 typedef uint64_t uoffset64_t;
0330
0331
0332 typedef int32_t soffset_t;
0333 typedef int64_t soffset64_t;
0334
0335
0336
0337 typedef uint16_t voffset_t;
0338
0339 typedef uintmax_t largest_scalar_t;
0340
0341
0342 #define FLATBUFFERS_MAX_BUFFER_SIZE (std::numeric_limits<::flatbuffers::soffset_t>::max)()
0343 #define FLATBUFFERS_MAX_64_BUFFER_SIZE (std::numeric_limits<::flatbuffers::soffset64_t>::max)()
0344
0345
0346
0347
0348
0349 #define FLATBUFFERS_MIN_BUFFER_SIZE sizeof(::flatbuffers::uoffset_t) + \
0350 sizeof(::flatbuffers::soffset_t) + sizeof(uint16_t) + sizeof(uint16_t)
0351
0352
0353 #ifndef FLATBUFFERS_MAX_ALIGNMENT
0354 #define FLATBUFFERS_MAX_ALIGNMENT 32
0355 #endif
0356
0357
0358 static const size_t kFileIdentifierLength = 4;
0359
0360 inline bool VerifyAlignmentRequirements(size_t align, size_t min_align = 1) {
0361 return (min_align <= align) && (align <= (FLATBUFFERS_MAX_ALIGNMENT)) &&
0362 (align & (align - 1)) == 0;
0363 }
0364
0365 #if defined(_MSC_VER)
0366 #pragma warning(push)
0367 #pragma warning(disable: 4127)
0368 #endif
0369
0370 template<typename T> T EndianSwap(T t) {
0371 #if defined(_MSC_VER)
0372 #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort
0373 #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong
0374 #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64
0375 #elif defined(__ICCARM__)
0376 #define FLATBUFFERS_BYTESWAP16 __REV16
0377 #define FLATBUFFERS_BYTESWAP32 __REV
0378 #define FLATBUFFERS_BYTESWAP64(x) \
0379 ((__REV(static_cast<uint32_t>(x >> 32U))) | (static_cast<uint64_t>(__REV(static_cast<uint32_t>(x)))) << 32U)
0380 #else
0381 #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__)
0382
0383 #define FLATBUFFERS_BYTESWAP16(x) \
0384 static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16))
0385 #else
0386 #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16
0387 #endif
0388 #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32
0389 #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64
0390 #endif
0391 if (sizeof(T) == 1) {
0392 return t;
0393 } else if (sizeof(T) == 2) {
0394 union { T t; uint16_t i; } u = { t };
0395 u.i = FLATBUFFERS_BYTESWAP16(u.i);
0396 return u.t;
0397 } else if (sizeof(T) == 4) {
0398 union { T t; uint32_t i; } u = { t };
0399 u.i = FLATBUFFERS_BYTESWAP32(u.i);
0400 return u.t;
0401 } else if (sizeof(T) == 8) {
0402 union { T t; uint64_t i; } u = { t };
0403 u.i = FLATBUFFERS_BYTESWAP64(u.i);
0404 return u.t;
0405 } else {
0406 FLATBUFFERS_ASSERT(0);
0407 return t;
0408 }
0409 }
0410
0411 #if defined(_MSC_VER)
0412 #pragma warning(pop)
0413 #endif
0414
0415
0416 template<typename T> T EndianScalar(T t) {
0417 #if FLATBUFFERS_LITTLEENDIAN
0418 return t;
0419 #else
0420 return EndianSwap(t);
0421 #endif
0422 }
0423
0424 template<typename T>
0425
0426 FLATBUFFERS_SUPPRESS_UBSAN("alignment")
0427 T ReadScalar(const void *p) {
0428 return EndianScalar(*reinterpret_cast<const T *>(p));
0429 }
0430
0431
0432
0433 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
0434 #pragma GCC diagnostic push
0435 #pragma GCC diagnostic ignored "-Wstringop-overflow"
0436 #endif
0437
0438 template<typename T>
0439
0440 FLATBUFFERS_SUPPRESS_UBSAN("alignment")
0441 void WriteScalar(void *p, T t) {
0442 *reinterpret_cast<T *>(p) = EndianScalar(t);
0443 }
0444
0445 template<typename T> struct Offset;
0446 template<typename T> FLATBUFFERS_SUPPRESS_UBSAN("alignment") void WriteScalar(void *p, Offset<T> t) {
0447 *reinterpret_cast<uoffset_t *>(p) = EndianScalar(t.o);
0448 }
0449
0450 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
0451 #pragma GCC diagnostic pop
0452 #endif
0453
0454
0455
0456
0457 FLATBUFFERS_SUPPRESS_UBSAN("unsigned-integer-overflow")
0458 inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) {
0459 return ((~buf_size) + 1) & (scalar_size - 1);
0460 }
0461
0462 #if !defined(_MSC_VER)
0463 #pragma GCC diagnostic push
0464 #pragma GCC diagnostic ignored "-Wfloat-equal"
0465 #endif
0466
0467
0468
0469 template<typename T> inline bool IsTheSameAs(T e, T def) { return e == def; }
0470 #if !defined(_MSC_VER)
0471 #pragma GCC diagnostic pop
0472 #endif
0473
0474 #if defined(FLATBUFFERS_NAN_DEFAULTS) && \
0475 defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
0476
0477 template<typename T> inline bool IsFloatTheSameAs(T e, T def) {
0478 return (e == def) || ((def != def) && (e != e));
0479 }
0480 template<> inline bool IsTheSameAs<float>(float e, float def) {
0481 return IsFloatTheSameAs(e, def);
0482 }
0483 template<> inline bool IsTheSameAs<double>(double e, double def) {
0484 return IsFloatTheSameAs(e, def);
0485 }
0486 #endif
0487
0488
0489
0490
0491 template<typename T>
0492 inline bool IsOutRange(const T &v, const T &low, const T &high) {
0493 return (v < low) || (high < v);
0494 }
0495
0496
0497 template<typename T>
0498 inline bool IsInRange(const T &v, const T &low, const T &high) {
0499 return !IsOutRange(v, low, high);
0500 }
0501
0502 }
0503 #endif