return ctypename[type.base_type];
}
-static std::string GenTypeWire(const Type &type, const char *postfix);
+static std::string GenTypeWire(const Parser &parser, const Type &type,
+ const char *postfix);
// Return a C++ pointer type, specialized to the actual struct/table types,
// and vector element types.
-static std::string GenTypePointer(const Type &type) {
+static std::string GenTypePointer(const Parser &parser, const Type &type) {
switch (type.base_type) {
case BASE_TYPE_STRING:
return "flatbuffers::String";
case BASE_TYPE_VECTOR:
- return "flatbuffers::Vector<" + GenTypeWire(type.VectorType(), "") + ">";
- case BASE_TYPE_STRUCT:
- return type.struct_def->name;
+ return "flatbuffers::Vector<" +
+ GenTypeWire(parser, type.VectorType(), "") + ">";
+ case BASE_TYPE_STRUCT: {
+ auto ns = type.struct_def->defined_namespace;
+ if (parser.namespaces_.back() != ns) {
+ std::string qualified_name;
+ for (auto it = ns->components.begin();
+ it != ns->components.end(); ++it) {
+ qualified_name += *it + "::";
+ }
+ return qualified_name + type.struct_def->name;
+ } else {
+ return type.struct_def->name;
+ }
+ }
case BASE_TYPE_UNION:
// fall through
default:
// Return a C++ type for any type (scalar/pointer) specifically for
// building a flatbuffer.
-static std::string GenTypeWire(const Type &type, const char *postfix) {
+static std::string GenTypeWire(const Parser &parser, const Type &type,
+ const char *postfix) {
return IsScalar(type.base_type)
? GenTypeBasic(type) + postfix
: IsStruct(type)
- ? "const " + GenTypePointer(type) + " *"
- : "flatbuffers::Offset<" + GenTypePointer(type) + ">" + postfix;
+ ? "const " + GenTypePointer(parser, type) + " *"
+ : "flatbuffers::Offset<" + GenTypePointer(parser, type) + ">" + postfix;
}
// Return a C++ type for any type (scalar/pointer) that reflects its
// serialized size.
-static std::string GenTypeSize(const Type &type) {
+static std::string GenTypeSize(const Parser &parser, const Type &type) {
return IsScalar(type.base_type)
? GenTypeBasic(type)
: IsStruct(type)
- ? GenTypePointer(type)
+ ? GenTypePointer(parser, type)
: "flatbuffers::uoffset_t";
}
// Return a C++ type for any type (scalar/pointer) specifically for
// using a flatbuffer.
-static std::string GenTypeGet(const Type &type, const char *afterbasic,
- const char *beforeptr, const char *afterptr) {
+static std::string GenTypeGet(const Parser &parser, const Type &type,
+ const char *afterbasic, const char *beforeptr,
+ const char *afterptr) {
return IsScalar(type.base_type)
? GenTypeBasic(type) + afterbasic
- : beforeptr + GenTypePointer(type) + afterptr;
+ : beforeptr + GenTypePointer(parser, type) + afterptr;
}
// Generate a documentation comment, if available.
auto &field = **it;
if (!field.deprecated) { // Deprecated fields won't be accessible.
GenComment(field.doc_comment, code_ptr, " ");
- code += " " + GenTypeGet(field.value.type, " ", "const ", " *");
+ code += " " + GenTypeGet(parser, field.value.type, " ", "const ", " *");
code += field.name + "() const { return ";
// Call a different accessor for pointers, that indirects.
code += IsScalar(field.value.type.base_type)
? "GetField<"
: (IsStruct(field.value.type) ? "GetStruct<" : "GetPointer<");
- code += GenTypeGet(field.value.type, "", "const ", " *") + ">(";
+ code += GenTypeGet(parser, field.value.type, "", "const ", " *") + ">(";
code += NumToString(field.value.offset);
// Default value as second arg for non-pointer types.
if (IsScalar(field.value.type.base_type))
++it) {
auto &field = **it;
if (!field.deprecated) {
- code += prefix + "VerifyField<" + GenTypeSize(field.value.type);
+ code += prefix + "VerifyField<" + GenTypeSize(parser, field.value.type);
code += ">(verifier, " + NumToString(field.value.offset);
code += " /* " + field.name + " */)";
switch (field.value.type.base_type) {
auto &field = **it;
if (!field.deprecated) {
code += " void add_" + field.name + "(";
- code += GenTypeWire(field.value.type, " ") + field.name + ") { fbb_.Add";
+ code += GenTypeWire(parser, field.value.type, " ") + field.name;
+ code += ") { fbb_.Add";
if (IsScalar(field.value.type.base_type))
- code += "Element<" + GenTypeWire(field.value.type, "") + ">";
+ code += "Element<" + GenTypeWire(parser, field.value.type, "") + ">";
else if (IsStruct(field.value.type))
code += "Struct";
else
++it) {
auto &field = **it;
if (!field.deprecated) {
- code += ",\n " + GenTypeWire(field.value.type, " ") + field.name;
- code += " = " + field.value.constant;
+ code += ",\n " + GenTypeWire(parser, field.value.type, " ");
+ code += field.name + " = " + field.value.constant;
}
}
code += ") {\n " + struct_def.name + "Builder builder_(_fbb);\n";
}
// Generate an accessor struct with constructor for a flatbuffers struct.
-static void GenStruct(StructDef &struct_def, std::string *code_ptr) {
+static void GenStruct(const Parser &parser, StructDef &struct_def,
+ std::string *code_ptr) {
if (struct_def.generated) return;
std::string &code = *code_ptr;
it != struct_def.fields.vec.end();
++it) {
auto &field = **it;
- code += " " + GenTypeGet(field.value.type, " ", "", " ");
+ code += " " + GenTypeGet(parser, field.value.type, " ", "", " ");
code += field.name + "_;\n";
if (field.padding) {
for (int i = 0; i < 4; i++)
++it) {
auto &field = **it;
if (it != struct_def.fields.vec.begin()) code += ", ";
- code += GenTypeGet(field.value.type, " ", "const ", " &") + field.name;
+ code += GenTypeGet(parser, field.value.type, " ", "const ", " &");
+ code += field.name;
}
code += ")\n : ";
padding_id = 0;
++it) {
auto &field = **it;
GenComment(field.doc_comment, code_ptr, " ");
- code += " " + GenTypeGet(field.value.type, " ", "const ", " &");
+ code += " " + GenTypeGet(parser, field.value.type, " ", "const ", " &");
code += field.name + "() const { return ";
if (IsScalar(field.value.type.base_type))
code += "flatbuffers::EndianScalar(" + field.name + "_)";
code += NumToString(struct_def.bytesize) + ");\n\n";
}
+void GenerateNestedNameSpaces(Namespace *ns, std::string *code_ptr) {
+ for (auto it = ns->components.begin(); it != ns->components.end(); ++it) {
+ *code_ptr += "namespace " + *it + " {\n";
+ }
+}
+
+void CloseNestedNameSpaces(Namespace *ns, std::string *code_ptr) {
+ for (auto it = ns->components.rbegin(); it != ns->components.rend(); ++it) {
+ *code_ptr += "} // namespace " + *it + "\n";
+ }
+}
+
} // namespace cpp
// Iterate through all definitions we haven't generate code for (enums, structs,
// Generate forward declarations for all structs/tables, since they may
// have circular references.
- std::string forward_decl_code;
+ std::string forward_decl_code_same_namespace;
+ std::string forward_decl_code_other_namespace;
+ Namespace *cur_name_space = nullptr;
for (auto it = parser.structs_.vec.begin();
it != parser.structs_.vec.end(); ++it) {
- if (!(*it)->generated)
- forward_decl_code += "struct " + (*it)->name + ";\n";
+ auto &struct_def = **it;
+ auto decl = "struct " + struct_def.name + ";\n";
+ if (struct_def.defined_namespace == parser.namespaces_.back()) {
+ forward_decl_code_same_namespace += decl;
+ } else {
+ // Wrap this decl in the correct namespace. Only open a namespace if
+ // the adjacent one is different.
+ // TODO: this could be done more intelligently, by sorting to
+ // namespace path and only opening/closing what is necessary, but that's
+ // quite a bit more complexity.
+ if (cur_name_space != struct_def.defined_namespace) {
+ if (cur_name_space) {
+ CloseNestedNameSpaces(cur_name_space,
+ &forward_decl_code_other_namespace);
+ }
+ GenerateNestedNameSpaces(struct_def.defined_namespace,
+ &forward_decl_code_other_namespace);
+ cur_name_space = struct_def.defined_namespace;
+ }
+ forward_decl_code_other_namespace += decl;
+ }
+ }
+ if (cur_name_space) {
+ CloseNestedNameSpaces(cur_name_space,
+ &forward_decl_code_other_namespace);
}
// Generate code for all structs, then all tables.
std::string decl_code;
for (auto it = parser.structs_.vec.begin();
it != parser.structs_.vec.end(); ++it) {
- if ((**it).fixed) GenStruct(**it, &decl_code);
+ if ((**it).fixed) GenStruct(parser, **it, &decl_code);
}
for (auto it = parser.structs_.vec.begin();
it != parser.structs_.vec.end(); ++it) {
}
// Only output file-level code if there were any declarations.
- if (enum_code.length() || forward_decl_code.length() || decl_code.length()) {
+ if (enum_code.length() || decl_code.length()) {
std::string code;
code = "// automatically generated by the FlatBuffers compiler,"
" do not modify\n\n";
// Generate include guard.
std::string include_guard = "FLATBUFFERS_GENERATED_" + include_guard_ident;
include_guard += "_";
- for (auto it = parser.name_space_.begin();
- it != parser.name_space_.end(); ++it) {
+ auto name_space = parser.namespaces_.back();
+ for (auto it = name_space->components.begin();
+ it != name_space->components.end(); ++it) {
include_guard += *it + "_";
}
include_guard += "H_";
code += "#include \"flatbuffers/flatbuffers.h\"\n\n";
- // Generate nested namespaces.
- for (auto it = parser.name_space_.begin();
- it != parser.name_space_.end(); ++it) {
- code += "namespace " + *it + " {\n";
- }
+ code += forward_decl_code_other_namespace;
+ code += "\n";
- // Output the main declaration code from above.
+ GenerateNestedNameSpaces(name_space, &code);
code += "\n";
- code += enum_code;
- code += forward_decl_code;
+
+ code += forward_decl_code_same_namespace;
code += "\n";
+
+ // Output the main declaration code from above.
+ code += enum_code;
code += decl_code;
code += enum_code_post;
}
}
- // Close the namespaces.
- for (auto it = parser.name_space_.rbegin();
- it != parser.name_space_.rend(); ++it) {
- code += "} // namespace " + *it + "\n";
- }
+ CloseNestedNameSpaces(name_space, &code);
// Close the include guard.
code += "\n#endif // " + include_guard + "\n";