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_message_field.h> |
| 36 | #include <google/protobuf/compiler/cpp/cpp_helpers.h> |
| 37 | #include <google/protobuf/io/printer.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 38 | #include <google/protobuf/stubs/strutil.h> |
| 39 | |
| 40 | namespace google { |
| 41 | namespace protobuf { |
| 42 | namespace compiler { |
| 43 | namespace cpp { |
| 44 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 47 | void SetMessageVariables(const FieldDescriptor* descriptor, |
| 48 | map<string, string>* variables) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 49 | SetCommonFieldVariables(descriptor, variables); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 50 | (*variables)["type"] = FieldMessageTypeName(descriptor); |
| 51 | (*variables)["stream_writer"] = (*variables)["declared_type"] + |
| 52 | (HasFastArraySerialization(descriptor->message_type()->file()) ? |
| 53 | "MaybeToArray" : |
| 54 | ""); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | } // namespace |
| 58 | |
| 59 | // =================================================================== |
| 60 | |
| 61 | MessageFieldGenerator:: |
| 62 | MessageFieldGenerator(const FieldDescriptor* descriptor) |
| 63 | : descriptor_(descriptor) { |
| 64 | SetMessageVariables(descriptor, &variables_); |
| 65 | } |
| 66 | |
| 67 | MessageFieldGenerator::~MessageFieldGenerator() {} |
| 68 | |
| 69 | void MessageFieldGenerator:: |
| 70 | GeneratePrivateMembers(io::Printer* printer) const { |
| 71 | printer->Print(variables_, "$type$* $name$_;\n"); |
| 72 | } |
| 73 | |
| 74 | void MessageFieldGenerator:: |
| 75 | GenerateAccessorDeclarations(io::Printer* printer) const { |
| 76 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 77 | "inline const $type$& $name$() const$deprecation$;\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 78 | "inline $type$* mutable_$name$()$deprecation$;\n" |
| 79 | "inline $type$* release_$name$()$deprecation$;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void MessageFieldGenerator:: |
| 83 | GenerateInlineAccessorDefinitions(io::Printer* printer) const { |
| 84 | printer->Print(variables_, |
| 85 | "inline const $type$& $classname$::$name$() const {\n" |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 86 | " return $name$_ != NULL ? *$name$_ : *default_instance_->$name$_;\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 87 | "}\n" |
| 88 | "inline $type$* $classname$::mutable_$name$() {\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 89 | " set_has_$name$();\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 90 | " if ($name$_ == NULL) $name$_ = new $type$;\n" |
| 91 | " return $name$_;\n" |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 92 | "}\n" |
| 93 | "inline $type$* $classname$::release_$name$() {\n" |
| 94 | " clear_has_$name$();\n" |
| 95 | " $type$* temp = $name$_;\n" |
| 96 | " $name$_ = NULL;\n" |
| 97 | " return temp;\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 98 | "}\n"); |
| 99 | } |
| 100 | |
| 101 | void MessageFieldGenerator:: |
| 102 | GenerateClearingCode(io::Printer* printer) const { |
| 103 | printer->Print(variables_, |
| 104 | "if ($name$_ != NULL) $name$_->$type$::Clear();\n"); |
| 105 | } |
| 106 | |
| 107 | void MessageFieldGenerator:: |
| 108 | GenerateMergingCode(io::Printer* printer) const { |
| 109 | printer->Print(variables_, |
| 110 | "mutable_$name$()->$type$::MergeFrom(from.$name$());\n"); |
| 111 | } |
| 112 | |
| 113 | void MessageFieldGenerator:: |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 114 | GenerateSwappingCode(io::Printer* printer) const { |
| 115 | printer->Print(variables_, "std::swap($name$_, other->$name$_);\n"); |
| 116 | } |
| 117 | |
| 118 | void MessageFieldGenerator:: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 119 | GenerateConstructorCode(io::Printer* printer) const { |
| 120 | printer->Print(variables_, "$name$_ = NULL;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void MessageFieldGenerator:: |
| 124 | GenerateMergeFromCodedStream(io::Printer* printer) const { |
| 125 | if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) { |
| 126 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 127 | "DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 128 | " input, mutable_$name$()));\n"); |
| 129 | } else { |
| 130 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 131 | "DO_(::google::protobuf::internal::WireFormatLite::ReadGroupNoVirtual(\n" |
| 132 | " $number$, input, mutable_$name$()));\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | void MessageFieldGenerator:: |
| 137 | GenerateSerializeWithCachedSizes(io::Printer* printer) const { |
| 138 | printer->Print(variables_, |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 139 | "::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 140 | " $number$, this->$name$(), output);\n"); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void MessageFieldGenerator:: |
| 144 | GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { |
| 145 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 146 | "target = ::google::protobuf::internal::WireFormatLite::\n" |
| 147 | " Write$declared_type$NoVirtualToArray(\n" |
| 148 | " $number$, this->$name$(), target);\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void MessageFieldGenerator:: |
| 152 | GenerateByteSize(io::Printer* printer) const { |
| 153 | printer->Print(variables_, |
| 154 | "total_size += $tag_size$ +\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 155 | " ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirtual(\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 156 | " this->$name$());\n"); |
| 157 | } |
| 158 | |
| 159 | // =================================================================== |
| 160 | |
| 161 | RepeatedMessageFieldGenerator:: |
| 162 | RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor) |
| 163 | : descriptor_(descriptor) { |
| 164 | SetMessageVariables(descriptor, &variables_); |
| 165 | } |
| 166 | |
| 167 | RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {} |
| 168 | |
| 169 | void RepeatedMessageFieldGenerator:: |
| 170 | GeneratePrivateMembers(io::Printer* printer) const { |
| 171 | printer->Print(variables_, |
| 172 | "::google::protobuf::RepeatedPtrField< $type$ > $name$_;\n"); |
| 173 | } |
| 174 | |
| 175 | void RepeatedMessageFieldGenerator:: |
| 176 | GenerateAccessorDeclarations(io::Printer* printer) const { |
| 177 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 178 | "inline const $type$& $name$(int index) const$deprecation$;\n" |
| 179 | "inline $type$* mutable_$name$(int index)$deprecation$;\n" |
| 180 | "inline $type$* add_$name$()$deprecation$;\n"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 181 | printer->Print(variables_, |
| 182 | "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n" |
| 183 | " $name$() const$deprecation$;\n" |
| 184 | "inline ::google::protobuf::RepeatedPtrField< $type$ >*\n" |
| 185 | " mutable_$name$()$deprecation$;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void RepeatedMessageFieldGenerator:: |
| 189 | GenerateInlineAccessorDefinitions(io::Printer* printer) const { |
| 190 | printer->Print(variables_, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 191 | "inline const $type$& $classname$::$name$(int index) const {\n" |
| 192 | " return $name$_.Get(index);\n" |
| 193 | "}\n" |
| 194 | "inline $type$* $classname$::mutable_$name$(int index) {\n" |
| 195 | " return $name$_.Mutable(index);\n" |
| 196 | "}\n" |
| 197 | "inline $type$* $classname$::add_$name$() {\n" |
| 198 | " return $name$_.Add();\n" |
| 199 | "}\n"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 200 | printer->Print(variables_, |
| 201 | "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n" |
| 202 | "$classname$::$name$() const {\n" |
| 203 | " return $name$_;\n" |
| 204 | "}\n" |
| 205 | "inline ::google::protobuf::RepeatedPtrField< $type$ >*\n" |
| 206 | "$classname$::mutable_$name$() {\n" |
| 207 | " return &$name$_;\n" |
| 208 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void RepeatedMessageFieldGenerator:: |
| 212 | GenerateClearingCode(io::Printer* printer) const { |
| 213 | printer->Print(variables_, "$name$_.Clear();\n"); |
| 214 | } |
| 215 | |
| 216 | void RepeatedMessageFieldGenerator:: |
| 217 | GenerateMergingCode(io::Printer* printer) const { |
| 218 | printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n"); |
| 219 | } |
| 220 | |
| 221 | void RepeatedMessageFieldGenerator:: |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 222 | GenerateSwappingCode(io::Printer* printer) const { |
| 223 | printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n"); |
| 224 | } |
| 225 | |
| 226 | void RepeatedMessageFieldGenerator:: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 227 | GenerateConstructorCode(io::Printer* printer) const { |
| 228 | // Not needed for repeated fields. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void RepeatedMessageFieldGenerator:: |
| 232 | GenerateMergeFromCodedStream(io::Printer* printer) const { |
| 233 | if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) { |
| 234 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 235 | "DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(\n" |
| 236 | " input, add_$name$()));\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 237 | } else { |
| 238 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 239 | "DO_(::google::protobuf::internal::WireFormatLite::ReadGroupNoVirtual(\n" |
| 240 | " $number$, input, add_$name$()));\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | void RepeatedMessageFieldGenerator:: |
| 245 | GenerateSerializeWithCachedSizes(io::Printer* printer) const { |
| 246 | printer->Print(variables_, |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 247 | "for (int i = 0; i < this->$name$_size(); i++) {\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 248 | " ::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 249 | " $number$, this->$name$(i), output);\n" |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 250 | "}\n"); |
| 251 | } |
| 252 | |
| 253 | void RepeatedMessageFieldGenerator:: |
| 254 | GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { |
| 255 | printer->Print(variables_, |
| 256 | "for (int i = 0; i < this->$name$_size(); i++) {\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 257 | " target = ::google::protobuf::internal::WireFormatLite::\n" |
| 258 | " Write$declared_type$NoVirtualToArray(\n" |
| 259 | " $number$, this->$name$(i), target);\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 260 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void RepeatedMessageFieldGenerator:: |
| 264 | GenerateByteSize(io::Printer* printer) const { |
| 265 | printer->Print(variables_, |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 266 | "total_size += $tag_size$ * this->$name$_size();\n" |
| 267 | "for (int i = 0; i < this->$name$_size(); i++) {\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 268 | " total_size +=\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 269 | " ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirtual(\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 270 | " this->$name$(i));\n" |
| 271 | "}\n"); |
| 272 | } |
| 273 | |
| 274 | } // namespace cpp |
| 275 | } // namespace compiler |
| 276 | } // namespace protobuf |
| 277 | } // namespace google |