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_string_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/descriptor.pb.h> |
| 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 SetStringVariables(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); |
liujisi@google.com | 5c20ca1 | 2010-12-21 05:33:13 +0000 | [diff] [blame] | 52 | (*variables)["default"] = DefaultValue(descriptor); |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 53 | (*variables)["default_length"] = |
| 54 | SimpleItoa(descriptor->default_value_string().length()); |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 55 | string default_variable_string = |
| 56 | descriptor->default_value_string().empty() |
| 57 | ? "&::google::protobuf::internal::GetEmptyStringAlreadyInited()" |
| 58 | : "_default_" + FieldName(descriptor) + "_"; |
| 59 | (*variables)["default_variable"] = default_variable_string; |
| 60 | (*variables)["default_value_init"] = |
| 61 | descriptor->default_value_string().empty() |
| 62 | ? "" : "*" + default_variable_string; |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 63 | (*variables)["pointer_type"] = |
| 64 | descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char"; |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 65 | // NOTE: Escaped here to unblock proto1->proto2 migration. |
| 66 | // TODO(liujisi): Extend this to apply for other conflicting methods. |
| 67 | (*variables)["release_name"] = |
| 68 | SafeFunctionName(descriptor->containing_type(), |
| 69 | descriptor, "release_"); |
| 70 | (*variables)["full_name"] = descriptor->full_name(); |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 71 | |
| 72 | (*variables)["string_piece"] = "::std::string"; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | } // namespace |
| 76 | |
| 77 | // =================================================================== |
| 78 | |
| 79 | StringFieldGenerator:: |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 80 | StringFieldGenerator(const FieldDescriptor* descriptor, |
| 81 | const Options& options) |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 82 | : descriptor_(descriptor) { |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 83 | SetStringVariables(descriptor, &variables_, options); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | StringFieldGenerator::~StringFieldGenerator() {} |
| 87 | |
| 88 | void StringFieldGenerator:: |
| 89 | GeneratePrivateMembers(io::Printer* printer) const { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 90 | // N.B. that we continue to use |ArenaStringPtr| instead of |string*| for |
| 91 | // string fields, even when SupportArenas(descriptor_) == false. Why? |
| 92 | // The simple answer is to avoid unmaintainable complexity. The reflection |
| 93 | // code assumes ArenaStringPtrs. These are *almost* in-memory-compatible with |
| 94 | // string*, except for the pointer tags and related ownership semantics. We |
| 95 | // could modify the runtime code to use string* for the not-supporting-arenas |
| 96 | // case, but this would require a way to detect which type of class was |
| 97 | // generated (adding overhead and complexity to GeneratedMessageReflection) |
| 98 | // and littering the runtime code paths with conditionals. It's simpler to |
| 99 | // stick with this but use lightweight accessors that assume arena == NULL. |
| 100 | // There should be very little overhead anyway because it's just a tagged |
| 101 | // pointer in-memory. |
| 102 | printer->Print(variables_, "::google::protobuf::internal::ArenaStringPtr $name$_;\n"); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void StringFieldGenerator:: |
| 106 | GenerateStaticMembers(io::Printer* printer) const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 107 | if (!descriptor_->default_value_string().empty()) { |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 108 | printer->Print(variables_, "static ::std::string* $default_variable$;\n"); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 109 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void StringFieldGenerator:: |
| 113 | GenerateAccessorDeclarations(io::Printer* printer) const { |
| 114 | // If we're using StringFieldGenerator for a field with a ctype, it's |
| 115 | // because that ctype isn't actually implemented. In particular, this is |
| 116 | // true of ctype=CORD and ctype=STRING_PIECE in the open source release. |
| 117 | // We aren't releasing Cord because it has too many Google-specific |
| 118 | // dependencies and we aren't releasing StringPiece because it's hardly |
| 119 | // useful outside of Google and because it would get confusing to have |
| 120 | // multiple instances of the StringPiece class in different libraries (PCRE |
| 121 | // already includes it for their C++ bindings, which came from Google). |
| 122 | // |
| 123 | // In any case, we make all the accessors private while still actually |
| 124 | // using a string to represent the field internally. This way, we can |
| 125 | // guarantee that if we do ever implement the ctype, it won't break any |
| 126 | // existing users who might be -- for whatever reason -- already using .proto |
| 127 | // files that applied the ctype. The field can still be accessed via the |
| 128 | // reflection interface since the reflection interface is independent of |
| 129 | // the string's underlying representation. |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 130 | |
| 131 | bool unknown_ctype = |
| 132 | descriptor_->options().ctype() != EffectiveStringCType(descriptor_); |
| 133 | |
| 134 | if (unknown_ctype) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 135 | printer->Outdent(); |
| 136 | printer->Print( |
| 137 | " private:\n" |
| 138 | " // Hidden due to unknown ctype option.\n"); |
| 139 | printer->Indent(); |
| 140 | } |
| 141 | |
| 142 | printer->Print(variables_, |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 143 | "const ::std::string& $name$() const$deprecation$;\n" |
| 144 | "void set_$name$(const ::std::string& value)$deprecation$;\n" |
| 145 | "void set_$name$(const char* value)$deprecation$;\n" |
| 146 | "void set_$name$(const $pointer_type$* value, size_t size)" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 147 | "$deprecation$;\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 148 | "::std::string* mutable_$name$()$deprecation$;\n" |
| 149 | "::std::string* $release_name$()$deprecation$;\n" |
| 150 | "void set_allocated_$name$(::std::string* $name$)$deprecation$;\n"); |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 151 | if (SupportsArenas(descriptor_)) { |
| 152 | printer->Print(variables_, |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 153 | "::std::string* unsafe_arena_release_$name$()$deprecation$;\n" |
| 154 | "void unsafe_arena_set_allocated_$name$(\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 155 | " ::std::string* $name$)$deprecation$;\n"); |
| 156 | } |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 157 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 158 | |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 159 | if (unknown_ctype) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 160 | printer->Outdent(); |
| 161 | printer->Print(" public:\n"); |
| 162 | printer->Indent(); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void StringFieldGenerator:: |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 167 | GenerateInlineAccessorDefinitions(io::Printer* printer, |
| 168 | bool is_inline) const { |
| 169 | map<string, string> variables(variables_); |
| 170 | variables["inline"] = is_inline ? "inline" : ""; |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 171 | if (SupportsArenas(descriptor_)) { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 172 | printer->Print(variables, |
| 173 | "$inline$ const ::std::string& $classname$::$name$() const {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 174 | " // @@protoc_insertion_point(field_get:$full_name$)\n" |
| 175 | " return $name$_.Get($default_variable$);\n" |
| 176 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 177 | "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 178 | " $set_hasbit$\n" |
| 179 | " $name$_.Set($default_variable$, value, GetArenaNoVirtual());\n" |
| 180 | " // @@protoc_insertion_point(field_set:$full_name$)\n" |
| 181 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 182 | "$inline$ void $classname$::set_$name$(const char* value) {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 183 | " $set_hasbit$\n" |
| 184 | " $name$_.Set($default_variable$, $string_piece$(value),\n" |
| 185 | " GetArenaNoVirtual());\n" |
| 186 | " // @@protoc_insertion_point(field_set_char:$full_name$)\n" |
| 187 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 188 | "$inline$ " |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 189 | "void $classname$::set_$name$(const $pointer_type$* value,\n" |
| 190 | " size_t size) {\n" |
| 191 | " $set_hasbit$\n" |
| 192 | " $name$_.Set($default_variable$, $string_piece$(\n" |
| 193 | " reinterpret_cast<const char*>(value), size), GetArenaNoVirtual());\n" |
| 194 | " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" |
| 195 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 196 | "$inline$ ::std::string* $classname$::mutable_$name$() {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 197 | " $set_hasbit$\n" |
| 198 | " // @@protoc_insertion_point(field_mutable:$full_name$)\n" |
| 199 | " return $name$_.Mutable($default_variable$, GetArenaNoVirtual());\n" |
| 200 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 201 | "$inline$ ::std::string* $classname$::$release_name$() {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 202 | " $clear_hasbit$\n" |
| 203 | " return $name$_.Release($default_variable$, GetArenaNoVirtual());\n" |
| 204 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 205 | "$inline$ ::std::string* $classname$::unsafe_arena_release_$name$() {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 206 | " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" |
| 207 | " $clear_hasbit$\n" |
| 208 | " return $name$_.UnsafeArenaRelease($default_variable$,\n" |
| 209 | " GetArenaNoVirtual());\n" |
| 210 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 211 | "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 212 | " if ($name$ != NULL) {\n" |
| 213 | " $set_hasbit$\n" |
| 214 | " } else {\n" |
| 215 | " $clear_hasbit$\n" |
| 216 | " }\n" |
| 217 | " $name$_.SetAllocated($default_variable$, $name$,\n" |
| 218 | " GetArenaNoVirtual());\n" |
| 219 | " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" |
| 220 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 221 | "$inline$ void $classname$::unsafe_arena_set_allocated_$name$(\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 222 | " ::std::string* $name$) {\n" |
| 223 | " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" |
| 224 | " if ($name$ != NULL) {\n" |
| 225 | " $set_hasbit$\n" |
| 226 | " } else {\n" |
| 227 | " $clear_hasbit$\n" |
| 228 | " }\n" |
| 229 | " $set_hasbit$\n" |
| 230 | " $name$_.UnsafeArenaSetAllocated($default_variable$,\n" |
| 231 | " $name$, GetArenaNoVirtual());\n" |
| 232 | " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" |
| 233 | "}\n"); |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 234 | } else { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 235 | // No-arena case. |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 236 | printer->Print(variables, |
| 237 | "$inline$ const ::std::string& $classname$::$name$() const {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 238 | " // @@protoc_insertion_point(field_get:$full_name$)\n" |
| 239 | " return $name$_.GetNoArena($default_variable$);\n" |
| 240 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 241 | "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 242 | " $set_hasbit$\n" |
| 243 | " $name$_.SetNoArena($default_variable$, value);\n" |
| 244 | " // @@protoc_insertion_point(field_set:$full_name$)\n" |
| 245 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 246 | "$inline$ void $classname$::set_$name$(const char* value) {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 247 | " $set_hasbit$\n" |
| 248 | " $name$_.SetNoArena($default_variable$, $string_piece$(value));\n" |
| 249 | " // @@protoc_insertion_point(field_set_char:$full_name$)\n" |
| 250 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 251 | "$inline$ " |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 252 | "void $classname$::set_$name$(const $pointer_type$* value, " |
| 253 | "size_t size) {\n" |
| 254 | " $set_hasbit$\n" |
| 255 | " $name$_.SetNoArena($default_variable$,\n" |
| 256 | " $string_piece$(reinterpret_cast<const char*>(value), size));\n" |
| 257 | " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" |
| 258 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 259 | "$inline$ ::std::string* $classname$::mutable_$name$() {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 260 | " $set_hasbit$\n" |
| 261 | " // @@protoc_insertion_point(field_mutable:$full_name$)\n" |
| 262 | " return $name$_.MutableNoArena($default_variable$);\n" |
| 263 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 264 | "$inline$ ::std::string* $classname$::$release_name$() {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 265 | " $clear_hasbit$\n" |
| 266 | " return $name$_.ReleaseNoArena($default_variable$);\n" |
| 267 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 268 | "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 269 | " if ($name$ != NULL) {\n" |
| 270 | " $set_hasbit$\n" |
| 271 | " } else {\n" |
| 272 | " $clear_hasbit$\n" |
| 273 | " }\n" |
| 274 | " $name$_.SetAllocatedNoArena($default_variable$, $name$);\n" |
| 275 | " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" |
| 276 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 277 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void StringFieldGenerator:: |
| 281 | GenerateNonInlineAccessorDefinitions(io::Printer* printer) const { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 282 | if (!descriptor_->default_value_string().empty()) { |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 283 | // Initialized in GenerateDefaultInstanceAllocator. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 284 | printer->Print(variables_, |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 285 | "::std::string* $classname$::$default_variable$ = NULL;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | void StringFieldGenerator:: |
| 290 | GenerateClearingCode(io::Printer* printer) const { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 291 | // Two-dimension specialization here: supporting arenas or not, and default |
| 292 | // value is the empty string or not. Complexity here ensures the minimal |
| 293 | // number of branches / amount of extraneous code at runtime (given that the |
| 294 | // below methods are inlined one-liners)! |
| 295 | if (SupportsArenas(descriptor_)) { |
| 296 | if (descriptor_->default_value_string().empty()) { |
| 297 | printer->Print(variables_, |
| 298 | "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n"); |
| 299 | } else { |
| 300 | printer->Print(variables_, |
| 301 | "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n"); |
| 302 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 303 | } else { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 304 | if (descriptor_->default_value_string().empty()) { |
| 305 | printer->Print(variables_, |
| 306 | "$name$_.ClearToEmptyNoArena($default_variable$);\n"); |
| 307 | } else { |
| 308 | printer->Print(variables_, |
| 309 | "$name$_.ClearToDefaultNoArena($default_variable$);\n"); |
| 310 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | void StringFieldGenerator:: |
| 315 | GenerateMergingCode(io::Printer* printer) const { |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 316 | if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) { |
| 317 | // TODO(gpike): improve this |
| 318 | printer->Print(variables_, "set_$name$(from.$name$());\n"); |
| 319 | } else { |
| 320 | printer->Print(variables_, |
| 321 | "$set_hasbit$\n" |
| 322 | "$name$_.AssignWithDefault($default_variable$, from.$name$_);\n"); |
| 323 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void StringFieldGenerator:: |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 327 | GenerateSwappingCode(io::Printer* printer) const { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 328 | printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n"); |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void StringFieldGenerator:: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 332 | GenerateConstructorCode(io::Printer* printer) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 333 | printer->Print(variables_, |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 334 | "$name$_.UnsafeSetDefault($default_variable$);\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | void StringFieldGenerator:: |
| 338 | GenerateDestructorCode(io::Printer* printer) const { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 339 | if (SupportsArenas(descriptor_)) { |
| 340 | printer->Print(variables_, |
| 341 | "$name$_.Destroy($default_variable$, GetArenaNoVirtual());\n"); |
| 342 | } else { |
| 343 | printer->Print(variables_, |
| 344 | "$name$_.DestroyNoArena($default_variable$);\n"); |
| 345 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | void StringFieldGenerator:: |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 349 | GenerateDefaultInstanceAllocator(io::Printer* printer) const { |
| 350 | if (!descriptor_->default_value_string().empty()) { |
| 351 | printer->Print(variables_, |
| 352 | "$classname$::$default_variable$ =\n" |
| 353 | " new ::std::string($default$, $default_length$);\n"); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | void StringFieldGenerator:: |
| 358 | GenerateShutdownCode(io::Printer* printer) const { |
| 359 | if (!descriptor_->default_value_string().empty()) { |
| 360 | printer->Print(variables_, |
liujisi@google.com | 2273ee4 | 2012-12-05 23:47:43 +0000 | [diff] [blame] | 361 | "delete $classname$::$default_variable$;\n"); |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | void StringFieldGenerator:: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 366 | GenerateMergeFromCodedStream(io::Printer* printer) const { |
| 367 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 368 | "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n" |
| 369 | " input, this->mutable_$name$()));\n"); |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 370 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 371 | if (HasUtf8Verification(descriptor_->file()) && |
| 372 | descriptor_->type() == FieldDescriptor::TYPE_STRING) { |
| 373 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 374 | "::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 375 | " this->$name$().data(), this->$name$().length(),\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 376 | " ::google::protobuf::internal::WireFormat::PARSE,\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 377 | " \"$full_name$\");\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 378 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | void StringFieldGenerator:: |
| 382 | GenerateSerializeWithCachedSizes(io::Printer* printer) const { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 383 | if (HasUtf8Verification(descriptor_->file()) && |
| 384 | descriptor_->type() == FieldDescriptor::TYPE_STRING) { |
| 385 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 386 | "::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 387 | " this->$name$().data(), this->$name$().length(),\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 388 | " ::google::protobuf::internal::WireFormat::SERIALIZE,\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 389 | " \"$full_name$\");\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 390 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 391 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 392 | "::google::protobuf::internal::WireFormatLite::Write$declared_type$MaybeAliased(\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 393 | " $number$, this->$name$(), output);\n"); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void StringFieldGenerator:: |
| 397 | GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 398 | if (HasUtf8Verification(descriptor_->file()) && |
| 399 | descriptor_->type() == FieldDescriptor::TYPE_STRING) { |
| 400 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 401 | "::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 402 | " this->$name$().data(), this->$name$().length(),\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 403 | " ::google::protobuf::internal::WireFormat::SERIALIZE,\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 404 | " \"$full_name$\");\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 405 | } |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 406 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 407 | "target =\n" |
| 408 | " ::google::protobuf::internal::WireFormatLite::Write$declared_type$ToArray(\n" |
| 409 | " $number$, this->$name$(), target);\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void StringFieldGenerator:: |
| 413 | GenerateByteSize(io::Printer* printer) const { |
| 414 | printer->Print(variables_, |
| 415 | "total_size += $tag_size$ +\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 416 | " ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n" |
| 417 | " this->$name$());\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // =================================================================== |
| 421 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 422 | StringOneofFieldGenerator:: |
| 423 | StringOneofFieldGenerator(const FieldDescriptor* descriptor, |
| 424 | const Options& options) |
| 425 | : StringFieldGenerator(descriptor, options) { |
| 426 | SetCommonOneofFieldVariables(descriptor, &variables_); |
| 427 | } |
| 428 | |
| 429 | StringOneofFieldGenerator::~StringOneofFieldGenerator() {} |
| 430 | |
| 431 | void StringOneofFieldGenerator:: |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 432 | GenerateInlineAccessorDefinitions(io::Printer* printer, |
| 433 | bool is_inline) const { |
| 434 | map<string, string> variables(variables_); |
| 435 | variables["inline"] = is_inline ? "inline" : ""; |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 436 | if (SupportsArenas(descriptor_)) { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 437 | printer->Print(variables, |
| 438 | "$inline$ const ::std::string& $classname$::$name$() const {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 439 | " // @@protoc_insertion_point(field_get:$full_name$)\n" |
| 440 | " if (has_$name$()) {\n" |
| 441 | " return $oneof_prefix$$name$_.Get($default_variable$);\n" |
| 442 | " }\n" |
| 443 | " return *$default_variable$;\n" |
| 444 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 445 | "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 446 | " if (!has_$name$()) {\n" |
| 447 | " clear_$oneof_name$();\n" |
| 448 | " set_has_$name$();\n" |
| 449 | " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
| 450 | " }\n" |
| 451 | " $oneof_prefix$$name$_.Set($default_variable$, value,\n" |
| 452 | " GetArenaNoVirtual());\n" |
| 453 | " // @@protoc_insertion_point(field_set:$full_name$)\n" |
| 454 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 455 | "$inline$ void $classname$::set_$name$(const char* value) {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 456 | " if (!has_$name$()) {\n" |
| 457 | " clear_$oneof_name$();\n" |
| 458 | " set_has_$name$();\n" |
| 459 | " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
| 460 | " }\n" |
| 461 | " $oneof_prefix$$name$_.Set($default_variable$,\n" |
| 462 | " $string_piece$(value), GetArenaNoVirtual());\n" |
| 463 | " // @@protoc_insertion_point(field_set_char:$full_name$)\n" |
| 464 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 465 | "$inline$ " |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 466 | "void $classname$::set_$name$(const $pointer_type$* value,\n" |
| 467 | " size_t size) {\n" |
| 468 | " if (!has_$name$()) {\n" |
| 469 | " clear_$oneof_name$();\n" |
| 470 | " set_has_$name$();\n" |
| 471 | " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
| 472 | " }\n" |
| 473 | " $oneof_prefix$$name$_.Set($default_variable$, $string_piece$(\n" |
| 474 | " reinterpret_cast<const char*>(value), size),\n" |
| 475 | " GetArenaNoVirtual());\n" |
| 476 | " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" |
| 477 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 478 | "$inline$ ::std::string* $classname$::mutable_$name$() {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 479 | " if (!has_$name$()) {\n" |
| 480 | " clear_$oneof_name$();\n" |
| 481 | " set_has_$name$();\n" |
| 482 | " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
| 483 | " }\n" |
| 484 | " return $oneof_prefix$$name$_.Mutable($default_variable$,\n" |
| 485 | " GetArenaNoVirtual());\n" |
| 486 | " // @@protoc_insertion_point(field_mutable:$full_name$)\n" |
| 487 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 488 | "$inline$ ::std::string* $classname$::$release_name$() {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 489 | " if (has_$name$()) {\n" |
| 490 | " clear_has_$oneof_name$();\n" |
| 491 | " return $oneof_prefix$$name$_.Release($default_variable$,\n" |
| 492 | " GetArenaNoVirtual());\n" |
| 493 | " } else {\n" |
| 494 | " return NULL;\n" |
| 495 | " }\n" |
| 496 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 497 | "$inline$ ::std::string* $classname$::unsafe_arena_release_$name$() {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 498 | " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" |
| 499 | " if (has_$name$()) {\n" |
| 500 | " clear_has_$oneof_name$();\n" |
| 501 | " return $oneof_prefix$$name$_.UnsafeArenaRelease(\n" |
| 502 | " $default_variable$, GetArenaNoVirtual());\n" |
| 503 | " } else {\n" |
| 504 | " return NULL;\n" |
| 505 | " }\n" |
| 506 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 507 | "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 508 | " if (!has_$name$()) {\n" |
| 509 | " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
| 510 | " }\n" |
| 511 | " clear_$oneof_name$();\n" |
| 512 | " if ($name$ != NULL) {\n" |
| 513 | " set_has_$name$();\n" |
| 514 | " $oneof_prefix$$name$_.SetAllocated($default_variable$, $name$,\n" |
| 515 | " GetArenaNoVirtual());\n" |
| 516 | " }\n" |
| 517 | " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" |
| 518 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 519 | "$inline$ void $classname$::unsafe_arena_set_allocated_$name$(" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 520 | "::std::string* $name$) {\n" |
| 521 | " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" |
| 522 | " if (!has_$name$()) {\n" |
| 523 | " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
| 524 | " }\n" |
| 525 | " clear_$oneof_name$();\n" |
| 526 | " if ($name$) {\n" |
| 527 | " set_has_$name$();\n" |
| 528 | " $oneof_prefix$$name$_.UnsafeArenaSetAllocated($default_variable$, " |
| 529 | "$name$, GetArenaNoVirtual());\n" |
| 530 | " }\n" |
| 531 | " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" |
| 532 | "}\n"); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 533 | } else { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 534 | // No-arena case. |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 535 | printer->Print(variables, |
| 536 | "$inline$ const ::std::string& $classname$::$name$() const {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 537 | " // @@protoc_insertion_point(field_get:$full_name$)\n" |
| 538 | " if (has_$name$()) {\n" |
| 539 | " return $oneof_prefix$$name$_.GetNoArena($default_variable$);\n" |
| 540 | " }\n" |
| 541 | " return *$default_variable$;\n" |
| 542 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 543 | "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 544 | " // @@protoc_insertion_point(field_set:$full_name$)\n" |
| 545 | " if (!has_$name$()) {\n" |
| 546 | " clear_$oneof_name$();\n" |
| 547 | " set_has_$name$();\n" |
| 548 | " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
| 549 | " }\n" |
| 550 | " $oneof_prefix$$name$_.SetNoArena($default_variable$, value);\n" |
| 551 | " // @@protoc_insertion_point(field_set:$full_name$)\n" |
| 552 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 553 | "$inline$ void $classname$::set_$name$(const char* value) {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 554 | " if (!has_$name$()) {\n" |
| 555 | " clear_$oneof_name$();\n" |
| 556 | " set_has_$name$();\n" |
| 557 | " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
| 558 | " }\n" |
| 559 | " $oneof_prefix$$name$_.SetNoArena($default_variable$,\n" |
| 560 | " $string_piece$(value));\n" |
| 561 | " // @@protoc_insertion_point(field_set_char:$full_name$)\n" |
| 562 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 563 | "$inline$ " |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 564 | "void $classname$::set_$name$(const $pointer_type$* value, size_t size) {\n" |
| 565 | " if (!has_$name$()) {\n" |
| 566 | " clear_$oneof_name$();\n" |
| 567 | " set_has_$name$();\n" |
| 568 | " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
| 569 | " }\n" |
| 570 | " $oneof_prefix$$name$_.SetNoArena($default_variable$, $string_piece$(\n" |
| 571 | " reinterpret_cast<const char*>(value), size));\n" |
| 572 | " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" |
| 573 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 574 | "$inline$ ::std::string* $classname$::mutable_$name$() {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 575 | " if (!has_$name$()) {\n" |
| 576 | " clear_$oneof_name$();\n" |
| 577 | " set_has_$name$();\n" |
| 578 | " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
| 579 | " }\n" |
| 580 | " // @@protoc_insertion_point(field_mutable:$full_name$)\n" |
| 581 | " return $oneof_prefix$$name$_.MutableNoArena($default_variable$);\n" |
| 582 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 583 | "$inline$ ::std::string* $classname$::$release_name$() {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 584 | " if (has_$name$()) {\n" |
| 585 | " clear_has_$oneof_name$();\n" |
| 586 | " return $oneof_prefix$$name$_.ReleaseNoArena($default_variable$);\n" |
| 587 | " } else {\n" |
| 588 | " return NULL;\n" |
| 589 | " }\n" |
| 590 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 591 | "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 592 | " if (!has_$name$()) {\n" |
| 593 | " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" |
| 594 | " }\n" |
| 595 | " clear_$oneof_name$();\n" |
| 596 | " if ($name$ != NULL) {\n" |
| 597 | " set_has_$name$();\n" |
| 598 | " $oneof_prefix$$name$_.SetAllocatedNoArena($default_variable$,\n" |
| 599 | " $name$);\n" |
| 600 | " }\n" |
| 601 | " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" |
| 602 | "}\n"); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 603 | } |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | void StringOneofFieldGenerator:: |
| 607 | GenerateClearingCode(io::Printer* printer) const { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 608 | if (SupportsArenas(descriptor_)) { |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 609 | printer->Print(variables_, |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 610 | "$oneof_prefix$$name$_.Destroy($default_variable$,\n" |
| 611 | " GetArenaNoVirtual());\n"); |
| 612 | } else { |
| 613 | printer->Print(variables_, |
| 614 | "$oneof_prefix$$name$_.DestroyNoArena($default_variable$);\n"); |
| 615 | } |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | void StringOneofFieldGenerator:: |
| 619 | GenerateSwappingCode(io::Printer* printer) const { |
| 620 | // Don't print any swapping code. Swapping the union will swap this field. |
| 621 | } |
| 622 | |
| 623 | void StringOneofFieldGenerator:: |
| 624 | GenerateConstructorCode(io::Printer* printer) const { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 625 | printer->Print(variables_, |
Nobuaki Sukegawa | 2413cb5 | 2014-11-29 22:03:31 +0900 | [diff] [blame] | 626 | " $classname$_default_oneof_instance_->$name$_.UnsafeSetDefault(" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 627 | "$default_variable$);\n"); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void StringOneofFieldGenerator:: |
| 631 | GenerateDestructorCode(io::Printer* printer) const { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 632 | if (SupportsArenas(descriptor_)) { |
| 633 | printer->Print(variables_, |
| 634 | "if (has_$name$()) {\n" |
| 635 | " $oneof_prefix$$name$_.Destroy($default_variable$,\n" |
| 636 | " GetArenaNoVirtual());\n" |
| 637 | "}\n"); |
| 638 | } else { |
| 639 | printer->Print(variables_, |
| 640 | "if (has_$name$()) {\n" |
| 641 | " $oneof_prefix$$name$_.DestroyNoArena($default_variable$);\n" |
| 642 | "}\n"); |
| 643 | } |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 646 | void StringOneofFieldGenerator:: |
| 647 | GenerateMergeFromCodedStream(io::Printer* printer) const { |
| 648 | printer->Print(variables_, |
| 649 | "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n" |
| 650 | " input, this->mutable_$name$()));\n"); |
| 651 | |
| 652 | if (HasUtf8Verification(descriptor_->file()) && |
| 653 | descriptor_->type() == FieldDescriptor::TYPE_STRING) { |
| 654 | printer->Print(variables_, |
| 655 | "::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n" |
| 656 | " this->$name$().data(), this->$name$().length(),\n" |
| 657 | " ::google::protobuf::internal::WireFormat::PARSE,\n" |
| 658 | " \"$full_name$\");\n"); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 663 | // =================================================================== |
| 664 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 665 | RepeatedStringFieldGenerator:: |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 666 | RepeatedStringFieldGenerator(const FieldDescriptor* descriptor, |
| 667 | const Options& options) |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 668 | : descriptor_(descriptor) { |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 669 | SetStringVariables(descriptor, &variables_, options); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | RepeatedStringFieldGenerator::~RepeatedStringFieldGenerator() {} |
| 673 | |
| 674 | void RepeatedStringFieldGenerator:: |
| 675 | GeneratePrivateMembers(io::Printer* printer) const { |
| 676 | printer->Print(variables_, |
| 677 | "::google::protobuf::RepeatedPtrField< ::std::string> $name$_;\n"); |
| 678 | } |
| 679 | |
| 680 | void RepeatedStringFieldGenerator:: |
| 681 | GenerateAccessorDeclarations(io::Printer* printer) const { |
| 682 | // See comment above about unknown ctypes. |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 683 | bool unknown_ctype = |
| 684 | descriptor_->options().ctype() != EffectiveStringCType(descriptor_); |
| 685 | |
| 686 | if (unknown_ctype) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 687 | printer->Outdent(); |
| 688 | printer->Print( |
| 689 | " private:\n" |
| 690 | " // Hidden due to unknown ctype option.\n"); |
| 691 | printer->Indent(); |
| 692 | } |
| 693 | |
| 694 | printer->Print(variables_, |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 695 | "const ::std::string& $name$(int index) const$deprecation$;\n" |
| 696 | "::std::string* mutable_$name$(int index)$deprecation$;\n" |
| 697 | "void set_$name$(int index, const ::std::string& value)$deprecation$;\n" |
| 698 | "void set_$name$(int index, const char* value)$deprecation$;\n" |
| 699 | "" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 700 | "void set_$name$(int index, const $pointer_type$* value, size_t size)" |
| 701 | "$deprecation$;\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 702 | "::std::string* add_$name$()$deprecation$;\n" |
| 703 | "void add_$name$(const ::std::string& value)$deprecation$;\n" |
| 704 | "void add_$name$(const char* value)$deprecation$;\n" |
| 705 | "void add_$name$(const $pointer_type$* value, size_t size)" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 706 | "$deprecation$;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 707 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 708 | printer->Print(variables_, |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 709 | "const ::google::protobuf::RepeatedPtrField< ::std::string>& $name$() const" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 710 | "$deprecation$;\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 711 | "::google::protobuf::RepeatedPtrField< ::std::string>* mutable_$name$()" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 712 | "$deprecation$;\n"); |
| 713 | |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 714 | if (unknown_ctype) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 715 | printer->Outdent(); |
| 716 | printer->Print(" public:\n"); |
| 717 | printer->Indent(); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | void RepeatedStringFieldGenerator:: |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 722 | GenerateInlineAccessorDefinitions(io::Printer* printer, |
| 723 | bool is_inline) const { |
| 724 | map<string, string> variables(variables_); |
| 725 | variables["inline"] = is_inline ? "inline" : ""; |
| 726 | printer->Print(variables, |
| 727 | "$inline$ const ::std::string& $classname$::$name$(int index) const {\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 728 | " // @@protoc_insertion_point(field_get:$full_name$)\n" |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 729 | " return $name$_.$cppget$(index);\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 730 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 731 | "$inline$ ::std::string* $classname$::mutable_$name$(int index) {\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 732 | " // @@protoc_insertion_point(field_mutable:$full_name$)\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 733 | " return $name$_.Mutable(index);\n" |
| 734 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 735 | "$inline$ void $classname$::set_$name$(int index, const ::std::string& value) {\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 736 | " // @@protoc_insertion_point(field_set:$full_name$)\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 737 | " $name$_.Mutable(index)->assign(value);\n" |
| 738 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 739 | "$inline$ void $classname$::set_$name$(int index, const char* value) {\n" |
temporal | f206351 | 2008-07-23 01:19:07 +0000 | [diff] [blame] | 740 | " $name$_.Mutable(index)->assign(value);\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 741 | " // @@protoc_insertion_point(field_set_char:$full_name$)\n" |
temporal | f206351 | 2008-07-23 01:19:07 +0000 | [diff] [blame] | 742 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 743 | "$inline$ void " |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 744 | "$classname$::set_$name$" |
| 745 | "(int index, const $pointer_type$* value, size_t size) {\n" |
| 746 | " $name$_.Mutable(index)->assign(\n" |
| 747 | " reinterpret_cast<const char*>(value), size);\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 748 | " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 749 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 750 | "$inline$ ::std::string* $classname$::add_$name$() {\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 751 | " return $name$_.Add();\n" |
| 752 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 753 | "$inline$ void $classname$::add_$name$(const ::std::string& value) {\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 754 | " $name$_.Add()->assign(value);\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 755 | " // @@protoc_insertion_point(field_add:$full_name$)\n" |
temporal | f206351 | 2008-07-23 01:19:07 +0000 | [diff] [blame] | 756 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 757 | "$inline$ void $classname$::add_$name$(const char* value) {\n" |
temporal | f206351 | 2008-07-23 01:19:07 +0000 | [diff] [blame] | 758 | " $name$_.Add()->assign(value);\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 759 | " // @@protoc_insertion_point(field_add_char:$full_name$)\n" |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 760 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 761 | "$inline$ void " |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 762 | "$classname$::add_$name$(const $pointer_type$* value, size_t size) {\n" |
| 763 | " $name$_.Add()->assign(reinterpret_cast<const char*>(value), size);\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 764 | " // @@protoc_insertion_point(field_add_pointer:$full_name$)\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 765 | "}\n"); |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 766 | printer->Print(variables, |
| 767 | "$inline$ const ::google::protobuf::RepeatedPtrField< ::std::string>&\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 768 | "$classname$::$name$() const {\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 769 | " // @@protoc_insertion_point(field_list:$full_name$)\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 770 | " return $name$_;\n" |
| 771 | "}\n" |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 772 | "$inline$ ::google::protobuf::RepeatedPtrField< ::std::string>*\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 773 | "$classname$::mutable_$name$() {\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 774 | " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 775 | " return &$name$_;\n" |
| 776 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | void RepeatedStringFieldGenerator:: |
| 780 | GenerateClearingCode(io::Printer* printer) const { |
| 781 | printer->Print(variables_, "$name$_.Clear();\n"); |
| 782 | } |
| 783 | |
| 784 | void RepeatedStringFieldGenerator:: |
| 785 | GenerateMergingCode(io::Printer* printer) const { |
| 786 | printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n"); |
| 787 | } |
| 788 | |
| 789 | void RepeatedStringFieldGenerator:: |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 790 | GenerateSwappingCode(io::Printer* printer) const { |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 791 | printer->Print(variables_, "$name$_.UnsafeArenaSwap(&other->$name$_);\n"); |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | void RepeatedStringFieldGenerator:: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 795 | GenerateConstructorCode(io::Printer* printer) const { |
| 796 | // Not needed for repeated fields. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | void RepeatedStringFieldGenerator:: |
| 800 | GenerateMergeFromCodedStream(io::Printer* printer) const { |
| 801 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 802 | "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n" |
| 803 | " input, this->add_$name$()));\n"); |
| 804 | if (HasUtf8Verification(descriptor_->file()) && |
| 805 | descriptor_->type() == FieldDescriptor::TYPE_STRING) { |
| 806 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 807 | "::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n" |
xiaofeng@google.com | 2072421 | 2012-05-16 05:41:31 +0000 | [diff] [blame] | 808 | " this->$name$(this->$name$_size() - 1).data(),\n" |
| 809 | " this->$name$(this->$name$_size() - 1).length(),\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 810 | " ::google::protobuf::internal::WireFormat::PARSE,\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 811 | " \"$full_name$\");\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 812 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | void RepeatedStringFieldGenerator:: |
| 816 | GenerateSerializeWithCachedSizes(io::Printer* printer) const { |
| 817 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 818 | "for (int i = 0; i < this->$name$_size(); i++) {\n"); |
| 819 | if (HasUtf8Verification(descriptor_->file()) && |
| 820 | descriptor_->type() == FieldDescriptor::TYPE_STRING) { |
| 821 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 822 | "::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 823 | " this->$name$(i).data(), this->$name$(i).length(),\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 824 | " ::google::protobuf::internal::WireFormat::SERIALIZE,\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 825 | " \"$full_name$\");\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 826 | } |
| 827 | printer->Print(variables_, |
| 828 | " ::google::protobuf::internal::WireFormatLite::Write$declared_type$(\n" |
| 829 | " $number$, this->$name$(i), output);\n" |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 830 | "}\n"); |
| 831 | } |
| 832 | |
| 833 | void RepeatedStringFieldGenerator:: |
| 834 | GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { |
| 835 | printer->Print(variables_, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 836 | "for (int i = 0; i < this->$name$_size(); i++) {\n"); |
| 837 | if (HasUtf8Verification(descriptor_->file()) && |
| 838 | descriptor_->type() == FieldDescriptor::TYPE_STRING) { |
| 839 | printer->Print(variables_, |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 840 | " ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 841 | " this->$name$(i).data(), this->$name$(i).length(),\n" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 842 | " ::google::protobuf::internal::WireFormat::SERIALIZE,\n" |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 843 | " \"$full_name$\");\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 844 | } |
| 845 | printer->Print(variables_, |
| 846 | " target = ::google::protobuf::internal::WireFormatLite::\n" |
| 847 | " Write$declared_type$ToArray($number$, this->$name$(i), target);\n" |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 848 | "}\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | void RepeatedStringFieldGenerator:: |
| 852 | GenerateByteSize(io::Printer* printer) const { |
| 853 | printer->Print(variables_, |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 854 | "total_size += $tag_size$ * this->$name$_size();\n" |
| 855 | "for (int i = 0; i < this->$name$_size(); i++) {\n" |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 856 | " total_size += ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n" |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 857 | " this->$name$(i));\n" |
| 858 | "}\n"); |
| 859 | } |
| 860 | |
| 861 | } // namespace cpp |
| 862 | } // namespace compiler |
| 863 | } // namespace protobuf |
| 864 | } // namespace google |