12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808 |
- #ifndef FLATBUFFERS_H_
- #define FLATBUFFERS_H_
- #include <assert.h>
- #include <cstdint>
- #include <cstddef>
- #include <cstdlib>
- #include <cstring>
- #include <string>
- #include <utility>
- #include <type_traits>
- #include <vector>
- #include <set>
- #include <algorithm>
- #include <memory>
- #ifdef _STLPORT_VERSION
- #define FLATBUFFERS_CPP98_STL
- #endif
- #ifndef FLATBUFFERS_CPP98_STL
- #include <functional>
- #endif
- #if __cplusplus <= 199711L && \
- (!defined(_MSC_VER) || _MSC_VER < 1600) && \
- (!defined(__GNUC__) || \
- (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40400))
- #error A C++11 compatible compiler with support for the auto typing is \
- required for FlatBuffers.
- #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
- #endif
- #if !defined(__clang__) && \
- defined(__GNUC__) && \
- (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600)
-
-
-
- #ifndef nullptr_t
- const class nullptr_t {
- public:
- template<class T> inline operator T*() const { return 0; }
- private:
- void operator&() const;
- } nullptr = {};
- #endif
- #ifndef constexpr
- #define constexpr const
- #endif
- #endif
- #if !defined(FLATBUFFERS_LITTLEENDIAN)
- #if defined(__GNUC__) || defined(__clang__)
- #ifdef __BIG_ENDIAN__
- #define FLATBUFFERS_LITTLEENDIAN 0
- #else
- #define FLATBUFFERS_LITTLEENDIAN 1
- #endif
- #elif defined(_MSC_VER)
- #if defined(_M_PPC)
- #define FLATBUFFERS_LITTLEENDIAN 0
- #else
- #define FLATBUFFERS_LITTLEENDIAN 1
- #endif
- #else
- #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN.
- #endif
- #endif
- #define FLATBUFFERS_VERSION_MAJOR 1
- #define FLATBUFFERS_VERSION_MINOR 5
- #define FLATBUFFERS_VERSION_REVISION 0
- #define FLATBUFFERS_STRING_EXPAND(X) #X
- #define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
- #if (!defined(_MSC_VER) || _MSC_VER > 1600) && \
- (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407))
- #define FLATBUFFERS_FINAL_CLASS final
- #else
- #define FLATBUFFERS_FINAL_CLASS
- #endif
- #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
- (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406))
- #define FLATBUFFERS_CONSTEXPR constexpr
- #else
- #define FLATBUFFERS_CONSTEXPR
- #endif
- namespace flatbuffers {
- typedef uint32_t uoffset_t;
- typedef int32_t soffset_t;
- typedef uint16_t voffset_t;
- typedef uintmax_t largest_scalar_t;
- #define FLATBUFFERS_MAX_BUFFER_SIZE ((1ULL << (sizeof(soffset_t) * 8 - 1)) - 1)
- #define FLATBUFFERS_MAX_ALIGNMENT 16
- #ifndef FLATBUFFERS_CPP98_STL
- typedef std::unique_ptr<uint8_t, std::function<void(uint8_t * )>>
- unique_ptr_t;
- #endif
- template<typename T> struct Offset {
- uoffset_t o;
- Offset() : o(0) {}
- Offset(uoffset_t _o) : o(_o) {}
- Offset<void> Union() const { return Offset<void>(o); }
- };
- inline void EndianCheck() {
- int endiantest = 1;
-
- assert(*reinterpret_cast<char *>(&endiantest) == FLATBUFFERS_LITTLEENDIAN);
- (void)endiantest;
- }
- template<typename T> T EndianSwap(T t) {
- #if defined(_MSC_VER)
- #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort
- #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong
- #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64
- #else
- #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408
-
- #define FLATBUFFERS_BYTESWAP16(x) \
- static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16))
- #else
- #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16
- #endif
- #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32
- #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64
- #endif
- if (sizeof(T) == 1) {
- return t;
- } else if (sizeof(T) == 2) {
- auto r = FLATBUFFERS_BYTESWAP16(*reinterpret_cast<uint16_t *>(&t));
- return *reinterpret_cast<T *>(&r);
- } else if (sizeof(T) == 4) {
- auto r = FLATBUFFERS_BYTESWAP32(*reinterpret_cast<uint32_t *>(&t));
- return *reinterpret_cast<T *>(&r);
- } else if (sizeof(T) == 8) {
- auto r = FLATBUFFERS_BYTESWAP64(*reinterpret_cast<uint64_t *>(&t));
- return *reinterpret_cast<T *>(&r);
- } else {
- assert(0);
- }
- }
- template<typename T> T EndianScalar(T t) {
- #if FLATBUFFERS_LITTLEENDIAN
- return t;
- #else
- return EndianSwap(t);
- #endif
- }
- template<typename T> T ReadScalar(const void *p) {
- return EndianScalar(*reinterpret_cast<const T *>(p));
- }
- template<typename T> void WriteScalar(void *p, T t) {
- *reinterpret_cast<T *>(p) = EndianScalar(t);
- }
- template<typename T> size_t AlignOf() {
- #ifdef _MSC_VER
- return __alignof(T);
- #else
- #ifndef alignof
- return __alignof__(T);
- #else
- return alignof(T);
- #endif
- #endif
- }
- template<typename T> struct IndirectHelper {
- typedef T return_type;
- typedef T mutable_return_type;
- static const size_t element_stride = sizeof(T);
- static return_type Read(const uint8_t *p, uoffset_t i) {
- return EndianScalar((reinterpret_cast<const T *>(p))[i]);
- }
- };
- template<typename T> struct IndirectHelper<Offset<T>> {
- typedef const T *return_type;
- typedef T *mutable_return_type;
- static const size_t element_stride = sizeof(uoffset_t);
- static return_type Read(const uint8_t *p, uoffset_t i) {
- p += i * sizeof(uoffset_t);
- return reinterpret_cast<return_type>(p + ReadScalar<uoffset_t>(p));
- }
- };
- template<typename T> struct IndirectHelper<const T *> {
- typedef const T *return_type;
- typedef T *mutable_return_type;
- static const size_t element_stride = sizeof(T);
- static return_type Read(const uint8_t *p, uoffset_t i) {
- return reinterpret_cast<const T *>(p + i * sizeof(T));
- }
- };
- template<typename T, typename IT>
- struct VectorIterator
- : public std::iterator<std::random_access_iterator_tag, IT, uoffset_t> {
- typedef std::iterator<std::random_access_iterator_tag, IT, uoffset_t> super_type;
- public:
- VectorIterator(const uint8_t *data, uoffset_t i) :
- data_(data + IndirectHelper<T>::element_stride * i) {}
- VectorIterator(const VectorIterator &other) : data_(other.data_) {}
- #ifndef FLATBUFFERS_CPP98_STL
- VectorIterator(VectorIterator &&other) : data_(std::move(other.data_)) {}
- #endif
- VectorIterator &operator=(const VectorIterator &other) {
- data_ = other.data_;
- return *this;
- }
- VectorIterator &operator=(VectorIterator &&other) {
- data_ = other.data_;
- return *this;
- }
- bool operator==(const VectorIterator &other) const {
- return data_ == other.data_;
- }
- bool operator!=(const VectorIterator &other) const {
- return data_ != other.data_;
- }
- ptrdiff_t operator-(const VectorIterator &other) const {
- return (data_ - other.data_) / IndirectHelper<T>::element_stride;
- }
- typename super_type::value_type operator *() const {
- return IndirectHelper<T>::Read(data_, 0);
- }
- typename super_type::value_type operator->() const {
- return IndirectHelper<T>::Read(data_, 0);
- }
- VectorIterator &operator++() {
- data_ += IndirectHelper<T>::element_stride;
- return *this;
- }
- VectorIterator operator++(int) {
- VectorIterator temp(data_, 0);
- data_ += IndirectHelper<T>::element_stride;
- return temp;
- }
- VectorIterator operator+(const uoffset_t &offset) {
- return VectorIterator(data_ + offset * IndirectHelper<T>::element_stride, 0);
- }
- VectorIterator& operator+=(const uoffset_t &offset) {
- data_ += offset * IndirectHelper<T>::element_stride;
- return *this;
- }
- VectorIterator &operator--() {
- data_ -= IndirectHelper<T>::element_stride;
- return *this;
- }
- VectorIterator operator--(int) {
- VectorIterator temp(data_, 0);
- data_ -= IndirectHelper<T>::element_stride;
- return temp;
- }
- VectorIterator operator-(const uoffset_t &offset) {
- return VectorIterator(data_ - offset * IndirectHelper<T>::element_stride, 0);
- }
- VectorIterator& operator-=(const uoffset_t &offset) {
- data_ -= offset * IndirectHelper<T>::element_stride;
- return *this;
- }
- private:
- const uint8_t *data_;
- };
- template<typename T> class Vector {
- public:
- typedef VectorIterator<T, typename IndirectHelper<T>::mutable_return_type>
- iterator;
- typedef VectorIterator<T, typename IndirectHelper<T>::return_type>
- const_iterator;
- uoffset_t size() const { return EndianScalar(length_); }
-
- uoffset_t Length() const { return size(); }
- typedef typename IndirectHelper<T>::return_type return_type;
- typedef typename IndirectHelper<T>::mutable_return_type mutable_return_type;
- return_type Get(uoffset_t i) const {
- assert(i < size());
- return IndirectHelper<T>::Read(Data(), i);
- }
- return_type operator[](uoffset_t i) const { return Get(i); }
-
-
-
- template<typename E> E GetEnum(uoffset_t i) const {
- return static_cast<E>(Get(i));
- }
- const void *GetStructFromOffset(size_t o) const {
- return reinterpret_cast<const void *>(Data() + o);
- }
- iterator begin() { return iterator(Data(), 0); }
- const_iterator begin() const { return const_iterator(Data(), 0); }
- iterator end() { return iterator(Data(), size()); }
- const_iterator end() const { return const_iterator(Data(), size()); }
-
-
- void Mutate(uoffset_t i, const T& val) {
- assert(i < size());
- WriteScalar(data() + i, val);
- }
-
-
-
- void MutateOffset(uoffset_t i, const uint8_t *val) {
- assert(i < size());
- assert(sizeof(T) == sizeof(uoffset_t));
- WriteScalar(data() + i,
- static_cast<uoffset_t>(val - (Data() + i * sizeof(uoffset_t))));
- }
-
- mutable_return_type GetMutableObject(uoffset_t i) const {
- assert(i < size());
- return const_cast<mutable_return_type>(IndirectHelper<T>::Read(Data(), i));
- }
-
- const uint8_t *Data() const {
- return reinterpret_cast<const uint8_t *>(&length_ + 1);
- }
- uint8_t *Data() {
- return reinterpret_cast<uint8_t *>(&length_ + 1);
- }
-
- const T *data() const { return reinterpret_cast<const T *>(Data()); }
- T *data() { return reinterpret_cast<T *>(Data()); }
- template<typename K> return_type LookupByKey(K key) const {
- void *search_result = std::bsearch(&key, Data(), size(),
- IndirectHelper<T>::element_stride, KeyCompare<K>);
- if (!search_result) {
- return nullptr;
- }
- const uint8_t *element = reinterpret_cast<const uint8_t *>(search_result);
- return IndirectHelper<T>::Read(element, 0);
- }
- protected:
-
-
- Vector();
- uoffset_t length_;
- private:
- template<typename K> static int KeyCompare(const void *ap, const void *bp) {
- const K *key = reinterpret_cast<const K *>(ap);
- const uint8_t *data = reinterpret_cast<const uint8_t *>(bp);
- auto table = IndirectHelper<T>::Read(data, 0);
-
-
- return -table->KeyCompareWithValue(*key);
- }
- };
- class VectorOfAny {
- public:
- uoffset_t size() const { return EndianScalar(length_); }
- const uint8_t *Data() const {
- return reinterpret_cast<const uint8_t *>(&length_ + 1);
- }
- uint8_t *Data() {
- return reinterpret_cast<uint8_t *>(&length_ + 1);
- }
- protected:
- VectorOfAny();
- uoffset_t length_;
- };
- template<typename T> static inline size_t VectorLength(const Vector<T> *v) {
- return v ? v->Length() : 0;
- }
- struct String : public Vector<char> {
- const char *c_str() const { return reinterpret_cast<const char *>(Data()); }
- std::string str() const { return std::string(c_str(), Length()); }
- bool operator <(const String &o) const {
- return strcmp(c_str(), o.c_str()) < 0;
- }
- };
- class simple_allocator {
- public:
- virtual ~simple_allocator() {}
- virtual uint8_t *allocate(size_t size) const { return new uint8_t[size]; }
- virtual void deallocate(uint8_t *p) const { delete[] p; }
- };
- class vector_downward {
- public:
- explicit vector_downward(size_t initial_size,
- const simple_allocator &allocator)
- : reserved_(initial_size),
- buf_(allocator.allocate(reserved_)),
- cur_(buf_ + reserved_),
- allocator_(allocator) {
- assert((initial_size & (sizeof(largest_scalar_t) - 1)) == 0);
- }
- ~vector_downward() {
- if (buf_)
- allocator_.deallocate(buf_);
- }
- void clear() {
- if (buf_ == nullptr)
- buf_ = allocator_.allocate(reserved_);
- cur_ = buf_ + reserved_;
- }
- #ifndef FLATBUFFERS_CPP98_STL
-
- unique_ptr_t release() {
-
- std::function<void(uint8_t *)> deleter(
- std::bind(&simple_allocator::deallocate, allocator_, buf_));
-
- unique_ptr_t retval(data(), deleter);
-
- buf_ = nullptr;
- cur_ = nullptr;
- return retval;
- }
- #endif
- size_t growth_policy(size_t bytes) {
- return (bytes / 2) & ~(sizeof(largest_scalar_t) - 1);
- }
- uint8_t *make_space(size_t len) {
- if (len > static_cast<size_t>(cur_ - buf_)) {
- auto old_size = size();
- auto largest_align = AlignOf<largest_scalar_t>();
- reserved_ += (std::max)(len, growth_policy(reserved_));
-
- reserved_ = (reserved_ + (largest_align - 1)) & ~(largest_align - 1);
- auto new_buf = allocator_.allocate(reserved_);
- auto new_cur = new_buf + reserved_ - old_size;
- memcpy(new_cur, cur_, old_size);
- cur_ = new_cur;
- allocator_.deallocate(buf_);
- buf_ = new_buf;
- }
- cur_ -= len;
-
-
- assert(size() < FLATBUFFERS_MAX_BUFFER_SIZE);
- return cur_;
- }
- uoffset_t size() const {
- assert(cur_ != nullptr && buf_ != nullptr);
- return static_cast<uoffset_t>(reserved_ - (cur_ - buf_));
- }
- uint8_t *data() const {
- assert(cur_ != nullptr);
- return cur_;
- }
- uint8_t *data_at(size_t offset) const { return buf_ + reserved_ - offset; }
-
-
- void push(const uint8_t *bytes, size_t num) {
- auto dest = make_space(num);
- for (size_t i = 0; i < num; i++) dest[i] = bytes[i];
- }
- void fill(size_t zero_pad_bytes) {
- auto dest = make_space(zero_pad_bytes);
- for (size_t i = 0; i < zero_pad_bytes; i++) dest[i] = 0;
- }
- void pop(size_t bytes_to_remove) { cur_ += bytes_to_remove; }
- private:
-
- vector_downward(const vector_downward &);
- vector_downward &operator=(const vector_downward &);
- size_t reserved_;
- uint8_t *buf_;
- uint8_t *cur_;
- const simple_allocator &allocator_;
- };
- inline voffset_t FieldIndexToOffset(voffset_t field_id) {
-
- const int fixed_fields = 2;
- return static_cast<voffset_t>((field_id + fixed_fields) * sizeof(voffset_t));
- }
- inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) {
- return ((~buf_size) + 1) & (scalar_size - 1);
- }
- template <typename T> const T* data(const std::vector<T> &v) {
- return v.empty() ? nullptr : &v.front();
- }
- template <typename T> T* data(std::vector<T> &v) {
- return v.empty() ? nullptr : &v.front();
- }
- class FlatBufferBuilder
- FLATBUFFERS_FINAL_CLASS
- {
- public:
-
-
-
-
-
-
- explicit FlatBufferBuilder(uoffset_t initial_size = 1024,
- const simple_allocator *allocator = nullptr)
- : buf_(initial_size, allocator ? *allocator : default_allocator),
- nested(false), finished(false), minalign_(1), force_defaults_(false),
- string_pool(nullptr) {
- offsetbuf_.reserve(16);
- vtables_.reserve(16);
- EndianCheck();
- }
- ~FlatBufferBuilder() {
- if (string_pool) delete string_pool;
- }
-
-
- void Clear() {
- buf_.clear();
- offsetbuf_.clear();
- nested = false;
- finished = false;
- vtables_.clear();
- minalign_ = 1;
- if (string_pool) string_pool->clear();
- }
-
-
- uoffset_t GetSize() const { return buf_.size(); }
-
-
-
- uint8_t *GetBufferPointer() const {
- Finished();
- return buf_.data();
- }
-
-
- uint8_t *GetCurrentBufferPointer() const { return buf_.data(); }
- #ifndef FLATBUFFERS_CPP98_STL
-
-
-
-
-
-
- unique_ptr_t ReleaseBufferPointer() {
- Finished();
- return buf_.release();
- }
- #endif
-
-
-
-
-
- size_t GetBufferMinAlignment() {
- Finished();
- return minalign_;
- }
-
- void Finished() const {
-
-
-
-
-
- assert(finished);
- }
-
-
-
-
- void ForceDefaults(bool fd) { force_defaults_ = fd; }
-
- void Pad(size_t num_bytes) { buf_.fill(num_bytes); }
- void Align(size_t elem_size) {
- if (elem_size > minalign_) minalign_ = elem_size;
- buf_.fill(PaddingBytes(buf_.size(), elem_size));
- }
- void PushFlatBuffer(const uint8_t *bytes, size_t size) {
- PushBytes(bytes, size);
- finished = true;
- }
- void PushBytes(const uint8_t *bytes, size_t size) {
- buf_.push(bytes, size);
- }
- void PopBytes(size_t amount) { buf_.pop(amount); }
- template<typename T> void AssertScalarT() {
- #ifndef FLATBUFFERS_CPP98_STL
-
- static_assert(std::is_scalar<T>::value
-
- || sizeof(T) == sizeof(Offset<void>),
- "T must be a scalar type");
- #endif
- }
-
- template<typename T> uoffset_t PushElement(T element) {
- AssertScalarT<T>();
- T litle_endian_element = EndianScalar(element);
- Align(sizeof(T));
- PushBytes(reinterpret_cast<uint8_t *>(&litle_endian_element), sizeof(T));
- return GetSize();
- }
- template<typename T> uoffset_t PushElement(Offset<T> off) {
-
- return PushElement(ReferTo(off.o));
- }
-
-
- void TrackField(voffset_t field, uoffset_t off) {
- FieldLoc fl = { off, field };
- offsetbuf_.push_back(fl);
- }
-
- template<typename T> void AddElement(voffset_t field, T e, T def) {
-
- if (e == def && !force_defaults_) return;
- auto off = PushElement(e);
- TrackField(field, off);
- }
- template<typename T> void AddOffset(voffset_t field, Offset<T> off) {
- if (!off.o) return;
- AddElement(field, ReferTo(off.o), static_cast<uoffset_t>(0));
- }
- template<typename T> void AddStruct(voffset_t field, const T *structptr) {
- if (!structptr) return;
- Align(AlignOf<T>());
- PushBytes(reinterpret_cast<const uint8_t *>(structptr), sizeof(T));
- TrackField(field, GetSize());
- }
- void AddStructOffset(voffset_t field, uoffset_t off) {
- TrackField(field, off);
- }
-
-
-
- uoffset_t ReferTo(uoffset_t off) {
-
- Align(sizeof(uoffset_t));
-
- assert(off && off <= GetSize());
- return GetSize() - off + static_cast<uoffset_t>(sizeof(uoffset_t));
- }
- void NotNested() {
-
-
-
-
-
-
-
-
- assert(!nested);
- }
-
-
- uoffset_t StartTable() {
- NotNested();
- nested = true;
- return GetSize();
- }
-
-
-
- uoffset_t EndTable(uoffset_t start, voffset_t numfields) {
-
- assert(nested);
-
-
- auto vtableoffsetloc = PushElement<soffset_t>(0);
-
-
-
- buf_.fill(numfields * sizeof(voffset_t));
- auto table_object_size = vtableoffsetloc - start;
- assert(table_object_size < 0x10000);
- PushElement<voffset_t>(static_cast<voffset_t>(table_object_size));
- PushElement<voffset_t>(FieldIndexToOffset(numfields));
-
- for (auto field_location = offsetbuf_.begin();
- field_location != offsetbuf_.end();
- ++field_location) {
- auto pos = static_cast<voffset_t>(vtableoffsetloc - field_location->off);
-
- assert(!ReadScalar<voffset_t>(buf_.data() + field_location->id));
- WriteScalar<voffset_t>(buf_.data() + field_location->id, pos);
- }
- offsetbuf_.clear();
- auto vt1 = reinterpret_cast<voffset_t *>(buf_.data());
- auto vt1_size = ReadScalar<voffset_t>(vt1);
- auto vt_use = GetSize();
-
-
- for (auto it = vtables_.begin(); it != vtables_.end(); ++it) {
- auto vt2 = reinterpret_cast<voffset_t *>(buf_.data_at(*it));
- auto vt2_size = *vt2;
- if (vt1_size != vt2_size || memcmp(vt2, vt1, vt1_size)) continue;
- vt_use = *it;
- buf_.pop(GetSize() - vtableoffsetloc);
- break;
- }
-
- if (vt_use == GetSize()) {
- vtables_.push_back(vt_use);
- }
-
-
-
-
-
- WriteScalar(buf_.data_at(vtableoffsetloc),
- static_cast<soffset_t>(vt_use) -
- static_cast<soffset_t>(vtableoffsetloc));
- nested = false;
- return vtableoffsetloc;
- }
-
-
- template<typename T> void Required(Offset<T> table, voffset_t field) {
- auto table_ptr = buf_.data_at(table.o);
- auto vtable_ptr = table_ptr - ReadScalar<soffset_t>(table_ptr);
- bool ok = ReadScalar<voffset_t>(vtable_ptr + field) != 0;
-
- assert(ok);
- (void)ok;
- }
- uoffset_t StartStruct(size_t alignment) {
- Align(alignment);
- return GetSize();
- }
- uoffset_t EndStruct() { return GetSize(); }
- void ClearOffsets() { offsetbuf_.clear(); }
-
-
- void PreAlign(size_t len, size_t alignment) {
- buf_.fill(PaddingBytes(GetSize() + len, alignment));
- }
- template<typename T> void PreAlign(size_t len) {
- AssertScalarT<T>();
- PreAlign(len, sizeof(T));
- }
-
-
-
-
-
- Offset<String> CreateString(const char *str, size_t len) {
- NotNested();
- PreAlign<uoffset_t>(len + 1);
- buf_.fill(1);
- PushBytes(reinterpret_cast<const uint8_t *>(str), len);
- PushElement(static_cast<uoffset_t>(len));
- return Offset<String>(GetSize());
- }
-
-
-
- Offset<String> CreateString(const char *str) {
- return CreateString(str, strlen(str));
- }
-
-
-
- Offset<String> CreateString(const std::string &str) {
- return CreateString(str.c_str(), str.length());
- }
-
-
-
- Offset<String> CreateString(const String *str) {
- return str ? CreateString(str->c_str(), str->Length()) : 0;
- }
-
-
-
-
-
-
- Offset<String> CreateSharedString(const char *str, size_t len) {
- if (!string_pool)
- string_pool = new StringOffsetMap(StringOffsetCompare(buf_));
- auto size_before_string = buf_.size();
-
-
- auto off = CreateString(str, len);
- auto it = string_pool->find(off);
-
- if (it != string_pool->end()) {
-
- buf_.pop(buf_.size() - size_before_string);
- return *it;
- }
-
- string_pool->insert(off);
- return off;
- }
-
-
-
-
-
- Offset<String> CreateSharedString(const char *str) {
- return CreateSharedString(str, strlen(str));
- }
-
-
-
-
-
- Offset<String> CreateSharedString(const std::string &str) {
- return CreateSharedString(str.c_str(), str.length());
- }
-
-
-
-
-
- Offset<String> CreateSharedString(const String *str) {
- return CreateSharedString(str->c_str(), str->Length());
- }
-
- uoffset_t EndVector(size_t len) {
- assert(nested);
- nested = false;
- return PushElement(static_cast<uoffset_t>(len));
- }
- void StartVector(size_t len, size_t elemsize) {
- NotNested();
- nested = true;
- PreAlign<uoffset_t>(len * elemsize);
- PreAlign(len * elemsize, elemsize);
- }
-
-
-
-
-
- void ForceVectorAlignment(size_t len, size_t elemsize, size_t alignment) {
- PreAlign(len * elemsize, alignment);
- }
- uint8_t *ReserveElements(size_t len, size_t elemsize) {
- return buf_.make_space(len * elemsize);
- }
-
-
-
-
-
-
-
-
- template<typename T> Offset<Vector<T>> CreateVector(const T *v, size_t len) {
- StartVector(len, sizeof(T));
- for (auto i = len; i > 0; ) {
- PushElement(v[--i]);
- }
- return Offset<Vector<T>>(EndVector(len));
- }
-
-
-
-
-
-
- template<typename T> Offset<Vector<T>> CreateVector(const std::vector<T> &v) {
- return CreateVector(data(v), v.size());
- }
-
-
-
- Offset<Vector<uint8_t>> CreateVector(const std::vector<bool> &v) {
- StartVector(v.size(), sizeof(uint8_t));
- for (auto i = v.size(); i > 0; ) {
- PushElement(static_cast<uint8_t>(v[--i]));
- }
- return Offset<Vector<uint8_t>>(EndVector(v.size()));
- }
- #ifndef FLATBUFFERS_CPP98_STL
-
-
-
-
-
-
-
- template<typename T> Offset<Vector<T>> CreateVector(size_t vector_size,
- const std::function<T (size_t i)> &f) {
- std::vector<T> elems(vector_size);
- for (size_t i = 0; i < vector_size; i++) elems[i] = f(i);
- return CreateVector(elems);
- }
- #endif
-
-
-
-
-
-
- Offset<Vector<Offset<String>>> CreateVectorOfStrings(
- const std::vector<std::string> &v) {
- std::vector<Offset<String>> offsets(v.size());
- for (size_t i = 0; i < v.size(); i++) offsets[i] = CreateString(v[i]);
- return CreateVector(offsets);
- }
-
-
-
-
-
-
-
- template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
- const T *v, size_t len) {
- StartVector(len * sizeof(T) / AlignOf<T>(), AlignOf<T>());
- PushBytes(reinterpret_cast<const uint8_t *>(v), sizeof(T) * len);
- return Offset<Vector<const T *>>(EndVector(len));
- }
-
-
-
-
-
-
- template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
- const std::vector<T> &v) {
- return CreateVectorOfStructs(data(v), v.size());
- }
-
- template<typename T>
- struct TableKeyComparator {
- TableKeyComparator(vector_downward& buf) : buf_(buf) {}
- bool operator()(const Offset<T> &a, const Offset<T> &b) const {
- auto table_a = reinterpret_cast<T *>(buf_.data_at(a.o));
- auto table_b = reinterpret_cast<T *>(buf_.data_at(b.o));
- return table_a->KeyCompareLessThan(table_b);
- }
- vector_downward& buf_;
- private:
- TableKeyComparator& operator= (const TableKeyComparator&);
- };
-
-
-
-
-
-
-
-
-
- template<typename T> Offset<Vector<Offset<T>>> CreateVectorOfSortedTables(
- Offset<T> *v, size_t len) {
- std::sort(v, v + len, TableKeyComparator<T>(buf_));
- return CreateVector(v, len);
- }
-
-
-
-
-
-
-
- template<typename T> Offset<Vector<Offset<T>>> CreateVectorOfSortedTables(
- std::vector<Offset<T>> *v) {
- return CreateVectorOfSortedTables(data(*v), v->size());
- }
-
-
-
-
-
-
-
- uoffset_t CreateUninitializedVector(size_t len, size_t elemsize,
- uint8_t **buf) {
- NotNested();
- StartVector(len, elemsize);
- buf_.make_space(len * elemsize);
- auto vec_start = GetSize();
- auto vec_end = EndVector(len);
- *buf = buf_.data_at(vec_start);
- return vec_end;
- }
-
-
-
-
-
-
-
-
- template<typename T> Offset<Vector<T>> CreateUninitializedVector(
- size_t len, T **buf) {
- return CreateUninitializedVector(len, sizeof(T),
- reinterpret_cast<uint8_t **>(buf));
- }
-
- static const size_t kFileIdentifierLength = 4;
-
-
-
- template<typename T> void Finish(Offset<T> root,
- const char *file_identifier = nullptr) {
- Finish(root.o, file_identifier, false);
- }
-
-
-
-
-
-
-
- template<typename T> void FinishSizePrefixed(Offset<T> root,
- const char *file_identifier = nullptr) {
- Finish(root.o, file_identifier, true);
- }
- private:
-
- FlatBufferBuilder(const FlatBufferBuilder &);
- FlatBufferBuilder &operator=(const FlatBufferBuilder &);
- void Finish(uoffset_t root, const char *file_identifier, bool size_prefix) {
- NotNested();
-
- PreAlign((size_prefix ? sizeof(uoffset_t) : 0) +
- sizeof(uoffset_t) +
- (file_identifier ? kFileIdentifierLength : 0),
- minalign_);
- if (file_identifier) {
- assert(strlen(file_identifier) == kFileIdentifierLength);
- buf_.push(reinterpret_cast<const uint8_t *>(file_identifier),
- kFileIdentifierLength);
- }
- PushElement(ReferTo(root));
- if (size_prefix) {
- PushElement(GetSize());
- }
- finished = true;
- }
- struct FieldLoc {
- uoffset_t off;
- voffset_t id;
- };
- simple_allocator default_allocator;
- vector_downward buf_;
-
- std::vector<FieldLoc> offsetbuf_;
-
- bool nested;
-
- bool finished;
- std::vector<uoffset_t> vtables_;
- size_t minalign_;
- bool force_defaults_;
- struct StringOffsetCompare {
- StringOffsetCompare(const vector_downward &buf) : buf_(&buf) {}
- bool operator() (const Offset<String> &a, const Offset<String> &b) const {
- auto stra = reinterpret_cast<const String *>(buf_->data_at(a.o));
- auto strb = reinterpret_cast<const String *>(buf_->data_at(b.o));
- return strncmp(stra->c_str(), strb->c_str(),
- std::min(stra->size(), strb->size()) + 1) < 0;
- }
- const vector_downward *buf_;
- };
-
- typedef std::set<Offset<String>, StringOffsetCompare> StringOffsetMap;
- StringOffsetMap *string_pool;
- };
- template<typename T> T *GetMutableRoot(void *buf) {
- EndianCheck();
- return reinterpret_cast<T *>(reinterpret_cast<uint8_t *>(buf) +
- EndianScalar(*reinterpret_cast<uoffset_t *>(buf)));
- }
- template<typename T> const T *GetRoot(const void *buf) {
- return GetMutableRoot<T>(const_cast<void *>(buf));
- }
- template<typename T> const T *GetSizePrefixedRoot(const void *buf) {
- return GetRoot<T>(reinterpret_cast<const uint8_t *>(buf) + sizeof(uoffset_t));
- }
- template<typename T> T *GetMutableTemporaryPointer(FlatBufferBuilder &fbb,
- Offset<T> offset) {
- return reinterpret_cast<T *>(fbb.GetCurrentBufferPointer() +
- fbb.GetSize() - offset.o);
- }
- template<typename T> const T *GetTemporaryPointer(FlatBufferBuilder &fbb,
- Offset<T> offset) {
- return GetMutableTemporaryPointer<T>(fbb, offset);
- }
- inline bool BufferHasIdentifier(const void *buf, const char *identifier) {
- return strncmp(reinterpret_cast<const char *>(buf) + sizeof(uoffset_t),
- identifier, FlatBufferBuilder::kFileIdentifierLength) == 0;
- }
- class Verifier FLATBUFFERS_FINAL_CLASS {
- public:
- Verifier(const uint8_t *buf, size_t buf_len, size_t _max_depth = 64,
- size_t _max_tables = 1000000)
- : buf_(buf), end_(buf + buf_len), depth_(0), max_depth_(_max_depth),
- num_tables_(0), max_tables_(_max_tables)
- #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
- , upper_bound_(buf)
- #endif
- {}
-
- bool Check(bool ok) const {
- #ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE
- assert(ok);
- #endif
- #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
- if (!ok)
- upper_bound_ = buf_;
- #endif
- return ok;
- }
-
- bool Verify(const void *elem, size_t elem_len) const {
- #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
- auto upper_bound = reinterpret_cast<const uint8_t *>(elem) + elem_len;
- if (upper_bound_ < upper_bound)
- upper_bound_ = upper_bound;
- #endif
- return Check(elem_len <= (size_t) (end_ - buf_) &&
- elem >= buf_ &&
- elem <= end_ - elem_len);
- }
-
- template<typename T> bool Verify(const void *elem) const {
- return Verify(elem, sizeof(T));
- }
-
- template<typename T> bool VerifyTable(const T *table) {
- return !table || table->Verify(*this);
- }
-
- template<typename T> bool Verify(const Vector<T> *vec) const {
- const uint8_t *end;
- return !vec ||
- VerifyVector(reinterpret_cast<const uint8_t *>(vec), sizeof(T),
- &end);
- }
-
- template<typename T> bool Verify(const Vector<const T *> *vec) const {
- return Verify(reinterpret_cast<const Vector<T> *>(vec));
- }
-
- bool Verify(const String *str) const {
- const uint8_t *end;
- return !str ||
- (VerifyVector(reinterpret_cast<const uint8_t *>(str), 1, &end) &&
- Verify(end, 1) &&
- Check(*end == '\0'));
- }
-
- bool VerifyVector(const uint8_t *vec, size_t elem_size,
- const uint8_t **end) const {
-
- if (!Verify<uoffset_t>(vec)) return false;
-
-
- auto size = ReadScalar<uoffset_t>(vec);
- auto max_elems = FLATBUFFERS_MAX_BUFFER_SIZE / elem_size;
- if (!Check(size < max_elems))
- return false;
- auto byte_size = sizeof(size) + elem_size * size;
- *end = vec + byte_size;
- return Verify(vec, byte_size);
- }
-
- bool VerifyVectorOfStrings(const Vector<Offset<String>> *vec) const {
- if (vec) {
- for (uoffset_t i = 0; i < vec->size(); i++) {
- if (!Verify(vec->Get(i))) return false;
- }
- }
- return true;
- }
-
- template<typename T> bool VerifyVectorOfTables(const Vector<Offset<T>> *vec) {
- if (vec) {
- for (uoffset_t i = 0; i < vec->size(); i++) {
- if (!vec->Get(i)->Verify(*this)) return false;
- }
- }
- return true;
- }
- template<typename T> bool VerifyBufferFromStart(const char *identifier,
- const uint8_t *start) {
- if (identifier &&
- (size_t(end_ - start) < 2 * sizeof(flatbuffers::uoffset_t) ||
- !BufferHasIdentifier(start, identifier))) {
- return false;
- }
-
- return Verify<uoffset_t>(start) &&
- reinterpret_cast<const T *>(start + ReadScalar<uoffset_t>(start))->
- Verify(*this)
- #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
- && GetComputedSize()
- #endif
- ;
- }
-
- template<typename T> bool VerifyBuffer(const char *identifier) {
- return VerifyBufferFromStart<T>(identifier, buf_);
- }
- template<typename T> bool VerifySizePrefixedBuffer(const char *identifier) {
- return Verify<uoffset_t>(buf_) &&
- ReadScalar<uoffset_t>(buf_) == end_ - buf_ - sizeof(uoffset_t) &&
- VerifyBufferFromStart<T>(identifier, buf_ + sizeof(uoffset_t));
- }
-
-
-
-
- bool VerifyComplexity() {
- depth_++;
- num_tables_++;
- return Check(depth_ <= max_depth_ && num_tables_ <= max_tables_);
- }
-
- bool EndTable() {
- depth_--;
- return true;
- }
- #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
-
- size_t GetComputedSize() const {
- uintptr_t size = upper_bound_ - buf_;
-
- size = (size - 1 + sizeof(uoffset_t)) & ~(sizeof(uoffset_t) - 1);
- return (buf_ + size > end_) ? 0 : size;
- }
- #endif
- private:
- const uint8_t *buf_;
- const uint8_t *end_;
- size_t depth_;
- size_t max_depth_;
- size_t num_tables_;
- size_t max_tables_;
- #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
- mutable const uint8_t *upper_bound_;
- #endif
- };
- struct BufferRefBase {};
- template<typename T> struct BufferRef : BufferRefBase {
- BufferRef() : buf(nullptr), len(0), must_free(false) {}
- BufferRef(uint8_t *_buf, uoffset_t _len)
- : buf(_buf), len(_len), must_free(false) {}
- ~BufferRef() { if (must_free) free(buf); }
- const T *GetRoot() const { return flatbuffers::GetRoot<T>(buf); }
- bool Verify() {
- Verifier verifier(buf, len);
- return verifier.VerifyBuffer<T>(nullptr);
- }
- uint8_t *buf;
- uoffset_t len;
- bool must_free;
- };
- class Struct FLATBUFFERS_FINAL_CLASS {
- public:
- template<typename T> T GetField(uoffset_t o) const {
- return ReadScalar<T>(&data_[o]);
- }
- template<typename T> T GetStruct(uoffset_t o) const {
- return reinterpret_cast<T>(&data_[o]);
- }
- const uint8_t *GetAddressOf(uoffset_t o) const { return &data_[o]; }
- uint8_t *GetAddressOf(uoffset_t o) { return &data_[o]; }
- private:
- uint8_t data_[1];
- };
- class Table {
- public:
- const uint8_t *GetVTable() const {
- return data_ - ReadScalar<soffset_t>(data_);
- }
-
-
- voffset_t GetOptionalFieldOffset(voffset_t field) const {
-
- auto vtable = GetVTable();
-
- auto vtsize = ReadScalar<voffset_t>(vtable);
-
-
- return field < vtsize ? ReadScalar<voffset_t>(vtable + field) : 0;
- }
- template<typename T> T GetField(voffset_t field, T defaultval) const {
- auto field_offset = GetOptionalFieldOffset(field);
- return field_offset ? ReadScalar<T>(data_ + field_offset) : defaultval;
- }
- template<typename P> P GetPointer(voffset_t field) {
- auto field_offset = GetOptionalFieldOffset(field);
- auto p = data_ + field_offset;
- return field_offset
- ? reinterpret_cast<P>(p + ReadScalar<uoffset_t>(p))
- : nullptr;
- }
- template<typename P> P GetPointer(voffset_t field) const {
- return const_cast<Table *>(this)->GetPointer<P>(field);
- }
- template<typename P> P GetStruct(voffset_t field) const {
- auto field_offset = GetOptionalFieldOffset(field);
- auto p = const_cast<uint8_t *>(data_ + field_offset);
- return field_offset ? reinterpret_cast<P>(p) : nullptr;
- }
- template<typename T> bool SetField(voffset_t field, T val) {
- auto field_offset = GetOptionalFieldOffset(field);
- if (!field_offset) return false;
- WriteScalar(data_ + field_offset, val);
- return true;
- }
- bool SetPointer(voffset_t field, const uint8_t *val) {
- auto field_offset = GetOptionalFieldOffset(field);
- if (!field_offset) return false;
- WriteScalar(data_ + field_offset,
- static_cast<uoffset_t>(val - (data_ + field_offset)));
- return true;
- }
- uint8_t *GetAddressOf(voffset_t field) {
- auto field_offset = GetOptionalFieldOffset(field);
- return field_offset ? data_ + field_offset : nullptr;
- }
- const uint8_t *GetAddressOf(voffset_t field) const {
- return const_cast<Table *>(this)->GetAddressOf(field);
- }
- bool CheckField(voffset_t field) const {
- return GetOptionalFieldOffset(field) != 0;
- }
-
-
- bool VerifyTableStart(Verifier &verifier) const {
-
- if (!verifier.Verify<soffset_t>(data_)) return false;
- auto vtable = GetVTable();
-
- return verifier.VerifyComplexity() &&
- verifier.Verify<voffset_t>(vtable) &&
- (ReadScalar<voffset_t>(vtable) & (sizeof(voffset_t) - 1)) == 0 &&
- verifier.Verify(vtable, ReadScalar<voffset_t>(vtable));
- }
-
- template<typename T> bool VerifyField(const Verifier &verifier,
- voffset_t field) const {
-
-
- auto field_offset = GetOptionalFieldOffset(field);
-
- return !field_offset || verifier.Verify<T>(data_ + field_offset);
- }
-
- template<typename T> bool VerifyFieldRequired(const Verifier &verifier,
- voffset_t field) const {
- auto field_offset = GetOptionalFieldOffset(field);
- return verifier.Check(field_offset != 0) &&
- verifier.Verify<T>(data_ + field_offset);
- }
- private:
-
-
- Table();
- Table(const Table &other);
- uint8_t data_[1];
- };
- inline const uint8_t *GetBufferStartFromRootPointer(const void *root) {
- auto table = reinterpret_cast<const Table *>(root);
- auto vtable = table->GetVTable();
-
- auto start = std::min(vtable, reinterpret_cast<const uint8_t *>(root));
-
- start = reinterpret_cast<const uint8_t *>(
- reinterpret_cast<uintptr_t>(start) & ~(sizeof(uoffset_t) - 1));
-
-
-
-
-
-
-
-
-
- static_assert(FlatBufferBuilder::kFileIdentifierLength == sizeof(uoffset_t),
- "file_identifier is assumed to be the same size as uoffset_t");
- for (auto possible_roots = FLATBUFFERS_MAX_ALIGNMENT / sizeof(uoffset_t) + 1;
- possible_roots;
- possible_roots--) {
- start -= sizeof(uoffset_t);
- if (ReadScalar<uoffset_t>(start) + start ==
- reinterpret_cast<const uint8_t *>(root)) return start;
- }
-
-
-
-
- assert(false);
- return nullptr;
- }
- struct NativeTable {
- };
- typedef uint64_t hash_value_t;
- #ifdef FLATBUFFERS_CPP98_STL
- typedef void (*resolver_function_t)(void **pointer_adr, hash_value_t hash);
- typedef hash_value_t (*rehasher_function_t)(void *pointer);
- #else
- typedef std::function<void (void **pointer_adr, hash_value_t hash)>
- resolver_function_t;
- typedef std::function<hash_value_t (void *pointer)> rehasher_function_t;
- #endif
- template<typename T> bool IsFieldPresent(const T *table, voffset_t field) {
-
- return reinterpret_cast<const Table *>(table)->CheckField(field);
- }
- inline int LookupEnum(const char **names, const char *name) {
- for (const char **p = names; *p; p++)
- if (!strcmp(*p, name))
- return static_cast<int>(p - names);
- return -1;
- }
- #if defined(_MSC_VER)
- #define MANUALLY_ALIGNED_STRUCT(alignment) \
- __pragma(pack(1)); \
- struct __declspec(align(alignment))
- #define STRUCT_END(name, size) \
- __pragma(pack()); \
- static_assert(sizeof(name) == size, "compiler breaks packing rules")
- #elif defined(__GNUC__) || defined(__clang__)
- #define MANUALLY_ALIGNED_STRUCT(alignment) \
- _Pragma("pack(1)") \
- struct __attribute__((aligned(alignment)))
- #define STRUCT_END(name, size) \
- _Pragma("pack()") \
- static_assert(sizeof(name) == size, "compiler breaks packing rules")
- #else
- #error Unknown compiler, please define structure alignment macros
- #endif
- #if !defined(_WIN32) && !defined(__CYGWIN__)
- extern volatile __attribute__((weak)) const char *flatbuffer_version_string;
- volatile __attribute__((weak)) const char *flatbuffer_version_string =
- "FlatBuffers "
- FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
- FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
- FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
- #endif
- #define DEFINE_BITMASK_OPERATORS(E, T)\
- inline E operator | (E lhs, E rhs){\
- return E(T(lhs) | T(rhs));\
- }\
- inline E operator & (E lhs, E rhs){\
- return E(T(lhs) & T(rhs));\
- }\
- inline E operator ^ (E lhs, E rhs){\
- return E(T(lhs) ^ T(rhs));\
- }\
- inline E operator ~ (E lhs){\
- return E(~T(lhs));\
- }\
- inline E operator |= (E &lhs, E rhs){\
- lhs = lhs | rhs;\
- return lhs;\
- }\
- inline E operator &= (E &lhs, E rhs){\
- lhs = lhs & rhs;\
- return lhs;\
- }\
- inline E operator ^= (E &lhs, E rhs){\
- lhs = lhs ^ rhs;\
- return lhs;\
- }\
- inline bool operator !(E rhs) \
- {\
- return !bool(T(rhs)); \
- }
- }
- #endif
|