base.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #ifndef FLATBUFFERS_BASE_H_
  2. #define FLATBUFFERS_BASE_H_
  3. // clang-format off
  4. // If activate should be declared and included first.
  5. #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
  6. defined(_MSC_VER) && defined(_DEBUG)
  7. // The _CRTDBG_MAP_ALLOC inside <crtdbg.h> will replace
  8. // calloc/free (etc) to its debug version using #define directives.
  9. #define _CRTDBG_MAP_ALLOC
  10. #include <stdlib.h>
  11. #include <crtdbg.h>
  12. // Replace operator new by trace-enabled version.
  13. #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
  14. #define new DEBUG_NEW
  15. #endif
  16. #if !defined(FLATBUFFERS_ASSERT)
  17. #include <assert.h>
  18. #define FLATBUFFERS_ASSERT assert
  19. #elif defined(FLATBUFFERS_ASSERT_INCLUDE)
  20. // Include file with forward declaration
  21. #include FLATBUFFERS_ASSERT_INCLUDE
  22. #endif
  23. #ifndef ARDUINO
  24. #include <cstdint>
  25. #endif
  26. #include <cstddef>
  27. #include <cstdlib>
  28. #include <cstring>
  29. #if defined(ARDUINO) && !defined(ARDUINOSTL_M_H)
  30. #include <utility.h>
  31. #else
  32. #include <utility>
  33. #endif
  34. #include <string>
  35. #include <type_traits>
  36. #include <vector>
  37. #include <set>
  38. #include <algorithm>
  39. #include <iterator>
  40. #include <memory>
  41. #ifdef _STLPORT_VERSION
  42. #define FLATBUFFERS_CPP98_STL
  43. #endif
  44. #ifndef FLATBUFFERS_CPP98_STL
  45. #include <functional>
  46. #endif
  47. #include "flatbuffers/stl_emulation.h"
  48. #if defined(__ICCARM__)
  49. #include <intrinsics.h>
  50. #endif
  51. // Note the __clang__ check is needed, because clang presents itself
  52. // as an older GNUC compiler (4.2).
  53. // Clang 3.3 and later implement all of the ISO C++ 2011 standard.
  54. // Clang 3.4 and later implement all of the ISO C++ 2014 standard.
  55. // http://clang.llvm.org/cxx_status.html
  56. // Note the MSVC value '__cplusplus' may be incorrect:
  57. // The '__cplusplus' predefined macro in the MSVC stuck at the value 199711L,
  58. // indicating (erroneously!) that the compiler conformed to the C++98 Standard.
  59. // This value should be correct starting from MSVC2017-15.7-Preview-3.
  60. // The '__cplusplus' will be valid only if MSVC2017-15.7-P3 and the `/Zc:__cplusplus` switch is set.
  61. // Workaround (for details see MSDN):
  62. // Use the _MSC_VER and _MSVC_LANG definition instead of the __cplusplus for compatibility.
  63. // The _MSVC_LANG macro reports the Standard version regardless of the '/Zc:__cplusplus' switch.
  64. #if defined(__GNUC__) && !defined(__clang__)
  65. #define FLATBUFFERS_GCC (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  66. #else
  67. #define FLATBUFFERS_GCC 0
  68. #endif
  69. #if defined(__clang__)
  70. #define FLATBUFFERS_CLANG (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
  71. #else
  72. #define FLATBUFFERS_CLANG 0
  73. #endif
  74. /// @cond FLATBUFFERS_INTERNAL
  75. #if __cplusplus <= 199711L && \
  76. (!defined(_MSC_VER) || _MSC_VER < 1600) && \
  77. (!defined(__GNUC__) || \
  78. (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40400))
  79. #error A C++11 compatible compiler with support for the auto typing is \
  80. required for FlatBuffers.
  81. #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
  82. #endif
  83. #if !defined(__clang__) && \
  84. defined(__GNUC__) && \
  85. (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600)
  86. // Backwards compatibility for g++ 4.4, and 4.5 which don't have the nullptr
  87. // and constexpr keywords. Note the __clang__ check is needed, because clang
  88. // presents itself as an older GNUC compiler.
  89. #ifndef nullptr_t
  90. const class nullptr_t {
  91. public:
  92. template<class T> inline operator T*() const { return 0; }
  93. private:
  94. void operator&() const;
  95. } nullptr = {};
  96. #endif
  97. #ifndef constexpr
  98. #define constexpr const
  99. #endif
  100. #endif
  101. // The wire format uses a little endian encoding (since that's efficient for
  102. // the common platforms).
  103. #if defined(__s390x__)
  104. #define FLATBUFFERS_LITTLEENDIAN 0
  105. #endif // __s390x__
  106. #if !defined(FLATBUFFERS_LITTLEENDIAN)
  107. #if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
  108. #if (defined(__BIG_ENDIAN__) || \
  109. (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
  110. #define FLATBUFFERS_LITTLEENDIAN 0
  111. #else
  112. #define FLATBUFFERS_LITTLEENDIAN 1
  113. #endif // __BIG_ENDIAN__
  114. #elif defined(_MSC_VER)
  115. #if defined(_M_PPC)
  116. #define FLATBUFFERS_LITTLEENDIAN 0
  117. #else
  118. #define FLATBUFFERS_LITTLEENDIAN 1
  119. #endif
  120. #else
  121. #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN.
  122. #endif
  123. #endif // !defined(FLATBUFFERS_LITTLEENDIAN)
  124. #define FLATBUFFERS_VERSION_MAJOR 1
  125. #define FLATBUFFERS_VERSION_MINOR 12
  126. #define FLATBUFFERS_VERSION_REVISION 0
  127. #define FLATBUFFERS_STRING_EXPAND(X) #X
  128. #define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
  129. namespace flatbuffers {
  130. // Returns version as string "MAJOR.MINOR.REVISION".
  131. const char* FLATBUFFERS_VERSION();
  132. }
  133. #if (!defined(_MSC_VER) || _MSC_VER > 1600) && \
  134. (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407)) || \
  135. defined(__clang__)
  136. #define FLATBUFFERS_FINAL_CLASS final
  137. #define FLATBUFFERS_OVERRIDE override
  138. #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE : flatbuffers::voffset_t
  139. #else
  140. #define FLATBUFFERS_FINAL_CLASS
  141. #define FLATBUFFERS_OVERRIDE
  142. #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE
  143. #endif
  144. #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
  145. (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \
  146. (defined(__cpp_constexpr) && __cpp_constexpr >= 200704)
  147. #define FLATBUFFERS_CONSTEXPR constexpr
  148. #else
  149. #define FLATBUFFERS_CONSTEXPR const
  150. #endif
  151. #if (defined(__cplusplus) && __cplusplus >= 201402L) || \
  152. (defined(__cpp_constexpr) && __cpp_constexpr >= 201304)
  153. #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR
  154. #else
  155. #define FLATBUFFERS_CONSTEXPR_CPP14
  156. #endif
  157. #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \
  158. (defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190023026)) || \
  159. defined(__clang__)
  160. #define FLATBUFFERS_NOEXCEPT noexcept
  161. #else
  162. #define FLATBUFFERS_NOEXCEPT
  163. #endif
  164. // NOTE: the FLATBUFFERS_DELETE_FUNC macro may change the access mode to
  165. // private, so be sure to put it at the end or reset access mode explicitly.
  166. #if (!defined(_MSC_VER) || _MSC_FULL_VER >= 180020827) && \
  167. (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 404)) || \
  168. defined(__clang__)
  169. #define FLATBUFFERS_DELETE_FUNC(func) func = delete;
  170. #else
  171. #define FLATBUFFERS_DELETE_FUNC(func) private: func;
  172. #endif
  173. #ifndef FLATBUFFERS_HAS_STRING_VIEW
  174. // Only provide flatbuffers::string_view if __has_include can be used
  175. // to detect a header that provides an implementation
  176. #if defined(__has_include)
  177. // Check for std::string_view (in c++17)
  178. #if __has_include(<string_view>) && (__cplusplus >= 201606 || (defined(_HAS_CXX17) && _HAS_CXX17))
  179. #include <string_view>
  180. namespace flatbuffers {
  181. typedef std::string_view string_view;
  182. }
  183. #define FLATBUFFERS_HAS_STRING_VIEW 1
  184. // Check for std::experimental::string_view (in c++14, compiler-dependent)
  185. #elif __has_include(<experimental/string_view>) && (__cplusplus >= 201411)
  186. #include <experimental/string_view>
  187. namespace flatbuffers {
  188. typedef std::experimental::string_view string_view;
  189. }
  190. #define FLATBUFFERS_HAS_STRING_VIEW 1
  191. // Check for absl::string_view
  192. #elif __has_include("absl/strings/string_view.h")
  193. #include "absl/strings/string_view.h"
  194. namespace flatbuffers {
  195. typedef absl::string_view string_view;
  196. }
  197. #define FLATBUFFERS_HAS_STRING_VIEW 1
  198. #endif
  199. #endif // __has_include
  200. #endif // !FLATBUFFERS_HAS_STRING_VIEW
  201. #ifndef FLATBUFFERS_HAS_NEW_STRTOD
  202. // Modern (C++11) strtod and strtof functions are available for use.
  203. // 1) nan/inf strings as argument of strtod;
  204. // 2) hex-float as argument of strtod/strtof.
  205. #if (defined(_MSC_VER) && _MSC_VER >= 1900) || \
  206. (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \
  207. (defined(__clang__))
  208. #define FLATBUFFERS_HAS_NEW_STRTOD 1
  209. #endif
  210. #endif // !FLATBUFFERS_HAS_NEW_STRTOD
  211. #ifndef FLATBUFFERS_LOCALE_INDEPENDENT
  212. // Enable locale independent functions {strtof_l, strtod_l,strtoll_l, strtoull_l}.
  213. // They are part of the POSIX-2008 but not part of the C/C++ standard.
  214. // GCC/Clang have definition (_XOPEN_SOURCE>=700) if POSIX-2008.
  215. #if ((defined(_MSC_VER) && _MSC_VER >= 1800) || \
  216. (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE>=700)))
  217. #define FLATBUFFERS_LOCALE_INDEPENDENT 1
  218. #else
  219. #define FLATBUFFERS_LOCALE_INDEPENDENT 0
  220. #endif
  221. #endif // !FLATBUFFERS_LOCALE_INDEPENDENT
  222. // Suppress Undefined Behavior Sanitizer (recoverable only). Usage:
  223. // - __supress_ubsan__("undefined")
  224. // - __supress_ubsan__("signed-integer-overflow")
  225. #if defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))
  226. #define __supress_ubsan__(type) __attribute__((no_sanitize(type)))
  227. #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)
  228. #define __supress_ubsan__(type) __attribute__((no_sanitize_undefined))
  229. #else
  230. #define __supress_ubsan__(type)
  231. #endif
  232. // This is constexpr function used for checking compile-time constants.
  233. // Avoid `#pragma warning(disable: 4127) // C4127: expression is constant`.
  234. template<typename T> FLATBUFFERS_CONSTEXPR inline bool IsConstTrue(T t) {
  235. return !!t;
  236. }
  237. // Enable C++ attribute [[]] if std:c++17 or higher.
  238. #if ((__cplusplus >= 201703L) \
  239. || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
  240. // All attributes unknown to an implementation are ignored without causing an error.
  241. #define FLATBUFFERS_ATTRIBUTE(attr) [[attr]]
  242. #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]]
  243. #else
  244. #define FLATBUFFERS_ATTRIBUTE(attr)
  245. #if FLATBUFFERS_CLANG >= 30800
  246. #define FLATBUFFERS_FALLTHROUGH() [[clang::fallthrough]]
  247. #elif FLATBUFFERS_GCC >= 70300
  248. #define FLATBUFFERS_FALLTHROUGH() [[gnu::fallthrough]]
  249. #else
  250. #define FLATBUFFERS_FALLTHROUGH()
  251. #endif
  252. #endif
  253. /// @endcond
  254. /// @file
  255. namespace flatbuffers {
  256. /// @cond FLATBUFFERS_INTERNAL
  257. // Our default offset / size type, 32bit on purpose on 64bit systems.
  258. // Also, using a consistent offset type maintains compatibility of serialized
  259. // offset values between 32bit and 64bit systems.
  260. typedef uint32_t uoffset_t;
  261. // Signed offsets for references that can go in both directions.
  262. typedef int32_t soffset_t;
  263. // Offset/index used in v-tables, can be changed to uint8_t in
  264. // format forks to save a bit of space if desired.
  265. typedef uint16_t voffset_t;
  266. typedef uintmax_t largest_scalar_t;
  267. // In 32bits, this evaluates to 2GB - 1
  268. #define FLATBUFFERS_MAX_BUFFER_SIZE ((1ULL << (sizeof(::flatbuffers::soffset_t) * 8 - 1)) - 1)
  269. // We support aligning the contents of buffers up to this size.
  270. #define FLATBUFFERS_MAX_ALIGNMENT 16
  271. #if defined(_MSC_VER)
  272. #pragma warning(push)
  273. #pragma warning(disable: 4127) // C4127: conditional expression is constant
  274. #endif
  275. template<typename T> T EndianSwap(T t) {
  276. #if defined(_MSC_VER)
  277. #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort
  278. #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong
  279. #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64
  280. #elif defined(__ICCARM__)
  281. #define FLATBUFFERS_BYTESWAP16 __REV16
  282. #define FLATBUFFERS_BYTESWAP32 __REV
  283. #define FLATBUFFERS_BYTESWAP64(x) \
  284. ((__REV(static_cast<uint32_t>(x >> 32U))) | (static_cast<uint64_t>(__REV(static_cast<uint32_t>(x)))) << 32U)
  285. #else
  286. #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__)
  287. // __builtin_bswap16 was missing prior to GCC 4.8.
  288. #define FLATBUFFERS_BYTESWAP16(x) \
  289. static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16))
  290. #else
  291. #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16
  292. #endif
  293. #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32
  294. #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64
  295. #endif
  296. if (sizeof(T) == 1) { // Compile-time if-then's.
  297. return t;
  298. } else if (sizeof(T) == 2) {
  299. union { T t; uint16_t i; } u = { t };
  300. u.i = FLATBUFFERS_BYTESWAP16(u.i);
  301. return u.t;
  302. } else if (sizeof(T) == 4) {
  303. union { T t; uint32_t i; } u = { t };
  304. u.i = FLATBUFFERS_BYTESWAP32(u.i);
  305. return u.t;
  306. } else if (sizeof(T) == 8) {
  307. union { T t; uint64_t i; } u = { t };
  308. u.i = FLATBUFFERS_BYTESWAP64(u.i);
  309. return u.t;
  310. } else {
  311. FLATBUFFERS_ASSERT(0);
  312. return t;
  313. }
  314. }
  315. #if defined(_MSC_VER)
  316. #pragma warning(pop)
  317. #endif
  318. template<typename T> T EndianScalar(T t) {
  319. #if FLATBUFFERS_LITTLEENDIAN
  320. return t;
  321. #else
  322. return EndianSwap(t);
  323. #endif
  324. }
  325. template<typename T>
  326. // UBSAN: C++ aliasing type rules, see std::bit_cast<> for details.
  327. __supress_ubsan__("alignment")
  328. T ReadScalar(const void *p) {
  329. return EndianScalar(*reinterpret_cast<const T *>(p));
  330. }
  331. template<typename T>
  332. // UBSAN: C++ aliasing type rules, see std::bit_cast<> for details.
  333. __supress_ubsan__("alignment")
  334. void WriteScalar(void *p, T t) {
  335. *reinterpret_cast<T *>(p) = EndianScalar(t);
  336. }
  337. template<typename T> struct Offset;
  338. template<typename T> __supress_ubsan__("alignment") void WriteScalar(void *p, Offset<T> t) {
  339. *reinterpret_cast<uoffset_t *>(p) = EndianScalar(t.o);
  340. }
  341. // Computes how many bytes you'd have to pad to be able to write an
  342. // "scalar_size" scalar if the buffer had grown to "buf_size" (downwards in
  343. // memory).
  344. __supress_ubsan__("unsigned-integer-overflow")
  345. inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) {
  346. return ((~buf_size) + 1) & (scalar_size - 1);
  347. }
  348. } // namespace flatbuffers
  349. #endif // FLATBUFFERS_BASE_H_