Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
base.h
00001 #ifndef FLATBUFFERS_BASE_H_ 00002 #define FLATBUFFERS_BASE_H_ 00003 00004 // clang-format off 00005 00006 // If activate should be declared and included first. 00007 #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \ 00008 defined(_MSC_VER) && defined(_DEBUG) 00009 // The _CRTDBG_MAP_ALLOC inside <crtdbg.h> will replace 00010 // calloc/free (etc) to its debug version using #define directives. 00011 #define _CRTDBG_MAP_ALLOC 00012 #include <stdlib.h> 00013 #include <crtdbg.h> 00014 // Replace operator new by trace-enabled version. 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 // Include file with forward declaration 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 // Note the __clang__ check is needed, because clang presents itself 00059 // as an older GNUC compiler (4.2). 00060 // Clang 3.3 and later implement all of the ISO C++ 2011 standard. 00061 // Clang 3.4 and later implement all of the ISO C++ 2014 standard. 00062 // http://clang.llvm.org/cxx_status.html 00063 00064 // Note the MSVC value '__cplusplus' may be incorrect: 00065 // The '__cplusplus' predefined macro in the MSVC stuck at the value 199711L, 00066 // indicating (erroneously!) that the compiler conformed to the C++98 Standard. 00067 // This value should be correct starting from MSVC2017-15.7-Preview-3. 00068 // The '__cplusplus' will be valid only if MSVC2017-15.7-P3 and the `/Zc:__cplusplus` switch is set. 00069 // Workaround (for details see MSDN): 00070 // Use the _MSC_VER and _MSVC_LANG definition instead of the __cplusplus for compatibility. 00071 // The _MSVC_LANG macro reports the Standard version regardless of the '/Zc:__cplusplus' switch. 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 00085 /// @cond FLATBUFFERS_INTERNAL 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 // Backwards compatability for g++ 4.4, and 4.5 which don't have the nullptr 00099 // and constexpr keywords. Note the __clang__ check is needed, because clang 00100 // presents itself as an older GNUC compiler. 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 // The wire format uses a little endian encoding (since that's efficient for 00115 // the common platforms). 00116 #if defined(__s390x__) 00117 #define FLATBUFFERS_LITTLEENDIAN 0 00118 #endif // __s390x__ 00119 #if !defined(FLATBUFFERS_LITTLEENDIAN) 00120 #if defined(__GNUC__) || defined(__clang__) 00121 #if (defined(__BIG_ENDIAN__) || \ 00122 (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) 00123 #define FLATBUFFERS_LITTLEENDIAN 0 00124 #else 00125 #define FLATBUFFERS_LITTLEENDIAN 1 00126 #endif // __BIG_ENDIAN__ 00127 #elif defined(_MSC_VER) 00128 #if defined(_M_PPC) 00129 #define FLATBUFFERS_LITTLEENDIAN 0 00130 #else 00131 #define FLATBUFFERS_LITTLEENDIAN 1 00132 #endif 00133 #else 00134 #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN. 00135 #endif 00136 #endif // !defined(FLATBUFFERS_LITTLEENDIAN) 00137 00138 #define FLATBUFFERS_VERSION_MAJOR 1 00139 #define FLATBUFFERS_VERSION_MINOR 11 00140 #define FLATBUFFERS_VERSION_REVISION 0 00141 #define FLATBUFFERS_STRING_EXPAND(X) #X 00142 #define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X) 00143 00144 #if (!defined(_MSC_VER) || _MSC_VER > 1600) && \ 00145 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407)) || \ 00146 defined(__clang__) 00147 #define FLATBUFFERS_FINAL_CLASS final 00148 #define FLATBUFFERS_OVERRIDE override 00149 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE : flatbuffers::voffset_t 00150 #else 00151 #define FLATBUFFERS_FINAL_CLASS 00152 #define FLATBUFFERS_OVERRIDE 00153 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE 00154 #endif 00155 00156 #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \ 00157 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \ 00158 (defined(__cpp_constexpr) && __cpp_constexpr >= 200704) 00159 #define FLATBUFFERS_CONSTEXPR constexpr 00160 #else 00161 #define FLATBUFFERS_CONSTEXPR const 00162 #endif 00163 00164 #if (defined(__cplusplus) && __cplusplus >= 201402L) || \ 00165 (defined(__cpp_constexpr) && __cpp_constexpr >= 201304) 00166 #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR 00167 #else 00168 #define FLATBUFFERS_CONSTEXPR_CPP14 00169 #endif 00170 00171 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \ 00172 (defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190023026)) || \ 00173 defined(__clang__) 00174 #define FLATBUFFERS_NOEXCEPT noexcept 00175 #else 00176 #define FLATBUFFERS_NOEXCEPT 00177 #endif 00178 00179 // NOTE: the FLATBUFFERS_DELETE_FUNC macro may change the access mode to 00180 // private, so be sure to put it at the end or reset access mode explicitly. 00181 #if (!defined(_MSC_VER) || _MSC_FULL_VER >= 180020827) && \ 00182 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 404)) || \ 00183 defined(__clang__) 00184 #define FLATBUFFERS_DELETE_FUNC(func) func = delete; 00185 #else 00186 #define FLATBUFFERS_DELETE_FUNC(func) private: func; 00187 #endif 00188 00189 #ifndef FLATBUFFERS_HAS_STRING_VIEW 00190 // Only provide flatbuffers::string_view if __has_include can be used 00191 // to detect a header that provides an implementation 00192 #if defined(__has_include) 00193 // Check for std::string_view (in c++17) 00194 #if __has_include(<string_view>) && (__cplusplus >= 201606 || _HAS_CXX17) 00195 #include <string_view> 00196 namespace flatbuffers { 00197 typedef std::string_view string_view; 00198 } 00199 #define FLATBUFFERS_HAS_STRING_VIEW 1 00200 // Check for std::experimental::string_view (in c++14, compiler-dependent) 00201 #elif __has_include(<experimental/string_view>) && (__cplusplus >= 201411) 00202 #include <experimental/string_view> 00203 namespace flatbuffers { 00204 typedef std::experimental::string_view string_view; 00205 } 00206 #define FLATBUFFERS_HAS_STRING_VIEW 1 00207 #endif 00208 #endif // __has_include 00209 #endif // !FLATBUFFERS_HAS_STRING_VIEW 00210 00211 #ifndef FLATBUFFERS_HAS_NEW_STRTOD 00212 // Modern (C++11) strtod and strtof functions are available for use. 00213 // 1) nan/inf strings as argument of strtod; 00214 // 2) hex-float as argument of strtod/strtof. 00215 #if (defined(_MSC_VER) && _MSC_VER >= 1900) || \ 00216 (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \ 00217 (defined(__clang__)) 00218 #define FLATBUFFERS_HAS_NEW_STRTOD 1 00219 #endif 00220 #endif // !FLATBUFFERS_HAS_NEW_STRTOD 00221 00222 #ifndef FLATBUFFERS_LOCALE_INDEPENDENT 00223 // Enable locale independent functions {strtof_l, strtod_l,strtoll_l, strtoull_l}. 00224 // They are part of the POSIX-2008 but not part of the C/C++ standard. 00225 // GCC/Clang have definition (_XOPEN_SOURCE>=700) if POSIX-2008. 00226 #if ((defined(_MSC_VER) && _MSC_VER >= 1800) || \ 00227 (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE>=700))) 00228 #define FLATBUFFERS_LOCALE_INDEPENDENT 1 00229 #else 00230 #define FLATBUFFERS_LOCALE_INDEPENDENT 0 00231 #endif 00232 #endif // !FLATBUFFERS_LOCALE_INDEPENDENT 00233 00234 // Suppress Undefined Behavior Sanitizer (recoverable only). Usage: 00235 // - __supress_ubsan__("undefined") 00236 // - __supress_ubsan__("signed-integer-overflow") 00237 #if defined(__clang__) 00238 #define __supress_ubsan__(type) __attribute__((no_sanitize(type))) 00239 #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409) 00240 #define __supress_ubsan__(type) __attribute__((no_sanitize_undefined)) 00241 #else 00242 #define __supress_ubsan__(type) 00243 #endif 00244 00245 // This is constexpr function used for checking compile-time constants. 00246 // Avoid `#pragma warning(disable: 4127) // C4127: expression is constant`. 00247 template<typename T> FLATBUFFERS_CONSTEXPR inline bool IsConstTrue(T t) { 00248 return !!t; 00249 } 00250 00251 // Enable C++ attribute [[]] if std:c++17 or higher. 00252 #if ((__cplusplus >= 201703L) \ 00253 || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))) 00254 // All attributes unknown to an implementation are ignored without causing an error. 00255 #define FLATBUFFERS_ATTRIBUTE(attr) [[attr]] 00256 00257 #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]] 00258 #else 00259 #define FLATBUFFERS_ATTRIBUTE(attr) 00260 00261 #if FLATBUFFERS_CLANG >= 30800 00262 #define FLATBUFFERS_FALLTHROUGH() [[clang::fallthrough]] 00263 #elif FLATBUFFERS_GCC >= 70300 00264 #define FLATBUFFERS_FALLTHROUGH() [[gnu::fallthrough]] 00265 #else 00266 #define FLATBUFFERS_FALLTHROUGH() 00267 #endif 00268 #endif 00269 00270 /// @endcond 00271 00272 /// @file 00273 namespace flatbuffers { 00274 00275 /// @cond FLATBUFFERS_INTERNAL 00276 // Our default offset / size type, 32bit on purpose on 64bit systems. 00277 // Also, using a consistent offset type maintains compatibility of serialized 00278 // offset values between 32bit and 64bit systems. 00279 typedef uint32_t uoffset_t; 00280 00281 // Signed offsets for references that can go in both directions. 00282 typedef int32_t soffset_t; 00283 00284 // Offset/index used in v-tables, can be changed to uint8_t in 00285 // format forks to save a bit of space if desired. 00286 typedef uint16_t voffset_t; 00287 00288 typedef uintmax_t largest_scalar_t; 00289 00290 // In 32bits, this evaluates to 2GB - 1 00291 #define FLATBUFFERS_MAX_BUFFER_SIZE ((1ULL << (sizeof(soffset_t) * 8 - 1)) - 1) 00292 00293 // We support aligning the contents of buffers up to this size. 00294 #define FLATBUFFERS_MAX_ALIGNMENT 16 00295 00296 #if defined(_MSC_VER) 00297 #pragma warning(push) 00298 #pragma warning(disable: 4127) // C4127: conditional expression is constant 00299 #endif 00300 00301 template<typename T> T EndianSwap(T t) { 00302 #if defined(_MSC_VER) 00303 #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort 00304 #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong 00305 #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64 00306 #else 00307 #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__) 00308 // __builtin_bswap16 was missing prior to GCC 4.8. 00309 #define FLATBUFFERS_BYTESWAP16(x) \ 00310 static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16)) 00311 #else 00312 #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16 00313 #endif 00314 #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32 00315 #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64 00316 #endif 00317 if (sizeof(T) == 1) { // Compile-time if-then's. 00318 return t; 00319 } else if (sizeof(T) == 2) { 00320 union { T t; uint16_t i; } u; 00321 u.t = t; 00322 u.i = FLATBUFFERS_BYTESWAP16(u.i); 00323 return u.t; 00324 } else if (sizeof(T) == 4) { 00325 union { T t; uint32_t i; } u; 00326 u.t = t; 00327 u.i = FLATBUFFERS_BYTESWAP32(u.i); 00328 return u.t; 00329 } else if (sizeof(T) == 8) { 00330 union { T t; uint64_t i; } u; 00331 u.t = t; 00332 u.i = FLATBUFFERS_BYTESWAP64(u.i); 00333 return u.t; 00334 } else { 00335 FLATBUFFERS_ASSERT(0); 00336 } 00337 } 00338 00339 #if defined(_MSC_VER) 00340 #pragma warning(pop) 00341 #endif 00342 00343 00344 template<typename T> T EndianScalar(T t) { 00345 #if FLATBUFFERS_LITTLEENDIAN 00346 return t; 00347 #else 00348 return EndianSwap(t); 00349 #endif 00350 } 00351 00352 template<typename T> 00353 // UBSAN: C++ aliasing type rules, see std::bit_cast<> for details. 00354 __supress_ubsan__("alignment") 00355 T ReadScalar(const void *p) { 00356 return EndianScalar(*reinterpret_cast<const T *>(p)); 00357 } 00358 00359 template<typename T> 00360 // UBSAN: C++ aliasing type rules, see std::bit_cast<> for details. 00361 __supress_ubsan__("alignment") 00362 void WriteScalar(void *p, T t) { 00363 *reinterpret_cast<T *>(p) = EndianScalar(t); 00364 } 00365 00366 template<typename T> struct Offset; 00367 template<typename T> __supress_ubsan__("alignment") void WriteScalar(void *p, Offset<T> t) { 00368 *reinterpret_cast<uoffset_t *>(p) = EndianScalar(t.o); 00369 } 00370 00371 // Computes how many bytes you'd have to pad to be able to write an 00372 // "scalar_size" scalar if the buffer had grown to "buf_size" (downwards in 00373 // memory). 00374 inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) { 00375 return ((~buf_size) + 1) & (scalar_size - 1); 00376 } 00377 00378 } // namespace flatbuffers 00379 #endif // FLATBUFFERS_BASE_H_
Generated on Wed Jul 13 2022 16:03:35 by
1.7.2