cpp_string_field.cc 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  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.
  18. //
  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.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <google/protobuf/compiler/cpp/cpp_string_field.h>
  34. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  35. #include <google/protobuf/descriptor.pb.h>
  36. #include <google/protobuf/io/printer.h>
  37. #include <google/protobuf/stubs/strutil.h>
  38. namespace google {
  39. namespace protobuf {
  40. namespace compiler {
  41. namespace cpp {
  42. namespace {
  43. void SetStringVariables(const FieldDescriptor* descriptor,
  44. std::map<string, string>* variables,
  45. const Options& options) {
  46. SetCommonFieldVariables(descriptor, variables, options);
  47. (*variables)["default"] = DefaultValue(descriptor);
  48. (*variables)["default_length"] =
  49. SimpleItoa(descriptor->default_value_string().length());
  50. string default_variable_string = MakeDefaultName(descriptor);
  51. (*variables)["default_variable_name"] = default_variable_string;
  52. (*variables)["default_variable"] =
  53. descriptor->default_value_string().empty()
  54. ? "&::google::protobuf::internal::GetEmptyStringAlreadyInited()"
  55. : "&" + Namespace(descriptor) + "::" + (*variables)["classname"] +
  56. "::" + default_variable_string + ".get()";
  57. (*variables)["pointer_type"] =
  58. descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char";
  59. (*variables)["null_check"] = "GOOGLE_DCHECK(value != NULL);\n";
  60. // NOTE: Escaped here to unblock proto1->proto2 migration.
  61. // TODO(liujisi): Extend this to apply for other conflicting methods.
  62. (*variables)["release_name"] =
  63. SafeFunctionName(descriptor->containing_type(),
  64. descriptor, "release_");
  65. (*variables)["full_name"] = descriptor->full_name();
  66. (*variables)["string_piece"] = "::std::string";
  67. (*variables)["lite"] =
  68. HasDescriptorMethods(descriptor->file(), options) ? "" : "Lite";
  69. }
  70. } // namespace
  71. // ===================================================================
  72. StringFieldGenerator::StringFieldGenerator(const FieldDescriptor* descriptor,
  73. const Options& options)
  74. : FieldGenerator(options),
  75. descriptor_(descriptor),
  76. lite_(!HasDescriptorMethods(descriptor->file(), options)),
  77. inlined_(false) {
  78. // TODO(ckennelly): Handle inlining for any.proto.
  79. if (IsAnyMessage(descriptor_->containing_type())) {
  80. inlined_ = false;
  81. }
  82. if (descriptor_->containing_type()->options().map_entry()) {
  83. inlined_ = false;
  84. }
  85. // Limit to proto2, as we rely on has bits to distinguish field presence for
  86. // release_$name$. On proto3, we cannot use the address of the string
  87. // instance when the field has been inlined.
  88. inlined_ = inlined_ && HasFieldPresence(descriptor_->file());
  89. SetStringVariables(descriptor, &variables_, options);
  90. }
  91. StringFieldGenerator::~StringFieldGenerator() {}
  92. void StringFieldGenerator::
  93. GeneratePrivateMembers(io::Printer* printer) const {
  94. if (inlined_) {
  95. printer->Print(variables_,
  96. "::google::protobuf::internal::InlinedStringField $name$_;\n");
  97. } else {
  98. // N.B. that we continue to use |ArenaStringPtr| instead of |string*| for
  99. // string fields, even when SupportArenas(descriptor_) == false. Why? The
  100. // simple answer is to avoid unmaintainable complexity. The reflection code
  101. // assumes ArenaStringPtrs. These are *almost* in-memory-compatible with
  102. // string*, except for the pointer tags and related ownership semantics. We
  103. // could modify the runtime code to use string* for the
  104. // not-supporting-arenas case, but this would require a way to detect which
  105. // type of class was generated (adding overhead and complexity to
  106. // GeneratedMessageReflection) and littering the runtime code paths with
  107. // conditionals. It's simpler to stick with this but use lightweight
  108. // accessors that assume arena == NULL. There should be very little
  109. // overhead anyway because it's just a tagged pointer in-memory.
  110. printer->Print(variables_, "::google::protobuf::internal::ArenaStringPtr $name$_;\n");
  111. }
  112. }
  113. void StringFieldGenerator::
  114. GenerateStaticMembers(io::Printer* printer) const {
  115. if (!descriptor_->default_value_string().empty()) {
  116. // We make the default instance public, so it can be initialized by
  117. // non-friend code.
  118. printer->Print(variables_,
  119. "public:\n"
  120. "static ::google::protobuf::internal::ExplicitlyConstructed< ::std::string>"
  121. " $default_variable_name$;\n"
  122. "private:\n");
  123. }
  124. }
  125. void StringFieldGenerator::
  126. GenerateAccessorDeclarations(io::Printer* printer) const {
  127. // If we're using StringFieldGenerator for a field with a ctype, it's
  128. // because that ctype isn't actually implemented. In particular, this is
  129. // true of ctype=CORD and ctype=STRING_PIECE in the open source release.
  130. // We aren't releasing Cord because it has too many Google-specific
  131. // dependencies and we aren't releasing StringPiece because it's hardly
  132. // useful outside of Google and because it would get confusing to have
  133. // multiple instances of the StringPiece class in different libraries (PCRE
  134. // already includes it for their C++ bindings, which came from Google).
  135. //
  136. // In any case, we make all the accessors private while still actually
  137. // using a string to represent the field internally. This way, we can
  138. // guarantee that if we do ever implement the ctype, it won't break any
  139. // existing users who might be -- for whatever reason -- already using .proto
  140. // files that applied the ctype. The field can still be accessed via the
  141. // reflection interface since the reflection interface is independent of
  142. // the string's underlying representation.
  143. bool unknown_ctype =
  144. descriptor_->options().ctype() != EffectiveStringCType(descriptor_);
  145. if (unknown_ctype) {
  146. printer->Outdent();
  147. printer->Print(
  148. " private:\n"
  149. " // Hidden due to unknown ctype option.\n");
  150. printer->Indent();
  151. }
  152. printer->Print(variables_,
  153. "$deprecated_attr$const ::std::string& $name$() const;\n");
  154. printer->Annotate("name", descriptor_);
  155. printer->Print(
  156. variables_,
  157. "$deprecated_attr$void ${$set_$name$$}$(const ::std::string& value);\n");
  158. printer->Annotate("{", "}", descriptor_);
  159. printer->Print(variables_,
  160. "#if LANG_CXX11\n"
  161. "$deprecated_attr$void ${$set_$name$$}$(::std::string&& value);\n"
  162. "#endif\n");
  163. printer->Annotate("{", "}", descriptor_);
  164. printer->Print(
  165. variables_,
  166. "$deprecated_attr$void ${$set_$name$$}$(const char* value);\n");
  167. printer->Annotate("{", "}", descriptor_);
  168. printer->Print(variables_,
  169. "$deprecated_attr$void ${$set_$name$$}$(const $pointer_type$* "
  170. "value, size_t size)"
  171. ";\n");
  172. printer->Annotate("{", "}", descriptor_);
  173. printer->Print(variables_,
  174. "$deprecated_attr$::std::string* ${$mutable_$name$$}$();\n");
  175. printer->Annotate("{", "}", descriptor_);
  176. printer->Print(variables_, "$deprecated_attr$::std::string* $release_name$();\n");
  177. printer->Annotate("release_name", descriptor_);
  178. printer->Print(
  179. variables_,
  180. "$deprecated_attr$void ${$set_allocated_$name$$}$(::std::string* $name$);\n");
  181. printer->Annotate("{", "}", descriptor_);
  182. if (SupportsArenas(descriptor_)) {
  183. printer->Print(
  184. variables_,
  185. "PROTOBUF_RUNTIME_DEPRECATED(\"The unsafe_arena_ accessors for\"\n"
  186. "\" string fields are deprecated and will be removed in a\"\n"
  187. "\" future release.\")\n"
  188. "::std::string* ${$unsafe_arena_release_$name$$}$();\n");
  189. printer->Annotate("{", "}", descriptor_);
  190. printer->Print(
  191. variables_,
  192. "PROTOBUF_RUNTIME_DEPRECATED(\"The unsafe_arena_ accessors for\"\n"
  193. "\" string fields are deprecated and will be removed in a\"\n"
  194. "\" future release.\")\n"
  195. "void ${$unsafe_arena_set_allocated_$name$$}$(\n"
  196. " ::std::string* $name$);\n");
  197. printer->Annotate("{", "}", descriptor_);
  198. }
  199. if (unknown_ctype) {
  200. printer->Outdent();
  201. printer->Print(" public:\n");
  202. printer->Indent();
  203. }
  204. }
  205. void StringFieldGenerator::
  206. GenerateInlineAccessorDefinitions(io::Printer* printer) const {
  207. if (SupportsArenas(descriptor_)) {
  208. printer->Print(
  209. variables_,
  210. "inline const ::std::string& $classname$::$name$() const {\n"
  211. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  212. " return $name$_.Get();\n"
  213. "}\n"
  214. "inline void $classname$::set_$name$(const ::std::string& value) {\n"
  215. " $set_hasbit$\n"
  216. " $name$_.Set$lite$($default_variable$, value, GetArenaNoVirtual());\n"
  217. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  218. "}\n"
  219. "#if LANG_CXX11\n"
  220. "inline void $classname$::set_$name$(::std::string&& value) {\n"
  221. " $set_hasbit$\n"
  222. " $name$_.Set$lite$(\n"
  223. " $default_variable$, ::std::move(value), GetArenaNoVirtual());\n"
  224. " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n"
  225. "}\n"
  226. "#endif\n"
  227. "inline void $classname$::set_$name$(const char* value) {\n"
  228. " $null_check$"
  229. " $set_hasbit$\n"
  230. " $name$_.Set$lite$($default_variable$, $string_piece$(value),\n"
  231. " GetArenaNoVirtual());\n"
  232. " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
  233. "}\n"
  234. "inline "
  235. "void $classname$::set_$name$(const $pointer_type$* value,\n"
  236. " size_t size) {\n"
  237. " $set_hasbit$\n"
  238. " $name$_.Set$lite$($default_variable$, $string_piece$(\n"
  239. " reinterpret_cast<const char*>(value), size), "
  240. "GetArenaNoVirtual());\n"
  241. " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
  242. "}\n"
  243. "inline ::std::string* $classname$::mutable_$name$() {\n"
  244. " $set_hasbit$\n"
  245. " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
  246. " return $name$_.Mutable($default_variable$, GetArenaNoVirtual());\n"
  247. "}\n"
  248. "inline ::std::string* $classname$::$release_name$() {\n"
  249. " // @@protoc_insertion_point(field_release:$full_name$)\n");
  250. if (HasFieldPresence(descriptor_->file())) {
  251. printer->Print(variables_,
  252. " if (!has_$name$()) {\n"
  253. " return NULL;\n"
  254. " }\n"
  255. " $clear_hasbit$\n"
  256. " return $name$_.ReleaseNonDefault("
  257. "$default_variable$, GetArenaNoVirtual());\n");
  258. } else {
  259. printer->Print(variables_,
  260. " $clear_hasbit$\n"
  261. " return $name$_.Release($default_variable$, GetArenaNoVirtual());\n");
  262. }
  263. printer->Print(variables_,
  264. "}\n"
  265. "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
  266. " if ($name$ != NULL) {\n"
  267. " $set_hasbit$\n"
  268. " } else {\n"
  269. " $clear_hasbit$\n"
  270. " }\n"
  271. " $name$_.SetAllocated($default_variable$, $name$,\n"
  272. " GetArenaNoVirtual());\n"
  273. " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
  274. "}\n"
  275. "inline ::std::string* $classname$::unsafe_arena_release_$name$() {\n"
  276. " // "
  277. "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n"
  278. " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n"
  279. " $clear_hasbit$\n"
  280. " return $name$_.UnsafeArenaRelease($default_variable$,\n"
  281. " GetArenaNoVirtual());\n"
  282. "}\n"
  283. "inline void $classname$::unsafe_arena_set_allocated_$name$(\n"
  284. " ::std::string* $name$) {\n"
  285. " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n"
  286. " if ($name$ != NULL) {\n"
  287. " $set_hasbit$\n"
  288. " } else {\n"
  289. " $clear_hasbit$\n"
  290. " }\n"
  291. " $name$_.UnsafeArenaSetAllocated($default_variable$,\n"
  292. " $name$, GetArenaNoVirtual());\n"
  293. " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:"
  294. "$full_name$)\n"
  295. "}\n");
  296. } else {
  297. // No-arena case.
  298. printer->Print(
  299. variables_,
  300. "inline const ::std::string& $classname$::$name$() const {\n"
  301. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  302. " return $name$_.GetNoArena();\n"
  303. "}\n"
  304. "inline void $classname$::set_$name$(const ::std::string& value) {\n"
  305. " $set_hasbit$\n"
  306. " $name$_.SetNoArena($default_variable$, value);\n"
  307. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  308. "}\n"
  309. "#if LANG_CXX11\n"
  310. "inline void $classname$::set_$name$(::std::string&& value) {\n"
  311. " $set_hasbit$\n"
  312. " $name$_.SetNoArena(\n"
  313. " $default_variable$, ::std::move(value));\n"
  314. " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n"
  315. "}\n"
  316. "#endif\n"
  317. "inline void $classname$::set_$name$(const char* value) {\n"
  318. " $null_check$"
  319. " $set_hasbit$\n"
  320. " $name$_.SetNoArena($default_variable$, $string_piece$(value));\n"
  321. " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
  322. "}\n"
  323. "inline "
  324. "void $classname$::set_$name$(const $pointer_type$* value, "
  325. "size_t size) {\n"
  326. " $set_hasbit$\n"
  327. " $name$_.SetNoArena($default_variable$,\n"
  328. " $string_piece$(reinterpret_cast<const char*>(value), size));\n"
  329. " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
  330. "}\n"
  331. "inline ::std::string* $classname$::mutable_$name$() {\n"
  332. " $set_hasbit$\n"
  333. " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
  334. " return $name$_.MutableNoArena($default_variable$);\n"
  335. "}\n"
  336. "inline ::std::string* $classname$::$release_name$() {\n"
  337. " // @@protoc_insertion_point(field_release:$full_name$)\n");
  338. if (HasFieldPresence(descriptor_->file())) {
  339. printer->Print(variables_,
  340. " if (!has_$name$()) {\n"
  341. " return NULL;\n"
  342. " }\n"
  343. " $clear_hasbit$\n"
  344. " return $name$_.ReleaseNonDefaultNoArena($default_variable$);\n");
  345. } else {
  346. printer->Print(variables_,
  347. " $clear_hasbit$\n"
  348. " return $name$_.ReleaseNoArena($default_variable$);\n");
  349. }
  350. printer->Print(variables_,
  351. "}\n"
  352. "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
  353. " if ($name$ != NULL) {\n"
  354. " $set_hasbit$\n"
  355. " } else {\n"
  356. " $clear_hasbit$\n"
  357. " }\n"
  358. " $name$_.SetAllocatedNoArena($default_variable$, $name$);\n"
  359. " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
  360. "}\n");
  361. }
  362. }
  363. void StringFieldGenerator::
  364. GenerateNonInlineAccessorDefinitions(io::Printer* printer) const {
  365. if (!descriptor_->default_value_string().empty()) {
  366. // Initialized in GenerateDefaultInstanceAllocator.
  367. printer->Print(variables_,
  368. "::google::protobuf::internal::ExplicitlyConstructed<::std::string> "
  369. "$classname$::$default_variable_name$;\n");
  370. }
  371. }
  372. void StringFieldGenerator::
  373. GenerateClearingCode(io::Printer* printer) const {
  374. // Two-dimension specialization here: supporting arenas or not, and default
  375. // value is the empty string or not. Complexity here ensures the minimal
  376. // number of branches / amount of extraneous code at runtime (given that the
  377. // below methods are inlined one-liners)!
  378. if (SupportsArenas(descriptor_)) {
  379. if (descriptor_->default_value_string().empty()) {
  380. printer->Print(variables_,
  381. "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n");
  382. } else {
  383. printer->Print(variables_,
  384. "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n");
  385. }
  386. } else {
  387. if (descriptor_->default_value_string().empty()) {
  388. printer->Print(variables_,
  389. "$name$_.ClearToEmptyNoArena($default_variable$);\n");
  390. } else {
  391. printer->Print(variables_,
  392. "$name$_.ClearToDefaultNoArena($default_variable$);\n");
  393. }
  394. }
  395. }
  396. void StringFieldGenerator::
  397. GenerateMessageClearingCode(io::Printer* printer) const {
  398. // Two-dimension specialization here: supporting arenas, field presence, or
  399. // not, and default value is the empty string or not. Complexity here ensures
  400. // the minimal number of branches / amount of extraneous code at runtime
  401. // (given that the below methods are inlined one-liners)!
  402. // If we have field presence, then the Clear() method of the protocol buffer
  403. // will have checked that this field is set. If so, we can avoid redundant
  404. // checks against default_variable.
  405. const bool must_be_present =
  406. HasFieldPresence(descriptor_->file());
  407. if (inlined_ && must_be_present) {
  408. // Calling mutable_$name$() gives us a string reference and sets the has bit
  409. // for $name$ (in proto2). We may get here when the string field is inlined
  410. // but the string's contents have not been changed by the user, so we cannot
  411. // make an assertion about the contents of the string and could never make
  412. // an assertion about the string instance.
  413. //
  414. // For non-inlined strings, we distinguish from non-default by comparing
  415. // instances, rather than contents.
  416. printer->Print(variables_,
  417. "GOOGLE_DCHECK(!$name$_.IsDefault($default_variable$));\n");
  418. }
  419. if (SupportsArenas(descriptor_)) {
  420. if (descriptor_->default_value_string().empty()) {
  421. if (must_be_present) {
  422. printer->Print(variables_,
  423. "$name$_.ClearNonDefaultToEmpty();\n");
  424. } else {
  425. printer->Print(variables_,
  426. "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n");
  427. }
  428. } else {
  429. // Clear to a non-empty default is more involved, as we try to use the
  430. // Arena if one is present and may need to reallocate the string.
  431. printer->Print(variables_,
  432. "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n");
  433. }
  434. } else if (must_be_present) {
  435. // When Arenas are disabled and field presence has been checked, we can
  436. // safely treat the ArenaStringPtr as a string*.
  437. if (descriptor_->default_value_string().empty()) {
  438. printer->Print(variables_, "$name$_.ClearNonDefaultToEmptyNoArena();\n");
  439. } else {
  440. printer->Print(
  441. variables_,
  442. "$name$_.UnsafeMutablePointer()->assign(*$default_variable$);\n");
  443. }
  444. } else {
  445. if (descriptor_->default_value_string().empty()) {
  446. printer->Print(variables_,
  447. "$name$_.ClearToEmptyNoArena($default_variable$);\n");
  448. } else {
  449. printer->Print(variables_,
  450. "$name$_.ClearToDefaultNoArena($default_variable$);\n");
  451. }
  452. }
  453. }
  454. void StringFieldGenerator::
  455. GenerateMergingCode(io::Printer* printer) const {
  456. if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) {
  457. // TODO(gpike): improve this
  458. printer->Print(variables_, "set_$name$(from.$name$());\n");
  459. } else {
  460. printer->Print(variables_,
  461. "$set_hasbit$\n"
  462. "$name$_.AssignWithDefault($default_variable$, from.$name$_);\n");
  463. }
  464. }
  465. void StringFieldGenerator::
  466. GenerateSwappingCode(io::Printer* printer) const {
  467. if (inlined_) {
  468. printer->Print(
  469. variables_,
  470. "$name$_.Swap(&other->$name$_);\n");
  471. } else {
  472. printer->Print(
  473. variables_,
  474. "$name$_.Swap(&other->$name$_, $default_variable$,\n"
  475. " GetArenaNoVirtual());\n");
  476. }
  477. }
  478. void StringFieldGenerator::
  479. GenerateConstructorCode(io::Printer* printer) const {
  480. // TODO(ckennelly): Construct non-empty strings as part of the initializer
  481. // list.
  482. if (inlined_ && descriptor_->default_value_string().empty()) {
  483. // Automatic initialization will construct the string.
  484. return;
  485. }
  486. printer->Print(variables_,
  487. "$name$_.UnsafeSetDefault($default_variable$);\n");
  488. }
  489. void StringFieldGenerator::
  490. GenerateCopyConstructorCode(io::Printer* printer) const {
  491. GenerateConstructorCode(printer);
  492. if (HasFieldPresence(descriptor_->file())) {
  493. printer->Print(variables_,
  494. "if (from.has_$name$()) {\n");
  495. } else {
  496. printer->Print(variables_,
  497. "if (from.$name$().size() > 0) {\n");
  498. }
  499. printer->Indent();
  500. if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) {
  501. // TODO(gpike): improve this
  502. printer->Print(variables_,
  503. "$name$_.Set$lite$($default_variable$, from.$name$(),\n"
  504. " GetArenaNoVirtual());\n");
  505. } else {
  506. printer->Print(variables_,
  507. "$name$_.AssignWithDefault($default_variable$, from.$name$_);\n");
  508. }
  509. printer->Outdent();
  510. printer->Print("}\n");
  511. }
  512. void StringFieldGenerator::
  513. GenerateDestructorCode(io::Printer* printer) const {
  514. if (inlined_) {
  515. // The destructor is automatically invoked.
  516. return;
  517. }
  518. printer->Print(variables_, "$name$_.DestroyNoArena($default_variable$);\n");
  519. }
  520. bool StringFieldGenerator::GenerateArenaDestructorCode(
  521. io::Printer* printer) const {
  522. if (!inlined_) {
  523. return false;
  524. }
  525. printer->Print(variables_,
  526. "_this->$name$_.DestroyNoArena($default_variable$);\n");
  527. return true;
  528. }
  529. void StringFieldGenerator::
  530. GenerateDefaultInstanceAllocator(io::Printer* printer) const {
  531. if (!descriptor_->default_value_string().empty()) {
  532. printer->Print(
  533. variables_,
  534. "$ns$::$classname$::$default_variable_name$.DefaultConstruct();\n"
  535. "*$ns$::$classname$::$default_variable_name$.get_mutable() = "
  536. "::std::string($default$, $default_length$);\n"
  537. "::google::protobuf::internal::OnShutdownDestroyString(\n"
  538. " $ns$::$classname$::$default_variable_name$.get_mutable());\n"
  539. );
  540. }
  541. }
  542. void StringFieldGenerator::
  543. GenerateMergeFromCodedStream(io::Printer* printer) const {
  544. printer->Print(variables_,
  545. "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n"
  546. " input, this->mutable_$name$()));\n");
  547. if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
  548. GenerateUtf8CheckCodeForString(
  549. descriptor_, options_, true, variables_,
  550. "this->$name$().data(), static_cast<int>(this->$name$().length()),\n",
  551. printer);
  552. }
  553. }
  554. bool StringFieldGenerator::
  555. MergeFromCodedStreamNeedsArena() const {
  556. return false;
  557. }
  558. void StringFieldGenerator::
  559. GenerateSerializeWithCachedSizes(io::Printer* printer) const {
  560. if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
  561. GenerateUtf8CheckCodeForString(
  562. descriptor_, options_, false, variables_,
  563. "this->$name$().data(), static_cast<int>(this->$name$().length()),\n",
  564. printer);
  565. }
  566. printer->Print(variables_,
  567. "::google::protobuf::internal::WireFormatLite::Write$declared_type$MaybeAliased(\n"
  568. " $number$, this->$name$(), output);\n");
  569. }
  570. void StringFieldGenerator::
  571. GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
  572. if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
  573. GenerateUtf8CheckCodeForString(
  574. descriptor_, options_, false, variables_,
  575. "this->$name$().data(), static_cast<int>(this->$name$().length()),\n",
  576. printer);
  577. }
  578. printer->Print(variables_,
  579. "target =\n"
  580. " ::google::protobuf::internal::WireFormatLite::Write$declared_type$ToArray(\n"
  581. " $number$, this->$name$(), target);\n");
  582. }
  583. void StringFieldGenerator::
  584. GenerateByteSize(io::Printer* printer) const {
  585. printer->Print(variables_,
  586. "total_size += $tag_size$ +\n"
  587. " ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n"
  588. " this->$name$());\n");
  589. }
  590. uint32 StringFieldGenerator::CalculateFieldTag() const {
  591. return inlined_ ? 1 : 0;
  592. }
  593. // ===================================================================
  594. StringOneofFieldGenerator::StringOneofFieldGenerator(
  595. const FieldDescriptor* descriptor, const Options& options)
  596. : StringFieldGenerator(descriptor, options) {
  597. inlined_ = false;
  598. SetCommonOneofFieldVariables(descriptor, &variables_);
  599. }
  600. StringOneofFieldGenerator::~StringOneofFieldGenerator() {}
  601. void StringOneofFieldGenerator::
  602. GenerateInlineAccessorDefinitions(io::Printer* printer) const {
  603. if (SupportsArenas(descriptor_)) {
  604. printer->Print(
  605. variables_,
  606. "inline const ::std::string& $classname$::$name$() const {\n"
  607. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  608. " if (has_$name$()) {\n"
  609. " return $field_member$.Get();\n"
  610. " }\n"
  611. " return *$default_variable$;\n"
  612. "}\n"
  613. "inline void $classname$::set_$name$(const ::std::string& value) {\n"
  614. " if (!has_$name$()) {\n"
  615. " clear_$oneof_name$();\n"
  616. " set_has_$name$();\n"
  617. " $field_member$.UnsafeSetDefault($default_variable$);\n"
  618. " }\n"
  619. " $field_member$.Set$lite$($default_variable$, value,\n"
  620. " GetArenaNoVirtual());\n"
  621. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  622. "}\n"
  623. "#if LANG_CXX11\n"
  624. "inline void $classname$::set_$name$(::std::string&& value) {\n"
  625. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  626. " if (!has_$name$()) {\n"
  627. " clear_$oneof_name$();\n"
  628. " set_has_$name$();\n"
  629. " $field_member$.UnsafeSetDefault($default_variable$);\n"
  630. " }\n"
  631. " $field_member$.Set$lite$(\n"
  632. " $default_variable$, ::std::move(value), GetArenaNoVirtual());\n"
  633. " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n"
  634. "}\n"
  635. "#endif\n"
  636. "inline void $classname$::set_$name$(const char* value) {\n"
  637. " $null_check$"
  638. " if (!has_$name$()) {\n"
  639. " clear_$oneof_name$();\n"
  640. " set_has_$name$();\n"
  641. " $field_member$.UnsafeSetDefault($default_variable$);\n"
  642. " }\n"
  643. " $field_member$.Set$lite$($default_variable$,\n"
  644. " $string_piece$(value), GetArenaNoVirtual());\n"
  645. " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
  646. "}\n"
  647. "inline "
  648. "void $classname$::set_$name$(const $pointer_type$* value,\n"
  649. " size_t size) {\n"
  650. " if (!has_$name$()) {\n"
  651. " clear_$oneof_name$();\n"
  652. " set_has_$name$();\n"
  653. " $field_member$.UnsafeSetDefault($default_variable$);\n"
  654. " }\n"
  655. " $field_member$.Set$lite$(\n"
  656. " $default_variable$, $string_piece$(\n"
  657. " reinterpret_cast<const char*>(value), size),\n"
  658. " GetArenaNoVirtual());\n"
  659. " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
  660. "}\n"
  661. "inline ::std::string* $classname$::mutable_$name$() {\n"
  662. " if (!has_$name$()) {\n"
  663. " clear_$oneof_name$();\n"
  664. " set_has_$name$();\n"
  665. " $field_member$.UnsafeSetDefault($default_variable$);\n"
  666. " }\n"
  667. " return $field_member$.Mutable($default_variable$,\n"
  668. " GetArenaNoVirtual());\n"
  669. " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
  670. "}\n"
  671. "inline ::std::string* $classname$::$release_name$() {\n"
  672. " // @@protoc_insertion_point(field_release:$full_name$)\n"
  673. " if (has_$name$()) {\n"
  674. " clear_has_$oneof_name$();\n"
  675. " return $field_member$.Release($default_variable$,\n"
  676. " GetArenaNoVirtual());\n"
  677. " } else {\n"
  678. " return NULL;\n"
  679. " }\n"
  680. "}\n"
  681. "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
  682. " if (!has_$name$()) {\n"
  683. " $field_member$.UnsafeSetDefault($default_variable$);\n"
  684. " }\n"
  685. " clear_$oneof_name$();\n"
  686. " if ($name$ != NULL) {\n"
  687. " set_has_$name$();\n"
  688. " $field_member$.SetAllocated($default_variable$, $name$,\n"
  689. " GetArenaNoVirtual());\n"
  690. " }\n"
  691. " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
  692. "}\n"
  693. "inline ::std::string* $classname$::unsafe_arena_release_$name$() {\n"
  694. " // "
  695. "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n"
  696. " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n"
  697. " if (has_$name$()) {\n"
  698. " clear_has_$oneof_name$();\n"
  699. " return $field_member$.UnsafeArenaRelease(\n"
  700. " $default_variable$, GetArenaNoVirtual());\n"
  701. " } else {\n"
  702. " return NULL;\n"
  703. " }\n"
  704. "}\n"
  705. "inline void $classname$::unsafe_arena_set_allocated_$name$("
  706. "::std::string* $name$) {\n"
  707. " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n"
  708. " if (!has_$name$()) {\n"
  709. " $field_member$.UnsafeSetDefault($default_variable$);\n"
  710. " }\n"
  711. " clear_$oneof_name$();\n"
  712. " if ($name$) {\n"
  713. " set_has_$name$();\n"
  714. " $field_member$.UnsafeArenaSetAllocated($default_variable$, "
  715. "$name$, GetArenaNoVirtual());\n"
  716. " }\n"
  717. " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:"
  718. "$full_name$)\n"
  719. "}\n");
  720. } else {
  721. // No-arena case.
  722. printer->Print(
  723. variables_,
  724. "inline const ::std::string& $classname$::$name$() const {\n"
  725. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  726. " if (has_$name$()) {\n"
  727. " return $field_member$.GetNoArena();\n"
  728. " }\n"
  729. " return *$default_variable$;\n"
  730. "}\n"
  731. "inline void $classname$::set_$name$(const ::std::string& value) {\n"
  732. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  733. " if (!has_$name$()) {\n"
  734. " clear_$oneof_name$();\n"
  735. " set_has_$name$();\n"
  736. " $field_member$.UnsafeSetDefault($default_variable$);\n"
  737. " }\n"
  738. " $field_member$.SetNoArena($default_variable$, value);\n"
  739. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  740. "}\n"
  741. "#if LANG_CXX11\n"
  742. "inline void $classname$::set_$name$(::std::string&& value) {\n"
  743. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  744. " if (!has_$name$()) {\n"
  745. " clear_$oneof_name$();\n"
  746. " set_has_$name$();\n"
  747. " $field_member$.UnsafeSetDefault($default_variable$);\n"
  748. " }\n"
  749. " $field_member$.SetNoArena($default_variable$, ::std::move(value));\n"
  750. " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n"
  751. "}\n"
  752. "#endif\n"
  753. "inline void $classname$::set_$name$(const char* value) {\n"
  754. " $null_check$"
  755. " if (!has_$name$()) {\n"
  756. " clear_$oneof_name$();\n"
  757. " set_has_$name$();\n"
  758. " $field_member$.UnsafeSetDefault($default_variable$);\n"
  759. " }\n"
  760. " $field_member$.SetNoArena($default_variable$,\n"
  761. " $string_piece$(value));\n"
  762. " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
  763. "}\n"
  764. "inline "
  765. "void $classname$::set_$name$(const $pointer_type$* value, size_t "
  766. "size) {\n"
  767. " if (!has_$name$()) {\n"
  768. " clear_$oneof_name$();\n"
  769. " set_has_$name$();\n"
  770. " $field_member$.UnsafeSetDefault($default_variable$);\n"
  771. " }\n"
  772. " $field_member$.SetNoArena($default_variable$, $string_piece$(\n"
  773. " reinterpret_cast<const char*>(value), size));\n"
  774. " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
  775. "}\n"
  776. "inline ::std::string* $classname$::mutable_$name$() {\n"
  777. " if (!has_$name$()) {\n"
  778. " clear_$oneof_name$();\n"
  779. " set_has_$name$();\n"
  780. " $field_member$.UnsafeSetDefault($default_variable$);\n"
  781. " }\n"
  782. " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
  783. " return $field_member$.MutableNoArena($default_variable$);\n"
  784. "}\n"
  785. "inline ::std::string* $classname$::$release_name$() {\n"
  786. " // @@protoc_insertion_point(field_release:$full_name$)\n"
  787. " if (has_$name$()) {\n"
  788. " clear_has_$oneof_name$();\n"
  789. " return $field_member$.ReleaseNoArena($default_variable$);\n"
  790. " } else {\n"
  791. " return NULL;\n"
  792. " }\n"
  793. "}\n"
  794. "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n"
  795. " if (!has_$name$()) {\n"
  796. " $field_member$.UnsafeSetDefault($default_variable$);\n"
  797. " }\n"
  798. " clear_$oneof_name$();\n"
  799. " if ($name$ != NULL) {\n"
  800. " set_has_$name$();\n"
  801. " $field_member$.SetAllocatedNoArena($default_variable$, $name$);\n"
  802. " }\n"
  803. " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
  804. "}\n");
  805. }
  806. }
  807. void StringOneofFieldGenerator::
  808. GenerateClearingCode(io::Printer* printer) const {
  809. if (SupportsArenas(descriptor_)) {
  810. printer->Print(variables_,
  811. "$field_member$.Destroy($default_variable$,\n"
  812. " GetArenaNoVirtual());\n");
  813. } else {
  814. printer->Print(variables_,
  815. "$field_member$.DestroyNoArena($default_variable$);\n");
  816. }
  817. }
  818. void StringOneofFieldGenerator::
  819. GenerateMessageClearingCode(io::Printer* printer) const {
  820. return GenerateClearingCode(printer);
  821. }
  822. void StringOneofFieldGenerator::
  823. GenerateSwappingCode(io::Printer* printer) const {
  824. // Don't print any swapping code. Swapping the union will swap this field.
  825. }
  826. void StringOneofFieldGenerator::
  827. GenerateConstructorCode(io::Printer* printer) const {
  828. printer->Print(
  829. variables_,
  830. "$ns$::_$classname$_default_instance_.$name$_.UnsafeSetDefault(\n"
  831. " $default_variable$);\n");
  832. }
  833. void StringOneofFieldGenerator::
  834. GenerateDestructorCode(io::Printer* printer) const {
  835. printer->Print(variables_,
  836. "if (has_$name$()) {\n"
  837. " $field_member$.DestroyNoArena($default_variable$);\n"
  838. "}\n");
  839. }
  840. void StringOneofFieldGenerator::
  841. GenerateMergeFromCodedStream(io::Printer* printer) const {
  842. printer->Print(variables_,
  843. "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n"
  844. " input, this->mutable_$name$()));\n");
  845. if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
  846. GenerateUtf8CheckCodeForString(
  847. descriptor_, options_, true, variables_,
  848. "this->$name$().data(), static_cast<int>(this->$name$().length()),\n",
  849. printer);
  850. }
  851. }
  852. // ===================================================================
  853. RepeatedStringFieldGenerator::RepeatedStringFieldGenerator(
  854. const FieldDescriptor* descriptor, const Options& options)
  855. : FieldGenerator(options), descriptor_(descriptor) {
  856. SetStringVariables(descriptor, &variables_, options);
  857. }
  858. RepeatedStringFieldGenerator::~RepeatedStringFieldGenerator() {}
  859. void RepeatedStringFieldGenerator::
  860. GeneratePrivateMembers(io::Printer* printer) const {
  861. printer->Print(variables_,
  862. "::google::protobuf::RepeatedPtrField< ::std::string> $name$_;\n");
  863. }
  864. void RepeatedStringFieldGenerator::
  865. GenerateAccessorDeclarations(io::Printer* printer) const {
  866. // See comment above about unknown ctypes.
  867. bool unknown_ctype =
  868. descriptor_->options().ctype() != EffectiveStringCType(descriptor_);
  869. if (unknown_ctype) {
  870. printer->Outdent();
  871. printer->Print(
  872. " private:\n"
  873. " // Hidden due to unknown ctype option.\n");
  874. printer->Indent();
  875. }
  876. printer->Print(variables_,
  877. "$deprecated_attr$const ::std::string& $name$(int index) const;\n");
  878. printer->Annotate("name", descriptor_);
  879. printer->Print(
  880. variables_,
  881. "$deprecated_attr$::std::string* ${$mutable_$name$$}$(int index);\n");
  882. printer->Annotate("{", "}", descriptor_);
  883. printer->Print(variables_,
  884. "$deprecated_attr$void ${$set_$name$$}$(int index, const "
  885. "::std::string& value);\n");
  886. printer->Annotate("{", "}", descriptor_);
  887. printer->Print(
  888. variables_,
  889. "#if LANG_CXX11\n"
  890. "$deprecated_attr$void ${$set_$name$$}$(int index, ::std::string&& value);\n"
  891. "#endif\n");
  892. printer->Annotate("{", "}", descriptor_);
  893. printer->Print(variables_,
  894. "$deprecated_attr$void ${$set_$name$$}$(int index, const "
  895. "char* value);\n");
  896. printer->Annotate("{", "}", descriptor_);
  897. printer->Print(variables_,
  898. ""
  899. "$deprecated_attr$void ${$set_$name$$}$("
  900. "int index, const $pointer_type$* value, size_t size);\n");
  901. printer->Annotate("{", "}", descriptor_);
  902. printer->Print(variables_,
  903. "$deprecated_attr$::std::string* ${$add_$name$$}$();\n");
  904. printer->Annotate("{", "}", descriptor_);
  905. printer->Print(
  906. variables_,
  907. "$deprecated_attr$void ${$add_$name$$}$(const ::std::string& value);\n");
  908. printer->Annotate("{", "}", descriptor_);
  909. printer->Print(variables_,
  910. "#if LANG_CXX11\n"
  911. "$deprecated_attr$void ${$add_$name$$}$(::std::string&& value);\n"
  912. "#endif\n");
  913. printer->Annotate("{", "}", descriptor_);
  914. printer->Print(
  915. variables_,
  916. "$deprecated_attr$void ${$add_$name$$}$(const char* value);\n");
  917. printer->Annotate("{", "}", descriptor_);
  918. printer->Print(variables_,
  919. "$deprecated_attr$void ${$add_$name$$}$(const $pointer_type$* "
  920. "value, size_t size)"
  921. ";\n");
  922. printer->Annotate("{", "}", descriptor_);
  923. printer->Print(
  924. variables_,
  925. "$deprecated_attr$const ::google::protobuf::RepeatedPtrField< ::std::string>& $name$() "
  926. "const;\n");
  927. printer->Annotate("name", descriptor_);
  928. printer->Print(variables_,
  929. "$deprecated_attr$::google::protobuf::RepeatedPtrField< ::std::string>* "
  930. "${$mutable_$name$$}$()"
  931. ";\n");
  932. printer->Annotate("{", "}", descriptor_);
  933. if (unknown_ctype) {
  934. printer->Outdent();
  935. printer->Print(" public:\n");
  936. printer->Indent();
  937. }
  938. }
  939. void RepeatedStringFieldGenerator::
  940. GenerateInlineAccessorDefinitions(io::Printer* printer) const {
  941. if (options_.safe_boundary_check) {
  942. printer->Print(variables_,
  943. "inline const ::std::string& $classname$::$name$(int index) const {\n"
  944. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  945. " return $name$_.InternalCheckedGet(\n"
  946. " index, ::google::protobuf::internal::GetEmptyStringAlreadyInited());\n"
  947. "}\n");
  948. } else {
  949. printer->Print(variables_,
  950. "inline const ::std::string& $classname$::$name$(int index) const {\n"
  951. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  952. " return $name$_.Get(index);\n"
  953. "}\n");
  954. }
  955. printer->Print(variables_,
  956. "inline ::std::string* $classname$::mutable_$name$(int index) {\n"
  957. " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
  958. " return $name$_.Mutable(index);\n"
  959. "}\n"
  960. "inline void $classname$::set_$name$(int index, const ::std::string& value) {\n"
  961. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  962. " $name$_.Mutable(index)->assign(value);\n"
  963. "}\n"
  964. "#if LANG_CXX11\n"
  965. "inline void $classname$::set_$name$(int index, ::std::string&& value) {\n"
  966. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  967. " $name$_.Mutable(index)->assign(std::move(value));\n"
  968. "}\n"
  969. "#endif\n"
  970. "inline void $classname$::set_$name$(int index, const char* value) {\n"
  971. " $null_check$"
  972. " $name$_.Mutable(index)->assign(value);\n"
  973. " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
  974. "}\n"
  975. "inline void "
  976. "$classname$::set_$name$"
  977. "(int index, const $pointer_type$* value, size_t size) {\n"
  978. " $name$_.Mutable(index)->assign(\n"
  979. " reinterpret_cast<const char*>(value), size);\n"
  980. " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
  981. "}\n"
  982. "inline ::std::string* $classname$::add_$name$() {\n"
  983. " // @@protoc_insertion_point(field_add_mutable:$full_name$)\n"
  984. " return $name$_.Add();\n"
  985. "}\n"
  986. "inline void $classname$::add_$name$(const ::std::string& value) {\n"
  987. " $name$_.Add()->assign(value);\n"
  988. " // @@protoc_insertion_point(field_add:$full_name$)\n"
  989. "}\n"
  990. "#if LANG_CXX11\n"
  991. "inline void $classname$::add_$name$(::std::string&& value) {\n"
  992. " $name$_.Add(std::move(value));\n"
  993. " // @@protoc_insertion_point(field_add:$full_name$)\n"
  994. "}\n"
  995. "#endif\n"
  996. "inline void $classname$::add_$name$(const char* value) {\n"
  997. " $null_check$"
  998. " $name$_.Add()->assign(value);\n"
  999. " // @@protoc_insertion_point(field_add_char:$full_name$)\n"
  1000. "}\n"
  1001. "inline void "
  1002. "$classname$::add_$name$(const $pointer_type$* value, size_t size) {\n"
  1003. " $name$_.Add()->assign(reinterpret_cast<const char*>(value), size);\n"
  1004. " // @@protoc_insertion_point(field_add_pointer:$full_name$)\n"
  1005. "}\n"
  1006. "inline const ::google::protobuf::RepeatedPtrField< ::std::string>&\n"
  1007. "$classname$::$name$() const {\n"
  1008. " // @@protoc_insertion_point(field_list:$full_name$)\n"
  1009. " return $name$_;\n"
  1010. "}\n"
  1011. "inline ::google::protobuf::RepeatedPtrField< ::std::string>*\n"
  1012. "$classname$::mutable_$name$() {\n"
  1013. " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n"
  1014. " return &$name$_;\n"
  1015. "}\n");
  1016. }
  1017. void RepeatedStringFieldGenerator::
  1018. GenerateClearingCode(io::Printer* printer) const {
  1019. printer->Print(variables_, "$name$_.Clear();\n");
  1020. }
  1021. void RepeatedStringFieldGenerator::
  1022. GenerateMergingCode(io::Printer* printer) const {
  1023. printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
  1024. }
  1025. void RepeatedStringFieldGenerator::
  1026. GenerateSwappingCode(io::Printer* printer) const {
  1027. printer->Print(variables_,
  1028. "$name$_.InternalSwap(CastToBase(&other->$name$_));\n");
  1029. }
  1030. void RepeatedStringFieldGenerator::
  1031. GenerateConstructorCode(io::Printer* printer) const {
  1032. // Not needed for repeated fields.
  1033. }
  1034. void RepeatedStringFieldGenerator::
  1035. GenerateCopyConstructorCode(io::Printer* printer) const {
  1036. printer->Print(variables_, "$name$_.CopyFrom(from.$name$_);");
  1037. }
  1038. void RepeatedStringFieldGenerator::
  1039. GenerateMergeFromCodedStream(io::Printer* printer) const {
  1040. printer->Print(variables_,
  1041. "DO_(::google::protobuf::internal::WireFormatLite::Read$declared_type$(\n"
  1042. " input, this->add_$name$()));\n");
  1043. if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
  1044. GenerateUtf8CheckCodeForString(
  1045. descriptor_, options_, true, variables_,
  1046. "this->$name$(this->$name$_size() - 1).data(),\n"
  1047. "static_cast<int>(this->$name$(this->$name$_size() - 1).length()),\n",
  1048. printer);
  1049. }
  1050. }
  1051. void RepeatedStringFieldGenerator::
  1052. GenerateSerializeWithCachedSizes(io::Printer* printer) const {
  1053. printer->Print(variables_,
  1054. "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n");
  1055. printer->Indent();
  1056. if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
  1057. GenerateUtf8CheckCodeForString(
  1058. descriptor_, options_, false, variables_,
  1059. "this->$name$(i).data(), static_cast<int>(this->$name$(i).length()),\n",
  1060. printer);
  1061. }
  1062. printer->Outdent();
  1063. printer->Print(variables_,
  1064. " ::google::protobuf::internal::WireFormatLite::Write$declared_type$(\n"
  1065. " $number$, this->$name$(i), output);\n"
  1066. "}\n");
  1067. }
  1068. void RepeatedStringFieldGenerator::
  1069. GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
  1070. printer->Print(variables_,
  1071. "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n");
  1072. printer->Indent();
  1073. if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
  1074. GenerateUtf8CheckCodeForString(
  1075. descriptor_, options_, false, variables_,
  1076. "this->$name$(i).data(), static_cast<int>(this->$name$(i).length()),\n",
  1077. printer);
  1078. }
  1079. printer->Outdent();
  1080. printer->Print(variables_,
  1081. " target = ::google::protobuf::internal::WireFormatLite::\n"
  1082. " Write$declared_type$ToArray($number$, this->$name$(i), target);\n"
  1083. "}\n");
  1084. }
  1085. void RepeatedStringFieldGenerator::
  1086. GenerateByteSize(io::Printer* printer) const {
  1087. printer->Print(variables_,
  1088. "total_size += $tag_size$ *\n"
  1089. " ::google::protobuf::internal::FromIntSize(this->$name$_size());\n"
  1090. "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"
  1091. " total_size += ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n"
  1092. " this->$name$(i));\n"
  1093. "}\n");
  1094. }
  1095. } // namespace cpp
  1096. } // namespace compiler
  1097. } // namespace protobuf
  1098. } // namespace google