12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301 |
- // Protocol Buffers - Google's data interchange format
- // Copyright 2008 Google Inc. All rights reserved.
- // https://developers.google.com/protocol-buffers/
- //
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions are
- // met:
- //
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- // * Redistributions in binary form must reproduce the above
- // copyright notice, this list of conditions and the following disclaimer
- // in the documentation and/or other materials provided with the
- // distribution.
- // * Neither the name of Google Inc. nor the names of its
- // contributors may be used to endorse or promote products derived from
- // this software without specific prior written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- // Author: kenton@google.com (Kenton Varda)
- // Based on original Protocol Buffers design by
- // Sanjay Ghemawat, Jeff Dean, and others.
- #include <google/protobuf/compiler/cpp/cpp_string_field.h>
- #include <google/protobuf/compiler/cpp/cpp_helpers.h>
- #include <google/protobuf/descriptor.pb.h>
- #include <google/protobuf/io/printer.h>
- #include <google/protobuf/stubs/strutil.h>
- namespace google {
- namespace protobuf {
- namespace compiler {
- namespace cpp {
- namespace {
- void SetStringVariables(const FieldDescriptor* descriptor,
- std::map<std::string, std::string>* variables,
- const Options& options) {
- SetCommonFieldVariables(descriptor, variables, options);
- (*variables)["default"] = DefaultValue(options, descriptor);
- (*variables)["default_length"] =
- StrCat(descriptor->default_value_string().length());
- std::string default_variable_string = MakeDefaultName(descriptor);
- (*variables)["default_variable_name"] = default_variable_string;
- (*variables)["default_variable"] =
- descriptor->default_value_string().empty()
- ? "&::" + (*variables)["proto_ns"] +
- "::internal::GetEmptyStringAlreadyInited()"
- : "&" + QualifiedClassName(descriptor->containing_type(), options) +
- "::" + default_variable_string + ".get()";
- (*variables)["pointer_type"] =
- descriptor->type() == FieldDescriptor::TYPE_BYTES ? "void" : "char";
- (*variables)["null_check"] = (*variables)["DCHK"] + "(value != nullptr);\n";
- // NOTE: Escaped here to unblock proto1->proto2 migration.
- // TODO(liujisi): Extend this to apply for other conflicting methods.
- (*variables)["release_name"] =
- SafeFunctionName(descriptor->containing_type(), descriptor, "release_");
- (*variables)["full_name"] = descriptor->full_name();
- if (options.opensource_runtime) {
- (*variables)["string_piece"] = "::std::string";
- } else {
- (*variables)["string_piece"] = "::StringPiece";
- }
- (*variables)["lite"] =
- HasDescriptorMethods(descriptor->file(), options) ? "" : "Lite";
- }
- } // namespace
- // ===================================================================
- StringFieldGenerator::StringFieldGenerator(const FieldDescriptor* descriptor,
- const Options& options)
- : FieldGenerator(descriptor, options),
- lite_(!HasDescriptorMethods(descriptor->file(), options)),
- inlined_(IsStringInlined(descriptor, options)) {
- SetStringVariables(descriptor, &variables_, options);
- }
- StringFieldGenerator::~StringFieldGenerator() {}
- void StringFieldGenerator::GeneratePrivateMembers(io::Printer* printer) const {
- Formatter format(printer, variables_);
- if (inlined_) {
- format("::$proto_ns$::internal::InlinedStringField $name$_;\n");
- } else {
- // N.B. that we continue to use |ArenaStringPtr| instead of |string*| for
- // string fields, even when SupportArenas(descriptor_) == false. Why? The
- // simple answer is to avoid unmaintainable complexity. The reflection code
- // assumes ArenaStringPtrs. These are *almost* in-memory-compatible with
- // string*, except for the pointer tags and related ownership semantics. We
- // could modify the runtime code to use string* for the
- // not-supporting-arenas case, but this would require a way to detect which
- // type of class was generated (adding overhead and complexity to
- // GeneratedMessageReflection) and littering the runtime code paths with
- // conditionals. It's simpler to stick with this but use lightweight
- // accessors that assume arena == NULL. There should be very little
- // overhead anyway because it's just a tagged pointer in-memory.
- format("::$proto_ns$::internal::ArenaStringPtr $name$_;\n");
- }
- }
- void StringFieldGenerator::GenerateStaticMembers(io::Printer* printer) const {
- Formatter format(printer, variables_);
- if (!descriptor_->default_value_string().empty()) {
- // We make the default instance public, so it can be initialized by
- // non-friend code.
- format(
- "public:\n"
- "static ::$proto_ns$::internal::ExplicitlyConstructed<std::string>"
- " $default_variable_name$;\n"
- "private:\n");
- }
- }
- void StringFieldGenerator::GenerateAccessorDeclarations(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- // If we're using StringFieldGenerator for a field with a ctype, it's
- // because that ctype isn't actually implemented. In particular, this is
- // true of ctype=CORD and ctype=STRING_PIECE in the open source release.
- // We aren't releasing Cord because it has too many Google-specific
- // dependencies and we aren't releasing StringPiece because it's hardly
- // useful outside of Google and because it would get confusing to have
- // multiple instances of the StringPiece class in different libraries (PCRE
- // already includes it for their C++ bindings, which came from Google).
- //
- // In any case, we make all the accessors private while still actually
- // using a string to represent the field internally. This way, we can
- // guarantee that if we do ever implement the ctype, it won't break any
- // existing users who might be -- for whatever reason -- already using .proto
- // files that applied the ctype. The field can still be accessed via the
- // reflection interface since the reflection interface is independent of
- // the string's underlying representation.
- bool unknown_ctype = descriptor_->options().ctype() !=
- EffectiveStringCType(descriptor_, options_);
- if (unknown_ctype) {
- format.Outdent();
- format(
- " private:\n"
- " // Hidden due to unknown ctype option.\n");
- format.Indent();
- }
- format(
- "$deprecated_attr$const std::string& ${1$$name$$}$() const;\n"
- "$deprecated_attr$void ${1$set_$name$$}$(const std::string& value);\n"
- "$deprecated_attr$void ${1$set_$name$$}$(std::string&& value);\n"
- "$deprecated_attr$void ${1$set_$name$$}$(const char* value);\n",
- descriptor_);
- if (!options_.opensource_runtime) {
- format(
- "$deprecated_attr$void ${1$set_$name$$}$(::StringPiece value);\n",
- descriptor_);
- }
- format(
- "$deprecated_attr$void ${1$set_$name$$}$(const $pointer_type$* "
- "value, size_t size)"
- ";\n"
- "$deprecated_attr$std::string* ${1$mutable_$name$$}$();\n"
- "$deprecated_attr$std::string* ${1$$release_name$$}$();\n"
- "$deprecated_attr$void ${1$set_allocated_$name$$}$(std::string* "
- "$name$);\n",
- descriptor_);
- if (options_.opensource_runtime) {
- if (SupportsArenas(descriptor_)) {
- format(
- "$GOOGLE_PROTOBUF$_RUNTIME_DEPRECATED(\"The unsafe_arena_ accessors "
- "for\"\n"
- "\" string fields are deprecated and will be removed in a\"\n"
- "\" future release.\")\n"
- "std::string* ${1$unsafe_arena_release_$name$$}$();\n"
- "$GOOGLE_PROTOBUF$_RUNTIME_DEPRECATED(\"The unsafe_arena_ accessors "
- "for\"\n"
- "\" string fields are deprecated and will be removed in a\"\n"
- "\" future release.\")\n"
- "void ${1$unsafe_arena_set_allocated_$name$$}$(\n"
- " std::string* $name$);\n",
- descriptor_);
- }
- }
- format(
- "private:\n"
- "const std::string& _internal_$name$() const;\n"
- "void _internal_set_$name$(const std::string& value);\n"
- "std::string* _internal_mutable_$name$();\n"
- "public:\n");
- if (unknown_ctype) {
- format.Outdent();
- format(" public:\n");
- format.Indent();
- }
- }
- void StringFieldGenerator::GenerateInlineAccessorDefinitions(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- format(
- "inline const std::string& $classname$::$name$() const {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_get:$full_name$)\n"
- " return _internal_$name$();\n"
- "}\n"
- "inline void $classname$::set_$name$(const std::string& value) {\n"
- "$annotate_accessor$"
- " _internal_set_$name$(value);\n"
- " // @@protoc_insertion_point(field_set:$full_name$)\n"
- "}\n"
- "inline std::string* $classname$::mutable_$name$() {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
- " return _internal_mutable_$name$();\n"
- "}\n");
- if (SupportsArenas(descriptor_)) {
- format(
- "inline const std::string& $classname$::_internal_$name$() const {\n"
- " return $name$_.Get();\n"
- "}\n"
- "inline void $classname$::_internal_set_$name$(const std::string& "
- "value) {\n"
- " $set_hasbit$\n"
- " $name$_.Set$lite$($default_variable$, value, GetArenaNoVirtual());\n"
- "}\n"
- "inline void $classname$::set_$name$(std::string&& value) {\n"
- "$annotate_accessor$"
- " $set_hasbit$\n"
- " $name$_.Set$lite$(\n"
- " $default_variable$, ::std::move(value), GetArenaNoVirtual());\n"
- " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n"
- "}\n"
- "inline void $classname$::set_$name$(const char* value) {\n"
- "$annotate_accessor$"
- " $null_check$"
- " $set_hasbit$\n"
- " $name$_.Set$lite$($default_variable$, $string_piece$(value),\n"
- " GetArenaNoVirtual());\n"
- " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
- "}\n");
- if (!options_.opensource_runtime) {
- format(
- "inline void $classname$::set_$name$(::StringPiece value) {\n"
- "$annotate_accessor$"
- " $set_hasbit$\n"
- " $name$_.Set$lite$($default_variable$, value, "
- "GetArenaNoVirtual());\n"
- " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n"
- "}\n");
- }
- format(
- "inline "
- "void $classname$::set_$name$(const $pointer_type$* value,\n"
- " size_t size) {\n"
- "$annotate_accessor$"
- " $set_hasbit$\n"
- " $name$_.Set$lite$($default_variable$, $string_piece$(\n"
- " reinterpret_cast<const char*>(value), size), "
- "GetArenaNoVirtual());\n"
- " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
- "}\n"
- "inline std::string* $classname$::_internal_mutable_$name$() {\n"
- " $set_hasbit$\n"
- " return $name$_.Mutable($default_variable$, GetArenaNoVirtual());\n"
- "}\n"
- "inline std::string* $classname$::$release_name$() {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_release:$full_name$)\n");
- if (HasFieldPresence(descriptor_->file())) {
- format(
- " if (!has_$name$()) {\n"
- " return nullptr;\n"
- " }\n"
- " $clear_hasbit$\n"
- " return $name$_.ReleaseNonDefault("
- "$default_variable$, GetArenaNoVirtual());\n");
- } else {
- format(
- " $clear_hasbit$\n"
- " return $name$_.Release($default_variable$, "
- "GetArenaNoVirtual());\n");
- }
- format(
- "}\n"
- "inline void $classname$::set_allocated_$name$(std::string* $name$) {\n"
- "$annotate_accessor$"
- " if ($name$ != nullptr) {\n"
- " $set_hasbit$\n"
- " } else {\n"
- " $clear_hasbit$\n"
- " }\n"
- " $name$_.SetAllocated($default_variable$, $name$,\n"
- " GetArenaNoVirtual());\n"
- " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
- "}\n");
- if (options_.opensource_runtime) {
- format(
- "inline std::string* $classname$::unsafe_arena_release_$name$() {\n"
- "$annotate_accessor$"
- " // "
- "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n"
- " $DCHK$(GetArenaNoVirtual() != nullptr);\n"
- " $clear_hasbit$\n"
- " return $name$_.UnsafeArenaRelease($default_variable$,\n"
- " GetArenaNoVirtual());\n"
- "}\n"
- "inline void $classname$::unsafe_arena_set_allocated_$name$(\n"
- "$annotate_accessor$"
- " std::string* $name$) {\n"
- " $DCHK$(GetArenaNoVirtual() != nullptr);\n"
- " if ($name$ != nullptr) {\n"
- " $set_hasbit$\n"
- " } else {\n"
- " $clear_hasbit$\n"
- " }\n"
- " $name$_.UnsafeArenaSetAllocated($default_variable$,\n"
- " $name$, GetArenaNoVirtual());\n"
- " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:"
- "$full_name$)\n"
- "}\n");
- }
- } else {
- // No-arena case.
- format(
- "inline const std::string& $classname$::_internal_$name$() const {\n"
- " return $name$_.GetNoArena();\n"
- "}\n"
- "inline void $classname$::_internal_set_$name$(const std::string& "
- "value) {\n"
- " $set_hasbit$\n"
- " $name$_.SetNoArena($default_variable$, value);\n"
- "}\n"
- "inline void $classname$::set_$name$(std::string&& value) {\n"
- "$annotate_accessor$"
- " $set_hasbit$\n"
- " $name$_.SetNoArena(\n"
- " $default_variable$, ::std::move(value));\n"
- " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n"
- "}\n"
- "inline void $classname$::set_$name$(const char* value) {\n"
- "$annotate_accessor$"
- " $null_check$"
- " $set_hasbit$\n"
- " $name$_.SetNoArena($default_variable$, $string_piece$(value));\n"
- " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
- "}\n");
- if (!options_.opensource_runtime) {
- format(
- "inline void $classname$::set_$name$(::StringPiece value) {\n"
- "$annotate_accessor$"
- " $set_hasbit$\n"
- " $name$_.SetNoArena($default_variable$, value);\n"
- " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n"
- "}\n");
- }
- format(
- "inline "
- "void $classname$::set_$name$(const $pointer_type$* value, "
- "size_t size) {\n"
- "$annotate_accessor$"
- " $set_hasbit$\n"
- " $name$_.SetNoArena($default_variable$,\n"
- " $string_piece$(reinterpret_cast<const char*>(value), size));\n"
- " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
- "}\n"
- "inline std::string* $classname$::_internal_mutable_$name$() {\n"
- " $set_hasbit$\n"
- " return $name$_.MutableNoArena($default_variable$);\n"
- "}\n"
- "inline std::string* $classname$::$release_name$() {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_release:$full_name$)\n");
- if (HasFieldPresence(descriptor_->file())) {
- format(
- " if (!has_$name$()) {\n"
- " return nullptr;\n"
- " }\n"
- " $clear_hasbit$\n"
- " return $name$_.ReleaseNonDefaultNoArena($default_variable$);\n");
- } else {
- format(
- " $clear_hasbit$\n"
- " return $name$_.ReleaseNoArena($default_variable$);\n");
- }
- format(
- "}\n"
- "inline void $classname$::set_allocated_$name$(std::string* $name$) {\n"
- "$annotate_accessor$"
- " if ($name$ != nullptr) {\n"
- " $set_hasbit$\n"
- " } else {\n"
- " $clear_hasbit$\n"
- " }\n"
- " $name$_.SetAllocatedNoArena($default_variable$, $name$);\n"
- " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
- "}\n");
- }
- }
- void StringFieldGenerator::GenerateNonInlineAccessorDefinitions(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- if (!descriptor_->default_value_string().empty()) {
- // Initialized in GenerateDefaultInstanceAllocator.
- format(
- "::$proto_ns$::internal::ExplicitlyConstructed<std::string> "
- "$classname$::$default_variable_name$;\n");
- }
- }
- void StringFieldGenerator::GenerateClearingCode(io::Printer* printer) const {
- Formatter format(printer, variables_);
- // Two-dimension specialization here: supporting arenas or not, and default
- // value is the empty string or not. Complexity here ensures the minimal
- // number of branches / amount of extraneous code at runtime (given that the
- // below methods are inlined one-liners)!
- if (SupportsArenas(descriptor_)) {
- if (descriptor_->default_value_string().empty()) {
- format(
- "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n");
- } else {
- format(
- "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n");
- }
- } else {
- if (descriptor_->default_value_string().empty()) {
- format("$name$_.ClearToEmptyNoArena($default_variable$);\n");
- } else {
- format("$name$_.ClearToDefaultNoArena($default_variable$);\n");
- }
- }
- }
- void StringFieldGenerator::GenerateMessageClearingCode(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- // Two-dimension specialization here: supporting arenas, field presence, or
- // not, and default value is the empty string or not. Complexity here ensures
- // the minimal number of branches / amount of extraneous code at runtime
- // (given that the below methods are inlined one-liners)!
- // If we have field presence, then the Clear() method of the protocol buffer
- // will have checked that this field is set. If so, we can avoid redundant
- // checks against default_variable.
- const bool must_be_present = HasFieldPresence(descriptor_->file());
- if (inlined_ && must_be_present) {
- // Calling mutable_$name$() gives us a string reference and sets the has bit
- // for $name$ (in proto2). We may get here when the string field is inlined
- // but the string's contents have not been changed by the user, so we cannot
- // make an assertion about the contents of the string and could never make
- // an assertion about the string instance.
- //
- // For non-inlined strings, we distinguish from non-default by comparing
- // instances, rather than contents.
- format("$DCHK$(!$name$_.IsDefault($default_variable$));\n");
- }
- if (SupportsArenas(descriptor_)) {
- if (descriptor_->default_value_string().empty()) {
- if (must_be_present) {
- format("$name$_.ClearNonDefaultToEmpty();\n");
- } else {
- format(
- "$name$_.ClearToEmpty($default_variable$, GetArenaNoVirtual());\n");
- }
- } else {
- // Clear to a non-empty default is more involved, as we try to use the
- // Arena if one is present and may need to reallocate the string.
- format(
- "$name$_.ClearToDefault($default_variable$, GetArenaNoVirtual());\n");
- }
- } else if (must_be_present) {
- // When Arenas are disabled and field presence has been checked, we can
- // safely treat the ArenaStringPtr as a string*.
- if (descriptor_->default_value_string().empty()) {
- format("$name$_.ClearNonDefaultToEmptyNoArena();\n");
- } else {
- format("$name$_.UnsafeMutablePointer()->assign(*$default_variable$);\n");
- }
- } else {
- if (descriptor_->default_value_string().empty()) {
- format("$name$_.ClearToEmptyNoArena($default_variable$);\n");
- } else {
- format("$name$_.ClearToDefaultNoArena($default_variable$);\n");
- }
- }
- }
- void StringFieldGenerator::GenerateMergingCode(io::Printer* printer) const {
- Formatter format(printer, variables_);
- if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) {
- // TODO(gpike): improve this
- format("_internal_set_$name$(from._internal_$name$());\n");
- } else {
- format(
- "$set_hasbit$\n"
- "$name$_.AssignWithDefault($default_variable$, from.$name$_);\n");
- }
- }
- void StringFieldGenerator::GenerateSwappingCode(io::Printer* printer) const {
- Formatter format(printer, variables_);
- if (inlined_) {
- format("$name$_.Swap(&other->$name$_);\n");
- } else {
- format(
- "$name$_.Swap(&other->$name$_, $default_variable$,\n"
- " GetArenaNoVirtual());\n");
- }
- }
- void StringFieldGenerator::GenerateConstructorCode(io::Printer* printer) const {
- Formatter format(printer, variables_);
- // TODO(ckennelly): Construct non-empty strings as part of the initializer
- // list.
- if (inlined_ && descriptor_->default_value_string().empty()) {
- // Automatic initialization will construct the string.
- return;
- }
- format("$name$_.UnsafeSetDefault($default_variable$);\n");
- }
- void StringFieldGenerator::GenerateCopyConstructorCode(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- GenerateConstructorCode(printer);
- if (HasFieldPresence(descriptor_->file())) {
- format("if (from._internal_has_$name$()) {\n");
- } else {
- format("if (!from._internal_$name$().empty()) {\n");
- }
- format.Indent();
- if (SupportsArenas(descriptor_) || descriptor_->containing_oneof() != NULL) {
- // TODO(gpike): improve this
- format(
- "$name$_.Set$lite$($default_variable$, from._internal_$name$(),\n"
- " GetArenaNoVirtual());\n");
- } else {
- format("$name$_.AssignWithDefault($default_variable$, from.$name$_);\n");
- }
- format.Outdent();
- format("}\n");
- }
- void StringFieldGenerator::GenerateDestructorCode(io::Printer* printer) const {
- Formatter format(printer, variables_);
- if (inlined_) {
- // The destructor is automatically invoked.
- return;
- }
- format("$name$_.DestroyNoArena($default_variable$);\n");
- }
- bool StringFieldGenerator::GenerateArenaDestructorCode(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- if (!inlined_) {
- return false;
- }
- format("_this->$name$_.DestroyNoArena($default_variable$);\n");
- return true;
- }
- void StringFieldGenerator::GenerateDefaultInstanceAllocator(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- if (!descriptor_->default_value_string().empty()) {
- format(
- "$ns$::$classname$::$default_variable_name$.DefaultConstruct();\n"
- "*$ns$::$classname$::$default_variable_name$.get_mutable() = "
- "std::string($default$, $default_length$);\n"
- "::$proto_ns$::internal::OnShutdownDestroyString(\n"
- " $ns$::$classname$::$default_variable_name$.get_mutable());\n");
- }
- }
- void StringFieldGenerator::GenerateMergeFromCodedStream(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- // The google3 version of proto2 has ArenaStrings and parses into them
- // directly, but for the open-source release, we always parse into std::string
- // instances. Note that for lite, we do similarly to the open source release
- // and use std::string, not ArenaString.
- if (!options_.opensource_runtime && !inlined_ &&
- SupportsArenas(descriptor_) && !lite_) {
- // If arena != NULL, the current string is either an ArenaString (no
- // destructor necessary) or a materialized std::string (and is on the
- // Arena's destructor list). No call to ArenaStringPtr::Destroy is needed.
- format(
- "if (arena != nullptr) {\n"
- " ::$proto_ns$::internal::TaggedPtr<std::string> str =\n"
- " ::$proto_ns$::internal::ReadArenaString(input, arena);\n"
- " DO_(!str.IsNull());\n"
- " $set_hasbit_io$\n"
- " $name$_.UnsafeSetTaggedPointer(str);\n"
- "} else {\n"
- " DO_(::$proto_ns$::internal::WireFormatLite::Read$declared_type$(\n"
- " input, this->_internal_mutable_$name$()));\n"
- "}\n");
- } else {
- format(
- "DO_(::$proto_ns$::internal::WireFormatLite::Read$declared_type$(\n"
- " input, this->_internal_mutable_$name$()));\n");
- }
- if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
- GenerateUtf8CheckCodeForString(
- descriptor_, options_, true,
- "this->_internal_$name$().data(), "
- "static_cast<int>(this->_internal_$name$().length()),\n",
- format);
- }
- }
- bool StringFieldGenerator::MergeFromCodedStreamNeedsArena() const {
- return !lite_ && !inlined_ && !options_.opensource_runtime;
- }
- void StringFieldGenerator::GenerateSerializeWithCachedSizesToArray(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
- GenerateUtf8CheckCodeForString(
- descriptor_, options_, false,
- "this->_internal_$name$().data(), "
- "static_cast<int>(this->_internal_$name$().length()),\n",
- format);
- }
- format(
- "target = stream->Write$declared_type$MaybeAliased(\n"
- " $number$, this->_internal_$name$(), target);\n");
- }
- void StringFieldGenerator::GenerateByteSize(io::Printer* printer) const {
- Formatter format(printer, variables_);
- format(
- "total_size += $tag_size$ +\n"
- " ::$proto_ns$::internal::WireFormatLite::$declared_type$Size(\n"
- " this->_internal_$name$());\n");
- }
- uint32 StringFieldGenerator::CalculateFieldTag() const {
- return inlined_ ? 1 : 0;
- }
- // ===================================================================
- StringOneofFieldGenerator::StringOneofFieldGenerator(
- const FieldDescriptor* descriptor, const Options& options)
- : StringFieldGenerator(descriptor, options) {
- inlined_ = false;
- SetCommonOneofFieldVariables(descriptor, &variables_);
- variables_["field_name"] = UnderscoresToCamelCase(descriptor->name(), true);
- variables_["oneof_index"] =
- StrCat(descriptor->containing_oneof()->index());
- // has_$name$() for oneof fields is private if has_bit is not present. In that
- // case, use _has_$name$() instead of _internal_has_$name$().
- variables_["internal"] =
- HasFieldPresence(descriptor->file()) ? "_internal_" : "";
- }
- StringOneofFieldGenerator::~StringOneofFieldGenerator() {}
- void StringOneofFieldGenerator::GenerateInlineAccessorDefinitions(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- format(
- "inline const std::string& $classname$::$name$() const {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_get:$full_name$)\n"
- " return _internal_$name$();\n"
- "}\n"
- "inline void $classname$::set_$name$(const std::string& value) {\n"
- "$annotate_accessor$"
- " _internal_set_$name$(value);\n"
- " // @@protoc_insertion_point(field_set:$full_name$)\n"
- "}\n"
- "inline std::string* $classname$::mutable_$name$() {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
- " return _internal_mutable_$name$();\n"
- "}\n");
- if (SupportsArenas(descriptor_)) {
- format(
- "inline const std::string& $classname$::_internal_$name$() const {\n"
- " if ($internal$has_$name$()) {\n"
- " return $field_member$.Get();\n"
- " }\n"
- " return *$default_variable$;\n"
- "}\n"
- "inline void $classname$::_internal_set_$name$(const std::string& "
- "value) {\n"
- " if (!$internal$has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " }\n"
- " $field_member$.Set$lite$($default_variable$, value,\n"
- " GetArenaNoVirtual());\n"
- "}\n"
- "inline void $classname$::set_$name$(std::string&& value) {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_set:$full_name$)\n"
- " if (!has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " }\n"
- " $field_member$.Set$lite$(\n"
- " $default_variable$, ::std::move(value), GetArenaNoVirtual());\n"
- " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n"
- "}\n"
- "inline void $classname$::set_$name$(const char* value) {\n"
- "$annotate_accessor$"
- " $null_check$"
- " if (!has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " }\n"
- " $field_member$.Set$lite$($default_variable$,\n"
- " $string_piece$(value), GetArenaNoVirtual());\n"
- " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
- "}\n");
- if (!options_.opensource_runtime) {
- format(
- "inline void $classname$::set_$name$(::StringPiece value) {\n"
- "$annotate_accessor$"
- " if (!has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " }\n"
- " $field_member$.Set$lite$($default_variable$, value,\n"
- " GetArenaNoVirtual());\n"
- " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n"
- "}\n");
- }
- format(
- "inline "
- "void $classname$::set_$name$(const $pointer_type$* value,\n"
- " size_t size) {\n"
- "$annotate_accessor$"
- " if (!has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " }\n"
- " $field_member$.Set$lite$(\n"
- " $default_variable$, $string_piece$(\n"
- " reinterpret_cast<const char*>(value), size),\n"
- " GetArenaNoVirtual());\n"
- " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
- "}\n"
- "inline std::string* $classname$::_internal_mutable_$name$() {\n"
- " if (!$internal$has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " }\n"
- " return $field_member$.Mutable($default_variable$,\n"
- " GetArenaNoVirtual());\n"
- "}\n"
- "inline std::string* $classname$::$release_name$() {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_release:$full_name$)\n"
- " if (has_$name$()) {\n"
- " clear_has_$oneof_name$();\n"
- " return $field_member$.Release($default_variable$,\n"
- " GetArenaNoVirtual());\n"
- " } else {\n"
- " return nullptr;\n"
- " }\n"
- "}\n"
- "inline void $classname$::set_allocated_$name$(std::string* $name$) {\n"
- "$annotate_accessor$"
- " if (has_$oneof_name$()) {\n"
- " clear_$oneof_name$();\n"
- " }\n"
- " if ($name$ != nullptr) {\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($name$);\n"
- " }\n"
- " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
- "}\n");
- if (options_.opensource_runtime) {
- format(
- "inline std::string* $classname$::unsafe_arena_release_$name$() {\n"
- "$annotate_accessor$"
- " // "
- "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n"
- " $DCHK$(GetArenaNoVirtual() != nullptr);\n"
- " if (has_$name$()) {\n"
- " clear_has_$oneof_name$();\n"
- " return $field_member$.UnsafeArenaRelease(\n"
- " $default_variable$, GetArenaNoVirtual());\n"
- " } else {\n"
- " return nullptr;\n"
- " }\n"
- "}\n"
- "inline void $classname$::unsafe_arena_set_allocated_$name$("
- "std::string* $name$) {\n"
- "$annotate_accessor$"
- " $DCHK$(GetArenaNoVirtual() != nullptr);\n"
- " if (!has_$name$()) {\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " }\n"
- " clear_$oneof_name$();\n"
- " if ($name$) {\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeArenaSetAllocated($default_variable$, "
- "$name$, GetArenaNoVirtual());\n"
- " }\n"
- " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:"
- "$full_name$)\n"
- "}\n");
- }
- } else {
- // No-arena case.
- format(
- "inline const std::string& $classname$::_internal_$name$() const {\n"
- " if ($internal$has_$name$()) {\n"
- " return $field_member$.GetNoArena();\n"
- " }\n"
- " return *$default_variable$;\n"
- "}\n"
- "inline void $classname$::_internal_set_$name$(const std::string& "
- "value) {\n"
- " if (!$internal$has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " }\n"
- " $field_member$.SetNoArena($default_variable$, value);\n"
- "}\n"
- "inline void $classname$::set_$name$(std::string&& value) {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_set:$full_name$)\n"
- " if (!has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " }\n"
- " $field_member$.SetNoArena($default_variable$, ::std::move(value));\n"
- " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n"
- "}\n"
- "inline void $classname$::set_$name$(const char* value) {\n"
- "$annotate_accessor$"
- " $null_check$"
- " if (!has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " }\n"
- " $field_member$.SetNoArena($default_variable$,\n"
- " $string_piece$(value));\n"
- " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
- "}\n");
- if (!options_.opensource_runtime) {
- format(
- "inline void $classname$::set_$name$(::StringPiece value) {\n"
- "$annotate_accessor$"
- " if (!has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " }\n"
- " $field_member$.SetNoArena($default_variable$, value);\n"
- " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n"
- "}\n");
- }
- format(
- "inline "
- "void $classname$::set_$name$(const $pointer_type$* value, size_t "
- "size) {\n"
- "$annotate_accessor$"
- " if (!has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " }\n"
- " $field_member$.SetNoArena($default_variable$, $string_piece$(\n"
- " reinterpret_cast<const char*>(value), size));\n"
- " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
- "}\n"
- "inline std::string* $classname$::_internal_mutable_$name$() {\n"
- " if (!$internal$has_$name$()) {\n"
- " clear_$oneof_name$();\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " }\n"
- " return $field_member$.MutableNoArena($default_variable$);\n"
- "}\n"
- "inline std::string* $classname$::$release_name$() {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_release:$full_name$)\n"
- " if (has_$name$()) {\n"
- " clear_has_$oneof_name$();\n"
- " return $field_member$.ReleaseNoArena($default_variable$);\n"
- " } else {\n"
- " return nullptr;\n"
- " }\n"
- "}\n"
- "inline void $classname$::set_allocated_$name$(std::string* $name$) {\n"
- "$annotate_accessor$"
- " if (has_$oneof_name$()) {\n"
- " clear_$oneof_name$();\n"
- " }\n"
- " if ($name$ != nullptr) {\n"
- " set_has_$name$();\n"
- " $field_member$.UnsafeSetDefault($name$);\n"
- " }\n"
- " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
- "}\n");
- }
- }
- void StringOneofFieldGenerator::GenerateClearingCode(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- if (SupportsArenas(descriptor_)) {
- format(
- "$field_member$.Destroy($default_variable$,\n"
- " GetArenaNoVirtual());\n");
- } else {
- format("$field_member$.DestroyNoArena($default_variable$);\n");
- }
- }
- void StringOneofFieldGenerator::GenerateMessageClearingCode(
- io::Printer* printer) const {
- return GenerateClearingCode(printer);
- }
- void StringOneofFieldGenerator::GenerateSwappingCode(
- io::Printer* printer) const {
- // Don't print any swapping code. Swapping the union will swap this field.
- }
- void StringOneofFieldGenerator::GenerateConstructorCode(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- format(
- "$ns$::_$classname$_default_instance_.$name$_.UnsafeSetDefault(\n"
- " $default_variable$);\n");
- }
- void StringOneofFieldGenerator::GenerateDestructorCode(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- format(
- "if (has_$name$()) {\n"
- " $field_member$.DestroyNoArena($default_variable$);\n"
- "}\n");
- }
- void StringOneofFieldGenerator::GenerateMergeFromCodedStream(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- // See above: ArenaString is not included in the open-source release.
- if (!options_.opensource_runtime && SupportsArenas(descriptor_) && !lite_) {
- // If has_$name$(), then the current string is either an ArenaString (no
- // destructor necessary) or a materialized std::string (and is on the
- // Arena's destructor list). No call to ArenaStringPtr::Destroy is needed.
- format(
- "if (arena != nullptr) {\n"
- " clear_$oneof_name$();\n"
- " if (!has_$name$()) {\n"
- " $field_member$.UnsafeSetDefault($default_variable$);\n"
- " set_has_$name$();\n"
- " }\n"
- " ::$proto_ns$::internal::TaggedPtr<std::string> new_value =\n"
- " ::$proto_ns$::internal::ReadArenaString(input, arena);\n"
- " DO_(!new_value.IsNull());\n"
- " $field_member$.UnsafeSetTaggedPointer(new_value);\n"
- "} else {\n"
- " DO_(::$proto_ns$::internal::WireFormatLite::Read$declared_type$(\n"
- " input, this->_internal_mutable_$name$()));\n"
- "}\n");
- } else {
- format(
- "DO_(::$proto_ns$::internal::WireFormatLite::Read$declared_type$(\n"
- " input, this->_internal_mutable_$name$()));\n");
- }
- if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
- GenerateUtf8CheckCodeForString(
- descriptor_, options_, true,
- "this->_internal_$name$().data(), "
- "static_cast<int>(this->_internal_$name$().length()),\n",
- format);
- }
- }
- // ===================================================================
- RepeatedStringFieldGenerator::RepeatedStringFieldGenerator(
- const FieldDescriptor* descriptor, const Options& options)
- : FieldGenerator(descriptor, options) {
- SetStringVariables(descriptor, &variables_, options);
- }
- RepeatedStringFieldGenerator::~RepeatedStringFieldGenerator() {}
- void RepeatedStringFieldGenerator::GeneratePrivateMembers(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- format("::$proto_ns$::RepeatedPtrField<std::string> $name$_;\n");
- }
- void RepeatedStringFieldGenerator::GenerateAccessorDeclarations(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- // See comment above about unknown ctypes.
- bool unknown_ctype = descriptor_->options().ctype() !=
- EffectiveStringCType(descriptor_, options_);
- if (unknown_ctype) {
- format.Outdent();
- format(
- " private:\n"
- " // Hidden due to unknown ctype option.\n");
- format.Indent();
- }
- format(
- "$deprecated_attr$const std::string& ${1$$name$$}$(int index) const;\n"
- "$deprecated_attr$std::string* ${1$mutable_$name$$}$(int index);\n"
- "$deprecated_attr$void ${1$set_$name$$}$(int index, const "
- "std::string& value);\n"
- "$deprecated_attr$void ${1$set_$name$$}$(int index, std::string&& "
- "value);\n"
- "$deprecated_attr$void ${1$set_$name$$}$(int index, const "
- "char* value);\n",
- descriptor_);
- if (!options_.opensource_runtime) {
- format(
- "$deprecated_attr$void ${1$set_$name$$}$(int index, "
- "StringPiece value);\n",
- descriptor_);
- }
- format(
- "$deprecated_attr$void ${1$set_$name$$}$("
- "int index, const $pointer_type$* value, size_t size);\n"
- "$deprecated_attr$std::string* ${1$add_$name$$}$();\n"
- "$deprecated_attr$void ${1$add_$name$$}$(const std::string& value);\n"
- "$deprecated_attr$void ${1$add_$name$$}$(std::string&& value);\n"
- "$deprecated_attr$void ${1$add_$name$$}$(const char* value);\n",
- descriptor_);
- if (!options_.opensource_runtime) {
- format(
- "$deprecated_attr$void ${1$add_$name$$}$(StringPiece value);\n",
- descriptor_);
- }
- format(
- "$deprecated_attr$void ${1$add_$name$$}$(const $pointer_type$* "
- "value, size_t size)"
- ";\n"
- "$deprecated_attr$const ::$proto_ns$::RepeatedPtrField<std::string>& "
- "${1$$name$$}$() "
- "const;\n"
- "$deprecated_attr$::$proto_ns$::RepeatedPtrField<std::string>* "
- "${1$mutable_$name$$}$()"
- ";\n"
- "private:\n"
- "std::string* _internal_add_$name$();\n"
- "public:\n",
- descriptor_);
- if (unknown_ctype) {
- format.Outdent();
- format(" public:\n");
- format.Indent();
- }
- }
- void RepeatedStringFieldGenerator::GenerateInlineAccessorDefinitions(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- format(
- "inline std::string* $classname$::add_$name$() {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_add_mutable:$full_name$)\n"
- " return _internal_add_$name$();\n"
- "}\n");
- if (options_.safe_boundary_check) {
- format(
- "inline const std::string& $classname$::$name$(int index) const {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_get:$full_name$)\n"
- " return $name$_.InternalCheckedGet(\n"
- " index, ::$proto_ns$::internal::GetEmptyStringAlreadyInited());\n"
- "}\n");
- } else {
- format(
- "inline const std::string& $classname$::$name$(int index) const {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_get:$full_name$)\n"
- " return $name$_.Get(index);\n"
- "}\n");
- }
- format(
- "inline std::string* $classname$::mutable_$name$(int index) {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
- " return $name$_.Mutable(index);\n"
- "}\n"
- "inline void $classname$::set_$name$(int index, const std::string& "
- "value) "
- "{\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_set:$full_name$)\n"
- " $name$_.Mutable(index)->assign(value);\n"
- "}\n"
- "inline void $classname$::set_$name$(int index, std::string&& value) {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_set:$full_name$)\n"
- " $name$_.Mutable(index)->assign(std::move(value));\n"
- "}\n"
- "inline void $classname$::set_$name$(int index, const char* value) {\n"
- "$annotate_accessor$"
- " $null_check$"
- " $name$_.Mutable(index)->assign(value);\n"
- " // @@protoc_insertion_point(field_set_char:$full_name$)\n"
- "}\n");
- if (!options_.opensource_runtime) {
- format(
- "inline void "
- "$classname$::set_$name$(int index, StringPiece value) {\n"
- "$annotate_accessor$"
- " $name$_.Mutable(index)->assign(value.data(), value.size());\n"
- " // @@protoc_insertion_point(field_set_string_piece:$full_name$)\n"
- "}\n");
- }
- format(
- "inline void "
- "$classname$::set_$name$"
- "(int index, const $pointer_type$* value, size_t size) {\n"
- "$annotate_accessor$"
- " $name$_.Mutable(index)->assign(\n"
- " reinterpret_cast<const char*>(value), size);\n"
- " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n"
- "}\n"
- "inline std::string* $classname$::_internal_add_$name$() {\n"
- " return $name$_.Add();\n"
- "}\n"
- "inline void $classname$::add_$name$(const std::string& value) {\n"
- "$annotate_accessor$"
- " $name$_.Add()->assign(value);\n"
- " // @@protoc_insertion_point(field_add:$full_name$)\n"
- "}\n"
- "inline void $classname$::add_$name$(std::string&& value) {\n"
- "$annotate_accessor$"
- " $name$_.Add(std::move(value));\n"
- " // @@protoc_insertion_point(field_add:$full_name$)\n"
- "}\n"
- "inline void $classname$::add_$name$(const char* value) {\n"
- "$annotate_accessor$"
- " $null_check$"
- " $name$_.Add()->assign(value);\n"
- " // @@protoc_insertion_point(field_add_char:$full_name$)\n"
- "}\n");
- if (!options_.opensource_runtime) {
- format(
- "inline void $classname$::add_$name$(StringPiece value) {\n"
- "$annotate_accessor$"
- " $name$_.Add()->assign(value.data(), value.size());\n"
- " // @@protoc_insertion_point(field_add_string_piece:$full_name$)\n"
- "}\n");
- }
- format(
- "inline void "
- "$classname$::add_$name$(const $pointer_type$* value, size_t size) {\n"
- "$annotate_accessor$"
- " $name$_.Add()->assign(reinterpret_cast<const char*>(value), size);\n"
- " // @@protoc_insertion_point(field_add_pointer:$full_name$)\n"
- "}\n"
- "inline const ::$proto_ns$::RepeatedPtrField<std::string>&\n"
- "$classname$::$name$() const {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_list:$full_name$)\n"
- " return $name$_;\n"
- "}\n"
- "inline ::$proto_ns$::RepeatedPtrField<std::string>*\n"
- "$classname$::mutable_$name$() {\n"
- "$annotate_accessor$"
- " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n"
- " return &$name$_;\n"
- "}\n");
- }
- void RepeatedStringFieldGenerator::GenerateClearingCode(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- format("$name$_.Clear();\n");
- }
- void RepeatedStringFieldGenerator::GenerateMergingCode(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- format("$name$_.MergeFrom(from.$name$_);\n");
- }
- void RepeatedStringFieldGenerator::GenerateSwappingCode(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- format("$name$_.InternalSwap(&other->$name$_);\n");
- }
- void RepeatedStringFieldGenerator::GenerateConstructorCode(
- io::Printer* printer) const {
- // Not needed for repeated fields.
- }
- void RepeatedStringFieldGenerator::GenerateCopyConstructorCode(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- format("$name$_.CopyFrom(from.$name$_);");
- }
- void RepeatedStringFieldGenerator::GenerateMergeFromCodedStream(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- format(
- "DO_(::$proto_ns$::internal::WireFormatLite::Read$declared_type$(\n"
- " input, this->_internal_add_$name$()));\n");
- if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
- GenerateUtf8CheckCodeForString(
- descriptor_, options_, true,
- "$name$_.Get($name$_.size() - 1).data(),\n"
- "static_cast<int>($name$_.Get($name$_.size() - 1).length()),\n",
- format);
- }
- }
- void RepeatedStringFieldGenerator::GenerateSerializeWithCachedSizesToArray(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- format(
- "for (auto it = $field_member$.pointer_begin(),\n"
- " end = $field_member$.pointer_end(); it < end; ++it) {\n"
- " const auto& s = **it;\n");
- // format("for (const std::string& s : this->$name$()) {\n");
- format.Indent();
- if (descriptor_->type() == FieldDescriptor::TYPE_STRING) {
- GenerateUtf8CheckCodeForString(descriptor_, options_, false,
- "s.data(), static_cast<int>(s.length()),\n",
- format);
- }
- format.Outdent();
- format(
- " target = stream->Write$declared_type$($number$, s, target);\n"
- "}\n");
- }
- void RepeatedStringFieldGenerator::GenerateByteSize(
- io::Printer* printer) const {
- Formatter format(printer, variables_);
- format(
- "total_size += $tag_size$ *\n"
- " ::$proto_ns$::internal::FromIntSize($name$_.size());\n"
- "for (int i = 0, n = $name$_.size(); i < n; i++) {\n"
- " total_size += "
- "::$proto_ns$::internal::WireFormatLite::$declared_type$Size(\n"
- " $name$_.Get(i));\n"
- "}\n");
- }
- } // namespace cpp
- } // namespace compiler
- } // namespace protobuf
- } // namespace google
|