Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_message_field.cc

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 27 matching lines...) Expand all
38 #include <google/protobuf/stubs/strutil.h> 38 #include <google/protobuf/stubs/strutil.h>
39 39
40 namespace google { 40 namespace google {
41 namespace protobuf { 41 namespace protobuf {
42 namespace compiler { 42 namespace compiler {
43 namespace cpp { 43 namespace cpp {
44 44
45 namespace { 45 namespace {
46 46
47 void SetMessageVariables(const FieldDescriptor* descriptor, 47 void SetMessageVariables(const FieldDescriptor* descriptor,
48 map<string, string>* variables, 48 std::map<string, string>* variables,
49 const Options& options) { 49 const Options& options) {
50 SetCommonFieldVariables(descriptor, variables, options); 50 SetCommonFieldVariables(descriptor, variables, options);
51 (*variables)["type"] = FieldMessageTypeName(descriptor); 51 (*variables)["type"] = FieldMessageTypeName(descriptor);
52 if (descriptor->options().weak() || !descriptor->containing_oneof()) { 52 if (descriptor->options().weak() || !descriptor->containing_oneof()) {
53 (*variables)["non_null_ptr_to_name"] = 53 (*variables)["non_null_ptr_to_name"] =
54 StrCat("this->", (*variables)["name"], "_"); 54 StrCat("this->", (*variables)["name"], "_");
55 } 55 }
56 (*variables)["stream_writer"] = 56 (*variables)["stream_writer"] =
57 (*variables)["declared_type"] + 57 (*variables)["declared_type"] +
58 (HasFastArraySerialization(descriptor->message_type()->file(), options) 58 (HasFastArraySerialization(descriptor->message_type()->file(), options)
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 printer->Print(variables_, 154 printer->Print(variables_,
155 " $name$_ = ::google::protobuf::Arena::Create< $type$ >(\n" 155 " $name$_ = ::google::protobuf::Arena::Create< $type$ >(\n"
156 " GetArenaNoVirtual());\n"); 156 " GetArenaNoVirtual());\n");
157 } 157 }
158 printer->Print(variables_, 158 printer->Print(variables_,
159 "}\n" 159 "}\n"
160 "$type$* $classname$::_slow_$release_name$() {\n" 160 "$type$* $classname$::_slow_$release_name$() {\n"
161 " if ($name$_ == NULL) {\n" 161 " if ($name$_ == NULL) {\n"
162 " return NULL;\n" 162 " return NULL;\n"
163 " } else {\n" 163 " } else {\n"
164 " $type$* temp = new $type$;\n" 164 " $type$* temp = new $type$(*$name$_);\n"
165 " temp->MergeFrom(*$name$_);\n"
166 " $name$_ = NULL;\n" 165 " $name$_ = NULL;\n"
167 " return temp;\n" 166 " return temp;\n"
168 " }\n" 167 " }\n"
169 "}\n" 168 "}\n"
170 "$type$* $classname$::unsafe_arena_release_$name$() {\n" 169 "$type$* $classname$::unsafe_arena_release_$name$() {\n"
171 " // @@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" 170 " // @@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n"
172 " $clear_hasbit$\n" 171 " $clear_hasbit$\n"
173 " $type$* temp = $name$_;\n" 172 " $type$* temp = $name$_;\n"
174 " $name$_ = NULL;\n" 173 " $name$_ = NULL;\n"
175 " return temp;\n" 174 " return temp;\n"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 "}\n"); 211 "}\n");
213 } 212 }
214 } 213 }
215 214
216 void MessageFieldGenerator:: 215 void MessageFieldGenerator::
217 GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const { 216 GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const {
218 if (!dependent_field_) { 217 if (!dependent_field_) {
219 return; 218 return;
220 } 219 }
221 220
222 map<string, string> variables(variables_); 221 std::map<string, string> variables(variables_);
223 // For the CRTP base class, all mutation methods are dependent, and so 222 // For the CRTP base class, all mutation methods are dependent, and so
224 // they must be in the header. 223 // they must be in the header.
225 variables["dependent_classname"] = 224 variables["dependent_classname"] =
226 DependentBaseClassTemplateName(descriptor_->containing_type()) + "<T>"; 225 DependentBaseClassTemplateName(descriptor_->containing_type()) + "<T>";
227 variables["this_message"] = DependentBaseDownCast(); 226 variables["this_message"] = DependentBaseDownCast();
228 if (!variables["set_hasbit"].empty()) { 227 if (!variables["set_hasbit"].empty()) {
229 variables["set_hasbit"] = 228 variables["set_hasbit"] =
230 variables["this_message"] + variables["set_hasbit"]; 229 variables["this_message"] + variables["set_hasbit"];
231 } 230 }
232 if (!variables["clear_hasbit"].empty()) { 231 if (!variables["clear_hasbit"].empty()) {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 " $clear_hasbit$\n" 338 " $clear_hasbit$\n"
340 " }\n" 339 " }\n"
341 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" 340 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
342 "}\n"); 341 "}\n");
343 } 342 }
344 } 343 }
345 344
346 void MessageFieldGenerator:: 345 void MessageFieldGenerator::
347 GenerateInlineAccessorDefinitions(io::Printer* printer, 346 GenerateInlineAccessorDefinitions(io::Printer* printer,
348 bool is_inline) const { 347 bool is_inline) const {
349 map<string, string> variables(variables_); 348 if (dependent_field_) {
349 // for dependent fields we cannot access its internal_default_instance,
350 // because the type is incomplete.
351 // TODO(gerbens) deprecate dependent base class.
352 std::map<string, string> variables(variables_);
353 variables["inline"] = is_inline ? "inline " : "";
354 printer->Print(variables,
355 "$inline$const $type$& $classname$::$name$() const {\n"
356 " // @@protoc_insertion_point(field_get:$full_name$)\n"
357 " return $name$_ != NULL ? *$name$_\n"
358 " : *internal_default_instance()->$name$_;\n"
359 "}\n");
360 return;
361 }
362
363 std::map<string, string> variables(variables_);
350 variables["inline"] = is_inline ? "inline " : ""; 364 variables["inline"] = is_inline ? "inline " : "";
351 printer->Print(variables, 365 printer->Print(variables,
352 "$inline$const $type$& $classname$::$name$() const {\n" 366 "$inline$const $type$& $classname$::$name$() const {\n"
353 " // @@protoc_insertion_point(field_get:$full_name$)\n"); 367 " // @@protoc_insertion_point(field_get:$full_name$)\n"
354 368 " return $name$_ != NULL ? *$name$_\n"
355 PrintHandlingOptionalStaticInitializers( 369 " : *$type$::internal_default_instance();\n"
356 variables, descriptor_->file(), options_, printer, 370 "}\n");
357 // With static initializers.
358 " return $name$_ != NULL ? *$name$_ : *default_instance_->$name$_;\n",
359 // Without.
360 " return $name$_ != NULL ? *$name$_ : *default_instance().$name$_;\n");
361 printer->Print(variables, "}\n");
362
363 if (dependent_field_) {
364 return;
365 }
366 371
367 if (SupportsArenas(descriptor_)) { 372 if (SupportsArenas(descriptor_)) {
368 printer->Print(variables, 373 printer->Print(variables,
369 "$inline$" 374 "$inline$"
370 "$type$* $classname$::mutable_$name$() {\n" 375 "$type$* $classname$::mutable_$name$() {\n"
371 " $set_hasbit$\n" 376 " $set_hasbit$\n"
372 " if ($name$_ == NULL) {\n" 377 " if ($name$_ == NULL) {\n"
373 " _slow_mutable_$name$();\n" 378 " _slow_mutable_$name$();\n"
374 " }\n" 379 " }\n"
375 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" 380 " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 " } else {\n" 462 " } else {\n"
458 " $clear_hasbit$\n" 463 " $clear_hasbit$\n"
459 " }\n" 464 " }\n"
460 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" 465 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
461 "}\n"); 466 "}\n");
462 } 467 }
463 } 468 }
464 469
465 void MessageFieldGenerator:: 470 void MessageFieldGenerator::
466 GenerateClearingCode(io::Printer* printer) const { 471 GenerateClearingCode(io::Printer* printer) const {
467 map<string, string> variables(variables_); 472 std::map<string, string> variables(variables_);
468 variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : ""; 473 variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : "";
469 if (!HasFieldPresence(descriptor_->file())) { 474 if (!HasFieldPresence(descriptor_->file())) {
470 // If we don't have has-bits, message presence is indicated only by ptr != 475 // If we don't have has-bits, message presence is indicated only by ptr !=
471 // NULL. Thus on clear, we need to delete the object. 476 // NULL. Thus on clear, we need to delete the object.
472 printer->Print(variables, 477 printer->Print(variables,
473 "if ($this_message$GetArenaNoVirtual() == NULL && " 478 "if ($this_message$GetArenaNoVirtual() == NULL && "
474 "$this_message$$name$_ != NULL) delete $this_message$$name$_;\n" 479 "$this_message$$name$_ != NULL) delete $this_message$$name$_;\n"
475 "$this_message$$name$_ = NULL;\n"); 480 "$this_message$$name$_ = NULL;\n");
476 } else { 481 } else {
477 printer->Print(variables, 482 printer->Print(variables,
478 "if ($this_message$$name$_ != NULL) $this_message$$name$_->" 483 "if ($this_message$$name$_ != NULL) $this_message$$name$_->"
479 "$dependent_type$::Clear();\n"); 484 "$dependent_type$::Clear();\n");
480 } 485 }
481 } 486 }
482 487
483 void MessageFieldGenerator:: 488 void MessageFieldGenerator::
489 GenerateMessageClearingCode(io::Printer* printer) const {
490 std::map<string, string> variables(variables_);
491 variables["type"] = FieldMessageTypeName(descriptor_);
492
493 if (!HasFieldPresence(descriptor_->file())) {
494 // If we don't have has-bits, message presence is indicated only by ptr !=
495 // NULL. Thus on clear, we need to delete the object.
496 printer->Print(variables_,
497 "if (GetArenaNoVirtual() == NULL && $name$_ != NULL) {\n"
498 " delete $name$_;\n"
499 "}\n"
500 "$name$_ = NULL;\n");
501 } else {
502 printer->Print(variables_,
503 "GOOGLE_DCHECK($name$_ != NULL);\n"
504 "$name$_->$type$::Clear();\n");
505 }
506 }
507
508 void MessageFieldGenerator::
484 GenerateMergingCode(io::Printer* printer) const { 509 GenerateMergingCode(io::Printer* printer) const {
485 printer->Print(variables_, 510 printer->Print(variables_,
486 "mutable_$name$()->$type$::MergeFrom(from.$name$());\n"); 511 "mutable_$name$()->$type$::MergeFrom(from.$name$());\n");
487 } 512 }
488 513
489 void MessageFieldGenerator:: 514 void MessageFieldGenerator::
490 GenerateSwappingCode(io::Printer* printer) const { 515 GenerateSwappingCode(io::Printer* printer) const {
491 printer->Print(variables_, "std::swap($name$_, other->$name$_);\n"); 516 printer->Print(variables_, "std::swap($name$_, other->$name$_);\n");
492 } 517 }
493 518
494 void MessageFieldGenerator:: 519 void MessageFieldGenerator::
520 GenerateDestructorCode(io::Printer* printer) const {
521 // In google3 a default instance will never get deleted so we don't need to
522 // worry about that but in opensource protobuf default instances are deleted
523 // in shutdown process and we need to take special care when handling them.
524 printer->Print(variables_,
525 "if (this != internal_default_instance()) {\n"
526 " delete $name$_;\n"
527 "}\n");
528 }
529
530 void MessageFieldGenerator::
495 GenerateConstructorCode(io::Printer* printer) const { 531 GenerateConstructorCode(io::Printer* printer) const {
496 printer->Print(variables_, "$name$_ = NULL;\n"); 532 printer->Print(variables_, "$name$_ = NULL;\n");
497 } 533 }
498 534
499 void MessageFieldGenerator:: 535 void MessageFieldGenerator::
536 GenerateCopyConstructorCode(io::Printer* printer) const {
537 // For non-Arena enabled messages, everything always goes on the heap.
538 //
539 // For Arena enabled messages, the logic is a bit more convoluted.
540 //
541 // In the copy constructor, we call InternalMetadataWithArena::MergeFrom,
542 // which does *not* copy the Arena pointer. In the generated MergeFrom
543 // (see MessageFieldGenerator::GenerateMergingCode), we:
544 // -> copy the has bits (but this is done in bulk by a memcpy in the copy
545 // constructor)
546 // -> check whether the destination field pointer is NULL (it will be, since
547 // we're initializing it and would have called SharedCtor) and if so:
548 // -> call _slow_mutable_$name$(), which calls either
549 // ::google::protobuf::Arena::CreateMessage<>(GetArenaNoVirtual()), or
550 // ::google::protobuf::Arena::Create<>(GetArenaNoVirtual())
551 //
552 // At this point, GetArenaNoVirtual returns NULL since the Arena pointer
553 // wasn't copied, so both of these methods allocate the submessage on the
554 // heap.
555
556 printer->Print(variables_,
557 "if (from.has_$name$()) {\n"
558 " $name$_ = new $type$(*from.$name$_);\n"
559 "} else {\n"
560 " $name$_ = NULL;\n"
561 "}\n");
562 }
563
564 void MessageFieldGenerator::
500 GenerateMergeFromCodedStream(io::Printer* printer) const { 565 GenerateMergeFromCodedStream(io::Printer* printer) const {
501 if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) { 566 if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) {
502 printer->Print(variables_, 567 printer->Print(variables_,
503 "DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(\n " 568 "DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(\n "
504 " input, mutable_$name$()));\n"); 569 " input, mutable_$name$()));\n");
505 } else { 570 } else {
506 printer->Print(variables_, 571 printer->Print(variables_,
507 "DO_(::google::protobuf::internal::WireFormatLite::ReadGroupNoVirtual(\n" 572 "DO_(::google::protobuf::internal::WireFormatLite::ReadGroupNoVirtual(\n"
508 " $number$, input, mutable_$name$()));\n"); 573 " $number$, input, mutable_$name$()));\n");
509 } 574 }
510 } 575 }
511 576
512 void MessageFieldGenerator:: 577 void MessageFieldGenerator::
513 GenerateSerializeWithCachedSizes(io::Printer* printer) const { 578 GenerateSerializeWithCachedSizes(io::Printer* printer) const {
514 printer->Print(variables_, 579 printer->Print(variables_,
515 "::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n" 580 "::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n"
516 " $number$, *$non_null_ptr_to_name$, output);\n"); 581 " $number$, *$non_null_ptr_to_name$, output);\n");
517 } 582 }
518 583
519 void MessageFieldGenerator:: 584 void MessageFieldGenerator::
520 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { 585 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
521 printer->Print(variables_, 586 printer->Print(variables_,
522 "target = ::google::protobuf::internal::WireFormatLite::\n" 587 "target = ::google::protobuf::internal::WireFormatLite::\n"
523 " Write$declared_type$NoVirtualToArray(\n" 588 " InternalWrite$declared_type$NoVirtualToArray(\n"
524 " $number$, *$non_null_ptr_to_name$, target);\n"); 589 " $number$, *$non_null_ptr_to_name$, false, target);\n");
525 } 590 }
526 591
527 void MessageFieldGenerator:: 592 void MessageFieldGenerator::
528 GenerateByteSize(io::Printer* printer) const { 593 GenerateByteSize(io::Printer* printer) const {
529 printer->Print(variables_, 594 printer->Print(variables_,
530 "total_size += $tag_size$ +\n" 595 "total_size += $tag_size$ +\n"
531 " ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirtua l(\n" 596 " ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirtua l(\n"
532 " *$non_null_ptr_to_name$);\n"); 597 " *$non_null_ptr_to_name$);\n");
533 } 598 }
534 599
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 "$deprecated_attr$const $type$& $name$() const;\n"); 634 "$deprecated_attr$const $type$& $name$() const;\n");
570 } 635 }
571 636
572 void MessageOneofFieldGenerator:: 637 void MessageOneofFieldGenerator::
573 GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const { 638 GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const {
574 // For the CRTP base class, all mutation methods are dependent, and so 639 // For the CRTP base class, all mutation methods are dependent, and so
575 // they must be in the header. 640 // they must be in the header.
576 if (!dependent_base_) { 641 if (!dependent_base_) {
577 return; 642 return;
578 } 643 }
579 map<string, string> variables(variables_); 644 std::map<string, string> variables(variables_);
580 variables["inline"] = "inline "; 645 variables["inline"] = "inline ";
581 variables["dependent_classname"] = 646 variables["dependent_classname"] =
582 DependentBaseClassTemplateName(descriptor_->containing_type()) + "<T>"; 647 DependentBaseClassTemplateName(descriptor_->containing_type()) + "<T>";
583 variables["this_message"] = "reinterpret_cast<T*>(this)->"; 648 variables["this_message"] = "reinterpret_cast<T*>(this)->";
584 // Const message access is needed for the dependent getter. 649 // Const message access is needed for the dependent getter.
585 variables["this_const_message"] = "reinterpret_cast<const T*>(this)->"; 650 variables["this_const_message"] = "reinterpret_cast<const T*>(this)->";
586 variables["tmpl"] = "template <class T>\n"; 651 variables["tmpl"] = "template <class T>\n";
587 variables["field_member"] = variables["this_message"] + 652 variables["field_member"] = variables["this_message"] +
588 variables["oneof_prefix"] + variables["name"] + 653 variables["oneof_prefix"] + variables["name"] +
589 "_"; 654 "_";
590 InternalGenerateInlineAccessorDefinitions(variables, printer); 655 InternalGenerateInlineAccessorDefinitions(variables, printer);
591 } 656 }
592 657
593 void MessageOneofFieldGenerator:: 658 void MessageOneofFieldGenerator::
594 GenerateInlineAccessorDefinitions(io::Printer* printer, 659 GenerateInlineAccessorDefinitions(io::Printer* printer,
595 bool is_inline) const { 660 bool is_inline) const {
596 if (dependent_base_) { 661 if (dependent_base_) {
597 return; 662 return;
598 } 663 }
599 map<string, string> variables(variables_); 664 std::map<string, string> variables(variables_);
600 variables["inline"] = is_inline ? "inline " : ""; 665 variables["inline"] = is_inline ? "inline " : "";
601 variables["dependent_classname"] = variables["classname"]; 666 variables["dependent_classname"] = variables["classname"];
602 variables["this_message"] = ""; 667 variables["this_message"] = "";
603 variables["this_const_message"] = ""; 668 variables["this_const_message"] = "";
604 variables["tmpl"] = ""; 669 variables["tmpl"] = "";
605 variables["field_member"] = 670 variables["field_member"] =
606 variables["oneof_prefix"] + variables["name"] + "_"; 671 variables["oneof_prefix"] + variables["name"] + "_";
607 variables["dependent_type"] = variables["type"]; 672 variables["dependent_type"] = variables["type"];
608 InternalGenerateInlineAccessorDefinitions(variables, printer); 673 InternalGenerateInlineAccessorDefinitions(variables, printer);
609 } 674 }
610 675
611 void MessageOneofFieldGenerator:: 676 void MessageOneofFieldGenerator::
612 GenerateNonInlineAccessorDefinitions(io::Printer* printer) const { 677 GenerateNonInlineAccessorDefinitions(io::Printer* printer) const {
613 map<string, string> variables(variables_); 678 std::map<string, string> variables(variables_);
614 variables["field_member"] = 679 variables["field_member"] =
615 variables["oneof_prefix"] + variables["name"] + "_"; 680 variables["oneof_prefix"] + variables["name"] + "_";
616 681
617 //printer->Print(variables, 682 //printer->Print(variables,
618 } 683 }
619 684
620 void MessageOneofFieldGenerator:: 685 void MessageOneofFieldGenerator::InternalGenerateInlineAccessorDefinitions(
621 InternalGenerateInlineAccessorDefinitions(const map<string, string>& variables, 686 const std::map<string, string>& variables, io::Printer* printer) const {
622 io::Printer* printer) const {
623 printer->Print(variables, 687 printer->Print(variables,
624 "$tmpl$" 688 "$tmpl$"
625 "$inline$ " 689 "$inline$ "
626 "const $type$& $dependent_classname$::$name$() const {\n" 690 "const $type$& $dependent_classname$::$name$() const {\n"
627 " // @@protoc_insertion_point(field_get:$full_name$)\n" 691 " // @@protoc_insertion_point(field_get:$full_name$)\n"
628 " return $this_const_message$has_$name$()\n" 692 " return $this_const_message$has_$name$()\n"
629 " ? *$this_const_message$$oneof_prefix$$name$_\n" 693 " ? *$this_const_message$$oneof_prefix$$name$_\n"
630 " : $dependent_type$::default_instance();\n" 694 " : $dependent_type$::default_instance();\n"
631 "}\n"); 695 "}\n");
632 696
(...skipping 23 matching lines...) Expand all
656 "}\n" 720 "}\n"
657 "$tmpl$" 721 "$tmpl$"
658 "$inline$" 722 "$inline$"
659 "$type$* $dependent_classname$::$release_name$() {\n" 723 "$type$* $dependent_classname$::$release_name$() {\n"
660 " // @@protoc_insertion_point(field_release:$full_name$)\n" 724 " // @@protoc_insertion_point(field_release:$full_name$)\n"
661 " if ($this_message$has_$name$()) {\n" 725 " if ($this_message$has_$name$()) {\n"
662 " $this_message$clear_has_$oneof_name$();\n" 726 " $this_message$clear_has_$oneof_name$();\n"
663 " if ($this_message$GetArenaNoVirtual() != NULL) {\n" 727 " if ($this_message$GetArenaNoVirtual() != NULL) {\n"
664 // N.B.: safe to use the underlying field pointer here because we are sure 728 // N.B.: safe to use the underlying field pointer here because we are sure
665 // that it is non-NULL (because has_$name$() returned true). 729 // that it is non-NULL (because has_$name$() returned true).
666 " $dependent_typename$* temp = new $dependent_typename$;\n" 730 " $dependent_typename$* temp = "
667 " temp->MergeFrom(*$field_member$);\n" 731 "new $dependent_typename$(*$field_member$);\n"
668 " $field_member$ = NULL;\n" 732 " $field_member$ = NULL;\n"
669 " return temp;\n" 733 " return temp;\n"
670 " } else {\n" 734 " } else {\n"
671 " $dependent_typename$* temp = $field_member$;\n" 735 " $dependent_typename$* temp = $field_member$;\n"
672 " $field_member$ = NULL;\n" 736 " $field_member$ = NULL;\n"
673 " return temp;\n" 737 " return temp;\n"
674 " }\n" 738 " }\n"
675 " } else {\n" 739 " } else {\n"
676 " return NULL;\n" 740 " return NULL;\n"
677 " }\n" 741 " }\n"
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
782 " $this_message$set_has_$name$();\n" 846 " $this_message$set_has_$name$();\n"
783 " $field_member$ = $name$;\n" 847 " $field_member$ = $name$;\n"
784 " }\n" 848 " }\n"
785 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" 849 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
786 "}\n"); 850 "}\n");
787 } 851 }
788 } 852 }
789 853
790 void MessageOneofFieldGenerator:: 854 void MessageOneofFieldGenerator::
791 GenerateClearingCode(io::Printer* printer) const { 855 GenerateClearingCode(io::Printer* printer) const {
792 map<string, string> variables(variables_); 856 std::map<string, string> variables(variables_);
793 variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : ""; 857 variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : "";
794 if (SupportsArenas(descriptor_)) { 858 if (SupportsArenas(descriptor_)) {
795 printer->Print(variables, 859 printer->Print(variables,
796 "if ($this_message$GetArenaNoVirtual() == NULL) {\n" 860 "if ($this_message$GetArenaNoVirtual() == NULL) {\n"
797 " delete $this_message$$oneof_prefix$$name$_;\n" 861 " delete $this_message$$oneof_prefix$$name$_;\n"
798 "}\n"); 862 "}\n");
799 } else { 863 } else {
800 printer->Print(variables, 864 printer->Print(variables,
801 "delete $this_message$$oneof_prefix$$name$_;\n"); 865 "delete $this_message$$oneof_prefix$$name$_;\n");
802 } 866 }
803 } 867 }
804 868
805 void MessageOneofFieldGenerator:: 869 void MessageOneofFieldGenerator::
870 GenerateMessageClearingCode(io::Printer* printer) const {
871 GenerateClearingCode(printer);
872 }
873
874 void MessageOneofFieldGenerator::
806 GenerateSwappingCode(io::Printer* printer) const { 875 GenerateSwappingCode(io::Printer* printer) const {
807 // Don't print any swapping code. Swapping the union will swap this field. 876 // Don't print any swapping code. Swapping the union will swap this field.
808 } 877 }
809 878
810 void MessageOneofFieldGenerator:: 879 void MessageOneofFieldGenerator::
880 GenerateDestructorCode(io::Printer* printer) const {
881 // We inherit from MessageFieldGenerator, so we need to override the default
882 // behavior.
883 }
884
885 void MessageOneofFieldGenerator::
811 GenerateConstructorCode(io::Printer* printer) const { 886 GenerateConstructorCode(io::Printer* printer) const {
812 // Don't print any constructor code. The field is in a union. We allocate 887 // Don't print any constructor code. The field is in a union. We allocate
813 // space only when this field is used. 888 // space only when this field is used.
814 } 889 }
815 890
816 // =================================================================== 891 // ===================================================================
817 892
818 RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator( 893 RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator(
819 const FieldDescriptor* descriptor, const Options& options) 894 const FieldDescriptor* descriptor, const Options& options)
820 : FieldGenerator(options), 895 : FieldGenerator(options),
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
872 "$deprecated_attr$const ::google::protobuf::RepeatedPtrField< $type$ >&\n" 947 "$deprecated_attr$const ::google::protobuf::RepeatedPtrField< $type$ >&\n"
873 " $name$() const;\n"); 948 " $name$() const;\n");
874 } 949 }
875 } 950 }
876 951
877 void RepeatedMessageFieldGenerator:: 952 void RepeatedMessageFieldGenerator::
878 GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const { 953 GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const {
879 if (!dependent_field_) { 954 if (!dependent_field_) {
880 return; 955 return;
881 } 956 }
882 map<string, string> variables(variables_); 957 std::map<string, string> variables(variables_);
883 // For the CRTP base class, all mutation methods are dependent, and so 958 // For the CRTP base class, all mutation methods are dependent, and so
884 // they must be in the header. 959 // they must be in the header.
885 variables["dependent_classname"] = 960 variables["dependent_classname"] =
886 DependentBaseClassTemplateName(descriptor_->containing_type()) + "<T>"; 961 DependentBaseClassTemplateName(descriptor_->containing_type()) + "<T>";
887 variables["this_message"] = DependentBaseDownCast(); 962 variables["this_message"] = DependentBaseDownCast();
888 variables["this_const_message"] = DependentBaseConstDownCast(); 963 variables["this_const_message"] = DependentBaseConstDownCast();
889 964
890 if (dependent_getter_) { 965 if (dependent_getter_) {
891 printer->Print(variables, 966 printer->Print(variables,
892 "template <class T>\n" 967 "template <class T>\n"
(...skipping 10 matching lines...) Expand all
903 // TODO(dlj): move insertion points 978 // TODO(dlj): move insertion points
904 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" 979 " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
905 " return $this_message$$name$_.Mutable(index);\n" 980 " return $this_message$$name$_.Mutable(index);\n"
906 "}\n" 981 "}\n"
907 "template <class T>\n" 982 "template <class T>\n"
908 "inline $type$* $dependent_classname$::add_$name$() {\n" 983 "inline $type$* $dependent_classname$::add_$name$() {\n"
909 " // @@protoc_insertion_point(field_add:$full_name$)\n" 984 " // @@protoc_insertion_point(field_add:$full_name$)\n"
910 " return $this_message$$name$_.Add();\n" 985 " return $this_message$$name$_.Add();\n"
911 "}\n"); 986 "}\n");
912 987
913
914 if (dependent_getter_) { 988 if (dependent_getter_) {
915 printer->Print(variables, 989 printer->Print(variables,
916 "template <class T>\n" 990 "template <class T>\n"
917 "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n" 991 "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n"
918 "$dependent_classname$::$name$() const {\n" 992 "$dependent_classname$::$name$() const {\n"
919 " // @@protoc_insertion_point(field_list:$full_name$)\n" 993 " // @@protoc_insertion_point(field_list:$full_name$)\n"
920 " return $this_const_message$$name$_;\n" 994 " return $this_const_message$$name$_;\n"
921 "}\n"); 995 "}\n");
922 } 996 }
923 997
924 // Generate mutable access to the entire list: 998 // Generate mutable access to the entire list:
925 printer->Print(variables, 999 printer->Print(variables,
926 "template <class T>\n" 1000 "template <class T>\n"
927 "inline ::google::protobuf::RepeatedPtrField< $type$ >*\n" 1001 "inline ::google::protobuf::RepeatedPtrField< $type$ >*\n"
928 "$dependent_classname$::mutable_$name$() {\n" 1002 "$dependent_classname$::mutable_$name$() {\n"
929 " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" 1003 " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n"
930 " return &$this_message$$name$_;\n" 1004 " return &$this_message$$name$_;\n"
931 "}\n"); 1005 "}\n");
932 } 1006 }
933 1007
934 void RepeatedMessageFieldGenerator:: 1008 void RepeatedMessageFieldGenerator::
935 GenerateInlineAccessorDefinitions(io::Printer* printer, 1009 GenerateInlineAccessorDefinitions(io::Printer* printer,
936 bool is_inline) const { 1010 bool is_inline) const {
937 map<string, string> variables(variables_); 1011 std::map<string, string> variables(variables_);
938 variables["inline"] = is_inline ? "inline " : ""; 1012 variables["inline"] = is_inline ? "inline " : "";
939 1013
940 if (!dependent_getter_) { 1014 if (!dependent_getter_) {
941 printer->Print(variables, 1015 printer->Print(variables,
942 "$inline$" 1016 "$inline$"
943 "const $type$& $classname$::$name$(int index) const {\n" 1017 "const $type$& $classname$::$name$(int index) const {\n"
944 " // @@protoc_insertion_point(field_get:$full_name$)\n" 1018 " // @@protoc_insertion_point(field_get:$full_name$)\n"
945 " return $name$_.$cppget$(index);\n" 1019 " return $name$_.$cppget$(index);\n"
946 "}\n"); 1020 "}\n");
947 } 1021 }
(...skipping 29 matching lines...) Expand all
977 "const ::google::protobuf::RepeatedPtrField< $type$ >&\n" 1051 "const ::google::protobuf::RepeatedPtrField< $type$ >&\n"
978 "$classname$::$name$() const {\n" 1052 "$classname$::$name$() const {\n"
979 " // @@protoc_insertion_point(field_list:$full_name$)\n" 1053 " // @@protoc_insertion_point(field_list:$full_name$)\n"
980 " return $name$_;\n" 1054 " return $name$_;\n"
981 "}\n"); 1055 "}\n");
982 } 1056 }
983 } 1057 }
984 1058
985 void RepeatedMessageFieldGenerator:: 1059 void RepeatedMessageFieldGenerator::
986 GenerateClearingCode(io::Printer* printer) const { 1060 GenerateClearingCode(io::Printer* printer) const {
987 map<string, string> variables(variables_); 1061 std::map<string, string> variables(variables_);
988 variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : ""; 1062 variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : "";
989 printer->Print(variables, "$this_message$$name$_.Clear();\n"); 1063 printer->Print(variables, "$this_message$$name$_.Clear();\n");
990 } 1064 }
991 1065
992 void RepeatedMessageFieldGenerator:: 1066 void RepeatedMessageFieldGenerator::
993 GenerateMergingCode(io::Printer* printer) const { 1067 GenerateMergingCode(io::Printer* printer) const {
994 printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n"); 1068 printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
995 } 1069 }
996 1070
997 void RepeatedMessageFieldGenerator:: 1071 void RepeatedMessageFieldGenerator::
(...skipping 28 matching lines...) Expand all
1026 " ::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n" 1100 " ::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n"
1027 " $number$, this->$name$(i), output);\n" 1101 " $number$, this->$name$(i), output);\n"
1028 "}\n"); 1102 "}\n");
1029 } 1103 }
1030 1104
1031 void RepeatedMessageFieldGenerator:: 1105 void RepeatedMessageFieldGenerator::
1032 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { 1106 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
1033 printer->Print(variables_, 1107 printer->Print(variables_,
1034 "for (unsigned int i = 0, n = this->$name$_size(); i < n; i++) {\n" 1108 "for (unsigned int i = 0, n = this->$name$_size(); i < n; i++) {\n"
1035 " target = ::google::protobuf::internal::WireFormatLite::\n" 1109 " target = ::google::protobuf::internal::WireFormatLite::\n"
1036 " Write$declared_type$NoVirtualToArray(\n" 1110 " InternalWrite$declared_type$NoVirtualToArray(\n"
1037 " $number$, this->$name$(i), target);\n" 1111 " $number$, this->$name$(i), false, target);\n"
1038 "}\n"); 1112 "}\n");
1039 } 1113 }
1040 1114
1041 void RepeatedMessageFieldGenerator:: 1115 void RepeatedMessageFieldGenerator::
1042 GenerateByteSize(io::Printer* printer) const { 1116 GenerateByteSize(io::Printer* printer) const {
1043 printer->Print(variables_, 1117 printer->Print(variables_,
1044 "total_size += $tag_size$ * this->$name$_size();\n" 1118 "{\n"
1045 "for (int i = 0; i < this->$name$_size(); i++) {\n" 1119 " unsigned int count = this->$name$_size();\n");
1120 printer->Indent();
1121 printer->Print(variables_,
1122 "total_size += $tag_size$UL * count;\n"
1123 "for (unsigned int i = 0; i < count; i++) {\n"
1046 " total_size +=\n" 1124 " total_size +=\n"
1047 " ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirt ual(\n" 1125 " ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirt ual(\n"
1048 " this->$name$(i));\n" 1126 " this->$name$(i));\n"
1049 "}\n"); 1127 "}\n");
1128 printer->Outdent();
1129 printer->Print("}\n");
1050 } 1130 }
1051 1131
1052 } // namespace cpp 1132 } // namespace cpp
1053 } // namespace compiler 1133 } // namespace compiler
1054 } // namespace protobuf 1134 } // namespace protobuf
1055 } // namespace google 1135 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698