|
| 1 | +#ifndef FLATBUFFERS_BASE_H_ |
| 2 | +#define FLATBUFFERS_BASE_H_ |
| 3 | + |
| 4 | +// clang-format off |
| 5 | +#if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \ |
| 6 | + defined(_MSC_VER) && defined(_DEBUG) |
| 7 | + #define _CRTDBG_MAP_ALLOC |
| 8 | +#endif |
| 9 | + |
| 10 | +#include <assert.h> |
| 11 | + |
| 12 | +#ifndef ARDUINO |
| 13 | +#include <cstdint> |
| 14 | +#endif |
| 15 | + |
| 16 | +#include <cstddef> |
| 17 | +#include <cstdlib> |
| 18 | +#include <cstring> |
| 19 | + |
| 20 | +#if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \ |
| 21 | + defined(_MSC_VER) && defined(_DEBUG) |
| 22 | + #include <crtdbg.h> |
| 23 | + #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) |
| 24 | + #define new DEBUG_NEW |
| 25 | +#endif |
| 26 | + |
| 27 | +#if defined(ARDUINO) && !defined(ARDUINOSTL_M_H) |
| 28 | + #include <utility.h> |
| 29 | +#else |
| 30 | + #include <utility> |
| 31 | +#endif |
| 32 | + |
| 33 | +#include <string> |
| 34 | +#include <type_traits> |
| 35 | +#include <vector> |
| 36 | +#include <set> |
| 37 | +#include <algorithm> |
| 38 | +#include <iterator> |
| 39 | +#include <memory> |
| 40 | + |
| 41 | +#ifdef _STLPORT_VERSION |
| 42 | + #define FLATBUFFERS_CPP98_STL |
| 43 | +#endif |
| 44 | +#ifndef FLATBUFFERS_CPP98_STL |
| 45 | + #include <functional> |
| 46 | +#endif |
| 47 | + |
| 48 | +#include "flatbuffers/stl_emulation.h" |
| 49 | + |
| 50 | +/// @cond FLATBUFFERS_INTERNAL |
| 51 | +#if __cplusplus <= 199711L && \ |
| 52 | + (!defined(_MSC_VER) || _MSC_VER < 1600) && \ |
| 53 | + (!defined(__GNUC__) || \ |
| 54 | + (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40400)) |
| 55 | + #error A C++11 compatible compiler with support for the auto typing is \ |
| 56 | + required for FlatBuffers. |
| 57 | + #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__ |
| 58 | +#endif |
| 59 | + |
| 60 | +#if !defined(__clang__) && \ |
| 61 | + defined(__GNUC__) && \ |
| 62 | + (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600) |
| 63 | + // Backwards compatability for g++ 4.4, and 4.5 which don't have the nullptr |
| 64 | + // and constexpr keywords. Note the __clang__ check is needed, because clang |
| 65 | + // presents itself as an older GNUC compiler. |
| 66 | + #ifndef nullptr_t |
| 67 | + const class nullptr_t { |
| 68 | + public: |
| 69 | + template<class T> inline operator T*() const { return 0; } |
| 70 | + private: |
| 71 | + void operator&() const; |
| 72 | + } nullptr = {}; |
| 73 | + #endif |
| 74 | + #ifndef constexpr |
| 75 | + #define constexpr const |
| 76 | + #endif |
| 77 | +#endif |
| 78 | + |
| 79 | +// The wire format uses a little endian encoding (since that's efficient for |
| 80 | +// the common platforms). |
| 81 | +#if defined(__s390x__) |
| 82 | + #define FLATBUFFERS_LITTLEENDIAN 0 |
| 83 | +#endif // __s390x__ |
| 84 | +#if !defined(FLATBUFFERS_LITTLEENDIAN) |
| 85 | + #if defined(__GNUC__) || defined(__clang__) |
| 86 | + #ifdef __BIG_ENDIAN__ |
| 87 | + #define FLATBUFFERS_LITTLEENDIAN 0 |
| 88 | + #else |
| 89 | + #define FLATBUFFERS_LITTLEENDIAN 1 |
| 90 | + #endif // __BIG_ENDIAN__ |
| 91 | + #elif defined(_MSC_VER) |
| 92 | + #if defined(_M_PPC) |
| 93 | + #define FLATBUFFERS_LITTLEENDIAN 0 |
| 94 | + #else |
| 95 | + #define FLATBUFFERS_LITTLEENDIAN 1 |
| 96 | + #endif |
| 97 | + #else |
| 98 | + #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN. |
| 99 | + #endif |
| 100 | +#endif // !defined(FLATBUFFERS_LITTLEENDIAN) |
| 101 | + |
| 102 | +#define FLATBUFFERS_VERSION_MAJOR 1 |
| 103 | +#define FLATBUFFERS_VERSION_MINOR 9 |
| 104 | +#define FLATBUFFERS_VERSION_REVISION 0 |
| 105 | +#define FLATBUFFERS_STRING_EXPAND(X) #X |
| 106 | +#define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X) |
| 107 | + |
| 108 | +#if (!defined(_MSC_VER) || _MSC_VER > 1600) && \ |
| 109 | + (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407)) |
| 110 | + #define FLATBUFFERS_FINAL_CLASS final |
| 111 | + #define FLATBUFFERS_OVERRIDE override |
| 112 | +#else |
| 113 | + #define FLATBUFFERS_FINAL_CLASS |
| 114 | + #define FLATBUFFERS_OVERRIDE |
| 115 | +#endif |
| 116 | + |
| 117 | +#if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \ |
| 118 | + (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) |
| 119 | + #define FLATBUFFERS_CONSTEXPR constexpr |
| 120 | +#else |
| 121 | + #define FLATBUFFERS_CONSTEXPR |
| 122 | +#endif |
| 123 | + |
| 124 | +#if (defined(__cplusplus) && __cplusplus >= 201402L) || \ |
| 125 | + (defined(__cpp_constexpr) && __cpp_constexpr >= 201304) |
| 126 | + #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR |
| 127 | +#else |
| 128 | + #define FLATBUFFERS_CONSTEXPR_CPP14 |
| 129 | +#endif |
| 130 | + |
| 131 | +#if defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC__ * 10 + __GNUC_MINOR__ >= 46 || \ |
| 132 | + defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023026 |
| 133 | + #define FLATBUFFERS_NOEXCEPT noexcept |
| 134 | +#else |
| 135 | + #define FLATBUFFERS_NOEXCEPT |
| 136 | +#endif |
| 137 | + |
| 138 | +// NOTE: the FLATBUFFERS_DELETE_FUNC macro may change the access mode to |
| 139 | +// private, so be sure to put it at the end or reset access mode explicitly. |
| 140 | +#if (!defined(_MSC_VER) || _MSC_FULL_VER >= 180020827) && \ |
| 141 | + (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 404)) |
| 142 | + #define FLATBUFFERS_DELETE_FUNC(func) func = delete; |
| 143 | +#else |
| 144 | + #define FLATBUFFERS_DELETE_FUNC(func) private: func; |
| 145 | +#endif |
| 146 | + |
| 147 | +#if defined(_MSC_VER) |
| 148 | + #pragma warning(push) |
| 149 | + #pragma warning(disable: 4127) // C4127: conditional expression is constant |
| 150 | +#endif |
| 151 | + |
| 152 | +/// @endcond |
| 153 | + |
| 154 | +/// @file |
| 155 | +namespace flatbuffers { |
| 156 | + |
| 157 | +/// @cond FLATBUFFERS_INTERNAL |
| 158 | +// Our default offset / size type, 32bit on purpose on 64bit systems. |
| 159 | +// Also, using a consistent offset type maintains compatibility of serialized |
| 160 | +// offset values between 32bit and 64bit systems. |
| 161 | +typedef uint32_t uoffset_t; |
| 162 | + |
| 163 | +// Signed offsets for references that can go in both directions. |
| 164 | +typedef int32_t soffset_t; |
| 165 | + |
| 166 | +// Offset/index used in v-tables, can be changed to uint8_t in |
| 167 | +// format forks to save a bit of space if desired. |
| 168 | +typedef uint16_t voffset_t; |
| 169 | + |
| 170 | +typedef uintmax_t largest_scalar_t; |
| 171 | + |
| 172 | +// In 32bits, this evaluates to 2GB - 1 |
| 173 | +#define FLATBUFFERS_MAX_BUFFER_SIZE ((1ULL << (sizeof(soffset_t) * 8 - 1)) - 1) |
| 174 | + |
| 175 | +// We support aligning the contents of buffers up to this size. |
| 176 | +#define FLATBUFFERS_MAX_ALIGNMENT 16 |
| 177 | + |
| 178 | +template<typename T> T EndianSwap(T t) { |
| 179 | + #if defined(_MSC_VER) |
| 180 | + #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort |
| 181 | + #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong |
| 182 | + #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64 |
| 183 | + #else |
| 184 | + #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__) |
| 185 | + // __builtin_bswap16 was missing prior to GCC 4.8. |
| 186 | + #define FLATBUFFERS_BYTESWAP16(x) \ |
| 187 | + static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16)) |
| 188 | + #else |
| 189 | + #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16 |
| 190 | + #endif |
| 191 | + #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32 |
| 192 | + #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64 |
| 193 | + #endif |
| 194 | + if (sizeof(T) == 1) { // Compile-time if-then's. |
| 195 | + return t; |
| 196 | + } else if (sizeof(T) == 2) { |
| 197 | + union { T t; uint16_t i; } u; |
| 198 | + u.t = t; |
| 199 | + u.i = FLATBUFFERS_BYTESWAP16(u.i); |
| 200 | + return u.t; |
| 201 | + } else if (sizeof(T) == 4) { |
| 202 | + union { T t; uint32_t i; } u; |
| 203 | + u.t = t; |
| 204 | + u.i = FLATBUFFERS_BYTESWAP32(u.i); |
| 205 | + return u.t; |
| 206 | + } else if (sizeof(T) == 8) { |
| 207 | + union { T t; uint64_t i; } u; |
| 208 | + u.t = t; |
| 209 | + u.i = FLATBUFFERS_BYTESWAP64(u.i); |
| 210 | + return u.t; |
| 211 | + } else { |
| 212 | + assert(0); |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | + |
| 217 | +template<typename T> T EndianScalar(T t) { |
| 218 | + #if FLATBUFFERS_LITTLEENDIAN |
| 219 | + return t; |
| 220 | + #else |
| 221 | + return EndianSwap(t); |
| 222 | + #endif |
| 223 | +} |
| 224 | + |
| 225 | +template<typename T> T ReadScalar(const void *p) { |
| 226 | + return EndianScalar(*reinterpret_cast<const T *>(p)); |
| 227 | +} |
| 228 | + |
| 229 | +template<typename T> void WriteScalar(void *p, T t) { |
| 230 | + *reinterpret_cast<T *>(p) = EndianScalar(t); |
| 231 | +} |
| 232 | + |
| 233 | +// Computes how many bytes you'd have to pad to be able to write an |
| 234 | +// "scalar_size" scalar if the buffer had grown to "buf_size" (downwards in |
| 235 | +// memory). |
| 236 | +inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) { |
| 237 | + return ((~buf_size) + 1) & (scalar_size - 1); |
| 238 | +} |
| 239 | + |
| 240 | +} // namespace flatbuffers |
| 241 | +#endif // FLATBUFFERS_BASE_H_ |
0 commit comments