2 * Copyright 2014 Google Inc. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 // independent from idl_parser, since this code is not needed for most clients
19 #include "flatbuffers/code_generators.h"
20 #include "flatbuffers/flatbuffers.h"
21 #include "flatbuffers/idl.h"
22 #include "flatbuffers/util.h"
24 #include <unordered_set>
26 namespace flatbuffers {
28 // Pedantic warning free version of toupper().
29 inline char ToUpper(char c) { return static_cast<char>(::toupper(c)); }
31 static std::string GeneratedFileName(const std::string &path,
32 const std::string &file_name) {
33 return path + file_name + "_generated.h";
37 class CppGenerator : public BaseGenerator {
39 CppGenerator(const Parser &parser, const std::string &path,
40 const std::string &file_name)
41 : BaseGenerator(parser, path, file_name, "", "::"),
42 cur_name_space_(nullptr) {
43 static const char * const keywords[] = {
140 for (auto kw = keywords; *kw; kw++) keywords_.insert(*kw);
143 std::string GenIncludeGuard() const {
144 // Generate include guard.
145 std::string guard = file_name_;
146 // Remove any non-alpha-numeric characters that may appear in a filename.
148 bool operator()(char c) const { return !isalnum(c); }
150 guard.erase(std::remove_if(guard.begin(), guard.end(), IsAlnum()),
152 guard = "FLATBUFFERS_GENERATED_" + guard;
154 // For further uniqueness, also add the namespace.
155 auto name_space = parser_.current_namespace_;
156 for (auto it = name_space->components.begin();
157 it != name_space->components.end(); ++it) {
161 std::transform(guard.begin(), guard.end(), guard.begin(), ToUpper);
165 void GenIncludeDependencies() {
166 int num_includes = 0;
167 for (auto it = parser_.native_included_files_.begin();
168 it != parser_.native_included_files_.end(); ++it) {
169 code_ += "#include \"" + *it + "\"";
172 for (auto it = parser_.included_files_.begin();
173 it != parser_.included_files_.end(); ++it) {
174 if (it->second.empty()) continue;
175 auto noext = flatbuffers::StripExtension(it->second);
176 auto basename = flatbuffers::StripPath(noext);
178 code_ += "#include \"" + parser_.opts.include_prefix +
179 (parser_.opts.keep_include_path ? noext : basename) +
183 if (num_includes) code_ += "";
186 std::string EscapeKeyword(const std::string &name) const {
187 return keywords_.find(name) == keywords_.end() ? name : name + "_";
190 std::string Name(const Definition &def) const {
191 return EscapeKeyword(def.name);
194 std::string Name(const EnumVal &ev) const { return EscapeKeyword(ev.name); }
196 // Iterate through all definitions we haven't generate code for (enums,
197 // structs, and tables) and output them to a single file.
200 code_ += "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n";
202 const auto include_guard = GenIncludeGuard();
203 code_ += "#ifndef " + include_guard;
204 code_ += "#define " + include_guard;
207 if (parser_.opts.gen_nullable) {
208 code_ += "#pragma clang system_header\n\n";
211 code_ += "#include \"flatbuffers/flatbuffers.h\"";
212 if (parser_.uses_flexbuffers_) {
213 code_ += "#include \"flatbuffers/flexbuffers.h\"";
217 if (parser_.opts.include_dependence_headers) { GenIncludeDependencies(); }
219 FLATBUFFERS_ASSERT(!cur_name_space_);
221 // Generate forward declarations for all structs/tables, since they may
222 // have circular references.
223 for (auto it = parser_.structs_.vec.begin();
224 it != parser_.structs_.vec.end(); ++it) {
225 const auto &struct_def = **it;
226 if (!struct_def.generated) {
227 SetNameSpace(struct_def.defined_namespace);
228 code_ += "struct " + Name(struct_def) + ";";
229 if (parser_.opts.generate_object_based_api && !struct_def.fixed) {
231 NativeName(Name(struct_def), &struct_def, parser_.opts) +
237 // Generate preablmle code for mini reflection.
238 if (parser_.opts.mini_reflect != IDLOptions::kNone) {
239 // To break cyclic dependencies, first pre-declare all tables/structs.
240 for (auto it = parser_.structs_.vec.begin();
241 it != parser_.structs_.vec.end(); ++it) {
242 const auto &struct_def = **it;
243 if (!struct_def.generated) {
244 SetNameSpace(struct_def.defined_namespace);
245 GenMiniReflectPre(&struct_def);
250 // Generate code for all the enum declarations.
251 for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
253 const auto &enum_def = **it;
254 if (!enum_def.generated) {
255 SetNameSpace(enum_def.defined_namespace);
260 // Generate code for all structs, then all tables.
261 for (auto it = parser_.structs_.vec.begin();
262 it != parser_.structs_.vec.end(); ++it) {
263 const auto &struct_def = **it;
264 if (struct_def.fixed && !struct_def.generated) {
265 SetNameSpace(struct_def.defined_namespace);
266 GenStruct(struct_def);
269 for (auto it = parser_.structs_.vec.begin();
270 it != parser_.structs_.vec.end(); ++it) {
271 const auto &struct_def = **it;
272 if (!struct_def.fixed && !struct_def.generated) {
273 SetNameSpace(struct_def.defined_namespace);
274 GenTable(struct_def);
277 for (auto it = parser_.structs_.vec.begin();
278 it != parser_.structs_.vec.end(); ++it) {
279 const auto &struct_def = **it;
280 if (!struct_def.fixed && !struct_def.generated) {
281 SetNameSpace(struct_def.defined_namespace);
282 GenTablePost(struct_def);
286 // Generate code for union verifiers.
287 for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
289 const auto &enum_def = **it;
290 if (enum_def.is_union && !enum_def.generated) {
291 SetNameSpace(enum_def.defined_namespace);
292 GenUnionPost(enum_def);
296 // Generate code for mini reflection.
297 if (parser_.opts.mini_reflect != IDLOptions::kNone) {
298 // Then the unions/enums that may refer to them.
299 for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
301 const auto &enum_def = **it;
302 if (!enum_def.generated) {
303 SetNameSpace(enum_def.defined_namespace);
304 GenMiniReflect(nullptr, &enum_def);
307 // Then the full tables/structs.
308 for (auto it = parser_.structs_.vec.begin();
309 it != parser_.structs_.vec.end(); ++it) {
310 const auto &struct_def = **it;
311 if (!struct_def.generated) {
312 SetNameSpace(struct_def.defined_namespace);
313 GenMiniReflect(&struct_def, nullptr);
318 // Generate convenient global helper functions:
319 if (parser_.root_struct_def_) {
320 auto &struct_def = *parser_.root_struct_def_;
321 SetNameSpace(struct_def.defined_namespace);
322 auto name = Name(struct_def);
323 auto qualified_name = cur_name_space_->GetFullyQualifiedName(name);
324 auto cpp_name = TranslateNameSpace(qualified_name);
326 code_.SetValue("STRUCT_NAME", name);
327 code_.SetValue("CPP_NAME", cpp_name);
328 code_.SetValue("NULLABLE_EXT", NullableExtension());
330 // The root datatype accessor:
331 code_ += "inline \\";
333 "const {{CPP_NAME}} *{{NULLABLE_EXT}}Get{{STRUCT_NAME}}(const void "
335 code_ += " return flatbuffers::GetRoot<{{CPP_NAME}}>(buf);";
339 code_ += "inline \\";
341 "const {{CPP_NAME}} *{{NULLABLE_EXT}}GetSizePrefixed{{STRUCT_NAME}}(const void "
343 code_ += " return flatbuffers::GetSizePrefixedRoot<{{CPP_NAME}}>(buf);";
347 if (parser_.opts.mutable_buffer) {
348 code_ += "inline \\";
349 code_ += "{{STRUCT_NAME}} *GetMutable{{STRUCT_NAME}}(void *buf) {";
350 code_ += " return flatbuffers::GetMutableRoot<{{STRUCT_NAME}}>(buf);";
355 if (parser_.file_identifier_.length()) {
356 // Return the identifier
357 code_ += "inline const char *{{STRUCT_NAME}}Identifier() {";
358 code_ += " return \"" + parser_.file_identifier_ + "\";";
362 // Check if a buffer has the identifier.
363 code_ += "inline \\";
364 code_ += "bool {{STRUCT_NAME}}BufferHasIdentifier(const void *buf) {";
365 code_ += " return flatbuffers::BufferHasIdentifier(";
366 code_ += " buf, {{STRUCT_NAME}}Identifier());";
371 // The root verifier.
372 if (parser_.file_identifier_.length()) {
373 code_.SetValue("ID", name + "Identifier()");
375 code_.SetValue("ID", "nullptr");
378 code_ += "inline bool Verify{{STRUCT_NAME}}Buffer(";
379 code_ += " flatbuffers::Verifier &verifier) {";
380 code_ += " return verifier.VerifyBuffer<{{CPP_NAME}}>({{ID}});";
384 code_ += "inline bool VerifySizePrefixed{{STRUCT_NAME}}Buffer(";
385 code_ += " flatbuffers::Verifier &verifier) {";
386 code_ += " return verifier.VerifySizePrefixedBuffer<{{CPP_NAME}}>({{ID}});";
390 if (parser_.file_extension_.length()) {
391 // Return the extension
392 code_ += "inline const char *{{STRUCT_NAME}}Extension() {";
393 code_ += " return \"" + parser_.file_extension_ + "\";";
398 // Finish a buffer with a given root object:
399 code_ += "inline void Finish{{STRUCT_NAME}}Buffer(";
400 code_ += " flatbuffers::FlatBufferBuilder &fbb,";
401 code_ += " flatbuffers::Offset<{{CPP_NAME}}> root) {";
402 if (parser_.file_identifier_.length())
403 code_ += " fbb.Finish(root, {{STRUCT_NAME}}Identifier());";
405 code_ += " fbb.Finish(root);";
409 code_ += "inline void FinishSizePrefixed{{STRUCT_NAME}}Buffer(";
410 code_ += " flatbuffers::FlatBufferBuilder &fbb,";
411 code_ += " flatbuffers::Offset<{{CPP_NAME}}> root) {";
412 if (parser_.file_identifier_.length())
413 code_ += " fbb.FinishSizePrefixed(root, {{STRUCT_NAME}}Identifier());";
415 code_ += " fbb.FinishSizePrefixed(root);";
419 if (parser_.opts.generate_object_based_api) {
420 // A convenient root unpack function.
422 NativeName(WrapInNameSpace(struct_def), &struct_def, parser_.opts);
423 code_.SetValue("UNPACK_RETURN",
424 GenTypeNativePtr(native_name, nullptr, false));
425 code_.SetValue("UNPACK_TYPE",
426 GenTypeNativePtr(native_name, nullptr, true));
428 code_ += "inline {{UNPACK_RETURN}} UnPack{{STRUCT_NAME}}(";
429 code_ += " const void *buf,";
430 code_ += " const flatbuffers::resolver_function_t *res = nullptr) {";
431 code_ += " return {{UNPACK_TYPE}}\\";
432 code_ += "(Get{{STRUCT_NAME}}(buf)->UnPack(res));";
438 if (cur_name_space_) SetNameSpace(nullptr);
440 // Close the include guard.
441 code_ += "#endif // " + include_guard;
443 const auto file_path = GeneratedFileName(path_, file_name_);
444 const auto final_code = code_.ToString();
445 return SaveFile(file_path.c_str(), final_code, false);
451 std::unordered_set<std::string> keywords_;
453 // This tracks the current namespace so we can insert namespace declarations.
454 const Namespace *cur_name_space_;
456 const Namespace *CurrentNameSpace() const { return cur_name_space_; }
458 // Translates a qualified name in flatbuffer text format to the same name in
459 // the equivalent C++ namespace.
460 static std::string TranslateNameSpace(const std::string &qualified_name) {
461 std::string cpp_qualified_name = qualified_name;
462 size_t start_pos = 0;
463 while ((start_pos = cpp_qualified_name.find(".", start_pos)) !=
465 cpp_qualified_name.replace(start_pos, 1, "::");
467 return cpp_qualified_name;
470 void GenComment(const std::vector<std::string> &dc, const char *prefix = "") {
472 ::flatbuffers::GenComment(dc, &text, nullptr, prefix);
473 code_ += text + "\\";
476 // Return a C++ type from the table in idl.h
477 std::string GenTypeBasic(const Type &type, bool user_facing_type) const {
478 static const char * const ctypename[] = {
480 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
482 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
483 #undef FLATBUFFERS_TD
486 if (user_facing_type) {
487 if (type.enum_def) return WrapInNameSpace(*type.enum_def);
488 if (type.base_type == BASE_TYPE_BOOL) return "bool";
490 return ctypename[type.base_type];
493 // Return a C++ pointer type, specialized to the actual struct/table types,
494 // and vector element types.
495 std::string GenTypePointer(const Type &type) const {
496 switch (type.base_type) {
497 case BASE_TYPE_STRING: {
498 return "flatbuffers::String";
500 case BASE_TYPE_VECTOR: {
501 const auto type_name = GenTypeWire(type.VectorType(), "", false);
502 return "flatbuffers::Vector<" + type_name + ">";
504 case BASE_TYPE_STRUCT: {
505 return WrapInNameSpace(*type.struct_def);
507 case BASE_TYPE_UNION:
509 default: { return "void"; }
513 // Return a C++ type for any type (scalar/pointer) specifically for
514 // building a flatbuffer.
515 std::string GenTypeWire(const Type &type, const char *postfix,
516 bool user_facing_type) const {
517 if (IsScalar(type.base_type)) {
518 return GenTypeBasic(type, user_facing_type) + postfix;
519 } else if (IsStruct(type)) {
520 return "const " + GenTypePointer(type) + " *";
522 return "flatbuffers::Offset<" + GenTypePointer(type) + ">" + postfix;
526 // Return a C++ type for any type (scalar/pointer) that reflects its
528 std::string GenTypeSize(const Type &type) const {
529 if (IsScalar(type.base_type)) {
530 return GenTypeBasic(type, false);
531 } else if (IsStruct(type)) {
532 return GenTypePointer(type);
534 return "flatbuffers::uoffset_t";
538 std::string NullableExtension() {
539 return parser_.opts.gen_nullable ? " _Nullable " : "";
542 static std::string NativeName(const std::string &name, const StructDef *sd,
543 const IDLOptions &opts) {
544 return sd && !sd->fixed ? opts.object_prefix + name + opts.object_suffix
548 const std::string &PtrType(const FieldDef *field) {
549 auto attr = field ? field->attributes.Lookup("cpp_ptr_type") : nullptr;
550 return attr ? attr->constant : parser_.opts.cpp_object_api_pointer_type;
553 const std::string NativeString(const FieldDef *field) {
554 auto attr = field ? field->attributes.Lookup("cpp_str_type") : nullptr;
555 auto &ret = attr ? attr->constant : parser_.opts.cpp_object_api_string_type;
556 if (ret.empty()) { return "std::string"; }
560 std::string GenTypeNativePtr(const std::string &type, const FieldDef *field,
561 bool is_constructor) {
562 auto &ptr_type = PtrType(field);
563 if (ptr_type != "naked") {
564 return ptr_type + "<" + type + ">";
565 } else if (is_constructor) {
572 std::string GenPtrGet(const FieldDef &field) {
573 auto cpp_ptr_type_get = field.attributes.Lookup("cpp_ptr_type_get");
574 if (cpp_ptr_type_get)
575 return cpp_ptr_type_get->constant;
576 auto &ptr_type = PtrType(&field);
577 return ptr_type == "naked" ? "" : ".get()";
580 std::string GenTypeNative(const Type &type, bool invector,
581 const FieldDef &field) {
582 switch (type.base_type) {
583 case BASE_TYPE_STRING: {
584 return NativeString(&field);
586 case BASE_TYPE_VECTOR: {
587 const auto type_name = GenTypeNative(type.VectorType(), true, field);
588 if (type.struct_def &&
589 type.struct_def->attributes.Lookup("native_custom_alloc")) {
590 auto native_custom_alloc =
591 type.struct_def->attributes.Lookup("native_custom_alloc");
592 return "std::vector<" + type_name + "," +
593 native_custom_alloc->constant + "<" + type_name + ">>";
595 return "std::vector<" + type_name + ">";
597 case BASE_TYPE_STRUCT: {
598 auto type_name = WrapInNameSpace(*type.struct_def);
599 if (IsStruct(type)) {
600 auto native_type = type.struct_def->attributes.Lookup("native_type");
601 if (native_type) { type_name = native_type->constant; }
602 if (invector || field.native_inline) {
605 return GenTypeNativePtr(type_name, &field, false);
608 return GenTypeNativePtr(
609 NativeName(type_name, type.struct_def, parser_.opts), &field,
613 case BASE_TYPE_UNION: {
614 return type.enum_def->name + "Union";
616 default: { return GenTypeBasic(type, true); }
620 // Return a C++ type for any type (scalar/pointer) specifically for
621 // using a flatbuffer.
622 std::string GenTypeGet(const Type &type, const char *afterbasic,
623 const char *beforeptr, const char *afterptr,
624 bool user_facing_type) {
625 if (IsScalar(type.base_type)) {
626 return GenTypeBasic(type, user_facing_type) + afterbasic;
628 return beforeptr + GenTypePointer(type) + afterptr;
632 std::string GenEnumDecl(const EnumDef &enum_def) const {
633 const IDLOptions &opts = parser_.opts;
634 return (opts.scoped_enums ? "enum class " : "enum ") + Name(enum_def);
637 std::string GenEnumValDecl(const EnumDef &enum_def,
638 const std::string &enum_val) const {
639 const IDLOptions &opts = parser_.opts;
640 return opts.prefixed_enums ? Name(enum_def) + "_" + enum_val : enum_val;
643 std::string GetEnumValUse(const EnumDef &enum_def,
644 const EnumVal &enum_val) const {
645 const IDLOptions &opts = parser_.opts;
646 if (opts.scoped_enums) {
647 return Name(enum_def) + "::" + Name(enum_val);
648 } else if (opts.prefixed_enums) {
649 return Name(enum_def) + "_" + Name(enum_val);
651 return Name(enum_val);
655 std::string StripUnionType(const std::string &name) {
656 return name.substr(0, name.size() - strlen(UnionTypeFieldSuffix()));
659 std::string GetUnionElement(const EnumVal &ev, bool wrap, bool actual_type,
660 bool native_type = false) {
661 if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
662 auto name = actual_type ? ev.union_type.struct_def->name : Name(ev);
663 return wrap ? WrapInNameSpace(ev.union_type.struct_def->defined_namespace,
666 } else if (ev.union_type.base_type == BASE_TYPE_STRING) {
667 return actual_type ? (native_type ? "std::string" : "flatbuffers::String")
670 FLATBUFFERS_ASSERT(false);
675 std::string UnionVerifySignature(const EnumDef &enum_def) {
676 return "bool Verify" + Name(enum_def) +
677 "(flatbuffers::Verifier &verifier, const void *obj, " +
678 Name(enum_def) + " type)";
681 std::string UnionVectorVerifySignature(const EnumDef &enum_def) {
682 return "bool Verify" + Name(enum_def) + "Vector" +
683 "(flatbuffers::Verifier &verifier, " +
684 "const flatbuffers::Vector<flatbuffers::Offset<void>> *values, " +
685 "const flatbuffers::Vector<uint8_t> *types)";
688 std::string UnionUnPackSignature(const EnumDef &enum_def, bool inclass) {
689 return (inclass ? "static " : "") + std::string("void *") +
690 (inclass ? "" : Name(enum_def) + "Union::") +
691 "UnPack(const void *obj, " + Name(enum_def) +
692 " type, const flatbuffers::resolver_function_t *resolver)";
695 std::string UnionPackSignature(const EnumDef &enum_def, bool inclass) {
696 return "flatbuffers::Offset<void> " +
697 (inclass ? "" : Name(enum_def) + "Union::") +
698 "Pack(flatbuffers::FlatBufferBuilder &_fbb, " +
699 "const flatbuffers::rehasher_function_t *_rehasher" +
700 (inclass ? " = nullptr" : "") + ") const";
703 std::string TableCreateSignature(const StructDef &struct_def, bool predecl,
704 const IDLOptions &opts) {
705 return "flatbuffers::Offset<" + Name(struct_def) + "> Create" +
706 Name(struct_def) + "(flatbuffers::FlatBufferBuilder &_fbb, const " +
707 NativeName(Name(struct_def), &struct_def, opts) +
708 " *_o, const flatbuffers::rehasher_function_t *_rehasher" +
709 (predecl ? " = nullptr" : "") + ")";
712 std::string TablePackSignature(const StructDef &struct_def, bool inclass,
713 const IDLOptions &opts) {
714 return std::string(inclass ? "static " : "") + "flatbuffers::Offset<" +
715 Name(struct_def) + "> " + (inclass ? "" : Name(struct_def) + "::") +
716 "Pack(flatbuffers::FlatBufferBuilder &_fbb, " + "const " +
717 NativeName(Name(struct_def), &struct_def, opts) + "* _o, " +
718 "const flatbuffers::rehasher_function_t *_rehasher" +
719 (inclass ? " = nullptr" : "") + ")";
722 std::string TableUnPackSignature(const StructDef &struct_def, bool inclass,
723 const IDLOptions &opts) {
724 return NativeName(Name(struct_def), &struct_def, opts) + " *" +
725 (inclass ? "" : Name(struct_def) + "::") +
726 "UnPack(const flatbuffers::resolver_function_t *_resolver" +
727 (inclass ? " = nullptr" : "") + ") const";
730 std::string TableUnPackToSignature(const StructDef &struct_def, bool inclass,
731 const IDLOptions &opts) {
732 return "void " + (inclass ? "" : Name(struct_def) + "::") + "UnPackTo(" +
733 NativeName(Name(struct_def), &struct_def, opts) + " *" +
734 "_o, const flatbuffers::resolver_function_t *_resolver" +
735 (inclass ? " = nullptr" : "") + ") const";
738 void GenMiniReflectPre(const StructDef *struct_def) {
739 code_.SetValue("NAME", struct_def->name);
740 code_ += "inline const flatbuffers::TypeTable *{{NAME}}TypeTable();";
744 void GenMiniReflect(const StructDef *struct_def, const EnumDef *enum_def) {
745 code_.SetValue("NAME", struct_def ? struct_def->name : enum_def->name);
746 code_.SetValue("SEQ_TYPE",
747 struct_def ? (struct_def->fixed ? "ST_STRUCT" : "ST_TABLE")
748 : (enum_def->is_union ? "ST_UNION" : "ST_ENUM"));
750 struct_def ? struct_def->fields.vec.size() : enum_def->vals.vec.size();
751 code_.SetValue("NUM_FIELDS", NumToString(num_fields));
752 std::vector<std::string> names;
753 std::vector<Type> types;
754 bool consecutive_enum_from_zero = true;
756 for (auto it = struct_def->fields.vec.begin();
757 it != struct_def->fields.vec.end(); ++it) {
758 const auto &field = **it;
759 names.push_back(Name(field));
760 types.push_back(field.value.type);
763 for (auto it = enum_def->vals.vec.begin(); it != enum_def->vals.vec.end();
765 const auto &ev = **it;
766 names.push_back(Name(ev));
767 types.push_back(enum_def->is_union ? ev.union_type
768 : Type(enum_def->underlying_type));
769 if (static_cast<int64_t>(it - enum_def->vals.vec.begin()) != ev.value) {
770 consecutive_enum_from_zero = false;
775 std::vector<std::string> type_refs;
776 for (auto it = types.begin(); it != types.end(); ++it) {
778 if (!ts.empty()) ts += ",\n ";
779 auto is_vector = type.base_type == BASE_TYPE_VECTOR;
780 auto bt = is_vector ? type.element : type.base_type;
781 auto et = IsScalar(bt) || bt == BASE_TYPE_STRING
782 ? bt - BASE_TYPE_UTYPE + ET_UTYPE
785 std::string ref_name =
787 ? WrapInNameSpace(*type.struct_def)
788 : type.enum_def ? WrapInNameSpace(*type.enum_def) : "";
789 if (!ref_name.empty()) {
790 auto rit = type_refs.begin();
791 for (; rit != type_refs.end(); ++rit) {
792 if (*rit == ref_name) {
793 ref_idx = static_cast<int>(rit - type_refs.begin());
797 if (rit == type_refs.end()) {
798 ref_idx = static_cast<int>(type_refs.size());
799 type_refs.push_back(ref_name);
802 ts += "{ flatbuffers::" + std::string(ElementaryTypeNames()[et]) + ", " +
803 NumToString(is_vector) + ", " + NumToString(ref_idx) + " }";
806 for (auto it = type_refs.begin(); it != type_refs.end(); ++it) {
807 if (!rs.empty()) rs += ",\n ";
808 rs += *it + "TypeTable";
811 for (auto it = names.begin(); it != names.end(); ++it) {
812 if (!ns.empty()) ns += ",\n ";
813 ns += "\"" + *it + "\"";
816 if (enum_def && !consecutive_enum_from_zero) {
817 for (auto it = enum_def->vals.vec.begin(); it != enum_def->vals.vec.end();
819 const auto &ev = **it;
820 if (!vs.empty()) vs += ", ";
821 vs += NumToString(ev.value);
823 } else if (struct_def && struct_def->fixed) {
824 for (auto it = struct_def->fields.vec.begin();
825 it != struct_def->fields.vec.end(); ++it) {
826 const auto &field = **it;
827 vs += NumToString(field.value.offset);
830 vs += NumToString(struct_def->bytesize);
832 code_.SetValue("TYPES", ts);
833 code_.SetValue("REFS", rs);
834 code_.SetValue("NAMES", ns);
835 code_.SetValue("VALUES", vs);
836 code_ += "inline const flatbuffers::TypeTable *{{NAME}}TypeTable() {";
838 code_ += " static const flatbuffers::TypeCode type_codes[] = {";
839 code_ += " {{TYPES}}";
842 if (!type_refs.empty()) {
843 code_ += " static const flatbuffers::TypeFunction type_refs[] = {";
844 code_ += " {{REFS}}";
848 code_ += " static const int32_t values[] = { {{VALUES}} };";
851 num_fields && parser_.opts.mini_reflect == IDLOptions::kTypesAndNames;
853 code_ += " static const char * const names[] = {";
854 code_ += " {{NAMES}}";
857 code_ += " static const flatbuffers::TypeTable tt = {";
858 code_ += std::string(" flatbuffers::{{SEQ_TYPE}}, {{NUM_FIELDS}}, ") +
859 (num_fields ? "type_codes, " : "nullptr, ") +
860 (!type_refs.empty() ? "type_refs, " : "nullptr, ") +
861 (!vs.empty() ? "values, " : "nullptr, ") +
862 (has_names ? "names" : "nullptr");
864 code_ += " return &tt;";
869 // Generate an enum declaration,
870 // an enum string lookup table,
871 // and an enum array of values
872 void GenEnum(const EnumDef &enum_def) {
873 code_.SetValue("ENUM_NAME", Name(enum_def));
874 code_.SetValue("BASE_TYPE", GenTypeBasic(enum_def.underlying_type, false));
875 code_.SetValue("SEP", "");
877 GenComment(enum_def.doc_comment);
878 code_ += GenEnumDecl(enum_def) + "\\";
879 if (parser_.opts.scoped_enums) code_ += " : {{BASE_TYPE}}\\";
883 const EnumVal *minv = nullptr, *maxv = nullptr;
884 for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
886 const auto &ev = **it;
888 GenComment(ev.doc_comment, " ");
889 code_.SetValue("KEY", GenEnumValDecl(enum_def, Name(ev)));
890 code_.SetValue("VALUE", NumToString(ev.value));
891 code_ += "{{SEP}} {{KEY}} = {{VALUE}}\\";
892 code_.SetValue("SEP", ",\n");
894 minv = !minv || minv->value > ev.value ? &ev : minv;
895 maxv = !maxv || maxv->value < ev.value ? &ev : maxv;
899 if (parser_.opts.scoped_enums || parser_.opts.prefixed_enums) {
900 FLATBUFFERS_ASSERT(minv && maxv);
902 code_.SetValue("SEP", ",\n");
903 if (enum_def.attributes.Lookup("bit_flags")) {
904 code_.SetValue("KEY", GenEnumValDecl(enum_def, "NONE"));
905 code_.SetValue("VALUE", "0");
906 code_ += "{{SEP}} {{KEY}} = {{VALUE}}\\";
908 code_.SetValue("KEY", GenEnumValDecl(enum_def, "ANY"));
909 code_.SetValue("VALUE", NumToString(anyv));
910 code_ += "{{SEP}} {{KEY}} = {{VALUE}}\\";
911 } else { // MIN & MAX are useless for bit_flags
912 code_.SetValue("KEY", GenEnumValDecl(enum_def, "MIN"));
913 code_.SetValue("VALUE", GenEnumValDecl(enum_def, minv->name));
914 code_ += "{{SEP}} {{KEY}} = {{VALUE}}\\";
916 code_.SetValue("KEY", GenEnumValDecl(enum_def, "MAX"));
917 code_.SetValue("VALUE", GenEnumValDecl(enum_def, maxv->name));
918 code_ += "{{SEP}} {{KEY}} = {{VALUE}}\\";
924 if (parser_.opts.scoped_enums && enum_def.attributes.Lookup("bit_flags")) {
925 code_ += "FLATBUFFERS_DEFINE_BITMASK_OPERATORS({{ENUM_NAME}}, {{BASE_TYPE}})";
929 // Generate an array of all enumeration values
930 auto num_fields = NumToString(enum_def.vals.vec.size());
931 code_ += "inline const {{ENUM_NAME}} (&EnumValues{{ENUM_NAME}}())[" + num_fields +
933 code_ += " static const {{ENUM_NAME}} values[] = {";
934 for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
936 const auto &ev = **it;
937 auto value = GetEnumValUse(enum_def, ev);
938 auto suffix = *it != enum_def.vals.vec.back() ? "," : "";
939 code_ += " " + value + suffix;
942 code_ += " return values;";
946 // Generate a generate string table for enum values.
947 // Problem is, if values are very sparse that could generate really big
948 // tables. Ideally in that case we generate a map lookup instead, but for
949 // the moment we simply don't output a table at all.
951 enum_def.vals.vec.back()->value - enum_def.vals.vec.front()->value + 1;
952 // Average distance between values above which we consider a table
953 // "too sparse". Change at will.
954 static const int kMaxSparseness = 5;
955 if (range / static_cast<int64_t>(enum_def.vals.vec.size()) <
957 code_ += "inline const char * const *EnumNames{{ENUM_NAME}}() {";
958 code_ += " static const char * const names[] = {";
960 auto val = enum_def.vals.vec.front()->value;
961 for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
963 const auto &ev = **it;
964 while (val++ != ev.value) { code_ += " \"\","; }
965 code_ += " \"" + Name(ev) + "\",";
970 code_ += " return names;";
974 code_ += "inline const char *EnumName{{ENUM_NAME}}({{ENUM_NAME}} e) {";
976 code_ += " const size_t index = static_cast<int>(e)\\";
977 if (enum_def.vals.vec.front()->value) {
978 auto vals = GetEnumValUse(enum_def, *enum_def.vals.vec.front());
979 code_ += " - static_cast<int>(" + vals + ")\\";
983 code_ += " return EnumNames{{ENUM_NAME}}()[index];";
988 // Generate type traits for unions to map from a type to union enum value.
989 if (enum_def.is_union && !enum_def.uses_type_aliases) {
990 for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
992 const auto &ev = **it;
994 if (it == enum_def.vals.vec.begin()) {
995 code_ += "template<typename T> struct {{ENUM_NAME}}Traits {";
997 auto name = GetUnionElement(ev, true, true);
998 code_ += "template<> struct {{ENUM_NAME}}Traits<" + name + "> {";
1001 auto value = GetEnumValUse(enum_def, ev);
1002 code_ += " static const {{ENUM_NAME}} enum_value = " + value + ";";
1008 if (parser_.opts.generate_object_based_api && enum_def.is_union) {
1009 // Generate a union type
1010 code_.SetValue("NAME", Name(enum_def));
1011 code_.SetValue("NONE",
1012 GetEnumValUse(enum_def, *enum_def.vals.Lookup("NONE")));
1014 code_ += "struct {{NAME}}Union {";
1015 code_ += " {{NAME}} type;";
1016 code_ += " void *value;";
1018 code_ += " {{NAME}}Union() : type({{NONE}}), value(nullptr) {}";
1019 code_ += " {{NAME}}Union({{NAME}}Union&& u) FLATBUFFERS_NOEXCEPT :";
1020 code_ += " type({{NONE}}), value(nullptr)";
1021 code_ += " { std::swap(type, u.type); std::swap(value, u.value); }";
1022 code_ += " {{NAME}}Union(const {{NAME}}Union &) FLATBUFFERS_NOEXCEPT;";
1024 " {{NAME}}Union &operator=(const {{NAME}}Union &u) "
1025 "FLATBUFFERS_NOEXCEPT";
1027 " { {{NAME}}Union t(u); std::swap(type, t.type); std::swap(value, "
1028 "t.value); return *this; }";
1030 " {{NAME}}Union &operator=({{NAME}}Union &&u) FLATBUFFERS_NOEXCEPT";
1032 " { std::swap(type, u.type); std::swap(value, u.value); return "
1034 code_ += " ~{{NAME}}Union() { Reset(); }";
1036 code_ += " void Reset();";
1038 if (!enum_def.uses_type_aliases) {
1039 code_ += "#ifndef FLATBUFFERS_CPP98_STL";
1040 code_ += " template <typename T>";
1041 code_ += " void Set(T&& val) {";
1042 code_ += " Reset();";
1044 " type = {{NAME}}Traits<typename T::TableType>::enum_value;";
1045 code_ += " if (type != {{NONE}}) {";
1046 code_ += " value = new T(std::forward<T>(val));";
1049 code_ += "#endif // FLATBUFFERS_CPP98_STL";
1052 code_ += " " + UnionUnPackSignature(enum_def, true) + ";";
1053 code_ += " " + UnionPackSignature(enum_def, true) + ";";
1056 for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1058 const auto &ev = **it;
1059 if (!ev.value) { continue; }
1061 const auto native_type =
1062 NativeName(GetUnionElement(ev, true, true, true),
1063 ev.union_type.struct_def, parser_.opts);
1064 code_.SetValue("NATIVE_TYPE", native_type);
1065 code_.SetValue("NATIVE_NAME", Name(ev));
1066 code_.SetValue("NATIVE_ID", GetEnumValUse(enum_def, ev));
1068 code_ += " {{NATIVE_TYPE}} *As{{NATIVE_NAME}}() {";
1069 code_ += " return type == {{NATIVE_ID}} ?";
1070 code_ += " reinterpret_cast<{{NATIVE_TYPE}} *>(value) : nullptr;";
1073 code_ += " const {{NATIVE_TYPE}} *As{{NATIVE_NAME}}() const {";
1074 code_ += " return type == {{NATIVE_ID}} ?";
1076 " reinterpret_cast<const {{NATIVE_TYPE}} *>(value) : nullptr;";
1083 if (enum_def.is_union) {
1084 code_ += UnionVerifySignature(enum_def) + ";";
1085 code_ += UnionVectorVerifySignature(enum_def) + ";";
1090 void GenUnionPost(const EnumDef &enum_def) {
1091 // Generate a verifier function for this union that can be called by the
1092 // table verifier functions. It uses a switch case to select a specific
1093 // verifier function to call, this should be safe even if the union type
1094 // has been corrupted, since the verifiers will simply fail when called
1095 // on the wrong type.
1096 code_.SetValue("ENUM_NAME", Name(enum_def));
1098 code_ += "inline " + UnionVerifySignature(enum_def) + " {";
1099 code_ += " switch (type) {";
1100 for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1102 const auto &ev = **it;
1103 code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
1106 code_.SetValue("TYPE", GetUnionElement(ev, true, true));
1107 code_ += " case {{LABEL}}: {";
1109 " auto ptr = reinterpret_cast<const {{TYPE}} *>(obj);";
1110 if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
1111 if (ev.union_type.struct_def->fixed) {
1112 code_ += " return true;";
1115 code_ += " return verifier.VerifyTable(ptr);";
1117 } else if (ev.union_type.base_type == BASE_TYPE_STRING) {
1119 code_ += " return verifier.Verify(ptr);";
1121 FLATBUFFERS_ASSERT(false);
1125 code_ += " case {{LABEL}}: {";
1126 code_ += " return true;"; // "NONE" enum value.
1130 code_ += " default: return false;";
1135 code_ += "inline " + UnionVectorVerifySignature(enum_def) + " {";
1136 code_ += " if (!values || !types) return !values && !types;";
1137 code_ += " if (values->size() != types->size()) return false;";
1138 code_ += " for (flatbuffers::uoffset_t i = 0; i < values->size(); ++i) {";
1139 code_ += " if (!Verify" + Name(enum_def) + "(";
1140 code_ += " verifier, values->Get(i), types->GetEnum<" +
1141 Name(enum_def) + ">(i))) {";
1142 code_ += " return false;";
1145 code_ += " return true;";
1149 if (parser_.opts.generate_object_based_api) {
1150 // Generate union Unpack() and Pack() functions.
1151 code_ += "inline " + UnionUnPackSignature(enum_def, false) + " {";
1152 code_ += " switch (type) {";
1153 for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1155 const auto &ev = **it;
1156 if (!ev.value) { continue; }
1158 code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
1159 code_.SetValue("TYPE", GetUnionElement(ev, true, true));
1160 code_ += " case {{LABEL}}: {";
1161 code_ += " auto ptr = reinterpret_cast<const {{TYPE}} *>(obj);";
1162 if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
1163 if (ev.union_type.struct_def->fixed) {
1164 code_ += " return new " +
1165 WrapInNameSpace(*ev.union_type.struct_def) + "(*ptr);";
1167 code_ += " return ptr->UnPack(resolver);";
1169 } else if (ev.union_type.base_type == BASE_TYPE_STRING) {
1170 code_ += " return new std::string(ptr->c_str(), ptr->size());";
1172 FLATBUFFERS_ASSERT(false);
1176 code_ += " default: return nullptr;";
1181 code_ += "inline " + UnionPackSignature(enum_def, false) + " {";
1182 code_ += " switch (type) {";
1183 for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1186 if (!ev.value) { continue; }
1188 code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
1189 code_.SetValue("TYPE",
1190 NativeName(GetUnionElement(ev, true, true, true),
1191 ev.union_type.struct_def, parser_.opts));
1192 code_.SetValue("NAME", GetUnionElement(ev, false, true));
1193 code_ += " case {{LABEL}}: {";
1194 code_ += " auto ptr = reinterpret_cast<const {{TYPE}} *>(value);";
1195 if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
1196 if (ev.union_type.struct_def->fixed) {
1197 code_ += " return _fbb.CreateStruct(*ptr).Union();";
1200 " return Create{{NAME}}(_fbb, ptr, _rehasher).Union();";
1202 } else if (ev.union_type.base_type == BASE_TYPE_STRING) {
1203 code_ += " return _fbb.CreateString(*ptr).Union();";
1205 FLATBUFFERS_ASSERT(false);
1209 code_ += " default: return 0;";
1214 // Union copy constructor
1216 "inline {{ENUM_NAME}}Union::{{ENUM_NAME}}Union(const "
1217 "{{ENUM_NAME}}Union &u) FLATBUFFERS_NOEXCEPT : type(u.type), "
1219 code_ += " switch (type) {";
1220 for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1222 const auto &ev = **it;
1223 if (!ev.value) { continue; }
1224 code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
1225 code_.SetValue("TYPE",
1226 NativeName(GetUnionElement(ev, true, true, true),
1227 ev.union_type.struct_def, parser_.opts));
1228 code_ += " case {{LABEL}}: {";
1229 bool copyable = true;
1230 if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
1231 // Don't generate code to copy if table is not copyable.
1232 // TODO(wvo): make tables copyable instead.
1233 for (auto fit = ev.union_type.struct_def->fields.vec.begin();
1234 fit != ev.union_type.struct_def->fields.vec.end(); ++fit) {
1235 const auto &field = **fit;
1236 if (!field.deprecated && field.value.type.struct_def) {
1244 " value = new {{TYPE}}(*reinterpret_cast<{{TYPE}} *>"
1247 code_ += " FLATBUFFERS_ASSERT(false); // {{TYPE}} not copyable.";
1252 code_ += " default:";
1258 // Union Reset() function.
1259 code_.SetValue("NONE",
1260 GetEnumValUse(enum_def, *enum_def.vals.Lookup("NONE")));
1262 code_ += "inline void {{ENUM_NAME}}Union::Reset() {";
1263 code_ += " switch (type) {";
1264 for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
1266 const auto &ev = **it;
1267 if (!ev.value) { continue; }
1268 code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
1269 code_.SetValue("TYPE",
1270 NativeName(GetUnionElement(ev, true, true, true),
1271 ev.union_type.struct_def, parser_.opts));
1272 code_ += " case {{LABEL}}: {";
1273 code_ += " auto ptr = reinterpret_cast<{{TYPE}} *>(value);";
1274 code_ += " delete ptr;";
1278 code_ += " default: break;";
1280 code_ += " value = nullptr;";
1281 code_ += " type = {{NONE}};";
1287 // Generates a value with optionally a cast applied if the field has a
1288 // different underlying type from its interface type (currently only the
1289 // case for enums. "from" specify the direction, true meaning from the
1290 // underlying type to the interface type.
1291 std::string GenUnderlyingCast(const FieldDef &field, bool from,
1292 const std::string &val) {
1293 if (from && field.value.type.base_type == BASE_TYPE_BOOL) {
1294 return val + " != 0";
1295 } else if ((field.value.type.enum_def &&
1296 IsScalar(field.value.type.base_type)) ||
1297 field.value.type.base_type == BASE_TYPE_BOOL) {
1298 return "static_cast<" + GenTypeBasic(field.value.type, from) + ">(" +
1305 std::string GenFieldOffsetName(const FieldDef &field) {
1306 std::string uname = Name(field);
1307 std::transform(uname.begin(), uname.end(), uname.begin(), ToUpper);
1308 return "VT_" + uname;
1311 void GenFullyQualifiedNameGetter(const StructDef &struct_def,
1312 const std::string &name) {
1313 if (!parser_.opts.generate_name_strings) { return; }
1314 auto fullname = struct_def.defined_namespace->GetFullyQualifiedName(name);
1315 code_.SetValue("NAME", fullname);
1316 code_.SetValue("CONSTEXPR", "FLATBUFFERS_CONSTEXPR");
1317 code_ += " static {{CONSTEXPR}} const char *GetFullyQualifiedName() {";
1318 code_ += " return \"{{NAME}}\";";
1322 std::string GenDefaultConstant(const FieldDef &field) {
1323 return field.value.type.base_type == BASE_TYPE_FLOAT
1324 ? field.value.constant + "f"
1325 : field.value.constant;
1328 std::string GetDefaultScalarValue(const FieldDef &field, bool is_ctor) {
1329 if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) {
1330 auto ev = field.value.type.enum_def->ReverseLookup(
1331 StringToInt(field.value.constant.c_str()), false);
1333 return WrapInNameSpace(field.value.type.enum_def->defined_namespace,
1334 GetEnumValUse(*field.value.type.enum_def, *ev));
1336 return GenUnderlyingCast(field, true, field.value.constant);
1338 } else if (field.value.type.base_type == BASE_TYPE_BOOL) {
1339 return field.value.constant == "0" ? "false" : "true";
1340 } else if (field.attributes.Lookup("cpp_type")) {
1342 if (PtrType(&field) == "naked") {
1351 return GenDefaultConstant(field);
1355 void GenParam(const FieldDef &field, bool direct, const char *prefix) {
1356 code_.SetValue("PRE", prefix);
1357 code_.SetValue("PARAM_NAME", Name(field));
1358 if (direct && field.value.type.base_type == BASE_TYPE_STRING) {
1359 code_.SetValue("PARAM_TYPE", "const char *");
1360 code_.SetValue("PARAM_VALUE", "nullptr");
1361 } else if (direct && field.value.type.base_type == BASE_TYPE_VECTOR) {
1362 const auto vtype = field.value.type.VectorType();
1364 if (IsStruct(vtype)) {
1365 type = WrapInNameSpace(*vtype.struct_def);
1367 type = GenTypeWire(vtype, "", false);
1369 code_.SetValue("PARAM_TYPE", "const std::vector<" + type + "> *");
1370 code_.SetValue("PARAM_VALUE", "nullptr");
1372 code_.SetValue("PARAM_TYPE", GenTypeWire(field.value.type, " ", true));
1373 code_.SetValue("PARAM_VALUE", GetDefaultScalarValue(field, false));
1375 code_ += "{{PRE}}{{PARAM_TYPE}}{{PARAM_NAME}} = {{PARAM_VALUE}}\\";
1378 // Generate a member, including a default value for scalars and raw pointers.
1379 void GenMember(const FieldDef &field) {
1380 if (!field.deprecated && // Deprecated fields won't be accessible.
1381 field.value.type.base_type != BASE_TYPE_UTYPE &&
1382 (field.value.type.base_type != BASE_TYPE_VECTOR ||
1383 field.value.type.element != BASE_TYPE_UTYPE)) {
1384 auto type = GenTypeNative(field.value.type, false, field);
1385 auto cpp_type = field.attributes.Lookup("cpp_type");
1387 (cpp_type ? (field.value.type.base_type == BASE_TYPE_VECTOR
1388 ? "std::vector<" + GenTypeNativePtr(cpp_type->constant, &field, false) + "> "
1389 : GenTypeNativePtr(cpp_type->constant, &field, false))
1391 code_.SetValue("FIELD_TYPE", full_type);
1392 code_.SetValue("FIELD_NAME", Name(field));
1393 code_ += " {{FIELD_TYPE}}{{FIELD_NAME}};";
1397 // Generate the default constructor for this struct. Properly initialize all
1398 // scalar members with default values.
1399 void GenDefaultConstructor(const StructDef &struct_def) {
1400 std::string initializer_list;
1401 for (auto it = struct_def.fields.vec.begin();
1402 it != struct_def.fields.vec.end(); ++it) {
1403 const auto &field = **it;
1404 if (!field.deprecated && // Deprecated fields won't be accessible.
1405 field.value.type.base_type != BASE_TYPE_UTYPE) {
1406 auto cpp_type = field.attributes.Lookup("cpp_type");
1407 auto native_default = field.attributes.Lookup("native_default");
1408 // Scalar types get parsed defaults, raw pointers get nullptrs.
1409 if (IsScalar(field.value.type.base_type)) {
1410 if (!initializer_list.empty()) { initializer_list += ",\n "; }
1411 initializer_list += Name(field);
1412 initializer_list += "(" + (native_default ? std::string(native_default->constant) : GetDefaultScalarValue(field, true)) + ")";
1413 } else if (field.value.type.base_type == BASE_TYPE_STRUCT) {
1414 if (IsStruct(field.value.type)) {
1415 if (native_default) {
1416 if (!initializer_list.empty()) {
1417 initializer_list += ",\n ";
1420 Name(field) + "(" + native_default->constant + ")";
1423 } else if (cpp_type && field.value.type.base_type != BASE_TYPE_VECTOR) {
1424 if (!initializer_list.empty()) { initializer_list += ",\n "; }
1425 initializer_list += Name(field) + "(0)";
1429 if (!initializer_list.empty()) {
1430 initializer_list = "\n : " + initializer_list;
1433 code_.SetValue("NATIVE_NAME",
1434 NativeName(Name(struct_def), &struct_def, parser_.opts));
1435 code_.SetValue("INIT_LIST", initializer_list);
1437 code_ += " {{NATIVE_NAME}}(){{INIT_LIST}} {";
1441 void GenOperatorNewDelete(const StructDef &struct_def) {
1442 if (auto native_custom_alloc =
1443 struct_def.attributes.Lookup("native_custom_alloc")) {
1444 code_ += " inline void *operator new (std::size_t count) {";
1445 code_ += " return " + native_custom_alloc->constant +
1446 "<{{NATIVE_NAME}}>().allocate(count / sizeof({{NATIVE_NAME}}));";
1448 code_ += " inline void operator delete (void *ptr) {";
1449 code_ += " return " + native_custom_alloc->constant +
1450 "<{{NATIVE_NAME}}>().deallocate(static_cast<{{NATIVE_NAME}}*>("
1456 void GenNativeTable(const StructDef &struct_def) {
1457 const auto native_name =
1458 NativeName(Name(struct_def), &struct_def, parser_.opts);
1459 code_.SetValue("STRUCT_NAME", Name(struct_def));
1460 code_.SetValue("NATIVE_NAME", native_name);
1462 // Generate a C++ object that can hold an unpacked version of this table.
1463 code_ += "struct {{NATIVE_NAME}} : public flatbuffers::NativeTable {";
1464 code_ += " typedef {{STRUCT_NAME}} TableType;";
1465 GenFullyQualifiedNameGetter(struct_def, native_name);
1466 for (auto it = struct_def.fields.vec.begin();
1467 it != struct_def.fields.vec.end(); ++it) {
1470 GenOperatorNewDelete(struct_def);
1471 GenDefaultConstructor(struct_def);
1476 // Generate the code to call the appropriate Verify function(s) for a field.
1477 void GenVerifyCall(const FieldDef &field, const char *prefix) {
1478 code_.SetValue("PRE", prefix);
1479 code_.SetValue("NAME", Name(field));
1480 code_.SetValue("REQUIRED", field.required ? "Required" : "");
1481 code_.SetValue("SIZE", GenTypeSize(field.value.type));
1482 code_.SetValue("OFFSET", GenFieldOffsetName(field));
1483 if (IsScalar(field.value.type.base_type) || IsStruct(field.value.type)) {
1485 "{{PRE}}VerifyField{{REQUIRED}}<{{SIZE}}>(verifier, {{OFFSET}})\\";
1487 code_ += "{{PRE}}VerifyOffset{{REQUIRED}}(verifier, {{OFFSET}})\\";
1490 switch (field.value.type.base_type) {
1491 case BASE_TYPE_UNION: {
1492 code_.SetValue("ENUM_NAME", field.value.type.enum_def->name);
1493 code_.SetValue("SUFFIX", UnionTypeFieldSuffix());
1495 "{{PRE}}Verify{{ENUM_NAME}}(verifier, {{NAME}}(), "
1496 "{{NAME}}{{SUFFIX}}())\\";
1499 case BASE_TYPE_STRUCT: {
1500 if (!field.value.type.struct_def->fixed) {
1501 code_ += "{{PRE}}verifier.VerifyTable({{NAME}}())\\";
1505 case BASE_TYPE_STRING: {
1506 code_ += "{{PRE}}verifier.Verify({{NAME}}())\\";
1509 case BASE_TYPE_VECTOR: {
1510 code_ += "{{PRE}}verifier.Verify({{NAME}}())\\";
1512 switch (field.value.type.element) {
1513 case BASE_TYPE_STRING: {
1514 code_ += "{{PRE}}verifier.VerifyVectorOfStrings({{NAME}}())\\";
1517 case BASE_TYPE_STRUCT: {
1518 if (!field.value.type.struct_def->fixed) {
1519 code_ += "{{PRE}}verifier.VerifyVectorOfTables({{NAME}}())\\";
1523 case BASE_TYPE_UNION: {
1524 code_.SetValue("ENUM_NAME", field.value.type.enum_def->name);
1526 "{{PRE}}Verify{{ENUM_NAME}}Vector(verifier, {{NAME}}(), "
1527 "{{NAME}}_type())\\";
1538 // Generate CompareWithValue method for a key field.
1539 void GenKeyFieldMethods(const FieldDef &field) {
1540 FLATBUFFERS_ASSERT(field.key);
1541 const bool is_string = (field.value.type.base_type == BASE_TYPE_STRING);
1543 code_ += " bool KeyCompareLessThan(const {{STRUCT_NAME}} *o) const {";
1545 // use operator< of flatbuffers::String
1546 code_ += " return *{{FIELD_NAME}}() < *o->{{FIELD_NAME}}();";
1548 code_ += " return {{FIELD_NAME}}() < o->{{FIELD_NAME}}();";
1553 code_ += " int KeyCompareWithValue(const char *val) const {";
1554 code_ += " return strcmp({{FIELD_NAME}}()->c_str(), val);";
1557 FLATBUFFERS_ASSERT(IsScalar(field.value.type.base_type));
1558 auto type = GenTypeBasic(field.value.type, false);
1559 if (parser_.opts.scoped_enums && field.value.type.enum_def &&
1560 IsScalar(field.value.type.base_type)) {
1561 type = GenTypeGet(field.value.type, " ", "const ", " *", true);
1563 // Returns {field<val: -1, field==val: 0, field>val: +1}.
1564 code_.SetValue("KEY_TYPE", type);
1565 code_ += " int KeyCompareWithValue({{KEY_TYPE}} val) const {";
1567 " return static_cast<int>({{FIELD_NAME}}() > val) - "
1568 "static_cast<int>({{FIELD_NAME}}() < val);";
1573 // Generate an accessor struct, builder structs & function for a table.
1574 void GenTable(const StructDef &struct_def) {
1575 if (parser_.opts.generate_object_based_api) { GenNativeTable(struct_def); }
1577 // Generate an accessor struct, with methods of the form:
1578 // type name() const { return GetField<type>(offset, defaultval); }
1579 GenComment(struct_def.doc_comment);
1581 code_.SetValue("STRUCT_NAME", Name(struct_def));
1583 "struct {{STRUCT_NAME}} FLATBUFFERS_FINAL_CLASS"
1584 " : private flatbuffers::Table {";
1585 if (parser_.opts.generate_object_based_api) {
1586 code_ += " typedef {{NATIVE_NAME}} NativeTableType;";
1588 if (parser_.opts.mini_reflect != IDLOptions::kNone) {
1589 code_ += " static const flatbuffers::TypeTable *MiniReflectTypeTable() {";
1590 code_ += " return {{STRUCT_NAME}}TypeTable();";
1595 GenFullyQualifiedNameGetter(struct_def, Name(struct_def));
1597 // Generate field id constants.
1598 if (struct_def.fields.vec.size() > 0) {
1599 // We need to add a trailing comma to all elements except the last one as
1600 // older versions of gcc complain about this.
1601 code_.SetValue("SEP", "");
1603 for (auto it = struct_def.fields.vec.begin();
1604 it != struct_def.fields.vec.end(); ++it) {
1605 const auto &field = **it;
1606 if (field.deprecated) {
1607 // Deprecated fields won't be accessible.
1611 code_.SetValue("OFFSET_NAME", GenFieldOffsetName(field));
1612 code_.SetValue("OFFSET_VALUE", NumToString(field.value.offset));
1613 code_ += "{{SEP}} {{OFFSET_NAME}} = {{OFFSET_VALUE}}\\";
1614 code_.SetValue("SEP", ",\n");
1620 // Generate the accessors.
1621 for (auto it = struct_def.fields.vec.begin();
1622 it != struct_def.fields.vec.end(); ++it) {
1623 const auto &field = **it;
1624 if (field.deprecated) {
1625 // Deprecated fields won't be accessible.
1629 const bool is_struct = IsStruct(field.value.type);
1630 const bool is_scalar = IsScalar(field.value.type.base_type);
1631 code_.SetValue("FIELD_NAME", Name(field));
1633 // Call a different accessor for pointers, that indirects.
1634 std::string accessor = "";
1636 accessor = "GetField<";
1637 } else if (is_struct) {
1638 accessor = "GetStruct<";
1640 accessor = "GetPointer<";
1642 auto offset_str = GenFieldOffsetName(field);
1644 GenTypeGet(field.value.type, "", "const ", " *", false);
1646 auto call = accessor + offset_type + ">(" + offset_str;
1647 // Default value as second arg for non-pointer types.
1648 if (is_scalar) { call += ", " + GenDefaultConstant(field); }
1651 std::string afterptr = " *" + NullableExtension();
1652 GenComment(field.doc_comment, " ");
1653 code_.SetValue("FIELD_TYPE", GenTypeGet(field.value.type, " ", "const ",
1654 afterptr.c_str(), true));
1655 code_.SetValue("FIELD_VALUE", GenUnderlyingCast(field, true, call));
1656 code_.SetValue("NULLABLE_EXT", NullableExtension());
1658 code_ += " {{FIELD_TYPE}}{{FIELD_NAME}}() const {";
1659 code_ += " return {{FIELD_VALUE}};";
1662 if (field.value.type.base_type == BASE_TYPE_UNION) {
1663 auto u = field.value.type.enum_def;
1666 " template<typename T> "
1667 "const T *{{NULLABLE_EXT}}{{FIELD_NAME}}_as() const;";
1669 for (auto u_it = u->vals.vec.begin(); u_it != u->vals.vec.end();
1672 if (ev.union_type.base_type == BASE_TYPE_NONE) { continue; }
1673 auto full_struct_name = GetUnionElement(ev, true, true);
1675 // @TODO: Mby make this decisions more universal? How?
1678 EscapeKeyword(field.name + UnionTypeFieldSuffix()));
1681 WrapInNameSpace(u->defined_namespace, GetEnumValUse(*u, ev)));
1682 code_.SetValue("U_FIELD_TYPE", "const " + full_struct_name + " *");
1683 code_.SetValue("U_FIELD_NAME", Name(field) + "_as_" + Name(ev));
1684 code_.SetValue("U_NULLABLE", NullableExtension());
1686 // `const Type *union_name_asType() const` accessor.
1687 code_ += " {{U_FIELD_TYPE}}{{U_NULLABLE}}{{U_FIELD_NAME}}() const {";
1689 " return {{U_GET_TYPE}}() == {{U_ELEMENT_TYPE}} ? "
1690 "static_cast<{{U_FIELD_TYPE}}>({{FIELD_NAME}}()) "
1696 if (parser_.opts.mutable_buffer) {
1698 const auto type = GenTypeWire(field.value.type, "", false);
1699 code_.SetValue("SET_FN", "SetField<" + type + ">");
1700 code_.SetValue("OFFSET_NAME", offset_str);
1701 code_.SetValue("FIELD_TYPE", GenTypeBasic(field.value.type, true));
1702 code_.SetValue("FIELD_VALUE",
1703 GenUnderlyingCast(field, false, "_" + Name(field)));
1704 code_.SetValue("DEFAULT_VALUE", GenDefaultConstant(field));
1707 " bool mutate_{{FIELD_NAME}}({{FIELD_TYPE}} "
1708 "_{{FIELD_NAME}}) {";
1710 " return {{SET_FN}}({{OFFSET_NAME}}, {{FIELD_VALUE}}, "
1711 "{{DEFAULT_VALUE}});";
1714 auto postptr = " *" + NullableExtension();
1716 GenTypeGet(field.value.type, " ", "", postptr.c_str(), true);
1717 auto underlying = accessor + type + ">(" + offset_str + ")";
1718 code_.SetValue("FIELD_TYPE", type);
1719 code_.SetValue("FIELD_VALUE",
1720 GenUnderlyingCast(field, true, underlying));
1722 code_ += " {{FIELD_TYPE}}mutable_{{FIELD_NAME}}() {";
1723 code_ += " return {{FIELD_VALUE}};";
1728 auto nested = field.attributes.Lookup("nested_flatbuffer");
1730 std::string qualified_name = nested->constant;
1731 auto nested_root = parser_.LookupStruct(nested->constant);
1732 if (nested_root == nullptr) {
1733 qualified_name = parser_.current_namespace_->GetFullyQualifiedName(
1735 nested_root = parser_.LookupStruct(qualified_name);
1737 FLATBUFFERS_ASSERT(nested_root); // Guaranteed to exist by parser.
1739 code_.SetValue("CPP_NAME", TranslateNameSpace(qualified_name));
1741 code_ += " const {{CPP_NAME}} *{{FIELD_NAME}}_nested_root() const {";
1742 code_ += " return flatbuffers::GetRoot<{{CPP_NAME}}>({{FIELD_NAME}}()->Data());";
1746 if (field.flexbuffer) {
1748 " flexbuffers::Reference {{FIELD_NAME}}_flexbuffer_root()"
1750 // Both Data() and size() are const-methods, therefore call order doesn't matter.
1752 " return flexbuffers::GetRoot({{FIELD_NAME}}()->Data(), "
1753 "{{FIELD_NAME}}()->size());";
1757 // Generate a comparison function for this field if it is a key.
1759 GenKeyFieldMethods(field);
1763 // Generate a verifier function that can check a buffer from an untrusted
1764 // source will never cause reads outside the buffer.
1765 code_ += " bool Verify(flatbuffers::Verifier &verifier) const {";
1766 code_ += " return VerifyTableStart(verifier)\\";
1767 for (auto it = struct_def.fields.vec.begin();
1768 it != struct_def.fields.vec.end(); ++it) {
1769 const auto &field = **it;
1770 if (field.deprecated) { continue; }
1771 GenVerifyCall(field, " &&\n ");
1774 code_ += " &&\n verifier.EndTable();";
1777 if (parser_.opts.generate_object_based_api) {
1778 // Generate the UnPack() pre declaration.
1780 " " + TableUnPackSignature(struct_def, true, parser_.opts) + ";";
1782 " " + TableUnPackToSignature(struct_def, true, parser_.opts) + ";";
1783 code_ += " " + TablePackSignature(struct_def, true, parser_.opts) + ";";
1786 code_ += "};"; // End of table.
1789 // Explicit specializations for union accessors
1790 for (auto it = struct_def.fields.vec.begin();
1791 it != struct_def.fields.vec.end(); ++it) {
1792 const auto &field = **it;
1793 if (field.deprecated || field.value.type.base_type != BASE_TYPE_UNION) {
1797 auto u = field.value.type.enum_def;
1798 if (u->uses_type_aliases) continue;
1800 code_.SetValue("FIELD_NAME", Name(field));
1802 for (auto u_it = u->vals.vec.begin(); u_it != u->vals.vec.end(); ++u_it) {
1804 if (ev.union_type.base_type == BASE_TYPE_NONE) { continue; }
1806 auto full_struct_name = GetUnionElement(ev, true, true);
1810 WrapInNameSpace(u->defined_namespace, GetEnumValUse(*u, ev)));
1811 code_.SetValue("U_FIELD_TYPE", "const " + full_struct_name + " *");
1812 code_.SetValue("U_ELEMENT_NAME", full_struct_name);
1813 code_.SetValue("U_FIELD_NAME", Name(field) + "_as_" + Name(ev));
1815 // `template<> const T *union_name_as<T>() const` accessor.
1818 "inline {{U_FIELD_TYPE}}{{STRUCT_NAME}}::{{FIELD_NAME}}_as"
1819 "<{{U_ELEMENT_NAME}}>() const {";
1820 code_ += " return {{U_FIELD_NAME}}();";
1826 GenBuilders(struct_def);
1828 if (parser_.opts.generate_object_based_api) {
1829 // Generate a pre-declaration for a CreateX method that works with an
1830 // unpacked C++ object.
1831 code_ += TableCreateSignature(struct_def, true, parser_.opts) + ";";
1836 void GenBuilders(const StructDef &struct_def) {
1837 code_.SetValue("STRUCT_NAME", Name(struct_def));
1839 // Generate a builder struct:
1840 code_ += "struct {{STRUCT_NAME}}Builder {";
1841 code_ += " flatbuffers::FlatBufferBuilder &fbb_;";
1842 code_ += " flatbuffers::uoffset_t start_;";
1844 bool has_string_or_vector_fields = false;
1845 for (auto it = struct_def.fields.vec.begin();
1846 it != struct_def.fields.vec.end(); ++it) {
1847 const auto &field = **it;
1848 if (!field.deprecated) {
1849 const bool is_scalar = IsScalar(field.value.type.base_type);
1850 const bool is_string = field.value.type.base_type == BASE_TYPE_STRING;
1851 const bool is_vector = field.value.type.base_type == BASE_TYPE_VECTOR;
1852 if (is_string || is_vector) { has_string_or_vector_fields = true; }
1854 std::string offset = GenFieldOffsetName(field);
1855 std::string name = GenUnderlyingCast(field, false, Name(field));
1856 std::string value = is_scalar ? GenDefaultConstant(field) : "";
1858 // Generate accessor functions of the form:
1859 // void add_name(type name) {
1860 // fbb_.AddElement<type>(offset, name, default);
1862 code_.SetValue("FIELD_NAME", Name(field));
1863 code_.SetValue("FIELD_TYPE", GenTypeWire(field.value.type, " ", true));
1864 code_.SetValue("ADD_OFFSET", Name(struct_def) + "::" + offset);
1865 code_.SetValue("ADD_NAME", name);
1866 code_.SetValue("ADD_VALUE", value);
1868 const auto type = GenTypeWire(field.value.type, "", false);
1869 code_.SetValue("ADD_FN", "AddElement<" + type + ">");
1870 } else if (IsStruct(field.value.type)) {
1871 code_.SetValue("ADD_FN", "AddStruct");
1873 code_.SetValue("ADD_FN", "AddOffset");
1876 code_ += " void add_{{FIELD_NAME}}({{FIELD_TYPE}}{{FIELD_NAME}}) {";
1877 code_ += " fbb_.{{ADD_FN}}(\\";
1879 code_ += "{{ADD_OFFSET}}, {{ADD_NAME}}, {{ADD_VALUE}});";
1881 code_ += "{{ADD_OFFSET}}, {{ADD_NAME}});";
1887 // Builder constructor
1889 " explicit {{STRUCT_NAME}}Builder(flatbuffers::FlatBufferBuilder "
1891 code_ += " : fbb_(_fbb) {";
1892 code_ += " start_ = fbb_.StartTable();";
1895 // Assignment operator;
1897 " {{STRUCT_NAME}}Builder &operator="
1898 "(const {{STRUCT_NAME}}Builder &);";
1900 // Finish() function.
1901 code_ += " flatbuffers::Offset<{{STRUCT_NAME}}> Finish() {";
1902 code_ += " const auto end = fbb_.EndTable(start_);";
1903 code_ += " auto o = flatbuffers::Offset<{{STRUCT_NAME}}>(end);";
1905 for (auto it = struct_def.fields.vec.begin();
1906 it != struct_def.fields.vec.end(); ++it) {
1907 const auto &field = **it;
1908 if (!field.deprecated && field.required) {
1909 code_.SetValue("FIELD_NAME", Name(field));
1910 code_.SetValue("OFFSET_NAME", GenFieldOffsetName(field));
1911 code_ += " fbb_.Required(o, {{STRUCT_NAME}}::{{OFFSET_NAME}});";
1914 code_ += " return o;";
1919 // Generate a convenient CreateX function that uses the above builder
1920 // to create a table in one go.
1922 "inline flatbuffers::Offset<{{STRUCT_NAME}}> "
1923 "Create{{STRUCT_NAME}}(";
1924 code_ += " flatbuffers::FlatBufferBuilder &_fbb\\";
1925 for (auto it = struct_def.fields.vec.begin();
1926 it != struct_def.fields.vec.end(); ++it) {
1927 const auto &field = **it;
1928 if (!field.deprecated) { GenParam(field, false, ",\n "); }
1932 code_ += " {{STRUCT_NAME}}Builder builder_(_fbb);";
1933 for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1;
1935 for (auto it = struct_def.fields.vec.rbegin();
1936 it != struct_def.fields.vec.rend(); ++it) {
1937 const auto &field = **it;
1938 if (!field.deprecated && (!struct_def.sortbysize ||
1939 size == SizeOf(field.value.type.base_type))) {
1940 code_.SetValue("FIELD_NAME", Name(field));
1941 code_ += " builder_.add_{{FIELD_NAME}}({{FIELD_NAME}});";
1945 code_ += " return builder_.Finish();";
1949 // Generate a CreateXDirect function with vector types as parameters
1950 if (has_string_or_vector_fields) {
1952 "inline flatbuffers::Offset<{{STRUCT_NAME}}> "
1953 "Create{{STRUCT_NAME}}Direct(";
1954 code_ += " flatbuffers::FlatBufferBuilder &_fbb\\";
1955 for (auto it = struct_def.fields.vec.begin();
1956 it != struct_def.fields.vec.end(); ++it) {
1957 const auto &field = **it;
1958 if (!field.deprecated) { GenParam(field, true, ",\n "); }
1961 // Need to call "Create" with the struct namespace.
1962 const auto qualified_create_name =
1963 struct_def.defined_namespace->GetFullyQualifiedName("Create");
1964 code_.SetValue("CREATE_NAME", TranslateNameSpace(qualified_create_name));
1967 code_ += " return {{CREATE_NAME}}{{STRUCT_NAME}}(";
1969 for (auto it = struct_def.fields.vec.begin();
1970 it != struct_def.fields.vec.end(); ++it) {
1971 const auto &field = **it;
1972 if (!field.deprecated) {
1973 code_.SetValue("FIELD_NAME", Name(field));
1975 if (field.value.type.base_type == BASE_TYPE_STRING) {
1977 ",\n {{FIELD_NAME}} ? "
1978 "_fbb.CreateString({{FIELD_NAME}}) : 0\\";
1979 } else if (field.value.type.base_type == BASE_TYPE_VECTOR) {
1980 code_ += ",\n {{FIELD_NAME}} ? \\";
1981 const auto vtype = field.value.type.VectorType();
1982 if (IsStruct(vtype)) {
1983 const auto type = WrapInNameSpace(*vtype.struct_def);
1984 code_ += "_fbb.CreateVectorOfStructs<" + type + ">\\";
1986 const auto type = GenTypeWire(vtype, "", false);
1987 code_ += "_fbb.CreateVector<" + type + ">\\";
1989 code_ += "(*{{FIELD_NAME}}) : 0\\";
1991 code_ += ",\n {{FIELD_NAME}}\\";
2001 std::string GenUnionUnpackVal(const FieldDef &afield,
2002 const char *vec_elem_access,
2003 const char *vec_type_access) {
2004 return afield.value.type.enum_def->name +
2005 "Union::UnPack(" + "_e" + vec_elem_access + ", " +
2006 EscapeKeyword(afield.name + UnionTypeFieldSuffix()) +
2007 "()" + vec_type_access + ", _resolver)";
2010 std::string GenUnpackVal(const Type &type, const std::string &val,
2011 bool invector, const FieldDef &afield) {
2012 switch (type.base_type) {
2013 case BASE_TYPE_STRING: {
2014 return val + "->str()";
2016 case BASE_TYPE_STRUCT: {
2017 const auto name = WrapInNameSpace(*type.struct_def);
2018 if (IsStruct(type)) {
2019 auto native_type = type.struct_def->attributes.Lookup("native_type");
2021 return "flatbuffers::UnPack(*" + val + ")";
2022 } else if (invector || afield.native_inline) {
2025 const auto ptype = GenTypeNativePtr(name, &afield, true);
2026 return ptype + "(new " + name + "(*" + val + "))";
2029 const auto ptype = GenTypeNativePtr(
2030 NativeName(name, type.struct_def, parser_.opts), &afield, true);
2031 return ptype + "(" + val + "->UnPack(_resolver))";
2034 case BASE_TYPE_UNION: {
2035 return GenUnionUnpackVal(
2036 afield, invector ? "->Get(_i)" : "",
2037 invector ? ("->GetEnum<" + type.enum_def->name + ">(_i)").c_str()
2047 std::string GenUnpackFieldStatement(const FieldDef &field,
2048 const FieldDef *union_field) {
2050 switch (field.value.type.base_type) {
2051 case BASE_TYPE_VECTOR: {
2052 auto cpp_type = field.attributes.Lookup("cpp_type");
2053 std::string indexing;
2054 if (field.value.type.enum_def) {
2055 indexing += "(" + WrapInNameSpace(*field.value.type.enum_def) + ")";
2057 indexing += "_e->Get(_i)";
2058 if (field.value.type.element == BASE_TYPE_BOOL) { indexing += " != 0"; }
2060 // Generate code that pushes data from _e to _o in the form:
2061 // for (uoffset_t i = 0; i < _e->size(); ++i) {
2062 // _o->field.push_back(_e->Get(_i));
2064 auto name = Name(field);
2065 if (field.value.type.element == BASE_TYPE_UTYPE) {
2066 name = StripUnionType(Name(field));
2069 field.value.type.element == BASE_TYPE_UTYPE
2071 : (field.value.type.element == BASE_TYPE_UNION ? ".value" : "");
2072 code += "{ _o->" + name + ".resize(_e->size()); ";
2073 code += "for (flatbuffers::uoffset_t _i = 0;";
2074 code += " _i < _e->size(); _i++) { ";
2076 // Generate code that resolves the cpp pointer type, of the form:
2078 // (*resolver)(&_o->field, (hash_value_t)(_e));
2080 // _o->field = nullptr;
2081 code += "//vector resolver, " + PtrType(&field) + "\n";
2082 code += "if (_resolver) ";
2083 code += "(*_resolver)";
2084 code += "(reinterpret_cast<void **>(&_o->" + name + "[_i]" + access + "), ";
2085 code += "static_cast<flatbuffers::hash_value_t>(" + indexing + "));";
2086 if (PtrType(&field) == "naked") {
2088 code += "_o->" + name + "[_i]" + access + " = nullptr";
2091 //code += "_o->" + name + "[_i]" + access + " = " + GenTypeNativePtr(cpp_type->constant, &field, true) + "();";
2092 code += "/* else do nothing */";
2095 code += "_o->" + name + "[_i]" + access + " = ";
2097 GenUnpackVal(field.value.type.VectorType(), indexing, true, field);
2102 case BASE_TYPE_UTYPE: {
2103 FLATBUFFERS_ASSERT(union_field->value.type.base_type == BASE_TYPE_UNION);
2104 // Generate code that sets the union type, of the form:
2105 // _o->field.type = _e;
2106 code += "_o->" + union_field->name + ".type = _e;";
2109 case BASE_TYPE_UNION: {
2110 // Generate code that sets the union value, of the form:
2111 // _o->field.value = Union::Unpack(_e, field_type(), resolver);
2112 code += "_o->" + Name(field) + ".value = ";
2113 code += GenUnionUnpackVal(field, "", "");
2118 auto cpp_type = field.attributes.Lookup("cpp_type");
2120 // Generate code that resolves the cpp pointer type, of the form:
2122 // (*resolver)(&_o->field, (hash_value_t)(_e));
2124 // _o->field = nullptr;
2125 code += "//scalar resolver, " + PtrType(&field) + " \n";
2126 code += "if (_resolver) ";
2127 code += "(*_resolver)";
2128 code += "(reinterpret_cast<void **>(&_o->" + Name(field) + "), ";
2129 code += "static_cast<flatbuffers::hash_value_t>(_e));";
2130 if (PtrType(&field) == "naked") {
2132 code += "_o->" + Name(field) + " = nullptr;";
2135 //code += "_o->" + Name(field) + " = " + GenTypeNativePtr(cpp_type->constant, &field, true) + "();";
2136 code += "/* else do nothing */;";
2139 // Generate code for assigning the value, of the form:
2140 // _o->field = value;
2141 code += "_o->" + Name(field) + " = ";
2142 code += GenUnpackVal(field.value.type, "_e", false, field) + ";";
2150 std::string GenCreateParam(const FieldDef &field) {
2151 std::string value = "_o->";
2152 if (field.value.type.base_type == BASE_TYPE_UTYPE) {
2153 value += StripUnionType(Name(field));
2156 value += Name(field);
2158 if (field.value.type.base_type != BASE_TYPE_VECTOR && field.attributes.Lookup("cpp_type")) {
2159 auto type = GenTypeBasic(field.value.type, false);
2163 type + ">((*_rehasher)(" + value + GenPtrGet(field) + ")) : 0";
2168 switch (field.value.type.base_type) {
2169 // String fields are of the form:
2170 // _fbb.CreateString(_o->field)
2171 case BASE_TYPE_STRING: {
2172 code += "_fbb.CreateString(" + value + ")";
2174 // For optional fields, check to see if there actually is any data
2175 // in _o->field before attempting to access it.
2176 if (!field.required) { code = value + ".empty() ? 0 : " + code; }
2179 // Vector fields come in several flavours, of the forms:
2180 // _fbb.CreateVector(_o->field);
2181 // _fbb.CreateVector((const utype*)_o->field.data(), _o->field.size());
2182 // _fbb.CreateVectorOfStrings(_o->field)
2183 // _fbb.CreateVectorOfStructs(_o->field)
2184 // _fbb.CreateVector<Offset<T>>(_o->field.size() [&](size_t i) {
2185 // return CreateT(_fbb, _o->Get(i), rehasher);
2187 case BASE_TYPE_VECTOR: {
2188 auto vector_type = field.value.type.VectorType();
2189 switch (vector_type.base_type) {
2190 case BASE_TYPE_STRING: {
2191 code += "_fbb.CreateVectorOfStrings(" + value + ")";
2194 case BASE_TYPE_STRUCT: {
2195 if (IsStruct(vector_type)) {
2197 field.value.type.struct_def->attributes.Lookup("native_type");
2199 code += "_fbb.CreateVectorOfNativeStructs<";
2200 code += WrapInNameSpace(*vector_type.struct_def) + ">";
2202 code += "_fbb.CreateVectorOfStructs";
2204 code += "(" + value + ")";
2206 code += "_fbb.CreateVector<flatbuffers::Offset<";
2207 code += WrapInNameSpace(*vector_type.struct_def) + ">> ";
2208 code += "(" + value + ".size(), ";
2209 code += "[](size_t i, _VectorArgs *__va) { ";
2210 code += "return Create" + vector_type.struct_def->name;
2211 code += "(*__va->__fbb, __va->_" + value + "[i]" +
2212 GenPtrGet(field) + ", ";
2213 code += "__va->__rehasher); }, &_va )";
2217 case BASE_TYPE_BOOL: {
2218 code += "_fbb.CreateVector(" + value + ")";
2221 case BASE_TYPE_UNION: {
2223 "_fbb.CreateVector<flatbuffers::"
2226 ".size(), [](size_t i, _VectorArgs *__va) { "
2228 value + "[i].Pack(*__va->__fbb, __va->__rehasher); }, &_va)";
2231 case BASE_TYPE_UTYPE: {
2232 value = StripUnionType(value);
2233 code += "_fbb.CreateVector<uint8_t>(" + value +
2234 ".size(), [](size_t i, _VectorArgs *__va) { "
2235 "return static_cast<uint8_t>(__va->_" +
2236 value + "[i].type); }, &_va)";
2240 if (field.value.type.enum_def) {
2241 // For enumerations, we need to get access to the array data for
2242 // the underlying storage type (eg. uint8_t).
2243 const auto basetype = GenTypeBasic(
2244 field.value.type.enum_def->underlying_type, false);
2245 code += "_fbb.CreateVector((const " + basetype + "*)" + value +
2246 ".data(), " + value + ".size())";
2247 } else if (field.attributes.Lookup("cpp_type")) {
2248 auto type = GenTypeBasic(vector_type, false);
2249 code += "_fbb.CreateVector<" + type + ">(" + value + ".size(), ";
2250 code += "[](size_t i, _VectorArgs *__va) { ";
2251 code += "return __va->__rehasher ? ";
2252 code += "static_cast<" + type + ">((*__va->__rehasher)";
2253 code += "(__va->_" + value + "[i]" + GenPtrGet(field) + ")) : 0";
2254 code += "; }, &_va )";
2256 code += "_fbb.CreateVector(" + value + ")";
2262 // For optional fields, check to see if there actually is any data
2263 // in _o->field before attempting to access it.
2264 if (!field.required) { code = value + ".size() ? " + code + " : 0"; }
2267 case BASE_TYPE_UNION: {
2268 // _o->field.Pack(_fbb);
2269 code += value + ".Pack(_fbb)";
2272 case BASE_TYPE_STRUCT: {
2273 if (IsStruct(field.value.type)) {
2275 field.value.type.struct_def->attributes.Lookup("native_type");
2277 code += "flatbuffers::Pack(" + value + ")";
2278 } else if (field.native_inline) {
2279 code += "&" + value;
2281 code += value + " ? " + value + GenPtrGet(field) + " : 0";
2284 // _o->field ? CreateT(_fbb, _o->field.get(), _rehasher);
2285 const auto type = field.value.type.struct_def->name;
2286 code += value + " ? Create" + type;
2287 code += "(_fbb, " + value + GenPtrGet(field) + ", _rehasher)";
2300 // Generate code for tables that needs to come after the regular definition.
2301 void GenTablePost(const StructDef &struct_def) {
2302 code_.SetValue("STRUCT_NAME", Name(struct_def));
2303 code_.SetValue("NATIVE_NAME",
2304 NativeName(Name(struct_def), &struct_def, parser_.opts));
2306 if (parser_.opts.generate_object_based_api) {
2307 // Generate the X::UnPack() method.
2308 code_ += "inline " +
2309 TableUnPackSignature(struct_def, false, parser_.opts) + " {";
2310 code_ += " auto _o = new {{NATIVE_NAME}}();";
2311 code_ += " UnPackTo(_o, _resolver);";
2312 code_ += " return _o;";
2316 code_ += "inline " +
2317 TableUnPackToSignature(struct_def, false, parser_.opts) + " {";
2318 code_ += " (void)_o;";
2319 code_ += " (void)_resolver;";
2321 for (auto it = struct_def.fields.vec.begin();
2322 it != struct_def.fields.vec.end(); ++it) {
2323 const auto &field = **it;
2324 if (field.deprecated) { continue; }
2326 // Assign a value from |this| to |_o|. Values from |this| are stored
2327 // in a variable |_e| by calling this->field_type(). The value is then
2328 // assigned to |_o| using the GenUnpackFieldStatement.
2329 const bool is_union = field.value.type.base_type == BASE_TYPE_UTYPE;
2330 const auto statement =
2331 GenUnpackFieldStatement(field, is_union ? *(it + 1) : nullptr);
2333 code_.SetValue("FIELD_NAME", Name(field));
2334 auto prefix = " { auto _e = {{FIELD_NAME}}(); ";
2335 auto check = IsScalar(field.value.type.base_type) ? "" : "if (_e) ";
2336 auto postfix = " };";
2337 code_ += std::string(prefix) + check + statement + postfix;
2342 // Generate the X::Pack member function that simply calls the global
2343 // CreateX function.
2344 code_ += "inline " + TablePackSignature(struct_def, false, parser_.opts) +
2346 code_ += " return Create{{STRUCT_NAME}}(_fbb, _o, _rehasher);";
2350 // Generate a CreateX method that works with an unpacked C++ object.
2351 code_ += "inline " +
2352 TableCreateSignature(struct_def, false, parser_.opts) + " {";
2353 code_ += " (void)_rehasher;";
2354 code_ += " (void)_o;";
2357 " struct _VectorArgs "
2358 "{ flatbuffers::FlatBufferBuilder *__fbb; "
2360 NativeName(Name(struct_def), &struct_def, parser_.opts) +
2362 "const flatbuffers::rehasher_function_t *__rehasher; } _va = { "
2363 "&_fbb, _o, _rehasher}; (void)_va;";
2365 for (auto it = struct_def.fields.vec.begin();
2366 it != struct_def.fields.vec.end(); ++it) {
2368 if (field.deprecated) { continue; }
2369 code_ += " auto _" + Name(field) + " = " + GenCreateParam(field) + ";";
2371 // Need to call "Create" with the struct namespace.
2372 const auto qualified_create_name =
2373 struct_def.defined_namespace->GetFullyQualifiedName("Create");
2374 code_.SetValue("CREATE_NAME", TranslateNameSpace(qualified_create_name));
2376 code_ += " return {{CREATE_NAME}}{{STRUCT_NAME}}(";
2378 for (auto it = struct_def.fields.vec.begin();
2379 it != struct_def.fields.vec.end(); ++it) {
2381 if (field.deprecated) { continue; }
2383 bool pass_by_address = false;
2384 if (field.value.type.base_type == BASE_TYPE_STRUCT) {
2385 if (IsStruct(field.value.type)) {
2387 field.value.type.struct_def->attributes.Lookup("native_type");
2388 if (native_type) { pass_by_address = true; }
2392 // Call the CreateX function using values from |_o|.
2393 if (pass_by_address) {
2394 code_ += ",\n &_" + Name(field) + "\\";
2396 code_ += ",\n _" + Name(field) + "\\";
2405 static void GenPadding(
2406 const FieldDef &field, std::string *code_ptr, int *id,
2407 const std::function<void(int bits, std::string *code_ptr, int *id)> &f) {
2408 if (field.padding) {
2409 for (int i = 0; i < 4; i++) {
2410 if (static_cast<int>(field.padding) & (1 << i)) {
2411 f((1 << i) * 8, code_ptr, id);
2414 FLATBUFFERS_ASSERT(!(field.padding & ~0xF));
2418 static void PaddingDefinition(int bits, std::string *code_ptr, int *id) {
2419 *code_ptr += " int" + NumToString(bits) + "_t padding" +
2420 NumToString((*id)++) + "__;";
2423 static void PaddingInitializer(int bits, std::string *code_ptr, int *id) {
2425 *code_ptr += ",\n padding" + NumToString((*id)++) + "__(0)";
2428 static void PaddingNoop(int bits, std::string *code_ptr, int *id) {
2430 *code_ptr += " (void)padding" + NumToString((*id)++) + "__;";
2433 // Generate an accessor struct with constructor for a flatbuffers struct.
2434 void GenStruct(const StructDef &struct_def) {
2435 // Generate an accessor struct, with private variables of the form:
2437 // Generates manual padding and alignment.
2438 // Variables are private because they contain little endian data on all
2440 GenComment(struct_def.doc_comment);
2441 code_.SetValue("ALIGN", NumToString(struct_def.minalign));
2442 code_.SetValue("STRUCT_NAME", Name(struct_def));
2445 "FLATBUFFERS_MANUALLY_ALIGNED_STRUCT({{ALIGN}}) "
2446 "{{STRUCT_NAME}} FLATBUFFERS_FINAL_CLASS {";
2447 code_ += " private:";
2450 for (auto it = struct_def.fields.vec.begin();
2451 it != struct_def.fields.vec.end(); ++it) {
2452 const auto &field = **it;
2453 code_.SetValue("FIELD_TYPE",
2454 GenTypeGet(field.value.type, " ", "", " ", false));
2455 code_.SetValue("FIELD_NAME", Name(field));
2456 code_ += " {{FIELD_TYPE}}{{FIELD_NAME}}_;";
2458 if (field.padding) {
2459 std::string padding;
2460 GenPadding(field, &padding, &padding_id, PaddingDefinition);
2465 // Generate GetFullyQualifiedName
2467 code_ += " public:";
2468 GenFullyQualifiedNameGetter(struct_def, Name(struct_def));
2470 // Generate a default constructor.
2471 code_ += " {{STRUCT_NAME}}() {";
2472 code_ += " memset(this, 0, sizeof({{STRUCT_NAME}}));";
2475 // Generate a constructor that takes all fields as arguments.
2476 std::string arg_list;
2477 std::string init_list;
2479 for (auto it = struct_def.fields.vec.begin();
2480 it != struct_def.fields.vec.end(); ++it) {
2481 const auto &field = **it;
2482 const auto member_name = Name(field) + "_";
2483 const auto arg_name = "_" + Name(field);
2484 const auto arg_type =
2485 GenTypeGet(field.value.type, " ", "const ", " &", true);
2487 if (it != struct_def.fields.vec.begin()) {
2489 init_list += ",\n ";
2491 arg_list += arg_type;
2492 arg_list += arg_name;
2493 init_list += member_name;
2494 if (IsScalar(field.value.type.base_type)) {
2495 auto type = GenUnderlyingCast(field, false, arg_name);
2496 init_list += "(flatbuffers::EndianScalar(" + type + "))";
2498 init_list += "(" + arg_name + ")";
2500 if (field.padding) {
2501 GenPadding(field, &init_list, &padding_id, PaddingInitializer);
2505 if (!arg_list.empty()) {
2506 code_.SetValue("ARG_LIST", arg_list);
2507 code_.SetValue("INIT_LIST", init_list);
2508 code_ += " {{STRUCT_NAME}}({{ARG_LIST}})";
2509 code_ += " : {{INIT_LIST}} {";
2511 for (auto it = struct_def.fields.vec.begin();
2512 it != struct_def.fields.vec.end(); ++it) {
2513 const auto &field = **it;
2514 if (field.padding) {
2515 std::string padding;
2516 GenPadding(field, &padding, &padding_id, PaddingNoop);
2523 // Generate accessor methods of the form:
2524 // type name() const { return flatbuffers::EndianScalar(name_); }
2525 for (auto it = struct_def.fields.vec.begin();
2526 it != struct_def.fields.vec.end(); ++it) {
2527 const auto &field = **it;
2529 auto field_type = GenTypeGet(field.value.type, " ", "const ", " &", true);
2530 auto is_scalar = IsScalar(field.value.type.base_type);
2531 auto member = Name(field) + "_";
2533 is_scalar ? "flatbuffers::EndianScalar(" + member + ")" : member;
2535 code_.SetValue("FIELD_NAME", Name(field));
2536 code_.SetValue("FIELD_TYPE", field_type);
2537 code_.SetValue("FIELD_VALUE", GenUnderlyingCast(field, true, value));
2539 GenComment(field.doc_comment, " ");
2540 code_ += " {{FIELD_TYPE}}{{FIELD_NAME}}() const {";
2541 code_ += " return {{FIELD_VALUE}};";
2544 if (parser_.opts.mutable_buffer) {
2545 auto mut_field_type = GenTypeGet(field.value.type, " ", "", " &", true);
2546 code_.SetValue("FIELD_TYPE", mut_field_type);
2548 code_.SetValue("ARG", GenTypeBasic(field.value.type, true));
2549 code_.SetValue("FIELD_VALUE",
2550 GenUnderlyingCast(field, false, "_" + Name(field)));
2552 code_ += " void mutate_{{FIELD_NAME}}({{ARG}} _{{FIELD_NAME}}) {";
2554 " flatbuffers::WriteScalar(&{{FIELD_NAME}}_, "
2555 "{{FIELD_VALUE}});";
2558 code_ += " {{FIELD_TYPE}}mutable_{{FIELD_NAME}}() {";
2559 code_ += " return {{FIELD_NAME}}_;";
2564 // Generate a comparison function for this field if it is a key.
2566 GenKeyFieldMethods(field);
2569 code_.SetValue("NATIVE_NAME", Name(struct_def));
2570 GenOperatorNewDelete(struct_def);
2573 code_.SetValue("STRUCT_BYTE_SIZE", NumToString(struct_def.bytesize));
2574 code_ += "FLATBUFFERS_STRUCT_END({{STRUCT_NAME}}, {{STRUCT_BYTE_SIZE}});";
2578 // Set up the correct namespace. Only open a namespace if the existing one is
2579 // different (closing/opening only what is necessary).
2581 // The file must start and end with an empty (or null) namespace so that
2582 // namespaces are properly opened and closed.
2583 void SetNameSpace(const Namespace *ns) {
2584 if (cur_name_space_ == ns) { return; }
2586 // Compute the size of the longest common namespace prefix.
2587 // If cur_name_space is A::B::C::D and ns is A::B::E::F::G,
2588 // the common prefix is A::B:: and we have old_size = 4, new_size = 5
2589 // and common_prefix_size = 2
2590 size_t old_size = cur_name_space_ ? cur_name_space_->components.size() : 0;
2591 size_t new_size = ns ? ns->components.size() : 0;
2593 size_t common_prefix_size = 0;
2594 while (common_prefix_size < old_size && common_prefix_size < new_size &&
2595 ns->components[common_prefix_size] ==
2596 cur_name_space_->components[common_prefix_size]) {
2597 common_prefix_size++;
2600 // Close cur_name_space in reverse order to reach the common prefix.
2601 // In the previous example, D then C are closed.
2602 for (size_t j = old_size; j > common_prefix_size; --j) {
2603 code_ += "} // namespace " + cur_name_space_->components[j - 1];
2605 if (old_size != common_prefix_size) { code_ += ""; }
2607 // open namespace parts to reach the ns namespace
2608 // in the previous example, E, then F, then G are opened
2609 for (auto j = common_prefix_size; j != new_size; ++j) {
2610 code_ += "namespace " + ns->components[j] + " {";
2612 if (new_size != common_prefix_size) { code_ += ""; }
2614 cur_name_space_ = ns;
2620 bool GenerateCPP(const Parser &parser, const std::string &path,
2621 const std::string &file_name) {
2622 cpp::CppGenerator generator(parser, path, file_name);
2623 return generator.generate();
2626 std::string CPPMakeRule(const Parser &parser, const std::string &path,
2627 const std::string &file_name) {
2628 const auto filebase =
2629 flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
2630 const auto included_files = parser.GetIncludedFilesRecursive(file_name);
2631 std::string make_rule = GeneratedFileName(path, filebase) + ": ";
2632 for (auto it = included_files.begin(); it != included_files.end(); ++it) {
2633 make_rule += " " + *it;
2638 } // namespace flatbuffers