Go to the documentation of this file.00001 #ifndef FLATBUFFERS_BASE_H_
00002 #define FLATBUFFERS_BASE_H_
00003
00004
00005
00006
00007 #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
00008 defined(_MSC_VER) && defined(_DEBUG)
00009
00010
00011 #define _CRTDBG_MAP_ALLOC
00012 #include <stdlib.h>
00013 #include <crtdbg.h>
00014
00015 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
00016 #define new DEBUG_NEW
00017 #endif
00018
00019 #if !defined(FLATBUFFERS_ASSERT)
00020 #include <assert.h>
00021 #define FLATBUFFERS_ASSERT assert
00022 #elif defined(FLATBUFFERS_ASSERT_INCLUDE)
00023
00024 #include FLATBUFFERS_ASSERT_INCLUDE
00025 #endif
00026
00027 #ifndef ARDUINO
00028 #include <cstdint>
00029 #endif
00030
00031 #include <cstddef>
00032 #include <cstdlib>
00033 #include <cstring>
00034
00035 #if defined(ARDUINO) && !defined(ARDUINOSTL_M_H)
00036 #include <utility.h>
00037 #else
00038 #include <utility>
00039 #endif
00040
00041 #include <string>
00042 #include <type_traits>
00043 #include <vector>
00044 #include <set>
00045 #include <algorithm>
00046 #include <iterator>
00047 #include <memory>
00048
00049 #ifdef _STLPORT_VERSION
00050 #define FLATBUFFERS_CPP98_STL
00051 #endif
00052 #ifndef FLATBUFFERS_CPP98_STL
00053 #include <functional>
00054 #endif
00055
00056 #include "flatbuffers/stl_emulation.h"
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 #if defined(__GNUC__) && !defined(__clang__)
00074 #define FLATBUFFERS_GCC (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
00075 #else
00076 #define FLATBUFFERS_GCC 0
00077 #endif
00078
00079 #if defined(__clang__)
00080 #define FLATBUFFERS_CLANG (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
00081 #else
00082 #define FLATBUFFERS_CLANG 0
00083 #endif
00084
00086 #if __cplusplus <= 199711L && \
00087 (!defined(_MSC_VER) || _MSC_VER < 1600) && \
00088 (!defined(__GNUC__) || \
00089 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40400))
00090 #error A C++11 compatible compiler with support for the auto typing is \
00091 required for FlatBuffers.
00092 #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
00093 #endif
00094
00095 #if !defined(__clang__) && \
00096 defined(__GNUC__) && \
00097 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600)
00098
00099
00100
00101 #ifndef nullptr_t
00102 const class nullptr_t {
00103 public:
00104 template<class T> inline operator T*() const { return 0; }
00105 private:
00106 void operator&() const;
00107 } nullptr = {};
00108 #endif
00109 #ifndef constexpr
00110 #define constexpr const
00111 #endif
00112 #endif
00113
00114
00115
00116 #if defined(__s390x__)
00117 #define FLATBUFFERS_LITTLEENDIAN 0
00118 #endif // __s390x__
00119 #if !defined(FLATBUFFERS_LITTLEENDIAN)
00120 #if defined(__GNUC__) || defined(__clang__)
00121 #ifdef __BIG_ENDIAN__
00122 #define FLATBUFFERS_LITTLEENDIAN 0
00123 #else
00124 #define FLATBUFFERS_LITTLEENDIAN 1
00125 #endif // __BIG_ENDIAN__
00126 #elif defined(_MSC_VER)
00127 #if defined(_M_PPC)
00128 #define FLATBUFFERS_LITTLEENDIAN 0
00129 #else
00130 #define FLATBUFFERS_LITTLEENDIAN 1
00131 #endif
00132 #else
00133 #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN.
00134 #endif
00135 #endif // !defined(FLATBUFFERS_LITTLEENDIAN)
00136
00137 #define FLATBUFFERS_VERSION_MAJOR 1
00138 #define FLATBUFFERS_VERSION_MINOR 10
00139 #define FLATBUFFERS_VERSION_REVISION 0
00140 #define FLATBUFFERS_STRING_EXPAND(X) #X
00141 #define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
00142
00143 #if (!defined(_MSC_VER) || _MSC_VER > 1600) && \
00144 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407)) || \
00145 defined(__clang__)
00146 #define FLATBUFFERS_FINAL_CLASS final
00147 #define FLATBUFFERS_OVERRIDE override
00148 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE : flatbuffers::voffset_t
00149 #else
00150 #define FLATBUFFERS_FINAL_CLASS
00151 #define FLATBUFFERS_OVERRIDE
00152 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE
00153 #endif
00154
00155 #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
00156 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \
00157 (defined(__cpp_constexpr) && __cpp_constexpr >= 200704)
00158 #define FLATBUFFERS_CONSTEXPR constexpr
00159 #else
00160 #define FLATBUFFERS_CONSTEXPR const
00161 #endif
00162
00163 #if (defined(__cplusplus) && __cplusplus >= 201402L) || \
00164 (defined(__cpp_constexpr) && __cpp_constexpr >= 201304)
00165 #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR
00166 #else
00167 #define FLATBUFFERS_CONSTEXPR_CPP14
00168 #endif
00169
00170 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \
00171 (defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190023026)) || \
00172 defined(__clang__)
00173 #define FLATBUFFERS_NOEXCEPT noexcept
00174 #else
00175 #define FLATBUFFERS_NOEXCEPT
00176 #endif
00177
00178
00179
00180 #if (!defined(_MSC_VER) || _MSC_FULL_VER >= 180020827) && \
00181 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 404)) || \
00182 defined(__clang__)
00183 #define FLATBUFFERS_DELETE_FUNC(func) func = delete;
00184 #else
00185 #define FLATBUFFERS_DELETE_FUNC(func) private: func;
00186 #endif
00187
00188 #ifndef FLATBUFFERS_HAS_STRING_VIEW
00189
00190
00191 #if defined(__has_include)
00192
00193 #if __has_include(<string_view>) && (__cplusplus >= 201606 || _HAS_CXX17)
00194 #include <string_view>
00195 namespace flatbuffers {
00196 typedef std::string_view string_view;
00197 }
00198 #define FLATBUFFERS_HAS_STRING_VIEW 1
00199
00200 #elif __has_include(<experimental/string_view>) && (__cplusplus >= 201411)
00201 #include <experimental/string_view>
00202 namespace flatbuffers {
00203 typedef std::experimental::string_view string_view;
00204 }
00205 #define FLATBUFFERS_HAS_STRING_VIEW 1
00206 #endif
00207 #endif // __has_include
00208 #endif // !FLATBUFFERS_HAS_STRING_VIEW
00209
00210 #ifndef FLATBUFFERS_HAS_NEW_STRTOD
00211
00212
00213
00214 #if (defined(_MSC_VER) && _MSC_VER >= 1900) || \
00215 (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \
00216 (defined(__clang__))
00217 #define FLATBUFFERS_HAS_NEW_STRTOD 1
00218 #endif
00219 #endif // !FLATBUFFERS_HAS_NEW_STRTOD
00220
00221 #ifndef FLATBUFFERS_LOCALE_INDEPENDENT
00222
00223
00224
00225 #if ((defined(_MSC_VER) && _MSC_VER >= 1800) || \
00226 (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE>=700)))
00227 #define FLATBUFFERS_LOCALE_INDEPENDENT 1
00228 #else
00229 #define FLATBUFFERS_LOCALE_INDEPENDENT 0
00230 #endif
00231 #endif // !FLATBUFFERS_LOCALE_INDEPENDENT
00232
00233
00234
00235
00236 #if defined(__clang__)
00237 #define __supress_ubsan__(type) __attribute__((no_sanitize(type)))
00238 #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)
00239 #define __supress_ubsan__(type) __attribute__((no_sanitize_undefined))
00240 #else
00241 #define __supress_ubsan__(type)
00242 #endif
00243
00244
00245
00246 template<typename T> FLATBUFFERS_CONSTEXPR inline bool IsConstTrue(T t) {
00247 return !!t;
00248 }
00249
00250
00251 #if ((__cplusplus >= 201703L) \
00252 || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
00253
00254 #define FLATBUFFERS_ATTRIBUTE(attr) [[attr]]
00255
00256 #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]]
00257 #else
00258 #define FLATBUFFERS_ATTRIBUTE(attr)
00259
00260 #if FLATBUFFERS_CLANG >= 30800
00261 #define FLATBUFFERS_FALLTHROUGH() [[clang::fallthrough]]
00262 #elif FLATBUFFERS_GCC >= 70300
00263 #define FLATBUFFERS_FALLTHROUGH() [[gnu::fallthrough]]
00264 #else
00265 #define FLATBUFFERS_FALLTHROUGH()
00266 #endif
00267 #endif
00268
00270
00272 namespace flatbuffers {
00273
00275
00276
00277
00278 typedef uint32_t uoffset_t;
00279
00280
00281 typedef int32_t soffset_t;
00282
00283
00284
00285 typedef uint16_t voffset_t;
00286
00287 typedef uintmax_t largest_scalar_t;
00288
00289
00290 #define FLATBUFFERS_MAX_BUFFER_SIZE ((1ULL << (sizeof(soffset_t) * 8 - 1)) - 1)
00291
00292
00293 #define FLATBUFFERS_MAX_ALIGNMENT 16
00294
00295 #if defined(_MSC_VER)
00296 #pragma warning(push)
00297 #pragma warning(disable: 4127) // C4127: conditional expression is constant
00298 #endif
00299
00300 template<typename T> T EndianSwap(T t) {
00301 #if defined(_MSC_VER)
00302 #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort
00303 #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong
00304 #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64
00305 #else
00306 #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__)
00307
00308 #define FLATBUFFERS_BYTESWAP16(x) \
00309 static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16))
00310 #else
00311 #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16
00312 #endif
00313 #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32
00314 #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64
00315 #endif
00316 if (sizeof(T) == 1) {
00317 return t;
00318 } else if (sizeof(T) == 2) {
00319 union { T t; uint16_t i; } u;
00320 u.t = t;
00321 u.i = FLATBUFFERS_BYTESWAP16(u.i);
00322 return u.t;
00323 } else if (sizeof(T) == 4) {
00324 union { T t; uint32_t i; } u;
00325 u.t = t;
00326 u.i = FLATBUFFERS_BYTESWAP32(u.i);
00327 return u.t;
00328 } else if (sizeof(T) == 8) {
00329 union { T t; uint64_t i; } u;
00330 u.t = t;
00331 u.i = FLATBUFFERS_BYTESWAP64(u.i);
00332 return u.t;
00333 } else {
00334 FLATBUFFERS_ASSERT(0);
00335 }
00336 }
00337
00338 #if defined(_MSC_VER)
00339 #pragma warning(pop)
00340 #endif
00341
00342
00343 template<typename T> T EndianScalar(T t) {
00344 #if FLATBUFFERS_LITTLEENDIAN
00345 return t;
00346 #else
00347 return EndianSwap(t);
00348 #endif
00349 }
00350
00351 template<typename T>
00352
00353 __supress_ubsan__("alignment")
00354 T ReadScalar(const void *p) {
00355 return EndianScalar(*reinterpret_cast<const T *>(p));
00356 }
00357
00358 template<typename T>
00359
00360 __supress_ubsan__("alignment")
00361 void WriteScalar(void *p, T t) {
00362 *reinterpret_cast<T *>(p) = EndianScalar(t);
00363 }
00364
00365
00366
00367
00368 inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) {
00369 return ((~buf_size) + 1) & (scalar_size - 1);
00370 }
00371
00372 }
00373 #endif // FLATBUFFERS_BASE_H_