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/stubs/strutil.h> |
38 #include <google/protobuf/wire_format.h> | 39 #include <google/protobuf/wire_format.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 SetEnumVariables(const FieldDescriptor* descriptor, | 48 void SetEnumVariables(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 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 std::map<string, string> variables(variables_); | 85 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:: | |
130 GenerateMergeFromCodedStream(io::Printer* printer) const { | 125 GenerateMergeFromCodedStream(io::Printer* printer) const { |
131 printer->Print(variables_, | 126 printer->Print(variables_, |
132 "int value;\n" | 127 "int value;\n" |
133 "DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n" | 128 "DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n" |
134 " int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n" | 129 " int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n" |
135 " input, &value)));\n"); | 130 " input, &value)));\n"); |
136 if (HasPreservingUnknownEnumSemantics(descriptor_->file())) { | 131 if (HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
137 printer->Print(variables_, | 132 printer->Print(variables_, |
138 "set_$name$(static_cast< $type$ >(value));\n"); | 133 "set_$name$(static_cast< $type$ >(value));\n"); |
139 } else { | 134 } else { |
140 printer->Print(variables_, | 135 printer->Print(variables_, |
141 "if ($type$_IsValid(value)) {\n" | 136 "if ($type$_IsValid(value)) {\n" |
142 " set_$name$(static_cast< $type$ >(value));\n"); | 137 " set_$name$(static_cast< $type$ >(value));\n"); |
143 if (UseUnknownFieldSet(descriptor_->file(), options_)) { | 138 if (UseUnknownFieldSet(descriptor_->file(), options_)) { |
144 printer->Print(variables_, | 139 printer->Print(variables_, |
145 "} else {\n" | 140 "} else {\n" |
146 " mutable_unknown_fields()->AddVarint($number$, value);\n"); | 141 " mutable_unknown_fields()->AddVarint($number$, value);\n"); |
147 } else { | 142 } else { |
148 printer->Print( | 143 printer->Print( |
149 "} else {\n" | 144 "} else {\n" |
150 " unknown_fields_stream.WriteVarint32($tag$u);\n" | 145 " unknown_fields_stream.WriteVarint32($tag$);\n" |
151 " unknown_fields_stream.WriteVarint32(value);\n", | 146 " unknown_fields_stream.WriteVarint32(value);\n", |
152 "tag", SimpleItoa(internal::WireFormat::MakeTag(descriptor_))); | 147 "tag", SimpleItoa(internal::WireFormat::MakeTag(descriptor_))); |
153 } | 148 } |
154 printer->Print(variables_, | 149 printer->Print(variables_, |
155 "}\n"); | 150 "}\n"); |
156 } | 151 } |
157 } | 152 } |
158 | 153 |
159 void EnumFieldGenerator:: | 154 void EnumFieldGenerator:: |
160 GenerateSerializeWithCachedSizes(io::Printer* printer) const { | 155 GenerateSerializeWithCachedSizes(io::Printer* printer) const { |
(...skipping 23 matching lines...) Expand all Loading... |
184 const Options& options) | 179 const Options& options) |
185 : EnumFieldGenerator(descriptor, options) { | 180 : EnumFieldGenerator(descriptor, options) { |
186 SetCommonOneofFieldVariables(descriptor, &variables_); | 181 SetCommonOneofFieldVariables(descriptor, &variables_); |
187 } | 182 } |
188 | 183 |
189 EnumOneofFieldGenerator::~EnumOneofFieldGenerator() {} | 184 EnumOneofFieldGenerator::~EnumOneofFieldGenerator() {} |
190 | 185 |
191 void EnumOneofFieldGenerator:: | 186 void EnumOneofFieldGenerator:: |
192 GenerateInlineAccessorDefinitions(io::Printer* printer, | 187 GenerateInlineAccessorDefinitions(io::Printer* printer, |
193 bool is_inline) const { | 188 bool is_inline) const { |
194 std::map<string, string> variables(variables_); | 189 map<string, string> variables(variables_); |
195 variables["inline"] = is_inline ? "inline " : ""; | 190 variables["inline"] = is_inline ? "inline" : ""; |
196 printer->Print(variables, | 191 printer->Print(variables, |
197 "$inline$$type$ $classname$::$name$() const {\n" | 192 "$inline$ $type$ $classname$::$name$() const {\n" |
198 " // @@protoc_insertion_point(field_get:$full_name$)\n" | 193 " // @@protoc_insertion_point(field_get:$full_name$)\n" |
199 " if (has_$name$()) {\n" | 194 " if (has_$name$()) {\n" |
200 " return static_cast< $type$ >($oneof_prefix$$name$_);\n" | 195 " return static_cast< $type$ >($oneof_prefix$$name$_);\n" |
201 " }\n" | 196 " }\n" |
202 " return static_cast< $type$ >($default$);\n" | 197 " return static_cast< $type$ >($default$);\n" |
203 "}\n" | 198 "}\n" |
204 "$inline$void $classname$::set_$name$($type$ value) {\n"); | 199 "$inline$ void $classname$::set_$name$($type$ value) {\n"); |
205 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { | 200 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
206 printer->Print(variables, | 201 printer->Print(variables, |
207 " assert($type$_IsValid(value));\n"); | 202 " assert($type$_IsValid(value));\n"); |
208 } | 203 } |
209 printer->Print(variables, | 204 printer->Print(variables, |
210 " if (!has_$name$()) {\n" | 205 " if (!has_$name$()) {\n" |
211 " clear_$oneof_name$();\n" | 206 " clear_$oneof_name$();\n" |
212 " set_has_$name$();\n" | 207 " set_has_$name$();\n" |
213 " }\n" | 208 " }\n" |
214 " $oneof_prefix$$name$_ = value;\n" | 209 " $oneof_prefix$$name$_ = value;\n" |
215 " // @@protoc_insertion_point(field_set:$full_name$)\n" | 210 " // @@protoc_insertion_point(field_set:$full_name$)\n" |
216 "}\n"); | 211 "}\n"); |
217 } | 212 } |
218 | 213 |
219 void EnumOneofFieldGenerator:: | 214 void EnumOneofFieldGenerator:: |
220 GenerateClearingCode(io::Printer* printer) const { | 215 GenerateClearingCode(io::Printer* printer) const { |
221 printer->Print(variables_, "$oneof_prefix$$name$_ = $default$;\n"); | 216 printer->Print(variables_, "$oneof_prefix$$name$_ = $default$;\n"); |
222 } | 217 } |
223 | 218 |
224 void EnumOneofFieldGenerator:: | 219 void EnumOneofFieldGenerator:: |
225 GenerateSwappingCode(io::Printer* printer) const { | 220 GenerateSwappingCode(io::Printer* printer) const { |
226 // Don't print any swapping code. Swapping the union will swap this field. | 221 // Don't print any swapping code. Swapping the union will swap this field. |
227 } | 222 } |
228 | 223 |
229 void EnumOneofFieldGenerator:: | 224 void EnumOneofFieldGenerator:: |
230 GenerateConstructorCode(io::Printer* printer) const { | 225 GenerateConstructorCode(io::Printer* printer) const { |
231 printer->Print( | 226 printer->Print(variables_, |
232 variables_, | 227 " $classname$_default_oneof_instance_->$name$_ = $default$;\n"); |
233 " $classname$_default_oneof_instance_.$name$_ = $default$;\n"); | |
234 } | 228 } |
235 | 229 |
236 // =================================================================== | 230 // =================================================================== |
237 | 231 |
238 RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator( | 232 RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator( |
239 const FieldDescriptor* descriptor, const Options& options) | 233 const FieldDescriptor* descriptor, const Options& options) |
240 : FieldGenerator(options), descriptor_(descriptor) { | 234 : FieldGenerator(options), descriptor_(descriptor) { |
241 SetEnumVariables(descriptor, &variables_, options); | 235 SetEnumVariables(descriptor, &variables_, options); |
242 } | 236 } |
243 | 237 |
(...skipping 17 matching lines...) Expand all Loading... |
261 "$deprecated_attr$void set_$name$(int index, $type$ value);\n" | 255 "$deprecated_attr$void set_$name$(int index, $type$ value);\n" |
262 "$deprecated_attr$void add_$name$($type$ value);\n"); | 256 "$deprecated_attr$void add_$name$($type$ value);\n"); |
263 printer->Print(variables_, | 257 printer->Print(variables_, |
264 "$deprecated_attr$const ::google::protobuf::RepeatedField<int>& $name$() con
st;\n" | 258 "$deprecated_attr$const ::google::protobuf::RepeatedField<int>& $name$() con
st;\n" |
265 "$deprecated_attr$::google::protobuf::RepeatedField<int>* mutable_$name$();\
n"); | 259 "$deprecated_attr$::google::protobuf::RepeatedField<int>* mutable_$name$();\
n"); |
266 } | 260 } |
267 | 261 |
268 void RepeatedEnumFieldGenerator:: | 262 void RepeatedEnumFieldGenerator:: |
269 GenerateInlineAccessorDefinitions(io::Printer* printer, | 263 GenerateInlineAccessorDefinitions(io::Printer* printer, |
270 bool is_inline) const { | 264 bool is_inline) const { |
271 std::map<string, string> variables(variables_); | 265 map<string, string> variables(variables_); |
272 variables["inline"] = is_inline ? "inline " : ""; | 266 variables["inline"] = is_inline ? "inline" : ""; |
273 printer->Print(variables, | 267 printer->Print(variables, |
274 "$inline$$type$ $classname$::$name$(int index) const {\n" | 268 "$inline$ $type$ $classname$::$name$(int index) const {\n" |
275 " // @@protoc_insertion_point(field_get:$full_name$)\n" | 269 " // @@protoc_insertion_point(field_get:$full_name$)\n" |
276 " return static_cast< $type$ >($name$_.Get(index));\n" | 270 " return static_cast< $type$ >($name$_.Get(index));\n" |
277 "}\n" | 271 "}\n" |
278 "$inline$void $classname$::set_$name$(int index, $type$ value) {\n"); | 272 "$inline$ void $classname$::set_$name$(int index, $type$ value) {\n"); |
279 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { | 273 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
280 printer->Print(variables, | 274 printer->Print(variables, |
281 " assert($type$_IsValid(value));\n"); | 275 " assert($type$_IsValid(value));\n"); |
282 } | 276 } |
283 printer->Print(variables, | 277 printer->Print(variables, |
284 " $name$_.Set(index, value);\n" | 278 " $name$_.Set(index, value);\n" |
285 " // @@protoc_insertion_point(field_set:$full_name$)\n" | 279 " // @@protoc_insertion_point(field_set:$full_name$)\n" |
286 "}\n" | 280 "}\n" |
287 "$inline$void $classname$::add_$name$($type$ value) {\n"); | 281 "$inline$ void $classname$::add_$name$($type$ value) {\n"); |
288 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { | 282 if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
289 printer->Print(variables, | 283 printer->Print(variables, |
290 " assert($type$_IsValid(value));\n"); | 284 " assert($type$_IsValid(value));\n"); |
291 } | 285 } |
292 printer->Print(variables, | 286 printer->Print(variables, |
293 " $name$_.Add(value);\n" | 287 " $name$_.Add(value);\n" |
294 " // @@protoc_insertion_point(field_add:$full_name$)\n" | 288 " // @@protoc_insertion_point(field_add:$full_name$)\n" |
295 "}\n"); | 289 "}\n"); |
296 printer->Print(variables, | 290 printer->Print(variables, |
297 "$inline$const ::google::protobuf::RepeatedField<int>&\n" | 291 "$inline$ const ::google::protobuf::RepeatedField<int>&\n" |
298 "$classname$::$name$() const {\n" | 292 "$classname$::$name$() const {\n" |
299 " // @@protoc_insertion_point(field_list:$full_name$)\n" | 293 " // @@protoc_insertion_point(field_list:$full_name$)\n" |
300 " return $name$_;\n" | 294 " return $name$_;\n" |
301 "}\n" | 295 "}\n" |
302 "$inline$::google::protobuf::RepeatedField<int>*\n" | 296 "$inline$ ::google::protobuf::RepeatedField<int>*\n" |
303 "$classname$::mutable_$name$() {\n" | 297 "$classname$::mutable_$name$() {\n" |
304 " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" | 298 " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" |
305 " return &$name$_;\n" | 299 " return &$name$_;\n" |
306 "}\n"); | 300 "}\n"); |
307 } | 301 } |
308 | 302 |
309 void RepeatedEnumFieldGenerator:: | 303 void RepeatedEnumFieldGenerator:: |
310 GenerateClearingCode(io::Printer* printer) const { | 304 GenerateClearingCode(io::Printer* printer) const { |
311 printer->Print(variables_, "$name$_.Clear();\n"); | 305 printer->Print(variables_, "$name$_.Clear();\n"); |
312 } | 306 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 " target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray
(\n" | 468 " target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray
(\n" |
475 " $number$, this->$name$(i), target);\n"); | 469 " $number$, this->$name$(i), target);\n"); |
476 } | 470 } |
477 printer->Print("}\n"); | 471 printer->Print("}\n"); |
478 } | 472 } |
479 | 473 |
480 void RepeatedEnumFieldGenerator:: | 474 void RepeatedEnumFieldGenerator:: |
481 GenerateByteSize(io::Printer* printer) const { | 475 GenerateByteSize(io::Printer* printer) const { |
482 printer->Print(variables_, | 476 printer->Print(variables_, |
483 "{\n" | 477 "{\n" |
484 " size_t data_size = 0;\n" | 478 " int data_size = 0;\n"); |
485 " unsigned int count = this->$name$_size();"); | |
486 printer->Indent(); | 479 printer->Indent(); |
487 printer->Print(variables_, | 480 printer->Print(variables_, |
488 "for (unsigned int i = 0; i < count; i++) {\n" | 481 "for (int i = 0; i < this->$name$_size(); i++) {\n" |
489 " data_size += ::google::protobuf::internal::WireFormatLite::EnumSize(\n" | 482 " data_size += ::google::protobuf::internal::WireFormatLite::EnumSize(\n" |
490 " this->$name$(i));\n" | 483 " this->$name$(i));\n" |
491 "}\n"); | 484 "}\n"); |
492 | 485 |
493 if (descriptor_->is_packed()) { | 486 if (descriptor_->is_packed()) { |
494 printer->Print(variables_, | 487 printer->Print(variables_, |
495 "if (data_size > 0) {\n" | 488 "if (data_size > 0) {\n" |
496 " total_size += $tag_size$ +\n" | 489 " total_size += $tag_size$ +\n" |
497 " ::google::protobuf::internal::WireFormatLite::Int32Size(data_size);\n
" | 490 " ::google::protobuf::internal::WireFormatLite::Int32Size(data_size);\n
" |
498 "}\n" | 491 "}\n" |
499 "int cached_size = ::google::protobuf::internal::ToCachedSize(data_size);\
n" | |
500 "GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n" | 492 "GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n" |
501 "_$name$_cached_byte_size_ = cached_size;\n" | 493 "_$name$_cached_byte_size_ = data_size;\n" |
502 "GOOGLE_SAFE_CONCURRENT_WRITES_END();\n" | 494 "GOOGLE_SAFE_CONCURRENT_WRITES_END();\n" |
503 "total_size += data_size;\n"); | 495 "total_size += data_size;\n"); |
504 } else { | 496 } else { |
505 printer->Print(variables_, | 497 printer->Print(variables_, |
506 "total_size += ($tag_size$UL * count) + data_size;\n"); | 498 "total_size += $tag_size$ * this->$name$_size() + data_size;\n"); |
507 } | 499 } |
508 printer->Outdent(); | 500 printer->Outdent(); |
509 printer->Print("}\n"); | 501 printer->Print("}\n"); |
510 } | 502 } |
511 | 503 |
512 } // namespace cpp | 504 } // namespace cpp |
513 } // namespace compiler | 505 } // namespace compiler |
514 } // namespace protobuf | 506 } // namespace protobuf |
515 } // namespace google | 507 } // namespace google |
OLD | NEW |