flexbuffers.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. /*
  2. * Copyright 2017 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_FLEXBUFFERS_H_
  17. #define FLATBUFFERS_FLEXBUFFERS_H_
  18. #include <map>
  19. // Used to select STL variant.
  20. #include "packages/TensorflowLiteMicro/flatbuffers/base.h"
  21. // We use the basic binary writing functions from the regular FlatBuffers.
  22. #include "packages/TensorflowLiteMicro/flatbuffers/util.h"
  23. #ifdef _MSC_VER
  24. # include <intrin.h>
  25. #endif
  26. #if defined(_MSC_VER)
  27. # pragma warning(push)
  28. # pragma warning(disable : 4127) // C4127: conditional expression is constant
  29. #endif
  30. namespace flexbuffers {
  31. class Reference;
  32. class Map;
  33. // These are used in the lower 2 bits of a type field to determine the size of
  34. // the elements (and or size field) of the item pointed to (e.g. vector).
  35. enum BitWidth {
  36. BIT_WIDTH_8 = 0,
  37. BIT_WIDTH_16 = 1,
  38. BIT_WIDTH_32 = 2,
  39. BIT_WIDTH_64 = 3,
  40. };
  41. // These are used as the upper 6 bits of a type field to indicate the actual
  42. // type.
  43. enum Type {
  44. FBT_NULL = 0,
  45. FBT_INT = 1,
  46. FBT_UINT = 2,
  47. FBT_FLOAT = 3,
  48. // Types above stored inline, types below store an offset.
  49. FBT_KEY = 4,
  50. FBT_STRING = 5,
  51. FBT_INDIRECT_INT = 6,
  52. FBT_INDIRECT_UINT = 7,
  53. FBT_INDIRECT_FLOAT = 8,
  54. FBT_MAP = 9,
  55. FBT_VECTOR = 10, // Untyped.
  56. FBT_VECTOR_INT = 11, // Typed any size (stores no type table).
  57. FBT_VECTOR_UINT = 12,
  58. FBT_VECTOR_FLOAT = 13,
  59. FBT_VECTOR_KEY = 14,
  60. // DEPRECATED, use FBT_VECTOR or FBT_VECTOR_KEY instead.
  61. // Read test.cpp/FlexBuffersDeprecatedTest() for details on why.
  62. FBT_VECTOR_STRING_DEPRECATED = 15,
  63. FBT_VECTOR_INT2 = 16, // Typed tuple (no type table, no size field).
  64. FBT_VECTOR_UINT2 = 17,
  65. FBT_VECTOR_FLOAT2 = 18,
  66. FBT_VECTOR_INT3 = 19, // Typed triple (no type table, no size field).
  67. FBT_VECTOR_UINT3 = 20,
  68. FBT_VECTOR_FLOAT3 = 21,
  69. FBT_VECTOR_INT4 = 22, // Typed quad (no type table, no size field).
  70. FBT_VECTOR_UINT4 = 23,
  71. FBT_VECTOR_FLOAT4 = 24,
  72. FBT_BLOB = 25,
  73. FBT_BOOL = 26,
  74. FBT_VECTOR_BOOL =
  75. 36, // To Allow the same type of conversion of type to vector type
  76. };
  77. inline bool IsInline(Type t) { return t <= FBT_FLOAT || t == FBT_BOOL; }
  78. inline bool IsTypedVectorElementType(Type t) {
  79. return (t >= FBT_INT && t <= FBT_STRING) || t == FBT_BOOL;
  80. }
  81. inline bool IsTypedVector(Type t) {
  82. return (t >= FBT_VECTOR_INT && t <= FBT_VECTOR_STRING_DEPRECATED) ||
  83. t == FBT_VECTOR_BOOL;
  84. }
  85. inline bool IsFixedTypedVector(Type t) {
  86. return t >= FBT_VECTOR_INT2 && t <= FBT_VECTOR_FLOAT4;
  87. }
  88. inline Type ToTypedVector(Type t, size_t fixed_len = 0) {
  89. FLATBUFFERS_ASSERT(IsTypedVectorElementType(t));
  90. switch (fixed_len) {
  91. case 0: return static_cast<Type>(t - FBT_INT + FBT_VECTOR_INT);
  92. case 2: return static_cast<Type>(t - FBT_INT + FBT_VECTOR_INT2);
  93. case 3: return static_cast<Type>(t - FBT_INT + FBT_VECTOR_INT3);
  94. case 4: return static_cast<Type>(t - FBT_INT + FBT_VECTOR_INT4);
  95. default: FLATBUFFERS_ASSERT(0); return FBT_NULL;
  96. }
  97. }
  98. inline Type ToTypedVectorElementType(Type t) {
  99. FLATBUFFERS_ASSERT(IsTypedVector(t));
  100. return static_cast<Type>(t - FBT_VECTOR_INT + FBT_INT);
  101. }
  102. inline Type ToFixedTypedVectorElementType(Type t, uint8_t *len) {
  103. FLATBUFFERS_ASSERT(IsFixedTypedVector(t));
  104. auto fixed_type = t - FBT_VECTOR_INT2;
  105. *len = static_cast<uint8_t>(fixed_type / 3 +
  106. 2); // 3 types each, starting from length 2.
  107. return static_cast<Type>(fixed_type % 3 + FBT_INT);
  108. }
  109. // TODO: implement proper support for 8/16bit floats, or decide not to
  110. // support them.
  111. typedef int16_t half;
  112. typedef int8_t quarter;
  113. // TODO: can we do this without conditionals using intrinsics or inline asm
  114. // on some platforms? Given branch prediction the method below should be
  115. // decently quick, but it is the most frequently executed function.
  116. // We could do an (unaligned) 64-bit read if we ifdef out the platforms for
  117. // which that doesn't work (or where we'd read into un-owned memory).
  118. template<typename R, typename T1, typename T2, typename T4, typename T8>
  119. R ReadSizedScalar(const uint8_t *data, uint8_t byte_width) {
  120. return byte_width < 4
  121. ? (byte_width < 2
  122. ? static_cast<R>(flatbuffers::ReadScalar<T1>(data))
  123. : static_cast<R>(flatbuffers::ReadScalar<T2>(data)))
  124. : (byte_width < 8
  125. ? static_cast<R>(flatbuffers::ReadScalar<T4>(data))
  126. : static_cast<R>(flatbuffers::ReadScalar<T8>(data)));
  127. }
  128. inline int64_t ReadInt64(const uint8_t *data, uint8_t byte_width) {
  129. return ReadSizedScalar<int64_t, int8_t, int16_t, int32_t, int64_t>(
  130. data, byte_width);
  131. }
  132. inline uint64_t ReadUInt64(const uint8_t *data, uint8_t byte_width) {
  133. // This is the "hottest" function (all offset lookups use this), so worth
  134. // optimizing if possible.
  135. // TODO: GCC apparently replaces memcpy by a rep movsb, but only if count is a
  136. // constant, which here it isn't. Test if memcpy is still faster than
  137. // the conditionals in ReadSizedScalar. Can also use inline asm.
  138. // clang-format off
  139. #if defined(_MSC_VER) && (defined(_M_X64) || defined _M_IX86)
  140. uint64_t u = 0;
  141. __movsb(reinterpret_cast<uint8_t *>(&u),
  142. reinterpret_cast<const uint8_t *>(data), byte_width);
  143. return flatbuffers::EndianScalar(u);
  144. #else
  145. return ReadSizedScalar<uint64_t, uint8_t, uint16_t, uint32_t, uint64_t>(
  146. data, byte_width);
  147. #endif
  148. // clang-format on
  149. }
  150. inline double ReadDouble(const uint8_t *data, uint8_t byte_width) {
  151. return ReadSizedScalar<double, quarter, half, float, double>(data,
  152. byte_width);
  153. }
  154. inline const uint8_t *Indirect(const uint8_t *offset, uint8_t byte_width) {
  155. return offset - ReadUInt64(offset, byte_width);
  156. }
  157. template<typename T> const uint8_t *Indirect(const uint8_t *offset) {
  158. return offset - flatbuffers::ReadScalar<T>(offset);
  159. }
  160. inline BitWidth WidthU(uint64_t u) {
  161. #define FLATBUFFERS_GET_FIELD_BIT_WIDTH(value, width) \
  162. { \
  163. if (!((u) & ~((1ULL << (width)) - 1ULL))) return BIT_WIDTH_##width; \
  164. }
  165. FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 8);
  166. FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 16);
  167. FLATBUFFERS_GET_FIELD_BIT_WIDTH(u, 32);
  168. #undef FLATBUFFERS_GET_FIELD_BIT_WIDTH
  169. return BIT_WIDTH_64;
  170. }
  171. inline BitWidth WidthI(int64_t i) {
  172. auto u = static_cast<uint64_t>(i) << 1;
  173. return WidthU(i >= 0 ? u : ~u);
  174. }
  175. inline BitWidth WidthF(double f) {
  176. return static_cast<double>(static_cast<float>(f)) == f ? BIT_WIDTH_32
  177. : BIT_WIDTH_64;
  178. }
  179. // Base class of all types below.
  180. // Points into the data buffer and allows access to one type.
  181. class Object {
  182. public:
  183. Object(const uint8_t *data, uint8_t byte_width)
  184. : data_(data), byte_width_(byte_width) {}
  185. protected:
  186. const uint8_t *data_;
  187. uint8_t byte_width_;
  188. };
  189. // Object that has a size, obtained either from size prefix, or elsewhere.
  190. class Sized : public Object {
  191. public:
  192. // Size prefix.
  193. Sized(const uint8_t *data, uint8_t byte_width)
  194. : Object(data, byte_width), size_(read_size()) {}
  195. // Manual size.
  196. Sized(const uint8_t *data, uint8_t byte_width, size_t sz)
  197. : Object(data, byte_width), size_(sz) {}
  198. size_t size() const { return size_; }
  199. // Access size stored in `byte_width_` bytes before data_ pointer.
  200. size_t read_size() const {
  201. return static_cast<size_t>(ReadUInt64(data_ - byte_width_, byte_width_));
  202. }
  203. protected:
  204. size_t size_;
  205. };
  206. class String : public Sized {
  207. public:
  208. // Size prefix.
  209. String(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
  210. // Manual size.
  211. String(const uint8_t *data, uint8_t byte_width, size_t sz)
  212. : Sized(data, byte_width, sz) {}
  213. size_t length() const { return size(); }
  214. const char *c_str() const { return reinterpret_cast<const char *>(data_); }
  215. std::string str() const { return std::string(c_str(), size()); }
  216. static String EmptyString() {
  217. static const char *empty_string = "";
  218. return String(reinterpret_cast<const uint8_t *>(empty_string), 1, 0);
  219. }
  220. bool IsTheEmptyString() const { return data_ == EmptyString().data_; }
  221. };
  222. class Blob : public Sized {
  223. public:
  224. Blob(const uint8_t *data_buf, uint8_t byte_width)
  225. : Sized(data_buf, byte_width) {}
  226. static Blob EmptyBlob() {
  227. static const uint8_t empty_blob[] = { 0 /*len*/ };
  228. return Blob(empty_blob + 1, 1);
  229. }
  230. bool IsTheEmptyBlob() const { return data_ == EmptyBlob().data_; }
  231. const uint8_t *data() const { return data_; }
  232. };
  233. class Vector : public Sized {
  234. public:
  235. Vector(const uint8_t *data, uint8_t byte_width) : Sized(data, byte_width) {}
  236. Reference operator[](size_t i) const;
  237. static Vector EmptyVector() {
  238. static const uint8_t empty_vector[] = { 0 /*len*/ };
  239. return Vector(empty_vector + 1, 1);
  240. }
  241. bool IsTheEmptyVector() const { return data_ == EmptyVector().data_; }
  242. };
  243. class TypedVector : public Sized {
  244. public:
  245. TypedVector(const uint8_t *data, uint8_t byte_width, Type element_type)
  246. : Sized(data, byte_width), type_(element_type) {}
  247. Reference operator[](size_t i) const;
  248. static TypedVector EmptyTypedVector() {
  249. static const uint8_t empty_typed_vector[] = { 0 /*len*/ };
  250. return TypedVector(empty_typed_vector + 1, 1, FBT_INT);
  251. }
  252. bool IsTheEmptyVector() const {
  253. return data_ == TypedVector::EmptyTypedVector().data_;
  254. }
  255. Type ElementType() { return type_; }
  256. friend Reference;
  257. private:
  258. Type type_;
  259. friend Map;
  260. };
  261. class FixedTypedVector : public Object {
  262. public:
  263. FixedTypedVector(const uint8_t *data, uint8_t byte_width, Type element_type,
  264. uint8_t len)
  265. : Object(data, byte_width), type_(element_type), len_(len) {}
  266. Reference operator[](size_t i) const;
  267. static FixedTypedVector EmptyFixedTypedVector() {
  268. static const uint8_t fixed_empty_vector[] = { 0 /* unused */ };
  269. return FixedTypedVector(fixed_empty_vector, 1, FBT_INT, 0);
  270. }
  271. bool IsTheEmptyFixedTypedVector() const {
  272. return data_ == FixedTypedVector::EmptyFixedTypedVector().data_;
  273. }
  274. Type ElementType() { return type_; }
  275. uint8_t size() { return len_; }
  276. private:
  277. Type type_;
  278. uint8_t len_;
  279. };
  280. class Map : public Vector {
  281. public:
  282. Map(const uint8_t *data, uint8_t byte_width) : Vector(data, byte_width) {}
  283. Reference operator[](const char *key) const;
  284. Reference operator[](const std::string &key) const;
  285. Vector Values() const { return Vector(data_, byte_width_); }
  286. TypedVector Keys() const {
  287. const size_t num_prefixed_fields = 3;
  288. auto keys_offset = data_ - byte_width_ * num_prefixed_fields;
  289. return TypedVector(Indirect(keys_offset, byte_width_),
  290. static_cast<uint8_t>(
  291. ReadUInt64(keys_offset + byte_width_, byte_width_)),
  292. FBT_KEY);
  293. }
  294. static Map EmptyMap() {
  295. static const uint8_t empty_map[] = {
  296. 0 /*keys_len*/, 0 /*keys_offset*/, 1 /*keys_width*/, 0 /*len*/
  297. };
  298. return Map(empty_map + 4, 1);
  299. }
  300. bool IsTheEmptyMap() const { return data_ == EmptyMap().data_; }
  301. };
  302. template<typename T>
  303. void AppendToString(std::string &s, T &&v, bool keys_quoted) {
  304. s += "[ ";
  305. for (size_t i = 0; i < v.size(); i++) {
  306. if (i) s += ", ";
  307. v[i].ToString(true, keys_quoted, s);
  308. }
  309. s += " ]";
  310. }
  311. class Reference {
  312. public:
  313. Reference()
  314. : data_(nullptr),
  315. parent_width_(0),
  316. byte_width_(BIT_WIDTH_8),
  317. type_(FBT_NULL) {}
  318. Reference(const uint8_t *data, uint8_t parent_width, uint8_t byte_width,
  319. Type type)
  320. : data_(data),
  321. parent_width_(parent_width),
  322. byte_width_(byte_width),
  323. type_(type) {}
  324. Reference(const uint8_t *data, uint8_t parent_width, uint8_t packed_type)
  325. : data_(data), parent_width_(parent_width) {
  326. byte_width_ = 1U << static_cast<BitWidth>(packed_type & 3);
  327. type_ = static_cast<Type>(packed_type >> 2);
  328. }
  329. Type GetType() const { return type_; }
  330. bool IsNull() const { return type_ == FBT_NULL; }
  331. bool IsBool() const { return type_ == FBT_BOOL; }
  332. bool IsInt() const { return type_ == FBT_INT || type_ == FBT_INDIRECT_INT; }
  333. bool IsUInt() const {
  334. return type_ == FBT_UINT || type_ == FBT_INDIRECT_UINT;
  335. }
  336. bool IsIntOrUint() const { return IsInt() || IsUInt(); }
  337. bool IsFloat() const {
  338. return type_ == FBT_FLOAT || type_ == FBT_INDIRECT_FLOAT;
  339. }
  340. bool IsNumeric() const { return IsIntOrUint() || IsFloat(); }
  341. bool IsString() const { return type_ == FBT_STRING; }
  342. bool IsKey() const { return type_ == FBT_KEY; }
  343. bool IsVector() const { return type_ == FBT_VECTOR || type_ == FBT_MAP; }
  344. bool IsUntypedVector() const { return type_ == FBT_VECTOR; }
  345. bool IsTypedVector() const { return flexbuffers::IsTypedVector(type_); }
  346. bool IsFixedTypedVector() const {
  347. return flexbuffers::IsFixedTypedVector(type_);
  348. }
  349. bool IsAnyVector() const {
  350. return (IsTypedVector() || IsFixedTypedVector() || IsVector());
  351. }
  352. bool IsMap() const { return type_ == FBT_MAP; }
  353. bool IsBlob() const { return type_ == FBT_BLOB; }
  354. bool AsBool() const {
  355. return (type_ == FBT_BOOL ? ReadUInt64(data_, parent_width_)
  356. : AsUInt64()) != 0;
  357. }
  358. // Reads any type as a int64_t. Never fails, does most sensible conversion.
  359. // Truncates floats, strings are attempted to be parsed for a number,
  360. // vectors/maps return their size. Returns 0 if all else fails.
  361. int64_t AsInt64() const {
  362. if (type_ == FBT_INT) {
  363. // A fast path for the common case.
  364. return ReadInt64(data_, parent_width_);
  365. } else
  366. switch (type_) {
  367. case FBT_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
  368. case FBT_UINT: return ReadUInt64(data_, parent_width_);
  369. case FBT_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
  370. case FBT_FLOAT:
  371. return static_cast<int64_t>(ReadDouble(data_, parent_width_));
  372. case FBT_INDIRECT_FLOAT:
  373. return static_cast<int64_t>(ReadDouble(Indirect(), byte_width_));
  374. case FBT_NULL: return 0;
  375. case FBT_STRING: return flatbuffers::StringToInt(AsString().c_str());
  376. case FBT_VECTOR: return static_cast<int64_t>(AsVector().size());
  377. case FBT_BOOL: return ReadInt64(data_, parent_width_);
  378. default:
  379. // Convert other things to int.
  380. return 0;
  381. }
  382. }
  383. // TODO: could specialize these to not use AsInt64() if that saves
  384. // extension ops in generated code, and use a faster op than ReadInt64.
  385. int32_t AsInt32() const { return static_cast<int32_t>(AsInt64()); }
  386. int16_t AsInt16() const { return static_cast<int16_t>(AsInt64()); }
  387. int8_t AsInt8() const { return static_cast<int8_t>(AsInt64()); }
  388. uint64_t AsUInt64() const {
  389. if (type_ == FBT_UINT) {
  390. // A fast path for the common case.
  391. return ReadUInt64(data_, parent_width_);
  392. } else
  393. switch (type_) {
  394. case FBT_INDIRECT_UINT: return ReadUInt64(Indirect(), byte_width_);
  395. case FBT_INT: return ReadInt64(data_, parent_width_);
  396. case FBT_INDIRECT_INT: return ReadInt64(Indirect(), byte_width_);
  397. case FBT_FLOAT:
  398. return static_cast<uint64_t>(ReadDouble(data_, parent_width_));
  399. case FBT_INDIRECT_FLOAT:
  400. return static_cast<uint64_t>(ReadDouble(Indirect(), byte_width_));
  401. case FBT_NULL: return 0;
  402. case FBT_STRING: return flatbuffers::StringToUInt(AsString().c_str());
  403. case FBT_VECTOR: return static_cast<uint64_t>(AsVector().size());
  404. case FBT_BOOL: return ReadUInt64(data_, parent_width_);
  405. default:
  406. // Convert other things to uint.
  407. return 0;
  408. }
  409. }
  410. uint32_t AsUInt32() const { return static_cast<uint32_t>(AsUInt64()); }
  411. uint16_t AsUInt16() const { return static_cast<uint16_t>(AsUInt64()); }
  412. uint8_t AsUInt8() const { return static_cast<uint8_t>(AsUInt64()); }
  413. double AsDouble() const {
  414. if (type_ == FBT_FLOAT) {
  415. // A fast path for the common case.
  416. return ReadDouble(data_, parent_width_);
  417. } else
  418. switch (type_) {
  419. case FBT_INDIRECT_FLOAT: return ReadDouble(Indirect(), byte_width_);
  420. case FBT_INT:
  421. return static_cast<double>(ReadInt64(data_, parent_width_));
  422. case FBT_UINT:
  423. return static_cast<double>(ReadUInt64(data_, parent_width_));
  424. case FBT_INDIRECT_INT:
  425. return static_cast<double>(ReadInt64(Indirect(), byte_width_));
  426. case FBT_INDIRECT_UINT:
  427. return static_cast<double>(ReadUInt64(Indirect(), byte_width_));
  428. case FBT_NULL: return 0.0;
  429. case FBT_STRING: {
  430. double d;
  431. flatbuffers::StringToNumber(AsString().c_str(), &d);
  432. return d;
  433. }
  434. case FBT_VECTOR: return static_cast<double>(AsVector().size());
  435. case FBT_BOOL:
  436. return static_cast<double>(ReadUInt64(data_, parent_width_));
  437. default:
  438. // Convert strings and other things to float.
  439. return 0;
  440. }
  441. }
  442. float AsFloat() const { return static_cast<float>(AsDouble()); }
  443. const char *AsKey() const {
  444. if (type_ == FBT_KEY || type_ == FBT_STRING) {
  445. return reinterpret_cast<const char *>(Indirect());
  446. } else {
  447. return "";
  448. }
  449. }
  450. // This function returns the empty string if you try to read something that
  451. // is not a string or key.
  452. String AsString() const {
  453. if (type_ == FBT_STRING) {
  454. return String(Indirect(), byte_width_);
  455. } else if (type_ == FBT_KEY) {
  456. auto key = Indirect();
  457. return String(key, byte_width_,
  458. strlen(reinterpret_cast<const char *>(key)));
  459. } else {
  460. return String::EmptyString();
  461. }
  462. }
  463. // Unlike AsString(), this will convert any type to a std::string.
  464. std::string ToString() const {
  465. std::string s;
  466. ToString(false, false, s);
  467. return s;
  468. }
  469. // Convert any type to a JSON-like string. strings_quoted determines if
  470. // string values at the top level receive "" quotes (inside other values
  471. // they always do). keys_quoted determines if keys are quoted, at any level.
  472. // TODO(wvo): add further options to have indentation/newlines.
  473. void ToString(bool strings_quoted, bool keys_quoted, std::string &s) const {
  474. if (type_ == FBT_STRING) {
  475. String str(Indirect(), byte_width_);
  476. if (strings_quoted) {
  477. flatbuffers::EscapeString(str.c_str(), str.length(), &s, true, false);
  478. } else {
  479. s.append(str.c_str(), str.length());
  480. }
  481. } else if (IsKey()) {
  482. auto str = AsKey();
  483. if (keys_quoted) {
  484. flatbuffers::EscapeString(str, strlen(str), &s, true, false);
  485. } else {
  486. s += str;
  487. }
  488. } else if (IsInt()) {
  489. s += flatbuffers::NumToString(AsInt64());
  490. } else if (IsUInt()) {
  491. s += flatbuffers::NumToString(AsUInt64());
  492. } else if (IsFloat()) {
  493. s += flatbuffers::NumToString(AsDouble());
  494. } else if (IsNull()) {
  495. s += "null";
  496. } else if (IsBool()) {
  497. s += AsBool() ? "true" : "false";
  498. } else if (IsMap()) {
  499. s += "{ ";
  500. auto m = AsMap();
  501. auto keys = m.Keys();
  502. auto vals = m.Values();
  503. for (size_t i = 0; i < keys.size(); i++) {
  504. keys[i].ToString(true, keys_quoted, s);
  505. s += ": ";
  506. vals[i].ToString(true, keys_quoted, s);
  507. if (i < keys.size() - 1) s += ", ";
  508. }
  509. s += " }";
  510. } else if (IsVector()) {
  511. AppendToString<Vector>(s, AsVector(), keys_quoted);
  512. } else if (IsTypedVector()) {
  513. AppendToString<TypedVector>(s, AsTypedVector(), keys_quoted);
  514. } else if (IsFixedTypedVector()) {
  515. AppendToString<FixedTypedVector>(s, AsFixedTypedVector(), keys_quoted);
  516. } else if (IsBlob()) {
  517. auto blob = AsBlob();
  518. flatbuffers::EscapeString(reinterpret_cast<const char *>(blob.data()),
  519. blob.size(), &s, true, false);
  520. } else {
  521. s += "(?)";
  522. }
  523. }
  524. // This function returns the empty blob if you try to read a not-blob.
  525. // Strings can be viewed as blobs too.
  526. Blob AsBlob() const {
  527. if (type_ == FBT_BLOB || type_ == FBT_STRING) {
  528. return Blob(Indirect(), byte_width_);
  529. } else {
  530. return Blob::EmptyBlob();
  531. }
  532. }
  533. // This function returns the empty vector if you try to read a not-vector.
  534. // Maps can be viewed as vectors too.
  535. Vector AsVector() const {
  536. if (type_ == FBT_VECTOR || type_ == FBT_MAP) {
  537. return Vector(Indirect(), byte_width_);
  538. } else {
  539. return Vector::EmptyVector();
  540. }
  541. }
  542. TypedVector AsTypedVector() const {
  543. if (IsTypedVector()) {
  544. auto tv =
  545. TypedVector(Indirect(), byte_width_, ToTypedVectorElementType(type_));
  546. if (tv.type_ == FBT_STRING) {
  547. // These can't be accessed as strings, since we don't know the bit-width
  548. // of the size field, see the declaration of
  549. // FBT_VECTOR_STRING_DEPRECATED above for details.
  550. // We change the type here to be keys, which are a subtype of strings,
  551. // and will ignore the size field. This will truncate strings with
  552. // embedded nulls.
  553. tv.type_ = FBT_KEY;
  554. }
  555. return tv;
  556. } else {
  557. return TypedVector::EmptyTypedVector();
  558. }
  559. }
  560. FixedTypedVector AsFixedTypedVector() const {
  561. if (IsFixedTypedVector()) {
  562. uint8_t len = 0;
  563. auto vtype = ToFixedTypedVectorElementType(type_, &len);
  564. return FixedTypedVector(Indirect(), byte_width_, vtype, len);
  565. } else {
  566. return FixedTypedVector::EmptyFixedTypedVector();
  567. }
  568. }
  569. Map AsMap() const {
  570. if (type_ == FBT_MAP) {
  571. return Map(Indirect(), byte_width_);
  572. } else {
  573. return Map::EmptyMap();
  574. }
  575. }
  576. template<typename T> T As() const;
  577. // Experimental: Mutation functions.
  578. // These allow scalars in an already created buffer to be updated in-place.
  579. // Since by default scalars are stored in the smallest possible space,
  580. // the new value may not fit, in which case these functions return false.
  581. // To avoid this, you can construct the values you intend to mutate using
  582. // Builder::ForceMinimumBitWidth.
  583. bool MutateInt(int64_t i) {
  584. if (type_ == FBT_INT) {
  585. return Mutate(data_, i, parent_width_, WidthI(i));
  586. } else if (type_ == FBT_INDIRECT_INT) {
  587. return Mutate(Indirect(), i, byte_width_, WidthI(i));
  588. } else if (type_ == FBT_UINT) {
  589. auto u = static_cast<uint64_t>(i);
  590. return Mutate(data_, u, parent_width_, WidthU(u));
  591. } else if (type_ == FBT_INDIRECT_UINT) {
  592. auto u = static_cast<uint64_t>(i);
  593. return Mutate(Indirect(), u, byte_width_, WidthU(u));
  594. } else {
  595. return false;
  596. }
  597. }
  598. bool MutateBool(bool b) {
  599. return type_ == FBT_BOOL && Mutate(data_, b, parent_width_, BIT_WIDTH_8);
  600. }
  601. bool MutateUInt(uint64_t u) {
  602. if (type_ == FBT_UINT) {
  603. return Mutate(data_, u, parent_width_, WidthU(u));
  604. } else if (type_ == FBT_INDIRECT_UINT) {
  605. return Mutate(Indirect(), u, byte_width_, WidthU(u));
  606. } else if (type_ == FBT_INT) {
  607. auto i = static_cast<int64_t>(u);
  608. return Mutate(data_, i, parent_width_, WidthI(i));
  609. } else if (type_ == FBT_INDIRECT_INT) {
  610. auto i = static_cast<int64_t>(u);
  611. return Mutate(Indirect(), i, byte_width_, WidthI(i));
  612. } else {
  613. return false;
  614. }
  615. }
  616. bool MutateFloat(float f) {
  617. if (type_ == FBT_FLOAT) {
  618. return MutateF(data_, f, parent_width_, BIT_WIDTH_32);
  619. } else if (type_ == FBT_INDIRECT_FLOAT) {
  620. return MutateF(Indirect(), f, byte_width_, BIT_WIDTH_32);
  621. } else {
  622. return false;
  623. }
  624. }
  625. bool MutateFloat(double d) {
  626. if (type_ == FBT_FLOAT) {
  627. return MutateF(data_, d, parent_width_, WidthF(d));
  628. } else if (type_ == FBT_INDIRECT_FLOAT) {
  629. return MutateF(Indirect(), d, byte_width_, WidthF(d));
  630. } else {
  631. return false;
  632. }
  633. }
  634. bool MutateString(const char *str, size_t len) {
  635. auto s = AsString();
  636. if (s.IsTheEmptyString()) return false;
  637. // This is very strict, could allow shorter strings, but that creates
  638. // garbage.
  639. if (s.length() != len) return false;
  640. memcpy(const_cast<char *>(s.c_str()), str, len);
  641. return true;
  642. }
  643. bool MutateString(const char *str) { return MutateString(str, strlen(str)); }
  644. bool MutateString(const std::string &str) {
  645. return MutateString(str.data(), str.length());
  646. }
  647. private:
  648. const uint8_t *Indirect() const {
  649. return flexbuffers::Indirect(data_, parent_width_);
  650. }
  651. template<typename T>
  652. bool Mutate(const uint8_t *dest, T t, size_t byte_width,
  653. BitWidth value_width) {
  654. auto fits = static_cast<size_t>(static_cast<size_t>(1U) << value_width) <=
  655. byte_width;
  656. if (fits) {
  657. t = flatbuffers::EndianScalar(t);
  658. memcpy(const_cast<uint8_t *>(dest), &t, byte_width);
  659. }
  660. return fits;
  661. }
  662. template<typename T>
  663. bool MutateF(const uint8_t *dest, T t, size_t byte_width,
  664. BitWidth value_width) {
  665. if (byte_width == sizeof(double))
  666. return Mutate(dest, static_cast<double>(t), byte_width, value_width);
  667. if (byte_width == sizeof(float))
  668. return Mutate(dest, static_cast<float>(t), byte_width, value_width);
  669. FLATBUFFERS_ASSERT(false);
  670. return false;
  671. }
  672. const uint8_t *data_;
  673. uint8_t parent_width_;
  674. uint8_t byte_width_;
  675. Type type_;
  676. };
  677. // Template specialization for As().
  678. template<> inline bool Reference::As<bool>() const { return AsBool(); }
  679. template<> inline int8_t Reference::As<int8_t>() const { return AsInt8(); }
  680. template<> inline int16_t Reference::As<int16_t>() const { return AsInt16(); }
  681. template<> inline int32_t Reference::As<int32_t>() const { return AsInt32(); }
  682. template<> inline int64_t Reference::As<int64_t>() const { return AsInt64(); }
  683. template<> inline uint8_t Reference::As<uint8_t>() const { return AsUInt8(); }
  684. template<> inline uint16_t Reference::As<uint16_t>() const {
  685. return AsUInt16();
  686. }
  687. template<> inline uint32_t Reference::As<uint32_t>() const {
  688. return AsUInt32();
  689. }
  690. template<> inline uint64_t Reference::As<uint64_t>() const {
  691. return AsUInt64();
  692. }
  693. template<> inline double Reference::As<double>() const { return AsDouble(); }
  694. template<> inline float Reference::As<float>() const { return AsFloat(); }
  695. template<> inline String Reference::As<String>() const { return AsString(); }
  696. template<> inline std::string Reference::As<std::string>() const {
  697. return AsString().str();
  698. }
  699. template<> inline Blob Reference::As<Blob>() const { return AsBlob(); }
  700. template<> inline Vector Reference::As<Vector>() const { return AsVector(); }
  701. template<> inline TypedVector Reference::As<TypedVector>() const {
  702. return AsTypedVector();
  703. }
  704. template<> inline FixedTypedVector Reference::As<FixedTypedVector>() const {
  705. return AsFixedTypedVector();
  706. }
  707. template<> inline Map Reference::As<Map>() const { return AsMap(); }
  708. inline uint8_t PackedType(BitWidth bit_width, Type type) {
  709. return static_cast<uint8_t>(bit_width | (type << 2));
  710. }
  711. inline uint8_t NullPackedType() { return PackedType(BIT_WIDTH_8, FBT_NULL); }
  712. // Vector accessors.
  713. // Note: if you try to access outside of bounds, you get a Null value back
  714. // instead. Normally this would be an assert, but since this is "dynamically
  715. // typed" data, you may not want that (someone sends you a 2d vector and you
  716. // wanted 3d).
  717. // The Null converts seamlessly into a default value for any other type.
  718. // TODO(wvo): Could introduce an #ifdef that makes this into an assert?
  719. inline Reference Vector::operator[](size_t i) const {
  720. auto len = size();
  721. if (i >= len) return Reference(nullptr, 1, NullPackedType());
  722. auto packed_type = (data_ + len * byte_width_)[i];
  723. auto elem = data_ + i * byte_width_;
  724. return Reference(elem, byte_width_, packed_type);
  725. }
  726. inline Reference TypedVector::operator[](size_t i) const {
  727. auto len = size();
  728. if (i >= len) return Reference(nullptr, 1, NullPackedType());
  729. auto elem = data_ + i * byte_width_;
  730. return Reference(elem, byte_width_, 1, type_);
  731. }
  732. inline Reference FixedTypedVector::operator[](size_t i) const {
  733. if (i >= len_) return Reference(nullptr, 1, NullPackedType());
  734. auto elem = data_ + i * byte_width_;
  735. return Reference(elem, byte_width_, 1, type_);
  736. }
  737. template<typename T> int KeyCompare(const void *key, const void *elem) {
  738. auto str_elem = reinterpret_cast<const char *>(
  739. Indirect<T>(reinterpret_cast<const uint8_t *>(elem)));
  740. auto skey = reinterpret_cast<const char *>(key);
  741. return strcmp(skey, str_elem);
  742. }
  743. inline Reference Map::operator[](const char *key) const {
  744. auto keys = Keys();
  745. // We can't pass keys.byte_width_ to the comparison function, so we have
  746. // to pick the right one ahead of time.
  747. int (*comp)(const void *, const void *) = nullptr;
  748. switch (keys.byte_width_) {
  749. case 1: comp = KeyCompare<uint8_t>; break;
  750. case 2: comp = KeyCompare<uint16_t>; break;
  751. case 4: comp = KeyCompare<uint32_t>; break;
  752. case 8: comp = KeyCompare<uint64_t>; break;
  753. }
  754. auto res = std::bsearch(key, keys.data_, keys.size(), keys.byte_width_, comp);
  755. if (!res) return Reference(nullptr, 1, NullPackedType());
  756. auto i = (reinterpret_cast<uint8_t *>(res) - keys.data_) / keys.byte_width_;
  757. return (*static_cast<const Vector *>(this))[i];
  758. }
  759. inline Reference Map::operator[](const std::string &key) const {
  760. return (*this)[key.c_str()];
  761. }
  762. inline Reference GetRoot(const uint8_t *buffer, size_t size) {
  763. // See Finish() below for the serialization counterpart of this.
  764. // The root starts at the end of the buffer, so we parse backwards from there.
  765. auto end = buffer + size;
  766. auto byte_width = *--end;
  767. auto packed_type = *--end;
  768. end -= byte_width; // The root data item.
  769. return Reference(end, byte_width, packed_type);
  770. }
  771. inline Reference GetRoot(const std::vector<uint8_t> &buffer) {
  772. return GetRoot(flatbuffers::vector_data(buffer), buffer.size());
  773. }
  774. // Flags that configure how the Builder behaves.
  775. // The "Share" flags determine if the Builder automatically tries to pool
  776. // this type. Pooling can reduce the size of serialized data if there are
  777. // multiple maps of the same kind, at the expense of slightly slower
  778. // serialization (the cost of lookups) and more memory use (std::set).
  779. // By default this is on for keys, but off for strings.
  780. // Turn keys off if you have e.g. only one map.
  781. // Turn strings on if you expect many non-unique string values.
  782. // Additionally, sharing key vectors can save space if you have maps with
  783. // identical field populations.
  784. enum BuilderFlag {
  785. BUILDER_FLAG_NONE = 0,
  786. BUILDER_FLAG_SHARE_KEYS = 1,
  787. BUILDER_FLAG_SHARE_STRINGS = 2,
  788. BUILDER_FLAG_SHARE_KEYS_AND_STRINGS = 3,
  789. BUILDER_FLAG_SHARE_KEY_VECTORS = 4,
  790. BUILDER_FLAG_SHARE_ALL = 7,
  791. };
  792. class Builder FLATBUFFERS_FINAL_CLASS {
  793. public:
  794. Builder(size_t initial_size = 256,
  795. BuilderFlag flags = BUILDER_FLAG_SHARE_KEYS)
  796. : buf_(initial_size),
  797. finished_(false),
  798. flags_(flags),
  799. force_min_bit_width_(BIT_WIDTH_8),
  800. key_pool(KeyOffsetCompare(buf_)),
  801. string_pool(StringOffsetCompare(buf_)) {
  802. buf_.clear();
  803. }
  804. /// @brief Get the serialized buffer (after you call `Finish()`).
  805. /// @return Returns a vector owned by this class.
  806. const std::vector<uint8_t> &GetBuffer() const {
  807. Finished();
  808. return buf_;
  809. }
  810. // Size of the buffer. Does not include unfinished values.
  811. size_t GetSize() const { return buf_.size(); }
  812. // Reset all state so we can re-use the buffer.
  813. void Clear() {
  814. buf_.clear();
  815. stack_.clear();
  816. finished_ = false;
  817. // flags_ remains as-is;
  818. force_min_bit_width_ = BIT_WIDTH_8;
  819. key_pool.clear();
  820. string_pool.clear();
  821. }
  822. // All value constructing functions below have two versions: one that
  823. // takes a key (for placement inside a map) and one that doesn't (for inside
  824. // vectors and elsewhere).
  825. void Null() { stack_.push_back(Value()); }
  826. void Null(const char *key) {
  827. Key(key);
  828. Null();
  829. }
  830. void Int(int64_t i) { stack_.push_back(Value(i, FBT_INT, WidthI(i))); }
  831. void Int(const char *key, int64_t i) {
  832. Key(key);
  833. Int(i);
  834. }
  835. void UInt(uint64_t u) { stack_.push_back(Value(u, FBT_UINT, WidthU(u))); }
  836. void UInt(const char *key, uint64_t u) {
  837. Key(key);
  838. UInt(u);
  839. }
  840. void Float(float f) { stack_.push_back(Value(f)); }
  841. void Float(const char *key, float f) {
  842. Key(key);
  843. Float(f);
  844. }
  845. void Double(double f) { stack_.push_back(Value(f)); }
  846. void Double(const char *key, double d) {
  847. Key(key);
  848. Double(d);
  849. }
  850. void Bool(bool b) { stack_.push_back(Value(b)); }
  851. void Bool(const char *key, bool b) {
  852. Key(key);
  853. Bool(b);
  854. }
  855. void IndirectInt(int64_t i) { PushIndirect(i, FBT_INDIRECT_INT, WidthI(i)); }
  856. void IndirectInt(const char *key, int64_t i) {
  857. Key(key);
  858. IndirectInt(i);
  859. }
  860. void IndirectUInt(uint64_t u) {
  861. PushIndirect(u, FBT_INDIRECT_UINT, WidthU(u));
  862. }
  863. void IndirectUInt(const char *key, uint64_t u) {
  864. Key(key);
  865. IndirectUInt(u);
  866. }
  867. void IndirectFloat(float f) {
  868. PushIndirect(f, FBT_INDIRECT_FLOAT, BIT_WIDTH_32);
  869. }
  870. void IndirectFloat(const char *key, float f) {
  871. Key(key);
  872. IndirectFloat(f);
  873. }
  874. void IndirectDouble(double f) {
  875. PushIndirect(f, FBT_INDIRECT_FLOAT, WidthF(f));
  876. }
  877. void IndirectDouble(const char *key, double d) {
  878. Key(key);
  879. IndirectDouble(d);
  880. }
  881. size_t Key(const char *str, size_t len) {
  882. auto sloc = buf_.size();
  883. WriteBytes(str, len + 1);
  884. if (flags_ & BUILDER_FLAG_SHARE_KEYS) {
  885. auto it = key_pool.find(sloc);
  886. if (it != key_pool.end()) {
  887. // Already in the buffer. Remove key we just serialized, and use
  888. // existing offset instead.
  889. buf_.resize(sloc);
  890. sloc = *it;
  891. } else {
  892. key_pool.insert(sloc);
  893. }
  894. }
  895. stack_.push_back(Value(static_cast<uint64_t>(sloc), FBT_KEY, BIT_WIDTH_8));
  896. return sloc;
  897. }
  898. size_t Key(const char *str) { return Key(str, strlen(str)); }
  899. size_t Key(const std::string &str) { return Key(str.c_str(), str.size()); }
  900. size_t String(const char *str, size_t len) {
  901. auto reset_to = buf_.size();
  902. auto sloc = CreateBlob(str, len, 1, FBT_STRING);
  903. if (flags_ & BUILDER_FLAG_SHARE_STRINGS) {
  904. StringOffset so(sloc, len);
  905. auto it = string_pool.find(so);
  906. if (it != string_pool.end()) {
  907. // Already in the buffer. Remove string we just serialized, and use
  908. // existing offset instead.
  909. buf_.resize(reset_to);
  910. sloc = it->first;
  911. stack_.back().u_ = sloc;
  912. } else {
  913. string_pool.insert(so);
  914. }
  915. }
  916. return sloc;
  917. }
  918. size_t String(const char *str) { return String(str, strlen(str)); }
  919. size_t String(const std::string &str) {
  920. return String(str.c_str(), str.size());
  921. }
  922. void String(const flexbuffers::String &str) {
  923. String(str.c_str(), str.length());
  924. }
  925. void String(const char *key, const char *str) {
  926. Key(key);
  927. String(str);
  928. }
  929. void String(const char *key, const std::string &str) {
  930. Key(key);
  931. String(str);
  932. }
  933. void String(const char *key, const flexbuffers::String &str) {
  934. Key(key);
  935. String(str);
  936. }
  937. size_t Blob(const void *data, size_t len) {
  938. return CreateBlob(data, len, 0, FBT_BLOB);
  939. }
  940. size_t Blob(const std::vector<uint8_t> &v) {
  941. return CreateBlob(flatbuffers::vector_data(v), v.size(), 0, FBT_BLOB);
  942. }
  943. // TODO(wvo): support all the FlexBuffer types (like flexbuffers::String),
  944. // e.g. Vector etc. Also in overloaded versions.
  945. // Also some FlatBuffers types?
  946. size_t StartVector() { return stack_.size(); }
  947. size_t StartVector(const char *key) {
  948. Key(key);
  949. return stack_.size();
  950. }
  951. size_t StartMap() { return stack_.size(); }
  952. size_t StartMap(const char *key) {
  953. Key(key);
  954. return stack_.size();
  955. }
  956. // TODO(wvo): allow this to specify an aligment greater than the natural
  957. // alignment.
  958. size_t EndVector(size_t start, bool typed, bool fixed) {
  959. auto vec = CreateVector(start, stack_.size() - start, 1, typed, fixed);
  960. // Remove temp elements and return vector.
  961. stack_.resize(start);
  962. stack_.push_back(vec);
  963. return static_cast<size_t>(vec.u_);
  964. }
  965. size_t EndMap(size_t start) {
  966. // We should have interleaved keys and values on the stack.
  967. // Make sure it is an even number:
  968. auto len = stack_.size() - start;
  969. FLATBUFFERS_ASSERT(!(len & 1));
  970. len /= 2;
  971. // Make sure keys are all strings:
  972. for (auto key = start; key < stack_.size(); key += 2) {
  973. FLATBUFFERS_ASSERT(stack_[key].type_ == FBT_KEY);
  974. }
  975. // Now sort values, so later we can do a binary search lookup.
  976. // We want to sort 2 array elements at a time.
  977. struct TwoValue {
  978. Value key;
  979. Value val;
  980. };
  981. // TODO(wvo): strict aliasing?
  982. // TODO(wvo): allow the caller to indicate the data is already sorted
  983. // for maximum efficiency? With an assert to check sortedness to make sure
  984. // we're not breaking binary search.
  985. // Or, we can track if the map is sorted as keys are added which would be
  986. // be quite cheap (cheaper than checking it here), so we can skip this
  987. // step automatically when appliccable, and encourage people to write in
  988. // sorted fashion.
  989. // std::sort is typically already a lot faster on sorted data though.
  990. auto dict =
  991. reinterpret_cast<TwoValue *>(flatbuffers::vector_data(stack_) + start);
  992. std::sort(dict, dict + len,
  993. [&](const TwoValue &a, const TwoValue &b) -> bool {
  994. auto as = reinterpret_cast<const char *>(
  995. flatbuffers::vector_data(buf_) + a.key.u_);
  996. auto bs = reinterpret_cast<const char *>(
  997. flatbuffers::vector_data(buf_) + b.key.u_);
  998. auto comp = strcmp(as, bs);
  999. // If this assertion hits, you've added two keys with the same
  1000. // value to this map.
  1001. // TODO: Have to check for pointer equality, as some sort
  1002. // implementation apparently call this function with the same
  1003. // element?? Why?
  1004. FLATBUFFERS_ASSERT(comp || &a == &b);
  1005. return comp < 0;
  1006. });
  1007. // First create a vector out of all keys.
  1008. // TODO(wvo): if kBuilderFlagShareKeyVectors is true, see if we can share
  1009. // the first vector.
  1010. auto keys = CreateVector(start, len, 2, true, false);
  1011. auto vec = CreateVector(start + 1, len, 2, false, false, &keys);
  1012. // Remove temp elements and return map.
  1013. stack_.resize(start);
  1014. stack_.push_back(vec);
  1015. return static_cast<size_t>(vec.u_);
  1016. }
  1017. template<typename F> size_t Vector(F f) {
  1018. auto start = StartVector();
  1019. f();
  1020. return EndVector(start, false, false);
  1021. }
  1022. template<typename F, typename T> size_t Vector(F f, T &state) {
  1023. auto start = StartVector();
  1024. f(state);
  1025. return EndVector(start, false, false);
  1026. }
  1027. template<typename F> size_t Vector(const char *key, F f) {
  1028. auto start = StartVector(key);
  1029. f();
  1030. return EndVector(start, false, false);
  1031. }
  1032. template<typename F, typename T>
  1033. size_t Vector(const char *key, F f, T &state) {
  1034. auto start = StartVector(key);
  1035. f(state);
  1036. return EndVector(start, false, false);
  1037. }
  1038. template<typename T> void Vector(const T *elems, size_t len) {
  1039. if (flatbuffers::is_scalar<T>::value) {
  1040. // This path should be a lot quicker and use less space.
  1041. ScalarVector(elems, len, false);
  1042. } else {
  1043. auto start = StartVector();
  1044. for (size_t i = 0; i < len; i++) Add(elems[i]);
  1045. EndVector(start, false, false);
  1046. }
  1047. }
  1048. template<typename T>
  1049. void Vector(const char *key, const T *elems, size_t len) {
  1050. Key(key);
  1051. Vector(elems, len);
  1052. }
  1053. template<typename T> void Vector(const std::vector<T> &vec) {
  1054. Vector(flatbuffers::vector_data(vec), vec.size());
  1055. }
  1056. template<typename F> size_t TypedVector(F f) {
  1057. auto start = StartVector();
  1058. f();
  1059. return EndVector(start, true, false);
  1060. }
  1061. template<typename F, typename T> size_t TypedVector(F f, T &state) {
  1062. auto start = StartVector();
  1063. f(state);
  1064. return EndVector(start, true, false);
  1065. }
  1066. template<typename F> size_t TypedVector(const char *key, F f) {
  1067. auto start = StartVector(key);
  1068. f();
  1069. return EndVector(start, true, false);
  1070. }
  1071. template<typename F, typename T>
  1072. size_t TypedVector(const char *key, F f, T &state) {
  1073. auto start = StartVector(key);
  1074. f(state);
  1075. return EndVector(start, true, false);
  1076. }
  1077. template<typename T> size_t FixedTypedVector(const T *elems, size_t len) {
  1078. // We only support a few fixed vector lengths. Anything bigger use a
  1079. // regular typed vector.
  1080. FLATBUFFERS_ASSERT(len >= 2 && len <= 4);
  1081. // And only scalar values.
  1082. static_assert(flatbuffers::is_scalar<T>::value, "Unrelated types");
  1083. return ScalarVector(elems, len, true);
  1084. }
  1085. template<typename T>
  1086. size_t FixedTypedVector(const char *key, const T *elems, size_t len) {
  1087. Key(key);
  1088. return FixedTypedVector(elems, len);
  1089. }
  1090. template<typename F> size_t Map(F f) {
  1091. auto start = StartMap();
  1092. f();
  1093. return EndMap(start);
  1094. }
  1095. template<typename F, typename T> size_t Map(F f, T &state) {
  1096. auto start = StartMap();
  1097. f(state);
  1098. return EndMap(start);
  1099. }
  1100. template<typename F> size_t Map(const char *key, F f) {
  1101. auto start = StartMap(key);
  1102. f();
  1103. return EndMap(start);
  1104. }
  1105. template<typename F, typename T> size_t Map(const char *key, F f, T &state) {
  1106. auto start = StartMap(key);
  1107. f(state);
  1108. return EndMap(start);
  1109. }
  1110. template<typename T> void Map(const std::map<std::string, T> &map) {
  1111. auto start = StartMap();
  1112. for (auto it = map.begin(); it != map.end(); ++it)
  1113. Add(it->first.c_str(), it->second);
  1114. EndMap(start);
  1115. }
  1116. // If you wish to share a value explicitly (a value not shared automatically
  1117. // through one of the BUILDER_FLAG_SHARE_* flags) you can do so with these
  1118. // functions. Or if you wish to turn those flags off for performance reasons
  1119. // and still do some explicit sharing. For example:
  1120. // builder.IndirectDouble(M_PI);
  1121. // auto id = builder.LastValue(); // Remember where we stored it.
  1122. // .. more code goes here ..
  1123. // builder.ReuseValue(id); // Refers to same double by offset.
  1124. // LastValue works regardless of whether the value has a key or not.
  1125. // Works on any data type.
  1126. struct Value;
  1127. Value LastValue() { return stack_.back(); }
  1128. void ReuseValue(Value v) { stack_.push_back(v); }
  1129. void ReuseValue(const char *key, Value v) {
  1130. Key(key);
  1131. ReuseValue(v);
  1132. }
  1133. // Overloaded Add that tries to call the correct function above.
  1134. void Add(int8_t i) { Int(i); }
  1135. void Add(int16_t i) { Int(i); }
  1136. void Add(int32_t i) { Int(i); }
  1137. void Add(int64_t i) { Int(i); }
  1138. void Add(uint8_t u) { UInt(u); }
  1139. void Add(uint16_t u) { UInt(u); }
  1140. void Add(uint32_t u) { UInt(u); }
  1141. void Add(uint64_t u) { UInt(u); }
  1142. void Add(float f) { Float(f); }
  1143. void Add(double d) { Double(d); }
  1144. void Add(bool b) { Bool(b); }
  1145. void Add(const char *str) { String(str); }
  1146. void Add(const std::string &str) { String(str); }
  1147. void Add(const flexbuffers::String &str) { String(str); }
  1148. template<typename T> void Add(const std::vector<T> &vec) { Vector(vec); }
  1149. template<typename T> void Add(const char *key, const T &t) {
  1150. Key(key);
  1151. Add(t);
  1152. }
  1153. template<typename T> void Add(const std::map<std::string, T> &map) {
  1154. Map(map);
  1155. }
  1156. template<typename T> void operator+=(const T &t) { Add(t); }
  1157. // This function is useful in combination with the Mutate* functions above.
  1158. // It forces elements of vectors and maps to have a minimum size, such that
  1159. // they can later be updated without failing.
  1160. // Call with no arguments to reset.
  1161. void ForceMinimumBitWidth(BitWidth bw = BIT_WIDTH_8) {
  1162. force_min_bit_width_ = bw;
  1163. }
  1164. void Finish() {
  1165. // If you hit this assert, you likely have objects that were never included
  1166. // in a parent. You need to have exactly one root to finish a buffer.
  1167. // Check your Start/End calls are matched, and all objects are inside
  1168. // some other object.
  1169. FLATBUFFERS_ASSERT(stack_.size() == 1);
  1170. // Write root value.
  1171. auto byte_width = Align(stack_[0].ElemWidth(buf_.size(), 0));
  1172. WriteAny(stack_[0], byte_width);
  1173. // Write root type.
  1174. Write(stack_[0].StoredPackedType(), 1);
  1175. // Write root size. Normally determined by parent, but root has no parent :)
  1176. Write(byte_width, 1);
  1177. finished_ = true;
  1178. }
  1179. private:
  1180. void Finished() const {
  1181. // If you get this assert, you're attempting to get access a buffer
  1182. // which hasn't been finished yet. Be sure to call
  1183. // Builder::Finish with your root object.
  1184. FLATBUFFERS_ASSERT(finished_);
  1185. }
  1186. // Align to prepare for writing a scalar with a certain size.
  1187. uint8_t Align(BitWidth alignment) {
  1188. auto byte_width = 1U << alignment;
  1189. buf_.insert(buf_.end(), flatbuffers::PaddingBytes(buf_.size(), byte_width),
  1190. 0);
  1191. return static_cast<uint8_t>(byte_width);
  1192. }
  1193. void WriteBytes(const void *val, size_t size) {
  1194. buf_.insert(buf_.end(), reinterpret_cast<const uint8_t *>(val),
  1195. reinterpret_cast<const uint8_t *>(val) + size);
  1196. }
  1197. template<typename T> void Write(T val, size_t byte_width) {
  1198. FLATBUFFERS_ASSERT(sizeof(T) >= byte_width);
  1199. val = flatbuffers::EndianScalar(val);
  1200. WriteBytes(&val, byte_width);
  1201. }
  1202. void WriteDouble(double f, uint8_t byte_width) {
  1203. switch (byte_width) {
  1204. case 8: Write(f, byte_width); break;
  1205. case 4: Write(static_cast<float>(f), byte_width); break;
  1206. // case 2: Write(static_cast<half>(f), byte_width); break;
  1207. // case 1: Write(static_cast<quarter>(f), byte_width); break;
  1208. default: FLATBUFFERS_ASSERT(0);
  1209. }
  1210. }
  1211. void WriteOffset(uint64_t o, uint8_t byte_width) {
  1212. auto reloff = buf_.size() - o;
  1213. FLATBUFFERS_ASSERT(byte_width == 8 || reloff < 1ULL << (byte_width * 8));
  1214. Write(reloff, byte_width);
  1215. }
  1216. template<typename T> void PushIndirect(T val, Type type, BitWidth bit_width) {
  1217. auto byte_width = Align(bit_width);
  1218. auto iloc = buf_.size();
  1219. Write(val, byte_width);
  1220. stack_.push_back(Value(static_cast<uint64_t>(iloc), type, bit_width));
  1221. }
  1222. static BitWidth WidthB(size_t byte_width) {
  1223. switch (byte_width) {
  1224. case 1: return BIT_WIDTH_8;
  1225. case 2: return BIT_WIDTH_16;
  1226. case 4: return BIT_WIDTH_32;
  1227. case 8: return BIT_WIDTH_64;
  1228. default: FLATBUFFERS_ASSERT(false); return BIT_WIDTH_64;
  1229. }
  1230. }
  1231. template<typename T> static Type GetScalarType() {
  1232. static_assert(flatbuffers::is_scalar<T>::value, "Unrelated types");
  1233. return flatbuffers::is_floating_point<T>::value
  1234. ? FBT_FLOAT
  1235. : flatbuffers::is_same<T, bool>::value
  1236. ? FBT_BOOL
  1237. : (flatbuffers::is_unsigned<T>::value ? FBT_UINT
  1238. : FBT_INT);
  1239. }
  1240. public:
  1241. // This was really intended to be private, except for LastValue/ReuseValue.
  1242. struct Value {
  1243. union {
  1244. int64_t i_;
  1245. uint64_t u_;
  1246. double f_;
  1247. };
  1248. Type type_;
  1249. // For scalars: of itself, for vector: of its elements, for string: length.
  1250. BitWidth min_bit_width_;
  1251. Value() : i_(0), type_(FBT_NULL), min_bit_width_(BIT_WIDTH_8) {}
  1252. Value(bool b)
  1253. : u_(static_cast<uint64_t>(b)),
  1254. type_(FBT_BOOL),
  1255. min_bit_width_(BIT_WIDTH_8) {}
  1256. Value(int64_t i, Type t, BitWidth bw)
  1257. : i_(i), type_(t), min_bit_width_(bw) {}
  1258. Value(uint64_t u, Type t, BitWidth bw)
  1259. : u_(u), type_(t), min_bit_width_(bw) {}
  1260. Value(float f) : f_(f), type_(FBT_FLOAT), min_bit_width_(BIT_WIDTH_32) {}
  1261. Value(double f) : f_(f), type_(FBT_FLOAT), min_bit_width_(WidthF(f)) {}
  1262. uint8_t StoredPackedType(BitWidth parent_bit_width_ = BIT_WIDTH_8) const {
  1263. return PackedType(StoredWidth(parent_bit_width_), type_);
  1264. }
  1265. BitWidth ElemWidth(size_t buf_size, size_t elem_index) const {
  1266. if (IsInline(type_)) {
  1267. return min_bit_width_;
  1268. } else {
  1269. // We have an absolute offset, but want to store a relative offset
  1270. // elem_index elements beyond the current buffer end. Since whether
  1271. // the relative offset fits in a certain byte_width depends on
  1272. // the size of the elements before it (and their alignment), we have
  1273. // to test for each size in turn.
  1274. for (size_t byte_width = 1;
  1275. byte_width <= sizeof(flatbuffers::largest_scalar_t);
  1276. byte_width *= 2) {
  1277. // Where are we going to write this offset?
  1278. auto offset_loc = buf_size +
  1279. flatbuffers::PaddingBytes(buf_size, byte_width) +
  1280. elem_index * byte_width;
  1281. // Compute relative offset.
  1282. auto offset = offset_loc - u_;
  1283. // Does it fit?
  1284. auto bit_width = WidthU(offset);
  1285. if (static_cast<size_t>(static_cast<size_t>(1U) << bit_width) ==
  1286. byte_width)
  1287. return bit_width;
  1288. }
  1289. FLATBUFFERS_ASSERT(false); // Must match one of the sizes above.
  1290. return BIT_WIDTH_64;
  1291. }
  1292. }
  1293. BitWidth StoredWidth(BitWidth parent_bit_width_ = BIT_WIDTH_8) const {
  1294. if (IsInline(type_)) {
  1295. return (std::max)(min_bit_width_, parent_bit_width_);
  1296. } else {
  1297. return min_bit_width_;
  1298. }
  1299. }
  1300. };
  1301. private:
  1302. void WriteAny(const Value &val, uint8_t byte_width) {
  1303. switch (val.type_) {
  1304. case FBT_NULL:
  1305. case FBT_INT: Write(val.i_, byte_width); break;
  1306. case FBT_BOOL:
  1307. case FBT_UINT: Write(val.u_, byte_width); break;
  1308. case FBT_FLOAT: WriteDouble(val.f_, byte_width); break;
  1309. default: WriteOffset(val.u_, byte_width); break;
  1310. }
  1311. }
  1312. size_t CreateBlob(const void *data, size_t len, size_t trailing, Type type) {
  1313. auto bit_width = WidthU(len);
  1314. auto byte_width = Align(bit_width);
  1315. Write<uint64_t>(len, byte_width);
  1316. auto sloc = buf_.size();
  1317. WriteBytes(data, len + trailing);
  1318. stack_.push_back(Value(static_cast<uint64_t>(sloc), type, bit_width));
  1319. return sloc;
  1320. }
  1321. template<typename T>
  1322. size_t ScalarVector(const T *elems, size_t len, bool fixed) {
  1323. auto vector_type = GetScalarType<T>();
  1324. auto byte_width = sizeof(T);
  1325. auto bit_width = WidthB(byte_width);
  1326. // If you get this assert, you're trying to write a vector with a size
  1327. // field that is bigger than the scalars you're trying to write (e.g. a
  1328. // byte vector > 255 elements). For such types, write a "blob" instead.
  1329. // TODO: instead of asserting, could write vector with larger elements
  1330. // instead, though that would be wasteful.
  1331. FLATBUFFERS_ASSERT(WidthU(len) <= bit_width);
  1332. Align(bit_width);
  1333. if (!fixed) Write<uint64_t>(len, byte_width);
  1334. auto vloc = buf_.size();
  1335. for (size_t i = 0; i < len; i++) Write(elems[i], byte_width);
  1336. stack_.push_back(Value(static_cast<uint64_t>(vloc),
  1337. ToTypedVector(vector_type, fixed ? len : 0),
  1338. bit_width));
  1339. return vloc;
  1340. }
  1341. Value CreateVector(size_t start, size_t vec_len, size_t step, bool typed,
  1342. bool fixed, const Value *keys = nullptr) {
  1343. FLATBUFFERS_ASSERT(
  1344. !fixed ||
  1345. typed); // typed=false, fixed=true combination is not supported.
  1346. // Figure out smallest bit width we can store this vector with.
  1347. auto bit_width = (std::max)(force_min_bit_width_, WidthU(vec_len));
  1348. auto prefix_elems = 1;
  1349. if (keys) {
  1350. // If this vector is part of a map, we will pre-fix an offset to the keys
  1351. // to this vector.
  1352. bit_width = (std::max)(bit_width, keys->ElemWidth(buf_.size(), 0));
  1353. prefix_elems += 2;
  1354. }
  1355. Type vector_type = FBT_KEY;
  1356. // Check bit widths and types for all elements.
  1357. for (size_t i = start; i < stack_.size(); i += step) {
  1358. auto elem_width =
  1359. stack_[i].ElemWidth(buf_.size(), i - start + prefix_elems);
  1360. bit_width = (std::max)(bit_width, elem_width);
  1361. if (typed) {
  1362. if (i == start) {
  1363. vector_type = stack_[i].type_;
  1364. } else {
  1365. // If you get this assert, you are writing a typed vector with
  1366. // elements that are not all the same type.
  1367. FLATBUFFERS_ASSERT(vector_type == stack_[i].type_);
  1368. }
  1369. }
  1370. }
  1371. // If you get this assert, your fixed types are not one of:
  1372. // Int / UInt / Float / Key.
  1373. FLATBUFFERS_ASSERT(!fixed || IsTypedVectorElementType(vector_type));
  1374. auto byte_width = Align(bit_width);
  1375. // Write vector. First the keys width/offset if available, and size.
  1376. if (keys) {
  1377. WriteOffset(keys->u_, byte_width);
  1378. Write<uint64_t>(1ULL << keys->min_bit_width_, byte_width);
  1379. }
  1380. if (!fixed) Write<uint64_t>(vec_len, byte_width);
  1381. // Then the actual data.
  1382. auto vloc = buf_.size();
  1383. for (size_t i = start; i < stack_.size(); i += step) {
  1384. WriteAny(stack_[i], byte_width);
  1385. }
  1386. // Then the types.
  1387. if (!typed) {
  1388. for (size_t i = start; i < stack_.size(); i += step) {
  1389. buf_.push_back(stack_[i].StoredPackedType(bit_width));
  1390. }
  1391. }
  1392. return Value(static_cast<uint64_t>(vloc),
  1393. keys ? FBT_MAP
  1394. : (typed ? ToTypedVector(vector_type, fixed ? vec_len : 0)
  1395. : FBT_VECTOR),
  1396. bit_width);
  1397. }
  1398. // You shouldn't really be copying instances of this class.
  1399. Builder(const Builder &);
  1400. Builder &operator=(const Builder &);
  1401. std::vector<uint8_t> buf_;
  1402. std::vector<Value> stack_;
  1403. bool finished_;
  1404. BuilderFlag flags_;
  1405. BitWidth force_min_bit_width_;
  1406. struct KeyOffsetCompare {
  1407. explicit KeyOffsetCompare(const std::vector<uint8_t> &buf) : buf_(&buf) {}
  1408. bool operator()(size_t a, size_t b) const {
  1409. auto stra =
  1410. reinterpret_cast<const char *>(flatbuffers::vector_data(*buf_) + a);
  1411. auto strb =
  1412. reinterpret_cast<const char *>(flatbuffers::vector_data(*buf_) + b);
  1413. return strcmp(stra, strb) < 0;
  1414. }
  1415. const std::vector<uint8_t> *buf_;
  1416. };
  1417. typedef std::pair<size_t, size_t> StringOffset;
  1418. struct StringOffsetCompare {
  1419. explicit StringOffsetCompare(const std::vector<uint8_t> &buf)
  1420. : buf_(&buf) {}
  1421. bool operator()(const StringOffset &a, const StringOffset &b) const {
  1422. auto stra = reinterpret_cast<const char *>(
  1423. flatbuffers::vector_data(*buf_) + a.first);
  1424. auto strb = reinterpret_cast<const char *>(
  1425. flatbuffers::vector_data(*buf_) + b.first);
  1426. return strncmp(stra, strb, (std::min)(a.second, b.second) + 1) < 0;
  1427. }
  1428. const std::vector<uint8_t> *buf_;
  1429. };
  1430. typedef std::set<size_t, KeyOffsetCompare> KeyOffsetMap;
  1431. typedef std::set<StringOffset, StringOffsetCompare> StringOffsetMap;
  1432. KeyOffsetMap key_pool;
  1433. StringOffsetMap string_pool;
  1434. };
  1435. } // namespace flexbuffers
  1436. #if defined(_MSC_VER)
  1437. # pragma warning(pop)
  1438. #endif
  1439. #endif // FLATBUFFERS_FLEXBUFFERS_H_