diff options
author | Derek Bailey <derekbailey@google.com> | 2023-01-07 12:17:07 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-07 12:17:07 -0800 |
commit | 75af533e95c085830d786268b59dc5bd8627d2eb (patch) | |
tree | 14a173d0a196ae4fd4beb0e8a505b3740acd1058 /src/idl_gen_cpp.cpp | |
parent | c95cf661afb0cf4bf0c887cf6dceb31c22de1312 (diff) | |
download | flatbuffers-75af533e95c085830d786268b59dc5bd8627d2eb.tar.gz flatbuffers-75af533e95c085830d786268b59dc5bd8627d2eb.tar.bz2 flatbuffers-75af533e95c085830d786268b59dc5bd8627d2eb.zip |
emit global scoped ::flatbuffers in c++ (#7764)
Diffstat (limited to 'src/idl_gen_cpp.cpp')
-rw-r--r-- | src/idl_gen_cpp.cpp | 225 |
1 files changed, 113 insertions, 112 deletions
diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 80f2d42b..262eb1ab 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -90,7 +90,7 @@ enum CppStandard { CPP_STD_X0 = 0, CPP_STD_11, CPP_STD_17 }; // Define a style of 'struct' constructor if it has 'Array' fields. enum GenArrayArgMode { kArrayArgModeNone, // don't generate initialization args - kArrayArgModeSpanStatic, // generate flatbuffers::span<T,N> + kArrayArgModeSpanStatic, // generate ::flatbuffers::span<T,N> }; // Extension of IDLOptions for cpp-generator. @@ -252,14 +252,13 @@ class CppGenerator : public BaseGenerator { // Get the name of the included file as defined by the schema, and strip // the .fbs extension. const std::string name_without_ext = - flatbuffers::StripExtension(included_file.schema_name); + StripExtension(included_file.schema_name); // If we are told to keep the prefix of the included schema, leave it // unchanged, otherwise strip the leading path off so just the "basename" // of the include is retained. const std::string basename = - opts_.keep_prefix ? name_without_ext - : flatbuffers::StripPath(name_without_ext); + opts_.keep_prefix ? name_without_ext : StripPath(name_without_ext); code_ += "#include \"" + GeneratedFileName(opts_.include_prefix, basename, opts_) + "\""; @@ -522,7 +521,7 @@ class CppGenerator : public BaseGenerator { code_ += "const {{CPP_NAME}} *{{NULLABLE_EXT}}Get{{STRUCT_NAME}}(const void " "*buf) {"; - code_ += " return flatbuffers::GetRoot<{{CPP_NAME}}>(buf);"; + code_ += " return ::flatbuffers::GetRoot<{{CPP_NAME}}>(buf);"; code_ += "}"; code_ += ""; @@ -531,14 +530,16 @@ class CppGenerator : public BaseGenerator { "const {{CPP_NAME}} " "*{{NULLABLE_EXT}}GetSizePrefixed{{STRUCT_NAME}}(const void " "*buf) {"; - code_ += " return flatbuffers::GetSizePrefixedRoot<{{CPP_NAME}}>(buf);"; + code_ += + " return ::flatbuffers::GetSizePrefixedRoot<{{CPP_NAME}}>(buf);"; code_ += "}"; code_ += ""; if (opts_.mutable_buffer) { code_ += "inline \\"; code_ += "{{STRUCT_NAME}} *GetMutable{{STRUCT_NAME}}(void *buf) {"; - code_ += " return flatbuffers::GetMutableRoot<{{STRUCT_NAME}}>(buf);"; + code_ += + " return ::flatbuffers::GetMutableRoot<{{STRUCT_NAME}}>(buf);"; code_ += "}"; code_ += ""; @@ -549,7 +550,7 @@ class CppGenerator : public BaseGenerator { "*buf) {"; code_ += " return " - "flatbuffers::GetMutableSizePrefixedRoot<{{CPP_NAME}}>(buf);"; + "::flatbuffers::GetMutableSizePrefixedRoot<{{CPP_NAME}}>(buf);"; code_ += "}"; code_ += ""; } @@ -564,7 +565,7 @@ class CppGenerator : public BaseGenerator { // Check if a buffer has the identifier. code_ += "inline \\"; code_ += "bool {{STRUCT_NAME}}BufferHasIdentifier(const void *buf) {"; - code_ += " return flatbuffers::BufferHasIdentifier("; + code_ += " return ::flatbuffers::BufferHasIdentifier("; code_ += " buf, {{STRUCT_NAME}}Identifier());"; code_ += "}"; code_ += ""; @@ -574,7 +575,7 @@ class CppGenerator : public BaseGenerator { code_ += "bool SizePrefixed{{STRUCT_NAME}}BufferHasIdentifier(const void " "*buf) {"; - code_ += " return flatbuffers::BufferHasIdentifier("; + code_ += " return ::flatbuffers::BufferHasIdentifier("; code_ += " buf, {{STRUCT_NAME}}Identifier(), true);"; code_ += "}"; code_ += ""; @@ -588,13 +589,13 @@ class CppGenerator : public BaseGenerator { } code_ += "inline bool Verify{{STRUCT_NAME}}Buffer("; - code_ += " flatbuffers::Verifier &verifier) {"; + code_ += " ::flatbuffers::Verifier &verifier) {"; code_ += " return verifier.VerifyBuffer<{{CPP_NAME}}>({{ID}});"; code_ += "}"; code_ += ""; code_ += "inline bool VerifySizePrefixed{{STRUCT_NAME}}Buffer("; - code_ += " flatbuffers::Verifier &verifier) {"; + code_ += " ::flatbuffers::Verifier &verifier) {"; code_ += " return verifier.VerifySizePrefixedBuffer<{{CPP_NAME}}>({{ID}});"; code_ += "}"; @@ -610,8 +611,8 @@ class CppGenerator : public BaseGenerator { // Finish a buffer with a given root object: code_ += "inline void Finish{{STRUCT_NAME}}Buffer("; - code_ += " flatbuffers::FlatBufferBuilder &fbb,"; - code_ += " flatbuffers::Offset<{{CPP_NAME}}> root) {"; + code_ += " ::flatbuffers::FlatBufferBuilder &fbb,"; + code_ += " ::flatbuffers::Offset<{{CPP_NAME}}> root) {"; if (parser_.file_identifier_.length()) code_ += " fbb.Finish(root, {{STRUCT_NAME}}Identifier());"; else @@ -620,8 +621,8 @@ class CppGenerator : public BaseGenerator { code_ += ""; code_ += "inline void FinishSizePrefixed{{STRUCT_NAME}}Buffer("; - code_ += " flatbuffers::FlatBufferBuilder &fbb,"; - code_ += " flatbuffers::Offset<{{CPP_NAME}}> root) {"; + code_ += " ::flatbuffers::FlatBufferBuilder &fbb,"; + code_ += " ::flatbuffers::Offset<{{CPP_NAME}}> root) {"; if (parser_.file_identifier_.length()) code_ += " fbb.FinishSizePrefixed(root, {{STRUCT_NAME}}Identifier());"; else @@ -639,7 +640,8 @@ class CppGenerator : public BaseGenerator { code_ += "inline {{UNPACK_RETURN}} UnPack{{STRUCT_NAME}}("; code_ += " const void *buf,"; - code_ += " const flatbuffers::resolver_function_t *res = nullptr) {"; + code_ += + " const ::flatbuffers::resolver_function_t *res = nullptr) {"; code_ += " return {{UNPACK_TYPE}}\\"; code_ += "(Get{{STRUCT_NAME}}(buf)->UnPack(res));"; code_ += "}"; @@ -647,7 +649,8 @@ class CppGenerator : public BaseGenerator { code_ += "inline {{UNPACK_RETURN}} UnPackSizePrefixed{{STRUCT_NAME}}("; code_ += " const void *buf,"; - code_ += " const flatbuffers::resolver_function_t *res = nullptr) {"; + code_ += + " const ::flatbuffers::resolver_function_t *res = nullptr) {"; code_ += " return {{UNPACK_TYPE}}\\"; code_ += "(GetSizePrefixed{{STRUCT_NAME}}(buf)->UnPack(res));"; code_ += "}"; @@ -735,12 +738,12 @@ class CppGenerator : public BaseGenerator { std::string GenTypePointer(const Type &type) const { switch (type.base_type) { case BASE_TYPE_STRING: { - return "flatbuffers::String"; + return "::flatbuffers::String"; } case BASE_TYPE_VECTOR: { const auto type_name = GenTypeWire( type.VectorType(), "", VectorElementUserFacing(type.VectorType())); - return "flatbuffers::Vector<" + type_name + ">"; + return "::flatbuffers::Vector<" + type_name + ">"; } case BASE_TYPE_STRUCT: { return WrapInNameSpace(*type.struct_def); @@ -762,7 +765,7 @@ class CppGenerator : public BaseGenerator { } else if (IsStruct(type)) { return "const " + GenTypePointer(type) + " *"; } else { - return "flatbuffers::Offset<" + GenTypePointer(type) + ">" + postfix; + return "::flatbuffers::Offset<" + GenTypePointer(type) + ">" + postfix; } } @@ -774,7 +777,7 @@ class CppGenerator : public BaseGenerator { } else if (IsStruct(type)) { return GenTypePointer(type); } else { - return "flatbuffers::uoffset_t"; + return "::flatbuffers::uoffset_t"; } } @@ -807,7 +810,8 @@ class CppGenerator : public BaseGenerator { } bool FlexibleStringConstructor(const FieldDef *field) { - auto attr = field != nullptr && (field->attributes.Lookup("cpp_str_flex_ctor") != nullptr); + auto attr = field != nullptr && + (field->attributes.Lookup("cpp_str_flex_ctor") != nullptr); auto ret = attr ? attr : opts_.cpp_object_api_string_flexible_constructor; return ret && NativeString(field) != "std::string"; // Only for custom string types. @@ -835,10 +839,10 @@ class CppGenerator : public BaseGenerator { return ptr_type == "naked" ? "" : ".get()"; } - std::string GenOptionalNull() { return "flatbuffers::nullopt"; } + std::string GenOptionalNull() { return "::flatbuffers::nullopt"; } std::string GenOptionalDecl(const Type &type) { - return "flatbuffers::Optional<" + GenTypeBasic(type, true) + ">"; + return "::flatbuffers::Optional<" + GenTypeBasic(type, true) + ">"; } std::string GenTypeNative(const Type &type, bool invector, @@ -912,10 +916,10 @@ class CppGenerator : public BaseGenerator { } std::string GenTypeSpan(const Type &type, bool immutable, size_t extent) { - // Generate "flatbuffers::span<const U, extent>". + // Generate "::flatbuffers::span<const U, extent>". FLATBUFFERS_ASSERT(IsSeries(type) && "unexpected type"); auto element_type = type.VectorType(); - std::string text = "flatbuffers::span<"; + std::string text = "::flatbuffers::span<"; text += immutable ? "const " : ""; if (IsScalar(element_type.base_type)) { text += GenTypeBasic(element_type, IsEnum(element_type)); @@ -935,7 +939,7 @@ class CppGenerator : public BaseGenerator { break; } } - if (extent != flatbuffers::dynamic_extent) { + if (extent != dynamic_extent) { text += ", "; text += NumToString(extent); } @@ -972,7 +976,7 @@ class CppGenerator : public BaseGenerator { } return WrapInNameSpace(ev.union_type.struct_def->defined_namespace, name); } else if (IsString(ev.union_type)) { - return native_type ? "std::string" : "flatbuffers::String"; + return native_type ? "std::string" : "::flatbuffers::String"; } else { FLATBUFFERS_ASSERT(false); return Name(ev); @@ -981,7 +985,7 @@ class CppGenerator : public BaseGenerator { std::string UnionVerifySignature(const EnumDef &enum_def) { return "bool Verify" + Name(enum_def) + - "(flatbuffers::Verifier &verifier, const void *obj, " + + "(::flatbuffers::Verifier &verifier, const void *obj, " + Name(enum_def) + " type)"; } @@ -989,42 +993,44 @@ class CppGenerator : public BaseGenerator { auto name = Name(enum_def); auto type = opts_.scoped_enums ? name : "uint8_t"; return "bool Verify" + name + "Vector" + - "(flatbuffers::Verifier &verifier, " + - "const flatbuffers::Vector<flatbuffers::Offset<void>> *values, " + - "const flatbuffers::Vector<" + type + "> *types)"; + "(::flatbuffers::Verifier &verifier, " + + "const ::flatbuffers::Vector<::flatbuffers::Offset<void>> " + "*values, " + + "const ::flatbuffers::Vector<" + type + "> *types)"; } std::string UnionUnPackSignature(const EnumDef &enum_def, bool inclass) { return (inclass ? "static " : "") + std::string("void *") + (inclass ? "" : Name(enum_def) + "Union::") + "UnPack(const void *obj, " + Name(enum_def) + - " type, const flatbuffers::resolver_function_t *resolver)"; + " type, const ::flatbuffers::resolver_function_t *resolver)"; } std::string UnionPackSignature(const EnumDef &enum_def, bool inclass) { - return "flatbuffers::Offset<void> " + + return "::flatbuffers::Offset<void> " + (inclass ? "" : Name(enum_def) + "Union::") + - "Pack(flatbuffers::FlatBufferBuilder &_fbb, " + - "const flatbuffers::rehasher_function_t *_rehasher" + + "Pack(::flatbuffers::FlatBufferBuilder &_fbb, " + + "const ::flatbuffers::rehasher_function_t *_rehasher" + (inclass ? " = nullptr" : "") + ") const"; } std::string TableCreateSignature(const StructDef &struct_def, bool predecl, const IDLOptions &opts) { - return "flatbuffers::Offset<" + Name(struct_def) + "> Create" + - Name(struct_def) + "(flatbuffers::FlatBufferBuilder &_fbb, const " + + return "::flatbuffers::Offset<" + Name(struct_def) + "> Create" + + Name(struct_def) + + "(::flatbuffers::FlatBufferBuilder &_fbb, const " + NativeName(Name(struct_def), &struct_def, opts) + - " *_o, const flatbuffers::rehasher_function_t *_rehasher" + + " *_o, const ::flatbuffers::rehasher_function_t *_rehasher" + (predecl ? " = nullptr" : "") + ")"; } std::string TablePackSignature(const StructDef &struct_def, bool inclass, const IDLOptions &opts) { - return std::string(inclass ? "static " : "") + "flatbuffers::Offset<" + + return std::string(inclass ? "static " : "") + "::flatbuffers::Offset<" + Name(struct_def) + "> " + (inclass ? "" : Name(struct_def) + "::") + - "Pack(flatbuffers::FlatBufferBuilder &_fbb, " + "const " + + "Pack(::flatbuffers::FlatBufferBuilder &_fbb, " + "const " + NativeName(Name(struct_def), &struct_def, opts) + "* _o, " + - "const flatbuffers::rehasher_function_t *_rehasher" + + "const ::flatbuffers::rehasher_function_t *_rehasher" + (inclass ? " = nullptr" : "") + ")"; } @@ -1032,7 +1038,7 @@ class CppGenerator : public BaseGenerator { const IDLOptions &opts) { return NativeName(Name(struct_def), &struct_def, opts) + " *" + (inclass ? "" : Name(struct_def) + "::") + - "UnPack(const flatbuffers::resolver_function_t *_resolver" + + "UnPack(const ::flatbuffers::resolver_function_t *_resolver" + (inclass ? " = nullptr" : "") + ") const"; } @@ -1040,13 +1046,13 @@ class CppGenerator : public BaseGenerator { const IDLOptions &opts) { return "void " + (inclass ? "" : Name(struct_def) + "::") + "UnPackTo(" + NativeName(Name(struct_def), &struct_def, opts) + " *" + - "_o, const flatbuffers::resolver_function_t *_resolver" + + "_o, const ::flatbuffers::resolver_function_t *_resolver" + (inclass ? " = nullptr" : "") + ") const"; } void GenMiniReflectPre(const StructDef *struct_def) { code_.SetValue("NAME", struct_def->name); - code_ += "inline const flatbuffers::TypeTable *{{NAME}}TypeTable();"; + code_ += "inline const ::flatbuffers::TypeTable *{{NAME}}TypeTable();"; code_ += ""; } @@ -1104,9 +1110,9 @@ class CppGenerator : public BaseGenerator { } } if (is_array) { array_sizes.push_back(type.fixed_length); } - ts += "{ flatbuffers::" + std::string(ElementaryTypeNames()[et]) + ", " + - NumToString(is_vector || is_array) + ", " + NumToString(ref_idx) + - " }"; + ts += "{ ::flatbuffers::" + std::string(ElementaryTypeNames()[et]) + + ", " + NumToString(is_vector || is_array) + ", " + + NumToString(ref_idx) + " }"; } std::string rs; for (auto &type_ref : type_refs) { @@ -1147,14 +1153,14 @@ class CppGenerator : public BaseGenerator { code_.SetValue("ARRAYSIZES", as); code_.SetValue("NAMES", ns); code_.SetValue("VALUES", vs); - code_ += "inline const flatbuffers::TypeTable *{{NAME}}TypeTable() {"; + code_ += "inline const ::flatbuffers::TypeTable *{{NAME}}TypeTable() {"; if (num_fields) { - code_ += " static const flatbuffers::TypeCode type_codes[] = {"; + code_ += " static const ::flatbuffers::TypeCode type_codes[] = {"; code_ += " {{TYPES}}"; code_ += " };"; } if (!type_refs.empty()) { - code_ += " static const flatbuffers::TypeFunction type_refs[] = {"; + code_ += " static const ::flatbuffers::TypeFunction type_refs[] = {"; code_ += " {{REFS}}"; code_ += " };"; } @@ -1172,8 +1178,8 @@ class CppGenerator : public BaseGenerator { code_ += " {{NAMES}}"; code_ += " };"; } - code_ += " static const flatbuffers::TypeTable tt = {"; - code_ += std::string(" flatbuffers::{{SEQ_TYPE}}, {{NUM_FIELDS}}, ") + + code_ += " static const ::flatbuffers::TypeTable tt = {"; + code_ += std::string(" ::flatbuffers::{{SEQ_TYPE}}, {{NUM_FIELDS}}, ") + (num_fields ? "type_codes, " : "nullptr, ") + (!type_refs.empty() ? "type_refs, " : "nullptr, ") + (!as.empty() ? "array_sizes, " : "nullptr, ") + @@ -1383,7 +1389,7 @@ class CppGenerator : public BaseGenerator { code_ += " if (lhs.type != rhs.type) return false;"; code_ += " switch (lhs.type) {"; - for (const auto &ev: enum_def.Vals()) { + for (const auto &ev : enum_def.Vals()) { code_.SetValue("NATIVE_ID", GetEnumValUse(enum_def, *ev)); if (ev->IsNonZero()) { const auto native_type = GetUnionElement(*ev, true, opts_); @@ -1467,7 +1473,7 @@ class CppGenerator : public BaseGenerator { code_ += "inline const char *EnumName{{ENUM_NAME}}({{ENUM_NAME}} e) {"; - code_ += " if (flatbuffers::IsOutRange(e, " + + code_ += " if (::flatbuffers::IsOutRange(e, " + GetEnumValUse(enum_def, *enum_def.MinValue()) + ", " + GetEnumValUse(enum_def, *enum_def.MaxValue()) + ")) return \"\";"; @@ -1547,7 +1553,8 @@ class CppGenerator : public BaseGenerator { code_ += "inline " + UnionVectorVerifySignature(enum_def) + " {"; code_ += " if (!values || !types) return !values && !types;"; code_ += " if (values->size() != types->size()) return false;"; - code_ += " for (flatbuffers::uoffset_t i = 0; i < values->size(); ++i) {"; + code_ += + " for (::flatbuffers::uoffset_t i = 0; i < values->size(); ++i) {"; code_ += " if (!Verify" + Name(enum_def) + "("; code_ += " verifier, values->Get(i), types->GetEnum<" + Name(enum_def) + ">(i))) {"; @@ -1628,7 +1635,7 @@ class CppGenerator : public BaseGenerator { "inline {{ENUM_NAME}}Union::{{ENUM_NAME}}Union(const " "{{ENUM_NAME}}Union &u) : type(u.type), value(nullptr) {"; code_ += " switch (type) {"; - for (const auto &ev: enum_def.Vals()) { + for (const auto &ev : enum_def.Vals()) { if (ev->IsZero()) { continue; } code_.SetValue("LABEL", GetEnumValUse(enum_def, *ev)); code_.SetValue("TYPE", GetUnionElement(*ev, true, opts_)); @@ -1944,7 +1951,7 @@ class CppGenerator : public BaseGenerator { std::string initializer_list; std::string vector_copies; std::string swaps; - for (const auto &field: struct_def.fields.vec) { + for (const auto &field : struct_def.fields.vec) { const auto &type = field->value.type; if (field->deprecated || type.base_type == BASE_TYPE_UTYPE) continue; if (type.base_type == BASE_TYPE_STRUCT) { @@ -2038,7 +2045,7 @@ class CppGenerator : public BaseGenerator { } void GenCompareOperator(const StructDef &struct_def, - const std::string& accessSuffix = "") { + const std::string &accessSuffix = "") { std::string compare_op; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { @@ -2137,12 +2144,10 @@ class CppGenerator : public BaseGenerator { code_.SetValue("NATIVE_NAME", native_name); // Generate a C++ object that can hold an unpacked version of this table. - code_ += "struct {{NATIVE_NAME}} : public flatbuffers::NativeTable {"; + code_ += "struct {{NATIVE_NAME}} : public ::flatbuffers::NativeTable {"; code_ += " typedef {{STRUCT_NAME}} TableType;"; GenFullyQualifiedNameGetter(struct_def, native_name); - for (const auto field : struct_def.fields.vec) { - GenMember(*field); - } + for (const auto field : struct_def.fields.vec) { GenMember(*field); } GenOperatorNewDelete(struct_def); GenDefaultConstructor(struct_def); GenCopyMoveCtorAndAssigOpDecls(struct_def); @@ -2248,7 +2253,7 @@ class CppGenerator : public BaseGenerator { code_ += " bool KeyCompareLessThan(const {{STRUCT_NAME}} * const o) const {"; if (is_string) { - // use operator< of flatbuffers::String + // use operator< of ::flatbuffers::String code_ += " return *{{FIELD_NAME}}() < *o->{{FIELD_NAME}}();"; } else if (is_array) { const auto &elem_type = field.value.type.VectorType(); @@ -2266,7 +2271,7 @@ class CppGenerator : public BaseGenerator { } else if (is_array) { const auto &elem_type = field.value.type.VectorType(); if (IsScalar(elem_type.base_type)) { - std::string input_type = "flatbuffers::Array<" + + std::string input_type = "::flatbuffers::Array<" + GenTypeBasic(elem_type, false) + ", " + NumToString(elem_type.fixed_length) + ">"; code_.SetValue("INPUT_TYPE", input_type); @@ -2276,7 +2281,7 @@ class CppGenerator : public BaseGenerator { code_ += " const {{INPUT_TYPE}} *curr_{{FIELD_NAME}} = {{FIELD_NAME}}();"; code_ += - " for (flatbuffers::uoffset_t i = 0; i < " + " for (::flatbuffers::uoffset_t i = 0; i < " "curr_{{FIELD_NAME}}->size(); i++) {"; code_ += " const auto lhs = curr_{{FIELD_NAME}}->Get(i);"; code_ += " const auto rhs = _{{FIELD_NAME}}->Get(i);"; @@ -2581,7 +2586,7 @@ class CppGenerator : public BaseGenerator { code_.SetValue("STRUCT_NAME", Name(struct_def)); code_ += "struct {{STRUCT_NAME}} FLATBUFFERS_FINAL_CLASS" - " : private flatbuffers::Table {"; + " : private ::flatbuffers::Table {"; if (opts_.generate_object_based_api) { code_ += " typedef {{NATIVE_NAME}} NativeTableType;"; } @@ -2589,7 +2594,7 @@ class CppGenerator : public BaseGenerator { if (opts_.g_cpp_std >= cpp::CPP_STD_17) { code_ += " struct Traits;"; } if (opts_.mini_reflect != IDLOptions::kNone) { code_ += - " static const flatbuffers::TypeTable *MiniReflectTypeTable() {"; + " static const ::flatbuffers::TypeTable *MiniReflectTypeTable() {"; code_ += " return {{STRUCT_NAME}}TypeTable();"; code_ += " }"; } @@ -2635,7 +2640,7 @@ class CppGenerator : public BaseGenerator { code_ += " const {{CPP_NAME}} *{{FIELD_NAME}}_nested_root() const {"; code_ += " return " - "flatbuffers::GetRoot<{{CPP_NAME}}>({{FIELD_NAME}}()->Data());"; + "::flatbuffers::GetRoot<{{CPP_NAME}}>({{FIELD_NAME}}()->Data());"; code_ += " }"; } @@ -2659,7 +2664,7 @@ class CppGenerator : public BaseGenerator { // Generate a verifier function that can check a buffer from an untrusted // source will never cause reads outside the buffer. - code_ += " bool Verify(flatbuffers::Verifier &verifier) const {"; + code_ += " bool Verify(::flatbuffers::Verifier &verifier) const {"; code_ += " return VerifyTableStart(verifier)\\"; for (const auto &field : struct_def.fields.vec) { if (field->deprecated) { continue; } @@ -2749,8 +2754,8 @@ class CppGenerator : public BaseGenerator { // Generate a builder struct: code_ += "struct {{STRUCT_NAME}}Builder {"; code_ += " typedef {{STRUCT_NAME}} Table;"; - code_ += " flatbuffers::FlatBufferBuilder &fbb_;"; - code_ += " flatbuffers::uoffset_t start_;"; + code_ += " ::flatbuffers::FlatBufferBuilder &fbb_;"; + code_ += " ::flatbuffers::uoffset_t start_;"; bool has_string_or_vector_fields = false; for (auto it = struct_def.fields.vec.begin(); @@ -2797,18 +2802,18 @@ class CppGenerator : public BaseGenerator { // Builder constructor code_ += - " explicit {{STRUCT_NAME}}Builder(flatbuffers::FlatBufferBuilder " + " explicit {{STRUCT_NAME}}Builder(::flatbuffers::FlatBufferBuilder " "&_fbb)"; code_ += " : fbb_(_fbb) {"; code_ += " start_ = fbb_.StartTable();"; code_ += " }"; // Finish() function. - code_ += " flatbuffers::Offset<{{STRUCT_NAME}}> Finish() {"; + code_ += " ::flatbuffers::Offset<{{STRUCT_NAME}}> Finish() {"; code_ += " const auto end = fbb_.EndTable(start_);"; - code_ += " auto o = flatbuffers::Offset<{{STRUCT_NAME}}>(end);"; + code_ += " auto o = ::flatbuffers::Offset<{{STRUCT_NAME}}>(end);"; - for (const auto &field: struct_def.fields.vec) { + for (const auto &field : struct_def.fields.vec) { if (!field->deprecated && field->IsRequired()) { code_.SetValue("FIELD_NAME", Name(*field)); code_.SetValue("OFFSET_NAME", GenFieldOffsetName(*field)); @@ -2823,13 +2828,11 @@ class CppGenerator : public BaseGenerator { // Generate a convenient CreateX function that uses the above builder // to create a table in one go. code_ += - "inline flatbuffers::Offset<{{STRUCT_NAME}}> " + "inline ::flatbuffers::Offset<{{STRUCT_NAME}}> " "Create{{STRUCT_NAME}}("; - code_ += " flatbuffers::FlatBufferBuilder &_fbb\\"; + code_ += " ::flatbuffers::FlatBufferBuilder &_fbb\\"; for (const auto &field : struct_def.fields.vec) { - if (!field->deprecated) { - GenParam(*field, false, ",\n "); - } + if (!field->deprecated) { GenParam(*field, false, ",\n "); } } code_ += ") {"; @@ -2863,9 +2866,9 @@ class CppGenerator : public BaseGenerator { // Generate a CreateXDirect function with vector types as parameters if (opts_.cpp_direct_copy && has_string_or_vector_fields) { code_ += - "inline flatbuffers::Offset<{{STRUCT_NAME}}> " + "inline ::flatbuffers::Offset<{{STRUCT_NAME}}> " "Create{{STRUCT_NAME}}Direct("; - code_ += " flatbuffers::FlatBufferBuilder &_fbb\\"; + code_ += " ::flatbuffers::FlatBufferBuilder &_fbb\\"; for (const auto &field : struct_def.fields.vec) { if (!field->deprecated) { GenParam(*field, true, ",\n "); } } @@ -2955,7 +2958,7 @@ class CppGenerator : public BaseGenerator { const auto &struct_attrs = type.struct_def->attributes; const auto native_type = struct_attrs.Lookup("native_type"); if (native_type) { - std::string unpack_call = "flatbuffers::UnPack"; + std::string unpack_call = "::flatbuffers::UnPack"; const auto pack_name = struct_attrs.Lookup("native_type_pack_name"); if (pack_name) { unpack_call += pack_name->constant; } unpack_call += "(*" + val + ")"; @@ -3029,7 +3032,7 @@ class CppGenerator : public BaseGenerator { : (field.value.type.element == BASE_TYPE_UNION ? ".value" : ""); - code += "for (flatbuffers::uoffset_t _i = 0;"; + code += "for (::flatbuffers::uoffset_t _i = 0;"; code += " _i < _e->size(); _i++) { "; auto cpp_type = field.attributes.Lookup("cpp_type"); if (cpp_type) { @@ -3044,7 +3047,7 @@ class CppGenerator : public BaseGenerator { code += "(reinterpret_cast<void **>(&_o->" + name + "[_i]" + access + "), "; code += - "static_cast<flatbuffers::hash_value_t>(" + indexing + "));"; + "static_cast<::flatbuffers::hash_value_t>(" + indexing + "));"; if (PtrType(&field) == "naked") { code += " else "; code += "_o->" + name + "[_i]" + access + " = nullptr"; @@ -3099,7 +3102,7 @@ class CppGenerator : public BaseGenerator { code += "if (_resolver) "; code += "(*_resolver)"; code += "(reinterpret_cast<void **>(&_o->" + Name(field) + "), "; - code += "static_cast<flatbuffers::hash_value_t>(_e));"; + code += "static_cast<::flatbuffers::hash_value_t>(_e));"; if (PtrType(&field) == "naked") { code += " else "; code += "_o->" + Name(field) + " = nullptr;"; @@ -3196,7 +3199,8 @@ class CppGenerator : public BaseGenerator { // Use by-function serialization to emulate // CreateVectorOfStrings(); this works also with non-std strings. code += - "_fbb.CreateVector<flatbuffers::Offset<flatbuffers::String>>" + "_fbb.CreateVector<::flatbuffers::Offset<::flatbuffers::" + "String>>" " "; code += "(" + value + ".size(), "; code += "[](size_t i, _VectorArgs *__va) { "; @@ -3219,7 +3223,7 @@ class CppGenerator : public BaseGenerator { const auto pack_name = struct_attrs.Lookup("native_type_pack_name"); if (pack_name) { - code += ", flatbuffers::Pack" + pack_name->constant; + code += ", ::flatbuffers::Pack" + pack_name->constant; } code += ")"; } else { @@ -3227,7 +3231,7 @@ class CppGenerator : public BaseGenerator { code += "(" + value + ")"; } } else { - code += "_fbb.CreateVector<flatbuffers::Offset<"; + code += "_fbb.CreateVector<::flatbuffers::Offset<"; code += WrapInNameSpace(*vector_type.struct_def) + ">> "; code += "(" + value + ".size(), "; code += "[](size_t i, _VectorArgs *__va) { "; @@ -3248,7 +3252,7 @@ class CppGenerator : public BaseGenerator { } case BASE_TYPE_UNION: { code += - "_fbb.CreateVector<flatbuffers::" + "_fbb.CreateVector<::flatbuffers::" "Offset<void>>(" + value + ".size(), [](size_t i, _VectorArgs *__va) { " @@ -3277,7 +3281,7 @@ class CppGenerator : public BaseGenerator { const auto basetype = GenTypeBasic( field.value.type.enum_def->underlying_type, false); code += "_fbb.CreateVectorScalarCast<" + basetype + - ">(flatbuffers::data(" + value + "), " + value + + ">(::flatbuffers::data(" + value + "), " + value + ".size())"; } else if (field.attributes.Lookup("cpp_type")) { auto type = GenTypeBasic(vector_type, false); @@ -3313,7 +3317,7 @@ class CppGenerator : public BaseGenerator { const auto &struct_attribs = field.value.type.struct_def->attributes; const auto native_type = struct_attribs.Lookup("native_type"); if (native_type) { - code += "flatbuffers::Pack"; + code += "::flatbuffers::Pack"; const auto pack_name = struct_attribs.Lookup("native_type_pack_name"); if (pack_name) { code += pack_name->constant; } @@ -3415,11 +3419,11 @@ class CppGenerator : public BaseGenerator { code_ += " struct _VectorArgs " - "{ flatbuffers::FlatBufferBuilder *__fbb; " + "{ ::flatbuffers::FlatBufferBuilder *__fbb; " "const " + NativeName(Name(struct_def), &struct_def, opts_) + "* __o; " - "const flatbuffers::rehasher_function_t *__rehasher; } _va = { " + "const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { " "&_fbb, _o, _rehasher}; (void)_va;"; for (auto it = struct_def.fields.vec.begin(); @@ -3568,7 +3572,7 @@ class CppGenerator : public BaseGenerator { init_list += Name(field) + "_"; if (IsScalar(type.base_type)) { auto scalar_type = GenUnderlyingCast(field, false, arg_name); - init_list += "(flatbuffers::EndianScalar(" + scalar_type + "))"; + init_list += "(::flatbuffers::EndianScalar(" + scalar_type + "))"; } else { FLATBUFFERS_ASSERT((is_array && !init_arrays) || IsStruct(type)); if (!is_array) @@ -3604,7 +3608,7 @@ class CppGenerator : public BaseGenerator { is_enum ? "CastToArrayOfEnum<" + face_type + ">" : "CastToArray"; const auto field_name = Name(*field) + "_"; const auto arg_name = "_" + Name(*field); - code_ += " flatbuffers::" + get_array + "(" + field_name + + code_ += " ::flatbuffers::" + get_array + "(" + field_name + ").CopyFromSpan(" + arg_name + ");"; } if (field->padding) { @@ -3624,7 +3628,7 @@ class CppGenerator : public BaseGenerator { // It requires a specialization of Array class. // Generate Array<uint8_t> for Array<bool>. const auto face_type = GenTypeGet(type, " ", "", "", is_enum); - std::string ret_type = "flatbuffers::Array<" + face_type + ", " + + std::string ret_type = "::flatbuffers::Array<" + face_type + ", " + NumToString(type.fixed_length) + ">"; if (mutable_accessor) code_ += " " + ret_type + " *mutable_{{FIELD_NAME}}() {"; @@ -3633,7 +3637,7 @@ class CppGenerator : public BaseGenerator { std::string get_array = is_enum ? "CastToArrayOfEnum<" + face_type + ">" : "CastToArray"; - code_ += " return &flatbuffers::" + get_array + "({{FIELD_VALUE}});"; + code_ += " return &::flatbuffers::" + get_array + "({{FIELD_VALUE}});"; code_ += " }"; } @@ -3654,7 +3658,7 @@ class CppGenerator : public BaseGenerator { code_ += " private:"; int padding_id = 0; - for (const auto &field: struct_def.fields.vec) { + for (const auto &field : struct_def.fields.vec) { const auto &field_type = field->value.type; code_.SetValue("FIELD_TYPE", GenTypeGet(field_type, " ", "", " ", false)); code_.SetValue("FIELD_NAME", Name(*field)); @@ -3680,7 +3684,7 @@ class CppGenerator : public BaseGenerator { // Make TypeTable accessible via the generated struct. if (opts_.mini_reflect != IDLOptions::kNone) { code_ += - " static const flatbuffers::TypeTable *MiniReflectTypeTable() {"; + " static const ::flatbuffers::TypeTable *MiniReflectTypeTable() {"; code_ += " return {{STRUCT_NAME}}TypeTable();"; code_ += " }"; } @@ -3694,17 +3698,15 @@ class CppGenerator : public BaseGenerator { // excluding arrays. GenStructConstructor(struct_def, kArrayArgModeNone); - auto arrays_num = std::count_if(struct_def.fields.vec.begin(), - struct_def.fields.vec.end(), - [](const flatbuffers::FieldDef *fd) { - return IsArray(fd->value.type); - }); + auto arrays_num = std::count_if( + struct_def.fields.vec.begin(), struct_def.fields.vec.end(), + [](const FieldDef *fd) { return IsArray(fd->value.type); }); if (arrays_num > 0) { GenStructConstructor(struct_def, kArrayArgModeSpanStatic); } // Generate accessor methods of the form: - // type name() const { return flatbuffers::EndianScalar(name_); } + // type name() const { return ::flatbuffers::EndianScalar(name_); } for (const auto &field : struct_def.fields.vec) { const auto &type = field->value.type; const auto is_scalar = IsScalar(type.base_type); @@ -3714,7 +3716,7 @@ class CppGenerator : public BaseGenerator { is_array ? "" : " &", true); auto member = Name(*field) + "_"; auto value = - is_scalar ? "flatbuffers::EndianScalar(" + member + ")" : member; + is_scalar ? "::flatbuffers::EndianScalar(" + member + ")" : member; code_.SetValue("FIELD_NAME", Name(*field)); code_.SetValue("FIELD_TYPE", field_type); @@ -3743,7 +3745,7 @@ class CppGenerator : public BaseGenerator { code_ += " void mutate_{{FIELD_NAME}}({{ARG}} _{{FIELD_NAME}}) {"; code_ += - " flatbuffers::WriteScalar(&{{FIELD_NAME}}_, " + " ::flatbuffers::WriteScalar(&{{FIELD_NAME}}_, " "{{FIELD_VALUE}});"; code_ += " }"; } else if (is_array) { @@ -3858,8 +3860,7 @@ bool GenerateCPP(const Parser &parser, const std::string &path, std::string CPPMakeRule(const Parser &parser, const std::string &path, const std::string &file_name) { - const auto filebase = - flatbuffers::StripPath(flatbuffers::StripExtension(file_name)); + const auto filebase = StripPath(StripExtension(file_name)); cpp::CppGenerator geneartor(parser, path, file_name, parser.opts); const auto included_files = parser.GetIncludedFilesRecursive(file_name); std::string make_rule = |