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. |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 3 | // https://developers.google.com/protocol-buffers/ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 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, |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 49 | map<string, string>* variables, |
| 50 | const Options& options) { |
| 51 | SetCommonFieldVariables(descriptor, variables, options); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 52 | const EnumValueDescriptor* default_value = descriptor->default_value_enum(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 53 | (*variables)["type"] = ClassName(descriptor->enum_type(), true); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 54 | (*variables)["default"] = Int32ToString(default_value->number()); |
| 55 | (*variables)["full_name"] = descriptor->full_name(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
| 60 | // =================================================================== |
| 61 | |
| 62 | EnumFieldGenerator:: |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 63 | EnumFieldGenerator(const FieldDescriptor* descriptor, |
| 64 | const Options& options) |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 65 | : descriptor_(descriptor) { |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 66 | SetEnumVariables(descriptor, &variables_, options); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | EnumFieldGenerator::~EnumFieldGenerator() {} |
| 70 | |
| 71 | void EnumFieldGenerator:: |
| 72 | GeneratePrivateMembers(io::Printer* printer) const { |
| 73 | printer->Print(variables_, "int $name$_;\n"); |
| 74 | } |
| 75 | |
| 76 | void EnumFieldGenerator:: |
| 77 | GenerateAccessorDeclarations(io::Printer* printer) const { |
| 78 | printer->Print(variables_, |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 79 | "$type$ $name$() const$deprecation$;\n" |
| 80 | "void set_$name$($type$ value)$deprecation$;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void EnumFieldGenerator:: |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 84 | GenerateInlineAccessorDefinitions(io::Printer* printer, |
| 85 | bool is_inline) const { |
| 86 | map<string, string> variables(variables_); |
| 87 | variables["inline"] = is_inline ? "inline" : ""; |
| 88 | printer->Print(variables, |
| 89 | "$inline$ $type$ $classname$::$name$() const {\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 90 | " // @@protoc_insertion_point(field_get:$full_name$)\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 91 | " return static_cast< $type$ >($name$_);\n" |
| 92 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 93 | "$inline$ void $classname$::set_$name$($type$ value) {\n"); |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 94 | if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 95 | printer->Print(variables, |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 96 | " assert($type$_IsValid(value));\n"); |
| 97 | } |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 98 | printer->Print(variables, |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 99 | " $set_hasbit$\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 100 | " $name$_ = value;\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 101 | " // @@protoc_insertion_point(field_set:$full_name$)\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 102 | "}\n"); |
| 103 | } |
| 104 | |
| 105 | void EnumFieldGenerator:: |
| 106 | GenerateClearingCode(io::Printer* printer) const { |
| 107 | printer->Print(variables_, "$name$_ = $default$;\n"); |
| 108 | } |
| 109 | |
| 110 | void EnumFieldGenerator:: |
| 111 | GenerateMergingCode(io::Printer* printer) const { |
| 112 | printer->Print(variables_, "set_$name$(from.$name$());\n"); |
| 113 | } |
| 114 | |
| 115 | void EnumFieldGenerator:: |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 116 | GenerateSwappingCode(io::Printer* printer) const { |
| 117 | printer->Print(variables_, "std::swap($name$_, other->$name$_);\n"); |
| 118 | } |
| 119 | |
| 120 | void EnumFieldGenerator:: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 121 | GenerateConstructorCode(io::Printer* printer) const { |
| 122 | printer->Print(variables_, "$name$_ = $default$;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void EnumFieldGenerator:: |
| 126 | GenerateMergeFromCodedStream(io::Printer* printer) const { |
| 127 | printer->Print(variables_, |
| 128 | "int value;\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 129 | "DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n" |
| 130 | " int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 131 | " input, &value)));\n"); |
| 132 | if (HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 133 | printer->Print(variables_, |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 134 | "set_$name$(static_cast< $type$ >(value));\n"); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 135 | } else { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 136 | printer->Print(variables_, |
| 137 | "if ($type$_IsValid(value)) {\n" |
| 138 | " set_$name$(static_cast< $type$ >(value));\n"); |
| 139 | if (UseUnknownFieldSet(descriptor_->file())) { |
| 140 | printer->Print(variables_, |
| 141 | "} else {\n" |
| 142 | " mutable_unknown_fields()->AddVarint($number$, value);\n"); |
| 143 | } else { |
| 144 | printer->Print( |
| 145 | "} else {\n" |
| 146 | " unknown_fields_stream.WriteVarint32(tag);\n" |
| 147 | " unknown_fields_stream.WriteVarint32(value);\n"); |
| 148 | } |
| 149 | printer->Print(variables_, |
| 150 | "}\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 151 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void EnumFieldGenerator:: |
| 155 | GenerateSerializeWithCachedSizes(io::Printer* printer) const { |
| 156 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 157 | "::google::protobuf::internal::WireFormatLite::WriteEnum(\n" |
| 158 | " $number$, this->$name$(), output);\n"); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void EnumFieldGenerator:: |
| 162 | GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { |
| 163 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 164 | "target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(\n" |
| 165 | " $number$, this->$name$(), target);\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void EnumFieldGenerator:: |
| 169 | GenerateByteSize(io::Printer* printer) const { |
| 170 | printer->Print(variables_, |
| 171 | "total_size += $tag_size$ +\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 172 | " ::google::protobuf::internal::WireFormatLite::EnumSize(this->$name$());\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // =================================================================== |
| 176 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 177 | EnumOneofFieldGenerator:: |
| 178 | EnumOneofFieldGenerator(const FieldDescriptor* descriptor, |
| 179 | const Options& options) |
| 180 | : EnumFieldGenerator(descriptor, options) { |
| 181 | SetCommonOneofFieldVariables(descriptor, &variables_); |
| 182 | } |
| 183 | |
| 184 | EnumOneofFieldGenerator::~EnumOneofFieldGenerator() {} |
| 185 | |
| 186 | void EnumOneofFieldGenerator:: |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 187 | GenerateInlineAccessorDefinitions(io::Printer* printer, |
| 188 | bool is_inline) const { |
| 189 | map<string, string> variables(variables_); |
| 190 | variables["inline"] = is_inline ? "inline" : ""; |
| 191 | printer->Print(variables, |
| 192 | "$inline$ $type$ $classname$::$name$() const {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 193 | " // @@protoc_insertion_point(field_get:$full_name$)\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 194 | " if (has_$name$()) {\n" |
| 195 | " return static_cast< $type$ >($oneof_prefix$$name$_);\n" |
| 196 | " }\n" |
| 197 | " return static_cast< $type$ >($default$);\n" |
| 198 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 199 | "$inline$ void $classname$::set_$name$($type$ value) {\n"); |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 200 | if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 201 | printer->Print(variables, |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 202 | " assert($type$_IsValid(value));\n"); |
| 203 | } |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 204 | printer->Print(variables, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 205 | " if (!has_$name$()) {\n" |
| 206 | " clear_$oneof_name$();\n" |
| 207 | " set_has_$name$();\n" |
| 208 | " }\n" |
| 209 | " $oneof_prefix$$name$_ = value;\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 210 | " // @@protoc_insertion_point(field_set:$full_name$)\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 211 | "}\n"); |
| 212 | } |
| 213 | |
| 214 | void EnumOneofFieldGenerator:: |
| 215 | GenerateClearingCode(io::Printer* printer) const { |
| 216 | printer->Print(variables_, "$oneof_prefix$$name$_ = $default$;\n"); |
| 217 | } |
| 218 | |
| 219 | void EnumOneofFieldGenerator:: |
| 220 | GenerateSwappingCode(io::Printer* printer) const { |
| 221 | // Don't print any swapping code. Swapping the union will swap this field. |
| 222 | } |
| 223 | |
| 224 | void EnumOneofFieldGenerator:: |
| 225 | GenerateConstructorCode(io::Printer* printer) const { |
| 226 | printer->Print(variables_, |
| 227 | " $classname$_default_oneof_instance_->$name$_ = $default$;\n"); |
| 228 | } |
| 229 | |
| 230 | // =================================================================== |
| 231 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 232 | RepeatedEnumFieldGenerator:: |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 233 | RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor, |
| 234 | const Options& options) |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 235 | : descriptor_(descriptor) { |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 236 | SetEnumVariables(descriptor, &variables_, options); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {} |
| 240 | |
| 241 | void RepeatedEnumFieldGenerator:: |
| 242 | GeneratePrivateMembers(io::Printer* printer) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 243 | printer->Print(variables_, |
| 244 | "::google::protobuf::RepeatedField<int> $name$_;\n"); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 245 | if (descriptor_->options().packed() |
| 246 | && HasGeneratedMethods(descriptor_->file())) { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 247 | printer->Print(variables_, |
| 248 | "mutable int _$name$_cached_byte_size_;\n"); |
| 249 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void RepeatedEnumFieldGenerator:: |
| 253 | GenerateAccessorDeclarations(io::Printer* printer) const { |
| 254 | printer->Print(variables_, |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 255 | "$type$ $name$(int index) const$deprecation$;\n" |
| 256 | "void set_$name$(int index, $type$ value)$deprecation$;\n" |
| 257 | "void add_$name$($type$ value)$deprecation$;\n"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 258 | printer->Print(variables_, |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 259 | "const ::google::protobuf::RepeatedField<int>& $name$() const$deprecation$;\n" |
| 260 | "::google::protobuf::RepeatedField<int>* mutable_$name$()$deprecation$;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void RepeatedEnumFieldGenerator:: |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 264 | GenerateInlineAccessorDefinitions(io::Printer* printer, |
| 265 | bool is_inline) const { |
| 266 | map<string, string> variables(variables_); |
| 267 | variables["inline"] = is_inline ? "inline" : ""; |
| 268 | printer->Print(variables, |
| 269 | "$inline$ $type$ $classname$::$name$(int index) const {\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 270 | " // @@protoc_insertion_point(field_get:$full_name$)\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 271 | " return static_cast< $type$ >($name$_.Get(index));\n" |
| 272 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 273 | "$inline$ void $classname$::set_$name$(int index, $type$ value) {\n"); |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 274 | if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 275 | printer->Print(variables, |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 276 | " assert($type$_IsValid(value));\n"); |
| 277 | } |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 278 | printer->Print(variables, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 279 | " $name$_.Set(index, value);\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 280 | " // @@protoc_insertion_point(field_set:$full_name$)\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 281 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 282 | "$inline$ void $classname$::add_$name$($type$ value) {\n"); |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 283 | if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 284 | printer->Print(variables, |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 285 | " assert($type$_IsValid(value));\n"); |
| 286 | } |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 287 | printer->Print(variables, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 288 | " $name$_.Add(value);\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 289 | " // @@protoc_insertion_point(field_add:$full_name$)\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 290 | "}\n"); |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 291 | printer->Print(variables, |
| 292 | "$inline$ const ::google::protobuf::RepeatedField<int>&\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 293 | "$classname$::$name$() const {\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 294 | " // @@protoc_insertion_point(field_list:$full_name$)\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 295 | " return $name$_;\n" |
| 296 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 297 | "$inline$ ::google::protobuf::RepeatedField<int>*\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 298 | "$classname$::mutable_$name$() {\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 299 | " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 300 | " return &$name$_;\n" |
| 301 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void RepeatedEnumFieldGenerator:: |
| 305 | GenerateClearingCode(io::Printer* printer) const { |
| 306 | printer->Print(variables_, "$name$_.Clear();\n"); |
| 307 | } |
| 308 | |
| 309 | void RepeatedEnumFieldGenerator:: |
| 310 | GenerateMergingCode(io::Printer* printer) const { |
| 311 | printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n"); |
| 312 | } |
| 313 | |
| 314 | void RepeatedEnumFieldGenerator:: |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 315 | GenerateSwappingCode(io::Printer* printer) const { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 316 | printer->Print(variables_, "$name$_.UnsafeArenaSwap(&other->$name$_);\n"); |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void RepeatedEnumFieldGenerator:: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 320 | GenerateConstructorCode(io::Printer* printer) const { |
| 321 | // Not needed for repeated fields. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void RepeatedEnumFieldGenerator:: |
| 325 | GenerateMergeFromCodedStream(io::Printer* printer) const { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 326 | // Don't use ReadRepeatedPrimitive here so that the enum can be validated. |
| 327 | printer->Print(variables_, |
| 328 | "int value;\n" |
| 329 | "DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n" |
| 330 | " int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 331 | " input, &value)));\n"); |
| 332 | if (HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 333 | printer->Print(variables_, |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 334 | "add_$name$(static_cast< $type$ >(value));\n"); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 335 | } else { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 336 | printer->Print(variables_, |
| 337 | "if ($type$_IsValid(value)) {\n" |
| 338 | " add_$name$(static_cast< $type$ >(value));\n"); |
| 339 | if (UseUnknownFieldSet(descriptor_->file())) { |
| 340 | printer->Print(variables_, |
| 341 | "} else {\n" |
| 342 | " mutable_unknown_fields()->AddVarint($number$, value);\n"); |
| 343 | } else { |
| 344 | printer->Print( |
| 345 | "} else {\n" |
| 346 | " unknown_fields_stream.WriteVarint32(tag);\n" |
| 347 | " unknown_fields_stream.WriteVarint32(value);\n"); |
| 348 | } |
| 349 | printer->Print("}\n"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 350 | } |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void RepeatedEnumFieldGenerator:: |
| 354 | GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const { |
| 355 | if (!descriptor_->options().packed()) { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 356 | // This path is rarely executed, so we use a non-inlined implementation. |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 357 | if (HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
| 358 | printer->Print(variables_, |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 359 | "DO_((::google::protobuf::internal::" |
| 360 | "WireFormatLite::ReadPackedEnumPreserveUnknowns(\n" |
| 361 | " input,\n" |
| 362 | " $number$,\n" |
| 363 | " NULL,\n" |
| 364 | " NULL,\n" |
| 365 | " this->mutable_$name$())));\n"); |
| 366 | } else if (UseUnknownFieldSet(descriptor_->file())) { |
| 367 | printer->Print(variables_, |
| 368 | "DO_((::google::protobuf::internal::WireFormat::ReadPackedEnumPreserveUnknowns(\n" |
| 369 | " input,\n" |
| 370 | " $number$,\n" |
| 371 | " $type$_IsValid,\n" |
| 372 | " mutable_unknown_fields(),\n" |
| 373 | " this->mutable_$name$())));\n"); |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 374 | } else { |
| 375 | printer->Print(variables_, |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 376 | "DO_((::google::protobuf::internal::" |
| 377 | "WireFormatLite::ReadPackedEnumPreserveUnknowns(\n" |
| 378 | " input,\n" |
| 379 | " $number$,\n" |
| 380 | " $type$_IsValid,\n" |
| 381 | " &unknown_fields_stream,\n" |
| 382 | " this->mutable_$name$())));\n"); |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 383 | } |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 384 | } else { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 385 | printer->Print(variables_, |
| 386 | "::google::protobuf::uint32 length;\n" |
| 387 | "DO_(input->ReadVarint32(&length));\n" |
| 388 | "::google::protobuf::io::CodedInputStream::Limit limit = " |
| 389 | "input->PushLimit(length);\n" |
| 390 | "while (input->BytesUntilLimit() > 0) {\n" |
| 391 | " int value;\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 392 | " DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n" |
| 393 | " int, ::google::protobuf::internal::WireFormatLite::TYPE_ENUM>(\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 394 | " input, &value)));\n"); |
| 395 | if (HasPreservingUnknownEnumSemantics(descriptor_->file())) { |
| 396 | printer->Print(variables_, |
| 397 | " add_$name$(static_cast< $type$ >(value));\n"); |
| 398 | } else { |
| 399 | printer->Print(variables_, |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 400 | " if ($type$_IsValid(value)) {\n" |
| 401 | " add_$name$(static_cast< $type$ >(value));\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 402 | " } else {\n"); |
| 403 | if (UseUnknownFieldSet(descriptor_->file())) { |
| 404 | printer->Print(variables_, |
| 405 | " mutable_unknown_fields()->AddVarint($number$, value);\n"); |
| 406 | } else { |
| 407 | printer->Print(variables_, |
| 408 | " unknown_fields_stream.WriteVarint32(tag);\n" |
| 409 | " unknown_fields_stream.WriteVarint32(value);\n"); |
| 410 | } |
| 411 | printer->Print( |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 412 | " }\n"); |
| 413 | } |
| 414 | printer->Print(variables_, |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 415 | "}\n" |
| 416 | "input->PopLimit(limit);\n"); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 417 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void RepeatedEnumFieldGenerator:: |
| 421 | GenerateSerializeWithCachedSizes(io::Printer* printer) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 422 | if (descriptor_->options().packed()) { |
| 423 | // Write the tag and the size. |
| 424 | printer->Print(variables_, |
| 425 | "if (this->$name$_size() > 0) {\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 426 | " ::google::protobuf::internal::WireFormatLite::WriteTag(\n" |
| 427 | " $number$,\n" |
| 428 | " ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n" |
| 429 | " output);\n" |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 430 | " output->WriteVarint32(_$name$_cached_byte_size_);\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 431 | "}\n"); |
| 432 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 433 | printer->Print(variables_, |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 434 | "for (int i = 0; i < this->$name$_size(); i++) {\n"); |
| 435 | if (descriptor_->options().packed()) { |
| 436 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 437 | " ::google::protobuf::internal::WireFormatLite::WriteEnumNoTag(\n" |
| 438 | " this->$name$(i), output);\n"); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 439 | } else { |
| 440 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 441 | " ::google::protobuf::internal::WireFormatLite::WriteEnum(\n" |
| 442 | " $number$, this->$name$(i), output);\n"); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 443 | } |
| 444 | printer->Print("}\n"); |
| 445 | } |
| 446 | |
| 447 | void RepeatedEnumFieldGenerator:: |
| 448 | GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { |
| 449 | if (descriptor_->options().packed()) { |
| 450 | // Write the tag and the size. |
| 451 | printer->Print(variables_, |
| 452 | "if (this->$name$_size() > 0) {\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 453 | " target = ::google::protobuf::internal::WireFormatLite::WriteTagToArray(\n" |
| 454 | " $number$,\n" |
| 455 | " ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n" |
| 456 | " target);\n" |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 457 | " target = ::google::protobuf::io::CodedOutputStream::WriteVarint32ToArray(" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 458 | " _$name$_cached_byte_size_, target);\n" |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 459 | "}\n"); |
| 460 | } |
| 461 | printer->Print(variables_, |
| 462 | "for (int i = 0; i < this->$name$_size(); i++) {\n"); |
| 463 | if (descriptor_->options().packed()) { |
| 464 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 465 | " target = ::google::protobuf::internal::WireFormatLite::WriteEnumNoTagToArray(\n" |
| 466 | " this->$name$(i), target);\n"); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 467 | } else { |
| 468 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 469 | " target = ::google::protobuf::internal::WireFormatLite::WriteEnumToArray(\n" |
| 470 | " $number$, this->$name$(i), target);\n"); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 471 | } |
| 472 | printer->Print("}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | void RepeatedEnumFieldGenerator:: |
| 476 | GenerateByteSize(io::Printer* printer) const { |
| 477 | printer->Print(variables_, |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 478 | "{\n" |
| 479 | " int data_size = 0;\n"); |
| 480 | printer->Indent(); |
| 481 | printer->Print(variables_, |
| 482 | "for (int i = 0; i < this->$name$_size(); i++) {\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 483 | " data_size += ::google::protobuf::internal::WireFormatLite::EnumSize(\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 484 | " this->$name$(i));\n" |
| 485 | "}\n"); |
| 486 | |
| 487 | if (descriptor_->options().packed()) { |
| 488 | printer->Print(variables_, |
| 489 | "if (data_size > 0) {\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 490 | " total_size += $tag_size$ +\n" |
| 491 | " ::google::protobuf::internal::WireFormatLite::Int32Size(data_size);\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 492 | "}\n" |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 493 | "GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 494 | "_$name$_cached_byte_size_ = data_size;\n" |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 495 | "GOOGLE_SAFE_CONCURRENT_WRITES_END();\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 496 | "total_size += data_size;\n"); |
| 497 | } else { |
| 498 | printer->Print(variables_, |
| 499 | "total_size += $tag_size$ * this->$name$_size() + data_size;\n"); |
| 500 | } |
| 501 | printer->Outdent(); |
| 502 | printer->Print("}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | } // namespace cpp |
| 506 | } // namespace compiler |
| 507 | } // namespace protobuf |
| 508 | } // namespace google |