1
0

flatbuffers.h 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  1. /*
  2. * Copyright 2014 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef FLATBUFFERS_H_
  17. #define FLATBUFFERS_H_
  18. #include <assert.h>
  19. #include <cstdint>
  20. #include <cstddef>
  21. #include <cstdlib>
  22. #include <cstring>
  23. #include <string>
  24. #include <utility>
  25. #include <type_traits>
  26. #include <vector>
  27. #include <set>
  28. #include <algorithm>
  29. #include <memory>
  30. #ifdef _STLPORT_VERSION
  31. #define FLATBUFFERS_CPP98_STL
  32. #endif
  33. #ifndef FLATBUFFERS_CPP98_STL
  34. #include <functional>
  35. #endif
  36. /// @cond FLATBUFFERS_INTERNAL
  37. #if __cplusplus <= 199711L && \
  38. (!defined(_MSC_VER) || _MSC_VER < 1600) && \
  39. (!defined(__GNUC__) || \
  40. (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40400))
  41. #error A C++11 compatible compiler with support for the auto typing is \
  42. required for FlatBuffers.
  43. #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
  44. #endif
  45. #if !defined(__clang__) && \
  46. defined(__GNUC__) && \
  47. (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600)
  48. // Backwards compatability for g++ 4.4, and 4.5 which don't have the nullptr
  49. // and constexpr keywords. Note the __clang__ check is needed, because clang
  50. // presents itself as an older GNUC compiler.
  51. #ifndef nullptr_t
  52. const class nullptr_t {
  53. public:
  54. template<class T> inline operator T*() const { return 0; }
  55. private:
  56. void operator&() const;
  57. } nullptr = {};
  58. #endif
  59. #ifndef constexpr
  60. #define constexpr const
  61. #endif
  62. #endif
  63. // The wire format uses a little endian encoding (since that's efficient for
  64. // the common platforms).
  65. #if !defined(FLATBUFFERS_LITTLEENDIAN)
  66. #if defined(__GNUC__) || defined(__clang__)
  67. #ifdef __BIG_ENDIAN__
  68. #define FLATBUFFERS_LITTLEENDIAN 0
  69. #else
  70. #define FLATBUFFERS_LITTLEENDIAN 1
  71. #endif // __BIG_ENDIAN__
  72. #elif defined(_MSC_VER)
  73. #if defined(_M_PPC)
  74. #define FLATBUFFERS_LITTLEENDIAN 0
  75. #else
  76. #define FLATBUFFERS_LITTLEENDIAN 1
  77. #endif
  78. #else
  79. #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN.
  80. #endif
  81. #endif // !defined(FLATBUFFERS_LITTLEENDIAN)
  82. #define FLATBUFFERS_VERSION_MAJOR 1
  83. #define FLATBUFFERS_VERSION_MINOR 5
  84. #define FLATBUFFERS_VERSION_REVISION 0
  85. #define FLATBUFFERS_STRING_EXPAND(X) #X
  86. #define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
  87. #if (!defined(_MSC_VER) || _MSC_VER > 1600) && \
  88. (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407))
  89. #define FLATBUFFERS_FINAL_CLASS final
  90. #else
  91. #define FLATBUFFERS_FINAL_CLASS
  92. #endif
  93. #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
  94. (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406))
  95. #define FLATBUFFERS_CONSTEXPR constexpr
  96. #else
  97. #define FLATBUFFERS_CONSTEXPR
  98. #endif
  99. /// @endcond
  100. /// @file
  101. namespace flatbuffers {
  102. /// @cond FLATBUFFERS_INTERNAL
  103. // Our default offset / size type, 32bit on purpose on 64bit systems.
  104. // Also, using a consistent offset type maintains compatibility of serialized
  105. // offset values between 32bit and 64bit systems.
  106. typedef uint32_t uoffset_t;
  107. // Signed offsets for references that can go in both directions.
  108. typedef int32_t soffset_t;
  109. // Offset/index used in v-tables, can be changed to uint8_t in
  110. // format forks to save a bit of space if desired.
  111. typedef uint16_t voffset_t;
  112. typedef uintmax_t largest_scalar_t;
  113. // In 32bits, this evaluates to 2GB - 1
  114. #define FLATBUFFERS_MAX_BUFFER_SIZE ((1ULL << (sizeof(soffset_t) * 8 - 1)) - 1)
  115. // We support aligning the contents of buffers up to this size.
  116. #define FLATBUFFERS_MAX_ALIGNMENT 16
  117. #ifndef FLATBUFFERS_CPP98_STL
  118. // Pointer to relinquished memory.
  119. typedef std::unique_ptr<uint8_t, std::function<void(uint8_t * /* unused */)>>
  120. unique_ptr_t;
  121. #endif
  122. // Wrapper for uoffset_t to allow safe template specialization.
  123. template<typename T> struct Offset {
  124. uoffset_t o;
  125. Offset() : o(0) {}
  126. Offset(uoffset_t _o) : o(_o) {}
  127. Offset<void> Union() const { return Offset<void>(o); }
  128. };
  129. inline void EndianCheck() {
  130. int endiantest = 1;
  131. // If this fails, see FLATBUFFERS_LITTLEENDIAN above.
  132. assert(*reinterpret_cast<char *>(&endiantest) == FLATBUFFERS_LITTLEENDIAN);
  133. (void)endiantest;
  134. }
  135. template<typename T> T EndianSwap(T t) {
  136. #if defined(_MSC_VER)
  137. #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort
  138. #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong
  139. #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64
  140. #else
  141. #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408
  142. // __builtin_bswap16 was missing prior to GCC 4.8.
  143. #define FLATBUFFERS_BYTESWAP16(x) \
  144. static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16))
  145. #else
  146. #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16
  147. #endif
  148. #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32
  149. #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64
  150. #endif
  151. if (sizeof(T) == 1) { // Compile-time if-then's.
  152. return t;
  153. } else if (sizeof(T) == 2) {
  154. auto r = FLATBUFFERS_BYTESWAP16(*reinterpret_cast<uint16_t *>(&t));
  155. return *reinterpret_cast<T *>(&r);
  156. } else if (sizeof(T) == 4) {
  157. auto r = FLATBUFFERS_BYTESWAP32(*reinterpret_cast<uint32_t *>(&t));
  158. return *reinterpret_cast<T *>(&r);
  159. } else if (sizeof(T) == 8) {
  160. auto r = FLATBUFFERS_BYTESWAP64(*reinterpret_cast<uint64_t *>(&t));
  161. return *reinterpret_cast<T *>(&r);
  162. } else {
  163. assert(0);
  164. }
  165. }
  166. template<typename T> T EndianScalar(T t) {
  167. #if FLATBUFFERS_LITTLEENDIAN
  168. return t;
  169. #else
  170. return EndianSwap(t);
  171. #endif
  172. }
  173. template<typename T> T ReadScalar(const void *p) {
  174. return EndianScalar(*reinterpret_cast<const T *>(p));
  175. }
  176. template<typename T> void WriteScalar(void *p, T t) {
  177. *reinterpret_cast<T *>(p) = EndianScalar(t);
  178. }
  179. template<typename T> size_t AlignOf() {
  180. #ifdef _MSC_VER
  181. return __alignof(T);
  182. #else
  183. #ifndef alignof
  184. return __alignof__(T);
  185. #else
  186. return alignof(T);
  187. #endif
  188. #endif
  189. }
  190. // When we read serialized data from memory, in the case of most scalars,
  191. // we want to just read T, but in the case of Offset, we want to actually
  192. // perform the indirection and return a pointer.
  193. // The template specialization below does just that.
  194. // It is wrapped in a struct since function templates can't overload on the
  195. // return type like this.
  196. // The typedef is for the convenience of callers of this function
  197. // (avoiding the need for a trailing return decltype)
  198. template<typename T> struct IndirectHelper {
  199. typedef T return_type;
  200. typedef T mutable_return_type;
  201. static const size_t element_stride = sizeof(T);
  202. static return_type Read(const uint8_t *p, uoffset_t i) {
  203. return EndianScalar((reinterpret_cast<const T *>(p))[i]);
  204. }
  205. };
  206. template<typename T> struct IndirectHelper<Offset<T>> {
  207. typedef const T *return_type;
  208. typedef T *mutable_return_type;
  209. static const size_t element_stride = sizeof(uoffset_t);
  210. static return_type Read(const uint8_t *p, uoffset_t i) {
  211. p += i * sizeof(uoffset_t);
  212. return reinterpret_cast<return_type>(p + ReadScalar<uoffset_t>(p));
  213. }
  214. };
  215. template<typename T> struct IndirectHelper<const T *> {
  216. typedef const T *return_type;
  217. typedef T *mutable_return_type;
  218. static const size_t element_stride = sizeof(T);
  219. static return_type Read(const uint8_t *p, uoffset_t i) {
  220. return reinterpret_cast<const T *>(p + i * sizeof(T));
  221. }
  222. };
  223. // An STL compatible iterator implementation for Vector below, effectively
  224. // calling Get() for every element.
  225. template<typename T, typename IT>
  226. struct VectorIterator
  227. : public std::iterator<std::random_access_iterator_tag, IT, uoffset_t> {
  228. typedef std::iterator<std::random_access_iterator_tag, IT, uoffset_t> super_type;
  229. public:
  230. VectorIterator(const uint8_t *data, uoffset_t i) :
  231. data_(data + IndirectHelper<T>::element_stride * i) {}
  232. VectorIterator(const VectorIterator &other) : data_(other.data_) {}
  233. #ifndef FLATBUFFERS_CPP98_STL
  234. VectorIterator(VectorIterator &&other) : data_(std::move(other.data_)) {}
  235. #endif
  236. VectorIterator &operator=(const VectorIterator &other) {
  237. data_ = other.data_;
  238. return *this;
  239. }
  240. VectorIterator &operator=(VectorIterator &&other) {
  241. data_ = other.data_;
  242. return *this;
  243. }
  244. bool operator==(const VectorIterator &other) const {
  245. return data_ == other.data_;
  246. }
  247. bool operator!=(const VectorIterator &other) const {
  248. return data_ != other.data_;
  249. }
  250. ptrdiff_t operator-(const VectorIterator &other) const {
  251. return (data_ - other.data_) / IndirectHelper<T>::element_stride;
  252. }
  253. typename super_type::value_type operator *() const {
  254. return IndirectHelper<T>::Read(data_, 0);
  255. }
  256. typename super_type::value_type operator->() const {
  257. return IndirectHelper<T>::Read(data_, 0);
  258. }
  259. VectorIterator &operator++() {
  260. data_ += IndirectHelper<T>::element_stride;
  261. return *this;
  262. }
  263. VectorIterator operator++(int) {
  264. VectorIterator temp(data_, 0);
  265. data_ += IndirectHelper<T>::element_stride;
  266. return temp;
  267. }
  268. VectorIterator operator+(const uoffset_t &offset) {
  269. return VectorIterator(data_ + offset * IndirectHelper<T>::element_stride, 0);
  270. }
  271. VectorIterator& operator+=(const uoffset_t &offset) {
  272. data_ += offset * IndirectHelper<T>::element_stride;
  273. return *this;
  274. }
  275. VectorIterator &operator--() {
  276. data_ -= IndirectHelper<T>::element_stride;
  277. return *this;
  278. }
  279. VectorIterator operator--(int) {
  280. VectorIterator temp(data_, 0);
  281. data_ -= IndirectHelper<T>::element_stride;
  282. return temp;
  283. }
  284. VectorIterator operator-(const uoffset_t &offset) {
  285. return VectorIterator(data_ - offset * IndirectHelper<T>::element_stride, 0);
  286. }
  287. VectorIterator& operator-=(const uoffset_t &offset) {
  288. data_ -= offset * IndirectHelper<T>::element_stride;
  289. return *this;
  290. }
  291. private:
  292. const uint8_t *data_;
  293. };
  294. // This is used as a helper type for accessing vectors.
  295. // Vector::data() assumes the vector elements start after the length field.
  296. template<typename T> class Vector {
  297. public:
  298. typedef VectorIterator<T, typename IndirectHelper<T>::mutable_return_type>
  299. iterator;
  300. typedef VectorIterator<T, typename IndirectHelper<T>::return_type>
  301. const_iterator;
  302. uoffset_t size() const { return EndianScalar(length_); }
  303. // Deprecated: use size(). Here for backwards compatibility.
  304. uoffset_t Length() const { return size(); }
  305. typedef typename IndirectHelper<T>::return_type return_type;
  306. typedef typename IndirectHelper<T>::mutable_return_type mutable_return_type;
  307. return_type Get(uoffset_t i) const {
  308. assert(i < size());
  309. return IndirectHelper<T>::Read(Data(), i);
  310. }
  311. return_type operator[](uoffset_t i) const { return Get(i); }
  312. // If this is a Vector of enums, T will be its storage type, not the enum
  313. // type. This function makes it convenient to retrieve value with enum
  314. // type E.
  315. template<typename E> E GetEnum(uoffset_t i) const {
  316. return static_cast<E>(Get(i));
  317. }
  318. const void *GetStructFromOffset(size_t o) const {
  319. return reinterpret_cast<const void *>(Data() + o);
  320. }
  321. iterator begin() { return iterator(Data(), 0); }
  322. const_iterator begin() const { return const_iterator(Data(), 0); }
  323. iterator end() { return iterator(Data(), size()); }
  324. const_iterator end() const { return const_iterator(Data(), size()); }
  325. // Change elements if you have a non-const pointer to this object.
  326. // Scalars only. See reflection.h, and the documentation.
  327. void Mutate(uoffset_t i, const T& val) {
  328. assert(i < size());
  329. WriteScalar(data() + i, val);
  330. }
  331. // Change an element of a vector of tables (or strings).
  332. // "val" points to the new table/string, as you can obtain from
  333. // e.g. reflection::AddFlatBuffer().
  334. void MutateOffset(uoffset_t i, const uint8_t *val) {
  335. assert(i < size());
  336. assert(sizeof(T) == sizeof(uoffset_t));
  337. WriteScalar(data() + i,
  338. static_cast<uoffset_t>(val - (Data() + i * sizeof(uoffset_t))));
  339. }
  340. // Get a mutable pointer to tables/strings inside this vector.
  341. mutable_return_type GetMutableObject(uoffset_t i) const {
  342. assert(i < size());
  343. return const_cast<mutable_return_type>(IndirectHelper<T>::Read(Data(), i));
  344. }
  345. // The raw data in little endian format. Use with care.
  346. const uint8_t *Data() const {
  347. return reinterpret_cast<const uint8_t *>(&length_ + 1);
  348. }
  349. uint8_t *Data() {
  350. return reinterpret_cast<uint8_t *>(&length_ + 1);
  351. }
  352. // Similarly, but typed, much like std::vector::data
  353. const T *data() const { return reinterpret_cast<const T *>(Data()); }
  354. T *data() { return reinterpret_cast<T *>(Data()); }
  355. template<typename K> return_type LookupByKey(K key) const {
  356. void *search_result = std::bsearch(&key, Data(), size(),
  357. IndirectHelper<T>::element_stride, KeyCompare<K>);
  358. if (!search_result) {
  359. return nullptr; // Key not found.
  360. }
  361. const uint8_t *element = reinterpret_cast<const uint8_t *>(search_result);
  362. return IndirectHelper<T>::Read(element, 0);
  363. }
  364. protected:
  365. // This class is only used to access pre-existing data. Don't ever
  366. // try to construct these manually.
  367. Vector();
  368. uoffset_t length_;
  369. private:
  370. template<typename K> static int KeyCompare(const void *ap, const void *bp) {
  371. const K *key = reinterpret_cast<const K *>(ap);
  372. const uint8_t *data = reinterpret_cast<const uint8_t *>(bp);
  373. auto table = IndirectHelper<T>::Read(data, 0);
  374. // std::bsearch compares with the operands transposed, so we negate the
  375. // result here.
  376. return -table->KeyCompareWithValue(*key);
  377. }
  378. };
  379. // Represent a vector much like the template above, but in this case we
  380. // don't know what the element types are (used with reflection.h).
  381. class VectorOfAny {
  382. public:
  383. uoffset_t size() const { return EndianScalar(length_); }
  384. const uint8_t *Data() const {
  385. return reinterpret_cast<const uint8_t *>(&length_ + 1);
  386. }
  387. uint8_t *Data() {
  388. return reinterpret_cast<uint8_t *>(&length_ + 1);
  389. }
  390. protected:
  391. VectorOfAny();
  392. uoffset_t length_;
  393. };
  394. // Convenient helper function to get the length of any vector, regardless
  395. // of wether it is null or not (the field is not set).
  396. template<typename T> static inline size_t VectorLength(const Vector<T> *v) {
  397. return v ? v->Length() : 0;
  398. }
  399. struct String : public Vector<char> {
  400. const char *c_str() const { return reinterpret_cast<const char *>(Data()); }
  401. std::string str() const { return std::string(c_str(), Length()); }
  402. bool operator <(const String &o) const {
  403. return strcmp(c_str(), o.c_str()) < 0;
  404. }
  405. };
  406. // Simple indirection for buffer allocation, to allow this to be overridden
  407. // with custom allocation (see the FlatBufferBuilder constructor).
  408. class simple_allocator {
  409. public:
  410. virtual ~simple_allocator() {}
  411. virtual uint8_t *allocate(size_t size) const { return new uint8_t[size]; }
  412. virtual void deallocate(uint8_t *p) const { delete[] p; }
  413. };
  414. // This is a minimal replication of std::vector<uint8_t> functionality,
  415. // except growing from higher to lower addresses. i.e push_back() inserts data
  416. // in the lowest address in the vector.
  417. class vector_downward {
  418. public:
  419. explicit vector_downward(size_t initial_size,
  420. const simple_allocator &allocator)
  421. : reserved_(initial_size),
  422. buf_(allocator.allocate(reserved_)),
  423. cur_(buf_ + reserved_),
  424. allocator_(allocator) {
  425. assert((initial_size & (sizeof(largest_scalar_t) - 1)) == 0);
  426. }
  427. ~vector_downward() {
  428. if (buf_)
  429. allocator_.deallocate(buf_);
  430. }
  431. void clear() {
  432. if (buf_ == nullptr)
  433. buf_ = allocator_.allocate(reserved_);
  434. cur_ = buf_ + reserved_;
  435. }
  436. #ifndef FLATBUFFERS_CPP98_STL
  437. // Relinquish the pointer to the caller.
  438. unique_ptr_t release() {
  439. // Actually deallocate from the start of the allocated memory.
  440. std::function<void(uint8_t *)> deleter(
  441. std::bind(&simple_allocator::deallocate, allocator_, buf_));
  442. // Point to the desired offset.
  443. unique_ptr_t retval(data(), deleter);
  444. // Don't deallocate when this instance is destroyed.
  445. buf_ = nullptr;
  446. cur_ = nullptr;
  447. return retval;
  448. }
  449. #endif
  450. size_t growth_policy(size_t bytes) {
  451. return (bytes / 2) & ~(sizeof(largest_scalar_t) - 1);
  452. }
  453. uint8_t *make_space(size_t len) {
  454. if (len > static_cast<size_t>(cur_ - buf_)) {
  455. auto old_size = size();
  456. auto largest_align = AlignOf<largest_scalar_t>();
  457. reserved_ += (std::max)(len, growth_policy(reserved_));
  458. // Round up to avoid undefined behavior from unaligned loads and stores.
  459. reserved_ = (reserved_ + (largest_align - 1)) & ~(largest_align - 1);
  460. auto new_buf = allocator_.allocate(reserved_);
  461. auto new_cur = new_buf + reserved_ - old_size;
  462. memcpy(new_cur, cur_, old_size);
  463. cur_ = new_cur;
  464. allocator_.deallocate(buf_);
  465. buf_ = new_buf;
  466. }
  467. cur_ -= len;
  468. // Beyond this, signed offsets may not have enough range:
  469. // (FlatBuffers > 2GB not supported).
  470. assert(size() < FLATBUFFERS_MAX_BUFFER_SIZE);
  471. return cur_;
  472. }
  473. uoffset_t size() const {
  474. assert(cur_ != nullptr && buf_ != nullptr);
  475. return static_cast<uoffset_t>(reserved_ - (cur_ - buf_));
  476. }
  477. uint8_t *data() const {
  478. assert(cur_ != nullptr);
  479. return cur_;
  480. }
  481. uint8_t *data_at(size_t offset) const { return buf_ + reserved_ - offset; }
  482. // push() & fill() are most frequently called with small byte counts (<= 4),
  483. // which is why we're using loops rather than calling memcpy/memset.
  484. void push(const uint8_t *bytes, size_t num) {
  485. auto dest = make_space(num);
  486. for (size_t i = 0; i < num; i++) dest[i] = bytes[i];
  487. }
  488. void fill(size_t zero_pad_bytes) {
  489. auto dest = make_space(zero_pad_bytes);
  490. for (size_t i = 0; i < zero_pad_bytes; i++) dest[i] = 0;
  491. }
  492. void pop(size_t bytes_to_remove) { cur_ += bytes_to_remove; }
  493. private:
  494. // You shouldn't really be copying instances of this class.
  495. vector_downward(const vector_downward &);
  496. vector_downward &operator=(const vector_downward &);
  497. size_t reserved_;
  498. uint8_t *buf_;
  499. uint8_t *cur_; // Points at location between empty (below) and used (above).
  500. const simple_allocator &allocator_;
  501. };
  502. // Converts a Field ID to a virtual table offset.
  503. inline voffset_t FieldIndexToOffset(voffset_t field_id) {
  504. // Should correspond to what EndTable() below builds up.
  505. const int fixed_fields = 2; // Vtable size and Object Size.
  506. return static_cast<voffset_t>((field_id + fixed_fields) * sizeof(voffset_t));
  507. }
  508. // Computes how many bytes you'd have to pad to be able to write an
  509. // "scalar_size" scalar if the buffer had grown to "buf_size" (downwards in
  510. // memory).
  511. inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) {
  512. return ((~buf_size) + 1) & (scalar_size - 1);
  513. }
  514. template <typename T> const T* data(const std::vector<T> &v) {
  515. return v.empty() ? nullptr : &v.front();
  516. }
  517. template <typename T> T* data(std::vector<T> &v) {
  518. return v.empty() ? nullptr : &v.front();
  519. }
  520. /// @endcond
  521. /// @addtogroup flatbuffers_cpp_api
  522. /// @{
  523. /// @class FlatBufferBuilder
  524. /// @brief Helper class to hold data needed in creation of a FlatBuffer.
  525. /// To serialize data, you typically call one of the `Create*()` functions in
  526. /// the generated code, which in turn call a sequence of `StartTable`/
  527. /// `PushElement`/`AddElement`/`EndTable`, or the builtin `CreateString`/
  528. /// `CreateVector` functions. Do this is depth-first order to build up a tree to
  529. /// the root. `Finish()` wraps up the buffer ready for transport.
  530. class FlatBufferBuilder
  531. /// @cond FLATBUFFERS_INTERNAL
  532. FLATBUFFERS_FINAL_CLASS
  533. /// @endcond
  534. {
  535. public:
  536. /// @brief Default constructor for FlatBufferBuilder.
  537. /// @param[in] initial_size The initial size of the buffer, in bytes. Defaults
  538. /// to`1024`.
  539. /// @param[in] allocator A pointer to the `simple_allocator` that should be
  540. /// used. Defaults to `nullptr`, which means the `default_allocator` will be
  541. /// be used.
  542. explicit FlatBufferBuilder(uoffset_t initial_size = 1024,
  543. const simple_allocator *allocator = nullptr)
  544. : buf_(initial_size, allocator ? *allocator : default_allocator),
  545. nested(false), finished(false), minalign_(1), force_defaults_(false),
  546. string_pool(nullptr) {
  547. offsetbuf_.reserve(16); // Avoid first few reallocs.
  548. vtables_.reserve(16);
  549. EndianCheck();
  550. }
  551. ~FlatBufferBuilder() {
  552. if (string_pool) delete string_pool;
  553. }
  554. /// @brief Reset all the state in this FlatBufferBuilder so it can be reused
  555. /// to construct another buffer.
  556. void Clear() {
  557. buf_.clear();
  558. offsetbuf_.clear();
  559. nested = false;
  560. finished = false;
  561. vtables_.clear();
  562. minalign_ = 1;
  563. if (string_pool) string_pool->clear();
  564. }
  565. /// @brief The current size of the serialized buffer, counting from the end.
  566. /// @return Returns an `uoffset_t` with the current size of the buffer.
  567. uoffset_t GetSize() const { return buf_.size(); }
  568. /// @brief Get the serialized buffer (after you call `Finish()`).
  569. /// @return Returns an `uint8_t` pointer to the FlatBuffer data inside the
  570. /// buffer.
  571. uint8_t *GetBufferPointer() const {
  572. Finished();
  573. return buf_.data();
  574. }
  575. /// @brief Get a pointer to an unfinished buffer.
  576. /// @return Returns a `uint8_t` pointer to the unfinished buffer.
  577. uint8_t *GetCurrentBufferPointer() const { return buf_.data(); }
  578. #ifndef FLATBUFFERS_CPP98_STL
  579. /// @brief Get the released pointer to the serialized buffer.
  580. /// @warning Do NOT attempt to use this FlatBufferBuilder afterwards!
  581. /// @return The `unique_ptr` returned has a special allocator that knows how
  582. /// to deallocate this pointer (since it points to the middle of an
  583. /// allocation). Thus, do not mix this pointer with other `unique_ptr`'s, or
  584. /// call `release()`/`reset()` on it.
  585. unique_ptr_t ReleaseBufferPointer() {
  586. Finished();
  587. return buf_.release();
  588. }
  589. #endif
  590. /// @brief get the minimum alignment this buffer needs to be accessed
  591. /// properly. This is only known once all elements have been written (after
  592. /// you call Finish()). You can use this information if you need to embed
  593. /// a FlatBuffer in some other buffer, such that you can later read it
  594. /// without first having to copy it into its own buffer.
  595. size_t GetBufferMinAlignment() {
  596. Finished();
  597. return minalign_;
  598. }
  599. /// @cond FLATBUFFERS_INTERNAL
  600. void Finished() const {
  601. // If you get this assert, you're attempting to get access a buffer
  602. // which hasn't been finished yet. Be sure to call
  603. // FlatBufferBuilder::Finish with your root table.
  604. // If you really need to access an unfinished buffer, call
  605. // GetCurrentBufferPointer instead.
  606. assert(finished);
  607. }
  608. /// @endcond
  609. /// @brief In order to save space, fields that are set to their default value
  610. /// don't get serialized into the buffer.
  611. /// @param[in] bool fd When set to `true`, always serializes default values.
  612. void ForceDefaults(bool fd) { force_defaults_ = fd; }
  613. /// @cond FLATBUFFERS_INTERNAL
  614. void Pad(size_t num_bytes) { buf_.fill(num_bytes); }
  615. void Align(size_t elem_size) {
  616. if (elem_size > minalign_) minalign_ = elem_size;
  617. buf_.fill(PaddingBytes(buf_.size(), elem_size));
  618. }
  619. void PushFlatBuffer(const uint8_t *bytes, size_t size) {
  620. PushBytes(bytes, size);
  621. finished = true;
  622. }
  623. void PushBytes(const uint8_t *bytes, size_t size) {
  624. buf_.push(bytes, size);
  625. }
  626. void PopBytes(size_t amount) { buf_.pop(amount); }
  627. template<typename T> void AssertScalarT() {
  628. #ifndef FLATBUFFERS_CPP98_STL
  629. // The code assumes power of 2 sizes and endian-swap-ability.
  630. static_assert(std::is_scalar<T>::value
  631. // The Offset<T> type is essentially a scalar but fails is_scalar.
  632. || sizeof(T) == sizeof(Offset<void>),
  633. "T must be a scalar type");
  634. #endif
  635. }
  636. // Write a single aligned scalar to the buffer
  637. template<typename T> uoffset_t PushElement(T element) {
  638. AssertScalarT<T>();
  639. T litle_endian_element = EndianScalar(element);
  640. Align(sizeof(T));
  641. PushBytes(reinterpret_cast<uint8_t *>(&litle_endian_element), sizeof(T));
  642. return GetSize();
  643. }
  644. template<typename T> uoffset_t PushElement(Offset<T> off) {
  645. // Special case for offsets: see ReferTo below.
  646. return PushElement(ReferTo(off.o));
  647. }
  648. // When writing fields, we track where they are, so we can create correct
  649. // vtables later.
  650. void TrackField(voffset_t field, uoffset_t off) {
  651. FieldLoc fl = { off, field };
  652. offsetbuf_.push_back(fl);
  653. }
  654. // Like PushElement, but additionally tracks the field this represents.
  655. template<typename T> void AddElement(voffset_t field, T e, T def) {
  656. // We don't serialize values equal to the default.
  657. if (e == def && !force_defaults_) return;
  658. auto off = PushElement(e);
  659. TrackField(field, off);
  660. }
  661. template<typename T> void AddOffset(voffset_t field, Offset<T> off) {
  662. if (!off.o) return; // An offset of 0 means NULL, don't store.
  663. AddElement(field, ReferTo(off.o), static_cast<uoffset_t>(0));
  664. }
  665. template<typename T> void AddStruct(voffset_t field, const T *structptr) {
  666. if (!structptr) return; // Default, don't store.
  667. Align(AlignOf<T>());
  668. PushBytes(reinterpret_cast<const uint8_t *>(structptr), sizeof(T));
  669. TrackField(field, GetSize());
  670. }
  671. void AddStructOffset(voffset_t field, uoffset_t off) {
  672. TrackField(field, off);
  673. }
  674. // Offsets initially are relative to the end of the buffer (downwards).
  675. // This function converts them to be relative to the current location
  676. // in the buffer (when stored here), pointing upwards.
  677. uoffset_t ReferTo(uoffset_t off) {
  678. // Align to ensure GetSize() below is correct.
  679. Align(sizeof(uoffset_t));
  680. // Offset must refer to something already in buffer.
  681. assert(off && off <= GetSize());
  682. return GetSize() - off + static_cast<uoffset_t>(sizeof(uoffset_t));
  683. }
  684. void NotNested() {
  685. // If you hit this, you're trying to construct a Table/Vector/String
  686. // during the construction of its parent table (between the MyTableBuilder
  687. // and table.Finish().
  688. // Move the creation of these sub-objects to above the MyTableBuilder to
  689. // not get this assert.
  690. // Ignoring this assert may appear to work in simple cases, but the reason
  691. // it is here is that storing objects in-line may cause vtable offsets
  692. // to not fit anymore. It also leads to vtable duplication.
  693. assert(!nested);
  694. }
  695. // From generated code (or from the parser), we call StartTable/EndTable
  696. // with a sequence of AddElement calls in between.
  697. uoffset_t StartTable() {
  698. NotNested();
  699. nested = true;
  700. return GetSize();
  701. }
  702. // This finishes one serialized object by generating the vtable if it's a
  703. // table, comparing it against existing vtables, and writing the
  704. // resulting vtable offset.
  705. uoffset_t EndTable(uoffset_t start, voffset_t numfields) {
  706. // If you get this assert, a corresponding StartTable wasn't called.
  707. assert(nested);
  708. // Write the vtable offset, which is the start of any Table.
  709. // We fill it's value later.
  710. auto vtableoffsetloc = PushElement<soffset_t>(0);
  711. // Write a vtable, which consists entirely of voffset_t elements.
  712. // It starts with the number of offsets, followed by a type id, followed
  713. // by the offsets themselves. In reverse:
  714. buf_.fill(numfields * sizeof(voffset_t));
  715. auto table_object_size = vtableoffsetloc - start;
  716. assert(table_object_size < 0x10000); // Vtable use 16bit offsets.
  717. PushElement<voffset_t>(static_cast<voffset_t>(table_object_size));
  718. PushElement<voffset_t>(FieldIndexToOffset(numfields));
  719. // Write the offsets into the table
  720. for (auto field_location = offsetbuf_.begin();
  721. field_location != offsetbuf_.end();
  722. ++field_location) {
  723. auto pos = static_cast<voffset_t>(vtableoffsetloc - field_location->off);
  724. // If this asserts, it means you've set a field twice.
  725. assert(!ReadScalar<voffset_t>(buf_.data() + field_location->id));
  726. WriteScalar<voffset_t>(buf_.data() + field_location->id, pos);
  727. }
  728. offsetbuf_.clear();
  729. auto vt1 = reinterpret_cast<voffset_t *>(buf_.data());
  730. auto vt1_size = ReadScalar<voffset_t>(vt1);
  731. auto vt_use = GetSize();
  732. // See if we already have generated a vtable with this exact same
  733. // layout before. If so, make it point to the old one, remove this one.
  734. for (auto it = vtables_.begin(); it != vtables_.end(); ++it) {
  735. auto vt2 = reinterpret_cast<voffset_t *>(buf_.data_at(*it));
  736. auto vt2_size = *vt2;
  737. if (vt1_size != vt2_size || memcmp(vt2, vt1, vt1_size)) continue;
  738. vt_use = *it;
  739. buf_.pop(GetSize() - vtableoffsetloc);
  740. break;
  741. }
  742. // If this is a new vtable, remember it.
  743. if (vt_use == GetSize()) {
  744. vtables_.push_back(vt_use);
  745. }
  746. // Fill the vtable offset we created above.
  747. // The offset points from the beginning of the object to where the
  748. // vtable is stored.
  749. // Offsets default direction is downward in memory for future format
  750. // flexibility (storing all vtables at the start of the file).
  751. WriteScalar(buf_.data_at(vtableoffsetloc),
  752. static_cast<soffset_t>(vt_use) -
  753. static_cast<soffset_t>(vtableoffsetloc));
  754. nested = false;
  755. return vtableoffsetloc;
  756. }
  757. // This checks a required field has been set in a given table that has
  758. // just been constructed.
  759. template<typename T> void Required(Offset<T> table, voffset_t field) {
  760. auto table_ptr = buf_.data_at(table.o);
  761. auto vtable_ptr = table_ptr - ReadScalar<soffset_t>(table_ptr);
  762. bool ok = ReadScalar<voffset_t>(vtable_ptr + field) != 0;
  763. // If this fails, the caller will show what field needs to be set.
  764. assert(ok);
  765. (void)ok;
  766. }
  767. uoffset_t StartStruct(size_t alignment) {
  768. Align(alignment);
  769. return GetSize();
  770. }
  771. uoffset_t EndStruct() { return GetSize(); }
  772. void ClearOffsets() { offsetbuf_.clear(); }
  773. // Aligns such that when "len" bytes are written, an object can be written
  774. // after it with "alignment" without padding.
  775. void PreAlign(size_t len, size_t alignment) {
  776. buf_.fill(PaddingBytes(GetSize() + len, alignment));
  777. }
  778. template<typename T> void PreAlign(size_t len) {
  779. AssertScalarT<T>();
  780. PreAlign(len, sizeof(T));
  781. }
  782. /// @endcond
  783. /// @brief Store a string in the buffer, which can contain any binary data.
  784. /// @param[in] str A const char pointer to the data to be stored as a string.
  785. /// @param[in] len The number of bytes that should be stored from `str`.
  786. /// @return Returns the offset in the buffer where the string starts.
  787. Offset<String> CreateString(const char *str, size_t len) {
  788. NotNested();
  789. PreAlign<uoffset_t>(len + 1); // Always 0-terminated.
  790. buf_.fill(1);
  791. PushBytes(reinterpret_cast<const uint8_t *>(str), len);
  792. PushElement(static_cast<uoffset_t>(len));
  793. return Offset<String>(GetSize());
  794. }
  795. /// @brief Store a string in the buffer, which is null-terminated.
  796. /// @param[in] str A const char pointer to a C-string to add to the buffer.
  797. /// @return Returns the offset in the buffer where the string starts.
  798. Offset<String> CreateString(const char *str) {
  799. return CreateString(str, strlen(str));
  800. }
  801. /// @brief Store a string in the buffer, which can contain any binary data.
  802. /// @param[in] str A const reference to a std::string to store in the buffer.
  803. /// @return Returns the offset in the buffer where the string starts.
  804. Offset<String> CreateString(const std::string &str) {
  805. return CreateString(str.c_str(), str.length());
  806. }
  807. /// @brief Store a string in the buffer, which can contain any binary data.
  808. /// @param[in] str A const pointer to a `String` struct to add to the buffer.
  809. /// @return Returns the offset in the buffer where the string starts
  810. Offset<String> CreateString(const String *str) {
  811. return str ? CreateString(str->c_str(), str->Length()) : 0;
  812. }
  813. /// @brief Store a string in the buffer, which can contain any binary data.
  814. /// If a string with this exact contents has already been serialized before,
  815. /// instead simply returns the offset of the existing string.
  816. /// @param[in] str A const char pointer to the data to be stored as a string.
  817. /// @param[in] len The number of bytes that should be stored from `str`.
  818. /// @return Returns the offset in the buffer where the string starts.
  819. Offset<String> CreateSharedString(const char *str, size_t len) {
  820. if (!string_pool)
  821. string_pool = new StringOffsetMap(StringOffsetCompare(buf_));
  822. auto size_before_string = buf_.size();
  823. // Must first serialize the string, since the set is all offsets into
  824. // buffer.
  825. auto off = CreateString(str, len);
  826. auto it = string_pool->find(off);
  827. // If it exists we reuse existing serialized data!
  828. if (it != string_pool->end()) {
  829. // We can remove the string we serialized.
  830. buf_.pop(buf_.size() - size_before_string);
  831. return *it;
  832. }
  833. // Record this string for future use.
  834. string_pool->insert(off);
  835. return off;
  836. }
  837. /// @brief Store a string in the buffer, which null-terminated.
  838. /// If a string with this exact contents has already been serialized before,
  839. /// instead simply returns the offset of the existing string.
  840. /// @param[in] str A const char pointer to a C-string to add to the buffer.
  841. /// @return Returns the offset in the buffer where the string starts.
  842. Offset<String> CreateSharedString(const char *str) {
  843. return CreateSharedString(str, strlen(str));
  844. }
  845. /// @brief Store a string in the buffer, which can contain any binary data.
  846. /// If a string with this exact contents has already been serialized before,
  847. /// instead simply returns the offset of the existing string.
  848. /// @param[in] str A const reference to a std::string to store in the buffer.
  849. /// @return Returns the offset in the buffer where the string starts.
  850. Offset<String> CreateSharedString(const std::string &str) {
  851. return CreateSharedString(str.c_str(), str.length());
  852. }
  853. /// @brief Store a string in the buffer, which can contain any binary data.
  854. /// If a string with this exact contents has already been serialized before,
  855. /// instead simply returns the offset of the existing string.
  856. /// @param[in] str A const pointer to a `String` struct to add to the buffer.
  857. /// @return Returns the offset in the buffer where the string starts
  858. Offset<String> CreateSharedString(const String *str) {
  859. return CreateSharedString(str->c_str(), str->Length());
  860. }
  861. /// @cond FLATBUFFERS_INTERNAL
  862. uoffset_t EndVector(size_t len) {
  863. assert(nested); // Hit if no corresponding StartVector.
  864. nested = false;
  865. return PushElement(static_cast<uoffset_t>(len));
  866. }
  867. void StartVector(size_t len, size_t elemsize) {
  868. NotNested();
  869. nested = true;
  870. PreAlign<uoffset_t>(len * elemsize);
  871. PreAlign(len * elemsize, elemsize); // Just in case elemsize > uoffset_t.
  872. }
  873. // Call this right before StartVector/CreateVector if you want to force the
  874. // alignment to be something different than what the element size would
  875. // normally dictate.
  876. // This is useful when storing a nested_flatbuffer in a vector of bytes,
  877. // or when storing SIMD floats, etc.
  878. void ForceVectorAlignment(size_t len, size_t elemsize, size_t alignment) {
  879. PreAlign(len * elemsize, alignment);
  880. }
  881. uint8_t *ReserveElements(size_t len, size_t elemsize) {
  882. return buf_.make_space(len * elemsize);
  883. }
  884. /// @endcond
  885. /// @brief Serialize an array into a FlatBuffer `vector`.
  886. /// @tparam T The data type of the array elements.
  887. /// @param[in] v A pointer to the array of type `T` to serialize into the
  888. /// buffer as a `vector`.
  889. /// @param[in] len The number of elements to serialize.
  890. /// @return Returns a typed `Offset` into the serialized data indicating
  891. /// where the vector is stored.
  892. template<typename T> Offset<Vector<T>> CreateVector(const T *v, size_t len) {
  893. StartVector(len, sizeof(T));
  894. for (auto i = len; i > 0; ) {
  895. PushElement(v[--i]);
  896. }
  897. return Offset<Vector<T>>(EndVector(len));
  898. }
  899. /// @brief Serialize a `std::vector` into a FlatBuffer `vector`.
  900. /// @tparam T The data type of the `std::vector` elements.
  901. /// @param v A const reference to the `std::vector` to serialize into the
  902. /// buffer as a `vector`.
  903. /// @return Returns a typed `Offset` into the serialized data indicating
  904. /// where the vector is stored.
  905. template<typename T> Offset<Vector<T>> CreateVector(const std::vector<T> &v) {
  906. return CreateVector(data(v), v.size());
  907. }
  908. // vector<bool> may be implemented using a bit-set, so we can't access it as
  909. // an array. Instead, read elements manually.
  910. // Background: https://isocpp.org/blog/2012/11/on-vectorbool
  911. Offset<Vector<uint8_t>> CreateVector(const std::vector<bool> &v) {
  912. StartVector(v.size(), sizeof(uint8_t));
  913. for (auto i = v.size(); i > 0; ) {
  914. PushElement(static_cast<uint8_t>(v[--i]));
  915. }
  916. return Offset<Vector<uint8_t>>(EndVector(v.size()));
  917. }
  918. #ifndef FLATBUFFERS_CPP98_STL
  919. /// @brief Serialize values returned by a function into a FlatBuffer `vector`.
  920. /// This is a convenience function that takes care of iteration for you.
  921. /// @tparam T The data type of the `std::vector` elements.
  922. /// @param f A function that takes the current iteration 0..vector_size-1 and
  923. /// returns any type that you can construct a FlatBuffers vector out of.
  924. /// @return Returns a typed `Offset` into the serialized data indicating
  925. /// where the vector is stored.
  926. template<typename T> Offset<Vector<T>> CreateVector(size_t vector_size,
  927. const std::function<T (size_t i)> &f) {
  928. std::vector<T> elems(vector_size);
  929. for (size_t i = 0; i < vector_size; i++) elems[i] = f(i);
  930. return CreateVector(elems);
  931. }
  932. #endif
  933. /// @brief Serialize a `std::vector<std::string>` into a FlatBuffer `vector`.
  934. /// This is a convenience function for a common case.
  935. /// @param v A const reference to the `std::vector` to serialize into the
  936. /// buffer as a `vector`.
  937. /// @return Returns a typed `Offset` into the serialized data indicating
  938. /// where the vector is stored.
  939. Offset<Vector<Offset<String>>> CreateVectorOfStrings(
  940. const std::vector<std::string> &v) {
  941. std::vector<Offset<String>> offsets(v.size());
  942. for (size_t i = 0; i < v.size(); i++) offsets[i] = CreateString(v[i]);
  943. return CreateVector(offsets);
  944. }
  945. /// @brief Serialize an array of structs into a FlatBuffer `vector`.
  946. /// @tparam T The data type of the struct array elements.
  947. /// @param[in] v A pointer to the array of type `T` to serialize into the
  948. /// buffer as a `vector`.
  949. /// @param[in] len The number of elements to serialize.
  950. /// @return Returns a typed `Offset` into the serialized data indicating
  951. /// where the vector is stored.
  952. template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
  953. const T *v, size_t len) {
  954. StartVector(len * sizeof(T) / AlignOf<T>(), AlignOf<T>());
  955. PushBytes(reinterpret_cast<const uint8_t *>(v), sizeof(T) * len);
  956. return Offset<Vector<const T *>>(EndVector(len));
  957. }
  958. /// @brief Serialize a `std::vector` of structs into a FlatBuffer `vector`.
  959. /// @tparam T The data type of the `std::vector` struct elements.
  960. /// @param[in]] v A const reference to the `std::vector` of structs to
  961. /// serialize into the buffer as a `vector`.
  962. /// @return Returns a typed `Offset` into the serialized data indicating
  963. /// where the vector is stored.
  964. template<typename T> Offset<Vector<const T *>> CreateVectorOfStructs(
  965. const std::vector<T> &v) {
  966. return CreateVectorOfStructs(data(v), v.size());
  967. }
  968. /// @cond FLATBUFFERS_INTERNAL
  969. template<typename T>
  970. struct TableKeyComparator {
  971. TableKeyComparator(vector_downward& buf) : buf_(buf) {}
  972. bool operator()(const Offset<T> &a, const Offset<T> &b) const {
  973. auto table_a = reinterpret_cast<T *>(buf_.data_at(a.o));
  974. auto table_b = reinterpret_cast<T *>(buf_.data_at(b.o));
  975. return table_a->KeyCompareLessThan(table_b);
  976. }
  977. vector_downward& buf_;
  978. private:
  979. TableKeyComparator& operator= (const TableKeyComparator&);
  980. };
  981. /// @endcond
  982. /// @brief Serialize an array of `table` offsets as a `vector` in the buffer
  983. /// in sorted order.
  984. /// @tparam T The data type that the offset refers to.
  985. /// @param[in] v An array of type `Offset<T>` that contains the `table`
  986. /// offsets to store in the buffer in sorted order.
  987. /// @param[in] len The number of elements to store in the `vector`.
  988. /// @return Returns a typed `Offset` into the serialized data indicating
  989. /// where the vector is stored.
  990. template<typename T> Offset<Vector<Offset<T>>> CreateVectorOfSortedTables(
  991. Offset<T> *v, size_t len) {
  992. std::sort(v, v + len, TableKeyComparator<T>(buf_));
  993. return CreateVector(v, len);
  994. }
  995. /// @brief Serialize an array of `table` offsets as a `vector` in the buffer
  996. /// in sorted order.
  997. /// @tparam T The data type that the offset refers to.
  998. /// @param[in] v An array of type `Offset<T>` that contains the `table`
  999. /// offsets to store in the buffer in sorted order.
  1000. /// @return Returns a typed `Offset` into the serialized data indicating
  1001. /// where the vector is stored.
  1002. template<typename T> Offset<Vector<Offset<T>>> CreateVectorOfSortedTables(
  1003. std::vector<Offset<T>> *v) {
  1004. return CreateVectorOfSortedTables(data(*v), v->size());
  1005. }
  1006. /// @brief Specialized version of `CreateVector` for non-copying use cases.
  1007. /// Write the data any time later to the returned buffer pointer `buf`.
  1008. /// @param[in] len The number of elements to store in the `vector`.
  1009. /// @param[in] elemsize The size of each element in the `vector`.
  1010. /// @param[out] buf A pointer to a `uint8_t` pointer that can be
  1011. /// written to at a later time to serialize the data into a `vector`
  1012. /// in the buffer.
  1013. uoffset_t CreateUninitializedVector(size_t len, size_t elemsize,
  1014. uint8_t **buf) {
  1015. NotNested();
  1016. StartVector(len, elemsize);
  1017. buf_.make_space(len * elemsize);
  1018. auto vec_start = GetSize();
  1019. auto vec_end = EndVector(len);
  1020. *buf = buf_.data_at(vec_start);
  1021. return vec_end;
  1022. }
  1023. /// @brief Specialized version of `CreateVector` for non-copying use cases.
  1024. /// Write the data any time later to the returned buffer pointer `buf`.
  1025. /// @tparam T The data type of the data that will be stored in the buffer
  1026. /// as a `vector`.
  1027. /// @param[in] len The number of elements to store in the `vector`.
  1028. /// @param[out] buf A pointer to a pointer of type `T` that can be
  1029. /// written to at a later time to serialize the data into a `vector`
  1030. /// in the buffer.
  1031. template<typename T> Offset<Vector<T>> CreateUninitializedVector(
  1032. size_t len, T **buf) {
  1033. return CreateUninitializedVector(len, sizeof(T),
  1034. reinterpret_cast<uint8_t **>(buf));
  1035. }
  1036. /// @brief The length of a FlatBuffer file header.
  1037. static const size_t kFileIdentifierLength = 4;
  1038. /// @brief Finish serializing a buffer by writing the root offset.
  1039. /// @param[in] file_identifier If a `file_identifier` is given, the buffer
  1040. /// will be prefixed with a standard FlatBuffers file header.
  1041. template<typename T> void Finish(Offset<T> root,
  1042. const char *file_identifier = nullptr) {
  1043. Finish(root.o, file_identifier, false);
  1044. }
  1045. /// @brief Finish a buffer with a 32 bit size field pre-fixed (size of the
  1046. /// buffer following the size field). These buffers are NOT compatible
  1047. /// with standard buffers created by Finish, i.e. you can't call GetRoot
  1048. /// on them, you have to use GetSizePrefixedRoot instead.
  1049. /// All >32 bit quantities in this buffer will be aligned when the whole
  1050. /// size pre-fixed buffer is aligned.
  1051. /// These kinds of buffers are useful for creating a stream of FlatBuffers.
  1052. template<typename T> void FinishSizePrefixed(Offset<T> root,
  1053. const char *file_identifier = nullptr) {
  1054. Finish(root.o, file_identifier, true);
  1055. }
  1056. private:
  1057. // You shouldn't really be copying instances of this class.
  1058. FlatBufferBuilder(const FlatBufferBuilder &);
  1059. FlatBufferBuilder &operator=(const FlatBufferBuilder &);
  1060. void Finish(uoffset_t root, const char *file_identifier, bool size_prefix) {
  1061. NotNested();
  1062. // This will cause the whole buffer to be aligned.
  1063. PreAlign((size_prefix ? sizeof(uoffset_t) : 0) +
  1064. sizeof(uoffset_t) +
  1065. (file_identifier ? kFileIdentifierLength : 0),
  1066. minalign_);
  1067. if (file_identifier) {
  1068. assert(strlen(file_identifier) == kFileIdentifierLength);
  1069. buf_.push(reinterpret_cast<const uint8_t *>(file_identifier),
  1070. kFileIdentifierLength);
  1071. }
  1072. PushElement(ReferTo(root)); // Location of root.
  1073. if (size_prefix) {
  1074. PushElement(GetSize());
  1075. }
  1076. finished = true;
  1077. }
  1078. struct FieldLoc {
  1079. uoffset_t off;
  1080. voffset_t id;
  1081. };
  1082. simple_allocator default_allocator;
  1083. vector_downward buf_;
  1084. // Accumulating offsets of table members while it is being built.
  1085. std::vector<FieldLoc> offsetbuf_;
  1086. // Ensure objects are not nested.
  1087. bool nested;
  1088. // Ensure the buffer is finished before it is being accessed.
  1089. bool finished;
  1090. std::vector<uoffset_t> vtables_; // todo: Could make this into a map?
  1091. size_t minalign_;
  1092. bool force_defaults_; // Serialize values equal to their defaults anyway.
  1093. struct StringOffsetCompare {
  1094. StringOffsetCompare(const vector_downward &buf) : buf_(&buf) {}
  1095. bool operator() (const Offset<String> &a, const Offset<String> &b) const {
  1096. auto stra = reinterpret_cast<const String *>(buf_->data_at(a.o));
  1097. auto strb = reinterpret_cast<const String *>(buf_->data_at(b.o));
  1098. return strncmp(stra->c_str(), strb->c_str(),
  1099. std::min(stra->size(), strb->size()) + 1) < 0;
  1100. }
  1101. const vector_downward *buf_;
  1102. };
  1103. // For use with CreateSharedString. Instantiated on first use only.
  1104. typedef std::set<Offset<String>, StringOffsetCompare> StringOffsetMap;
  1105. StringOffsetMap *string_pool;
  1106. };
  1107. /// @}
  1108. /// @cond FLATBUFFERS_INTERNAL
  1109. // Helpers to get a typed pointer to the root object contained in the buffer.
  1110. template<typename T> T *GetMutableRoot(void *buf) {
  1111. EndianCheck();
  1112. return reinterpret_cast<T *>(reinterpret_cast<uint8_t *>(buf) +
  1113. EndianScalar(*reinterpret_cast<uoffset_t *>(buf)));
  1114. }
  1115. template<typename T> const T *GetRoot(const void *buf) {
  1116. return GetMutableRoot<T>(const_cast<void *>(buf));
  1117. }
  1118. template<typename T> const T *GetSizePrefixedRoot(const void *buf) {
  1119. return GetRoot<T>(reinterpret_cast<const uint8_t *>(buf) + sizeof(uoffset_t));
  1120. }
  1121. /// Helpers to get a typed pointer to objects that are currently being built.
  1122. /// @warning Creating new objects will lead to reallocations and invalidates
  1123. /// the pointer!
  1124. template<typename T> T *GetMutableTemporaryPointer(FlatBufferBuilder &fbb,
  1125. Offset<T> offset) {
  1126. return reinterpret_cast<T *>(fbb.GetCurrentBufferPointer() +
  1127. fbb.GetSize() - offset.o);
  1128. }
  1129. template<typename T> const T *GetTemporaryPointer(FlatBufferBuilder &fbb,
  1130. Offset<T> offset) {
  1131. return GetMutableTemporaryPointer<T>(fbb, offset);
  1132. }
  1133. // Helper to see if the identifier in a buffer has the expected value.
  1134. inline bool BufferHasIdentifier(const void *buf, const char *identifier) {
  1135. return strncmp(reinterpret_cast<const char *>(buf) + sizeof(uoffset_t),
  1136. identifier, FlatBufferBuilder::kFileIdentifierLength) == 0;
  1137. }
  1138. // Helper class to verify the integrity of a FlatBuffer
  1139. class Verifier FLATBUFFERS_FINAL_CLASS {
  1140. public:
  1141. Verifier(const uint8_t *buf, size_t buf_len, size_t _max_depth = 64,
  1142. size_t _max_tables = 1000000)
  1143. : buf_(buf), end_(buf + buf_len), depth_(0), max_depth_(_max_depth),
  1144. num_tables_(0), max_tables_(_max_tables)
  1145. #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
  1146. , upper_bound_(buf)
  1147. #endif
  1148. {}
  1149. // Central location where any verification failures register.
  1150. bool Check(bool ok) const {
  1151. #ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE
  1152. assert(ok);
  1153. #endif
  1154. #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
  1155. if (!ok)
  1156. upper_bound_ = buf_;
  1157. #endif
  1158. return ok;
  1159. }
  1160. // Verify any range within the buffer.
  1161. bool Verify(const void *elem, size_t elem_len) const {
  1162. #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
  1163. auto upper_bound = reinterpret_cast<const uint8_t *>(elem) + elem_len;
  1164. if (upper_bound_ < upper_bound)
  1165. upper_bound_ = upper_bound;
  1166. #endif
  1167. return Check(elem_len <= (size_t) (end_ - buf_) &&
  1168. elem >= buf_ &&
  1169. elem <= end_ - elem_len);
  1170. }
  1171. // Verify a range indicated by sizeof(T).
  1172. template<typename T> bool Verify(const void *elem) const {
  1173. return Verify(elem, sizeof(T));
  1174. }
  1175. // Verify a pointer (may be NULL) of a table type.
  1176. template<typename T> bool VerifyTable(const T *table) {
  1177. return !table || table->Verify(*this);
  1178. }
  1179. // Verify a pointer (may be NULL) of any vector type.
  1180. template<typename T> bool Verify(const Vector<T> *vec) const {
  1181. const uint8_t *end;
  1182. return !vec ||
  1183. VerifyVector(reinterpret_cast<const uint8_t *>(vec), sizeof(T),
  1184. &end);
  1185. }
  1186. // Verify a pointer (may be NULL) of a vector to struct.
  1187. template<typename T> bool Verify(const Vector<const T *> *vec) const {
  1188. return Verify(reinterpret_cast<const Vector<T> *>(vec));
  1189. }
  1190. // Verify a pointer (may be NULL) to string.
  1191. bool Verify(const String *str) const {
  1192. const uint8_t *end;
  1193. return !str ||
  1194. (VerifyVector(reinterpret_cast<const uint8_t *>(str), 1, &end) &&
  1195. Verify(end, 1) && // Must have terminator
  1196. Check(*end == '\0')); // Terminating byte must be 0.
  1197. }
  1198. // Common code between vectors and strings.
  1199. bool VerifyVector(const uint8_t *vec, size_t elem_size,
  1200. const uint8_t **end) const {
  1201. // Check we can read the size field.
  1202. if (!Verify<uoffset_t>(vec)) return false;
  1203. // Check the whole array. If this is a string, the byte past the array
  1204. // must be 0.
  1205. auto size = ReadScalar<uoffset_t>(vec);
  1206. auto max_elems = FLATBUFFERS_MAX_BUFFER_SIZE / elem_size;
  1207. if (!Check(size < max_elems))
  1208. return false; // Protect against byte_size overflowing.
  1209. auto byte_size = sizeof(size) + elem_size * size;
  1210. *end = vec + byte_size;
  1211. return Verify(vec, byte_size);
  1212. }
  1213. // Special case for string contents, after the above has been called.
  1214. bool VerifyVectorOfStrings(const Vector<Offset<String>> *vec) const {
  1215. if (vec) {
  1216. for (uoffset_t i = 0; i < vec->size(); i++) {
  1217. if (!Verify(vec->Get(i))) return false;
  1218. }
  1219. }
  1220. return true;
  1221. }
  1222. // Special case for table contents, after the above has been called.
  1223. template<typename T> bool VerifyVectorOfTables(const Vector<Offset<T>> *vec) {
  1224. if (vec) {
  1225. for (uoffset_t i = 0; i < vec->size(); i++) {
  1226. if (!vec->Get(i)->Verify(*this)) return false;
  1227. }
  1228. }
  1229. return true;
  1230. }
  1231. template<typename T> bool VerifyBufferFromStart(const char *identifier,
  1232. const uint8_t *start) {
  1233. if (identifier &&
  1234. (size_t(end_ - start) < 2 * sizeof(flatbuffers::uoffset_t) ||
  1235. !BufferHasIdentifier(start, identifier))) {
  1236. return false;
  1237. }
  1238. // Call T::Verify, which must be in the generated code for this type.
  1239. return Verify<uoffset_t>(start) &&
  1240. reinterpret_cast<const T *>(start + ReadScalar<uoffset_t>(start))->
  1241. Verify(*this)
  1242. #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
  1243. && GetComputedSize()
  1244. #endif
  1245. ;
  1246. }
  1247. // Verify this whole buffer, starting with root type T.
  1248. template<typename T> bool VerifyBuffer(const char *identifier) {
  1249. return VerifyBufferFromStart<T>(identifier, buf_);
  1250. }
  1251. template<typename T> bool VerifySizePrefixedBuffer(const char *identifier) {
  1252. return Verify<uoffset_t>(buf_) &&
  1253. ReadScalar<uoffset_t>(buf_) == end_ - buf_ - sizeof(uoffset_t) &&
  1254. VerifyBufferFromStart<T>(identifier, buf_ + sizeof(uoffset_t));
  1255. }
  1256. // Called at the start of a table to increase counters measuring data
  1257. // structure depth and amount, and possibly bails out with false if
  1258. // limits set by the constructor have been hit. Needs to be balanced
  1259. // with EndTable().
  1260. bool VerifyComplexity() {
  1261. depth_++;
  1262. num_tables_++;
  1263. return Check(depth_ <= max_depth_ && num_tables_ <= max_tables_);
  1264. }
  1265. // Called at the end of a table to pop the depth count.
  1266. bool EndTable() {
  1267. depth_--;
  1268. return true;
  1269. }
  1270. #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
  1271. // Returns the message size in bytes
  1272. size_t GetComputedSize() const {
  1273. uintptr_t size = upper_bound_ - buf_;
  1274. // Align the size to uoffset_t
  1275. size = (size - 1 + sizeof(uoffset_t)) & ~(sizeof(uoffset_t) - 1);
  1276. return (buf_ + size > end_) ? 0 : size;
  1277. }
  1278. #endif
  1279. private:
  1280. const uint8_t *buf_;
  1281. const uint8_t *end_;
  1282. size_t depth_;
  1283. size_t max_depth_;
  1284. size_t num_tables_;
  1285. size_t max_tables_;
  1286. #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
  1287. mutable const uint8_t *upper_bound_;
  1288. #endif
  1289. };
  1290. // Convenient way to bundle a buffer and its length, to pass it around
  1291. // typed by its root.
  1292. // A BufferRef does not own its buffer.
  1293. struct BufferRefBase {}; // for std::is_base_of
  1294. template<typename T> struct BufferRef : BufferRefBase {
  1295. BufferRef() : buf(nullptr), len(0), must_free(false) {}
  1296. BufferRef(uint8_t *_buf, uoffset_t _len)
  1297. : buf(_buf), len(_len), must_free(false) {}
  1298. ~BufferRef() { if (must_free) free(buf); }
  1299. const T *GetRoot() const { return flatbuffers::GetRoot<T>(buf); }
  1300. bool Verify() {
  1301. Verifier verifier(buf, len);
  1302. return verifier.VerifyBuffer<T>(nullptr);
  1303. }
  1304. uint8_t *buf;
  1305. uoffset_t len;
  1306. bool must_free;
  1307. };
  1308. // "structs" are flat structures that do not have an offset table, thus
  1309. // always have all members present and do not support forwards/backwards
  1310. // compatible extensions.
  1311. class Struct FLATBUFFERS_FINAL_CLASS {
  1312. public:
  1313. template<typename T> T GetField(uoffset_t o) const {
  1314. return ReadScalar<T>(&data_[o]);
  1315. }
  1316. template<typename T> T GetStruct(uoffset_t o) const {
  1317. return reinterpret_cast<T>(&data_[o]);
  1318. }
  1319. const uint8_t *GetAddressOf(uoffset_t o) const { return &data_[o]; }
  1320. uint8_t *GetAddressOf(uoffset_t o) { return &data_[o]; }
  1321. private:
  1322. uint8_t data_[1];
  1323. };
  1324. // "tables" use an offset table (possibly shared) that allows fields to be
  1325. // omitted and added at will, but uses an extra indirection to read.
  1326. class Table {
  1327. public:
  1328. const uint8_t *GetVTable() const {
  1329. return data_ - ReadScalar<soffset_t>(data_);
  1330. }
  1331. // This gets the field offset for any of the functions below it, or 0
  1332. // if the field was not present.
  1333. voffset_t GetOptionalFieldOffset(voffset_t field) const {
  1334. // The vtable offset is always at the start.
  1335. auto vtable = GetVTable();
  1336. // The first element is the size of the vtable (fields + type id + itself).
  1337. auto vtsize = ReadScalar<voffset_t>(vtable);
  1338. // If the field we're accessing is outside the vtable, we're reading older
  1339. // data, so it's the same as if the offset was 0 (not present).
  1340. return field < vtsize ? ReadScalar<voffset_t>(vtable + field) : 0;
  1341. }
  1342. template<typename T> T GetField(voffset_t field, T defaultval) const {
  1343. auto field_offset = GetOptionalFieldOffset(field);
  1344. return field_offset ? ReadScalar<T>(data_ + field_offset) : defaultval;
  1345. }
  1346. template<typename P> P GetPointer(voffset_t field) {
  1347. auto field_offset = GetOptionalFieldOffset(field);
  1348. auto p = data_ + field_offset;
  1349. return field_offset
  1350. ? reinterpret_cast<P>(p + ReadScalar<uoffset_t>(p))
  1351. : nullptr;
  1352. }
  1353. template<typename P> P GetPointer(voffset_t field) const {
  1354. return const_cast<Table *>(this)->GetPointer<P>(field);
  1355. }
  1356. template<typename P> P GetStruct(voffset_t field) const {
  1357. auto field_offset = GetOptionalFieldOffset(field);
  1358. auto p = const_cast<uint8_t *>(data_ + field_offset);
  1359. return field_offset ? reinterpret_cast<P>(p) : nullptr;
  1360. }
  1361. template<typename T> bool SetField(voffset_t field, T val) {
  1362. auto field_offset = GetOptionalFieldOffset(field);
  1363. if (!field_offset) return false;
  1364. WriteScalar(data_ + field_offset, val);
  1365. return true;
  1366. }
  1367. bool SetPointer(voffset_t field, const uint8_t *val) {
  1368. auto field_offset = GetOptionalFieldOffset(field);
  1369. if (!field_offset) return false;
  1370. WriteScalar(data_ + field_offset,
  1371. static_cast<uoffset_t>(val - (data_ + field_offset)));
  1372. return true;
  1373. }
  1374. uint8_t *GetAddressOf(voffset_t field) {
  1375. auto field_offset = GetOptionalFieldOffset(field);
  1376. return field_offset ? data_ + field_offset : nullptr;
  1377. }
  1378. const uint8_t *GetAddressOf(voffset_t field) const {
  1379. return const_cast<Table *>(this)->GetAddressOf(field);
  1380. }
  1381. bool CheckField(voffset_t field) const {
  1382. return GetOptionalFieldOffset(field) != 0;
  1383. }
  1384. // Verify the vtable of this table.
  1385. // Call this once per table, followed by VerifyField once per field.
  1386. bool VerifyTableStart(Verifier &verifier) const {
  1387. // Check the vtable offset.
  1388. if (!verifier.Verify<soffset_t>(data_)) return false;
  1389. auto vtable = GetVTable();
  1390. // Check the vtable size field, then check vtable fits in its entirety.
  1391. return verifier.VerifyComplexity() &&
  1392. verifier.Verify<voffset_t>(vtable) &&
  1393. (ReadScalar<voffset_t>(vtable) & (sizeof(voffset_t) - 1)) == 0 &&
  1394. verifier.Verify(vtable, ReadScalar<voffset_t>(vtable));
  1395. }
  1396. // Verify a particular field.
  1397. template<typename T> bool VerifyField(const Verifier &verifier,
  1398. voffset_t field) const {
  1399. // Calling GetOptionalFieldOffset should be safe now thanks to
  1400. // VerifyTable().
  1401. auto field_offset = GetOptionalFieldOffset(field);
  1402. // Check the actual field.
  1403. return !field_offset || verifier.Verify<T>(data_ + field_offset);
  1404. }
  1405. // VerifyField for required fields.
  1406. template<typename T> bool VerifyFieldRequired(const Verifier &verifier,
  1407. voffset_t field) const {
  1408. auto field_offset = GetOptionalFieldOffset(field);
  1409. return verifier.Check(field_offset != 0) &&
  1410. verifier.Verify<T>(data_ + field_offset);
  1411. }
  1412. private:
  1413. // private constructor & copy constructor: you obtain instances of this
  1414. // class by pointing to existing data only
  1415. Table();
  1416. Table(const Table &other);
  1417. uint8_t data_[1];
  1418. };
  1419. /// @brief This can compute the start of a FlatBuffer from a root pointer, i.e.
  1420. /// it is the opposite transformation of GetRoot().
  1421. /// This may be useful if you want to pass on a root and have the recipient
  1422. /// delete the buffer afterwards.
  1423. inline const uint8_t *GetBufferStartFromRootPointer(const void *root) {
  1424. auto table = reinterpret_cast<const Table *>(root);
  1425. auto vtable = table->GetVTable();
  1426. // Either the vtable is before the root or after the root.
  1427. auto start = std::min(vtable, reinterpret_cast<const uint8_t *>(root));
  1428. // Align to at least sizeof(uoffset_t).
  1429. start = reinterpret_cast<const uint8_t *>(
  1430. reinterpret_cast<uintptr_t>(start) & ~(sizeof(uoffset_t) - 1));
  1431. // Additionally, there may be a file_identifier in the buffer, and the root
  1432. // offset. The buffer may have been aligned to any size between
  1433. // sizeof(uoffset_t) and FLATBUFFERS_MAX_ALIGNMENT (see "force_align").
  1434. // Sadly, the exact alignment is only known when constructing the buffer,
  1435. // since it depends on the presence of values with said alignment properties.
  1436. // So instead, we simply look at the next uoffset_t values (root,
  1437. // file_identifier, and alignment padding) to see which points to the root.
  1438. // None of the other values can "impersonate" the root since they will either
  1439. // be 0 or four ASCII characters.
  1440. static_assert(FlatBufferBuilder::kFileIdentifierLength == sizeof(uoffset_t),
  1441. "file_identifier is assumed to be the same size as uoffset_t");
  1442. for (auto possible_roots = FLATBUFFERS_MAX_ALIGNMENT / sizeof(uoffset_t) + 1;
  1443. possible_roots;
  1444. possible_roots--) {
  1445. start -= sizeof(uoffset_t);
  1446. if (ReadScalar<uoffset_t>(start) + start ==
  1447. reinterpret_cast<const uint8_t *>(root)) return start;
  1448. }
  1449. // We didn't find the root, either the "root" passed isn't really a root,
  1450. // or the buffer is corrupt.
  1451. // Assert, because calling this function with bad data may cause reads
  1452. // outside of buffer boundaries.
  1453. assert(false);
  1454. return nullptr;
  1455. }
  1456. // Base class for native objects (FlatBuffer data de-serialized into native
  1457. // C++ data structures).
  1458. // Contains no functionality, purely documentative.
  1459. struct NativeTable {
  1460. };
  1461. /// @brief Function types to be used with resolving hashes into objects and
  1462. /// back again. The resolver gets a pointer to a field inside an object API
  1463. /// object that is of the type specified in the schema using the attribute
  1464. /// `cpp_type` (it is thus important whatever you write to this address
  1465. /// matches that type). The value of this field is initially null, so you
  1466. /// may choose to implement a delayed binding lookup using this function
  1467. /// if you wish. The resolver does the opposite lookup, for when the object
  1468. /// is being serialized again.
  1469. typedef uint64_t hash_value_t;
  1470. #ifdef FLATBUFFERS_CPP98_STL
  1471. typedef void (*resolver_function_t)(void **pointer_adr, hash_value_t hash);
  1472. typedef hash_value_t (*rehasher_function_t)(void *pointer);
  1473. #else
  1474. typedef std::function<void (void **pointer_adr, hash_value_t hash)>
  1475. resolver_function_t;
  1476. typedef std::function<hash_value_t (void *pointer)> rehasher_function_t;
  1477. #endif
  1478. // Helper function to test if a field is present, using any of the field
  1479. // enums in the generated code.
  1480. // `table` must be a generated table type. Since this is a template parameter,
  1481. // this is not typechecked to be a subclass of Table, so beware!
  1482. // Note: this function will return false for fields equal to the default
  1483. // value, since they're not stored in the buffer (unless force_defaults was
  1484. // used).
  1485. template<typename T> bool IsFieldPresent(const T *table, voffset_t field) {
  1486. // Cast, since Table is a private baseclass of any table types.
  1487. return reinterpret_cast<const Table *>(table)->CheckField(field);
  1488. }
  1489. // Utility function for reverse lookups on the EnumNames*() functions
  1490. // (in the generated C++ code)
  1491. // names must be NULL terminated.
  1492. inline int LookupEnum(const char **names, const char *name) {
  1493. for (const char **p = names; *p; p++)
  1494. if (!strcmp(*p, name))
  1495. return static_cast<int>(p - names);
  1496. return -1;
  1497. }
  1498. // These macros allow us to layout a struct with a guarantee that they'll end
  1499. // up looking the same on different compilers and platforms.
  1500. // It does this by disallowing the compiler to do any padding, and then
  1501. // does padding itself by inserting extra padding fields that make every
  1502. // element aligned to its own size.
  1503. // Additionally, it manually sets the alignment of the struct as a whole,
  1504. // which is typically its largest element, or a custom size set in the schema
  1505. // by the force_align attribute.
  1506. // These are used in the generated code only.
  1507. #if defined(_MSC_VER)
  1508. #define MANUALLY_ALIGNED_STRUCT(alignment) \
  1509. __pragma(pack(1)); \
  1510. struct __declspec(align(alignment))
  1511. #define STRUCT_END(name, size) \
  1512. __pragma(pack()); \
  1513. static_assert(sizeof(name) == size, "compiler breaks packing rules")
  1514. #elif defined(__GNUC__) || defined(__clang__)
  1515. #define MANUALLY_ALIGNED_STRUCT(alignment) \
  1516. _Pragma("pack(1)") \
  1517. struct __attribute__((aligned(alignment)))
  1518. #define STRUCT_END(name, size) \
  1519. _Pragma("pack()") \
  1520. static_assert(sizeof(name) == size, "compiler breaks packing rules")
  1521. #else
  1522. #error Unknown compiler, please define structure alignment macros
  1523. #endif
  1524. // String which identifies the current version of FlatBuffers.
  1525. // flatbuffer_version_string is used by Google developers to identify which
  1526. // applications uploaded to Google Play are using this library. This allows
  1527. // the development team at Google to determine the popularity of the library.
  1528. // How it works: Applications that are uploaded to the Google Play Store are
  1529. // scanned for this version string. We track which applications are using it
  1530. // to measure popularity. You are free to remove it (of course) but we would
  1531. // appreciate if you left it in.
  1532. // Weak linkage is culled by VS & doesn't work on cygwin.
  1533. #if !defined(_WIN32) && !defined(__CYGWIN__)
  1534. extern volatile __attribute__((weak)) const char *flatbuffer_version_string;
  1535. volatile __attribute__((weak)) const char *flatbuffer_version_string =
  1536. "FlatBuffers "
  1537. FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
  1538. FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
  1539. FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
  1540. #endif // !defined(_WIN32) && !defined(__CYGWIN__)
  1541. #define DEFINE_BITMASK_OPERATORS(E, T)\
  1542. inline E operator | (E lhs, E rhs){\
  1543. return E(T(lhs) | T(rhs));\
  1544. }\
  1545. inline E operator & (E lhs, E rhs){\
  1546. return E(T(lhs) & T(rhs));\
  1547. }\
  1548. inline E operator ^ (E lhs, E rhs){\
  1549. return E(T(lhs) ^ T(rhs));\
  1550. }\
  1551. inline E operator ~ (E lhs){\
  1552. return E(~T(lhs));\
  1553. }\
  1554. inline E operator |= (E &lhs, E rhs){\
  1555. lhs = lhs | rhs;\
  1556. return lhs;\
  1557. }\
  1558. inline E operator &= (E &lhs, E rhs){\
  1559. lhs = lhs & rhs;\
  1560. return lhs;\
  1561. }\
  1562. inline E operator ^= (E &lhs, E rhs){\
  1563. lhs = lhs ^ rhs;\
  1564. return lhs;\
  1565. }\
  1566. inline bool operator !(E rhs) \
  1567. {\
  1568. return !bool(T(rhs)); \
  1569. }
  1570. /// @endcond
  1571. } // namespace flatbuffers
  1572. #endif // FLATBUFFERS_H_