PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/flatbuffers/src/idl_gen_text.cpp

https://github.com/DennisOSRM/Project-OSRM
C++ | 378 lines | 310 code | 25 blank | 43 comment | 66 complexity | 1b087a32ea9c573fe6f3c784f455b48c MD5 | raw file
  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. // independent from idl_parser, since this code is not needed for most clients
  17. #include "flatbuffers/flatbuffers.h"
  18. #include "flatbuffers/flexbuffers.h"
  19. #include "flatbuffers/idl.h"
  20. #include "flatbuffers/util.h"
  21. namespace flatbuffers {
  22. static bool GenStruct(const StructDef &struct_def, const Table *table,
  23. int indent, const IDLOptions &opts, std::string *_text);
  24. // If indentation is less than 0, that indicates we don't want any newlines
  25. // either.
  26. const char *NewLine(const IDLOptions &opts) {
  27. return opts.indent_step >= 0 ? "\n" : "";
  28. }
  29. int Indent(const IDLOptions &opts) { return std::max(opts.indent_step, 0); }
  30. // Output an identifier with or without quotes depending on strictness.
  31. void OutputIdentifier(const std::string &name, const IDLOptions &opts,
  32. std::string *_text) {
  33. std::string &text = *_text;
  34. if (opts.strict_json) text += "\"";
  35. text += name;
  36. if (opts.strict_json) text += "\"";
  37. }
  38. // Print (and its template specialization below for pointers) generate text
  39. // for a single FlatBuffer value into JSON format.
  40. // The general case for scalars:
  41. template<typename T>
  42. bool Print(T val, Type type, int /*indent*/, Type * /*union_type*/,
  43. const IDLOptions &opts, std::string *_text) {
  44. std::string &text = *_text;
  45. if (type.enum_def && opts.output_enum_identifiers) {
  46. std::vector<EnumVal const *> enum_values;
  47. if (auto ev = type.enum_def->ReverseLookup(static_cast<int64_t>(val))) {
  48. enum_values.push_back(ev);
  49. } else if (val && type.enum_def->attributes.Lookup("bit_flags")) {
  50. for (auto it = type.enum_def->Vals().begin(),
  51. e = type.enum_def->Vals().end();
  52. it != e; ++it) {
  53. if ((*it)->GetAsUInt64() & static_cast<uint64_t>(val))
  54. enum_values.push_back(*it);
  55. }
  56. }
  57. if (!enum_values.empty()) {
  58. text += '\"';
  59. for (auto it = enum_values.begin(), e = enum_values.end(); it != e; ++it)
  60. text += (*it)->name + ' ';
  61. text[text.length() - 1] = '\"';
  62. return true;
  63. }
  64. }
  65. if (type.base_type == BASE_TYPE_BOOL) {
  66. text += val != 0 ? "true" : "false";
  67. } else {
  68. text += NumToString(val);
  69. }
  70. return true;
  71. }
  72. // Print a vector or an array of JSON values, comma seperated, wrapped in "[]".
  73. template<typename T, typename Container>
  74. bool PrintContainer(const Container &c, size_t size, Type type, int indent,
  75. const IDLOptions &opts, std::string *_text) {
  76. std::string &text = *_text;
  77. text += "[";
  78. text += NewLine(opts);
  79. for (uoffset_t i = 0; i < size; i++) {
  80. if (i) {
  81. if (!opts.protobuf_ascii_alike) text += ",";
  82. text += NewLine(opts);
  83. }
  84. text.append(indent + Indent(opts), ' ');
  85. if (IsStruct(type)) {
  86. if (!Print(reinterpret_cast<const void *>(c.Data() +
  87. i * type.struct_def->bytesize),
  88. type, indent + Indent(opts), nullptr, opts, _text)) {
  89. return false;
  90. }
  91. } else {
  92. if (!Print(c[i], type, indent + Indent(opts), nullptr, opts, _text)) {
  93. return false;
  94. }
  95. }
  96. }
  97. text += NewLine(opts);
  98. text.append(indent, ' ');
  99. text += "]";
  100. return true;
  101. }
  102. template<typename T>
  103. bool PrintVector(const Vector<T> &v, Type type, int indent,
  104. const IDLOptions &opts, std::string *_text) {
  105. return PrintContainer<T, Vector<T>>(v, v.size(), type, indent, opts, _text);
  106. }
  107. // Print an array a sequence of JSON values, comma separated, wrapped in "[]".
  108. template<typename T>
  109. bool PrintArray(const Array<T, 0xFFFF> &a, size_t size, Type type, int indent,
  110. const IDLOptions &opts, std::string *_text) {
  111. return PrintContainer<T, Array<T, 0xFFFF>>(a, size, type, indent, opts,
  112. _text);
  113. }
  114. // Specialization of Print above for pointer types.
  115. template<>
  116. bool Print<const void *>(const void *val, Type type, int indent,
  117. Type *union_type, const IDLOptions &opts,
  118. std::string *_text) {
  119. switch (type.base_type) {
  120. case BASE_TYPE_UNION:
  121. // If this assert hits, you have an corrupt buffer, a union type field
  122. // was not present or was out of range.
  123. FLATBUFFERS_ASSERT(union_type);
  124. return Print<const void *>(val, *union_type, indent, nullptr, opts,
  125. _text);
  126. case BASE_TYPE_STRUCT:
  127. if (!GenStruct(*type.struct_def, reinterpret_cast<const Table *>(val),
  128. indent, opts, _text)) {
  129. return false;
  130. }
  131. break;
  132. case BASE_TYPE_STRING: {
  133. auto s = reinterpret_cast<const String *>(val);
  134. if (!EscapeString(s->c_str(), s->size(), _text, opts.allow_non_utf8,
  135. opts.natural_utf8)) {
  136. return false;
  137. }
  138. break;
  139. }
  140. case BASE_TYPE_VECTOR: {
  141. const auto vec_type = type.VectorType();
  142. // Call PrintVector above specifically for each element type:
  143. // clang-format off
  144. switch (vec_type.base_type) {
  145. #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
  146. CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
  147. case BASE_TYPE_ ## ENUM: \
  148. if (!PrintVector<CTYPE>( \
  149. *reinterpret_cast<const Vector<CTYPE> *>(val), \
  150. vec_type, indent, opts, _text)) { \
  151. return false; \
  152. } \
  153. break;
  154. FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
  155. #undef FLATBUFFERS_TD
  156. }
  157. // clang-format on
  158. break;
  159. }
  160. case BASE_TYPE_ARRAY: {
  161. const auto vec_type = type.VectorType();
  162. // Call PrintArray above specifically for each element type:
  163. // clang-format off
  164. switch (vec_type.base_type) {
  165. #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
  166. CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
  167. case BASE_TYPE_ ## ENUM: \
  168. if (!PrintArray<CTYPE>( \
  169. *reinterpret_cast<const Array<CTYPE, 0xFFFF> *>(val), \
  170. type.fixed_length, \
  171. vec_type, indent, opts, _text)) { \
  172. return false; \
  173. } \
  174. break;
  175. FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
  176. FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD)
  177. #undef FLATBUFFERS_TD
  178. case BASE_TYPE_ARRAY: FLATBUFFERS_ASSERT(0);
  179. }
  180. // clang-format on
  181. break;
  182. }
  183. default: FLATBUFFERS_ASSERT(0);
  184. }
  185. return true;
  186. }
  187. template<typename T> static T GetFieldDefault(const FieldDef &fd) {
  188. T val;
  189. auto check = StringToNumber(fd.value.constant.c_str(), &val);
  190. (void)check;
  191. FLATBUFFERS_ASSERT(check);
  192. return val;
  193. }
  194. // Generate text for a scalar field.
  195. template<typename T>
  196. static bool GenField(const FieldDef &fd, const Table *table, bool fixed,
  197. const IDLOptions &opts, int indent, std::string *_text) {
  198. return Print(
  199. fixed ? reinterpret_cast<const Struct *>(table)->GetField<T>(
  200. fd.value.offset)
  201. : table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)),
  202. fd.value.type, indent, nullptr, opts, _text);
  203. }
  204. static bool GenStruct(const StructDef &struct_def, const Table *table,
  205. int indent, const IDLOptions &opts, std::string *_text);
  206. // Generate text for non-scalar field.
  207. static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
  208. int indent, Type *union_type, const IDLOptions &opts,
  209. std::string *_text) {
  210. const void *val = nullptr;
  211. if (fixed) {
  212. // The only non-scalar fields in structs are structs or arrays.
  213. FLATBUFFERS_ASSERT(IsStruct(fd.value.type) || IsArray(fd.value.type));
  214. val = reinterpret_cast<const Struct *>(table)->GetStruct<const void *>(
  215. fd.value.offset);
  216. } else if (fd.flexbuffer) {
  217. auto vec = table->GetPointer<const Vector<uint8_t> *>(fd.value.offset);
  218. auto root = flexbuffers::GetRoot(vec->data(), vec->size());
  219. root.ToString(true, opts.strict_json, *_text);
  220. return true;
  221. } else if (fd.nested_flatbuffer) {
  222. auto vec = table->GetPointer<const Vector<uint8_t> *>(fd.value.offset);
  223. auto root = GetRoot<Table>(vec->data());
  224. return GenStruct(*fd.nested_flatbuffer, root, indent, opts, _text);
  225. } else {
  226. val = IsStruct(fd.value.type)
  227. ? table->GetStruct<const void *>(fd.value.offset)
  228. : table->GetPointer<const void *>(fd.value.offset);
  229. }
  230. return Print(val, fd.value.type, indent, union_type, opts, _text);
  231. }
  232. // Generate text for a struct or table, values separated by commas, indented,
  233. // and bracketed by "{}"
  234. static bool GenStruct(const StructDef &struct_def, const Table *table,
  235. int indent, const IDLOptions &opts, std::string *_text) {
  236. std::string &text = *_text;
  237. text += "{";
  238. int fieldout = 0;
  239. Type *union_type = nullptr;
  240. for (auto it = struct_def.fields.vec.begin();
  241. it != struct_def.fields.vec.end(); ++it) {
  242. FieldDef &fd = **it;
  243. auto is_present = struct_def.fixed || table->CheckField(fd.value.offset);
  244. auto output_anyway = opts.output_default_scalars_in_json &&
  245. IsScalar(fd.value.type.base_type) && !fd.deprecated;
  246. if (is_present || output_anyway) {
  247. if (fieldout++) {
  248. if (!opts.protobuf_ascii_alike) text += ",";
  249. }
  250. text += NewLine(opts);
  251. text.append(indent + Indent(opts), ' ');
  252. OutputIdentifier(fd.name, opts, _text);
  253. if (!opts.protobuf_ascii_alike ||
  254. (fd.value.type.base_type != BASE_TYPE_STRUCT &&
  255. fd.value.type.base_type != BASE_TYPE_VECTOR))
  256. text += ":";
  257. text += " ";
  258. switch (fd.value.type.base_type) {
  259. // clang-format off
  260. #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
  261. CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
  262. case BASE_TYPE_ ## ENUM: \
  263. if (!GenField<CTYPE>(fd, table, struct_def.fixed, \
  264. opts, indent + Indent(opts), _text)) { \
  265. return false; \
  266. } \
  267. break;
  268. FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
  269. #undef FLATBUFFERS_TD
  270. // Generate drop-thru case statements for all pointer types:
  271. #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
  272. CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
  273. case BASE_TYPE_ ## ENUM:
  274. FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD)
  275. FLATBUFFERS_GEN_TYPE_ARRAY(FLATBUFFERS_TD)
  276. #undef FLATBUFFERS_TD
  277. if (!GenFieldOffset(fd, table, struct_def.fixed, indent + Indent(opts),
  278. union_type, opts, _text)) {
  279. return false;
  280. }
  281. break;
  282. // clang-format on
  283. }
  284. if (fd.value.type.base_type == BASE_TYPE_UTYPE) {
  285. auto enum_val = fd.value.type.enum_def->ReverseLookup(
  286. table->GetField<uint8_t>(fd.value.offset, 0), true);
  287. union_type = enum_val ? &enum_val->union_type : nullptr;
  288. }
  289. }
  290. }
  291. text += NewLine(opts);
  292. text.append(indent, ' ');
  293. text += "}";
  294. return true;
  295. }
  296. // Generate a text representation of a flatbuffer in JSON format.
  297. bool GenerateTextFromTable(const Parser &parser, const void *table,
  298. const std::string &table_name, std::string *_text) {
  299. auto struct_def = parser.LookupStruct(table_name);
  300. if (struct_def == nullptr) {
  301. return false;
  302. }
  303. auto &text = *_text;
  304. text.reserve(1024); // Reduce amount of inevitable reallocs.
  305. auto root = static_cast<const Table *>(table);
  306. if (!GenStruct(*struct_def, root, 0, parser.opts, &text)) {
  307. return false;
  308. }
  309. text += NewLine(parser.opts);
  310. return true;
  311. }
  312. // Generate a text representation of a flatbuffer in JSON format.
  313. bool GenerateText(const Parser &parser, const void *flatbuffer,
  314. std::string *_text) {
  315. std::string &text = *_text;
  316. FLATBUFFERS_ASSERT(parser.root_struct_def_); // call SetRootType()
  317. text.reserve(1024); // Reduce amount of inevitable reallocs.
  318. auto root = parser.opts.size_prefixed ?
  319. GetSizePrefixedRoot<Table>(flatbuffer) : GetRoot<Table>(flatbuffer);
  320. if (!GenStruct(*parser.root_struct_def_, root, 0, parser.opts, _text)) {
  321. return false;
  322. }
  323. text += NewLine(parser.opts);
  324. return true;
  325. }
  326. std::string TextFileName(const std::string &path,
  327. const std::string &file_name) {
  328. return path + file_name + ".json";
  329. }
  330. bool GenerateTextFile(const Parser &parser, const std::string &path,
  331. const std::string &file_name) {
  332. if (!parser.builder_.GetSize() || !parser.root_struct_def_) return true;
  333. std::string text;
  334. if (!GenerateText(parser, parser.builder_.GetBufferPointer(), &text)) {
  335. return false;
  336. }
  337. return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(), text,
  338. false);
  339. }
  340. std::string TextMakeRule(const Parser &parser, const std::string &path,
  341. const std::string &file_name) {
  342. if (!parser.builder_.GetSize() || !parser.root_struct_def_) return "";
  343. std::string filebase =
  344. flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
  345. std::string make_rule = TextFileName(path, filebase) + ": " + file_name;
  346. auto included_files =
  347. parser.GetIncludedFilesRecursive(parser.root_struct_def_->file);
  348. for (auto it = included_files.begin(); it != included_files.end(); ++it) {
  349. make_rule += " " + *it;
  350. }
  351. return make_rule;
  352. }
  353. } // namespace flatbuffers