1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
35 #include <google/protobuf/compiler/cpp/cpp_enum_field.h>
36 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
37 #include <google/protobuf/io/printer.h>
38 #include <google/protobuf/descriptor.pb.h>
39 #include <google/protobuf/stubs/strutil.h>
48 void SetEnumVariables(const FieldDescriptor* descriptor,
49 map<string, string>* variables,
50 const Options& options) {
51 SetCommonFieldVariables(descriptor, variables, options);
52 const EnumValueDescriptor* default_value = descriptor->default_value_enum();
53 (*variables)["type"] = ClassName(descriptor->enum_type(), true);
54 (*variables)["default"] = SimpleItoa(default_value->number());
59 // ===================================================================
62 EnumFieldGenerator(const FieldDescriptor* descriptor,
63 const Options& options)
64 : descriptor_(descriptor) {
65 SetEnumVariables(descriptor, &variables_, options);
68 EnumFieldGenerator::~EnumFieldGenerator() {}
70 void EnumFieldGenerator::
71 GeneratePrivateMembers(io::Printer* printer) const {
72 printer->Print(variables_, "int $name$_;\n");
75 void EnumFieldGenerator::
76 GenerateAccessorDeclarations(io::Printer* printer) const {
77 printer->Print(variables_,
78 "inline $type$ $name$() const$deprecation$;\n"
79 "inline void set_$name$($type$ value)$deprecation$;\n");
82 void EnumFieldGenerator::
83 GenerateInlineAccessorDefinitions(io::Printer* printer) const {
84 printer->Print(variables_,
85 "inline $type$ $classname$::$name$() const {\n"
86 " return static_cast< $type$ >($name$_);\n"
88 "inline void $classname$::set_$name$($type$ value) {\n"
89 " assert($type$_IsValid(value));\n"
90 " set_has_$name$();\n"
95 void EnumFieldGenerator::
96 GenerateClearingCode(io::Printer* printer) const {
97 printer->Print(variables_, "$name$_ = $default$;\n");
100 void EnumFieldGenerator::
101 GenerateMergingCode(io::Printer* printer) const {
102 printer->Print(variables_, "set_$name$(from.$name$());\n");
105 void EnumFieldGenerator::
106 GenerateSwappingCode(io::Printer* printer) const {
107 printer->Print(variables_, "std::swap($name$_, other->$name$_);\n");
110 void EnumFieldGenerator::
111 GenerateConstructorCode(io::Printer* printer) const {
112 printer->Print(variables_, "$name$_ = $default$;\n");
115 void EnumFieldGenerator::
116 GenerateMergeFromCodedStream(io::Printer* printer) const {
117 printer->Print(variables_,
119 "DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n"
120 " int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n"
121 " input, &value)));\n"
122 "if ($type$_IsValid(value)) {\n"
123 " set_$name$(static_cast< $type$ >(value));\n");
124 if (HasUnknownFields(descriptor_->file())) {
125 printer->Print(variables_,
127 " mutable_unknown_fields()->AddVarint($number$, value);\n");
129 printer->Print(variables_,
133 void EnumFieldGenerator::
134 GenerateSerializeWithCachedSizes(io::Printer* printer) const {
135 printer->Print(variables_,
136 "::google::protobuf::internal::WireFormatLite::WriteEnum(\n"
137 " $number$, this->$name$(), output);\n");
140 void EnumFieldGenerator::
141 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
142 printer->Print(variables_,
143 "target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(\n"
144 " $number$, this->$name$(), target);\n");
147 void EnumFieldGenerator::
148 GenerateByteSize(io::Printer* printer) const {
149 printer->Print(variables_,
150 "total_size += $tag_size$ +\n"
151 " ::google::protobuf::internal::WireFormatLite::EnumSize(this->$name$());\n");
154 // ===================================================================
156 RepeatedEnumFieldGenerator::
157 RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor,
158 const Options& options)
159 : descriptor_(descriptor) {
160 SetEnumVariables(descriptor, &variables_, options);
163 RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {}
165 void RepeatedEnumFieldGenerator::
166 GeneratePrivateMembers(io::Printer* printer) const {
167 printer->Print(variables_,
168 "::google::protobuf::RepeatedField<int> $name$_;\n");
169 if (descriptor_->options().packed() && HasGeneratedMethods(descriptor_->file())) {
170 printer->Print(variables_,
171 "mutable int _$name$_cached_byte_size_;\n");
175 void RepeatedEnumFieldGenerator::
176 GenerateAccessorDeclarations(io::Printer* printer) const {
177 printer->Print(variables_,
178 "inline $type$ $name$(int index) const$deprecation$;\n"
179 "inline void set_$name$(int index, $type$ value)$deprecation$;\n"
180 "inline void add_$name$($type$ value)$deprecation$;\n");
181 printer->Print(variables_,
182 "inline const ::google::protobuf::RepeatedField<int>& $name$() const$deprecation$;\n"
183 "inline ::google::protobuf::RepeatedField<int>* mutable_$name$()$deprecation$;\n");
186 void RepeatedEnumFieldGenerator::
187 GenerateInlineAccessorDefinitions(io::Printer* printer) const {
188 printer->Print(variables_,
189 "inline $type$ $classname$::$name$(int index) const {\n"
190 " return static_cast< $type$ >($name$_.Get(index));\n"
192 "inline void $classname$::set_$name$(int index, $type$ value) {\n"
193 " assert($type$_IsValid(value));\n"
194 " $name$_.Set(index, value);\n"
196 "inline void $classname$::add_$name$($type$ value) {\n"
197 " assert($type$_IsValid(value));\n"
198 " $name$_.Add(value);\n"
200 printer->Print(variables_,
201 "inline const ::google::protobuf::RepeatedField<int>&\n"
202 "$classname$::$name$() const {\n"
205 "inline ::google::protobuf::RepeatedField<int>*\n"
206 "$classname$::mutable_$name$() {\n"
207 " return &$name$_;\n"
211 void RepeatedEnumFieldGenerator::
212 GenerateClearingCode(io::Printer* printer) const {
213 printer->Print(variables_, "$name$_.Clear();\n");
216 void RepeatedEnumFieldGenerator::
217 GenerateMergingCode(io::Printer* printer) const {
218 printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
221 void RepeatedEnumFieldGenerator::
222 GenerateSwappingCode(io::Printer* printer) const {
223 printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n");
226 void RepeatedEnumFieldGenerator::
227 GenerateConstructorCode(io::Printer* printer) const {
228 // Not needed for repeated fields.
231 void RepeatedEnumFieldGenerator::
232 GenerateMergeFromCodedStream(io::Printer* printer) const {
233 // Don't use ReadRepeatedPrimitive here so that the enum can be validated.
234 printer->Print(variables_,
236 "DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n"
237 " int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n"
238 " input, &value)));\n"
239 "if ($type$_IsValid(value)) {\n"
240 " add_$name$(static_cast< $type$ >(value));\n");
241 if (HasUnknownFields(descriptor_->file())) {
242 printer->Print(variables_,
244 " mutable_unknown_fields()->AddVarint($number$, value);\n");
246 printer->Print("}\n");
249 void RepeatedEnumFieldGenerator::
250 GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const {
251 if (!descriptor_->options().packed()) {
252 // We use a non-inlined implementation in this case, since this path will
253 // rarely be executed.
254 printer->Print(variables_,
255 "DO_((::google::protobuf::internal::WireFormatLite::ReadPackedEnumNoInline(\n"
257 " &$type$_IsValid,\n"
258 " this->mutable_$name$())));\n");
260 printer->Print(variables_,
261 "::google::protobuf::uint32 length;\n"
262 "DO_(input->ReadVarint32(&length));\n"
263 "::google::protobuf::io::CodedInputStream::Limit limit = "
264 "input->PushLimit(length);\n"
265 "while (input->BytesUntilLimit() > 0) {\n"
267 " DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n"
268 " int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n"
269 " input, &value)));\n"
270 " if ($type$_IsValid(value)) {\n"
271 " add_$name$(static_cast< $type$ >(value));\n"
274 "input->PopLimit(limit);\n");
278 void RepeatedEnumFieldGenerator::
279 GenerateSerializeWithCachedSizes(io::Printer* printer) const {
280 if (descriptor_->options().packed()) {
281 // Write the tag and the size.
282 printer->Print(variables_,
283 "if (this->$name$_size() > 0) {\n"
284 " ::google::protobuf::internal::WireFormatLite::WriteTag(\n"
286 " ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n"
288 " output->WriteVarint32(_$name$_cached_byte_size_);\n"
291 printer->Print(variables_,
292 "for (int i = 0; i < this->$name$_size(); i++) {\n");
293 if (descriptor_->options().packed()) {
294 printer->Print(variables_,
295 " ::google::protobuf::internal::WireFormatLite::WriteEnumNoTag(\n"
296 " this->$name$(i), output);\n");
298 printer->Print(variables_,
299 " ::google::protobuf::internal::WireFormatLite::WriteEnum(\n"
300 " $number$, this->$name$(i), output);\n");
302 printer->Print("}\n");
305 void RepeatedEnumFieldGenerator::
306 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
307 if (descriptor_->options().packed()) {
308 // Write the tag and the size.
309 printer->Print(variables_,
310 "if (this->$name$_size() > 0) {\n"
311 " target = ::google::protobuf::internal::WireFormatLite::WriteTagToArray(\n"
313 " ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n"
315 " target = ::google::protobuf::io::CodedOutputStream::WriteVarint32ToArray("
316 " _$name$_cached_byte_size_, target);\n"
319 printer->Print(variables_,
320 "for (int i = 0; i < this->$name$_size(); i++) {\n");
321 if (descriptor_->options().packed()) {
322 printer->Print(variables_,
323 " target = ::google::protobuf::internal::WireFormatLite::WriteEnumNoTagToArray(\n"
324 " this->$name$(i), target);\n");
326 printer->Print(variables_,
327 " target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(\n"
328 " $number$, this->$name$(i), target);\n");
330 printer->Print("}\n");
333 void RepeatedEnumFieldGenerator::
334 GenerateByteSize(io::Printer* printer) const {
335 printer->Print(variables_,
337 " int data_size = 0;\n");
339 printer->Print(variables_,
340 "for (int i = 0; i < this->$name$_size(); i++) {\n"
341 " data_size += ::google::protobuf::internal::WireFormatLite::EnumSize(\n"
342 " this->$name$(i));\n"
345 if (descriptor_->options().packed()) {
346 printer->Print(variables_,
347 "if (data_size > 0) {\n"
348 " total_size += $tag_size$ +\n"
349 " ::google::protobuf::internal::WireFormatLite::Int32Size(data_size);\n"
351 "GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n"
352 "_$name$_cached_byte_size_ = data_size;\n"
353 "GOOGLE_SAFE_CONCURRENT_WRITES_END();\n"
354 "total_size += data_size;\n");
356 printer->Print(variables_,
357 "total_size += $tag_size$ * this->$name$_size() + data_size;\n");
360 printer->Print("}\n");
364 } // namespace compiler
365 } // namespace protobuf
366 } // namespace google