Index: third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_string_field.cc |
=================================================================== |
--- third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_string_field.cc (revision 216642) |
+++ third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_string_field.cc (working copy) |
@@ -46,11 +46,14 @@ |
namespace { |
void SetStringVariables(const FieldDescriptor* descriptor, |
- map<string, string>* variables) { |
- SetCommonFieldVariables(descriptor, variables); |
+ map<string, string>* variables, |
+ const Options& options) { |
+ SetCommonFieldVariables(descriptor, variables, options); |
(*variables)["default"] = DefaultValue(descriptor); |
+ (*variables)["default_length"] = |
+ SimpleItoa(descriptor->default_value_string().length()); |
(*variables)["default_variable"] = descriptor->default_value_string().empty() |
- ? "::google::protobuf::internal::kEmptyString" |
+ ? "&::google::protobuf::internal::kEmptyString" |
: "_default_" + FieldName(descriptor) + "_"; |
(*variables)["pointer_type"] = |
descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char"; |
@@ -61,9 +64,10 @@ |
// =================================================================== |
StringFieldGenerator:: |
-StringFieldGenerator(const FieldDescriptor* descriptor) |
+StringFieldGenerator(const FieldDescriptor* descriptor, |
+ const Options& options) |
: descriptor_(descriptor) { |
- SetStringVariables(descriptor, &variables_); |
+ SetStringVariables(descriptor, &variables_, options); |
} |
StringFieldGenerator::~StringFieldGenerator() {} |
@@ -72,7 +76,7 @@ |
GeneratePrivateMembers(io::Printer* printer) const { |
printer->Print(variables_, "::std::string* $name$_;\n"); |
if (!descriptor_->default_value_string().empty()) { |
- printer->Print(variables_, "static const ::std::string $default_variable$;\n"); |
+ printer->Print(variables_, "static ::std::string* $default_variable$;\n"); |
} |
} |
@@ -109,8 +113,10 @@ |
"inline void set_$name$(const $pointer_type$* value, size_t size)" |
"$deprecation$;\n" |
"inline ::std::string* mutable_$name$()$deprecation$;\n" |
- "inline ::std::string* release_$name$()$deprecation$;\n"); |
+ "inline ::std::string* release_$name$()$deprecation$;\n" |
+ "inline void set_allocated_$name$(::std::string* $name$)$deprecation$;\n"); |
+ |
if (descriptor_->options().ctype() != FieldOptions::STRING) { |
printer->Outdent(); |
printer->Print(" public:\n"); |
@@ -126,14 +132,14 @@ |
"}\n" |
"inline void $classname$::set_$name$(const ::std::string& value) {\n" |
" set_has_$name$();\n" |
- " if ($name$_ == &$default_variable$) {\n" |
+ " if ($name$_ == $default_variable$) {\n" |
" $name$_ = new ::std::string;\n" |
" }\n" |
" $name$_->assign(value);\n" |
"}\n" |
"inline void $classname$::set_$name$(const char* value) {\n" |
" set_has_$name$();\n" |
- " if ($name$_ == &$default_variable$) {\n" |
+ " if ($name$_ == $default_variable$) {\n" |
" $name$_ = new ::std::string;\n" |
" }\n" |
" $name$_->assign(value);\n" |
@@ -141,20 +147,20 @@ |
"inline " |
"void $classname$::set_$name$(const $pointer_type$* value, size_t size) {\n" |
" set_has_$name$();\n" |
- " if ($name$_ == &$default_variable$) {\n" |
+ " if ($name$_ == $default_variable$) {\n" |
" $name$_ = new ::std::string;\n" |
" }\n" |
" $name$_->assign(reinterpret_cast<const char*>(value), size);\n" |
"}\n" |
"inline ::std::string* $classname$::mutable_$name$() {\n" |
" set_has_$name$();\n" |
- " if ($name$_ == &$default_variable$) {\n"); |
+ " if ($name$_ == $default_variable$) {\n"); |
if (descriptor_->default_value_string().empty()) { |
printer->Print(variables_, |
" $name$_ = new ::std::string;\n"); |
} else { |
printer->Print(variables_, |
- " $name$_ = new ::std::string($default_variable$);\n"); |
+ " $name$_ = new ::std::string(*$default_variable$);\n"); |
} |
printer->Print(variables_, |
" }\n" |
@@ -162,21 +168,34 @@ |
"}\n" |
"inline ::std::string* $classname$::release_$name$() {\n" |
" clear_has_$name$();\n" |
- " if ($name$_ == &$default_variable$) {\n" |
+ " if ($name$_ == $default_variable$) {\n" |
" return NULL;\n" |
" } else {\n" |
" ::std::string* temp = $name$_;\n" |
- " $name$_ = const_cast< ::std::string*>(&$default_variable$);\n" |
+ " $name$_ = const_cast< ::std::string*>($default_variable$);\n" |
" return temp;\n" |
" }\n" |
+ "}\n" |
+ "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n" |
+ " if ($name$_ != $default_variable$) {\n" |
+ " delete $name$_;\n" |
+ " }\n" |
+ " if ($name$) {\n" |
+ " set_has_$name$();\n" |
+ " $name$_ = $name$;\n" |
+ " } else {\n" |
+ " clear_has_$name$();\n" |
+ " $name$_ = const_cast< ::std::string*>($default_variable$);\n" |
+ " }\n" |
"}\n"); |
} |
void StringFieldGenerator:: |
GenerateNonInlineAccessorDefinitions(io::Printer* printer) const { |
if (!descriptor_->default_value_string().empty()) { |
+ // Initialized in GenerateDefaultInstanceAllocator. |
printer->Print(variables_, |
- "const ::std::string $classname$::$default_variable$($default$);\n"); |
+ "::std::string* $classname$::$default_variable$ = NULL;\n"); |
} |
} |
@@ -184,13 +203,13 @@ |
GenerateClearingCode(io::Printer* printer) const { |
if (descriptor_->default_value_string().empty()) { |
printer->Print(variables_, |
- "if ($name$_ != &$default_variable$) {\n" |
+ "if ($name$_ != $default_variable$) {\n" |
" $name$_->clear();\n" |
"}\n"); |
} else { |
printer->Print(variables_, |
- "if ($name$_ != &$default_variable$) {\n" |
- " $name$_->assign($default_variable$);\n" |
+ "if ($name$_ != $default_variable$) {\n" |
+ " $name$_->assign(*$default_variable$);\n" |
"}\n"); |
} |
} |
@@ -208,18 +227,35 @@ |
void StringFieldGenerator:: |
GenerateConstructorCode(io::Printer* printer) const { |
printer->Print(variables_, |
- "$name$_ = const_cast< ::std::string*>(&$default_variable$);\n"); |
+ "$name$_ = const_cast< ::std::string*>($default_variable$);\n"); |
} |
void StringFieldGenerator:: |
GenerateDestructorCode(io::Printer* printer) const { |
printer->Print(variables_, |
- "if ($name$_ != &$default_variable$) {\n" |
+ "if ($name$_ != $default_variable$) {\n" |
" delete $name$_;\n" |
"}\n"); |
} |
void StringFieldGenerator:: |
+GenerateDefaultInstanceAllocator(io::Printer* printer) const { |
+ if (!descriptor_->default_value_string().empty()) { |
+ printer->Print(variables_, |
+ "$classname$::$default_variable$ =\n" |
+ " new ::std::string($default$, $default_length$);\n"); |
+ } |
+} |
+ |
+void StringFieldGenerator:: |
+GenerateShutdownCode(io::Printer* printer) const { |
+ if (!descriptor_->default_value_string().empty()) { |
+ printer->Print(variables_, |
+ "delete $classname$::$default_variable$;\n"); |
+ } |
+} |
+ |
+void StringFieldGenerator:: |
GenerateMergeFromCodedStream(io::Printer* printer) const { |
printer->Print(variables_, |
"DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n" |
@@ -273,9 +309,10 @@ |
// =================================================================== |
RepeatedStringFieldGenerator:: |
-RepeatedStringFieldGenerator(const FieldDescriptor* descriptor) |
+RepeatedStringFieldGenerator(const FieldDescriptor* descriptor, |
+ const Options& options) |
: descriptor_(descriptor) { |
- SetStringVariables(descriptor, &variables_); |
+ SetStringVariables(descriptor, &variables_, options); |
} |
RepeatedStringFieldGenerator::~RepeatedStringFieldGenerator() {} |
@@ -328,7 +365,7 @@ |
GenerateInlineAccessorDefinitions(io::Printer* printer) const { |
printer->Print(variables_, |
"inline const ::std::string& $classname$::$name$(int index) const {\n" |
- " return $name$_.Get(index);\n" |
+ " return $name$_.$cppget$(index);\n" |
"}\n" |
"inline ::std::string* $classname$::mutable_$name$(int index) {\n" |
" return $name$_.Mutable(index);\n" |