base.h
Go to the documentation of this file.
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) && defined(__AVR__)
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 <limits>
47 #include <iterator>
48 #include <memory>
49 
50 #if defined(__unix__) && !defined(FLATBUFFERS_LOCALE_INDEPENDENT)
51  #include <unistd.h>
52 #endif
53 
54 #ifdef __ANDROID__
55  #include <android/api-level.h>
56 #endif
57 
58 #if defined(__ICCARM__)
59 #include <intrinsics.h>
60 #endif
61 
62 // Note the __clang__ check is needed, because clang presents itself
63 // as an older GNUC compiler (4.2).
64 // Clang 3.3 and later implement all of the ISO C++ 2011 standard.
65 // Clang 3.4 and later implement all of the ISO C++ 2014 standard.
66 // http://clang.llvm.org/cxx_status.html
67 
68 // Note the MSVC value '__cplusplus' may be incorrect:
69 // The '__cplusplus' predefined macro in the MSVC stuck at the value 199711L,
70 // indicating (erroneously!) that the compiler conformed to the C++98 Standard.
71 // This value should be correct starting from MSVC2017-15.7-Preview-3.
72 // The '__cplusplus' will be valid only if MSVC2017-15.7-P3 and the `/Zc:__cplusplus` switch is set.
73 // Workaround (for details see MSDN):
74 // Use the _MSC_VER and _MSVC_LANG definition instead of the __cplusplus for compatibility.
75 // The _MSVC_LANG macro reports the Standard version regardless of the '/Zc:__cplusplus' switch.
76 
77 #if defined(__GNUC__) && !defined(__clang__)
78  #define FLATBUFFERS_GCC (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
79 #else
80  #define FLATBUFFERS_GCC 0
81 #endif
82 
83 #if defined(__clang__)
84  #define FLATBUFFERS_CLANG (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
85 #else
86  #define FLATBUFFERS_CLANG 0
87 #endif
88 
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__
97 #endif
98 
99 #if !defined(__clang__) && \
100  defined(__GNUC__) && \
101  (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600)
102  // Backwards compatibility for g++ 4.4, and 4.5 which don't have the nullptr
103  // and constexpr keywords. Note the __clang__ check is needed, because clang
104  // presents itself as an older GNUC compiler.
105  #ifndef nullptr_t
106  const class nullptr_t {
107  public:
108  template<class T> inline operator T*() const { return 0; }
109  private:
110  void operator&() const;
111  } nullptr = {};
112  #endif
113  #ifndef constexpr
114  #define constexpr const
115  #endif
116 #endif
117 
118 // The wire format uses a little endian encoding (since that's efficient for
119 // the common platforms).
120 #if defined(__s390x__)
121  #define FLATBUFFERS_LITTLEENDIAN 0
122 #endif // __s390x__
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
128  #else
129  #define FLATBUFFERS_LITTLEENDIAN 1
130  #endif // __BIG_ENDIAN__
131  #elif defined(_MSC_VER)
132  #if defined(_M_PPC)
133  #define FLATBUFFERS_LITTLEENDIAN 0
134  #else
135  #define FLATBUFFERS_LITTLEENDIAN 1
136  #endif
137  #else
138  #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN.
139  #endif
140 #endif // !defined(FLATBUFFERS_LITTLEENDIAN)
141 
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 {
148  // Returns version as string "MAJOR.MINOR.REVISION".
149  const char* FLATBUFFERS_VERSION();
150 }
151 
152 #if (!defined(_MSC_VER) || _MSC_VER > 1600) && \
153  (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407)) || \
154  defined(__clang__)
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
159 #else
160  #define FLATBUFFERS_FINAL_CLASS
161  #define FLATBUFFERS_OVERRIDE
162  #define FLATBUFFERS_EXPLICIT_CPP11
163  #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE
164 #endif
165 
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
172 #else
173  #define FLATBUFFERS_CONSTEXPR const
174  #define FLATBUFFERS_CONSTEXPR_CPP11
175 #endif
176 
177 #if (defined(__cplusplus) && __cplusplus >= 201402L) || \
178  (defined(__cpp_constexpr) && __cpp_constexpr >= 201304)
179  #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR_CPP11
180 #else
181  #define FLATBUFFERS_CONSTEXPR_CPP14
182 #endif
183 
184 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \
185  (defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190023026)) || \
186  defined(__clang__)
187  #define FLATBUFFERS_NOEXCEPT noexcept
188 #else
189  #define FLATBUFFERS_NOEXCEPT
190 #endif
191 
192 // NOTE: the FLATBUFFERS_DELETE_FUNC macro may change the access mode to
193 // private, so be sure to put it at the end or reset access mode explicitly.
194 #if (!defined(_MSC_VER) || _MSC_FULL_VER >= 180020827) && \
195  (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 404)) || \
196  defined(__clang__)
197  #define FLATBUFFERS_DELETE_FUNC(func) func = delete
198 #else
199  #define FLATBUFFERS_DELETE_FUNC(func) private: func
200 #endif
201 
202 #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
203  (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \
204  defined(__clang__)
205  #define FLATBUFFERS_DEFAULT_DECLARATION
206 #endif
207 
208 // Check if we can use template aliases
209 // Not possible if Microsoft Compiler before 2012
210 // Possible is the language feature __cpp_alias_templates is defined well
211 // Or possible if the C++ std is C+11 or newer
212 #if (defined(_MSC_VER) && _MSC_VER > 1700 /* MSVC2012 */) \
213  || (defined(__cpp_alias_templates) && __cpp_alias_templates >= 200704) \
214  || (defined(__cplusplus) && __cplusplus >= 201103L)
215  #define FLATBUFFERS_TEMPLATES_ALIASES
216 #endif
217 
218 #ifndef FLATBUFFERS_HAS_STRING_VIEW
219  // Only provide flatbuffers::string_view if __has_include can be used
220  // to detect a header that provides an implementation
221  #if defined(__has_include)
222  // Check for std::string_view (in c++17)
223  #if __has_include(<string_view>) && (__cplusplus >= 201606 || (defined(_HAS_CXX17) && _HAS_CXX17))
224  #include <string_view>
225  namespace flatbuffers {
227  }
228  #define FLATBUFFERS_HAS_STRING_VIEW 1
229  // Check for std::experimental::string_view (in c++14, compiler-dependent)
230  #elif __has_include(<experimental/string_view>) && (__cplusplus >= 201411)
231  #include <experimental/string_view>
232  namespace flatbuffers {
234  }
235  #define FLATBUFFERS_HAS_STRING_VIEW 1
236  // Check for absl::string_view
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 {
245  }
246  #define FLATBUFFERS_HAS_STRING_VIEW 1
247  #endif
248  #endif
249  #endif // __has_include
250 #endif // !FLATBUFFERS_HAS_STRING_VIEW
251 
252 #ifndef FLATBUFFERS_GENERAL_HEAP_ALLOC_OK
253  // Allow heap allocations to be used
254  #define FLATBUFFERS_GENERAL_HEAP_ALLOC_OK 1
255 #endif // !FLATBUFFERS_GENERAL_HEAP_ALLOC_OK
256 
257 #ifndef FLATBUFFERS_HAS_NEW_STRTOD
258  // Modern (C++11) strtod and strtof functions are available for use.
259  // 1) nan/inf strings as argument of strtod;
260  // 2) hex-float as argument of strtod/strtof.
261  #if (defined(_MSC_VER) && _MSC_VER >= 1900) || \
262  (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \
263  (defined(__clang__))
264  #define FLATBUFFERS_HAS_NEW_STRTOD 1
265  #endif
266 #endif // !FLATBUFFERS_HAS_NEW_STRTOD
267 
268 #ifndef FLATBUFFERS_LOCALE_INDEPENDENT
269  // Enable locale independent functions {strtof_l, strtod_l,strtoll_l,
270  // strtoull_l}.
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
276  #else
277  #define FLATBUFFERS_LOCALE_INDEPENDENT 0
278  #endif
279 #endif // !FLATBUFFERS_LOCALE_INDEPENDENT
280 
281 // Suppress Undefined Behavior Sanitizer (recoverable only). Usage:
282 // - FLATBUFFERS_SUPPRESS_UBSAN("undefined")
283 // - FLATBUFFERS_SUPPRESS_UBSAN("signed-integer-overflow")
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))
288 #else
289  #define FLATBUFFERS_SUPPRESS_UBSAN(type)
290 #endif
291 
292 namespace flatbuffers {
293  // This is constexpr function used for checking compile-time constants.
294  // Avoid `#pragma warning(disable: 4127) // C4127: expression is constant`.
295  template<typename T> FLATBUFFERS_CONSTEXPR inline bool IsConstTrue(T t) {
296  return !!t;
297  }
298 }
299 
300 // Enable C++ attribute [[]] if std:c++17 or higher.
301 #if ((__cplusplus >= 201703L) \
302  || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
303  // All attributes unknown to an implementation are ignored without causing an error.
304  #define FLATBUFFERS_ATTRIBUTE(attr) attr
305 
306  #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]]
307 #else
308  #define FLATBUFFERS_ATTRIBUTE(attr)
309 
310  #if FLATBUFFERS_CLANG >= 30800
311  #define FLATBUFFERS_FALLTHROUGH() [[clang::fallthrough]]
312  #elif FLATBUFFERS_GCC >= 70300
313  #define FLATBUFFERS_FALLTHROUGH() [[gnu::fallthrough]]
314  #else
315  #define FLATBUFFERS_FALLTHROUGH()
316  #endif
317 #endif
318 
320 
322 namespace flatbuffers {
323 
325 // Our default offset / size type, 32bit on purpose on 64bit systems.
326 // Also, using a consistent offset type maintains compatibility of serialized
327 // offset values between 32bit and 64bit systems.
328 typedef uint32_t uoffset_t;
329 typedef uint64_t uoffset64_t;
330 
331 // Signed offsets for references that can go in both directions.
332 typedef int32_t soffset_t;
333 typedef int64_t soffset64_t;
334 
335 // Offset/index used in v-tables, can be changed to uint8_t in
336 // format forks to save a bit of space if desired.
337 typedef uint16_t voffset_t;
338 
339 typedef uintmax_t largest_scalar_t;
340 
341 // In 32bits, this evaluates to 2GB - 1
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()
344 
345 // The minimum size buffer that can be a valid flatbuffer.
346 // Includes the offset to the root table (uoffset_t), the offset to the vtable
347 // of the root table (soffset_t), the size of the vtable (uint16_t), and the
348 // size of the referring table (uint16_t).
349 #define FLATBUFFERS_MIN_BUFFER_SIZE sizeof(uoffset_t) + sizeof(soffset_t) + \
350  sizeof(uint16_t) + sizeof(uint16_t)
351 
352 // We support aligning the contents of buffers up to this size.
353 #ifndef FLATBUFFERS_MAX_ALIGNMENT
354  #define FLATBUFFERS_MAX_ALIGNMENT 32
355 #endif
356 
358 static const size_t kFileIdentifierLength = 4;
359 
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; // must be power of 2
363 }
364 
365 #if defined(_MSC_VER)
366  #pragma warning(push)
367  #pragma warning(disable: 4127) // C4127: conditional expression is constant
368 #endif
369 
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)
380  #else
381  #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__)
382  // __builtin_bswap16 was missing prior to GCC 4.8.
383  #define FLATBUFFERS_BYTESWAP16(x) \
384  static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16))
385  #else
386  #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16
387  #endif
388  #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32
389  #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64
390  #endif
391  if (sizeof(T) == 1) { // Compile-time if-then's.
392  return t;
393  } else if (sizeof(T) == 2) {
394  union { T t; uint16_t i; } u = { t };
395  u.i = FLATBUFFERS_BYTESWAP16(u.i);
396  return u.t;
397  } else if (sizeof(T) == 4) {
398  union { T t; uint32_t i; } u = { t };
399  u.i = FLATBUFFERS_BYTESWAP32(u.i);
400  return u.t;
401  } else if (sizeof(T) == 8) {
402  union { T t; uint64_t i; } u = { t };
403  u.i = FLATBUFFERS_BYTESWAP64(u.i);
404  return u.t;
405  } else {
407  return t;
408  }
409 }
410 
411 #if defined(_MSC_VER)
412  #pragma warning(pop)
413 #endif
414 
415 
416 template<typename T> T EndianScalar(T t) {
417  #if FLATBUFFERS_LITTLEENDIAN
418  return t;
419  #else
420  return EndianSwap(t);
421  #endif
422 }
423 
424 template<typename T>
425 // UBSAN: C++ aliasing type rules, see std::bit_cast<> for details.
426 FLATBUFFERS_SUPPRESS_UBSAN("alignment")
427 T ReadScalar(const void *p) {
428  return EndianScalar(*reinterpret_cast<const T *>(p));
429 }
430 
431 // See https://github.com/google/flatbuffers/issues/5950
432 
433 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
434  #pragma GCC diagnostic push
435  #pragma GCC diagnostic ignored "-Wstringop-overflow"
436 #endif
437 
438 template<typename T>
439 // UBSAN: C++ aliasing type rules, see std::bit_cast<> for details.
440 FLATBUFFERS_SUPPRESS_UBSAN("alignment")
441 void WriteScalar(void *p, T t) {
442  *reinterpret_cast<T *>(p) = EndianScalar(t);
443 }
444 
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);
448 }
449 
450 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
451  #pragma GCC diagnostic pop
452 #endif
453 
454 // Computes how many bytes you'd have to pad to be able to write an
455 // "scalar_size" scalar if the buffer had grown to "buf_size" (downwards in
456 // memory).
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);
460 }
461 
462 // Generic 'operator==' with conditional specialisations.
463 // T e - new value of a scalar field.
464 // T def - default of scalar (is known at compile-time).
465 template<typename T> inline bool IsTheSameAs(T e, T def) { return e == def; }
466 
467 #if defined(FLATBUFFERS_NAN_DEFAULTS) && \
468  defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
469 // Like `operator==(e, def)` with weak NaN if T=(float|double).
470 template<typename T> inline bool IsFloatTheSameAs(T e, T def) {
471  return (e == def) || ((def != def) && (e != e));
472 }
473 template<> inline bool IsTheSameAs<float>(float e, float def) {
474  return IsFloatTheSameAs(e, def);
475 }
476 template<> inline bool IsTheSameAs<double>(double e, double def) {
477  return IsFloatTheSameAs(e, def);
478 }
479 #endif
480 
481 // Check 'v' is out of closed range [low; high].
482 // Workaround for GCC warning [-Werror=type-limits]:
483 // comparison is always true due to limited range of data type.
484 template<typename T>
485 inline bool IsOutRange(const T &v, const T &low, const T &high) {
486  return (v < low) || (high < v);
487 }
488 
489 // Check 'v' is in closed range [low; high].
490 template<typename T>
491 inline bool IsInRange(const T &v, const T &low, const T &high) {
492  return !IsOutRange(v, low, high);
493 }
494 
495 } // namespace flatbuffers
496 #endif // FLATBUFFERS_BASE_H_
FLATBUFFERS_ASSERT
#define FLATBUFFERS_ASSERT
Definition: base.h:21
detail::void
j template void())
Definition: json.hpp:4893
lexyd::p
constexpr auto p
Parses the production.
Definition: production.hpp:127
lexy::_detail::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:184
lexyd::operator&
constexpr auto operator&(C1 c1, C2 c2) -> _cand< decltype(_make_char_class(c1)), decltype(_make_char_class(c2))>
Definition: char_class.hpp:584


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07