blob: 1a1bcd3d2ee7d00a8bed95b7b66c470f2dd7d616 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
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>
temporal40ee5512008-07-10 02:12:20 +000038#include <google/protobuf/descriptor.pb.h>
39#include <google/protobuf/stubs/strutil.h>
40
41namespace google {
42namespace protobuf {
43namespace compiler {
44namespace cpp {
45
temporal40ee5512008-07-10 02:12:20 +000046namespace {
47
temporal40ee5512008-07-10 02:12:20 +000048void SetStringVariables(const FieldDescriptor* descriptor,
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000049 map<string, string>* variables,
50 const Options& options) {
51 SetCommonFieldVariables(descriptor, variables, options);
liujisi@google.com5c20ca12010-12-21 05:33:13 +000052 (*variables)["default"] = DefaultValue(descriptor);
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000053 (*variables)["default_length"] =
54 SimpleItoa(descriptor->default_value_string().length());
Feng Xiao6ef984a2014-11-10 17:34:54 -080055 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.comd37d46d2009-04-25 02:53:47 +000063 (*variables)["pointer_type"] =
64 descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char";
jieluo@google.com4de8f552014-07-18 00:47:59 +000065 // 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 Xiao6ef984a2014-11-10 17:34:54 -080071
72 (*variables)["string_piece"] = "::std::string";
temporal40ee5512008-07-10 02:12:20 +000073}
74
75} // namespace
76
77// ===================================================================
78
79StringFieldGenerator::
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000080StringFieldGenerator(const FieldDescriptor* descriptor,
81 const Options& options)
temporal40ee5512008-07-10 02:12:20 +000082 : descriptor_(descriptor) {
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +000083 SetStringVariables(descriptor, &variables_, options);
temporal40ee5512008-07-10 02:12:20 +000084}
85
86StringFieldGenerator::~StringFieldGenerator() {}
87
88void StringFieldGenerator::
89GeneratePrivateMembers(io::Printer* printer) const {
Feng Xiao6ef984a2014-11-10 17:34:54 -080090 // 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.com4de8f552014-07-18 00:47:59 +0000103}
104
105void StringFieldGenerator::
106GenerateStaticMembers(io::Printer* printer) const {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000107 if (!descriptor_->default_value_string().empty()) {
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000108 printer->Print(variables_, "static ::std::string* $default_variable$;\n");
liujisi@google.com33165fe2010-11-02 13:14:58 +0000109 }
temporal40ee5512008-07-10 02:12:20 +0000110}
111
112void StringFieldGenerator::
113GenerateAccessorDeclarations(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 Liu885b6122015-02-28 14:51:22 -0800130
131 bool unknown_ctype =
132 descriptor_->options().ctype() != EffectiveStringCType(descriptor_);
133
134 if (unknown_ctype) {
temporal40ee5512008-07-10 02:12:20 +0000135 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 Liu885b6122015-02-28 14:51:22 -0800143 "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.com80b1d622009-07-29 01:13:20 +0000147 "$deprecation$;\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800148 "::std::string* mutable_$name$()$deprecation$;\n"
149 "::std::string* $release_name$()$deprecation$;\n"
150 "void set_allocated_$name$(::std::string* $name$)$deprecation$;\n");
Feng Xiao6ef984a2014-11-10 17:34:54 -0800151 if (SupportsArenas(descriptor_)) {
152 printer->Print(variables_,
Jisi Liu885b6122015-02-28 14:51:22 -0800153 "::std::string* unsafe_arena_release_$name$()$deprecation$;\n"
154 "void unsafe_arena_set_allocated_$name$(\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800155 " ::std::string* $name$)$deprecation$;\n");
156 }
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000157
temporal40ee5512008-07-10 02:12:20 +0000158
Jisi Liu885b6122015-02-28 14:51:22 -0800159 if (unknown_ctype) {
temporal40ee5512008-07-10 02:12:20 +0000160 printer->Outdent();
161 printer->Print(" public:\n");
162 printer->Indent();
163 }
164}
165
166void StringFieldGenerator::
Jisi Liu885b6122015-02-28 14:51:22 -0800167GenerateInlineAccessorDefinitions(io::Printer* printer,
168 bool is_inline) const {
169 map<string, string> variables(variables_);
170 variables["inline"] = is_inline ? "inline" : "";
Feng Xiao6ef984a2014-11-10 17:34:54 -0800171 if (SupportsArenas(descriptor_)) {
Jisi Liu885b6122015-02-28 14:51:22 -0800172 printer->Print(variables,
173 "$inline$ const ::std::string& $classname$::$name$() const {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800174 " // @@protoc_insertion_point(field_get:$full_name$)\n"
175 " return $name$_.Get($default_variable$);\n"
176 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800177 "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800178 " $set_hasbit$\n"
179 " $name$_.Set($default_variable$, value, GetArenaNoVirtual());\n"
180 " // @@protoc_insertion_point(field_set:$full_name$)\n"
181 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800182 "$inline$ void $classname$::set_$name$(const char* value) {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800183 " $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 Liu885b6122015-02-28 14:51:22 -0800188 "$inline$ "
Feng Xiao6ef984a2014-11-10 17:34:54 -0800189 "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 Liu885b6122015-02-28 14:51:22 -0800196 "$inline$ ::std::string* $classname$::mutable_$name$() {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800197 " $set_hasbit$\n"
198 " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
199 " return $name$_.Mutable($default_variable$, GetArenaNoVirtual());\n"
200 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800201 "$inline$ ::std::string* $classname$::$release_name$() {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800202 " $clear_hasbit$\n"
203 " return $name$_.Release($default_variable$, GetArenaNoVirtual());\n"
204 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800205 "$inline$ ::std::string* $classname$::unsafe_arena_release_$name$() {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800206 " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n"
207 " $clear_hasbit$\n"
208 " return $name$_.UnsafeArenaRelease($default_variable$,\n"
209 " GetArenaNoVirtual());\n"
210 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800211 "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800212 " 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 Liu885b6122015-02-28 14:51:22 -0800221 "$inline$ void $classname$::unsafe_arena_set_allocated_$name$(\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800222 " ::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.com26bd9ee2008-11-21 00:06:27 +0000234 } else {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800235 // No-arena case.
Jisi Liu885b6122015-02-28 14:51:22 -0800236 printer->Print(variables,
237 "$inline$ const ::std::string& $classname$::$name$() const {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800238 " // @@protoc_insertion_point(field_get:$full_name$)\n"
239 " return $name$_.GetNoArena($default_variable$);\n"
240 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800241 "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800242 " $set_hasbit$\n"
243 " $name$_.SetNoArena($default_variable$, value);\n"
244 " // @@protoc_insertion_point(field_set:$full_name$)\n"
245 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800246 "$inline$ void $classname$::set_$name$(const char* value) {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800247 " $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 Liu885b6122015-02-28 14:51:22 -0800251 "$inline$ "
Feng Xiao6ef984a2014-11-10 17:34:54 -0800252 "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 Liu885b6122015-02-28 14:51:22 -0800259 "$inline$ ::std::string* $classname$::mutable_$name$() {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800260 " $set_hasbit$\n"
261 " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
262 " return $name$_.MutableNoArena($default_variable$);\n"
263 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800264 "$inline$ ::std::string* $classname$::$release_name$() {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800265 " $clear_hasbit$\n"
266 " return $name$_.ReleaseNoArena($default_variable$);\n"
267 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800268 "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800269 " 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");
temporal40ee5512008-07-10 02:12:20 +0000277 }
temporal40ee5512008-07-10 02:12:20 +0000278}
279
280void StringFieldGenerator::
281GenerateNonInlineAccessorDefinitions(io::Printer* printer) const {
liujisi@google.com33165fe2010-11-02 13:14:58 +0000282 if (!descriptor_->default_value_string().empty()) {
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000283 // Initialized in GenerateDefaultInstanceAllocator.
temporal40ee5512008-07-10 02:12:20 +0000284 printer->Print(variables_,
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000285 "::std::string* $classname$::$default_variable$ = NULL;\n");
temporal40ee5512008-07-10 02:12:20 +0000286 }
287}
288
289void StringFieldGenerator::
290GenerateClearingCode(io::Printer* printer) const {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800291 // 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 }
temporal40ee5512008-07-10 02:12:20 +0000303 } else {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800304 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 }
temporal40ee5512008-07-10 02:12:20 +0000311 }
312}
313
314void StringFieldGenerator::
315GenerateMergingCode(io::Printer* printer) const {
Feng Xiaof157a562014-11-14 11:50:31 -0800316 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 }
temporal40ee5512008-07-10 02:12:20 +0000324}
325
326void StringFieldGenerator::
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000327GenerateSwappingCode(io::Printer* printer) const {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800328 printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n");
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000329}
330
331void StringFieldGenerator::
kenton@google.comd37d46d2009-04-25 02:53:47 +0000332GenerateConstructorCode(io::Printer* printer) const {
temporal40ee5512008-07-10 02:12:20 +0000333 printer->Print(variables_,
Feng Xiao6ef984a2014-11-10 17:34:54 -0800334 "$name$_.UnsafeSetDefault($default_variable$);\n");
temporal40ee5512008-07-10 02:12:20 +0000335}
336
337void StringFieldGenerator::
338GenerateDestructorCode(io::Printer* printer) const {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800339 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 }
temporal40ee5512008-07-10 02:12:20 +0000346}
347
348void StringFieldGenerator::
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000349GenerateDefaultInstanceAllocator(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
357void StringFieldGenerator::
358GenerateShutdownCode(io::Printer* printer) const {
359 if (!descriptor_->default_value_string().empty()) {
360 printer->Print(variables_,
liujisi@google.com2273ee42012-12-05 23:47:43 +0000361 "delete $classname$::$default_variable$;\n");
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000362 }
363}
364
365void StringFieldGenerator::
temporal40ee5512008-07-10 02:12:20 +0000366GenerateMergeFromCodedStream(io::Printer* printer) const {
367 printer->Print(variables_,
kenton@google.com80b1d622009-07-29 01:13:20 +0000368 "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n"
369 " input, this->mutable_$name$()));\n");
Feng Xiao6ef984a2014-11-10 17:34:54 -0800370
kenton@google.com80b1d622009-07-29 01:13:20 +0000371 if (HasUtf8Verification(descriptor_->file()) &&
372 descriptor_->type() == FieldDescriptor::TYPE_STRING) {
373 printer->Print(variables_,
jieluo@google.com4de8f552014-07-18 00:47:59 +0000374 "::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n"
kenton@google.com80b1d622009-07-29 01:13:20 +0000375 " this->$name$().data(), this->$name$().length(),\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000376 " ::google::protobuf::internal::WireFormat::PARSE,\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800377 " \"$full_name$\");\n");
kenton@google.com80b1d622009-07-29 01:13:20 +0000378 }
temporal40ee5512008-07-10 02:12:20 +0000379}
380
381void StringFieldGenerator::
382GenerateSerializeWithCachedSizes(io::Printer* printer) const {
kenton@google.com80b1d622009-07-29 01:13:20 +0000383 if (HasUtf8Verification(descriptor_->file()) &&
384 descriptor_->type() == FieldDescriptor::TYPE_STRING) {
385 printer->Print(variables_,
jieluo@google.com4de8f552014-07-18 00:47:59 +0000386 "::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n"
kenton@google.com80b1d622009-07-29 01:13:20 +0000387 " this->$name$().data(), this->$name$().length(),\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000388 " ::google::protobuf::internal::WireFormat::SERIALIZE,\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800389 " \"$full_name$\");\n");
kenton@google.com80b1d622009-07-29 01:13:20 +0000390 }
temporal40ee5512008-07-10 02:12:20 +0000391 printer->Print(variables_,
jieluo@google.com4de8f552014-07-18 00:47:59 +0000392 "::google::protobuf::internal::WireFormatLite::Write$declared_type$MaybeAliased(\n"
kenton@google.com80b1d622009-07-29 01:13:20 +0000393 " $number$, this->$name$(), output);\n");
kenton@google.comd37d46d2009-04-25 02:53:47 +0000394}
395
396void StringFieldGenerator::
397GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
kenton@google.com80b1d622009-07-29 01:13:20 +0000398 if (HasUtf8Verification(descriptor_->file()) &&
399 descriptor_->type() == FieldDescriptor::TYPE_STRING) {
400 printer->Print(variables_,
jieluo@google.com4de8f552014-07-18 00:47:59 +0000401 "::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n"
kenton@google.com80b1d622009-07-29 01:13:20 +0000402 " this->$name$().data(), this->$name$().length(),\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000403 " ::google::protobuf::internal::WireFormat::SERIALIZE,\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800404 " \"$full_name$\");\n");
kenton@google.com80b1d622009-07-29 01:13:20 +0000405 }
kenton@google.comd37d46d2009-04-25 02:53:47 +0000406 printer->Print(variables_,
kenton@google.com80b1d622009-07-29 01:13:20 +0000407 "target =\n"
408 " ::google::protobuf::internal::WireFormatLite::Write$declared_type$ToArray(\n"
409 " $number$, this->$name$(), target);\n");
temporal40ee5512008-07-10 02:12:20 +0000410}
411
412void StringFieldGenerator::
413GenerateByteSize(io::Printer* printer) const {
414 printer->Print(variables_,
415 "total_size += $tag_size$ +\n"
kenton@google.com80b1d622009-07-29 01:13:20 +0000416 " ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n"
417 " this->$name$());\n");
temporal40ee5512008-07-10 02:12:20 +0000418}
419
420// ===================================================================
421
jieluo@google.com4de8f552014-07-18 00:47:59 +0000422StringOneofFieldGenerator::
423StringOneofFieldGenerator(const FieldDescriptor* descriptor,
424 const Options& options)
425 : StringFieldGenerator(descriptor, options) {
426 SetCommonOneofFieldVariables(descriptor, &variables_);
427}
428
429StringOneofFieldGenerator::~StringOneofFieldGenerator() {}
430
431void StringOneofFieldGenerator::
Jisi Liu885b6122015-02-28 14:51:22 -0800432GenerateInlineAccessorDefinitions(io::Printer* printer,
433 bool is_inline) const {
434 map<string, string> variables(variables_);
435 variables["inline"] = is_inline ? "inline" : "";
Feng Xiao6ef984a2014-11-10 17:34:54 -0800436 if (SupportsArenas(descriptor_)) {
Jisi Liu885b6122015-02-28 14:51:22 -0800437 printer->Print(variables,
438 "$inline$ const ::std::string& $classname$::$name$() const {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800439 " // @@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 Liu885b6122015-02-28 14:51:22 -0800445 "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800446 " 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 Liu885b6122015-02-28 14:51:22 -0800455 "$inline$ void $classname$::set_$name$(const char* value) {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800456 " 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 Liu885b6122015-02-28 14:51:22 -0800465 "$inline$ "
Feng Xiao6ef984a2014-11-10 17:34:54 -0800466 "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 Liu885b6122015-02-28 14:51:22 -0800478 "$inline$ ::std::string* $classname$::mutable_$name$() {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800479 " 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 Liu885b6122015-02-28 14:51:22 -0800488 "$inline$ ::std::string* $classname$::$release_name$() {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800489 " 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 Liu885b6122015-02-28 14:51:22 -0800497 "$inline$ ::std::string* $classname$::unsafe_arena_release_$name$() {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800498 " 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 Liu885b6122015-02-28 14:51:22 -0800507 "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800508 " 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 Liu885b6122015-02-28 14:51:22 -0800519 "$inline$ void $classname$::unsafe_arena_set_allocated_$name$("
Feng Xiao6ef984a2014-11-10 17:34:54 -0800520 "::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.com4de8f552014-07-18 00:47:59 +0000533 } else {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800534 // No-arena case.
Jisi Liu885b6122015-02-28 14:51:22 -0800535 printer->Print(variables,
536 "$inline$ const ::std::string& $classname$::$name$() const {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800537 " // @@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 Liu885b6122015-02-28 14:51:22 -0800543 "$inline$ void $classname$::set_$name$(const ::std::string& value) {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800544 " // @@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 Liu885b6122015-02-28 14:51:22 -0800553 "$inline$ void $classname$::set_$name$(const char* value) {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800554 " 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 Liu885b6122015-02-28 14:51:22 -0800563 "$inline$ "
Feng Xiao6ef984a2014-11-10 17:34:54 -0800564 "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 Liu885b6122015-02-28 14:51:22 -0800574 "$inline$ ::std::string* $classname$::mutable_$name$() {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800575 " 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 Liu885b6122015-02-28 14:51:22 -0800583 "$inline$ ::std::string* $classname$::$release_name$() {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800584 " 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 Liu885b6122015-02-28 14:51:22 -0800591 "$inline$ void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800592 " 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.com4de8f552014-07-18 00:47:59 +0000603 }
jieluo@google.com4de8f552014-07-18 00:47:59 +0000604}
605
606void StringOneofFieldGenerator::
607GenerateClearingCode(io::Printer* printer) const {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800608 if (SupportsArenas(descriptor_)) {
jieluo@google.com4de8f552014-07-18 00:47:59 +0000609 printer->Print(variables_,
Feng Xiao6ef984a2014-11-10 17:34:54 -0800610 "$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.com4de8f552014-07-18 00:47:59 +0000616}
617
618void StringOneofFieldGenerator::
619GenerateSwappingCode(io::Printer* printer) const {
620 // Don't print any swapping code. Swapping the union will swap this field.
621}
622
623void StringOneofFieldGenerator::
624GenerateConstructorCode(io::Printer* printer) const {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800625 printer->Print(variables_,
Nobuaki Sukegawa2413cb52014-11-29 22:03:31 +0900626 " $classname$_default_oneof_instance_->$name$_.UnsafeSetDefault("
Feng Xiao6ef984a2014-11-10 17:34:54 -0800627 "$default_variable$);\n");
jieluo@google.com4de8f552014-07-18 00:47:59 +0000628}
629
630void StringOneofFieldGenerator::
631GenerateDestructorCode(io::Printer* printer) const {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800632 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.com4de8f552014-07-18 00:47:59 +0000644}
645
Feng Xiao6ef984a2014-11-10 17:34:54 -0800646void StringOneofFieldGenerator::
647GenerateMergeFromCodedStream(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.com4de8f552014-07-18 00:47:59 +0000663// ===================================================================
664
temporal40ee5512008-07-10 02:12:20 +0000665RepeatedStringFieldGenerator::
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000666RepeatedStringFieldGenerator(const FieldDescriptor* descriptor,
667 const Options& options)
temporal40ee5512008-07-10 02:12:20 +0000668 : descriptor_(descriptor) {
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000669 SetStringVariables(descriptor, &variables_, options);
temporal40ee5512008-07-10 02:12:20 +0000670}
671
672RepeatedStringFieldGenerator::~RepeatedStringFieldGenerator() {}
673
674void RepeatedStringFieldGenerator::
675GeneratePrivateMembers(io::Printer* printer) const {
676 printer->Print(variables_,
677 "::google::protobuf::RepeatedPtrField< ::std::string> $name$_;\n");
678}
679
680void RepeatedStringFieldGenerator::
681GenerateAccessorDeclarations(io::Printer* printer) const {
682 // See comment above about unknown ctypes.
Jisi Liu885b6122015-02-28 14:51:22 -0800683 bool unknown_ctype =
684 descriptor_->options().ctype() != EffectiveStringCType(descriptor_);
685
686 if (unknown_ctype) {
temporal40ee5512008-07-10 02:12:20 +0000687 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 Liu885b6122015-02-28 14:51:22 -0800695 "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.com80b1d622009-07-29 01:13:20 +0000700 "void set_$name$(int index, const $pointer_type$* value, size_t size)"
701 "$deprecation$;\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800702 "::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.com80b1d622009-07-29 01:13:20 +0000706 "$deprecation$;\n");
temporal40ee5512008-07-10 02:12:20 +0000707
kenton@google.comfccb1462009-12-18 02:11:36 +0000708 printer->Print(variables_,
Jisi Liu885b6122015-02-28 14:51:22 -0800709 "const ::google::protobuf::RepeatedPtrField< ::std::string>& $name$() const"
kenton@google.comfccb1462009-12-18 02:11:36 +0000710 "$deprecation$;\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800711 "::google::protobuf::RepeatedPtrField< ::std::string>* mutable_$name$()"
kenton@google.comfccb1462009-12-18 02:11:36 +0000712 "$deprecation$;\n");
713
Jisi Liu885b6122015-02-28 14:51:22 -0800714 if (unknown_ctype) {
temporal40ee5512008-07-10 02:12:20 +0000715 printer->Outdent();
716 printer->Print(" public:\n");
717 printer->Indent();
718 }
719}
720
721void RepeatedStringFieldGenerator::
Jisi Liu885b6122015-02-28 14:51:22 -0800722GenerateInlineAccessorDefinitions(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.com4de8f552014-07-18 00:47:59 +0000728 " // @@protoc_insertion_point(field_get:$full_name$)\n"
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000729 " return $name$_.$cppget$(index);\n"
temporal40ee5512008-07-10 02:12:20 +0000730 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800731 "$inline$ ::std::string* $classname$::mutable_$name$(int index) {\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000732 " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
temporal40ee5512008-07-10 02:12:20 +0000733 " return $name$_.Mutable(index);\n"
734 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800735 "$inline$ void $classname$::set_$name$(int index, const ::std::string& value) {\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000736 " // @@protoc_insertion_point(field_set:$full_name$)\n"
temporal40ee5512008-07-10 02:12:20 +0000737 " $name$_.Mutable(index)->assign(value);\n"
738 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800739 "$inline$ void $classname$::set_$name$(int index, const char* value) {\n"
temporalf2063512008-07-23 01:19:07 +0000740 " $name$_.Mutable(index)->assign(value);\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000741 " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
temporalf2063512008-07-23 01:19:07 +0000742 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800743 "$inline$ void "
kenton@google.comd37d46d2009-04-25 02:53:47 +0000744 "$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.com4de8f552014-07-18 00:47:59 +0000748 " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
kenton@google.comd37d46d2009-04-25 02:53:47 +0000749 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800750 "$inline$ ::std::string* $classname$::add_$name$() {\n"
temporal40ee5512008-07-10 02:12:20 +0000751 " return $name$_.Add();\n"
752 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800753 "$inline$ void $classname$::add_$name$(const ::std::string& value) {\n"
temporal40ee5512008-07-10 02:12:20 +0000754 " $name$_.Add()->assign(value);\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000755 " // @@protoc_insertion_point(field_add:$full_name$)\n"
temporalf2063512008-07-23 01:19:07 +0000756 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800757 "$inline$ void $classname$::add_$name$(const char* value) {\n"
temporalf2063512008-07-23 01:19:07 +0000758 " $name$_.Add()->assign(value);\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000759 " // @@protoc_insertion_point(field_add_char:$full_name$)\n"
kenton@google.comd37d46d2009-04-25 02:53:47 +0000760 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800761 "$inline$ void "
kenton@google.comd37d46d2009-04-25 02:53:47 +0000762 "$classname$::add_$name$(const $pointer_type$* value, size_t size) {\n"
763 " $name$_.Add()->assign(reinterpret_cast<const char*>(value), size);\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000764 " // @@protoc_insertion_point(field_add_pointer:$full_name$)\n"
temporal40ee5512008-07-10 02:12:20 +0000765 "}\n");
Jisi Liu885b6122015-02-28 14:51:22 -0800766 printer->Print(variables,
767 "$inline$ const ::google::protobuf::RepeatedPtrField< ::std::string>&\n"
kenton@google.comfccb1462009-12-18 02:11:36 +0000768 "$classname$::$name$() const {\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000769 " // @@protoc_insertion_point(field_list:$full_name$)\n"
kenton@google.comfccb1462009-12-18 02:11:36 +0000770 " return $name$_;\n"
771 "}\n"
Jisi Liu885b6122015-02-28 14:51:22 -0800772 "$inline$ ::google::protobuf::RepeatedPtrField< ::std::string>*\n"
kenton@google.comfccb1462009-12-18 02:11:36 +0000773 "$classname$::mutable_$name$() {\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000774 " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n"
kenton@google.comfccb1462009-12-18 02:11:36 +0000775 " return &$name$_;\n"
776 "}\n");
temporal40ee5512008-07-10 02:12:20 +0000777}
778
779void RepeatedStringFieldGenerator::
780GenerateClearingCode(io::Printer* printer) const {
781 printer->Print(variables_, "$name$_.Clear();\n");
782}
783
784void RepeatedStringFieldGenerator::
785GenerateMergingCode(io::Printer* printer) const {
786 printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
787}
788
789void RepeatedStringFieldGenerator::
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000790GenerateSwappingCode(io::Printer* printer) const {
Feng Xiao6ef984a2014-11-10 17:34:54 -0800791 printer->Print(variables_, "$name$_.UnsafeArenaSwap(&other->$name$_);\n");
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000792}
793
794void RepeatedStringFieldGenerator::
kenton@google.comd37d46d2009-04-25 02:53:47 +0000795GenerateConstructorCode(io::Printer* printer) const {
796 // Not needed for repeated fields.
temporal40ee5512008-07-10 02:12:20 +0000797}
798
799void RepeatedStringFieldGenerator::
800GenerateMergeFromCodedStream(io::Printer* printer) const {
801 printer->Print(variables_,
kenton@google.com80b1d622009-07-29 01:13:20 +0000802 "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.com4de8f552014-07-18 00:47:59 +0000807 "::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n"
xiaofeng@google.com20724212012-05-16 05:41:31 +0000808 " this->$name$(this->$name$_size() - 1).data(),\n"
809 " this->$name$(this->$name$_size() - 1).length(),\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000810 " ::google::protobuf::internal::WireFormat::PARSE,\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800811 " \"$full_name$\");\n");
kenton@google.com80b1d622009-07-29 01:13:20 +0000812 }
temporal40ee5512008-07-10 02:12:20 +0000813}
814
815void RepeatedStringFieldGenerator::
816GenerateSerializeWithCachedSizes(io::Printer* printer) const {
817 printer->Print(variables_,
kenton@google.com80b1d622009-07-29 01:13:20 +0000818 "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.com4de8f552014-07-18 00:47:59 +0000822 "::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n"
kenton@google.com80b1d622009-07-29 01:13:20 +0000823 " this->$name$(i).data(), this->$name$(i).length(),\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000824 " ::google::protobuf::internal::WireFormat::SERIALIZE,\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800825 " \"$full_name$\");\n");
kenton@google.com80b1d622009-07-29 01:13:20 +0000826 }
827 printer->Print(variables_,
828 " ::google::protobuf::internal::WireFormatLite::Write$declared_type$(\n"
829 " $number$, this->$name$(i), output);\n"
kenton@google.comd37d46d2009-04-25 02:53:47 +0000830 "}\n");
831}
832
833void RepeatedStringFieldGenerator::
834GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
835 printer->Print(variables_,
kenton@google.com80b1d622009-07-29 01:13:20 +0000836 "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.com4de8f552014-07-18 00:47:59 +0000840 " ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(\n"
kenton@google.com80b1d622009-07-29 01:13:20 +0000841 " this->$name$(i).data(), this->$name$(i).length(),\n"
jieluo@google.com4de8f552014-07-18 00:47:59 +0000842 " ::google::protobuf::internal::WireFormat::SERIALIZE,\n"
Feng Xiao6ef984a2014-11-10 17:34:54 -0800843 " \"$full_name$\");\n");
kenton@google.com80b1d622009-07-29 01:13:20 +0000844 }
845 printer->Print(variables_,
846 " target = ::google::protobuf::internal::WireFormatLite::\n"
847 " Write$declared_type$ToArray($number$, this->$name$(i), target);\n"
kenton@google.com2d6daa72009-01-22 01:27:00 +0000848 "}\n");
temporal40ee5512008-07-10 02:12:20 +0000849}
850
851void RepeatedStringFieldGenerator::
852GenerateByteSize(io::Printer* printer) const {
853 printer->Print(variables_,
kenton@google.com2d6daa72009-01-22 01:27:00 +0000854 "total_size += $tag_size$ * this->$name$_size();\n"
855 "for (int i = 0; i < this->$name$_size(); i++) {\n"
kenton@google.com80b1d622009-07-29 01:13:20 +0000856 " total_size += ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n"
temporal40ee5512008-07-10 02:12:20 +0000857 " this->$name$(i));\n"
858 "}\n");
859}
860
861} // namespace cpp
862} // namespace compiler
863} // namespace protobuf
864} // namespace google