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