Go to the documentation of this file.00001 #ifndef FLATBUFFERS_BASE_H_
00002 #define FLATBUFFERS_BASE_H_
00003
00004
00005 #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
00006 defined(_MSC_VER) && defined(_DEBUG)
00007 #define _CRTDBG_MAP_ALLOC
00008 #endif
00009
00010 #include <assert.h>
00011
00012 #if !defined(FLATBUFFERS_ASSERT)
00013 #define FLATBUFFERS_ASSERT assert
00014 #endif
00015
00016 #ifndef ARDUINO
00017 #include <cstdint>
00018 #endif
00019
00020 #include <cstddef>
00021 #include <cstdlib>
00022 #include <cstring>
00023
00024 #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
00025 defined(_MSC_VER) && defined(_DEBUG)
00026 #include <crtdbg.h>
00027 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
00028 #define new DEBUG_NEW
00029 #endif
00030
00031 #if defined(ARDUINO) && !defined(ARDUINOSTL_M_H)
00032 #include <utility.h>
00033 #else
00034 #include <utility>
00035 #endif
00036
00037 #include <string>
00038 #include <type_traits>
00039 #include <vector>
00040 #include <set>
00041 #include <algorithm>
00042 #include <iterator>
00043 #include <memory>
00044
00045 #ifdef _STLPORT_VERSION
00046 #define FLATBUFFERS_CPP98_STL
00047 #endif
00048 #ifndef FLATBUFFERS_CPP98_STL
00049 #include <functional>
00050 #endif
00051
00052 #include "flatbuffers/stl_emulation.h"
00053
00055 #if __cplusplus <= 199711L && \
00056 (!defined(_MSC_VER) || _MSC_VER < 1600) && \
00057 (!defined(__GNUC__) || \
00058 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40400))
00059 #error A C++11 compatible compiler with support for the auto typing is \
00060 required for FlatBuffers.
00061 #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
00062 #endif
00063
00064 #if !defined(__clang__) && \
00065 defined(__GNUC__) && \
00066 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600)
00067
00068
00069
00070 #ifndef nullptr_t
00071 const class nullptr_t {
00072 public:
00073 template<class T> inline operator T*() const { return 0; }
00074 private:
00075 void operator&() const;
00076 } nullptr = {};
00077 #endif
00078 #ifndef constexpr
00079 #define constexpr const
00080 #endif
00081 #endif
00082
00083
00084
00085 #if defined(__s390x__)
00086 #define FLATBUFFERS_LITTLEENDIAN 0
00087 #endif // __s390x__
00088 #if !defined(FLATBUFFERS_LITTLEENDIAN)
00089 #if defined(__GNUC__) || defined(__clang__)
00090 #ifdef __BIG_ENDIAN__
00091 #define FLATBUFFERS_LITTLEENDIAN 0
00092 #else
00093 #define FLATBUFFERS_LITTLEENDIAN 1
00094 #endif // __BIG_ENDIAN__
00095 #elif defined(_MSC_VER)
00096 #if defined(_M_PPC)
00097 #define FLATBUFFERS_LITTLEENDIAN 0
00098 #else
00099 #define FLATBUFFERS_LITTLEENDIAN 1
00100 #endif
00101 #else
00102 #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN.
00103 #endif
00104 #endif // !defined(FLATBUFFERS_LITTLEENDIAN)
00105
00106 #define FLATBUFFERS_VERSION_MAJOR 1
00107 #define FLATBUFFERS_VERSION_MINOR 9
00108 #define FLATBUFFERS_VERSION_REVISION 0
00109 #define FLATBUFFERS_STRING_EXPAND(X) #X
00110 #define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
00111
00112 #if (!defined(_MSC_VER) || _MSC_VER > 1600) && \
00113 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407))
00114 #define FLATBUFFERS_FINAL_CLASS final
00115 #define FLATBUFFERS_OVERRIDE override
00116 #else
00117 #define FLATBUFFERS_FINAL_CLASS
00118 #define FLATBUFFERS_OVERRIDE
00119 #endif
00120
00121 #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
00122 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406))
00123 #define FLATBUFFERS_CONSTEXPR constexpr
00124 #else
00125 #define FLATBUFFERS_CONSTEXPR
00126 #endif
00127
00128 #if (defined(__cplusplus) && __cplusplus >= 201402L) || \
00129 (defined(__cpp_constexpr) && __cpp_constexpr >= 201304)
00130 #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR
00131 #else
00132 #define FLATBUFFERS_CONSTEXPR_CPP14
00133 #endif
00134
00135 #if defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC__ * 10 + __GNUC_MINOR__ >= 46 || \
00136 defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023026
00137 #define FLATBUFFERS_NOEXCEPT noexcept
00138 #else
00139 #define FLATBUFFERS_NOEXCEPT
00140 #endif
00141
00142
00143
00144 #if (!defined(_MSC_VER) || _MSC_FULL_VER >= 180020827) && \
00145 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 404))
00146 #define FLATBUFFERS_DELETE_FUNC(func) func = delete;
00147 #else
00148 #define FLATBUFFERS_DELETE_FUNC(func) private: func;
00149 #endif
00150
00151 #if defined(_MSC_VER)
00152 #pragma warning(push)
00153 #pragma warning(disable: 4127) // C4127: conditional expression is constant
00154 #endif
00155
00156 #ifndef FLATBUFFERS_HAS_STRING_VIEW
00157
00158
00159 #if defined(__has_include)
00160
00161 #if __has_include(<string_view>) && (__cplusplus > 201402)
00162 #include <string_view>
00163 namespace flatbuffers {
00164 typedef std::string_view string_view;
00165 }
00166 #define FLATBUFFERS_HAS_STRING_VIEW 1
00167
00168 #elif __has_include(<experimental/string_view>) && (__cplusplus > 201103)
00169 #include <experimental/string_view>
00170 namespace flatbuffers {
00171 typedef std::experimental::string_view string_view;
00172 }
00173 #define FLATBUFFERS_HAS_STRING_VIEW 1
00174 #endif
00175 #endif // __has_include
00176 #endif // !FLATBUFFERS_HAS_STRING_VIEW
00177
00179
00181 namespace flatbuffers {
00182
00184
00185
00186
00187 typedef uint32_t uoffset_t;
00188
00189
00190 typedef int32_t soffset_t;
00191
00192
00193
00194 typedef uint16_t voffset_t;
00195
00196 typedef uintmax_t largest_scalar_t;
00197
00198
00199 #define FLATBUFFERS_MAX_BUFFER_SIZE ((1ULL << (sizeof(soffset_t) * 8 - 1)) - 1)
00200
00201
00202 #define FLATBUFFERS_MAX_ALIGNMENT 16
00203
00204 template<typename T> T EndianSwap(T t) {
00205 #if defined(_MSC_VER)
00206 #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort
00207 #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong
00208 #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64
00209 #else
00210 #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__)
00211
00212 #define FLATBUFFERS_BYTESWAP16(x) \
00213 static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16))
00214 #else
00215 #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16
00216 #endif
00217 #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32
00218 #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64
00219 #endif
00220 if (sizeof(T) == 1) {
00221 return t;
00222 } else if (sizeof(T) == 2) {
00223 union { T t; uint16_t i; } u;
00224 u.t = t;
00225 u.i = FLATBUFFERS_BYTESWAP16(u.i);
00226 return u.t;
00227 } else if (sizeof(T) == 4) {
00228 union { T t; uint32_t i; } u;
00229 u.t = t;
00230 u.i = FLATBUFFERS_BYTESWAP32(u.i);
00231 return u.t;
00232 } else if (sizeof(T) == 8) {
00233 union { T t; uint64_t i; } u;
00234 u.t = t;
00235 u.i = FLATBUFFERS_BYTESWAP64(u.i);
00236 return u.t;
00237 } else {
00238 FLATBUFFERS_ASSERT(0);
00239 }
00240 }
00241
00242
00243 template<typename T> T EndianScalar(T t) {
00244 #if FLATBUFFERS_LITTLEENDIAN
00245 return t;
00246 #else
00247 return EndianSwap(t);
00248 #endif
00249 }
00250
00251 template<typename T> T ReadScalar(const void *p) {
00252 return EndianScalar(*reinterpret_cast<const T *>(p));
00253 }
00254
00255 template<typename T> void WriteScalar(void *p, T t) {
00256 *reinterpret_cast<T *>(p) = EndianScalar(t);
00257 }
00258
00259
00260
00261
00262 inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) {
00263 return ((~buf_size) + 1) & (scalar_size - 1);
00264 }
00265
00266 }
00267 #endif // FLATBUFFERS_BASE_H_