diff options
Diffstat (limited to 'src/google/protobuf/compiler/cpp/cpp_map_field.cc')
-rw-r--r-- | src/google/protobuf/compiler/cpp/cpp_map_field.cc | 118 |
1 files changed, 54 insertions, 64 deletions
diff --git a/src/google/protobuf/compiler/cpp/cpp_map_field.cc b/src/google/protobuf/compiler/cpp/cpp_map_field.cc index b4eaf485..0e485cac 100644 --- a/src/google/protobuf/compiler/cpp/cpp_map_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_map_field.cc @@ -32,6 +32,7 @@ #include <google/protobuf/compiler/cpp/cpp_helpers.h> #include <google/protobuf/io/printer.h> #include <google/protobuf/wire_format.h> + #include <google/protobuf/stubs/strutil.h> namespace google { @@ -103,9 +104,7 @@ void SetMessageVariables(const FieldDescriptor* descriptor, MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor, const Options& options) - : FieldGenerator(options), - descriptor_(descriptor), - dependent_field_(options.proto_h && IsFieldDependent(descriptor)) { + : FieldGenerator(options), descriptor_(descriptor) { SetMessageVariables(descriptor, &variables_, options); } @@ -114,40 +113,36 @@ MapFieldGenerator::~MapFieldGenerator() {} void MapFieldGenerator:: GeneratePrivateMembers(io::Printer* printer) const { printer->Print(variables_, - "typedef ::google::protobuf::internal::MapEntryLite<\n" - " $key_cpp$, $val_cpp$,\n" - " $key_wire_type$,\n" - " $val_wire_type$,\n" - " $default_enum_value$ >\n" - " $map_classname$;\n" - "::google::protobuf::internal::MapField$lite$<\n" - " $key_cpp$, $val_cpp$,\n" - " $key_wire_type$,\n" - " $val_wire_type$,\n" - " $default_enum_value$ > $name$_;\n"); + "::google::protobuf::internal::MapField$lite$<\n" + " $map_classname$,\n" + " $key_cpp$, $val_cpp$,\n" + " $key_wire_type$,\n" + " $val_wire_type$,\n" + " $default_enum_value$ > $name$_;\n"); } void MapFieldGenerator:: GenerateAccessorDeclarations(io::Printer* printer) const { - printer->Print(variables_, + printer->Print( + variables_, "$deprecated_attr$const ::google::protobuf::Map< $key_cpp$, $val_cpp$ >&\n" - " $name$() const;\n" - "$deprecated_attr$::google::protobuf::Map< $key_cpp$, $val_cpp$ >*\n" - " mutable_$name$();\n"); + " $name$() const;\n"); + printer->Annotate("name", descriptor_); + printer->Print(variables_, + "$deprecated_attr$::google::protobuf::Map< $key_cpp$, $val_cpp$ >*\n" + " ${$mutable_$name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); } void MapFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const { - std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline" : ""; - printer->Print(variables, - "$inline$ const ::google::protobuf::Map< $key_cpp$, $val_cpp$ >&\n" +GenerateInlineAccessorDefinitions(io::Printer* printer) const { + printer->Print(variables_, + "inline const ::google::protobuf::Map< $key_cpp$, $val_cpp$ >&\n" "$classname$::$name$() const {\n" " // @@protoc_insertion_point(field_map:$full_name$)\n" " return $name$_.GetMap();\n" "}\n" - "$inline$ ::google::protobuf::Map< $key_cpp$, $val_cpp$ >*\n" + "inline ::google::protobuf::Map< $key_cpp$, $val_cpp$ >*\n" "$classname$::mutable_$name$() {\n" " // @@protoc_insertion_point(field_mutable_map:$full_name$)\n" " return $name$_.MutableMap();\n" @@ -156,9 +151,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, void MapFieldGenerator:: GenerateClearingCode(io::Printer* printer) const { - std::map<string, string> variables(variables_); - variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : ""; - printer->Print(variables, "$this_message$$name$_.Clear();\n"); + printer->Print(variables_, "$name$_.Clear();\n"); } void MapFieldGenerator:: @@ -172,17 +165,6 @@ GenerateSwappingCode(io::Printer* printer) const { } void MapFieldGenerator:: -GenerateConstructorCode(io::Printer* printer) const { - if (HasDescriptorMethods(descriptor_->file(), options_)) { - printer->Print(variables_, - "$name$_.SetAssignDescriptorCallback(\n" - " $file_namespace$::protobuf_AssignDescriptorsOnce);\n" - "$name$_.SetEntryDescriptor(\n" - " &$type$_descriptor);\n"); - } -} - -void MapFieldGenerator:: GenerateCopyConstructorCode(io::Printer* printer) const { GenerateConstructorCode(printer); GenerateMergingCode(printer); @@ -199,13 +181,15 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { string value; if (IsProto3Field(descriptor_) || value_field->type() != FieldDescriptor::TYPE_ENUM) { - printer->Print(variables_, + printer->Print( + variables_, "$map_classname$::Parser< ::google::protobuf::internal::MapField$lite$<\n" - " $key_cpp$, $val_cpp$,\n" - " $key_wire_type$,\n" - " $val_wire_type$,\n" - " $default_enum_value$ >,\n" - " ::google::protobuf::Map< $key_cpp$, $val_cpp$ > >" + " $map_classname$,\n" + " $key_cpp$, $val_cpp$,\n" + " $key_wire_type$,\n" + " $val_wire_type$,\n" + " $default_enum_value$ >,\n" + " ::google::protobuf::Map< $key_cpp$, $val_cpp$ > >" " parser(&$name$_);\n" "DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(\n" " input, &parser));\n"); @@ -216,7 +200,7 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { key = "entry->key()"; value = "entry->value()"; printer->Print(variables_, - "::google::protobuf::scoped_ptr<$map_classname$> entry($name$_.NewEntry());\n"); + "::std::unique_ptr<$map_classname$> entry($name$_.NewEntry());\n"); printer->Print(variables_, "{\n" " ::std::string data;\n" @@ -233,7 +217,8 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { } else { printer->Print(variables_, " unknown_fields_stream.WriteVarint32($tag$u);\n" - " unknown_fields_stream.WriteVarint32(data.size());\n" + " unknown_fields_stream.WriteVarint32(\n" + " static_cast< ::google::protobuf::uint32>(data.size()));\n" " unknown_fields_stream.WriteString(data);\n"); } @@ -244,12 +229,16 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { if (key_field->type() == FieldDescriptor::TYPE_STRING) { GenerateUtf8CheckCodeForString( - key_field, options_, true, variables_, - StrCat(key, ".data(), ", key, ".length(),\n").data(), printer); + key_field, options_, true, variables_, + StrCat(key, ".data(), static_cast<int>(", key, ".length()),\n").data(), + printer); } if (value_field->type() == FieldDescriptor::TYPE_STRING) { - GenerateUtf8CheckCodeForString(value_field, options_, true, variables_, - StrCat(value, ".data(), ", value, ".length(),\n").data(), printer); + GenerateUtf8CheckCodeForString( + value_field, options_, true, variables_, + StrCat(value, ".data(), static_cast<int>(", value, ".length()),\n") + .data(), + printer); } // If entry is allocated by arena, its desctructor should be avoided. @@ -267,7 +256,7 @@ static void GenerateSerializationLoop(io::Printer* printer, const string& ptr, bool loop_via_iterators) { printer->Print(variables, - StrCat("::google::protobuf::scoped_ptr<$map_classname$> entry;\n", + StrCat("::std::unique_ptr<$map_classname$> entry;\n", loop_header, " {\n").c_str()); printer->Indent(); @@ -353,14 +342,14 @@ void MapFieldGenerator::GenerateSerializeWithCachedSizes( printer->Indent(); printer->Indent(); if (string_key) { - GenerateUtf8CheckCodeForString(key_field, options_, false, variables, - "p->first.data(), p->first.length(),\n", - printer); + GenerateUtf8CheckCodeForString( + key_field, options_, false, variables, + "p->first.data(), static_cast<int>(p->first.length()),\n", printer); } if (string_value) { - GenerateUtf8CheckCodeForString(value_field, options_, false, variables, - "p->second.data(), p->second.length(),\n", - printer); + GenerateUtf8CheckCodeForString( + value_field, options_, false, variables, + "p->second.data(), static_cast<int>(p->second.length()),\n", printer); } printer->Outdent(); printer->Outdent(); @@ -374,20 +363,21 @@ void MapFieldGenerator::GenerateSerializeWithCachedSizes( "\n" "if ($deterministic$ &&\n" " this->$name$().size() > 1) {\n" - " ::google::protobuf::scoped_array<SortItem> items(\n" + " ::std::unique_ptr<SortItem[]> items(\n" " new SortItem[this->$name$().size()]);\n" " typedef ::google::protobuf::Map< $key_cpp$, $val_cpp$ >::size_type size_type;\n" " size_type n = 0;\n" " for (::google::protobuf::Map< $key_cpp$, $val_cpp$ >::const_iterator\n" " it = this->$name$().begin();\n" " it != this->$name$().end(); ++it, ++n) {\n" - " items[n] = SortItem(&*it);\n" + " items[static_cast<ptrdiff_t>(n)] = SortItem(&*it);\n" " }\n" - " ::std::sort(&items[0], &items[n], Less());\n"); + " ::std::sort(&items[0], &items[static_cast<ptrdiff_t>(n)], Less());\n"); printer->Indent(); GenerateSerializationLoop(printer, variables, SupportsArenas(descriptor_), - utf8_check, "for (size_type i = 0; i < n; i++)", - string_key ? "items[i]" : "items[i].second", false); + utf8_check, "for (size_type i = 0; i < n; i++)", + string_key ? "items[static_cast<ptrdiff_t>(i)]" : + "items[static_cast<ptrdiff_t>(i)].second", false); printer->Outdent(); printer->Print( "} else {\n"); @@ -410,7 +400,7 @@ GenerateByteSize(io::Printer* printer) const { "total_size += $tag_size$ *\n" " ::google::protobuf::internal::FromIntSize(this->$name$_size());\n" "{\n" - " ::google::protobuf::scoped_ptr<$map_classname$> entry;\n" + " ::std::unique_ptr<$map_classname$> entry;\n" " for (::google::protobuf::Map< $key_cpp$, $val_cpp$ >::const_iterator\n" " it = this->$name$().begin();\n" " it != this->$name$().end(); ++it) {\n"); |