35
35
#include <google/protobuf/compiler/cpp/cpp_enum_field.h>
36
36
#include <google/protobuf/compiler/cpp/cpp_helpers.h>
37
37
#include <google/protobuf/io/printer.h>
38
#include <google/protobuf/wire_format_inl.h>
38
#include <google/protobuf/descriptor.pb.h>
39
39
#include <google/protobuf/stubs/strutil.h>
43
43
namespace compiler {
46
using internal::WireFormat;
50
// TODO(kenton): Factor out a "SetCommonFieldVariables()" to get rid of
51
// repeat code between this and the other field types.
52
48
void SetEnumVariables(const FieldDescriptor* descriptor,
53
49
map<string, string>* variables) {
50
SetCommonFieldVariables(descriptor, variables);
54
51
const EnumValueDescriptor* default_value = descriptor->default_value_enum();
56
(*variables)["name"] = FieldName(descriptor);
57
52
(*variables)["type"] = ClassName(descriptor->enum_type(), true);
58
53
(*variables)["default"] = SimpleItoa(default_value->number());
59
(*variables)["index"] = SimpleItoa(descriptor->index());
60
(*variables)["number"] = SimpleItoa(descriptor->number());
61
(*variables)["classname"] = ClassName(FieldScope(descriptor), false);
62
(*variables)["tag_size"] = SimpleItoa(
63
WireFormat::TagSize(descriptor->number(), descriptor->type()));
83
73
void EnumFieldGenerator::
84
74
GenerateAccessorDeclarations(io::Printer* printer) const {
85
75
printer->Print(variables_,
86
"inline $type$ $name$() const;\n"
87
"inline void set_$name$($type$ value);\n");
76
"inline $type$ $name$() const$deprecation$;\n"
77
"inline void set_$name$($type$ value)$deprecation$;\n");
90
80
void EnumFieldGenerator::
124
114
GenerateMergeFromCodedStream(io::Printer* printer) const {
125
115
printer->Print(variables_,
127
"DO_(::google::protobuf::internal::WireFormat::ReadEnum(input, &value));\n"
117
"DO_(::google::protobuf::internal::WireFormatLite::ReadEnum(input, &value));\n"
128
118
"if ($type$_IsValid(value)) {\n"
129
" set_$name$(static_cast< $type$ >(value));\n"
131
" mutable_unknown_fields()->AddVarint($number$, value);\n"
119
" set_$name$(static_cast< $type$ >(value));\n");
120
if (HasUnknownFields(descriptor_->file())) {
121
printer->Print(variables_,
123
" mutable_unknown_fields()->AddVarint($number$, value);\n");
125
printer->Print(variables_,
135
129
void EnumFieldGenerator::
136
130
GenerateSerializeWithCachedSizes(io::Printer* printer) const {
137
131
printer->Print(variables_,
138
"::google::protobuf::internal::WireFormat::WriteEnum("
139
"$number$, this->$name$(), output);\n");
132
"::google::protobuf::internal::WireFormatLite::WriteEnum(\n"
133
" $number$, this->$name$(), output);\n");
142
136
void EnumFieldGenerator::
143
137
GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
144
138
printer->Print(variables_,
145
"target = ::google::protobuf::internal::WireFormat::WriteEnumToArray("
146
"$number$, this->$name$(), target);\n");
139
"target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(\n"
140
" $number$, this->$name$(), target);\n");
149
143
void EnumFieldGenerator::
150
144
GenerateByteSize(io::Printer* printer) const {
151
145
printer->Print(variables_,
152
146
"total_size += $tag_size$ +\n"
153
" ::google::protobuf::internal::WireFormat::EnumSize(this->$name$());\n");
147
" ::google::protobuf::internal::WireFormatLite::EnumSize(this->$name$());\n");
156
150
// ===================================================================
167
161
GeneratePrivateMembers(io::Printer* printer) const {
168
162
printer->Print(variables_,
169
163
"::google::protobuf::RepeatedField<int> $name$_;\n");
170
if (descriptor_->options().packed() &&
171
descriptor_->file()->options().optimize_for() == FileOptions::SPEED) {
164
if (descriptor_->options().packed() && HasGeneratedMethods(descriptor_->file())) {
172
165
printer->Print(variables_,
173
166
"mutable int _$name$_cached_byte_size_;\n");
177
170
void RepeatedEnumFieldGenerator::
178
171
GenerateAccessorDeclarations(io::Printer* printer) const {
179
172
printer->Print(variables_,
180
"inline const ::google::protobuf::RepeatedField<int>& $name$() const;\n"
181
"inline ::google::protobuf::RepeatedField<int>* mutable_$name$();\n"
182
"inline $type$ $name$(int index) const;\n"
183
"inline void set_$name$(int index, $type$ value);\n"
184
"inline void add_$name$($type$ value);\n");
173
"inline const ::google::protobuf::RepeatedField<int>& $name$() const$deprecation$;\n"
174
"inline ::google::protobuf::RepeatedField<int>* mutable_$name$()$deprecation$;\n"
175
"inline $type$ $name$(int index) const$deprecation$;\n"
176
"inline void set_$name$(int index, $type$ value)$deprecation$;\n"
177
"inline void add_$name$($type$ value)$deprecation$;\n");
187
180
void RepeatedEnumFieldGenerator::
238
231
"input->PushLimit(length);\n"
239
232
"while (input->BytesUntilLimit() > 0) {\n"
241
" DO_(::google::protobuf::internal::WireFormat::ReadEnum(input, &value));\n"
234
" DO_(::google::protobuf::internal::WireFormatLite::ReadEnum(input, &value));\n"
242
235
" if ($type$_IsValid(value)) {\n"
243
236
" add_$name$(static_cast< $type$ >(value));\n"
248
241
printer->Print(variables_,
250
"DO_(::google::protobuf::internal::WireFormat::ReadEnum(input, &value));\n"
243
"DO_(::google::protobuf::internal::WireFormatLite::ReadEnum(input, &value));\n"
251
244
"if ($type$_IsValid(value)) {\n"
252
" add_$name$(static_cast< $type$ >(value));\n"
254
" mutable_unknown_fields()->AddVarint($number$, value);\n"
245
" add_$name$(static_cast< $type$ >(value));\n");
246
if (HasUnknownFields(descriptor_->file())) {
247
printer->Print(variables_,
249
" mutable_unknown_fields()->AddVarint($number$, value);\n");
251
printer->Print(variables_,
262
259
// Write the tag and the size.
263
260
printer->Print(variables_,
264
261
"if (this->$name$_size() > 0) {\n"
265
" ::google::protobuf::internal::WireFormat::WriteTag("
267
"::google::protobuf::internal::WireFormat::WIRETYPE_LENGTH_DELIMITED, "
262
" ::google::protobuf::internal::WireFormatLite::WriteTag(\n"
264
" ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n"
269
266
" output->WriteVarint32(_$name$_cached_byte_size_);\n"
273
270
"for (int i = 0; i < this->$name$_size(); i++) {\n");
274
271
if (descriptor_->options().packed()) {
275
272
printer->Print(variables_,
276
" ::google::protobuf::internal::WireFormat::WriteEnumNoTag("
277
"this->$name$(i), output);\n");
273
" ::google::protobuf::internal::WireFormatLite::WriteEnumNoTag(\n"
274
" this->$name$(i), output);\n");
279
276
printer->Print(variables_,
280
" ::google::protobuf::internal::WireFormat::WriteEnum("
281
"$number$, this->$name$(i), output);\n");
277
" ::google::protobuf::internal::WireFormatLite::WriteEnum(\n"
278
" $number$, this->$name$(i), output);\n");
283
280
printer->Print("}\n");
289
286
// Write the tag and the size.
290
287
printer->Print(variables_,
291
288
"if (this->$name$_size() > 0) {\n"
292
" target = ::google::protobuf::internal::WireFormat::WriteTagToArray("
294
"::google::protobuf::internal::WireFormat::WIRETYPE_LENGTH_DELIMITED, "
289
" target = ::google::protobuf::internal::WireFormatLite::WriteTagToArray(\n"
291
" ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n"
296
293
" target = ::google::protobuf::io::CodedOutputStream::WriteVarint32ToArray("
297
"_$name$_cached_byte_size_, target);\n"
294
" _$name$_cached_byte_size_, target);\n"
300
297
printer->Print(variables_,
301
298
"for (int i = 0; i < this->$name$_size(); i++) {\n");
302
299
if (descriptor_->options().packed()) {
303
300
printer->Print(variables_,
304
" target = ::google::protobuf::internal::WireFormat::WriteEnumNoTagToArray("
305
"this->$name$(i), target);\n");
301
" target = ::google::protobuf::internal::WireFormatLite::WriteEnumNoTagToArray(\n"
302
" this->$name$(i), target);\n");
307
304
printer->Print(variables_,
308
" target = ::google::protobuf::internal::WireFormat::WriteEnumToArray("
309
"$number$, this->$name$(i), target);\n");
305
" target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(\n"
306
" $number$, this->$name$(i), target);\n");
311
308
printer->Print("}\n");
319
316
printer->Indent();
320
317
printer->Print(variables_,
321
318
"for (int i = 0; i < this->$name$_size(); i++) {\n"
322
" data_size += ::google::protobuf::internal::WireFormat::EnumSize(\n"
319
" data_size += ::google::protobuf::internal::WireFormatLite::EnumSize(\n"
323
320
" this->$name$(i));\n"
326
323
if (descriptor_->options().packed()) {
327
324
printer->Print(variables_,
328
325
"if (data_size > 0) {\n"
329
" total_size += $tag_size$ + "
330
"::google::protobuf::internal::WireFormat::Int32Size(data_size);\n"
326
" total_size += $tag_size$ +\n"
327
" ::google::protobuf::internal::WireFormatLite::Int32Size(data_size);\n"
332
329
"_$name$_cached_byte_size_ = data_size;\n"
333
330
"total_size += data_size;\n");