diff options
Diffstat (limited to 'src/google/protobuf/compiler/cpp/cpp_string_field.cc')
-rw-r--r-- | src/google/protobuf/compiler/cpp/cpp_string_field.cc | 1083 |
1 files changed, 623 insertions, 460 deletions
diff --git a/src/google/protobuf/compiler/cpp/cpp_string_field.cc b/src/google/protobuf/compiler/cpp/cpp_string_field.cc index cf6c4b33..ecfabe64 100644 --- a/src/google/protobuf/compiler/cpp/cpp_string_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_string_field.cc @@ -36,9 +36,9 @@ #include <google/protobuf/compiler/cpp/cpp_helpers.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/io/printer.h> - #include <google/protobuf/stubs/strutil.h> + namespace google { namespace protobuf { namespace compiler { @@ -50,19 +50,20 @@ void SetStringVariables(const FieldDescriptor* descriptor, std::map<string, string>* variables, const Options& options) { SetCommonFieldVariables(descriptor, variables, options); - (*variables)["default"] = DefaultValue(descriptor); + (*variables)["default"] = DefaultValue(options, descriptor); (*variables)["default_length"] = SimpleItoa(descriptor->default_value_string().length()); string default_variable_string = MakeDefaultName(descriptor); (*variables)["default_variable_name"] = default_variable_string; (*variables)["default_variable"] = descriptor->default_value_string().empty() - ? "&::google::protobuf::internal::GetEmptyStringAlreadyInited()" + ? "&::" + (*variables)["proto_ns"] + + "::internal::GetEmptyStringAlreadyInited()" : "&" + Namespace(descriptor) + "::" + (*variables)["classname"] + "::" + default_variable_string + ".get()"; (*variables)["pointer_type"] = descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char"; - (*variables)["null_check"] = "GOOGLE_DCHECK(value != NULL);\n"; + (*variables)["null_check"] = (*variables)["DCHK"] + "(value != NULL);\n"; // NOTE: Escaped here to unblock proto1->proto2 migration. // TODO(liujisi): Extend this to apply for other conflicting methods. (*variables)["release_name"] = @@ -70,7 +71,11 @@ void SetStringVariables(const FieldDescriptor* descriptor, descriptor, "release_"); (*variables)["full_name"] = descriptor->full_name(); - (*variables)["string_piece"] = "::std::string"; + if (options.opensource_runtime) { + (*variables)["string_piece"] = "::std::string"; + } else { + (*variables)["string_piece"] = "::StringPiece"; + } (*variables)["lite"] = HasDescriptorMethods(descriptor->file(), options) ? "" : "Lite"; @@ -82,8 +87,7 @@ void SetStringVariables(const FieldDescriptor* descriptor, StringFieldGenerator::StringFieldGenerator(const FieldDescriptor* descriptor, const Options& options) - : FieldGenerator(options), - descriptor_(descriptor), + : FieldGenerator(descriptor, options), lite_(!HasDescriptorMethods(descriptor->file(), options)), inlined_(false) { @@ -107,9 +111,9 @@ StringFieldGenerator::~StringFieldGenerator() {} void StringFieldGenerator:: GeneratePrivateMembers(io::Printer* printer) const { + Formatter format(printer, variables_); if (inlined_) { - printer->Print(variables_, - "::google::protobuf::internal::InlinedStringField $name$_;\n"); + format("::$proto_ns$::internal::InlinedStringField $name$_;\n"); } else { // N.B. that we continue to use |ArenaStringPtr| instead of |string*| for // string fields, even when SupportArenas(descriptor_) == false. Why? The @@ -123,25 +127,27 @@ GeneratePrivateMembers(io::Printer* printer) const { // conditionals. It's simpler to stick with this but use lightweight // accessors that assume arena == NULL. There should be very little // overhead anyway because it's just a tagged pointer in-memory. - printer->Print(variables_, "::google::protobuf::internal::ArenaStringPtr $name$_;\n"); + format("::$proto_ns$::internal::ArenaStringPtr $name$_;\n"); } } void StringFieldGenerator:: GenerateStaticMembers(io::Printer* printer) const { + Formatter format(printer, variables_); if (!descriptor_->default_value_string().empty()) { // We make the default instance public, so it can be initialized by // non-friend code. - printer->Print(variables_, - "public:\n" - "static ::google::protobuf::internal::ExplicitlyConstructed< ::std::string>" - " $default_variable_name$;\n" - "private:\n"); + format( + "public:\n" + "static ::$proto_ns$::internal::ExplicitlyConstructed<$string$>" + " $default_variable_name$;\n" + "private:\n"); } } void StringFieldGenerator:: GenerateAccessorDeclarations(io::Printer* printer) const { + Formatter format(printer, variables_); // If we're using StringFieldGenerator for a field with a ctype, it's // because that ctype isn't actually implemented. In particular, this is // true of ctype=CORD and ctype=STRING_PIECE in the open source release. @@ -159,90 +165,82 @@ GenerateAccessorDeclarations(io::Printer* printer) const { // reflection interface since the reflection interface is independent of // the string's underlying representation. - bool unknown_ctype = - descriptor_->options().ctype() != EffectiveStringCType(descriptor_); + bool unknown_ctype = descriptor_->options().ctype() != + EffectiveStringCType(descriptor_, options_); if (unknown_ctype) { - printer->Outdent(); - printer->Print( - " private:\n" - " // Hidden due to unknown ctype option.\n"); - printer->Indent(); + format.Outdent(); + format( + " private:\n" + " // Hidden due to unknown ctype option.\n"); + format.Indent(); } - printer->Print(variables_, - "$deprecated_attr$const ::std::string& $name$() const;\n"); - printer->Annotate("name", descriptor_); - printer->Print( - variables_, - "$deprecated_attr$void ${$set_$name$$}$(const ::std::string& value);\n"); - printer->Annotate("{", "}", descriptor_); - - printer->Print(variables_, - "#if LANG_CXX11\n" - "$deprecated_attr$void ${$set_$name$$}$(::std::string&& value);\n" - "#endif\n"); - printer->Annotate("{", "}", descriptor_); - - printer->Print( - variables_, - "$deprecated_attr$void ${$set_$name$$}$(const char* value);\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, - "$deprecated_attr$void ${$set_$name$$}$(const $pointer_type$* " - "value, size_t size)" - ";\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, - "$deprecated_attr$::std::string* ${$mutable_$name$$}$();\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, "$deprecated_attr$::std::string* $release_name$();\n"); - printer->Annotate("release_name", descriptor_); - printer->Print( - variables_, - "$deprecated_attr$void ${$set_allocated_$name$$}$(::std::string* $name$);\n"); - printer->Annotate("{", "}", descriptor_); - if (SupportsArenas(descriptor_)) { - printer->Print( - variables_, - "PROTOBUF_RUNTIME_DEPRECATED(\"The unsafe_arena_ accessors for\"\n" - "\" string fields are deprecated and will be removed in a\"\n" - "\" future release.\")\n" - "::std::string* ${$unsafe_arena_release_$name$$}$();\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print( - variables_, - "PROTOBUF_RUNTIME_DEPRECATED(\"The unsafe_arena_ accessors for\"\n" - "\" string fields are deprecated and will be removed in a\"\n" - "\" future release.\")\n" - "void ${$unsafe_arena_set_allocated_$name$$}$(\n" - " ::std::string* $name$);\n"); - printer->Annotate("{", "}", descriptor_); + format( + "$deprecated_attr$const $string$& ${1$$name$$}$() const;\n" + "$deprecated_attr$void ${1$set_$name$$}$(const $string$& value);\n" + "#if LANG_CXX11\n" + "$deprecated_attr$void ${1$set_$name$$}$($string$&& value);\n" + "#endif\n" + "$deprecated_attr$void ${1$set_$name$$}$(const char* value);\n", + descriptor_); + if (!options_.opensource_runtime) { + format( + "$deprecated_attr$void ${1$set_$name$$}$(::StringPiece value);\n" + "#ifdef HAS_GLOBAL_STRING\n" + "$deprecated_attr$void ${1$set_$name$$}$(const ::std::string& value);\n" + "#endif\n", + descriptor_); + } + format( + "$deprecated_attr$void ${1$set_$name$$}$(const $pointer_type$* " + "value, size_t size)" + ";\n" + "$deprecated_attr$$string$* ${1$mutable_$name$$}$();\n" + "$deprecated_attr$$string$* ${1$$release_name$$}$();\n" + "$deprecated_attr$void ${1$set_allocated_$name$$}$($string$* $name$);\n", + descriptor_); + if (options_.opensource_runtime) { + if (SupportsArenas(descriptor_)) { + format( + "$GOOGLE_PROTOBUF$_RUNTIME_DEPRECATED(\"The unsafe_arena_ accessors " + "for\"\n" + "\" string fields are deprecated and will be removed in a\"\n" + "\" future release.\")\n" + "$string$* ${1$unsafe_arena_release_$name$$}$();\n" + "$GOOGLE_PROTOBUF$_RUNTIME_DEPRECATED(\"The unsafe_arena_ accessors " + "for\"\n" + "\" string fields are deprecated and will be removed in a\"\n" + "\" future release.\")\n" + "void ${1$unsafe_arena_set_allocated_$name$$}$(\n" + " $string$* $name$);\n", + descriptor_); + } } if (unknown_ctype) { - printer->Outdent(); - printer->Print(" public:\n"); - printer->Indent(); + format.Outdent(); + format(" public:\n"); + format.Indent(); } } void StringFieldGenerator:: GenerateInlineAccessorDefinitions(io::Printer* printer) const { + Formatter format(printer, variables_); if (SupportsArenas(descriptor_)) { - printer->Print( - variables_, - "inline const ::std::string& $classname$::$name$() const {\n" + format( + "inline const $string$& $classname$::$name$() const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_.Get();\n" "}\n" - "inline void $classname$::set_$name$(const ::std::string& value) {\n" + "inline void $classname$::set_$name$(const $string$& value) {\n" " $set_hasbit$\n" " $name$_.Set$lite$($default_variable$, value, GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" "#if LANG_CXX11\n" - "inline void $classname$::set_$name$(::std::string&& value) {\n" + "inline void $classname$::set_$name$($string$&& value) {\n" " $set_hasbit$\n" " $name$_.Set$lite$(\n" " $default_variable$, ::std::move(value), GetArenaNoVirtual());\n" @@ -255,7 +253,26 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " $name$_.Set$lite$($default_variable$, $string_piece$(value),\n" " GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_char:$full_name$)\n" - "}\n" + "}\n"); + if (!options_.opensource_runtime) { + format( + "inline void $classname$::set_$name$(::StringPiece value) {\n" + " $set_hasbit$\n" + " $name$_.Set$lite$($default_variable$, value, " + "GetArenaNoVirtual());\n" + " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n" + "}\n" + "#ifdef HAS_GLOBAL_STRING\n" + "inline void $classname$::set_$name$(const ::std::string& value) {\n" + " $set_hasbit$\n" + " $name$_.Set$lite$($default_variable$, " + "::StringPiece(value.data(),\n" + " value.size()), GetArenaNoVirtual());\n" + " // @@protoc_insertion_point(field_set_std_string:$full_name$)\n" + "}\n" + "#endif\n"); + } + format( "inline " "void $classname$::set_$name$(const $pointer_type$* value,\n" " size_t size) {\n" @@ -265,31 +282,32 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { "GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "inline ::std::string* $classname$::mutable_$name$() {\n" + "inline $string$* $classname$::mutable_$name$() {\n" " $set_hasbit$\n" " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $name$_.Mutable($default_variable$, GetArenaNoVirtual());\n" "}\n" - "inline ::std::string* $classname$::$release_name$() {\n" + "inline $string$* $classname$::$release_name$() {\n" " // @@protoc_insertion_point(field_release:$full_name$)\n"); if (HasFieldPresence(descriptor_->file())) { - printer->Print(variables_, - " if (!has_$name$()) {\n" - " return NULL;\n" - " }\n" - " $clear_hasbit$\n" - " return $name$_.ReleaseNonDefault(" - "$default_variable$, GetArenaNoVirtual());\n"); + format( + " if (!has_$name$()) {\n" + " return NULL;\n" + " }\n" + " $clear_hasbit$\n" + " return $name$_.ReleaseNonDefault(" + "$default_variable$, GetArenaNoVirtual());\n"); } else { - printer->Print(variables_, - " $clear_hasbit$\n" - " return $name$_.Release($default_variable$, GetArenaNoVirtual());\n"); + format( + " $clear_hasbit$\n" + " return $name$_.Release($default_variable$, " + "GetArenaNoVirtual());\n"); } - printer->Print(variables_, + format( "}\n" - "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n" + "inline void $classname$::set_allocated_$name$($string$* $name$) {\n" " if ($name$ != NULL) {\n" " $set_hasbit$\n" " } else {\n" @@ -298,43 +316,45 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " $name$_.SetAllocated($default_variable$, $name$,\n" " GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" - "}\n" - "inline ::std::string* $classname$::unsafe_arena_release_$name$() {\n" - " // " - "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" - " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" - " $clear_hasbit$\n" - " return $name$_.UnsafeArenaRelease($default_variable$,\n" - " GetArenaNoVirtual());\n" - "}\n" - "inline void $classname$::unsafe_arena_set_allocated_$name$(\n" - " ::std::string* $name$) {\n" - " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" - " if ($name$ != NULL) {\n" - " $set_hasbit$\n" - " } else {\n" - " $clear_hasbit$\n" - " }\n" - " $name$_.UnsafeArenaSetAllocated($default_variable$,\n" - " $name$, GetArenaNoVirtual());\n" - " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:" - "$full_name$)\n" "}\n"); + if (options_.opensource_runtime) { + format( + "inline $string$* $classname$::unsafe_arena_release_$name$() {\n" + " // " + "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" + " $DCHK$(GetArenaNoVirtual() != NULL);\n" + " $clear_hasbit$\n" + " return $name$_.UnsafeArenaRelease($default_variable$,\n" + " GetArenaNoVirtual());\n" + "}\n" + "inline void $classname$::unsafe_arena_set_allocated_$name$(\n" + " $string$* $name$) {\n" + " $DCHK$(GetArenaNoVirtual() != NULL);\n" + " if ($name$ != NULL) {\n" + " $set_hasbit$\n" + " } else {\n" + " $clear_hasbit$\n" + " }\n" + " $name$_.UnsafeArenaSetAllocated($default_variable$,\n" + " $name$, GetArenaNoVirtual());\n" + " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:" + "$full_name$)\n" + "}\n"); + } } else { // No-arena case. - printer->Print( - variables_, - "inline const ::std::string& $classname$::$name$() const {\n" + format( + "inline const $string$& $classname$::$name$() const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_.GetNoArena();\n" "}\n" - "inline void $classname$::set_$name$(const ::std::string& value) {\n" + "inline void $classname$::set_$name$(const $string$& value) {\n" " $set_hasbit$\n" " $name$_.SetNoArena($default_variable$, value);\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" "#if LANG_CXX11\n" - "inline void $classname$::set_$name$(::std::string&& value) {\n" + "inline void $classname$::set_$name$($string$&& value) {\n" " $set_hasbit$\n" " $name$_.SetNoArena(\n" " $default_variable$, ::std::move(value));\n" @@ -346,7 +366,24 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " $set_hasbit$\n" " $name$_.SetNoArena($default_variable$, $string_piece$(value));\n" " // @@protoc_insertion_point(field_set_char:$full_name$)\n" - "}\n" + "}\n"); + if (!options_.opensource_runtime) { + format( + "inline void $classname$::set_$name$(::StringPiece value) {\n" + " $set_hasbit$\n" + " $name$_.SetNoArena($default_variable$, value);\n" + " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n" + "}\n" + "#ifdef HAS_GLOBAL_STRING\n" + "inline void $classname$::set_$name$(const ::std::string& value) {\n" + " $set_hasbit$\n" + " $name$_.SetNoArena($default_variable$,\n" + " ::StringPiece(value.data(), value.size()));\n" + " // @@protoc_insertion_point(field_set_std_string:$full_name$)\n" + "}\n" + "#endif\n"); + } + format( "inline " "void $classname$::set_$name$(const $pointer_type$* value, " "size_t size) {\n" @@ -355,30 +392,30 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " $string_piece$(reinterpret_cast<const char*>(value), size));\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "inline ::std::string* $classname$::mutable_$name$() {\n" + "inline $string$* $classname$::mutable_$name$() {\n" " $set_hasbit$\n" " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $name$_.MutableNoArena($default_variable$);\n" "}\n" - "inline ::std::string* $classname$::$release_name$() {\n" + "inline $string$* $classname$::$release_name$() {\n" " // @@protoc_insertion_point(field_release:$full_name$)\n"); if (HasFieldPresence(descriptor_->file())) { - printer->Print(variables_, - " if (!has_$name$()) {\n" - " return NULL;\n" - " }\n" - " $clear_hasbit$\n" - " return $name$_.ReleaseNonDefaultNoArena($default_variable$);\n"); + format( + " if (!has_$name$()) {\n" + " return NULL;\n" + " }\n" + " $clear_hasbit$\n" + " return $name$_.ReleaseNonDefaultNoArena($default_variable$);\n"); } else { - printer->Print(variables_, - " $clear_hasbit$\n" - " return $name$_.ReleaseNoArena($default_variable$);\n"); + format( + " $clear_hasbit$\n" + " return $name$_.ReleaseNoArena($default_variable$);\n"); } - printer->Print(variables_, + format( "}\n" - "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n" + "inline void $classname$::set_allocated_$name$($string$* $name$) {\n" " if ($name$ != NULL) {\n" " $set_hasbit$\n" " } else {\n" @@ -392,41 +429,42 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { void StringFieldGenerator:: GenerateNonInlineAccessorDefinitions(io::Printer* printer) const { + Formatter format(printer, variables_); if (!descriptor_->default_value_string().empty()) { // Initialized in GenerateDefaultInstanceAllocator. - printer->Print(variables_, - "::google::protobuf::internal::ExplicitlyConstructed<::std::string> " - "$classname$::$default_variable_name$;\n"); + format( + "::$proto_ns$::internal::ExplicitlyConstructed<$string$> " + "$classname$::$default_variable_name$;\n"); } } void StringFieldGenerator:: GenerateClearingCode(io::Printer* printer) const { + Formatter format(printer, variables_); // Two-dimension specialization here: supporting arenas or not, and default // value is the empty string or not. Complexity here ensures the minimal // number of branches / amount of extraneous code at runtime (given that the // below methods are inlined one-liners)! if (SupportsArenas(descriptor_)) { if (descriptor_->default_value_string().empty()) { - printer->Print(variables_, - "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n"); + format( + "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n"); } else { - printer->Print(variables_, - "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n"); + format( + "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n"); } } else { if (descriptor_->default_value_string().empty()) { - printer->Print(variables_, - "$name$_.ClearToEmptyNoArena($default_variable$);\n"); + format("$name$_.ClearToEmptyNoArena($default_variable$);\n"); } else { - printer->Print(variables_, - "$name$_.ClearToDefaultNoArena($default_variable$);\n"); + format("$name$_.ClearToDefaultNoArena($default_variable$);\n"); } } } void StringFieldGenerator:: GenerateMessageClearingCode(io::Printer* printer) const { + Formatter format(printer, variables_); // Two-dimension specialization here: supporting arenas, field presence, or // not, and default value is the empty string or not. Complexity here ensures // the minimal number of branches / amount of extraneous code at runtime @@ -447,67 +485,60 @@ GenerateMessageClearingCode(io::Printer* printer) const { // // For non-inlined strings, we distinguish from non-default by comparing // instances, rather than contents. - printer->Print(variables_, - "GOOGLE_DCHECK(!$name$_.IsDefault($default_variable$));\n"); + format("$DCHK$(!$name$_.IsDefault($default_variable$));\n"); } if (SupportsArenas(descriptor_)) { if (descriptor_->default_value_string().empty()) { if (must_be_present) { - printer->Print(variables_, - "$name$_.ClearNonDefaultToEmpty();\n"); + format("$name$_.ClearNonDefaultToEmpty();\n"); } else { - printer->Print(variables_, - "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n"); + format( + "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n"); } } else { // Clear to a non-empty default is more involved, as we try to use the // Arena if one is present and may need to reallocate the string. - printer->Print(variables_, - "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n"); + format( + "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n"); } } else if (must_be_present) { // When Arenas are disabled and field presence has been checked, we can // safely treat the ArenaStringPtr as a string*. if (descriptor_->default_value_string().empty()) { - printer->Print(variables_, "$name$_.ClearNonDefaultToEmptyNoArena();\n"); + format("$name$_.ClearNonDefaultToEmptyNoArena();\n"); } else { - printer->Print( - variables_, - "$name$_.UnsafeMutablePointer()->assign(*$default_variable$);\n"); + format("$name$_.UnsafeMutablePointer()->assign(*$default_variable$);\n"); } } else { if (descriptor_->default_value_string().empty()) { - printer->Print(variables_, - "$name$_.ClearToEmptyNoArena($default_variable$);\n"); + format("$name$_.ClearToEmptyNoArena($default_variable$);\n"); } else { - printer->Print(variables_, - "$name$_.ClearToDefaultNoArena($default_variable$);\n"); + format("$name$_.ClearToDefaultNoArena($default_variable$);\n"); } } } void StringFieldGenerator:: GenerateMergingCode(io::Printer* printer) const { + Formatter format(printer, variables_); if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) { // TODO(gpike): improve this - printer->Print(variables_, "set_$name$(from.$name$());\n"); + format("set_$name$(from.$name$());\n"); } else { - printer->Print(variables_, - "$set_hasbit$\n" - "$name$_.AssignWithDefault($default_variable$, from.$name$_);\n"); + format( + "$set_hasbit$\n" + "$name$_.AssignWithDefault($default_variable$, from.$name$_);\n"); } } void StringFieldGenerator:: GenerateSwappingCode(io::Printer* printer) const { + Formatter format(printer, variables_); if (inlined_) { - printer->Print( - variables_, - "$name$_.Swap(&other->$name$_);\n"); + format("$name$_.Swap(&other->$name$_);\n"); } else { - printer->Print( - variables_, + format( "$name$_.Swap(&other->$name$_, $default_variable$,\n" " GetArenaNoVirtual());\n"); } @@ -515,6 +546,7 @@ GenerateSwappingCode(io::Printer* printer) const { void StringFieldGenerator:: GenerateConstructorCode(io::Printer* printer) const { + Formatter format(printer, variables_); // TODO(ckennelly): Construct non-empty strings as part of the initializer // list. if (inlined_ && descriptor_->default_value_string().empty()) { @@ -522,125 +554,149 @@ GenerateConstructorCode(io::Printer* printer) const { return; } - printer->Print(variables_, - "$name$_.UnsafeSetDefault($default_variable$);\n"); + format("$name$_.UnsafeSetDefault($default_variable$);\n"); } void StringFieldGenerator:: GenerateCopyConstructorCode(io::Printer* printer) const { + Formatter format(printer, variables_); GenerateConstructorCode(printer); if (HasFieldPresence(descriptor_->file())) { - printer->Print(variables_, - "if (from.has_$name$()) {\n"); + format("if (from.has_$name$()) {\n"); } else { - printer->Print(variables_, - "if (from.$name$().size() > 0) {\n"); + format("if (from.$name$().size() > 0) {\n"); } - printer->Indent(); + format.Indent(); if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) { // TODO(gpike): improve this - printer->Print(variables_, - "$name$_.Set$lite$($default_variable$, from.$name$(),\n" - " GetArenaNoVirtual());\n"); + format( + "$name$_.Set$lite$($default_variable$, from.$name$(),\n" + " GetArenaNoVirtual());\n"); } else { - printer->Print(variables_, - "$name$_.AssignWithDefault($default_variable$, from.$name$_);\n"); + format("$name$_.AssignWithDefault($default_variable$, from.$name$_);\n"); } - printer->Outdent(); - printer->Print("}\n"); + format.Outdent(); + format("}\n"); } void StringFieldGenerator:: GenerateDestructorCode(io::Printer* printer) const { + Formatter format(printer, variables_); if (inlined_) { // The destructor is automatically invoked. return; } - printer->Print(variables_, "$name$_.DestroyNoArena($default_variable$);\n"); + format("$name$_.DestroyNoArena($default_variable$);\n"); } bool StringFieldGenerator::GenerateArenaDestructorCode( io::Printer* printer) const { + Formatter format(printer, variables_); if (!inlined_) { return false; } - printer->Print(variables_, - "_this->$name$_.DestroyNoArena($default_variable$);\n"); + format("_this->$name$_.DestroyNoArena($default_variable$);\n"); return true; } void StringFieldGenerator:: GenerateDefaultInstanceAllocator(io::Printer* printer) const { + Formatter format(printer, variables_); if (!descriptor_->default_value_string().empty()) { - printer->Print( - variables_, + format( "$ns$::$classname$::$default_variable_name$.DefaultConstruct();\n" "*$ns$::$classname$::$default_variable_name$.get_mutable() = " - "::std::string($default$, $default_length$);\n" - "::google::protobuf::internal::OnShutdownDestroyString(\n" - " $ns$::$classname$::$default_variable_name$.get_mutable());\n" - ); + "$string$($default$, $default_length$);\n" + "::$proto_ns$::internal::OnShutdownDestroyString(\n" + " $ns$::$classname$::$default_variable_name$.get_mutable());\n"); } } void StringFieldGenerator:: GenerateMergeFromCodedStream(io::Printer* printer) const { - printer->Print(variables_, - "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n" - " input, this->mutable_$name$()));\n"); + Formatter format(printer, variables_); + // The google3 version of proto2 has ArenaStrings and parses into them + // directly, but for the open-source release, we always parse into ::std::string + // instances. Note that for lite, we do similarly to the open source release + // and use ::std::string, not ArenaString. + if (!options_.opensource_runtime && !inlined_ && + SupportsArenas(descriptor_) && !lite_) { + // If arena != NULL, the current string is either an ArenaString (no + // destructor necessary) or a materialized ::std::string (and is on the Arena's + // destructor list). No call to ArenaStringPtr::Destroy is needed. + format( + "if (arena != NULL) {\n" + " ::$proto_ns$::internal::TaggedPtr<$string$> str =\n" + " ::$proto_ns$::internal::ReadArenaString(input, arena);\n" + " DO_(!str.IsNull());\n" + " $set_hasbit_io$\n" + " $name$_.UnsafeSetTaggedPointer(str);\n" + "} else {\n" + " DO_(::$proto_ns$::internal::WireFormatLite::Read$declared_type$(\n" + " input, this->mutable_$name$()));\n" + "}\n"); + } else { + format( + "DO_(::$proto_ns$::internal::WireFormatLite::Read$declared_type$(\n" + " input, this->mutable_$name$()));\n"); + } if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { GenerateUtf8CheckCodeForString( - descriptor_, options_, true, variables_, + descriptor_, options_, true, "this->$name$().data(), static_cast<int>(this->$name$().length()),\n", - printer); + format); } } bool StringFieldGenerator:: MergeFromCodedStreamNeedsArena() const { - return false; + return !lite_ && !inlined_ && !options_.opensource_runtime; } void StringFieldGenerator:: GenerateSerializeWithCachedSizes(io::Printer* printer) const { + Formatter format(printer, variables_); if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { GenerateUtf8CheckCodeForString( - descriptor_, options_, false, variables_, + descriptor_, options_, false, "this->$name$().data(), static_cast<int>(this->$name$().length()),\n", - printer); + format); } - printer->Print(variables_, - "::google::protobuf::internal::WireFormatLite::Write$declared_type$MaybeAliased(\n" - " $number$, this->$name$(), output);\n"); + format( + "::$proto_ns$::internal::WireFormatLite::Write$declared_type$" + "MaybeAliased(\n" + " $number$, this->$name$(), output);\n"); } void StringFieldGenerator:: GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { + Formatter format(printer, variables_); if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { GenerateUtf8CheckCodeForString( - descriptor_, options_, false, variables_, + descriptor_, options_, false, "this->$name$().data(), static_cast<int>(this->$name$().length()),\n", - printer); + format); } - printer->Print(variables_, - "target =\n" - " ::google::protobuf::internal::WireFormatLite::Write$declared_type$ToArray(\n" - " $number$, this->$name$(), target);\n"); + format( + "target =\n" + " ::$proto_ns$::internal::WireFormatLite::Write$declared_type$ToArray(\n" + " $number$, this->$name$(), target);\n"); } void StringFieldGenerator:: GenerateByteSize(io::Printer* printer) const { - printer->Print(variables_, - "total_size += $tag_size$ +\n" - " ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n" - " this->$name$());\n"); + Formatter format(printer, variables_); + format( + "total_size += $tag_size$ +\n" + " ::$proto_ns$::internal::WireFormatLite::$declared_type$Size(\n" + " this->$name$());\n"); } uint32 StringFieldGenerator::CalculateFieldTag() const { @@ -661,17 +717,17 @@ StringOneofFieldGenerator::~StringOneofFieldGenerator() {} void StringOneofFieldGenerator:: GenerateInlineAccessorDefinitions(io::Printer* printer) const { + Formatter format(printer, variables_); if (SupportsArenas(descriptor_)) { - printer->Print( - variables_, - "inline const ::std::string& $classname$::$name$() const {\n" + format( + "inline const $string$& $classname$::$name$() const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " if (has_$name$()) {\n" " return $field_member$.Get();\n" " }\n" " return *$default_variable$;\n" "}\n" - "inline void $classname$::set_$name$(const ::std::string& value) {\n" + "inline void $classname$::set_$name$(const $string$& value) {\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -682,7 +738,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" "#if LANG_CXX11\n" - "inline void $classname$::set_$name$(::std::string&& value) {\n" + "inline void $classname$::set_$name$($string$&& value) {\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" @@ -704,7 +760,34 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " $field_member$.Set$lite$($default_variable$,\n" " $string_piece$(value), GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_char:$full_name$)\n" - "}\n" + "}\n"); + if (!options_.opensource_runtime) { + format( + "inline void $classname$::set_$name$(::StringPiece value) {\n" + " if (!has_$name$()) {\n" + " clear_$oneof_name$();\n" + " set_has_$name$();\n" + " $field_member$.UnsafeSetDefault($default_variable$);\n" + " }\n" + " $field_member$.Set$lite$($default_variable$, value,\n" + " GetArenaNoVirtual());\n" + " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n" + "}\n" + "#ifdef HAS_GLOBAL_STRING\n" + "inline void $classname$::set_$name$(const ::std::string& value) {\n" + " if (!has_$name$()) {\n" + " clear_$oneof_name$();\n" + " set_has_$name$();\n" + " $field_member$.UnsafeSetDefault($default_variable$);\n" + " }\n" + " $field_member$.Set$lite$($default_variable$,\n" + " ::StringPiece(value.data(), value.size()), " + "GetArenaNoVirtual());\n" + " // @@protoc_insertion_point(field_set_std_string:$full_name$)\n" + "}\n" + "#endif\n"); + } + format( "inline " "void $classname$::set_$name$(const $pointer_type$* value,\n" " size_t size) {\n" @@ -719,7 +802,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "inline ::std::string* $classname$::mutable_$name$() {\n" + "inline $string$* $classname$::mutable_$name$() {\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -729,7 +812,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_mutable:$full_name$)\n" "}\n" - "inline ::std::string* $classname$::$release_name$() {\n" + "inline $string$* $classname$::$release_name$() {\n" " // @@protoc_insertion_point(field_release:$full_name$)\n" " if (has_$name$()) {\n" " clear_has_$oneof_name$();\n" @@ -739,57 +822,57 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " return NULL;\n" " }\n" "}\n" - "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n" - " if (!has_$name$()) {\n" - " $field_member$.UnsafeSetDefault($default_variable$);\n" + "inline void $classname$::set_allocated_$name$($string$* $name$) {\n" + " if (has_$oneof_name$()) {\n" + " clear_$oneof_name$();\n" " }\n" - " clear_$oneof_name$();\n" " if ($name$ != NULL) {\n" " set_has_$name$();\n" - " $field_member$.SetAllocated($default_variable$, $name$,\n" - " GetArenaNoVirtual());\n" + " $field_member$.UnsafeSetDefault($name$);\n" " }\n" " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" - "}\n" - "inline ::std::string* $classname$::unsafe_arena_release_$name$() {\n" - " // " - "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" - " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" - " if (has_$name$()) {\n" - " clear_has_$oneof_name$();\n" - " return $field_member$.UnsafeArenaRelease(\n" - " $default_variable$, GetArenaNoVirtual());\n" - " } else {\n" - " return NULL;\n" - " }\n" - "}\n" - "inline void $classname$::unsafe_arena_set_allocated_$name$(" - "::std::string* $name$) {\n" - " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" - " if (!has_$name$()) {\n" - " $field_member$.UnsafeSetDefault($default_variable$);\n" - " }\n" - " clear_$oneof_name$();\n" - " if ($name$) {\n" - " set_has_$name$();\n" - " $field_member$.UnsafeArenaSetAllocated($default_variable$, " - "$name$, GetArenaNoVirtual());\n" - " }\n" - " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:" - "$full_name$)\n" "}\n"); + if (options_.opensource_runtime) { + format( + "inline $string$* $classname$::unsafe_arena_release_$name$() {\n" + " // " + "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" + " $DCHK$(GetArenaNoVirtual() != NULL);\n" + " if (has_$name$()) {\n" + " clear_has_$oneof_name$();\n" + " return $field_member$.UnsafeArenaRelease(\n" + " $default_variable$, GetArenaNoVirtual());\n" + " } else {\n" + " return NULL;\n" + " }\n" + "}\n" + "inline void $classname$::unsafe_arena_set_allocated_$name$(" + "$string$* $name$) {\n" + " $DCHK$(GetArenaNoVirtual() != NULL);\n" + " if (!has_$name$()) {\n" + " $field_member$.UnsafeSetDefault($default_variable$);\n" + " }\n" + " clear_$oneof_name$();\n" + " if ($name$) {\n" + " set_has_$name$();\n" + " $field_member$.UnsafeArenaSetAllocated($default_variable$, " + "$name$, GetArenaNoVirtual());\n" + " }\n" + " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:" + "$full_name$)\n" + "}\n"); + } } else { // No-arena case. - printer->Print( - variables_, - "inline const ::std::string& $classname$::$name$() const {\n" + format( + "inline const $string$& $classname$::$name$() const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " if (has_$name$()) {\n" " return $field_member$.GetNoArena();\n" " }\n" " return *$default_variable$;\n" "}\n" - "inline void $classname$::set_$name$(const ::std::string& value) {\n" + "inline void $classname$::set_$name$(const $string$& value) {\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" @@ -800,7 +883,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" "#if LANG_CXX11\n" - "inline void $classname$::set_$name$(::std::string&& value) {\n" + "inline void $classname$::set_$name$($string$&& value) {\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" @@ -821,7 +904,32 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " $field_member$.SetNoArena($default_variable$,\n" " $string_piece$(value));\n" " // @@protoc_insertion_point(field_set_char:$full_name$)\n" - "}\n" + "}\n"); + if (!options_.opensource_runtime) { + format( + "inline void $classname$::set_$name$(::StringPiece value) {\n" + " if (!has_$name$()) {\n" + " clear_$oneof_name$();\n" + " set_has_$name$();\n" + " $field_member$.UnsafeSetDefault($default_variable$);\n" + " }\n" + " $field_member$.SetNoArena($default_variable$, value);\n" + " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n" + "}\n" + "#ifdef HAS_GLOBAL_STRING\n" + "inline void $classname$::set_$name$(const ::std::string& value) {\n" + " if (!has_$name$()) {\n" + " clear_$oneof_name$();\n" + " set_has_$name$();\n" + " $field_member$.UnsafeSetDefault($default_variable$);\n" + " }\n" + " $field_member$.SetNoArena($default_variable$,\n" + " ::StringPiece(value.data(), value.size()));\n" + " // @@protoc_insertion_point(field_set_std_string:$full_name$)\n" + "}\n" + "#endif\n"); + } + format( "inline " "void $classname$::set_$name$(const $pointer_type$* value, size_t " "size) {\n" @@ -834,7 +942,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " reinterpret_cast<const char*>(value), size));\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "inline ::std::string* $classname$::mutable_$name$() {\n" + "inline $string$* $classname$::mutable_$name$() {\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -843,7 +951,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $field_member$.MutableNoArena($default_variable$);\n" "}\n" - "inline ::std::string* $classname$::$release_name$() {\n" + "inline $string$* $classname$::$release_name$() {\n" " // @@protoc_insertion_point(field_release:$full_name$)\n" " if (has_$name$()) {\n" " clear_has_$oneof_name$();\n" @@ -852,14 +960,13 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { " return NULL;\n" " }\n" "}\n" - "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n" - " if (!has_$name$()) {\n" - " $field_member$.UnsafeSetDefault($default_variable$);\n" + "inline void $classname$::set_allocated_$name$($string$* $name$) {\n" + " if (has_$oneof_name$()) {\n" + " clear_$oneof_name$();\n" " }\n" - " clear_$oneof_name$();\n" " if ($name$ != NULL) {\n" " set_has_$name$();\n" - " $field_member$.SetAllocatedNoArena($default_variable$, $name$);\n" + " $field_member$.UnsafeSetDefault($name$);\n" " }\n" " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" "}\n"); @@ -868,13 +975,13 @@ GenerateInlineAccessorDefinitions(io::Printer* printer) const { void StringOneofFieldGenerator:: GenerateClearingCode(io::Printer* printer) const { + Formatter format(printer, variables_); if (SupportsArenas(descriptor_)) { - printer->Print(variables_, - "$field_member$.Destroy($default_variable$,\n" - " GetArenaNoVirtual());\n"); + format( + "$field_member$.Destroy($default_variable$,\n" + " GetArenaNoVirtual());\n"); } else { - printer->Print(variables_, - "$field_member$.DestroyNoArena($default_variable$);\n"); + format("$field_member$.DestroyNoArena($default_variable$);\n"); } } @@ -890,31 +997,55 @@ GenerateSwappingCode(io::Printer* printer) const { void StringOneofFieldGenerator:: GenerateConstructorCode(io::Printer* printer) const { - printer->Print( - variables_, + Formatter format(printer, variables_); + format( "$ns$::_$classname$_default_instance_.$name$_.UnsafeSetDefault(\n" " $default_variable$);\n"); } void StringOneofFieldGenerator:: GenerateDestructorCode(io::Printer* printer) const { - printer->Print(variables_, - "if (has_$name$()) {\n" - " $field_member$.DestroyNoArena($default_variable$);\n" - "}\n"); + Formatter format(printer, variables_); + format( + "if (has_$name$()) {\n" + " $field_member$.DestroyNoArena($default_variable$);\n" + "}\n"); } void StringOneofFieldGenerator:: GenerateMergeFromCodedStream(io::Printer* printer) const { - printer->Print(variables_, - "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n" - " input, this->mutable_$name$()));\n"); + Formatter format(printer, variables_); + // See above: ArenaString is not included in the open-source release. + if (!options_.opensource_runtime && SupportsArenas(descriptor_) && !lite_) { + // If has_$name$(), then the current string is either an ArenaString (no + // destructor necessary) or a materialized ::std::string (and is on the Arena's + // destructor list). No call to ArenaStringPtr::Destroy is needed. + format( + "if (arena != NULL) {\n" + " clear_$oneof_name$();\n" + " if (!has_$name$()) {\n" + " $field_member$.UnsafeSetDefault($default_variable$);\n" + " set_has_$name$();\n" + " }\n" + " ::$proto_ns$::internal::TaggedPtr<$string$> new_value =\n" + " ::$proto_ns$::internal::ReadArenaString(input, arena);\n" + " DO_(!new_value.IsNull());\n" + " $field_member$.UnsafeSetTaggedPointer(new_value);\n" + "} else {\n" + " DO_(::$proto_ns$::internal::WireFormatLite::Read$declared_type$(\n" + " input, this->mutable_$name$()));\n" + "}\n"); + } else { + format( + "DO_(::$proto_ns$::internal::WireFormatLite::Read$declared_type$(\n" + " input, this->mutable_$name$()));\n"); + } if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { GenerateUtf8CheckCodeForString( - descriptor_, options_, true, variables_, + descriptor_, options_, true, "this->$name$().data(), static_cast<int>(this->$name$().length()),\n", - printer); + format); } } @@ -923,7 +1054,7 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { RepeatedStringFieldGenerator::RepeatedStringFieldGenerator( const FieldDescriptor* descriptor, const Options& options) - : FieldGenerator(options), descriptor_(descriptor) { + : FieldGenerator(descriptor, options) { SetStringVariables(descriptor, &variables_, options); } @@ -931,182 +1062,210 @@ RepeatedStringFieldGenerator::~RepeatedStringFieldGenerator() {} void RepeatedStringFieldGenerator:: GeneratePrivateMembers(io::Printer* printer) const { - printer->Print(variables_, - "::google::protobuf::RepeatedPtrField< ::std::string> $name$_;\n"); + Formatter format(printer, variables_); + format("::$proto_ns$::RepeatedPtrField<$string$> $name$_;\n"); } void RepeatedStringFieldGenerator:: GenerateAccessorDeclarations(io::Printer* printer) const { + Formatter format(printer, variables_); // See comment above about unknown ctypes. - bool unknown_ctype = - descriptor_->options().ctype() != EffectiveStringCType(descriptor_); + bool unknown_ctype = descriptor_->options().ctype() != + EffectiveStringCType(descriptor_, options_); if (unknown_ctype) { - printer->Outdent(); - printer->Print( - " private:\n" - " // Hidden due to unknown ctype option.\n"); - printer->Indent(); + format.Outdent(); + format( + " private:\n" + " // Hidden due to unknown ctype option.\n"); + format.Indent(); } - printer->Print(variables_, - "$deprecated_attr$const ::std::string& $name$(int index) const;\n"); - printer->Annotate("name", descriptor_); - printer->Print( - variables_, - "$deprecated_attr$::std::string* ${$mutable_$name$$}$(int index);\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, - "$deprecated_attr$void ${$set_$name$$}$(int index, const " - "::std::string& value);\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print( - variables_, + format( + "$deprecated_attr$const $string$& ${1$$name$$}$(int index) const;\n" + "$deprecated_attr$$string$* ${1$mutable_$name$$}$(int index);\n" + "$deprecated_attr$void ${1$set_$name$$}$(int index, const " + "$string$& value);\n" + "#if LANG_CXX11\n" + "$deprecated_attr$void ${1$set_$name$$}$(int index, $string$&& value);\n" + "#endif\n" + "$deprecated_attr$void ${1$set_$name$$}$(int index, const " + "char* value);\n", + descriptor_); + if (!options_.opensource_runtime) { + format( + "$deprecated_attr$void ${1$set_$name$$}$(int index, " + "StringPiece value);\n" + "#ifdef HAS_GLOBAL_STRING\n" + "$deprecated_attr$void ${1$set_$name$$}$(int index, const " + "::std::string& value);\n" + "#endif\n", + descriptor_); + } + format( + "$deprecated_attr$void ${1$set_$name$$}$(" + "int index, const $pointer_type$* value, size_t size);\n" + "$deprecated_attr$$string$* ${1$add_$name$$}$();\n" + "$deprecated_attr$void ${1$add_$name$$}$(const $string$& value);\n" "#if LANG_CXX11\n" - "$deprecated_attr$void ${$set_$name$$}$(int index, ::std::string&& value);\n" - "#endif\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, - "$deprecated_attr$void ${$set_$name$$}$(int index, const " - "char* value);\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, - "" - "$deprecated_attr$void ${$set_$name$$}$(" - "int index, const $pointer_type$* value, size_t size);\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, - "$deprecated_attr$::std::string* ${$add_$name$$}$();\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print( - variables_, - "$deprecated_attr$void ${$add_$name$$}$(const ::std::string& value);\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, - "#if LANG_CXX11\n" - "$deprecated_attr$void ${$add_$name$$}$(::std::string&& value);\n" - "#endif\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print( - variables_, - "$deprecated_attr$void ${$add_$name$$}$(const char* value);\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, - "$deprecated_attr$void ${$add_$name$$}$(const $pointer_type$* " - "value, size_t size)" - ";\n"); - printer->Annotate("{", "}", descriptor_); - printer->Print( - variables_, - "$deprecated_attr$const ::google::protobuf::RepeatedPtrField< ::std::string>& $name$() " - "const;\n"); - printer->Annotate("name", descriptor_); - printer->Print(variables_, - "$deprecated_attr$::google::protobuf::RepeatedPtrField< ::std::string>* " - "${$mutable_$name$$}$()" - ";\n"); - printer->Annotate("{", "}", descriptor_); + "$deprecated_attr$void ${1$add_$name$$}$($string$&& value);\n" + "#endif\n" + "$deprecated_attr$void ${1$add_$name$$}$(const char* value);\n", + descriptor_); + if (!options_.opensource_runtime) { + format( + "$deprecated_attr$void ${1$add_$name$$}$(StringPiece value);\n" + "#ifdef HAS_GLOBAL_STRING\n" + "$deprecated_attr$void ${1$add_$name$$}$(const ::std::string& value);\n" + "#endif\n", + descriptor_); + } + format( + "$deprecated_attr$void ${1$add_$name$$}$(const $pointer_type$* " + "value, size_t size)" + ";\n" + "$deprecated_attr$const ::$proto_ns$::RepeatedPtrField<$string$>& " + "${1$$name$$}$() " + "const;\n" + "$deprecated_attr$::$proto_ns$::RepeatedPtrField<$string$>* " + "${1$mutable_$name$$}$()" + ";\n", + descriptor_); if (unknown_ctype) { - printer->Outdent(); - printer->Print(" public:\n"); - printer->Indent(); + format.Outdent(); + format(" public:\n"); + format.Indent(); } } void RepeatedStringFieldGenerator:: GenerateInlineAccessorDefinitions(io::Printer* printer) const { + Formatter format(printer, variables_); if (options_.safe_boundary_check) { - printer->Print(variables_, - "inline const ::std::string& $classname$::$name$(int index) const {\n" - " // @@protoc_insertion_point(field_get:$full_name$)\n" - " return $name$_.InternalCheckedGet(\n" - " index, ::google::protobuf::internal::GetEmptyStringAlreadyInited());\n" - "}\n"); + format( + "inline const $string$& $classname$::$name$(int index) const {\n" + " // @@protoc_insertion_point(field_get:$full_name$)\n" + " return $name$_.InternalCheckedGet(\n" + " index, ::$proto_ns$::internal::GetEmptyStringAlreadyInited());\n" + "}\n"); } else { - printer->Print(variables_, - "inline const ::std::string& $classname$::$name$(int index) const {\n" - " // @@protoc_insertion_point(field_get:$full_name$)\n" - " return $name$_.Get(index);\n" + format( + "inline const $string$& $classname$::$name$(int index) const {\n" + " // @@protoc_insertion_point(field_get:$full_name$)\n" + " return $name$_.Get(index);\n" + "}\n"); + } + format( + "inline $string$* $classname$::mutable_$name$(int index) {\n" + " // @@protoc_insertion_point(field_mutable:$full_name$)\n" + " return $name$_.Mutable(index);\n" + "}\n" + "inline void $classname$::set_$name$(int index, const $string$& value) " + "{\n" + " // @@protoc_insertion_point(field_set:$full_name$)\n" + " $name$_.Mutable(index)->assign(value);\n" + "}\n" + "#if LANG_CXX11\n" + "inline void $classname$::set_$name$(int index, $string$&& value) {\n" + " // @@protoc_insertion_point(field_set:$full_name$)\n" + " $name$_.Mutable(index)->assign(std::move(value));\n" + "}\n" + "#endif\n" + "inline void $classname$::set_$name$(int index, const char* value) {\n" + " $null_check$" + " $name$_.Mutable(index)->assign(value);\n" + " // @@protoc_insertion_point(field_set_char:$full_name$)\n" "}\n"); + if (!options_.opensource_runtime) { + format( + "inline void " + "$classname$::set_$name$(int index, StringPiece value) {\n" + " $name$_.Mutable(index)->assign(value.data(), value.size());\n" + " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n" + "}\n" + "#ifdef HAS_GLOBAL_STRING\n" + "inline void " + "$classname$::set_$name$(int index, const std::string& value) {\n" + " $name$_.Mutable(index)->assign(value.data(), value.size());\n" + " // @@protoc_insertion_point(field_set_std_string:$full_name$)\n" + "}\n" + "#endif\n"); } - printer->Print(variables_, - "inline ::std::string* $classname$::mutable_$name$(int index) {\n" - " // @@protoc_insertion_point(field_mutable:$full_name$)\n" - " return $name$_.Mutable(index);\n" - "}\n" - "inline void $classname$::set_$name$(int index, const ::std::string& value) {\n" - " // @@protoc_insertion_point(field_set:$full_name$)\n" - " $name$_.Mutable(index)->assign(value);\n" - "}\n" - "#if LANG_CXX11\n" - "inline void $classname$::set_$name$(int index, ::std::string&& value) {\n" - " // @@protoc_insertion_point(field_set:$full_name$)\n" - " $name$_.Mutable(index)->assign(std::move(value));\n" - "}\n" - "#endif\n" - "inline void $classname$::set_$name$(int index, const char* value) {\n" - " $null_check$" - " $name$_.Mutable(index)->assign(value);\n" - " // @@protoc_insertion_point(field_set_char:$full_name$)\n" - "}\n" - "inline void " - "$classname$::set_$name$" - "(int index, const $pointer_type$* value, size_t size) {\n" - " $name$_.Mutable(index)->assign(\n" - " reinterpret_cast<const char*>(value), size);\n" - " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" - "}\n" - "inline ::std::string* $classname$::add_$name$() {\n" - " // @@protoc_insertion_point(field_add_mutable:$full_name$)\n" - " return $name$_.Add();\n" - "}\n" - "inline void $classname$::add_$name$(const ::std::string& value) {\n" - " $name$_.Add()->assign(value);\n" - " // @@protoc_insertion_point(field_add:$full_name$)\n" - "}\n" - "#if LANG_CXX11\n" - "inline void $classname$::add_$name$(::std::string&& value) {\n" - " $name$_.Add(std::move(value));\n" - " // @@protoc_insertion_point(field_add:$full_name$)\n" - "}\n" - "#endif\n" - "inline void $classname$::add_$name$(const char* value) {\n" - " $null_check$" - " $name$_.Add()->assign(value);\n" - " // @@protoc_insertion_point(field_add_char:$full_name$)\n" - "}\n" - "inline void " - "$classname$::add_$name$(const $pointer_type$* value, size_t size) {\n" - " $name$_.Add()->assign(reinterpret_cast<const char*>(value), size);\n" - " // @@protoc_insertion_point(field_add_pointer:$full_name$)\n" - "}\n" - "inline const ::google::protobuf::RepeatedPtrField< ::std::string>&\n" - "$classname$::$name$() const {\n" - " // @@protoc_insertion_point(field_list:$full_name$)\n" - " return $name$_;\n" - "}\n" - "inline ::google::protobuf::RepeatedPtrField< ::std::string>*\n" - "$classname$::mutable_$name$() {\n" - " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" - " return &$name$_;\n" - "}\n"); + format( + "inline void " + "$classname$::set_$name$" + "(int index, const $pointer_type$* value, size_t size) {\n" + " $name$_.Mutable(index)->assign(\n" + " reinterpret_cast<const char*>(value), size);\n" + " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" + "}\n" + "inline $string$* $classname$::add_$name$() {\n" + " // @@protoc_insertion_point(field_add_mutable:$full_name$)\n" + " return $name$_.Add();\n" + "}\n" + "inline void $classname$::add_$name$(const $string$& value) {\n" + " $name$_.Add()->assign(value);\n" + " // @@protoc_insertion_point(field_add:$full_name$)\n" + "}\n" + "#if LANG_CXX11\n" + "inline void $classname$::add_$name$($string$&& value) {\n" + " $name$_.Add(std::move(value));\n" + " // @@protoc_insertion_point(field_add:$full_name$)\n" + "}\n" + "#endif\n" + "inline void $classname$::add_$name$(const char* value) {\n" + " $null_check$" + " $name$_.Add()->assign(value);\n" + " // @@protoc_insertion_point(field_add_char:$full_name$)\n" + "}\n"); + if (!options_.opensource_runtime) { + format( + "inline void $classname$::add_$name$(StringPiece value) {\n" + " $name$_.Add()->assign(value.data(), value.size());\n" + " // @@protoc_insertion_point(field_add_string_piece:$full_name$)\n" + "}\n" + "#ifdef HAS_GLOBAL_STRING\n" + "inline void $classname$::add_$name$(const ::std::string& value) {\n" + " $name$_.Add()->assign(value.data(), value.size());\n" + " // @@protoc_insertion_point(field_add_std_string:$full_name$)\n" + "}\n" + "#endif\n"); + } + format( + "inline void " + "$classname$::add_$name$(const $pointer_type$* value, size_t size) {\n" + " $name$_.Add()->assign(reinterpret_cast<const char*>(value), size);\n" + " // @@protoc_insertion_point(field_add_pointer:$full_name$)\n" + "}\n" + "inline const ::$proto_ns$::RepeatedPtrField<$string$>&\n" + "$classname$::$name$() const {\n" + " // @@protoc_insertion_point(field_list:$full_name$)\n" + " return $name$_;\n" + "}\n" + "inline ::$proto_ns$::RepeatedPtrField<$string$>*\n" + "$classname$::mutable_$name$() {\n" + " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" + " return &$name$_;\n" + "}\n"); } void RepeatedStringFieldGenerator:: GenerateClearingCode(io::Printer* printer) const { - printer->Print(variables_, "$name$_.Clear();\n"); + Formatter format(printer, variables_); + format("$name$_.Clear();\n"); } void RepeatedStringFieldGenerator:: GenerateMergingCode(io::Printer* printer) const { - printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n"); + Formatter format(printer, variables_); + format("$name$_.MergeFrom(from.$name$_);\n"); } void RepeatedStringFieldGenerator:: GenerateSwappingCode(io::Printer* printer) const { - printer->Print(variables_, - "$name$_.InternalSwap(CastToBase(&other->$name$_));\n"); + Formatter format(printer, variables_); + format("$name$_.InternalSwap(CastToBase(&other->$name$_));\n"); } void RepeatedStringFieldGenerator:: @@ -1116,68 +1275,72 @@ GenerateConstructorCode(io::Printer* printer) const { void RepeatedStringFieldGenerator:: GenerateCopyConstructorCode(io::Printer* printer) const { - printer->Print(variables_, "$name$_.CopyFrom(from.$name$_);"); + Formatter format(printer, variables_); + format("$name$_.CopyFrom(from.$name$_);"); } void RepeatedStringFieldGenerator:: GenerateMergeFromCodedStream(io::Printer* printer) const { - printer->Print(variables_, - "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n" - " input, this->add_$name$()));\n"); + Formatter format(printer, variables_); + format( + "DO_(::$proto_ns$::internal::WireFormatLite::Read$declared_type$(\n" + " input, this->add_$name$()));\n"); if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { GenerateUtf8CheckCodeForString( - descriptor_, options_, true, variables_, + descriptor_, options_, true, "this->$name$(this->$name$_size() - 1).data(),\n" "static_cast<int>(this->$name$(this->$name$_size() - 1).length()),\n", - printer); + format); } } void RepeatedStringFieldGenerator:: GenerateSerializeWithCachedSizes(io::Printer* printer) const { - printer->Print(variables_, - "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); - printer->Indent(); + Formatter format(printer, variables_); + format("for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); + format.Indent(); if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { GenerateUtf8CheckCodeForString( - descriptor_, options_, false, variables_, + descriptor_, options_, false, "this->$name$(i).data(), static_cast<int>(this->$name$(i).length()),\n", - printer); + format); } - printer->Outdent(); - printer->Print(variables_, - " ::google::protobuf::internal::WireFormatLite::Write$declared_type$(\n" - " $number$, this->$name$(i), output);\n" - "}\n"); + format.Outdent(); + format( + " ::$proto_ns$::internal::WireFormatLite::Write$declared_type$(\n" + " $number$, this->$name$(i), output);\n" + "}\n"); } void RepeatedStringFieldGenerator:: GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { - printer->Print(variables_, - "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); - printer->Indent(); + Formatter format(printer, variables_); + format("for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); + format.Indent(); if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { GenerateUtf8CheckCodeForString( - descriptor_, options_, false, variables_, + descriptor_, options_, false, "this->$name$(i).data(), static_cast<int>(this->$name$(i).length()),\n", - printer); + format); } - printer->Outdent(); - printer->Print(variables_, - " target = ::google::protobuf::internal::WireFormatLite::\n" - " Write$declared_type$ToArray($number$, this->$name$(i), target);\n" - "}\n"); + format.Outdent(); + format( + " target = ::$proto_ns$::internal::WireFormatLite::\n" + " Write$declared_type$ToArray($number$, this->$name$(i), target);\n" + "}\n"); } void RepeatedStringFieldGenerator:: GenerateByteSize(io::Printer* printer) const { - printer->Print(variables_, - "total_size += $tag_size$ *\n" - " ::google::protobuf::internal::FromIntSize(this->$name$_size());\n" - "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n" - " total_size += ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n" - " this->$name$(i));\n" - "}\n"); + Formatter format(printer, variables_); + format( + "total_size += $tag_size$ *\n" + " ::$proto_ns$::internal::FromIntSize(this->$name$_size());\n" + "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n" + " total_size += " + "::$proto_ns$::internal::WireFormatLite::$declared_type$Size(\n" + " this->$name$(i));\n" + "}\n"); } } // namespace cpp |