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 __has_include("absl/base/config.h") && \
242 (__cplusplus >= 201411)
243 #include "absl/base/config.h"
244 #if !defined(ABSL_USES_STD_STRING_VIEW)
245 #include "absl/strings/string_view.h"
247 typedef absl::string_view string_view;
249 #define FLATBUFFERS_HAS_STRING_VIEW 1
252 #endif // __has_include
253 #endif // !FLATBUFFERS_HAS_STRING_VIEW
255 #ifndef FLATBUFFERS_HAS_NEW_STRTOD
259 #if (defined(_MSC_VER) && _MSC_VER >= 1900) || \
260 (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \
262 #define FLATBUFFERS_HAS_NEW_STRTOD 1
264 #endif // !FLATBUFFERS_HAS_NEW_STRTOD
266 #ifndef FLATBUFFERS_LOCALE_INDEPENDENT
268 #if ((defined(_MSC_VER) && _MSC_VER >= 1800) || \
269 (defined(_XOPEN_VERSION) && (_XOPEN_VERSION>=700)) && (!defined(__ANDROID_API__) || (defined(__ANDROID_API__) && (__ANDROID_API__>=21))))
270 #define FLATBUFFERS_LOCALE_INDEPENDENT 1
272 #define FLATBUFFERS_LOCALE_INDEPENDENT 0
274 #endif // !FLATBUFFERS_LOCALE_INDEPENDENT
279 #if defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))
280 #define __supress_ubsan__(type) __attribute__((no_sanitize(type)))
281 #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)
282 #define __supress_ubsan__(type) __attribute__((no_sanitize_undefined))
284 #define __supress_ubsan__(type)
289 template<
typename T> FLATBUFFERS_CONSTEXPR
inline bool IsConstTrue(T t) {
294 #if ((__cplusplus >= 201703L) \
295 || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
297 #define FLATBUFFERS_ATTRIBUTE(attr) [[attr]]
299 #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]]
301 #define FLATBUFFERS_ATTRIBUTE(attr)
303 #if FLATBUFFERS_CLANG >= 30800
304 #define FLATBUFFERS_FALLTHROUGH() [[clang::fallthrough]]
305 #elif FLATBUFFERS_GCC >= 70300
306 #define FLATBUFFERS_FALLTHROUGH() [[gnu::fallthrough]]
308 #define FLATBUFFERS_FALLTHROUGH()
321 typedef uint32_t uoffset_t;
324 typedef int32_t soffset_t;
328 typedef uint16_t voffset_t;
330 typedef uintmax_t largest_scalar_t;
333 #define FLATBUFFERS_MAX_BUFFER_SIZE ((1ULL << (sizeof(::flatbuffers::soffset_t) * 8 - 1)) - 1)
336 #define FLATBUFFERS_MAX_ALIGNMENT 16
338 inline bool VerifyAlignmentRequirements(
size_t align,
size_t min_align = 1) {
339 return (min_align <= align) && (align <= (FLATBUFFERS_MAX_ALIGNMENT)) &&
340 (align & (align - 1)) == 0;
343 #if defined(_MSC_VER)
344 #pragma warning(disable: 4351) // C4351: new behavior: elements of array ... will be default initialized
345 #pragma warning(push)
346 #pragma warning(disable: 4127) // C4127: conditional expression is constant
349 template<
typename T> T EndianSwap(T t) {
350 #if defined(_MSC_VER)
351 #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort
352 #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong
353 #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64
354 #elif defined(__ICCARM__)
355 #define FLATBUFFERS_BYTESWAP16 __REV16
356 #define FLATBUFFERS_BYTESWAP32 __REV
357 #define FLATBUFFERS_BYTESWAP64(x) \
358 ((__REV(static_cast<uint32_t>(x >> 32U))) | (static_cast<uint64_t>(__REV(static_cast<uint32_t>(x)))) << 32U)
360 #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__)
362 #define FLATBUFFERS_BYTESWAP16(x) \
363 static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16))
365 #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16
367 #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32
368 #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64
370 if (
sizeof(T) == 1) {
372 }
else if (
sizeof(T) == 2) {
373 union { T t; uint16_t i; } u = { t };
374 u.i = FLATBUFFERS_BYTESWAP16(u.i);
376 }
else if (
sizeof(T) == 4) {
377 union { T t; uint32_t i; } u = { t };
378 u.i = FLATBUFFERS_BYTESWAP32(u.i);
380 }
else if (
sizeof(T) == 8) {
381 union { T t; uint64_t i; } u = { t };
382 u.i = FLATBUFFERS_BYTESWAP64(u.i);
390 #if defined(_MSC_VER)
395 template<
typename T> T EndianScalar(T t) {
396 #if FLATBUFFERS_LITTLEENDIAN
399 return EndianSwap(t);
405 __supress_ubsan__(
"alignment")
406 T ReadScalar(const
void *p) {
407 return EndianScalar(*
reinterpret_cast<const T *
>(p));
412 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
413 #pragma GCC diagnostic push
414 #pragma GCC diagnostic ignored "-Wstringop-overflow"
419 __supress_ubsan__(
"alignment")
420 void WriteScalar(
void *p, T t) {
421 *
reinterpret_cast<T *
>(p) = EndianScalar(t);
424 template<
typename T>
struct Offset;
425 template<
typename T> __supress_ubsan__(
"alignment") void WriteScalar(
void *p, Offset<T> t) {
426 *
reinterpret_cast<uoffset_t *
>(p) = EndianScalar(t.o);
429 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
430 #pragma GCC diagnostic pop
436 __supress_ubsan__(
"unsigned-integer-overflow")
437 inline
size_t PaddingBytes(
size_t buf_size,
size_t scalar_size) {
438 return ((~buf_size) + 1) & (scalar_size - 1);
442 #endif // FLATBUFFERS_BASE_H_