~ubuntu-branches/ubuntu/raring/protobuf/raring

« back to all changes in this revision

Viewing changes to src/google/protobuf/compiler/cpp/cpp_enum_field.cc

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2010-02-11 11:13:19 UTC
  • mfrom: (2.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100211111319-zdn8hmw0gh8s4cf8
Tags: 2.2.0a-0.1ubuntu1
* Merge from Debian testing.
* Remaining Ubuntu changes:
  - Don't use python2.4.
* Ubuntu changes dropped:
  - Disable death tests on Itanium, fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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>
40
40
 
41
41
namespace google {
43
43
namespace compiler {
44
44
namespace cpp {
45
45
 
46
 
using internal::WireFormat;
47
 
 
48
46
namespace {
49
47
 
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();
55
 
 
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()));
64
54
}
65
55
 
66
56
}  // namespace
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");
88
78
}
89
79
 
90
80
void EnumFieldGenerator::
124
114
GenerateMergeFromCodedStream(io::Printer* printer) const {
125
115
  printer->Print(variables_,
126
116
    "int value;\n"
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"
130
 
    "} else {\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_,
 
122
      "} else {\n"
 
123
      "  mutable_unknown_fields()->AddVarint($number$, value);\n");
 
124
  }
 
125
  printer->Print(variables_,
132
126
    "}\n");
133
127
}
134
128
 
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");
140
134
}
141
135
 
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");
147
141
}
148
142
 
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");
154
148
}
155
149
 
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");
174
167
  }
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");
185
178
}
186
179
 
187
180
void RepeatedEnumFieldGenerator::
238
231
          "input->PushLimit(length);\n"
239
232
      "while (input->BytesUntilLimit() > 0) {\n"
240
233
      "  int value;\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"
244
237
      "  }\n"
247
240
  } else {
248
241
    printer->Print(variables_,
249
242
      "int value;\n"
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"
253
 
      "} else {\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_,
 
248
        "} else {\n"
 
249
        "  mutable_unknown_fields()->AddVarint($number$, value);\n");
 
250
    }
 
251
    printer->Print(variables_,
255
252
      "}\n");
256
253
  }
257
254
}
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("
266
 
          "$number$, "
267
 
          "::google::protobuf::internal::WireFormat::WIRETYPE_LENGTH_DELIMITED, "
268
 
          "output);\n"
 
262
      "  ::google::protobuf::internal::WireFormatLite::WriteTag(\n"
 
263
      "    $number$,\n"
 
264
      "    ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n"
 
265
      "    output);\n"
269
266
      "  output->WriteVarint32(_$name$_cached_byte_size_);\n"
270
267
      "}\n");
271
268
  }
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");
278
275
  } else {
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");
282
279
  }
283
280
  printer->Print("}\n");
284
281
}
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("
293
 
          "$number$, "
294
 
          "::google::protobuf::internal::WireFormat::WIRETYPE_LENGTH_DELIMITED, "
295
 
          "target);\n"
 
289
      "  target = ::google::protobuf::internal::WireFormatLite::WriteTagToArray(\n"
 
290
      "    $number$,\n"
 
291
      "    ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n"
 
292
      "    target);\n"
296
293
      "  target = ::google::protobuf::io::CodedOutputStream::WriteVarint32ToArray("
297
 
          "_$name$_cached_byte_size_, target);\n"
 
294
      "    _$name$_cached_byte_size_, target);\n"
298
295
      "}\n");
299
296
  }
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");
306
303
  } else {
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");
310
307
  }
311
308
  printer->Print("}\n");
312
309
}
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"
324
321
      "}\n");
325
322
 
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"
331
328
      "}\n"
332
329
      "_$name$_cached_byte_size_ = data_size;\n"
333
330
      "total_size += data_size;\n");