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 17 matching lines...) Expand all Loading... |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 // Author: kenton@google.com (Kenton Varda) | 31 // Author: kenton@google.com (Kenton Varda) |
32 // Based on original Protocol Buffers design by | 32 // Based on original Protocol Buffers design by |
33 // Sanjay Ghemawat, Jeff Dean, and others. | 33 // Sanjay Ghemawat, Jeff Dean, and others. |
34 | 34 |
35 #include <google/protobuf/compiler/cpp/cpp_enum_field.h> | 35 #include <google/protobuf/compiler/cpp/cpp_enum_field.h> |
36 #include <google/protobuf/compiler/cpp/cpp_helpers.h> | 36 #include <google/protobuf/compiler/cpp/cpp_helpers.h> |
37 #include <google/protobuf/io/printer.h> | 37 #include <google/protobuf/io/printer.h> |
| 38 #include <google/protobuf/wire_format.h> |
38 #include <google/protobuf/stubs/strutil.h> | 39 #include <google/protobuf/stubs/strutil.h> |
39 #include <google/protobuf/wire_format.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 SetEnumVariables(const FieldDescriptor* descriptor, | 48 void SetEnumVariables(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 const EnumValueDescriptor* default_value = descriptor->default_value_enum(); | 52 const EnumValueDescriptor* default_value = descriptor->default_value_enum(); |
53 (*variables)["type"] = ClassName(descriptor->enum_type(), true); | 53 (*variables)["type"] = ClassName(descriptor->enum_type(), true); |
54 (*variables)["default"] = Int32ToString(default_value->number()); | 54 (*variables)["default"] = Int32ToString(default_value->number()); |
55 (*variables)["full_name"] = descriptor->full_name(); | 55 (*variables)["full_name"] = descriptor->full_name(); |
56 } | 56 } |
57 | 57 |
58 } // namespace | 58 } // namespace |
59 | 59 |
(...skipping 15 matching lines...) Expand all Loading... |
75 void EnumFieldGenerator:: | 75 void EnumFieldGenerator:: |
76 GenerateAccessorDeclarations(io::Printer* printer) const { | 76 GenerateAccessorDeclarations(io::Printer* printer) const { |
77 printer->Print(variables_, | 77 printer->Print(variables_, |
78 "$deprecated_attr$$type$ $name$() const;\n" | 78 "$deprecated_attr$$type$ $name$() const;\n" |
79 "$deprecated_attr$void set_$name$($type$ value);\n"); | 79 "$deprecated_attr$void set_$name$($type$ value);\n"); |
80 } | 80 } |
81 | 81 |
82 void EnumFieldGenerator:: | 82 void EnumFieldGenerator:: |
83 GenerateInlineAccessorDefinitions(io::Printer* printer, | 83 GenerateInlineAccessorDefinitions(io::Printer* printer, |
84 bool is_inline) const { | 84 bool is_inline) const { |
85 map<string, string> variables(variables_); | 85 std::map<string, string> variables(variables_); |
86 variables["inline"] = is_inline ? "inline" : ""; | 86 variables["inline"] = is_inline ? "inline " : ""; |
87 printer->Print(variables, | 87 printer->Print(variables, |
88 "$inline$ $type$ $classname$::$name$() const {\n" | 88 "$inline$$type$ $classname$::$name$() const {\n" |
89 " // @@protoc_insertion_point(field_get:$full_name$)\n" | 89 " // @@protoc_insertion_point(field_get:$full_name$)\n" |
90 " return static_cast< $type$ >($name$_);\n" | 90 " return static_cast< $type$ >($name$_);\n" |
91 "}\n" | 91 "}\n" |
92 "$inline$ void $classname$::set_$name$($type$ value) {\n"); | 92 "$inline$void $classname$::set_$name$($type$ value) {\n"); |
93 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { | 93 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
94 printer->Print(variables, | 94 printer->Print(variables, |
95 " assert($type$_IsValid(value));\n"); | 95 " assert($type$_IsValid(value));\n"); |
96 } | 96 } |
97 printer->Print(variables, | 97 printer->Print(variables, |
98 " $set_hasbit$\n" | 98 " $set_hasbit$\n" |
99 " $name$_ = value;\n" | 99 " $name$_ = value;\n" |
100 " // @@protoc_insertion_point(field_set:$full_name$)\n" | 100 " // @@protoc_insertion_point(field_set:$full_name$)\n" |
101 "}\n"); | 101 "}\n"); |
102 } | 102 } |
(...skipping 12 matching lines...) Expand all Loading... |
115 GenerateSwappingCode(io::Printer* printer) const { | 115 GenerateSwappingCode(io::Printer* printer) const { |
116 printer->Print(variables_, "std::swap($name$_, other->$name$_);\n"); | 116 printer->Print(variables_, "std::swap($name$_, other->$name$_);\n"); |
117 } | 117 } |
118 | 118 |
119 void EnumFieldGenerator:: | 119 void EnumFieldGenerator:: |
120 GenerateConstructorCode(io::Printer* printer) const { | 120 GenerateConstructorCode(io::Printer* printer) const { |
121 printer->Print(variables_, "$name$_ = $default$;\n"); | 121 printer->Print(variables_, "$name$_ = $default$;\n"); |
122 } | 122 } |
123 | 123 |
124 void EnumFieldGenerator:: | 124 void EnumFieldGenerator:: |
| 125 GenerateCopyConstructorCode(io::Printer* printer) const { |
| 126 printer->Print(variables_, "$name$_ = from.$name$_;\n"); |
| 127 } |
| 128 |
| 129 void EnumFieldGenerator:: |
125 GenerateMergeFromCodedStream(io::Printer* printer) const { | 130 GenerateMergeFromCodedStream(io::Printer* printer) const { |
126 printer->Print(variables_, | 131 printer->Print(variables_, |
127 "int value;\n" | 132 "int value;\n" |
128 "DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n" | 133 "DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n" |
129 " int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n" | 134 " int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n" |
130 " input, &value)));\n"); | 135 " input, &value)));\n"); |
131 if (HasPreservingUnknownEnumSemantics(descriptor_->file())) { | 136 if (HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
132 printer->Print(variables_, | 137 printer->Print(variables_, |
133 "set_$name$(static_cast< $type$ >(value));\n"); | 138 "set_$name$(static_cast< $type$ >(value));\n"); |
134 } else { | 139 } else { |
135 printer->Print(variables_, | 140 printer->Print(variables_, |
136 "if ($type$_IsValid(value)) {\n" | 141 "if ($type$_IsValid(value)) {\n" |
137 " set_$name$(static_cast< $type$ >(value));\n"); | 142 " set_$name$(static_cast< $type$ >(value));\n"); |
138 if (UseUnknownFieldSet(descriptor_->file(), options_)) { | 143 if (UseUnknownFieldSet(descriptor_->file(), options_)) { |
139 printer->Print(variables_, | 144 printer->Print(variables_, |
140 "} else {\n" | 145 "} else {\n" |
141 " mutable_unknown_fields()->AddVarint($number$, value);\n"); | 146 " mutable_unknown_fields()->AddVarint($number$, value);\n"); |
142 } else { | 147 } else { |
143 printer->Print( | 148 printer->Print( |
144 "} else {\n" | 149 "} else {\n" |
145 " unknown_fields_stream.WriteVarint32($tag$);\n" | 150 " unknown_fields_stream.WriteVarint32($tag$u);\n" |
146 " unknown_fields_stream.WriteVarint32(value);\n", | 151 " unknown_fields_stream.WriteVarint32(value);\n", |
147 "tag", SimpleItoa(internal::WireFormat::MakeTag(descriptor_))); | 152 "tag", SimpleItoa(internal::WireFormat::MakeTag(descriptor_))); |
148 } | 153 } |
149 printer->Print(variables_, | 154 printer->Print(variables_, |
150 "}\n"); | 155 "}\n"); |
151 } | 156 } |
152 } | 157 } |
153 | 158 |
154 void EnumFieldGenerator:: | 159 void EnumFieldGenerator:: |
155 GenerateSerializeWithCachedSizes(io::Printer* printer) const { | 160 GenerateSerializeWithCachedSizes(io::Printer* printer) const { |
(...skipping 23 matching lines...) Expand all Loading... |
179 const Options& options) | 184 const Options& options) |
180 : EnumFieldGenerator(descriptor, options) { | 185 : EnumFieldGenerator(descriptor, options) { |
181 SetCommonOneofFieldVariables(descriptor, &variables_); | 186 SetCommonOneofFieldVariables(descriptor, &variables_); |
182 } | 187 } |
183 | 188 |
184 EnumOneofFieldGenerator::~EnumOneofFieldGenerator() {} | 189 EnumOneofFieldGenerator::~EnumOneofFieldGenerator() {} |
185 | 190 |
186 void EnumOneofFieldGenerator:: | 191 void EnumOneofFieldGenerator:: |
187 GenerateInlineAccessorDefinitions(io::Printer* printer, | 192 GenerateInlineAccessorDefinitions(io::Printer* printer, |
188 bool is_inline) const { | 193 bool is_inline) const { |
189 map<string, string> variables(variables_); | 194 std::map<string, string> variables(variables_); |
190 variables["inline"] = is_inline ? "inline" : ""; | 195 variables["inline"] = is_inline ? "inline " : ""; |
191 printer->Print(variables, | 196 printer->Print(variables, |
192 "$inline$ $type$ $classname$::$name$() const {\n" | 197 "$inline$$type$ $classname$::$name$() const {\n" |
193 " // @@protoc_insertion_point(field_get:$full_name$)\n" | 198 " // @@protoc_insertion_point(field_get:$full_name$)\n" |
194 " if (has_$name$()) {\n" | 199 " if (has_$name$()) {\n" |
195 " return static_cast< $type$ >($oneof_prefix$$name$_);\n" | 200 " return static_cast< $type$ >($oneof_prefix$$name$_);\n" |
196 " }\n" | 201 " }\n" |
197 " return static_cast< $type$ >($default$);\n" | 202 " return static_cast< $type$ >($default$);\n" |
198 "}\n" | 203 "}\n" |
199 "$inline$ void $classname$::set_$name$($type$ value) {\n"); | 204 "$inline$void $classname$::set_$name$($type$ value) {\n"); |
200 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { | 205 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
201 printer->Print(variables, | 206 printer->Print(variables, |
202 " assert($type$_IsValid(value));\n"); | 207 " assert($type$_IsValid(value));\n"); |
203 } | 208 } |
204 printer->Print(variables, | 209 printer->Print(variables, |
205 " if (!has_$name$()) {\n" | 210 " if (!has_$name$()) {\n" |
206 " clear_$oneof_name$();\n" | 211 " clear_$oneof_name$();\n" |
207 " set_has_$name$();\n" | 212 " set_has_$name$();\n" |
208 " }\n" | 213 " }\n" |
209 " $oneof_prefix$$name$_ = value;\n" | 214 " $oneof_prefix$$name$_ = value;\n" |
210 " // @@protoc_insertion_point(field_set:$full_name$)\n" | 215 " // @@protoc_insertion_point(field_set:$full_name$)\n" |
211 "}\n"); | 216 "}\n"); |
212 } | 217 } |
213 | 218 |
214 void EnumOneofFieldGenerator:: | 219 void EnumOneofFieldGenerator:: |
215 GenerateClearingCode(io::Printer* printer) const { | 220 GenerateClearingCode(io::Printer* printer) const { |
216 printer->Print(variables_, "$oneof_prefix$$name$_ = $default$;\n"); | 221 printer->Print(variables_, "$oneof_prefix$$name$_ = $default$;\n"); |
217 } | 222 } |
218 | 223 |
219 void EnumOneofFieldGenerator:: | 224 void EnumOneofFieldGenerator:: |
220 GenerateSwappingCode(io::Printer* printer) const { | 225 GenerateSwappingCode(io::Printer* printer) const { |
221 // Don't print any swapping code. Swapping the union will swap this field. | 226 // Don't print any swapping code. Swapping the union will swap this field. |
222 } | 227 } |
223 | 228 |
224 void EnumOneofFieldGenerator:: | 229 void EnumOneofFieldGenerator:: |
225 GenerateConstructorCode(io::Printer* printer) const { | 230 GenerateConstructorCode(io::Printer* printer) const { |
226 printer->Print(variables_, | 231 printer->Print( |
227 " $classname$_default_oneof_instance_->$name$_ = $default$;\n"); | 232 variables_, |
| 233 " $classname$_default_oneof_instance_.$name$_ = $default$;\n"); |
228 } | 234 } |
229 | 235 |
230 // =================================================================== | 236 // =================================================================== |
231 | 237 |
232 RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator( | 238 RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator( |
233 const FieldDescriptor* descriptor, const Options& options) | 239 const FieldDescriptor* descriptor, const Options& options) |
234 : FieldGenerator(options), descriptor_(descriptor) { | 240 : FieldGenerator(options), descriptor_(descriptor) { |
235 SetEnumVariables(descriptor, &variables_, options); | 241 SetEnumVariables(descriptor, &variables_, options); |
236 } | 242 } |
237 | 243 |
(...skipping 17 matching lines...) Expand all Loading... |
255 "$deprecated_attr$void set_$name$(int index, $type$ value);\n" | 261 "$deprecated_attr$void set_$name$(int index, $type$ value);\n" |
256 "$deprecated_attr$void add_$name$($type$ value);\n"); | 262 "$deprecated_attr$void add_$name$($type$ value);\n"); |
257 printer->Print(variables_, | 263 printer->Print(variables_, |
258 "$deprecated_attr$const ::google::protobuf::RepeatedField<int>& $name$() con
st;\n" | 264 "$deprecated_attr$const ::google::protobuf::RepeatedField<int>& $name$() con
st;\n" |
259 "$deprecated_attr$::google::protobuf::RepeatedField<int>* mutable_$name$();\
n"); | 265 "$deprecated_attr$::google::protobuf::RepeatedField<int>* mutable_$name$();\
n"); |
260 } | 266 } |
261 | 267 |
262 void RepeatedEnumFieldGenerator:: | 268 void RepeatedEnumFieldGenerator:: |
263 GenerateInlineAccessorDefinitions(io::Printer* printer, | 269 GenerateInlineAccessorDefinitions(io::Printer* printer, |
264 bool is_inline) const { | 270 bool is_inline) const { |
265 map<string, string> variables(variables_); | 271 std::map<string, string> variables(variables_); |
266 variables["inline"] = is_inline ? "inline" : ""; | 272 variables["inline"] = is_inline ? "inline " : ""; |
267 printer->Print(variables, | 273 printer->Print(variables, |
268 "$inline$ $type$ $classname$::$name$(int index) const {\n" | 274 "$inline$$type$ $classname$::$name$(int index) const {\n" |
269 " // @@protoc_insertion_point(field_get:$full_name$)\n" | 275 " // @@protoc_insertion_point(field_get:$full_name$)\n" |
270 " return static_cast< $type$ >($name$_.Get(index));\n" | 276 " return static_cast< $type$ >($name$_.Get(index));\n" |
271 "}\n" | 277 "}\n" |
272 "$inline$ void $classname$::set_$name$(int index, $type$ value) {\n"); | 278 "$inline$void $classname$::set_$name$(int index, $type$ value) {\n"); |
273 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { | 279 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
274 printer->Print(variables, | 280 printer->Print(variables, |
275 " assert($type$_IsValid(value));\n"); | 281 " assert($type$_IsValid(value));\n"); |
276 } | 282 } |
277 printer->Print(variables, | 283 printer->Print(variables, |
278 " $name$_.Set(index, value);\n" | 284 " $name$_.Set(index, value);\n" |
279 " // @@protoc_insertion_point(field_set:$full_name$)\n" | 285 " // @@protoc_insertion_point(field_set:$full_name$)\n" |
280 "}\n" | 286 "}\n" |
281 "$inline$ void $classname$::add_$name$($type$ value) {\n"); | 287 "$inline$void $classname$::add_$name$($type$ value) {\n"); |
282 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { | 288 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
283 printer->Print(variables, | 289 printer->Print(variables, |
284 " assert($type$_IsValid(value));\n"); | 290 " assert($type$_IsValid(value));\n"); |
285 } | 291 } |
286 printer->Print(variables, | 292 printer->Print(variables, |
287 " $name$_.Add(value);\n" | 293 " $name$_.Add(value);\n" |
288 " // @@protoc_insertion_point(field_add:$full_name$)\n" | 294 " // @@protoc_insertion_point(field_add:$full_name$)\n" |
289 "}\n"); | 295 "}\n"); |
290 printer->Print(variables, | 296 printer->Print(variables, |
291 "$inline$ const ::google::protobuf::RepeatedField<int>&\n" | 297 "$inline$const ::google::protobuf::RepeatedField<int>&\n" |
292 "$classname$::$name$() const {\n" | 298 "$classname$::$name$() const {\n" |
293 " // @@protoc_insertion_point(field_list:$full_name$)\n" | 299 " // @@protoc_insertion_point(field_list:$full_name$)\n" |
294 " return $name$_;\n" | 300 " return $name$_;\n" |
295 "}\n" | 301 "}\n" |
296 "$inline$ ::google::protobuf::RepeatedField<int>*\n" | 302 "$inline$::google::protobuf::RepeatedField<int>*\n" |
297 "$classname$::mutable_$name$() {\n" | 303 "$classname$::mutable_$name$() {\n" |
298 " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" | 304 " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" |
299 " return &$name$_;\n" | 305 " return &$name$_;\n" |
300 "}\n"); | 306 "}\n"); |
301 } | 307 } |
302 | 308 |
303 void RepeatedEnumFieldGenerator:: | 309 void RepeatedEnumFieldGenerator:: |
304 GenerateClearingCode(io::Printer* printer) const { | 310 GenerateClearingCode(io::Printer* printer) const { |
305 printer->Print(variables_, "$name$_.Clear();\n"); | 311 printer->Print(variables_, "$name$_.Clear();\n"); |
306 } | 312 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 " target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray
(\n" | 474 " target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray
(\n" |
469 " $number$, this->$name$(i), target);\n"); | 475 " $number$, this->$name$(i), target);\n"); |
470 } | 476 } |
471 printer->Print("}\n"); | 477 printer->Print("}\n"); |
472 } | 478 } |
473 | 479 |
474 void RepeatedEnumFieldGenerator:: | 480 void RepeatedEnumFieldGenerator:: |
475 GenerateByteSize(io::Printer* printer) const { | 481 GenerateByteSize(io::Printer* printer) const { |
476 printer->Print(variables_, | 482 printer->Print(variables_, |
477 "{\n" | 483 "{\n" |
478 " int data_size = 0;\n"); | 484 " size_t data_size = 0;\n" |
| 485 " unsigned int count = this->$name$_size();"); |
479 printer->Indent(); | 486 printer->Indent(); |
480 printer->Print(variables_, | 487 printer->Print(variables_, |
481 "for (int i = 0; i < this->$name$_size(); i++) {\n" | 488 "for (unsigned int i = 0; i < count; i++) {\n" |
482 " data_size += ::google::protobuf::internal::WireFormatLite::EnumSize(\n" | 489 " data_size += ::google::protobuf::internal::WireFormatLite::EnumSize(\n" |
483 " this->$name$(i));\n" | 490 " this->$name$(i));\n" |
484 "}\n"); | 491 "}\n"); |
485 | 492 |
486 if (descriptor_->is_packed()) { | 493 if (descriptor_->is_packed()) { |
487 printer->Print(variables_, | 494 printer->Print(variables_, |
488 "if (data_size > 0) {\n" | 495 "if (data_size > 0) {\n" |
489 " total_size += $tag_size$ +\n" | 496 " total_size += $tag_size$ +\n" |
490 " ::google::protobuf::internal::WireFormatLite::Int32Size(data_size);\n
" | 497 " ::google::protobuf::internal::WireFormatLite::Int32Size(data_size);\n
" |
491 "}\n" | 498 "}\n" |
| 499 "int cached_size = ::google::protobuf::internal::ToCachedSize(data_size);\
n" |
492 "GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n" | 500 "GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n" |
493 "_$name$_cached_byte_size_ = data_size;\n" | 501 "_$name$_cached_byte_size_ = cached_size;\n" |
494 "GOOGLE_SAFE_CONCURRENT_WRITES_END();\n" | 502 "GOOGLE_SAFE_CONCURRENT_WRITES_END();\n" |
495 "total_size += data_size;\n"); | 503 "total_size += data_size;\n"); |
496 } else { | 504 } else { |
497 printer->Print(variables_, | 505 printer->Print(variables_, |
498 "total_size += $tag_size$ * this->$name$_size() + data_size;\n"); | 506 "total_size += ($tag_size$UL * count) + data_size;\n"); |
499 } | 507 } |
500 printer->Outdent(); | 508 printer->Outdent(); |
501 printer->Print("}\n"); | 509 printer->Print("}\n"); |
502 } | 510 } |
503 | 511 |
504 } // namespace cpp | 512 } // namespace cpp |
505 } // namespace compiler | 513 } // namespace compiler |
506 } // namespace protobuf | 514 } // namespace protobuf |
507 } // namespace google | 515 } // namespace google |
OLD | NEW |