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