OLD | NEW |
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 // https://developers.google.com/protocol-buffers/ | 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. |
(...skipping 28 matching lines...) Expand all Loading... |
39 #include <google/protobuf/stubs/strutil.h> | 39 #include <google/protobuf/stubs/strutil.h> |
40 | 40 |
41 namespace google { | 41 namespace google { |
42 namespace protobuf { | 42 namespace protobuf { |
43 namespace compiler { | 43 namespace compiler { |
44 namespace cpp { | 44 namespace cpp { |
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 std::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 string default_variable_string = | 55 string default_variable_string = "_default_" + FieldName(descriptor) + "_"; |
| 56 (*variables)["default_variable_name"] = default_variable_string; |
| 57 (*variables)["default_variable"] = |
56 descriptor->default_value_string().empty() | 58 descriptor->default_value_string().empty() |
57 ? "&::google::protobuf::internal::GetEmptyStringAlreadyInited()" | 59 ? "&::google::protobuf::internal::GetEmptyStringAlreadyInited()" |
58 : "_default_" + FieldName(descriptor) + "_"; | 60 : "&" + (*variables)["classname"] + "::" + default_variable_string + |
59 (*variables)["default_variable"] = default_variable_string; | 61 ".get()"; |
60 (*variables)["default_value_init"] = | |
61 descriptor->default_value_string().empty() | |
62 ? "" : "*" + default_variable_string; | |
63 (*variables)["pointer_type"] = | 62 (*variables)["pointer_type"] = |
64 descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char"; | 63 descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char"; |
65 // NOTE: Escaped here to unblock proto1->proto2 migration. | 64 // NOTE: Escaped here to unblock proto1->proto2 migration. |
66 // TODO(liujisi): Extend this to apply for other conflicting methods. | 65 // TODO(liujisi): Extend this to apply for other conflicting methods. |
67 (*variables)["release_name"] = | 66 (*variables)["release_name"] = |
68 SafeFunctionName(descriptor->containing_type(), | 67 SafeFunctionName(descriptor->containing_type(), |
69 descriptor, "release_"); | 68 descriptor, "release_"); |
70 (*variables)["full_name"] = descriptor->full_name(); | 69 (*variables)["full_name"] = descriptor->full_name(); |
71 | 70 |
72 (*variables)["string_piece"] = "::std::string"; | 71 (*variables)["string_piece"] = "::std::string"; |
(...skipping 24 matching lines...) Expand all Loading... |
97 // and littering the runtime code paths with conditionals. It's simpler to | 96 // and littering the runtime code paths with conditionals. It's simpler to |
98 // stick with this but use lightweight accessors that assume arena == NULL. | 97 // stick with this but use lightweight accessors that assume arena == NULL. |
99 // There should be very little overhead anyway because it's just a tagged | 98 // There should be very little overhead anyway because it's just a tagged |
100 // pointer in-memory. | 99 // pointer in-memory. |
101 printer->Print(variables_, "::google::protobuf::internal::ArenaStringPtr $name
$_;\n"); | 100 printer->Print(variables_, "::google::protobuf::internal::ArenaStringPtr $name
$_;\n"); |
102 } | 101 } |
103 | 102 |
104 void StringFieldGenerator:: | 103 void StringFieldGenerator:: |
105 GenerateStaticMembers(io::Printer* printer) const { | 104 GenerateStaticMembers(io::Printer* printer) const { |
106 if (!descriptor_->default_value_string().empty()) { | 105 if (!descriptor_->default_value_string().empty()) { |
107 printer->Print(variables_, "static ::std::string* $default_variable$;\n"); | 106 printer->Print(variables_, |
| 107 "static ::google::protobuf::internal::ExplicitlyConstructed<
::std::string>" |
| 108 " $default_variable_name$;\n"); |
108 } | 109 } |
109 } | 110 } |
110 | 111 |
111 void StringFieldGenerator:: | 112 void StringFieldGenerator:: |
112 GenerateAccessorDeclarations(io::Printer* printer) const { | 113 GenerateAccessorDeclarations(io::Printer* printer) const { |
113 // 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 |
114 // because that ctype isn't actually implemented. In particular, this is | 115 // because that ctype isn't actually implemented. In particular, this is |
115 // 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. |
116 // 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 |
117 // dependencies and we aren't releasing StringPiece because it's hardly | 118 // dependencies and we aren't releasing StringPiece because it's hardly |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 if (unknown_ctype) { | 159 if (unknown_ctype) { |
159 printer->Outdent(); | 160 printer->Outdent(); |
160 printer->Print(" public:\n"); | 161 printer->Print(" public:\n"); |
161 printer->Indent(); | 162 printer->Indent(); |
162 } | 163 } |
163 } | 164 } |
164 | 165 |
165 void StringFieldGenerator:: | 166 void StringFieldGenerator:: |
166 GenerateInlineAccessorDefinitions(io::Printer* printer, | 167 GenerateInlineAccessorDefinitions(io::Printer* printer, |
167 bool is_inline) const { | 168 bool is_inline) const { |
168 map<string, string> variables(variables_); | 169 std::map<string, string> variables(variables_); |
169 variables["inline"] = is_inline ? "inline" : ""; | 170 variables["inline"] = is_inline ? "inline " : ""; |
170 if (SupportsArenas(descriptor_)) { | 171 if (SupportsArenas(descriptor_)) { |
171 printer->Print(variables, | 172 printer->Print( |
172 "$inline$ const ::std::string& $classname$::$name$() const {\n" | 173 variables, |
173 " // @@protoc_insertion_point(field_get:$full_name$)\n" | 174 "$inline$const ::std::string& $classname$::$name$() const {\n" |
174 " return $name$_.Get($default_variable$);\n" | 175 " // @@protoc_insertion_point(field_get:$full_name$)\n" |
175 "}\n" | 176 " return $name$_.Get();\n" |
176 "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n" | 177 "}\n" |
177 " $set_hasbit$\n" | 178 "$inline$void $classname$::set_$name$(const ::std::string& value) {\n" |
178 " $name$_.Set($default_variable$, value, GetArenaNoVirtual());\n" | 179 " $set_hasbit$\n" |
179 " // @@protoc_insertion_point(field_set:$full_name$)\n" | 180 " $name$_.Set($default_variable$, value, GetArenaNoVirtual());\n" |
180 "}\n" | 181 " // @@protoc_insertion_point(field_set:$full_name$)\n" |
181 "$inline$ void $classname$::set_$name$(const char* value) {\n" | 182 "}\n" |
182 " $set_hasbit$\n" | 183 "$inline$void $classname$::set_$name$(const char* value) {\n" |
183 " $name$_.Set($default_variable$, $string_piece$(value),\n" | 184 " $set_hasbit$\n" |
184 " GetArenaNoVirtual());\n" | 185 " $name$_.Set($default_variable$, $string_piece$(value),\n" |
185 " // @@protoc_insertion_point(field_set_char:$full_name$)\n" | 186 " GetArenaNoVirtual());\n" |
186 "}\n" | 187 " // @@protoc_insertion_point(field_set_char:$full_name$)\n" |
187 "$inline$ " | 188 "}\n" |
188 "void $classname$::set_$name$(const $pointer_type$* value,\n" | 189 "$inline$" |
189 " size_t size) {\n" | 190 "void $classname$::set_$name$(const $pointer_type$* value,\n" |
190 " $set_hasbit$\n" | 191 " size_t size) {\n" |
191 " $name$_.Set($default_variable$, $string_piece$(\n" | 192 " $set_hasbit$\n" |
192 " reinterpret_cast<const char*>(value), size), GetArenaNoVirtual());\
n" | 193 " $name$_.Set($default_variable$, $string_piece$(\n" |
193 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" | 194 " reinterpret_cast<const char*>(value), size), " |
194 "}\n" | 195 "GetArenaNoVirtual());\n" |
195 "$inline$ ::std::string* $classname$::mutable_$name$() {\n" | 196 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" |
196 " $set_hasbit$\n" | 197 "}\n" |
197 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" | 198 "$inline$::std::string* $classname$::mutable_$name$() {\n" |
198 " return $name$_.Mutable($default_variable$, GetArenaNoVirtual());\n" | 199 " $set_hasbit$\n" |
199 "}\n" | 200 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" |
200 "$inline$ ::std::string* $classname$::$release_name$() {\n" | 201 " return $name$_.Mutable($default_variable$, GetArenaNoVirtual());\n" |
201 " // @@protoc_insertion_point(field_release:$full_name$)\n" | 202 "}\n" |
202 " $clear_hasbit$\n" | 203 "$inline$::std::string* $classname$::$release_name$() {\n" |
203 " return $name$_.Release($default_variable$, GetArenaNoVirtual());\n" | 204 " // @@protoc_insertion_point(field_release:$full_name$)\n" |
204 "}\n" | 205 " $clear_hasbit$\n" |
205 "$inline$ ::std::string* $classname$::unsafe_arena_release_$name$() {\n" | 206 " return $name$_.Release($default_variable$, GetArenaNoVirtual());\n" |
206 " // @@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" | 207 "}\n" |
207 " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" | 208 "$inline$::std::string* $classname$::unsafe_arena_release_$name$() {\n" |
208 " $clear_hasbit$\n" | 209 " // " |
209 " return $name$_.UnsafeArenaRelease($default_variable$,\n" | 210 "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" |
210 " GetArenaNoVirtual());\n" | 211 " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" |
211 "}\n" | 212 " $clear_hasbit$\n" |
212 "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\
n" | 213 " return $name$_.UnsafeArenaRelease($default_variable$,\n" |
213 " if ($name$ != NULL) {\n" | 214 " GetArenaNoVirtual());\n" |
214 " $set_hasbit$\n" | 215 "}\n" |
215 " } else {\n" | 216 "$inline$void $classname$::set_allocated_$name$(::std::string* $name$) {
\n" |
216 " $clear_hasbit$\n" | 217 " if ($name$ != NULL) {\n" |
217 " }\n" | 218 " $set_hasbit$\n" |
218 " $name$_.SetAllocated($default_variable$, $name$,\n" | 219 " } else {\n" |
219 " GetArenaNoVirtual());\n" | 220 " $clear_hasbit$\n" |
220 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" | 221 " }\n" |
221 "}\n" | 222 " $name$_.SetAllocated($default_variable$, $name$,\n" |
222 "$inline$ void $classname$::unsafe_arena_set_allocated_$name$(\n" | 223 " GetArenaNoVirtual());\n" |
223 " ::std::string* $name$) {\n" | 224 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" |
224 " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" | 225 "}\n" |
225 " if ($name$ != NULL) {\n" | 226 "$inline$void $classname$::unsafe_arena_set_allocated_$name$(\n" |
226 " $set_hasbit$\n" | 227 " ::std::string* $name$) {\n" |
227 " } else {\n" | 228 " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" |
228 " $clear_hasbit$\n" | 229 " if ($name$ != NULL) {\n" |
229 " }\n" | 230 " $set_hasbit$\n" |
230 " $name$_.UnsafeArenaSetAllocated($default_variable$,\n" | 231 " } else {\n" |
231 " $name$, GetArenaNoVirtual());\n" | 232 " $clear_hasbit$\n" |
232 " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:" | 233 " }\n" |
233 "$full_name$)\n" | 234 " $name$_.UnsafeArenaSetAllocated($default_variable$,\n" |
234 "}\n"); | 235 " $name$, GetArenaNoVirtual());\n" |
| 236 " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:" |
| 237 "$full_name$)\n" |
| 238 "}\n"); |
235 } else { | 239 } else { |
236 // No-arena case. | 240 // No-arena case. |
237 printer->Print(variables, | 241 printer->Print( |
238 "$inline$ const ::std::string& $classname$::$name$() const {\n" | 242 variables, |
239 " // @@protoc_insertion_point(field_get:$full_name$)\n" | 243 "$inline$const ::std::string& $classname$::$name$() const {\n" |
240 " return $name$_.GetNoArena($default_variable$);\n" | 244 " // @@protoc_insertion_point(field_get:$full_name$)\n" |
241 "}\n" | 245 " return $name$_.GetNoArena();\n" |
242 "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n" | 246 "}\n" |
243 " $set_hasbit$\n" | 247 "$inline$void $classname$::set_$name$(const ::std::string& value) {\n" |
244 " $name$_.SetNoArena($default_variable$, value);\n" | 248 " $set_hasbit$\n" |
245 " // @@protoc_insertion_point(field_set:$full_name$)\n" | 249 " $name$_.SetNoArena($default_variable$, value);\n" |
246 "}\n" | 250 " // @@protoc_insertion_point(field_set:$full_name$)\n" |
247 "$inline$ void $classname$::set_$name$(const char* value) {\n" | 251 "}\n" |
248 " $set_hasbit$\n" | 252 "$inline$void $classname$::set_$name$(const char* value) {\n" |
249 " $name$_.SetNoArena($default_variable$, $string_piece$(value));\n" | 253 " $set_hasbit$\n" |
250 " // @@protoc_insertion_point(field_set_char:$full_name$)\n" | 254 " $name$_.SetNoArena($default_variable$, $string_piece$(value));\n" |
251 "}\n" | 255 " // @@protoc_insertion_point(field_set_char:$full_name$)\n" |
252 "$inline$ " | 256 "}\n" |
253 "void $classname$::set_$name$(const $pointer_type$* value, " | 257 "$inline$" |
254 "size_t size) {\n" | 258 "void $classname$::set_$name$(const $pointer_type$* value, " |
255 " $set_hasbit$\n" | 259 "size_t size) {\n" |
256 " $name$_.SetNoArena($default_variable$,\n" | 260 " $set_hasbit$\n" |
257 " $string_piece$(reinterpret_cast<const char*>(value), size));\n" | 261 " $name$_.SetNoArena($default_variable$,\n" |
258 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" | 262 " $string_piece$(reinterpret_cast<const char*>(value), size));\n" |
259 "}\n" | 263 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" |
260 "$inline$ ::std::string* $classname$::mutable_$name$() {\n" | 264 "}\n" |
261 " $set_hasbit$\n" | 265 "$inline$::std::string* $classname$::mutable_$name$() {\n" |
262 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" | 266 " $set_hasbit$\n" |
263 " return $name$_.MutableNoArena($default_variable$);\n" | 267 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" |
264 "}\n" | 268 " return $name$_.MutableNoArena($default_variable$);\n" |
265 "$inline$ ::std::string* $classname$::$release_name$() {\n" | 269 "}\n" |
266 " // @@protoc_insertion_point(field_release:$full_name$)\n" | 270 "$inline$::std::string* $classname$::$release_name$() {\n" |
267 " $clear_hasbit$\n" | 271 " // @@protoc_insertion_point(field_release:$full_name$)\n" |
268 " return $name$_.ReleaseNoArena($default_variable$);\n" | 272 " $clear_hasbit$\n" |
269 "}\n" | 273 " return $name$_.ReleaseNoArena($default_variable$);\n" |
270 "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\
n" | 274 "}\n" |
271 " if ($name$ != NULL) {\n" | 275 "$inline$void $classname$::set_allocated_$name$(::std::string* $name$) {
\n" |
272 " $set_hasbit$\n" | 276 " if ($name$ != NULL) {\n" |
273 " } else {\n" | 277 " $set_hasbit$\n" |
274 " $clear_hasbit$\n" | 278 " } else {\n" |
275 " }\n" | 279 " $clear_hasbit$\n" |
276 " $name$_.SetAllocatedNoArena($default_variable$, $name$);\n" | 280 " }\n" |
277 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" | 281 " $name$_.SetAllocatedNoArena($default_variable$, $name$);\n" |
278 "}\n"); | 282 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" |
| 283 "}\n"); |
279 } | 284 } |
280 } | 285 } |
281 | 286 |
282 void StringFieldGenerator:: | 287 void StringFieldGenerator:: |
283 GenerateNonInlineAccessorDefinitions(io::Printer* printer) const { | 288 GenerateNonInlineAccessorDefinitions(io::Printer* printer) const { |
284 if (!descriptor_->default_value_string().empty()) { | 289 if (!descriptor_->default_value_string().empty()) { |
285 // Initialized in GenerateDefaultInstanceAllocator. | 290 // Initialized in GenerateDefaultInstanceAllocator. |
286 printer->Print(variables_, | 291 printer->Print(variables_, |
287 "::std::string* $classname$::$default_variable$ = NULL;\n"); | 292 "::google::protobuf::internal::ExplicitlyConstructed< ::std::
string> " |
| 293 "$classname$::$default_variable_name$;\n"); |
288 } | 294 } |
289 } | 295 } |
290 | 296 |
291 void StringFieldGenerator:: | 297 void StringFieldGenerator:: |
292 GenerateClearingCode(io::Printer* printer) const { | 298 GenerateClearingCode(io::Printer* printer) const { |
293 // Two-dimension specialization here: supporting arenas or not, and default | 299 // Two-dimension specialization here: supporting arenas or not, and default |
294 // value is the empty string or not. Complexity here ensures the minimal | 300 // value is the empty string or not. Complexity here ensures the minimal |
295 // number of branches / amount of extraneous code at runtime (given that the | 301 // number of branches / amount of extraneous code at runtime (given that the |
296 // below methods are inlined one-liners)! | 302 // below methods are inlined one-liners)! |
297 if (SupportsArenas(descriptor_)) { | 303 if (SupportsArenas(descriptor_)) { |
298 if (descriptor_->default_value_string().empty()) { | 304 if (descriptor_->default_value_string().empty()) { |
299 printer->Print(variables_, | 305 printer->Print(variables_, |
300 "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n"); | 306 "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n"); |
301 } else { | 307 } else { |
302 printer->Print(variables_, | 308 printer->Print(variables_, |
303 "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n"); | 309 "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n"); |
304 } | 310 } |
305 } else { | 311 } else { |
306 if (descriptor_->default_value_string().empty()) { | 312 if (descriptor_->default_value_string().empty()) { |
307 printer->Print(variables_, | 313 printer->Print(variables_, |
308 "$name$_.ClearToEmptyNoArena($default_variable$);\n"); | 314 "$name$_.ClearToEmptyNoArena($default_variable$);\n"); |
309 } else { | 315 } else { |
310 printer->Print(variables_, | 316 printer->Print(variables_, |
311 "$name$_.ClearToDefaultNoArena($default_variable$);\n"); | 317 "$name$_.ClearToDefaultNoArena($default_variable$);\n"); |
312 } | 318 } |
313 } | 319 } |
314 } | 320 } |
315 | 321 |
316 void StringFieldGenerator:: | 322 void StringFieldGenerator:: |
| 323 GenerateMessageClearingCode(io::Printer* printer) const { |
| 324 // Two-dimension specialization here: supporting arenas, field presence, or |
| 325 // not, and default value is the empty string or not. Complexity here ensures |
| 326 // the minimal number of branches / amount of extraneous code at runtime |
| 327 // (given that the below methods are inlined one-liners)! |
| 328 |
| 329 // If we have field presence, then the Clear() method of the protocol buffer |
| 330 // will have checked that this field is set. If so, we can avoid redundant |
| 331 // checks against default_variable. |
| 332 const bool must_be_present = HasFieldPresence(descriptor_->file()); |
| 333 |
| 334 if (must_be_present) { |
| 335 printer->Print(variables_, |
| 336 "GOOGLE_DCHECK(!$name$_.IsDefault($default_variable$));\n"); |
| 337 } |
| 338 |
| 339 if (SupportsArenas(descriptor_)) { |
| 340 if (descriptor_->default_value_string().empty()) { |
| 341 printer->Print(variables_, |
| 342 "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n"); |
| 343 } else { |
| 344 printer->Print(variables_, |
| 345 "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n"); |
| 346 } |
| 347 } else if (must_be_present) { |
| 348 // When Arenas are disabled and field presence has been checked, we can |
| 349 // safely treat the ArenaStringPtr as a string*. |
| 350 if (descriptor_->default_value_string().empty()) { |
| 351 printer->Print(variables_, |
| 352 "(*$name$_.UnsafeRawStringPointer())->clear();\n"); |
| 353 } else { |
| 354 printer->Print(variables_, |
| 355 "(*$name$_.UnsafeRawStringPointer())->assign(*$default_variable$);\n"); |
| 356 } |
| 357 } else { |
| 358 if (descriptor_->default_value_string().empty()) { |
| 359 printer->Print(variables_, |
| 360 "$name$_.ClearToEmptyNoArena($default_variable$);\n"); |
| 361 } else { |
| 362 printer->Print(variables_, |
| 363 "$name$_.ClearToDefaultNoArena($default_variable$);\n"); |
| 364 } |
| 365 } |
| 366 } |
| 367 |
| 368 void StringFieldGenerator:: |
317 GenerateMergingCode(io::Printer* printer) const { | 369 GenerateMergingCode(io::Printer* printer) const { |
318 if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) { | 370 if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) { |
319 // TODO(gpike): improve this | 371 // TODO(gpike): improve this |
320 printer->Print(variables_, "set_$name$(from.$name$());\n"); | 372 printer->Print(variables_, "set_$name$(from.$name$());\n"); |
321 } else { | 373 } else { |
322 printer->Print(variables_, | 374 printer->Print(variables_, |
323 "$set_hasbit$\n" | 375 "$set_hasbit$\n" |
324 "$name$_.AssignWithDefault($default_variable$, from.$name$_);\n"); | 376 "$name$_.AssignWithDefault($default_variable$, from.$name$_);\n"); |
325 } | 377 } |
326 } | 378 } |
327 | 379 |
328 void StringFieldGenerator:: | 380 void StringFieldGenerator:: |
329 GenerateSwappingCode(io::Printer* printer) const { | 381 GenerateSwappingCode(io::Printer* printer) const { |
330 printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n"); | 382 printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n"); |
331 } | 383 } |
332 | 384 |
333 void StringFieldGenerator:: | 385 void StringFieldGenerator:: |
334 GenerateConstructorCode(io::Printer* printer) const { | 386 GenerateConstructorCode(io::Printer* printer) const { |
335 printer->Print(variables_, | 387 printer->Print(variables_, |
336 "$name$_.UnsafeSetDefault($default_variable$);\n"); | 388 "$name$_.UnsafeSetDefault($default_variable$);\n"); |
337 } | 389 } |
338 | 390 |
339 void StringFieldGenerator:: | 391 void StringFieldGenerator:: |
| 392 GenerateCopyConstructorCode(io::Printer* printer) const { |
| 393 GenerateConstructorCode(printer); |
| 394 |
| 395 if (HasFieldPresence(descriptor_->file())) { |
| 396 printer->Print(variables_, |
| 397 "if (from.has_$name$()) {\n"); |
| 398 } else { |
| 399 printer->Print(variables_, |
| 400 "if (from.$name$().size() > 0) {\n"); |
| 401 } |
| 402 |
| 403 printer->Indent(); |
| 404 |
| 405 if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) { |
| 406 // TODO(gpike): improve this |
| 407 printer->Print(variables_, |
| 408 "$name$_.Set($default_variable$, from.$name$(),\n" |
| 409 " GetArenaNoVirtual());\n"); |
| 410 } else { |
| 411 printer->Print(variables_, |
| 412 "$name$_.AssignWithDefault($default_variable$, from.$name$_);\n"); |
| 413 } |
| 414 |
| 415 printer->Outdent(); |
| 416 printer->Print("}\n"); |
| 417 } |
| 418 |
| 419 void StringFieldGenerator:: |
340 GenerateDestructorCode(io::Printer* printer) const { | 420 GenerateDestructorCode(io::Printer* printer) const { |
341 if (SupportsArenas(descriptor_)) { | 421 if (SupportsArenas(descriptor_)) { |
| 422 // The variable |arena| is defined by the enclosing code. |
| 423 // See MessageGenerator::GenerateSharedDestructorCode. |
342 printer->Print(variables_, | 424 printer->Print(variables_, |
343 "$name$_.Destroy($default_variable$, GetArenaNoVirtual());\n"); | 425 "$name$_.Destroy($default_variable$, arena);\n"); |
344 } else { | 426 } else { |
345 printer->Print(variables_, | 427 printer->Print(variables_, |
346 "$name$_.DestroyNoArena($default_variable$);\n"); | 428 "$name$_.DestroyNoArena($default_variable$);\n"); |
347 } | 429 } |
348 } | 430 } |
349 | 431 |
350 void StringFieldGenerator:: | 432 void StringFieldGenerator:: |
351 GenerateDefaultInstanceAllocator(io::Printer* printer) const { | 433 GenerateDefaultInstanceAllocator(io::Printer* printer) const { |
352 if (!descriptor_->default_value_string().empty()) { | 434 if (!descriptor_->default_value_string().empty()) { |
353 printer->Print(variables_, | 435 printer->Print(variables_, |
354 "$classname$::$default_variable$ =\n" | 436 "$classname$::$default_variable_name$.DefaultConstruct();\n" |
355 " new ::std::string($default$, $default_length$);\n"); | 437 "*$classname$::$default_variable_name$.get_mutable() = " |
| 438 "::std::string($default$, $default_length$);\n"); |
356 } | 439 } |
357 } | 440 } |
358 | 441 |
359 void StringFieldGenerator:: | 442 void StringFieldGenerator:: |
360 GenerateShutdownCode(io::Printer* printer) const { | 443 GenerateShutdownCode(io::Printer* printer) const { |
361 if (!descriptor_->default_value_string().empty()) { | 444 if (!descriptor_->default_value_string().empty()) { |
362 printer->Print(variables_, | 445 printer->Print(variables_, |
363 "delete $classname$::$default_variable$;\n"); | 446 "$classname$::$default_variable_name$.Shutdown();\n"); |
364 } | 447 } |
365 } | 448 } |
366 | 449 |
367 void StringFieldGenerator:: | 450 void StringFieldGenerator:: |
368 GenerateMergeFromCodedStream(io::Printer* printer) const { | 451 GenerateMergeFromCodedStream(io::Printer* printer) const { |
369 printer->Print(variables_, | 452 printer->Print(variables_, |
370 "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n" | 453 "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n" |
371 " input, this->mutable_$name$()));\n"); | 454 " input, this->mutable_$name$()));\n"); |
372 | 455 |
373 if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { | 456 if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 : StringFieldGenerator(descriptor, options), | 501 : StringFieldGenerator(descriptor, options), |
419 dependent_field_(options.proto_h) { | 502 dependent_field_(options.proto_h) { |
420 SetCommonOneofFieldVariables(descriptor, &variables_); | 503 SetCommonOneofFieldVariables(descriptor, &variables_); |
421 } | 504 } |
422 | 505 |
423 StringOneofFieldGenerator::~StringOneofFieldGenerator() {} | 506 StringOneofFieldGenerator::~StringOneofFieldGenerator() {} |
424 | 507 |
425 void StringOneofFieldGenerator:: | 508 void StringOneofFieldGenerator:: |
426 GenerateInlineAccessorDefinitions(io::Printer* printer, | 509 GenerateInlineAccessorDefinitions(io::Printer* printer, |
427 bool is_inline) const { | 510 bool is_inline) const { |
428 map<string, string> variables(variables_); | 511 std::map<string, string> variables(variables_); |
429 variables["inline"] = is_inline ? "inline" : ""; | 512 variables["inline"] = is_inline ? "inline " : ""; |
430 if (SupportsArenas(descriptor_)) { | 513 if (SupportsArenas(descriptor_)) { |
431 printer->Print(variables, | 514 printer->Print( |
432 "$inline$ const ::std::string& $classname$::$name$() const {\n" | 515 variables, |
433 " // @@protoc_insertion_point(field_get:$full_name$)\n" | 516 "$inline$const ::std::string& $classname$::$name$() const {\n" |
434 " if (has_$name$()) {\n" | 517 " // @@protoc_insertion_point(field_get:$full_name$)\n" |
435 " return $oneof_prefix$$name$_.Get($default_variable$);\n" | 518 " if (has_$name$()) {\n" |
436 " }\n" | 519 " return $oneof_prefix$$name$_.Get();\n" |
437 " return *$default_variable$;\n" | 520 " }\n" |
438 "}\n" | 521 " return *$default_variable$;\n" |
439 "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n" | 522 "}\n" |
440 " if (!has_$name$()) {\n" | 523 "$inline$void $classname$::set_$name$(const ::std::string& value) {\n" |
441 " clear_$oneof_name$();\n" | 524 " if (!has_$name$()) {\n" |
442 " set_has_$name$();\n" | 525 " clear_$oneof_name$();\n" |
443 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" | 526 " set_has_$name$();\n" |
444 " }\n" | 527 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
445 " $oneof_prefix$$name$_.Set($default_variable$, value,\n" | 528 " }\n" |
446 " GetArenaNoVirtual());\n" | 529 " $oneof_prefix$$name$_.Set($default_variable$, value,\n" |
447 " // @@protoc_insertion_point(field_set:$full_name$)\n" | 530 " GetArenaNoVirtual());\n" |
448 "}\n" | 531 " // @@protoc_insertion_point(field_set:$full_name$)\n" |
449 "$inline$ void $classname$::set_$name$(const char* value) {\n" | 532 "}\n" |
450 " if (!has_$name$()) {\n" | 533 "$inline$void $classname$::set_$name$(const char* value) {\n" |
451 " clear_$oneof_name$();\n" | 534 " if (!has_$name$()) {\n" |
452 " set_has_$name$();\n" | 535 " clear_$oneof_name$();\n" |
453 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" | 536 " set_has_$name$();\n" |
454 " }\n" | 537 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
455 " $oneof_prefix$$name$_.Set($default_variable$,\n" | 538 " }\n" |
456 " $string_piece$(value), GetArenaNoVirtual());\n" | 539 " $oneof_prefix$$name$_.Set($default_variable$,\n" |
457 " // @@protoc_insertion_point(field_set_char:$full_name$)\n" | 540 " $string_piece$(value), GetArenaNoVirtual());\n" |
458 "}\n" | 541 " // @@protoc_insertion_point(field_set_char:$full_name$)\n" |
459 "$inline$ " | 542 "}\n" |
460 "void $classname$::set_$name$(const $pointer_type$* value,\n" | 543 "$inline$" |
461 " size_t size) {\n" | 544 "void $classname$::set_$name$(const $pointer_type$* value,\n" |
462 " if (!has_$name$()) {\n" | 545 " size_t size) {\n" |
463 " clear_$oneof_name$();\n" | 546 " if (!has_$name$()) {\n" |
464 " set_has_$name$();\n" | 547 " clear_$oneof_name$();\n" |
465 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" | 548 " set_has_$name$();\n" |
466 " }\n" | 549 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
467 " $oneof_prefix$$name$_.Set($default_variable$, $string_piece$(\n" | 550 " }\n" |
468 " reinterpret_cast<const char*>(value), size),\n" | 551 " $oneof_prefix$$name$_.Set($default_variable$, $string_piece$(\n" |
469 " GetArenaNoVirtual());\n" | 552 " reinterpret_cast<const char*>(value), size),\n" |
470 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" | 553 " GetArenaNoVirtual());\n" |
471 "}\n" | 554 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" |
472 "$inline$ ::std::string* $classname$::mutable_$name$() {\n" | 555 "}\n" |
473 " if (!has_$name$()) {\n" | 556 "$inline$::std::string* $classname$::mutable_$name$() {\n" |
474 " clear_$oneof_name$();\n" | 557 " if (!has_$name$()) {\n" |
475 " set_has_$name$();\n" | 558 " clear_$oneof_name$();\n" |
476 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" | 559 " set_has_$name$();\n" |
477 " }\n" | 560 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
478 " return $oneof_prefix$$name$_.Mutable($default_variable$,\n" | 561 " }\n" |
479 " GetArenaNoVirtual());\n" | 562 " return $oneof_prefix$$name$_.Mutable($default_variable$,\n" |
480 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" | 563 " GetArenaNoVirtual());\n" |
481 "}\n" | 564 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" |
482 "$inline$ ::std::string* $classname$::$release_name$() {\n" | 565 "}\n" |
483 " // @@protoc_insertion_point(field_release:$full_name$)\n" | 566 "$inline$::std::string* $classname$::$release_name$() {\n" |
484 " if (has_$name$()) {\n" | 567 " // @@protoc_insertion_point(field_release:$full_name$)\n" |
485 " clear_has_$oneof_name$();\n" | 568 " if (has_$name$()) {\n" |
486 " return $oneof_prefix$$name$_.Release($default_variable$,\n" | 569 " clear_has_$oneof_name$();\n" |
487 " GetArenaNoVirtual());\n" | 570 " return $oneof_prefix$$name$_.Release($default_variable$,\n" |
488 " } else {\n" | 571 " GetArenaNoVirtual());\n" |
489 " return NULL;\n" | 572 " } else {\n" |
490 " }\n" | 573 " return NULL;\n" |
491 "}\n" | 574 " }\n" |
492 "$inline$ ::std::string* $classname$::unsafe_arena_release_$name$() {\n" | 575 "}\n" |
493 " // @@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" | 576 "$inline$::std::string* $classname$::unsafe_arena_release_$name$() {\n" |
494 " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" | 577 " // " |
495 " if (has_$name$()) {\n" | 578 "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" |
496 " clear_has_$oneof_name$();\n" | 579 " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" |
497 " return $oneof_prefix$$name$_.UnsafeArenaRelease(\n" | 580 " if (has_$name$()) {\n" |
498 " $default_variable$, GetArenaNoVirtual());\n" | 581 " clear_has_$oneof_name$();\n" |
499 " } else {\n" | 582 " return $oneof_prefix$$name$_.UnsafeArenaRelease(\n" |
500 " return NULL;\n" | 583 " $default_variable$, GetArenaNoVirtual());\n" |
501 " }\n" | 584 " } else {\n" |
502 "}\n" | 585 " return NULL;\n" |
503 "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\
n" | 586 " }\n" |
504 " if (!has_$name$()) {\n" | 587 "}\n" |
505 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" | 588 "$inline$void $classname$::set_allocated_$name$(::std::string* $name$) {
\n" |
506 " }\n" | 589 " if (!has_$name$()) {\n" |
507 " clear_$oneof_name$();\n" | 590 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
508 " if ($name$ != NULL) {\n" | 591 " }\n" |
509 " set_has_$name$();\n" | 592 " clear_$oneof_name$();\n" |
510 " $oneof_prefix$$name$_.SetAllocated($default_variable$, $name$,\n" | 593 " if ($name$ != NULL) {\n" |
511 " GetArenaNoVirtual());\n" | 594 " set_has_$name$();\n" |
512 " }\n" | 595 " $oneof_prefix$$name$_.SetAllocated($default_variable$, $name$,\n" |
513 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" | 596 " GetArenaNoVirtual());\n" |
514 "}\n" | 597 " }\n" |
515 "$inline$ void $classname$::unsafe_arena_set_allocated_$name$(" | 598 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" |
516 "::std::string* $name$) {\n" | 599 "}\n" |
517 " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" | 600 "$inline$void $classname$::unsafe_arena_set_allocated_$name$(" |
518 " if (!has_$name$()) {\n" | 601 "::std::string* $name$) {\n" |
519 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" | 602 " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" |
520 " }\n" | 603 " if (!has_$name$()) {\n" |
521 " clear_$oneof_name$();\n" | 604 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
522 " if ($name$) {\n" | 605 " }\n" |
523 " set_has_$name$();\n" | 606 " clear_$oneof_name$();\n" |
524 " $oneof_prefix$$name$_.UnsafeArenaSetAllocated($default_variable$, " | 607 " if ($name$) {\n" |
525 "$name$, GetArenaNoVirtual());\n" | 608 " set_has_$name$();\n" |
526 " }\n" | 609 " $oneof_prefix$$name$_.UnsafeArenaSetAllocated($default_variable$, " |
527 " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:" | 610 "$name$, GetArenaNoVirtual());\n" |
528 "$full_name$)\n" | 611 " }\n" |
529 "}\n"); | 612 " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:" |
| 613 "$full_name$)\n" |
| 614 "}\n"); |
530 } else { | 615 } else { |
531 // No-arena case. | 616 // No-arena case. |
532 printer->Print(variables, | 617 printer->Print( |
533 "$inline$ const ::std::string& $classname$::$name$() const {\n" | 618 variables, |
534 " // @@protoc_insertion_point(field_get:$full_name$)\n" | 619 "$inline$const ::std::string& $classname$::$name$() const {\n" |
535 " if (has_$name$()) {\n" | 620 " // @@protoc_insertion_point(field_get:$full_name$)\n" |
536 " return $oneof_prefix$$name$_.GetNoArena($default_variable$);\n" | 621 " if (has_$name$()) {\n" |
537 " }\n" | 622 " return $oneof_prefix$$name$_.GetNoArena();\n" |
538 " return *$default_variable$;\n" | 623 " }\n" |
539 "}\n" | 624 " return *$default_variable$;\n" |
540 "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n" | 625 "}\n" |
541 " // @@protoc_insertion_point(field_set:$full_name$)\n" | 626 "$inline$void $classname$::set_$name$(const ::std::string& value) {\n" |
542 " if (!has_$name$()) {\n" | 627 " // @@protoc_insertion_point(field_set:$full_name$)\n" |
543 " clear_$oneof_name$();\n" | 628 " if (!has_$name$()) {\n" |
544 " set_has_$name$();\n" | 629 " clear_$oneof_name$();\n" |
545 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" | 630 " set_has_$name$();\n" |
546 " }\n" | 631 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
547 " $oneof_prefix$$name$_.SetNoArena($default_variable$, value);\n" | 632 " }\n" |
548 " // @@protoc_insertion_point(field_set:$full_name$)\n" | 633 " $oneof_prefix$$name$_.SetNoArena($default_variable$, value);\n" |
549 "}\n" | 634 " // @@protoc_insertion_point(field_set:$full_name$)\n" |
550 "$inline$ void $classname$::set_$name$(const char* value) {\n" | 635 "}\n" |
551 " if (!has_$name$()) {\n" | 636 "$inline$void $classname$::set_$name$(const char* value) {\n" |
552 " clear_$oneof_name$();\n" | 637 " if (!has_$name$()) {\n" |
553 " set_has_$name$();\n" | 638 " clear_$oneof_name$();\n" |
554 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" | 639 " set_has_$name$();\n" |
555 " }\n" | 640 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
556 " $oneof_prefix$$name$_.SetNoArena($default_variable$,\n" | 641 " }\n" |
557 " $string_piece$(value));\n" | 642 " $oneof_prefix$$name$_.SetNoArena($default_variable$,\n" |
558 " // @@protoc_insertion_point(field_set_char:$full_name$)\n" | 643 " $string_piece$(value));\n" |
559 "}\n" | 644 " // @@protoc_insertion_point(field_set_char:$full_name$)\n" |
560 "$inline$ " | 645 "}\n" |
561 "void $classname$::set_$name$(const $pointer_type$* value, size_t size) {\
n" | 646 "$inline$" |
562 " if (!has_$name$()) {\n" | 647 "void $classname$::set_$name$(const $pointer_type$* value, size_t " |
563 " clear_$oneof_name$();\n" | 648 "size) {\n" |
564 " set_has_$name$();\n" | 649 " if (!has_$name$()) {\n" |
565 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" | 650 " clear_$oneof_name$();\n" |
566 " }\n" | 651 " set_has_$name$();\n" |
567 " $oneof_prefix$$name$_.SetNoArena($default_variable$, $string_piece$(\n" | 652 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
568 " reinterpret_cast<const char*>(value), size));\n" | 653 " }\n" |
569 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" | 654 " $oneof_prefix$$name$_.SetNoArena($default_variable$, " |
570 "}\n" | 655 "$string_piece$(\n" |
571 "$inline$ ::std::string* $classname$::mutable_$name$() {\n" | 656 " reinterpret_cast<const char*>(value), size));\n" |
572 " if (!has_$name$()) {\n" | 657 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" |
573 " clear_$oneof_name$();\n" | 658 "}\n" |
574 " set_has_$name$();\n" | 659 "$inline$::std::string* $classname$::mutable_$name$() {\n" |
575 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" | 660 " if (!has_$name$()) {\n" |
576 " }\n" | 661 " clear_$oneof_name$();\n" |
577 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" | 662 " set_has_$name$();\n" |
578 " return $oneof_prefix$$name$_.MutableNoArena($default_variable$);\n" | 663 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
579 "}\n" | 664 " }\n" |
580 "$inline$ ::std::string* $classname$::$release_name$() {\n" | 665 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" |
581 " // @@protoc_insertion_point(field_release:$full_name$)\n" | 666 " return $oneof_prefix$$name$_.MutableNoArena($default_variable$);\n" |
582 " if (has_$name$()) {\n" | 667 "}\n" |
583 " clear_has_$oneof_name$();\n" | 668 "$inline$::std::string* $classname$::$release_name$() {\n" |
584 " return $oneof_prefix$$name$_.ReleaseNoArena($default_variable$);\n" | 669 " // @@protoc_insertion_point(field_release:$full_name$)\n" |
585 " } else {\n" | 670 " if (has_$name$()) {\n" |
586 " return NULL;\n" | 671 " clear_has_$oneof_name$();\n" |
587 " }\n" | 672 " return $oneof_prefix$$name$_.ReleaseNoArena($default_variable$);\n" |
588 "}\n" | 673 " } else {\n" |
589 "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\
n" | 674 " return NULL;\n" |
590 " if (!has_$name$()) {\n" | 675 " }\n" |
591 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" | 676 "}\n" |
592 " }\n" | 677 "$inline$void $classname$::set_allocated_$name$(::std::string* $name$) {
\n" |
593 " clear_$oneof_name$();\n" | 678 " if (!has_$name$()) {\n" |
594 " if ($name$ != NULL) {\n" | 679 " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
595 " set_has_$name$();\n" | 680 " }\n" |
596 " $oneof_prefix$$name$_.SetAllocatedNoArena($default_variable$,\n" | 681 " clear_$oneof_name$();\n" |
597 " $name$);\n" | 682 " if ($name$ != NULL) {\n" |
598 " }\n" | 683 " set_has_$name$();\n" |
599 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" | 684 " $oneof_prefix$$name$_.SetAllocatedNoArena($default_variable$,\n" |
600 "}\n"); | 685 " $name$);\n" |
| 686 " }\n" |
| 687 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" |
| 688 "}\n"); |
601 } | 689 } |
602 } | 690 } |
603 | 691 |
604 void StringOneofFieldGenerator:: | 692 void StringOneofFieldGenerator:: |
605 GenerateClearingCode(io::Printer* printer) const { | 693 GenerateClearingCode(io::Printer* printer) const { |
606 map<string, string> variables(variables_); | 694 std::map<string, string> variables(variables_); |
607 if (dependent_field_) { | 695 if (dependent_field_) { |
608 variables["this_message"] = DependentBaseDownCast(); | 696 variables["this_message"] = DependentBaseDownCast(); |
609 // This clearing code may be in the dependent base class. If the default | 697 // This clearing code may be in the dependent base class. If the default |
610 // value is an empty string, then the $default_variable$ is a global | 698 // value is an empty string, then the $default_variable$ is a global |
611 // singleton. If the default is not empty, we need to down-cast to get the | 699 // singleton. If the default is not empty, we need to down-cast to get the |
612 // default value's global singleton instance. See SetStringVariables() for | 700 // default value's global singleton instance. See SetStringVariables() for |
613 // possible values of default_variable. | 701 // possible values of default_variable. |
614 if (!descriptor_->default_value_string().empty()) { | 702 if (!descriptor_->default_value_string().empty()) { |
615 variables["default_variable"] = | 703 variables["default_variable"] = "&" + DependentBaseDownCast() + |
616 DependentBaseDownCast() + variables["default_variable"]; | 704 variables["default_variable_name"] + |
| 705 ".get()"; |
617 } | 706 } |
618 } else { | 707 } else { |
619 variables["this_message"] = ""; | 708 variables["this_message"] = ""; |
620 } | 709 } |
621 if (SupportsArenas(descriptor_)) { | 710 if (SupportsArenas(descriptor_)) { |
622 printer->Print(variables, | 711 printer->Print(variables, |
623 "$this_message$$oneof_prefix$$name$_.Destroy($default_variable$,\n" | 712 "$this_message$$oneof_prefix$$name$_.Destroy($default_variable$,\n" |
624 " $this_message$GetArenaNoVirtual());\n"); | 713 " $this_message$GetArenaNoVirtual());\n"); |
625 } else { | 714 } else { |
626 printer->Print(variables, | 715 printer->Print(variables, |
627 "$this_message$$oneof_prefix$$name$_." | 716 "$this_message$$oneof_prefix$$name$_." |
628 "DestroyNoArena($default_variable$);\n"); | 717 "DestroyNoArena($default_variable$);\n"); |
629 } | 718 } |
630 } | 719 } |
631 | 720 |
632 void StringOneofFieldGenerator:: | 721 void StringOneofFieldGenerator:: |
| 722 GenerateMessageClearingCode(io::Printer* printer) const { |
| 723 return GenerateClearingCode(printer); |
| 724 } |
| 725 |
| 726 void StringOneofFieldGenerator:: |
633 GenerateSwappingCode(io::Printer* printer) const { | 727 GenerateSwappingCode(io::Printer* printer) const { |
634 // Don't print any swapping code. Swapping the union will swap this field. | 728 // Don't print any swapping code. Swapping the union will swap this field. |
635 } | 729 } |
636 | 730 |
637 void StringOneofFieldGenerator:: | 731 void StringOneofFieldGenerator:: |
638 GenerateConstructorCode(io::Printer* printer) const { | 732 GenerateConstructorCode(io::Printer* printer) const { |
639 printer->Print(variables_, | 733 printer->Print( |
640 " $classname$_default_oneof_instance_->$name$_.UnsafeSetDefault(" | 734 variables_, |
641 "$default_variable$);\n"); | 735 "$classname$_default_oneof_instance_.$name$_.UnsafeSetDefault(\n" |
| 736 " $default_variable$);\n"); |
642 } | 737 } |
643 | 738 |
644 void StringOneofFieldGenerator:: | 739 void StringOneofFieldGenerator:: |
645 GenerateDestructorCode(io::Printer* printer) const { | 740 GenerateDestructorCode(io::Printer* printer) const { |
646 if (SupportsArenas(descriptor_)) { | 741 if (SupportsArenas(descriptor_)) { |
647 printer->Print(variables_, | 742 printer->Print(variables_, |
648 "if (has_$name$()) {\n" | 743 "if (has_$name$()) {\n" |
649 " $oneof_prefix$$name$_.Destroy($default_variable$,\n" | 744 " $oneof_prefix$$name$_.Destroy($default_variable$,\n" |
650 " GetArenaNoVirtual());\n" | 745 " GetArenaNoVirtual());\n" |
651 "}\n"); | 746 "}\n"); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
724 if (unknown_ctype) { | 819 if (unknown_ctype) { |
725 printer->Outdent(); | 820 printer->Outdent(); |
726 printer->Print(" public:\n"); | 821 printer->Print(" public:\n"); |
727 printer->Indent(); | 822 printer->Indent(); |
728 } | 823 } |
729 } | 824 } |
730 | 825 |
731 void RepeatedStringFieldGenerator:: | 826 void RepeatedStringFieldGenerator:: |
732 GenerateInlineAccessorDefinitions(io::Printer* printer, | 827 GenerateInlineAccessorDefinitions(io::Printer* printer, |
733 bool is_inline) const { | 828 bool is_inline) const { |
734 map<string, string> variables(variables_); | 829 std::map<string, string> variables(variables_); |
735 variables["inline"] = is_inline ? "inline" : ""; | 830 variables["inline"] = is_inline ? "inline " : ""; |
736 printer->Print(variables, | 831 printer->Print(variables, |
737 "$inline$ const ::std::string& $classname$::$name$(int index) const {\n" | 832 "$inline$const ::std::string& $classname$::$name$(int index) const {\n" |
738 " // @@protoc_insertion_point(field_get:$full_name$)\n" | 833 " // @@protoc_insertion_point(field_get:$full_name$)\n" |
739 " return $name$_.$cppget$(index);\n" | 834 " return $name$_.$cppget$(index);\n" |
740 "}\n" | 835 "}\n" |
741 "$inline$ ::std::string* $classname$::mutable_$name$(int index) {\n" | 836 "$inline$::std::string* $classname$::mutable_$name$(int index) {\n" |
742 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" | 837 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" |
743 " return $name$_.Mutable(index);\n" | 838 " return $name$_.Mutable(index);\n" |
744 "}\n" | 839 "}\n" |
745 "$inline$ void $classname$::set_$name$(int index, const ::std::string& value
) {\n" | 840 "$inline$void $classname$::set_$name$(int index, const ::std::string& value)
{\n" |
746 " // @@protoc_insertion_point(field_set:$full_name$)\n" | 841 " // @@protoc_insertion_point(field_set:$full_name$)\n" |
747 " $name$_.Mutable(index)->assign(value);\n" | 842 " $name$_.Mutable(index)->assign(value);\n" |
748 "}\n" | 843 "}\n" |
749 "$inline$ void $classname$::set_$name$(int index, const char* value) {\n" | 844 "$inline$void $classname$::set_$name$(int index, const char* value) {\n" |
750 " $name$_.Mutable(index)->assign(value);\n" | 845 " $name$_.Mutable(index)->assign(value);\n" |
751 " // @@protoc_insertion_point(field_set_char:$full_name$)\n" | 846 " // @@protoc_insertion_point(field_set_char:$full_name$)\n" |
752 "}\n" | 847 "}\n" |
753 "$inline$ void " | 848 "$inline$void " |
754 "$classname$::set_$name$" | 849 "$classname$::set_$name$" |
755 "(int index, const $pointer_type$* value, size_t size) {\n" | 850 "(int index, const $pointer_type$* value, size_t size) {\n" |
756 " $name$_.Mutable(index)->assign(\n" | 851 " $name$_.Mutable(index)->assign(\n" |
757 " reinterpret_cast<const char*>(value), size);\n" | 852 " reinterpret_cast<const char*>(value), size);\n" |
758 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" | 853 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" |
759 "}\n" | 854 "}\n" |
760 "$inline$ ::std::string* $classname$::add_$name$() {\n" | 855 "$inline$::std::string* $classname$::add_$name$() {\n" |
761 " // @@protoc_insertion_point(field_add_mutable:$full_name$)\n" | 856 " // @@protoc_insertion_point(field_add_mutable:$full_name$)\n" |
762 " return $name$_.Add();\n" | 857 " return $name$_.Add();\n" |
763 "}\n" | 858 "}\n" |
764 "$inline$ void $classname$::add_$name$(const ::std::string& value) {\n" | 859 "$inline$void $classname$::add_$name$(const ::std::string& value) {\n" |
765 " $name$_.Add()->assign(value);\n" | 860 " $name$_.Add()->assign(value);\n" |
766 " // @@protoc_insertion_point(field_add:$full_name$)\n" | 861 " // @@protoc_insertion_point(field_add:$full_name$)\n" |
767 "}\n" | 862 "}\n" |
768 "$inline$ void $classname$::add_$name$(const char* value) {\n" | 863 "$inline$void $classname$::add_$name$(const char* value) {\n" |
769 " $name$_.Add()->assign(value);\n" | 864 " $name$_.Add()->assign(value);\n" |
770 " // @@protoc_insertion_point(field_add_char:$full_name$)\n" | 865 " // @@protoc_insertion_point(field_add_char:$full_name$)\n" |
771 "}\n" | 866 "}\n" |
772 "$inline$ void " | 867 "$inline$void " |
773 "$classname$::add_$name$(const $pointer_type$* value, size_t size) {\n" | 868 "$classname$::add_$name$(const $pointer_type$* value, size_t size) {\n" |
774 " $name$_.Add()->assign(reinterpret_cast<const char*>(value), size);\n" | 869 " $name$_.Add()->assign(reinterpret_cast<const char*>(value), size);\n" |
775 " // @@protoc_insertion_point(field_add_pointer:$full_name$)\n" | 870 " // @@protoc_insertion_point(field_add_pointer:$full_name$)\n" |
776 "}\n"); | 871 "}\n"); |
777 printer->Print(variables, | 872 printer->Print(variables, |
778 "$inline$ const ::google::protobuf::RepeatedPtrField< ::std::string>&\n" | 873 "$inline$const ::google::protobuf::RepeatedPtrField< ::std::string>&\n" |
779 "$classname$::$name$() const {\n" | 874 "$classname$::$name$() const {\n" |
780 " // @@protoc_insertion_point(field_list:$full_name$)\n" | 875 " // @@protoc_insertion_point(field_list:$full_name$)\n" |
781 " return $name$_;\n" | 876 " return $name$_;\n" |
782 "}\n" | 877 "}\n" |
783 "$inline$ ::google::protobuf::RepeatedPtrField< ::std::string>*\n" | 878 "$inline$::google::protobuf::RepeatedPtrField< ::std::string>*\n" |
784 "$classname$::mutable_$name$() {\n" | 879 "$classname$::mutable_$name$() {\n" |
785 " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" | 880 " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" |
786 " return &$name$_;\n" | 881 " return &$name$_;\n" |
787 "}\n"); | 882 "}\n"); |
788 } | 883 } |
789 | 884 |
790 void RepeatedStringFieldGenerator:: | 885 void RepeatedStringFieldGenerator:: |
791 GenerateClearingCode(io::Printer* printer) const { | 886 GenerateClearingCode(io::Printer* printer) const { |
792 printer->Print(variables_, "$name$_.Clear();\n"); | 887 printer->Print(variables_, "$name$_.Clear();\n"); |
793 } | 888 } |
794 | 889 |
795 void RepeatedStringFieldGenerator:: | 890 void RepeatedStringFieldGenerator:: |
796 GenerateMergingCode(io::Printer* printer) const { | 891 GenerateMergingCode(io::Printer* printer) const { |
797 printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n"); | 892 printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n"); |
798 } | 893 } |
799 | 894 |
800 void RepeatedStringFieldGenerator:: | 895 void RepeatedStringFieldGenerator:: |
801 GenerateSwappingCode(io::Printer* printer) const { | 896 GenerateSwappingCode(io::Printer* printer) const { |
802 printer->Print(variables_, "$name$_.UnsafeArenaSwap(&other->$name$_);\n"); | 897 printer->Print(variables_, "$name$_.UnsafeArenaSwap(&other->$name$_);\n"); |
803 } | 898 } |
804 | 899 |
805 void RepeatedStringFieldGenerator:: | 900 void RepeatedStringFieldGenerator:: |
806 GenerateConstructorCode(io::Printer* printer) const { | 901 GenerateConstructorCode(io::Printer* printer) const { |
807 // Not needed for repeated fields. | 902 // Not needed for repeated fields. |
808 } | 903 } |
809 | 904 |
810 void RepeatedStringFieldGenerator:: | 905 void RepeatedStringFieldGenerator:: |
| 906 GenerateCopyConstructorCode(io::Printer* printer) const { |
| 907 printer->Print(variables_, "$name$_.CopyFrom(from.$name$_);"); |
| 908 } |
| 909 |
| 910 void RepeatedStringFieldGenerator:: |
811 GenerateMergeFromCodedStream(io::Printer* printer) const { | 911 GenerateMergeFromCodedStream(io::Printer* printer) const { |
812 printer->Print(variables_, | 912 printer->Print(variables_, |
813 "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n" | 913 "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n" |
814 " input, this->add_$name$()));\n"); | 914 " input, this->add_$name$()));\n"); |
815 if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { | 915 if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { |
816 GenerateUtf8CheckCodeForString( | 916 GenerateUtf8CheckCodeForString( |
817 descriptor_, options_, true, variables_, | 917 descriptor_, options_, true, variables_, |
818 "this->$name$(this->$name$_size() - 1).data(),\n" | 918 "this->$name$(this->$name$_size() - 1).data(),\n" |
819 "this->$name$(this->$name$_size() - 1).length(),\n", | 919 "this->$name$(this->$name$_size() - 1).length(),\n", |
820 printer); | 920 printer); |
(...skipping 30 matching lines...) Expand all Loading... |
851 printer->Outdent(); | 951 printer->Outdent(); |
852 printer->Print(variables_, | 952 printer->Print(variables_, |
853 " target = ::google::protobuf::internal::WireFormatLite::\n" | 953 " target = ::google::protobuf::internal::WireFormatLite::\n" |
854 " Write$declared_type$ToArray($number$, this->$name$(i), target);\n" | 954 " Write$declared_type$ToArray($number$, this->$name$(i), target);\n" |
855 "}\n"); | 955 "}\n"); |
856 } | 956 } |
857 | 957 |
858 void RepeatedStringFieldGenerator:: | 958 void RepeatedStringFieldGenerator:: |
859 GenerateByteSize(io::Printer* printer) const { | 959 GenerateByteSize(io::Printer* printer) const { |
860 printer->Print(variables_, | 960 printer->Print(variables_, |
861 "total_size += $tag_size$ * this->$name$_size();\n" | 961 "total_size += $tag_size$ *\n" |
| 962 " ::google::protobuf::internal::FromIntSize(this->$name$_size());\n" |
862 "for (int i = 0; i < this->$name$_size(); i++) {\n" | 963 "for (int i = 0; i < this->$name$_size(); i++) {\n" |
863 " total_size += ::google::protobuf::internal::WireFormatLite::$declared_typ
e$Size(\n" | 964 " total_size += ::google::protobuf::internal::WireFormatLite::$declared_typ
e$Size(\n" |
864 " this->$name$(i));\n" | 965 " this->$name$(i));\n" |
865 "}\n"); | 966 "}\n"); |
866 } | 967 } |
867 | 968 |
868 } // namespace cpp | 969 } // namespace cpp |
869 } // namespace compiler | 970 } // namespace compiler |
870 } // namespace protobuf | 971 } // namespace protobuf |
871 } // namespace google | 972 } // namespace google |
OLD | NEW |