cpp_string_field.cc 50 KB

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