temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3 | // http://code.google.com/p/protobuf/ |
| 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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 |
| 14 | // distribution. |
| 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 30 | |
| 31 | // Author: kenton@google.com (Kenton Varda) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | |
| 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> |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 38 | #include <google/protobuf/descriptor.pb.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 39 | #include <google/protobuf/stubs/strutil.h> |
| 40 | |
| 41 | namespace google { |
| 42 | namespace protobuf { |
| 43 | namespace compiler { |
| 44 | namespace cpp { |
| 45 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 46 | namespace { |
| 47 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 48 | void SetEnumVariables(const FieldDescriptor* descriptor, |
| 49 | map<string, string>* variables) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 50 | SetCommonFieldVariables(descriptor, variables); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 51 | const EnumValueDescriptor* default_value = descriptor->default_value_enum(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 52 | (*variables)["type"] = ClassName(descriptor->enum_type(), true); |
| 53 | (*variables)["default"] = SimpleItoa(default_value->number()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | } // namespace |
| 57 | |
| 58 | // =================================================================== |
| 59 | |
| 60 | EnumFieldGenerator:: |
| 61 | EnumFieldGenerator(const FieldDescriptor* descriptor) |
| 62 | : descriptor_(descriptor) { |
| 63 | SetEnumVariables(descriptor, &variables_); |
| 64 | } |
| 65 | |
| 66 | EnumFieldGenerator::~EnumFieldGenerator() {} |
| 67 | |
| 68 | void EnumFieldGenerator:: |
| 69 | GeneratePrivateMembers(io::Printer* printer) const { |
| 70 | printer->Print(variables_, "int $name$_;\n"); |
| 71 | } |
| 72 | |
| 73 | void EnumFieldGenerator:: |
| 74 | GenerateAccessorDeclarations(io::Printer* printer) const { |
| 75 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 76 | "inline $type$ $name$() const$deprecation$;\n" |
| 77 | "inline void set_$name$($type$ value)$deprecation$;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void EnumFieldGenerator:: |
| 81 | GenerateInlineAccessorDefinitions(io::Printer* printer) const { |
| 82 | printer->Print(variables_, |
| 83 | "inline $type$ $classname$::$name$() const {\n" |
| 84 | " return static_cast< $type$ >($name$_);\n" |
| 85 | "}\n" |
| 86 | "inline void $classname$::set_$name$($type$ value) {\n" |
| 87 | " GOOGLE_DCHECK($type$_IsValid(value));\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 88 | " set_has_$name$();\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 89 | " $name$_ = value;\n" |
| 90 | "}\n"); |
| 91 | } |
| 92 | |
| 93 | void EnumFieldGenerator:: |
| 94 | GenerateClearingCode(io::Printer* printer) const { |
| 95 | printer->Print(variables_, "$name$_ = $default$;\n"); |
| 96 | } |
| 97 | |
| 98 | void EnumFieldGenerator:: |
| 99 | GenerateMergingCode(io::Printer* printer) const { |
| 100 | printer->Print(variables_, "set_$name$(from.$name$());\n"); |
| 101 | } |
| 102 | |
| 103 | void EnumFieldGenerator:: |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 104 | GenerateSwappingCode(io::Printer* printer) const { |
| 105 | printer->Print(variables_, "std::swap($name$_, other->$name$_);\n"); |
| 106 | } |
| 107 | |
| 108 | void EnumFieldGenerator:: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 109 | GenerateConstructorCode(io::Printer* printer) const { |
| 110 | printer->Print(variables_, "$name$_ = $default$;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void EnumFieldGenerator:: |
| 114 | GenerateMergeFromCodedStream(io::Printer* printer) const { |
| 115 | printer->Print(variables_, |
| 116 | "int value;\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 117 | "DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n" |
| 118 | " int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n" |
| 119 | " input, &value)));\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 120 | "if ($type$_IsValid(value)) {\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 121 | " set_$name$(static_cast< $type$ >(value));\n"); |
| 122 | if (HasUnknownFields(descriptor_->file())) { |
| 123 | printer->Print(variables_, |
| 124 | "} else {\n" |
| 125 | " mutable_unknown_fields()->AddVarint($number$, value);\n"); |
| 126 | } |
| 127 | printer->Print(variables_, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 128 | "}\n"); |
| 129 | } |
| 130 | |
| 131 | void EnumFieldGenerator:: |
| 132 | GenerateSerializeWithCachedSizes(io::Printer* printer) const { |
| 133 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 134 | "::google::protobuf::internal::WireFormatLite::WriteEnum(\n" |
| 135 | " $number$, this->$name$(), output);\n"); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void EnumFieldGenerator:: |
| 139 | GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { |
| 140 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 141 | "target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(\n" |
| 142 | " $number$, this->$name$(), target);\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void EnumFieldGenerator:: |
| 146 | GenerateByteSize(io::Printer* printer) const { |
| 147 | printer->Print(variables_, |
| 148 | "total_size += $tag_size$ +\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 149 | " ::google::protobuf::internal::WireFormatLite::EnumSize(this->$name$());\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // =================================================================== |
| 153 | |
| 154 | RepeatedEnumFieldGenerator:: |
| 155 | RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor) |
| 156 | : descriptor_(descriptor) { |
| 157 | SetEnumVariables(descriptor, &variables_); |
| 158 | } |
| 159 | |
| 160 | RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {} |
| 161 | |
| 162 | void RepeatedEnumFieldGenerator:: |
| 163 | GeneratePrivateMembers(io::Printer* printer) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 164 | printer->Print(variables_, |
| 165 | "::google::protobuf::RepeatedField<int> $name$_;\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 166 | if (descriptor_->options().packed() && HasGeneratedMethods(descriptor_->file())) { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 167 | printer->Print(variables_, |
| 168 | "mutable int _$name$_cached_byte_size_;\n"); |
| 169 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void RepeatedEnumFieldGenerator:: |
| 173 | GenerateAccessorDeclarations(io::Printer* printer) const { |
| 174 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 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"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 178 | printer->Print(variables_, |
| 179 | "inline const ::google::protobuf::RepeatedField<int>& $name$() const$deprecation$;\n" |
| 180 | "inline ::google::protobuf::RepeatedField<int>* mutable_$name$()$deprecation$;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void RepeatedEnumFieldGenerator:: |
| 184 | GenerateInlineAccessorDefinitions(io::Printer* printer) const { |
| 185 | printer->Print(variables_, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 186 | "inline $type$ $classname$::$name$(int index) const {\n" |
| 187 | " return static_cast< $type$ >($name$_.Get(index));\n" |
| 188 | "}\n" |
| 189 | "inline void $classname$::set_$name$(int index, $type$ value) {\n" |
| 190 | " GOOGLE_DCHECK($type$_IsValid(value));\n" |
| 191 | " $name$_.Set(index, value);\n" |
| 192 | "}\n" |
| 193 | "inline void $classname$::add_$name$($type$ value) {\n" |
| 194 | " GOOGLE_DCHECK($type$_IsValid(value));\n" |
| 195 | " $name$_.Add(value);\n" |
| 196 | "}\n"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 197 | printer->Print(variables_, |
| 198 | "inline const ::google::protobuf::RepeatedField<int>&\n" |
| 199 | "$classname$::$name$() const {\n" |
| 200 | " return $name$_;\n" |
| 201 | "}\n" |
| 202 | "inline ::google::protobuf::RepeatedField<int>*\n" |
| 203 | "$classname$::mutable_$name$() {\n" |
| 204 | " return &$name$_;\n" |
| 205 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void RepeatedEnumFieldGenerator:: |
| 209 | GenerateClearingCode(io::Printer* printer) const { |
| 210 | printer->Print(variables_, "$name$_.Clear();\n"); |
| 211 | } |
| 212 | |
| 213 | void RepeatedEnumFieldGenerator:: |
| 214 | GenerateMergingCode(io::Printer* printer) const { |
| 215 | printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n"); |
| 216 | } |
| 217 | |
| 218 | void RepeatedEnumFieldGenerator:: |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 219 | GenerateSwappingCode(io::Printer* printer) const { |
| 220 | printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n"); |
| 221 | } |
| 222 | |
| 223 | void RepeatedEnumFieldGenerator:: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 224 | GenerateConstructorCode(io::Printer* printer) const { |
| 225 | // Not needed for repeated fields. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void RepeatedEnumFieldGenerator:: |
| 229 | GenerateMergeFromCodedStream(io::Printer* printer) const { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 230 | // Don't use ReadRepeatedPrimitive here so that the enum can be validated. |
| 231 | printer->Print(variables_, |
| 232 | "int value;\n" |
| 233 | "DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n" |
| 234 | " int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n" |
| 235 | " input, &value)));\n" |
| 236 | "if ($type$_IsValid(value)) {\n" |
| 237 | " add_$name$(static_cast< $type$ >(value));\n"); |
| 238 | if (HasUnknownFields(descriptor_->file())) { |
| 239 | printer->Print(variables_, |
| 240 | "} else {\n" |
| 241 | " mutable_unknown_fields()->AddVarint($number$, value);\n"); |
| 242 | } |
| 243 | printer->Print("}\n"); |
| 244 | } |
| 245 | |
| 246 | void RepeatedEnumFieldGenerator:: |
| 247 | GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const { |
| 248 | if (!descriptor_->options().packed()) { |
| 249 | // We use a non-inlined implementation in this case, since this path will |
| 250 | // rarely be executed. |
| 251 | printer->Print(variables_, |
| 252 | "DO_((::google::protobuf::internal::WireFormatLite::ReadPackedEnumNoInline(\n" |
| 253 | " input,\n" |
| 254 | " &$type$_IsValid,\n" |
| 255 | " this->mutable_$name$())));\n"); |
| 256 | } else { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 257 | printer->Print(variables_, |
| 258 | "::google::protobuf::uint32 length;\n" |
| 259 | "DO_(input->ReadVarint32(&length));\n" |
| 260 | "::google::protobuf::io::CodedInputStream::Limit limit = " |
| 261 | "input->PushLimit(length);\n" |
| 262 | "while (input->BytesUntilLimit() > 0) {\n" |
| 263 | " int value;\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 264 | " DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n" |
| 265 | " int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n" |
| 266 | " input, &value)));\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 267 | " if ($type$_IsValid(value)) {\n" |
| 268 | " add_$name$(static_cast< $type$ >(value));\n" |
| 269 | " }\n" |
| 270 | "}\n" |
| 271 | "input->PopLimit(limit);\n"); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 272 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void RepeatedEnumFieldGenerator:: |
| 276 | GenerateSerializeWithCachedSizes(io::Printer* printer) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 277 | if (descriptor_->options().packed()) { |
| 278 | // Write the tag and the size. |
| 279 | printer->Print(variables_, |
| 280 | "if (this->$name$_size() > 0) {\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 281 | " ::google::protobuf::internal::WireFormatLite::WriteTag(\n" |
| 282 | " $number$,\n" |
| 283 | " ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n" |
| 284 | " output);\n" |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 285 | " output->WriteVarint32(_$name$_cached_byte_size_);\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 286 | "}\n"); |
| 287 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 288 | printer->Print(variables_, |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 289 | "for (int i = 0; i < this->$name$_size(); i++) {\n"); |
| 290 | if (descriptor_->options().packed()) { |
| 291 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 292 | " ::google::protobuf::internal::WireFormatLite::WriteEnumNoTag(\n" |
| 293 | " this->$name$(i), output);\n"); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 294 | } else { |
| 295 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 296 | " ::google::protobuf::internal::WireFormatLite::WriteEnum(\n" |
| 297 | " $number$, this->$name$(i), output);\n"); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 298 | } |
| 299 | printer->Print("}\n"); |
| 300 | } |
| 301 | |
| 302 | void RepeatedEnumFieldGenerator:: |
| 303 | GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { |
| 304 | if (descriptor_->options().packed()) { |
| 305 | // Write the tag and the size. |
| 306 | printer->Print(variables_, |
| 307 | "if (this->$name$_size() > 0) {\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 308 | " target = ::google::protobuf::internal::WireFormatLite::WriteTagToArray(\n" |
| 309 | " $number$,\n" |
| 310 | " ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n" |
| 311 | " target);\n" |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 312 | " target = ::google::protobuf::io::CodedOutputStream::WriteVarint32ToArray(" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 313 | " _$name$_cached_byte_size_, target);\n" |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 314 | "}\n"); |
| 315 | } |
| 316 | printer->Print(variables_, |
| 317 | "for (int i = 0; i < this->$name$_size(); i++) {\n"); |
| 318 | if (descriptor_->options().packed()) { |
| 319 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 320 | " target = ::google::protobuf::internal::WireFormatLite::WriteEnumNoTagToArray(\n" |
| 321 | " this->$name$(i), target);\n"); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 322 | } else { |
| 323 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 324 | " target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(\n" |
| 325 | " $number$, this->$name$(i), target);\n"); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 326 | } |
| 327 | printer->Print("}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void RepeatedEnumFieldGenerator:: |
| 331 | GenerateByteSize(io::Printer* printer) const { |
| 332 | printer->Print(variables_, |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 333 | "{\n" |
| 334 | " int data_size = 0;\n"); |
| 335 | printer->Indent(); |
| 336 | printer->Print(variables_, |
| 337 | "for (int i = 0; i < this->$name$_size(); i++) {\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 338 | " data_size += ::google::protobuf::internal::WireFormatLite::EnumSize(\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 339 | " this->$name$(i));\n" |
| 340 | "}\n"); |
| 341 | |
| 342 | if (descriptor_->options().packed()) { |
| 343 | printer->Print(variables_, |
| 344 | "if (data_size > 0) {\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 345 | " total_size += $tag_size$ +\n" |
| 346 | " ::google::protobuf::internal::WireFormatLite::Int32Size(data_size);\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 347 | "}\n" |
| 348 | "_$name$_cached_byte_size_ = data_size;\n" |
| 349 | "total_size += data_size;\n"); |
| 350 | } else { |
| 351 | printer->Print(variables_, |
| 352 | "total_size += $tag_size$ * this->$name$_size() + data_size;\n"); |
| 353 | } |
| 354 | printer->Outdent(); |
| 355 | printer->Print("}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | } // namespace cpp |
| 359 | } // namespace compiler |
| 360 | } // namespace protobuf |
| 361 | } // namespace google |