Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(481)

Side by Side Diff: third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_string_field.cc

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update defines Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above 11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer 12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the 13 // in the documentation and/or other materials provided with the
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 45
46 namespace { 46 namespace {
47 47
48 void SetStringVariables(const FieldDescriptor* descriptor, 48 void SetStringVariables(const FieldDescriptor* descriptor,
49 map<string, string>* variables, 49 map<string, string>* variables,
50 const Options& options) { 50 const Options& options) {
51 SetCommonFieldVariables(descriptor, variables, options); 51 SetCommonFieldVariables(descriptor, variables, options);
52 (*variables)["default"] = DefaultValue(descriptor); 52 (*variables)["default"] = DefaultValue(descriptor);
53 (*variables)["default_length"] = 53 (*variables)["default_length"] =
54 SimpleItoa(descriptor->default_value_string().length()); 54 SimpleItoa(descriptor->default_value_string().length());
55 (*variables)["default_variable"] = descriptor->default_value_string().empty() 55 string default_variable_string =
56 ? "&::google::protobuf::internal::GetEmptyString()" 56 descriptor->default_value_string().empty()
57 : "_default_" + FieldName(descriptor) + "_"; 57 ? "&::google::protobuf::internal::GetEmptyStringAlreadyInited()"
58 : "_default_" + FieldName(descriptor) + "_";
59 (*variables)["default_variable"] = default_variable_string;
60 (*variables)["default_value_init"] =
61 descriptor->default_value_string().empty()
62 ? "" : "*" + default_variable_string;
58 (*variables)["pointer_type"] = 63 (*variables)["pointer_type"] =
59 descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char"; 64 descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char";
65 // NOTE: Escaped here to unblock proto1->proto2 migration.
66 // TODO(liujisi): Extend this to apply for other conflicting methods.
67 (*variables)["release_name"] =
68 SafeFunctionName(descriptor->containing_type(),
69 descriptor, "release_");
70 (*variables)["full_name"] = descriptor->full_name();
71
72 (*variables)["string_piece"] = "::std::string";
60 } 73 }
61 74
62 } // namespace 75 } // namespace
63 76
64 // =================================================================== 77 // ===================================================================
65 78
66 StringFieldGenerator:: 79 StringFieldGenerator::
67 StringFieldGenerator(const FieldDescriptor* descriptor, 80 StringFieldGenerator(const FieldDescriptor* descriptor,
68 const Options& options) 81 const Options& options)
69 : descriptor_(descriptor) { 82 : descriptor_(descriptor) {
70 SetStringVariables(descriptor, &variables_, options); 83 SetStringVariables(descriptor, &variables_, options);
71 } 84 }
72 85
73 StringFieldGenerator::~StringFieldGenerator() {} 86 StringFieldGenerator::~StringFieldGenerator() {}
74 87
75 void StringFieldGenerator:: 88 void StringFieldGenerator::
76 GeneratePrivateMembers(io::Printer* printer) const { 89 GeneratePrivateMembers(io::Printer* printer) const {
77 printer->Print(variables_, "::std::string* $name$_;\n"); 90 // N.B. that we continue to use |ArenaStringPtr| instead of |string*| for
91 // string fields, even when SupportArenas(descriptor_) == false. Why?
92 // The simple answer is to avoid unmaintainable complexity. The reflection
93 // code assumes ArenaStringPtrs. These are *almost* in-memory-compatible with
94 // string*, except for the pointer tags and related ownership semantics. We
95 // could modify the runtime code to use string* for the not-supporting-arenas
96 // case, but this would require a way to detect which type of class was
97 // generated (adding overhead and complexity to GeneratedMessageReflection)
98 // and littering the runtime code paths with conditionals. It's simpler to
99 // stick with this but use lightweight accessors that assume arena == NULL.
100 // There should be very little overhead anyway because it's just a tagged
101 // pointer in-memory.
102 printer->Print(variables_, "::google::protobuf::internal::ArenaStringPtr $name $_;\n");
103 }
104
105 void StringFieldGenerator::
106 GenerateStaticMembers(io::Printer* printer) const {
78 if (!descriptor_->default_value_string().empty()) { 107 if (!descriptor_->default_value_string().empty()) {
79 printer->Print(variables_, "static ::std::string* $default_variable$;\n"); 108 printer->Print(variables_, "static ::std::string* $default_variable$;\n");
80 } 109 }
81 } 110 }
82 111
83 void StringFieldGenerator:: 112 void StringFieldGenerator::
84 GenerateAccessorDeclarations(io::Printer* printer) const { 113 GenerateAccessorDeclarations(io::Printer* printer) const {
85 // If we're using StringFieldGenerator for a field with a ctype, it's 114 // If we're using StringFieldGenerator for a field with a ctype, it's
86 // because that ctype isn't actually implemented. In particular, this is 115 // because that ctype isn't actually implemented. In particular, this is
87 // true of ctype=CORD and ctype=STRING_PIECE in the open source release. 116 // true of ctype=CORD and ctype=STRING_PIECE in the open source release.
88 // We aren't releasing Cord because it has too many Google-specific 117 // We aren't releasing Cord because it has too many Google-specific
89 // dependencies and we aren't releasing StringPiece because it's hardly 118 // dependencies and we aren't releasing StringPiece because it's hardly
90 // useful outside of Google and because it would get confusing to have 119 // useful outside of Google and because it would get confusing to have
91 // multiple instances of the StringPiece class in different libraries (PCRE 120 // multiple instances of the StringPiece class in different libraries (PCRE
92 // already includes it for their C++ bindings, which came from Google). 121 // already includes it for their C++ bindings, which came from Google).
93 // 122 //
94 // In any case, we make all the accessors private while still actually 123 // In any case, we make all the accessors private while still actually
95 // using a string to represent the field internally. This way, we can 124 // using a string to represent the field internally. This way, we can
96 // guarantee that if we do ever implement the ctype, it won't break any 125 // guarantee that if we do ever implement the ctype, it won't break any
97 // existing users who might be -- for whatever reason -- already using .proto 126 // existing users who might be -- for whatever reason -- already using .proto
98 // files that applied the ctype. The field can still be accessed via the 127 // files that applied the ctype. The field can still be accessed via the
99 // reflection interface since the reflection interface is independent of 128 // reflection interface since the reflection interface is independent of
100 // the string's underlying representation. 129 // the string's underlying representation.
101 if (descriptor_->options().ctype() != FieldOptions::STRING) { 130
131 bool unknown_ctype =
132 descriptor_->options().ctype() != EffectiveStringCType(descriptor_);
133
134 if (unknown_ctype) {
102 printer->Outdent(); 135 printer->Outdent();
103 printer->Print( 136 printer->Print(
104 " private:\n" 137 " private:\n"
105 " // Hidden due to unknown ctype option.\n"); 138 " // Hidden due to unknown ctype option.\n");
106 printer->Indent(); 139 printer->Indent();
107 } 140 }
108 141
109 printer->Print(variables_, 142 printer->Print(variables_,
110 "inline const ::std::string& $name$() const$deprecation$;\n" 143 "const ::std::string& $name$() const$deprecation$;\n"
111 "inline void set_$name$(const ::std::string& value)$deprecation$;\n" 144 "void set_$name$(const ::std::string& value)$deprecation$;\n"
112 "inline void set_$name$(const char* value)$deprecation$;\n" 145 "void set_$name$(const char* value)$deprecation$;\n"
113 "inline void set_$name$(const $pointer_type$* value, size_t size)" 146 "void set_$name$(const $pointer_type$* value, size_t size)"
114 "$deprecation$;\n" 147 "$deprecation$;\n"
115 "inline ::std::string* mutable_$name$()$deprecation$;\n" 148 "::std::string* mutable_$name$()$deprecation$;\n"
116 "inline ::std::string* release_$name$()$deprecation$;\n" 149 "::std::string* $release_name$()$deprecation$;\n"
117 "inline void set_allocated_$name$(::std::string* $name$)$deprecation$;\n"); 150 "void set_allocated_$name$(::std::string* $name$)$deprecation$;\n");
151 if (SupportsArenas(descriptor_)) {
152 printer->Print(variables_,
153 "::std::string* unsafe_arena_release_$name$()$deprecation$;\n"
154 "void unsafe_arena_set_allocated_$name$(\n"
155 " ::std::string* $name$)$deprecation$;\n");
156 }
118 157
119 158
120 if (descriptor_->options().ctype() != FieldOptions::STRING) { 159 if (unknown_ctype) {
121 printer->Outdent(); 160 printer->Outdent();
122 printer->Print(" public:\n"); 161 printer->Print(" public:\n");
123 printer->Indent(); 162 printer->Indent();
124 } 163 }
125 } 164 }
126 165
127 void StringFieldGenerator:: 166 void StringFieldGenerator::
128 GenerateInlineAccessorDefinitions(io::Printer* printer) const { 167 GenerateInlineAccessorDefinitions(io::Printer* printer,
129 printer->Print(variables_, 168 bool is_inline) const {
130 "inline const ::std::string& $classname$::$name$() const {\n" 169 map<string, string> variables(variables_);
131 " return *$name$_;\n" 170 variables["inline"] = is_inline ? "inline" : "";
132 "}\n" 171 if (SupportsArenas(descriptor_)) {
133 "inline void $classname$::set_$name$(const ::std::string& value) {\n" 172 printer->Print(variables,
134 " set_has_$name$();\n" 173 "$inline$ const ::std::string& $classname$::$name$() const {\n"
135 " if ($name$_ == $default_variable$) {\n" 174 " // @@protoc_insertion_point(field_get:$full_name$)\n"
136 " $name$_ = new ::std::string;\n" 175 " return $name$_.Get($default_variable$);\n"
137 " }\n" 176 "}\n"
138 " $name$_->assign(value);\n" 177 "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n"
139 "}\n" 178 " $set_hasbit$\n"
140 "inline void $classname$::set_$name$(const char* value) {\n" 179 " $name$_.Set($default_variable$, value, GetArenaNoVirtual());\n"
141 " set_has_$name$();\n" 180 " // @@protoc_insertion_point(field_set:$full_name$)\n"
142 " if ($name$_ == $default_variable$) {\n" 181 "}\n"
143 " $name$_ = new ::std::string;\n" 182 "$inline$ void $classname$::set_$name$(const char* value) {\n"
144 " }\n" 183 " $set_hasbit$\n"
145 " $name$_->assign(value);\n" 184 " $name$_.Set($default_variable$, $string_piece$(value),\n"
146 "}\n" 185 " GetArenaNoVirtual());\n"
147 "inline " 186 " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
148 "void $classname$::set_$name$(const $pointer_type$* value, size_t size) {\n" 187 "}\n"
149 " set_has_$name$();\n" 188 "$inline$ "
150 " if ($name$_ == $default_variable$) {\n" 189 "void $classname$::set_$name$(const $pointer_type$* value,\n"
151 " $name$_ = new ::std::string;\n" 190 " size_t size) {\n"
152 " }\n" 191 " $set_hasbit$\n"
153 " $name$_->assign(reinterpret_cast<const char*>(value), size);\n" 192 " $name$_.Set($default_variable$, $string_piece$(\n"
154 "}\n" 193 " reinterpret_cast<const char*>(value), size), GetArenaNoVirtual());\ n"
155 "inline ::std::string* $classname$::mutable_$name$() {\n" 194 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
156 " set_has_$name$();\n" 195 "}\n"
157 " if ($name$_ == $default_variable$) {\n"); 196 "$inline$ ::std::string* $classname$::mutable_$name$() {\n"
158 if (descriptor_->default_value_string().empty()) { 197 " $set_hasbit$\n"
159 printer->Print(variables_, 198 " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
160 " $name$_ = new ::std::string;\n"); 199 " return $name$_.Mutable($default_variable$, GetArenaNoVirtual());\n"
200 "}\n"
201 "$inline$ ::std::string* $classname$::$release_name$() {\n"
202 " $clear_hasbit$\n"
203 " return $name$_.Release($default_variable$, GetArenaNoVirtual());\n"
204 "}\n"
205 "$inline$ ::std::string* $classname$::unsafe_arena_release_$name$() {\n"
206 " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n"
207 " $clear_hasbit$\n"
208 " return $name$_.UnsafeArenaRelease($default_variable$,\n"
209 " GetArenaNoVirtual());\n"
210 "}\n"
211 "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\ n"
212 " if ($name$ != NULL) {\n"
213 " $set_hasbit$\n"
214 " } else {\n"
215 " $clear_hasbit$\n"
216 " }\n"
217 " $name$_.SetAllocated($default_variable$, $name$,\n"
218 " GetArenaNoVirtual());\n"
219 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
220 "}\n"
221 "$inline$ void $classname$::unsafe_arena_set_allocated_$name$(\n"
222 " ::std::string* $name$) {\n"
223 " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n"
224 " if ($name$ != NULL) {\n"
225 " $set_hasbit$\n"
226 " } else {\n"
227 " $clear_hasbit$\n"
228 " }\n"
229 " $name$_.UnsafeArenaSetAllocated($default_variable$,\n"
230 " $name$, GetArenaNoVirtual());\n"
231 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
232 "}\n");
161 } else { 233 } else {
162 printer->Print(variables_, 234 // No-arena case.
163 " $name$_ = new ::std::string(*$default_variable$);\n"); 235 printer->Print(variables,
236 "$inline$ const ::std::string& $classname$::$name$() const {\n"
237 " // @@protoc_insertion_point(field_get:$full_name$)\n"
238 " return $name$_.GetNoArena($default_variable$);\n"
239 "}\n"
240 "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n"
241 " $set_hasbit$\n"
242 " $name$_.SetNoArena($default_variable$, value);\n"
243 " // @@protoc_insertion_point(field_set:$full_name$)\n"
244 "}\n"
245 "$inline$ void $classname$::set_$name$(const char* value) {\n"
246 " $set_hasbit$\n"
247 " $name$_.SetNoArena($default_variable$, $string_piece$(value));\n"
248 " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
249 "}\n"
250 "$inline$ "
251 "void $classname$::set_$name$(const $pointer_type$* value, "
252 "size_t size) {\n"
253 " $set_hasbit$\n"
254 " $name$_.SetNoArena($default_variable$,\n"
255 " $string_piece$(reinterpret_cast<const char*>(value), size));\n"
256 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
257 "}\n"
258 "$inline$ ::std::string* $classname$::mutable_$name$() {\n"
259 " $set_hasbit$\n"
260 " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
261 " return $name$_.MutableNoArena($default_variable$);\n"
262 "}\n"
263 "$inline$ ::std::string* $classname$::$release_name$() {\n"
264 " $clear_hasbit$\n"
265 " return $name$_.ReleaseNoArena($default_variable$);\n"
266 "}\n"
267 "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\ n"
268 " if ($name$ != NULL) {\n"
269 " $set_hasbit$\n"
270 " } else {\n"
271 " $clear_hasbit$\n"
272 " }\n"
273 " $name$_.SetAllocatedNoArena($default_variable$, $name$);\n"
274 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
275 "}\n");
164 } 276 }
165 printer->Print(variables_,
166 " }\n"
167 " return $name$_;\n"
168 "}\n"
169 "inline ::std::string* $classname$::release_$name$() {\n"
170 " clear_has_$name$();\n"
171 " if ($name$_ == $default_variable$) {\n"
172 " return NULL;\n"
173 " } else {\n"
174 " ::std::string* temp = $name$_;\n"
175 " $name$_ = const_cast< ::std::string*>($default_variable$);\n"
176 " return temp;\n"
177 " }\n"
178 "}\n"
179 "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
180 " if ($name$_ != $default_variable$) {\n"
181 " delete $name$_;\n"
182 " }\n"
183 " if ($name$) {\n"
184 " set_has_$name$();\n"
185 " $name$_ = $name$;\n"
186 " } else {\n"
187 " clear_has_$name$();\n"
188 " $name$_ = const_cast< ::std::string*>($default_variable$);\n"
189 " }\n"
190 "}\n");
191 } 277 }
192 278
193 void StringFieldGenerator:: 279 void StringFieldGenerator::
194 GenerateNonInlineAccessorDefinitions(io::Printer* printer) const { 280 GenerateNonInlineAccessorDefinitions(io::Printer* printer) const {
195 if (!descriptor_->default_value_string().empty()) { 281 if (!descriptor_->default_value_string().empty()) {
196 // Initialized in GenerateDefaultInstanceAllocator. 282 // Initialized in GenerateDefaultInstanceAllocator.
197 printer->Print(variables_, 283 printer->Print(variables_,
198 "::std::string* $classname$::$default_variable$ = NULL;\n"); 284 "::std::string* $classname$::$default_variable$ = NULL;\n");
199 } 285 }
200 } 286 }
201 287
202 void StringFieldGenerator:: 288 void StringFieldGenerator::
203 GenerateClearingCode(io::Printer* printer) const { 289 GenerateClearingCode(io::Printer* printer) const {
204 if (descriptor_->default_value_string().empty()) { 290 // Two-dimension specialization here: supporting arenas or not, and default
205 printer->Print(variables_, 291 // value is the empty string or not. Complexity here ensures the minimal
206 "if ($name$_ != $default_variable$) {\n" 292 // number of branches / amount of extraneous code at runtime (given that the
207 " $name$_->clear();\n" 293 // below methods are inlined one-liners)!
208 "}\n"); 294 if (SupportsArenas(descriptor_)) {
295 if (descriptor_->default_value_string().empty()) {
296 printer->Print(variables_,
297 "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n");
298 } else {
299 printer->Print(variables_,
300 "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n");
301 }
209 } else { 302 } else {
210 printer->Print(variables_, 303 if (descriptor_->default_value_string().empty()) {
211 "if ($name$_ != $default_variable$) {\n" 304 printer->Print(variables_,
212 " $name$_->assign(*$default_variable$);\n" 305 "$name$_.ClearToEmptyNoArena($default_variable$);\n");
213 "}\n"); 306 } else {
307 printer->Print(variables_,
308 "$name$_.ClearToDefaultNoArena($default_variable$);\n");
309 }
214 } 310 }
215 } 311 }
216 312
217 void StringFieldGenerator:: 313 void StringFieldGenerator::
218 GenerateMergingCode(io::Printer* printer) const { 314 GenerateMergingCode(io::Printer* printer) const {
219 printer->Print(variables_, "set_$name$(from.$name$());\n"); 315 if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) {
316 // TODO(gpike): improve this
317 printer->Print(variables_, "set_$name$(from.$name$());\n");
318 } else {
319 printer->Print(variables_,
320 "$set_hasbit$\n"
321 "$name$_.AssignWithDefault($default_variable$, from.$name$_);\n");
322 }
220 } 323 }
221 324
222 void StringFieldGenerator:: 325 void StringFieldGenerator::
223 GenerateSwappingCode(io::Printer* printer) const { 326 GenerateSwappingCode(io::Printer* printer) const {
224 printer->Print(variables_, "std::swap($name$_, other->$name$_);\n"); 327 printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n");
225 } 328 }
226 329
227 void StringFieldGenerator:: 330 void StringFieldGenerator::
228 GenerateConstructorCode(io::Printer* printer) const { 331 GenerateConstructorCode(io::Printer* printer) const {
229 printer->Print(variables_, 332 printer->Print(variables_,
230 "$name$_ = const_cast< ::std::string*>($default_variable$);\n"); 333 "$name$_.UnsafeSetDefault($default_variable$);\n");
231 } 334 }
232 335
233 void StringFieldGenerator:: 336 void StringFieldGenerator::
234 GenerateDestructorCode(io::Printer* printer) const { 337 GenerateDestructorCode(io::Printer* printer) const {
235 printer->Print(variables_, 338 if (SupportsArenas(descriptor_)) {
236 "if ($name$_ != $default_variable$) {\n" 339 printer->Print(variables_,
237 " delete $name$_;\n" 340 "$name$_.Destroy($default_variable$, GetArenaNoVirtual());\n");
238 "}\n"); 341 } else {
342 printer->Print(variables_,
343 "$name$_.DestroyNoArena($default_variable$);\n");
344 }
239 } 345 }
240 346
241 void StringFieldGenerator:: 347 void StringFieldGenerator::
242 GenerateDefaultInstanceAllocator(io::Printer* printer) const { 348 GenerateDefaultInstanceAllocator(io::Printer* printer) const {
243 if (!descriptor_->default_value_string().empty()) { 349 if (!descriptor_->default_value_string().empty()) {
244 printer->Print(variables_, 350 printer->Print(variables_,
245 "$classname$::$default_variable$ =\n" 351 "$classname$::$default_variable$ =\n"
246 " new ::std::string($default$, $default_length$);\n"); 352 " new ::std::string($default$, $default_length$);\n");
247 } 353 }
248 } 354 }
249 355
250 void StringFieldGenerator:: 356 void StringFieldGenerator::
251 GenerateShutdownCode(io::Printer* printer) const { 357 GenerateShutdownCode(io::Printer* printer) const {
252 if (!descriptor_->default_value_string().empty()) { 358 if (!descriptor_->default_value_string().empty()) {
253 printer->Print(variables_, 359 printer->Print(variables_,
254 "delete $classname$::$default_variable$;\n"); 360 "delete $classname$::$default_variable$;\n");
255 } 361 }
256 } 362 }
257 363
258 void StringFieldGenerator:: 364 void StringFieldGenerator::
259 GenerateMergeFromCodedStream(io::Printer* printer) const { 365 GenerateMergeFromCodedStream(io::Printer* printer) const {
260 printer->Print(variables_, 366 printer->Print(variables_,
261 "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n" 367 "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n"
262 " input, this->mutable_$name$()));\n"); 368 " input, this->mutable_$name$()));\n");
263 if (HasUtf8Verification(descriptor_->file()) && 369
264 descriptor_->type() == FieldDescriptor::TYPE_STRING) { 370 if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
265 printer->Print(variables_, 371 GenerateUtf8CheckCodeForString(
266 "::google::protobuf::internal::WireFormat::VerifyUTF8String(\n" 372 descriptor_, true, variables_,
267 " this->$name$().data(), this->$name$().length(),\n" 373 "this->$name$().data(), this->$name$().length(),\n", printer);
268 " ::google::protobuf::internal::WireFormat::PARSE);\n");
269 } 374 }
270 } 375 }
271 376
272 void StringFieldGenerator:: 377 void StringFieldGenerator::
273 GenerateSerializeWithCachedSizes(io::Printer* printer) const { 378 GenerateSerializeWithCachedSizes(io::Printer* printer) const {
274 if (HasUtf8Verification(descriptor_->file()) && 379 if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
275 descriptor_->type() == FieldDescriptor::TYPE_STRING) { 380 GenerateUtf8CheckCodeForString(
276 printer->Print(variables_, 381 descriptor_, false, variables_,
277 "::google::protobuf::internal::WireFormat::VerifyUTF8String(\n" 382 "this->$name$().data(), this->$name$().length(),\n", printer);
278 " this->$name$().data(), this->$name$().length(),\n"
279 " ::google::protobuf::internal::WireFormat::SERIALIZE);\n");
280 } 383 }
281 printer->Print(variables_, 384 printer->Print(variables_,
282 "::google::protobuf::internal::WireFormatLite::Write$declared_type$(\n" 385 "::google::protobuf::internal::WireFormatLite::Write$declared_type$MaybeAlia sed(\n"
283 " $number$, this->$name$(), output);\n"); 386 " $number$, this->$name$(), output);\n");
284 } 387 }
285 388
286 void StringFieldGenerator:: 389 void StringFieldGenerator::
287 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { 390 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
288 if (HasUtf8Verification(descriptor_->file()) && 391 if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
289 descriptor_->type() == FieldDescriptor::TYPE_STRING) { 392 GenerateUtf8CheckCodeForString(
290 printer->Print(variables_, 393 descriptor_, false, variables_,
291 "::google::protobuf::internal::WireFormat::VerifyUTF8String(\n" 394 "this->$name$().data(), this->$name$().length(),\n", printer);
292 " this->$name$().data(), this->$name$().length(),\n"
293 " ::google::protobuf::internal::WireFormat::SERIALIZE);\n");
294 } 395 }
295 printer->Print(variables_, 396 printer->Print(variables_,
296 "target =\n" 397 "target =\n"
297 " ::google::protobuf::internal::WireFormatLite::Write$declared_type$ToArray (\n" 398 " ::google::protobuf::internal::WireFormatLite::Write$declared_type$ToArray (\n"
298 " $number$, this->$name$(), target);\n"); 399 " $number$, this->$name$(), target);\n");
299 } 400 }
300 401
301 void StringFieldGenerator:: 402 void StringFieldGenerator::
302 GenerateByteSize(io::Printer* printer) const { 403 GenerateByteSize(io::Printer* printer) const {
303 printer->Print(variables_, 404 printer->Print(variables_,
304 "total_size += $tag_size$ +\n" 405 "total_size += $tag_size$ +\n"
305 " ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n" 406 " ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n"
306 " this->$name$());\n"); 407 " this->$name$());\n");
307 } 408 }
308 409
309 // =================================================================== 410 // ===================================================================
310 411
412 StringOneofFieldGenerator::
413 StringOneofFieldGenerator(const FieldDescriptor* descriptor,
414 const Options& options)
415 : StringFieldGenerator(descriptor, options),
416 dependent_field_(options.proto_h) {
417 SetCommonOneofFieldVariables(descriptor, &variables_);
418 }
419
420 StringOneofFieldGenerator::~StringOneofFieldGenerator() {}
421
422 void StringOneofFieldGenerator::
423 GenerateInlineAccessorDefinitions(io::Printer* printer,
424 bool is_inline) const {
425 map<string, string> variables(variables_);
426 variables["inline"] = is_inline ? "inline" : "";
427 if (SupportsArenas(descriptor_)) {
428 printer->Print(variables,
429 "$inline$ const ::std::string& $classname$::$name$() const {\n"
430 " // @@protoc_insertion_point(field_get:$full_name$)\n"
431 " if (has_$name$()) {\n"
432 " return $oneof_prefix$$name$_.Get($default_variable$);\n"
433 " }\n"
434 " return *$default_variable$;\n"
435 "}\n"
436 "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n"
437 " if (!has_$name$()) {\n"
438 " clear_$oneof_name$();\n"
439 " set_has_$name$();\n"
440 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
441 " }\n"
442 " $oneof_prefix$$name$_.Set($default_variable$, value,\n"
443 " GetArenaNoVirtual());\n"
444 " // @@protoc_insertion_point(field_set:$full_name$)\n"
445 "}\n"
446 "$inline$ void $classname$::set_$name$(const char* value) {\n"
447 " if (!has_$name$()) {\n"
448 " clear_$oneof_name$();\n"
449 " set_has_$name$();\n"
450 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
451 " }\n"
452 " $oneof_prefix$$name$_.Set($default_variable$,\n"
453 " $string_piece$(value), GetArenaNoVirtual());\n"
454 " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
455 "}\n"
456 "$inline$ "
457 "void $classname$::set_$name$(const $pointer_type$* value,\n"
458 " size_t size) {\n"
459 " if (!has_$name$()) {\n"
460 " clear_$oneof_name$();\n"
461 " set_has_$name$();\n"
462 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
463 " }\n"
464 " $oneof_prefix$$name$_.Set($default_variable$, $string_piece$(\n"
465 " reinterpret_cast<const char*>(value), size),\n"
466 " GetArenaNoVirtual());\n"
467 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
468 "}\n"
469 "$inline$ ::std::string* $classname$::mutable_$name$() {\n"
470 " if (!has_$name$()) {\n"
471 " clear_$oneof_name$();\n"
472 " set_has_$name$();\n"
473 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
474 " }\n"
475 " return $oneof_prefix$$name$_.Mutable($default_variable$,\n"
476 " GetArenaNoVirtual());\n"
477 " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
478 "}\n"
479 "$inline$ ::std::string* $classname$::$release_name$() {\n"
480 " if (has_$name$()) {\n"
481 " clear_has_$oneof_name$();\n"
482 " return $oneof_prefix$$name$_.Release($default_variable$,\n"
483 " GetArenaNoVirtual());\n"
484 " } else {\n"
485 " return NULL;\n"
486 " }\n"
487 "}\n"
488 "$inline$ ::std::string* $classname$::unsafe_arena_release_$name$() {\n"
489 " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n"
490 " if (has_$name$()) {\n"
491 " clear_has_$oneof_name$();\n"
492 " return $oneof_prefix$$name$_.UnsafeArenaRelease(\n"
493 " $default_variable$, GetArenaNoVirtual());\n"
494 " } else {\n"
495 " return NULL;\n"
496 " }\n"
497 "}\n"
498 "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\ n"
499 " if (!has_$name$()) {\n"
500 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
501 " }\n"
502 " clear_$oneof_name$();\n"
503 " if ($name$ != NULL) {\n"
504 " set_has_$name$();\n"
505 " $oneof_prefix$$name$_.SetAllocated($default_variable$, $name$,\n"
506 " GetArenaNoVirtual());\n"
507 " }\n"
508 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
509 "}\n"
510 "$inline$ void $classname$::unsafe_arena_set_allocated_$name$("
511 "::std::string* $name$) {\n"
512 " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n"
513 " if (!has_$name$()) {\n"
514 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
515 " }\n"
516 " clear_$oneof_name$();\n"
517 " if ($name$) {\n"
518 " set_has_$name$();\n"
519 " $oneof_prefix$$name$_.UnsafeArenaSetAllocated($default_variable$, "
520 "$name$, GetArenaNoVirtual());\n"
521 " }\n"
522 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
523 "}\n");
524 } else {
525 // No-arena case.
526 printer->Print(variables,
527 "$inline$ const ::std::string& $classname$::$name$() const {\n"
528 " // @@protoc_insertion_point(field_get:$full_name$)\n"
529 " if (has_$name$()) {\n"
530 " return $oneof_prefix$$name$_.GetNoArena($default_variable$);\n"
531 " }\n"
532 " return *$default_variable$;\n"
533 "}\n"
534 "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n"
535 " // @@protoc_insertion_point(field_set:$full_name$)\n"
536 " if (!has_$name$()) {\n"
537 " clear_$oneof_name$();\n"
538 " set_has_$name$();\n"
539 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
540 " }\n"
541 " $oneof_prefix$$name$_.SetNoArena($default_variable$, value);\n"
542 " // @@protoc_insertion_point(field_set:$full_name$)\n"
543 "}\n"
544 "$inline$ void $classname$::set_$name$(const char* value) {\n"
545 " if (!has_$name$()) {\n"
546 " clear_$oneof_name$();\n"
547 " set_has_$name$();\n"
548 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
549 " }\n"
550 " $oneof_prefix$$name$_.SetNoArena($default_variable$,\n"
551 " $string_piece$(value));\n"
552 " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
553 "}\n"
554 "$inline$ "
555 "void $classname$::set_$name$(const $pointer_type$* value, size_t size) {\ n"
556 " if (!has_$name$()) {\n"
557 " clear_$oneof_name$();\n"
558 " set_has_$name$();\n"
559 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
560 " }\n"
561 " $oneof_prefix$$name$_.SetNoArena($default_variable$, $string_piece$(\n"
562 " reinterpret_cast<const char*>(value), size));\n"
563 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
564 "}\n"
565 "$inline$ ::std::string* $classname$::mutable_$name$() {\n"
566 " if (!has_$name$()) {\n"
567 " clear_$oneof_name$();\n"
568 " set_has_$name$();\n"
569 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
570 " }\n"
571 " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
572 " return $oneof_prefix$$name$_.MutableNoArena($default_variable$);\n"
573 "}\n"
574 "$inline$ ::std::string* $classname$::$release_name$() {\n"
575 " if (has_$name$()) {\n"
576 " clear_has_$oneof_name$();\n"
577 " return $oneof_prefix$$name$_.ReleaseNoArena($default_variable$);\n"
578 " } else {\n"
579 " return NULL;\n"
580 " }\n"
581 "}\n"
582 "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\ n"
583 " if (!has_$name$()) {\n"
584 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n"
585 " }\n"
586 " clear_$oneof_name$();\n"
587 " if ($name$ != NULL) {\n"
588 " set_has_$name$();\n"
589 " $oneof_prefix$$name$_.SetAllocatedNoArena($default_variable$,\n"
590 " $name$);\n"
591 " }\n"
592 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
593 "}\n");
594 }
595 }
596
597 void StringOneofFieldGenerator::
598 GenerateClearingCode(io::Printer* printer) const {
599 map<string, string> variables(variables_);
600 if (dependent_field_) {
601 variables["this_message"] = DependentBaseDownCast();
602 // This clearing code may be in the dependent base class. If the default
603 // value is an empty string, then the $default_variable$ is a global
604 // singleton. If the default is not empty, we need to down-cast to get the
605 // default value's global singleton instance. See SetStringVariables() for
606 // possible values of default_variable.
607 if (!descriptor_->default_value_string().empty()) {
608 variables["default_variable"] =
609 DependentBaseDownCast() + variables["default_variable"];
610 }
611 } else {
612 variables["this_message"] = "";
613 }
614 if (SupportsArenas(descriptor_)) {
615 printer->Print(variables,
616 "$this_message$$oneof_prefix$$name$_.Destroy($default_variable$,\n"
617 " $this_message$GetArenaNoVirtual());\n");
618 } else {
619 printer->Print(variables,
620 "$this_message$$oneof_prefix$$name$_."
621 "DestroyNoArena($default_variable$);\n");
622 }
623 }
624
625 void StringOneofFieldGenerator::
626 GenerateSwappingCode(io::Printer* printer) const {
627 // Don't print any swapping code. Swapping the union will swap this field.
628 }
629
630 void StringOneofFieldGenerator::
631 GenerateConstructorCode(io::Printer* printer) const {
632 printer->Print(variables_,
633 " $classname$_default_oneof_instance_->$name$_.UnsafeSetDefault("
634 "$default_variable$);\n");
635 }
636
637 void StringOneofFieldGenerator::
638 GenerateDestructorCode(io::Printer* printer) const {
639 if (SupportsArenas(descriptor_)) {
640 printer->Print(variables_,
641 "if (has_$name$()) {\n"
642 " $oneof_prefix$$name$_.Destroy($default_variable$,\n"
643 " GetArenaNoVirtual());\n"
644 "}\n");
645 } else {
646 printer->Print(variables_,
647 "if (has_$name$()) {\n"
648 " $oneof_prefix$$name$_.DestroyNoArena($default_variable$);\n"
649 "}\n");
650 }
651 }
652
653 void StringOneofFieldGenerator::
654 GenerateMergeFromCodedStream(io::Printer* printer) const {
655 printer->Print(variables_,
656 "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n"
657 " input, this->mutable_$name$()));\n");
658
659 if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
660 GenerateUtf8CheckCodeForString(
661 descriptor_, true, variables_,
662 "this->$name$().data(), this->$name$().length(),\n", printer);
663 }
664 }
665
666
667 // ===================================================================
668
311 RepeatedStringFieldGenerator:: 669 RepeatedStringFieldGenerator::
312 RepeatedStringFieldGenerator(const FieldDescriptor* descriptor, 670 RepeatedStringFieldGenerator(const FieldDescriptor* descriptor,
313 const Options& options) 671 const Options& options)
314 : descriptor_(descriptor) { 672 : descriptor_(descriptor) {
315 SetStringVariables(descriptor, &variables_, options); 673 SetStringVariables(descriptor, &variables_, options);
316 } 674 }
317 675
318 RepeatedStringFieldGenerator::~RepeatedStringFieldGenerator() {} 676 RepeatedStringFieldGenerator::~RepeatedStringFieldGenerator() {}
319 677
320 void RepeatedStringFieldGenerator:: 678 void RepeatedStringFieldGenerator::
321 GeneratePrivateMembers(io::Printer* printer) const { 679 GeneratePrivateMembers(io::Printer* printer) const {
322 printer->Print(variables_, 680 printer->Print(variables_,
323 "::google::protobuf::RepeatedPtrField< ::std::string> $name$_;\n"); 681 "::google::protobuf::RepeatedPtrField< ::std::string> $name$_;\n");
324 } 682 }
325 683
326 void RepeatedStringFieldGenerator:: 684 void RepeatedStringFieldGenerator::
327 GenerateAccessorDeclarations(io::Printer* printer) const { 685 GenerateAccessorDeclarations(io::Printer* printer) const {
328 // See comment above about unknown ctypes. 686 // See comment above about unknown ctypes.
329 if (descriptor_->options().ctype() != FieldOptions::STRING) { 687 bool unknown_ctype =
688 descriptor_->options().ctype() != EffectiveStringCType(descriptor_);
689
690 if (unknown_ctype) {
330 printer->Outdent(); 691 printer->Outdent();
331 printer->Print( 692 printer->Print(
332 " private:\n" 693 " private:\n"
333 " // Hidden due to unknown ctype option.\n"); 694 " // Hidden due to unknown ctype option.\n");
334 printer->Indent(); 695 printer->Indent();
335 } 696 }
336 697
337 printer->Print(variables_, 698 printer->Print(variables_,
338 "inline const ::std::string& $name$(int index) const$deprecation$;\n" 699 "const ::std::string& $name$(int index) const$deprecation$;\n"
339 "inline ::std::string* mutable_$name$(int index)$deprecation$;\n" 700 "::std::string* mutable_$name$(int index)$deprecation$;\n"
340 "inline void set_$name$(int index, const ::std::string& value)$deprecation$; \n" 701 "void set_$name$(int index, const ::std::string& value)$deprecation$;\n"
341 "inline void set_$name$(int index, const char* value)$deprecation$;\n" 702 "void set_$name$(int index, const char* value)$deprecation$;\n"
342 "inline " 703 ""
343 "void set_$name$(int index, const $pointer_type$* value, size_t size)" 704 "void set_$name$(int index, const $pointer_type$* value, size_t size)"
344 "$deprecation$;\n" 705 "$deprecation$;\n"
345 "inline ::std::string* add_$name$()$deprecation$;\n" 706 "::std::string* add_$name$()$deprecation$;\n"
346 "inline void add_$name$(const ::std::string& value)$deprecation$;\n" 707 "void add_$name$(const ::std::string& value)$deprecation$;\n"
347 "inline void add_$name$(const char* value)$deprecation$;\n" 708 "void add_$name$(const char* value)$deprecation$;\n"
348 "inline void add_$name$(const $pointer_type$* value, size_t size)" 709 "void add_$name$(const $pointer_type$* value, size_t size)"
349 "$deprecation$;\n"); 710 "$deprecation$;\n");
350 711
351 printer->Print(variables_, 712 printer->Print(variables_,
352 "inline const ::google::protobuf::RepeatedPtrField< ::std::string>& $name$() const" 713 "const ::google::protobuf::RepeatedPtrField< ::std::string>& $name$() const"
353 "$deprecation$;\n" 714 "$deprecation$;\n"
354 "inline ::google::protobuf::RepeatedPtrField< ::std::string>* mutable_$name$ ()" 715 "::google::protobuf::RepeatedPtrField< ::std::string>* mutable_$name$()"
355 "$deprecation$;\n"); 716 "$deprecation$;\n");
356 717
357 if (descriptor_->options().ctype() != FieldOptions::STRING) { 718 if (unknown_ctype) {
358 printer->Outdent(); 719 printer->Outdent();
359 printer->Print(" public:\n"); 720 printer->Print(" public:\n");
360 printer->Indent(); 721 printer->Indent();
361 } 722 }
362 } 723 }
363 724
364 void RepeatedStringFieldGenerator:: 725 void RepeatedStringFieldGenerator::
365 GenerateInlineAccessorDefinitions(io::Printer* printer) const { 726 GenerateInlineAccessorDefinitions(io::Printer* printer,
366 printer->Print(variables_, 727 bool is_inline) const {
367 "inline const ::std::string& $classname$::$name$(int index) const {\n" 728 map<string, string> variables(variables_);
729 variables["inline"] = is_inline ? "inline" : "";
730 printer->Print(variables,
731 "$inline$ const ::std::string& $classname$::$name$(int index) const {\n"
732 " // @@protoc_insertion_point(field_get:$full_name$)\n"
368 " return $name$_.$cppget$(index);\n" 733 " return $name$_.$cppget$(index);\n"
369 "}\n" 734 "}\n"
370 "inline ::std::string* $classname$::mutable_$name$(int index) {\n" 735 "$inline$ ::std::string* $classname$::mutable_$name$(int index) {\n"
736 " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
371 " return $name$_.Mutable(index);\n" 737 " return $name$_.Mutable(index);\n"
372 "}\n" 738 "}\n"
373 "inline void $classname$::set_$name$(int index, const ::std::string& value) {\n" 739 "$inline$ void $classname$::set_$name$(int index, const ::std::string& value ) {\n"
740 " // @@protoc_insertion_point(field_set:$full_name$)\n"
374 " $name$_.Mutable(index)->assign(value);\n" 741 " $name$_.Mutable(index)->assign(value);\n"
375 "}\n" 742 "}\n"
376 "inline void $classname$::set_$name$(int index, const char* value) {\n" 743 "$inline$ void $classname$::set_$name$(int index, const char* value) {\n"
377 " $name$_.Mutable(index)->assign(value);\n" 744 " $name$_.Mutable(index)->assign(value);\n"
745 " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
378 "}\n" 746 "}\n"
379 "inline void " 747 "$inline$ void "
380 "$classname$::set_$name$" 748 "$classname$::set_$name$"
381 "(int index, const $pointer_type$* value, size_t size) {\n" 749 "(int index, const $pointer_type$* value, size_t size) {\n"
382 " $name$_.Mutable(index)->assign(\n" 750 " $name$_.Mutable(index)->assign(\n"
383 " reinterpret_cast<const char*>(value), size);\n" 751 " reinterpret_cast<const char*>(value), size);\n"
752 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
384 "}\n" 753 "}\n"
385 "inline ::std::string* $classname$::add_$name$() {\n" 754 "$inline$ ::std::string* $classname$::add_$name$() {\n"
386 " return $name$_.Add();\n" 755 " return $name$_.Add();\n"
387 "}\n" 756 "}\n"
388 "inline void $classname$::add_$name$(const ::std::string& value) {\n" 757 "$inline$ void $classname$::add_$name$(const ::std::string& value) {\n"
389 " $name$_.Add()->assign(value);\n" 758 " $name$_.Add()->assign(value);\n"
759 " // @@protoc_insertion_point(field_add:$full_name$)\n"
390 "}\n" 760 "}\n"
391 "inline void $classname$::add_$name$(const char* value) {\n" 761 "$inline$ void $classname$::add_$name$(const char* value) {\n"
392 " $name$_.Add()->assign(value);\n" 762 " $name$_.Add()->assign(value);\n"
763 " // @@protoc_insertion_point(field_add_char:$full_name$)\n"
393 "}\n" 764 "}\n"
394 "inline void " 765 "$inline$ void "
395 "$classname$::add_$name$(const $pointer_type$* value, size_t size) {\n" 766 "$classname$::add_$name$(const $pointer_type$* value, size_t size) {\n"
396 " $name$_.Add()->assign(reinterpret_cast<const char*>(value), size);\n" 767 " $name$_.Add()->assign(reinterpret_cast<const char*>(value), size);\n"
768 " // @@protoc_insertion_point(field_add_pointer:$full_name$)\n"
397 "}\n"); 769 "}\n");
398 printer->Print(variables_, 770 printer->Print(variables,
399 "inline const ::google::protobuf::RepeatedPtrField< ::std::string>&\n" 771 "$inline$ const ::google::protobuf::RepeatedPtrField< ::std::string>&\n"
400 "$classname$::$name$() const {\n" 772 "$classname$::$name$() const {\n"
773 " // @@protoc_insertion_point(field_list:$full_name$)\n"
401 " return $name$_;\n" 774 " return $name$_;\n"
402 "}\n" 775 "}\n"
403 "inline ::google::protobuf::RepeatedPtrField< ::std::string>*\n" 776 "$inline$ ::google::protobuf::RepeatedPtrField< ::std::string>*\n"
404 "$classname$::mutable_$name$() {\n" 777 "$classname$::mutable_$name$() {\n"
778 " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n"
405 " return &$name$_;\n" 779 " return &$name$_;\n"
406 "}\n"); 780 "}\n");
407 } 781 }
408 782
409 void RepeatedStringFieldGenerator:: 783 void RepeatedStringFieldGenerator::
410 GenerateClearingCode(io::Printer* printer) const { 784 GenerateClearingCode(io::Printer* printer) const {
411 printer->Print(variables_, "$name$_.Clear();\n"); 785 printer->Print(variables_, "$name$_.Clear();\n");
412 } 786 }
413 787
414 void RepeatedStringFieldGenerator:: 788 void RepeatedStringFieldGenerator::
415 GenerateMergingCode(io::Printer* printer) const { 789 GenerateMergingCode(io::Printer* printer) const {
416 printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n"); 790 printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
417 } 791 }
418 792
419 void RepeatedStringFieldGenerator:: 793 void RepeatedStringFieldGenerator::
420 GenerateSwappingCode(io::Printer* printer) const { 794 GenerateSwappingCode(io::Printer* printer) const {
421 printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n"); 795 printer->Print(variables_, "$name$_.UnsafeArenaSwap(&other->$name$_);\n");
422 } 796 }
423 797
424 void RepeatedStringFieldGenerator:: 798 void RepeatedStringFieldGenerator::
425 GenerateConstructorCode(io::Printer* printer) const { 799 GenerateConstructorCode(io::Printer* printer) const {
426 // Not needed for repeated fields. 800 // Not needed for repeated fields.
427 } 801 }
428 802
429 void RepeatedStringFieldGenerator:: 803 void RepeatedStringFieldGenerator::
430 GenerateMergeFromCodedStream(io::Printer* printer) const { 804 GenerateMergeFromCodedStream(io::Printer* printer) const {
431 printer->Print(variables_, 805 printer->Print(variables_,
432 "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n" 806 "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n"
433 " input, this->add_$name$()));\n"); 807 " input, this->add_$name$()));\n");
434 if (HasUtf8Verification(descriptor_->file()) && 808 if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
435 descriptor_->type() == FieldDescriptor::TYPE_STRING) { 809 GenerateUtf8CheckCodeForString(
436 printer->Print(variables_, 810 descriptor_, true, variables_,
437 "::google::protobuf::internal::WireFormat::VerifyUTF8String(\n" 811 "this->$name$(this->$name$_size() - 1).data(),\n"
438 " this->$name$(this->$name$_size() - 1).data(),\n" 812 "this->$name$(this->$name$_size() - 1).length(),\n",
439 " this->$name$(this->$name$_size() - 1).length(),\n" 813 printer);
440 " ::google::protobuf::internal::WireFormat::PARSE);\n");
441 } 814 }
442 } 815 }
443 816
444 void RepeatedStringFieldGenerator:: 817 void RepeatedStringFieldGenerator::
445 GenerateSerializeWithCachedSizes(io::Printer* printer) const { 818 GenerateSerializeWithCachedSizes(io::Printer* printer) const {
446 printer->Print(variables_, 819 printer->Print(variables_,
447 "for (int i = 0; i < this->$name$_size(); i++) {\n"); 820 "for (int i = 0; i < this->$name$_size(); i++) {\n");
448 if (HasUtf8Verification(descriptor_->file()) && 821 printer->Indent();
449 descriptor_->type() == FieldDescriptor::TYPE_STRING) { 822 if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
450 printer->Print(variables_, 823 GenerateUtf8CheckCodeForString(
451 "::google::protobuf::internal::WireFormat::VerifyUTF8String(\n" 824 descriptor_, false, variables_,
452 " this->$name$(i).data(), this->$name$(i).length(),\n" 825 "this->$name$(i).data(), this->$name$(i).length(),\n", printer);
453 " ::google::protobuf::internal::WireFormat::SERIALIZE);\n");
454 } 826 }
827 printer->Outdent();
455 printer->Print(variables_, 828 printer->Print(variables_,
456 " ::google::protobuf::internal::WireFormatLite::Write$declared_type$(\n" 829 " ::google::protobuf::internal::WireFormatLite::Write$declared_type$(\n"
457 " $number$, this->$name$(i), output);\n" 830 " $number$, this->$name$(i), output);\n"
458 "}\n"); 831 "}\n");
459 } 832 }
460 833
461 void RepeatedStringFieldGenerator:: 834 void RepeatedStringFieldGenerator::
462 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { 835 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
463 printer->Print(variables_, 836 printer->Print(variables_,
464 "for (int i = 0; i < this->$name$_size(); i++) {\n"); 837 "for (int i = 0; i < this->$name$_size(); i++) {\n");
465 if (HasUtf8Verification(descriptor_->file()) && 838 printer->Indent();
466 descriptor_->type() == FieldDescriptor::TYPE_STRING) { 839 if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
467 printer->Print(variables_, 840 GenerateUtf8CheckCodeForString(
468 " ::google::protobuf::internal::WireFormat::VerifyUTF8String(\n" 841 descriptor_, false, variables_,
469 " this->$name$(i).data(), this->$name$(i).length(),\n" 842 "this->$name$(i).data(), this->$name$(i).length(),\n", printer);
470 " ::google::protobuf::internal::WireFormat::SERIALIZE);\n");
471 } 843 }
844 printer->Outdent();
472 printer->Print(variables_, 845 printer->Print(variables_,
473 " target = ::google::protobuf::internal::WireFormatLite::\n" 846 " target = ::google::protobuf::internal::WireFormatLite::\n"
474 " Write$declared_type$ToArray($number$, this->$name$(i), target);\n" 847 " Write$declared_type$ToArray($number$, this->$name$(i), target);\n"
475 "}\n"); 848 "}\n");
476 } 849 }
477 850
478 void RepeatedStringFieldGenerator:: 851 void RepeatedStringFieldGenerator::
479 GenerateByteSize(io::Printer* printer) const { 852 GenerateByteSize(io::Printer* printer) const {
480 printer->Print(variables_, 853 printer->Print(variables_,
481 "total_size += $tag_size$ * this->$name$_size();\n" 854 "total_size += $tag_size$ * this->$name$_size();\n"
482 "for (int i = 0; i < this->$name$_size(); i++) {\n" 855 "for (int i = 0; i < this->$name$_size(); i++) {\n"
483 " total_size += ::google::protobuf::internal::WireFormatLite::$declared_typ e$Size(\n" 856 " total_size += ::google::protobuf::internal::WireFormatLite::$declared_typ e$Size(\n"
484 " this->$name$(i));\n" 857 " this->$name$(i));\n"
485 "}\n"); 858 "}\n");
486 } 859 }
487 860
488 } // namespace cpp 861 } // namespace cpp
489 } // namespace compiler 862 } // namespace compiler
490 } // namespace protobuf 863 } // namespace protobuf
491 } // namespace google 864 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698