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

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

Issue 2600753002: Reverts third_party/protobuf: Update to HEAD (f52e188fe4) (Closed)
Patch Set: Created 3 years, 12 months 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 std::map<string, string>* variables, 48 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$(*$name$_);\n" 164 " $type$* temp = new $type$;\n"
165 " temp->MergeFrom(*$name$_);\n"
165 " $name$_ = NULL;\n" 166 " $name$_ = NULL;\n"
166 " return temp;\n" 167 " return temp;\n"
167 " }\n" 168 " }\n"
168 "}\n" 169 "}\n"
169 "$type$* $classname$::unsafe_arena_release_$name$() {\n" 170 "$type$* $classname$::unsafe_arena_release_$name$() {\n"
170 " // @@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" 171 " // @@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n"
171 " $clear_hasbit$\n" 172 " $clear_hasbit$\n"
172 " $type$* temp = $name$_;\n" 173 " $type$* temp = $name$_;\n"
173 " $name$_ = NULL;\n" 174 " $name$_ = NULL;\n"
174 " return temp;\n" 175 " return temp;\n"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 "}\n"); 212 "}\n");
212 } 213 }
213 } 214 }
214 215
215 void MessageFieldGenerator:: 216 void MessageFieldGenerator::
216 GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const { 217 GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const {
217 if (!dependent_field_) { 218 if (!dependent_field_) {
218 return; 219 return;
219 } 220 }
220 221
221 std::map<string, string> variables(variables_); 222 map<string, string> variables(variables_);
222 // For the CRTP base class, all mutation methods are dependent, and so 223 // For the CRTP base class, all mutation methods are dependent, and so
223 // they must be in the header. 224 // they must be in the header.
224 variables["dependent_classname"] = 225 variables["dependent_classname"] =
225 DependentBaseClassTemplateName(descriptor_->containing_type()) + "<T>"; 226 DependentBaseClassTemplateName(descriptor_->containing_type()) + "<T>";
226 variables["this_message"] = DependentBaseDownCast(); 227 variables["this_message"] = DependentBaseDownCast();
227 if (!variables["set_hasbit"].empty()) { 228 if (!variables["set_hasbit"].empty()) {
228 variables["set_hasbit"] = 229 variables["set_hasbit"] =
229 variables["this_message"] + variables["set_hasbit"]; 230 variables["this_message"] + variables["set_hasbit"];
230 } 231 }
231 if (!variables["clear_hasbit"].empty()) { 232 if (!variables["clear_hasbit"].empty()) {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 " $clear_hasbit$\n" 339 " $clear_hasbit$\n"
339 " }\n" 340 " }\n"
340 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" 341 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
341 "}\n"); 342 "}\n");
342 } 343 }
343 } 344 }
344 345
345 void MessageFieldGenerator:: 346 void MessageFieldGenerator::
346 GenerateInlineAccessorDefinitions(io::Printer* printer, 347 GenerateInlineAccessorDefinitions(io::Printer* printer,
347 bool is_inline) const { 348 bool is_inline) const {
349 map<string, string> variables(variables_);
350 variables["inline"] = is_inline ? "inline " : "";
351 printer->Print(variables,
352 "$inline$const $type$& $classname$::$name$() const {\n"
353 " // @@protoc_insertion_point(field_get:$full_name$)\n");
354
355 PrintHandlingOptionalStaticInitializers(
356 variables, descriptor_->file(), options_, printer,
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
348 if (dependent_field_) { 363 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; 364 return;
361 } 365 }
362 366
363 std::map<string, string> variables(variables_);
364 variables["inline"] = is_inline ? "inline " : "";
365 printer->Print(variables,
366 "$inline$const $type$& $classname$::$name$() const {\n"
367 " // @@protoc_insertion_point(field_get:$full_name$)\n"
368 " return $name$_ != NULL ? *$name$_\n"
369 " : *$type$::internal_default_instance();\n"
370 "}\n");
371
372 if (SupportsArenas(descriptor_)) { 367 if (SupportsArenas(descriptor_)) {
373 printer->Print(variables, 368 printer->Print(variables,
374 "$inline$" 369 "$inline$"
375 "$type$* $classname$::mutable_$name$() {\n" 370 "$type$* $classname$::mutable_$name$() {\n"
376 " $set_hasbit$\n" 371 " $set_hasbit$\n"
377 " if ($name$_ == NULL) {\n" 372 " if ($name$_ == NULL) {\n"
378 " _slow_mutable_$name$();\n" 373 " _slow_mutable_$name$();\n"
379 " }\n" 374 " }\n"
380 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" 375 " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
381 " return $name$_;\n" 376 " return $name$_;\n"
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 " } else {\n" 457 " } else {\n"
463 " $clear_hasbit$\n" 458 " $clear_hasbit$\n"
464 " }\n" 459 " }\n"
465 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" 460 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
466 "}\n"); 461 "}\n");
467 } 462 }
468 } 463 }
469 464
470 void MessageFieldGenerator:: 465 void MessageFieldGenerator::
471 GenerateClearingCode(io::Printer* printer) const { 466 GenerateClearingCode(io::Printer* printer) const {
472 std::map<string, string> variables(variables_); 467 map<string, string> variables(variables_);
473 variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : ""; 468 variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : "";
474 if (!HasFieldPresence(descriptor_->file())) { 469 if (!HasFieldPresence(descriptor_->file())) {
475 // If we don't have has-bits, message presence is indicated only by ptr != 470 // If we don't have has-bits, message presence is indicated only by ptr !=
476 // NULL. Thus on clear, we need to delete the object. 471 // NULL. Thus on clear, we need to delete the object.
477 printer->Print(variables, 472 printer->Print(variables,
478 "if ($this_message$GetArenaNoVirtual() == NULL && " 473 "if ($this_message$GetArenaNoVirtual() == NULL && "
479 "$this_message$$name$_ != NULL) delete $this_message$$name$_;\n" 474 "$this_message$$name$_ != NULL) delete $this_message$$name$_;\n"
480 "$this_message$$name$_ = NULL;\n"); 475 "$this_message$$name$_ = NULL;\n");
481 } else { 476 } else {
482 printer->Print(variables, 477 printer->Print(variables,
483 "if ($this_message$$name$_ != NULL) $this_message$$name$_->" 478 "if ($this_message$$name$_ != NULL) $this_message$$name$_->"
484 "$dependent_type$::Clear();\n"); 479 "$dependent_type$::Clear();\n");
485 } 480 }
486 } 481 }
487 482
488 void MessageFieldGenerator:: 483 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::
509 GenerateMergingCode(io::Printer* printer) const { 484 GenerateMergingCode(io::Printer* printer) const {
510 printer->Print(variables_, 485 printer->Print(variables_,
511 "mutable_$name$()->$type$::MergeFrom(from.$name$());\n"); 486 "mutable_$name$()->$type$::MergeFrom(from.$name$());\n");
512 } 487 }
513 488
514 void MessageFieldGenerator:: 489 void MessageFieldGenerator::
515 GenerateSwappingCode(io::Printer* printer) const { 490 GenerateSwappingCode(io::Printer* printer) const {
516 printer->Print(variables_, "std::swap($name$_, other->$name$_);\n"); 491 printer->Print(variables_, "std::swap($name$_, other->$name$_);\n");
517 } 492 }
518 493
519 void MessageFieldGenerator:: 494 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::
531 GenerateConstructorCode(io::Printer* printer) const { 495 GenerateConstructorCode(io::Printer* printer) const {
532 printer->Print(variables_, "$name$_ = NULL;\n"); 496 printer->Print(variables_, "$name$_ = NULL;\n");
533 } 497 }
534 498
535 void MessageFieldGenerator:: 499 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::
565 GenerateMergeFromCodedStream(io::Printer* printer) const { 500 GenerateMergeFromCodedStream(io::Printer* printer) const {
566 if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) { 501 if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) {
567 printer->Print(variables_, 502 printer->Print(variables_,
568 "DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(\n " 503 "DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(\n "
569 " input, mutable_$name$()));\n"); 504 " input, mutable_$name$()));\n");
570 } else { 505 } else {
571 printer->Print(variables_, 506 printer->Print(variables_,
572 "DO_(::google::protobuf::internal::WireFormatLite::ReadGroupNoVirtual(\n" 507 "DO_(::google::protobuf::internal::WireFormatLite::ReadGroupNoVirtual(\n"
573 " $number$, input, mutable_$name$()));\n"); 508 " $number$, input, mutable_$name$()));\n");
574 } 509 }
575 } 510 }
576 511
577 void MessageFieldGenerator:: 512 void MessageFieldGenerator::
578 GenerateSerializeWithCachedSizes(io::Printer* printer) const { 513 GenerateSerializeWithCachedSizes(io::Printer* printer) const {
579 printer->Print(variables_, 514 printer->Print(variables_,
580 "::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n" 515 "::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n"
581 " $number$, *$non_null_ptr_to_name$, output);\n"); 516 " $number$, *$non_null_ptr_to_name$, output);\n");
582 } 517 }
583 518
584 void MessageFieldGenerator:: 519 void MessageFieldGenerator::
585 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { 520 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
586 printer->Print(variables_, 521 printer->Print(variables_,
587 "target = ::google::protobuf::internal::WireFormatLite::\n" 522 "target = ::google::protobuf::internal::WireFormatLite::\n"
588 " InternalWrite$declared_type$NoVirtualToArray(\n" 523 " Write$declared_type$NoVirtualToArray(\n"
589 " $number$, *$non_null_ptr_to_name$, false, target);\n"); 524 " $number$, *$non_null_ptr_to_name$, target);\n");
590 } 525 }
591 526
592 void MessageFieldGenerator:: 527 void MessageFieldGenerator::
593 GenerateByteSize(io::Printer* printer) const { 528 GenerateByteSize(io::Printer* printer) const {
594 printer->Print(variables_, 529 printer->Print(variables_,
595 "total_size += $tag_size$ +\n" 530 "total_size += $tag_size$ +\n"
596 " ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirtua l(\n" 531 " ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirtua l(\n"
597 " *$non_null_ptr_to_name$);\n"); 532 " *$non_null_ptr_to_name$);\n");
598 } 533 }
599 534
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 "$deprecated_attr$const $type$& $name$() const;\n"); 569 "$deprecated_attr$const $type$& $name$() const;\n");
635 } 570 }
636 571
637 void MessageOneofFieldGenerator:: 572 void MessageOneofFieldGenerator::
638 GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const { 573 GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const {
639 // For the CRTP base class, all mutation methods are dependent, and so 574 // For the CRTP base class, all mutation methods are dependent, and so
640 // they must be in the header. 575 // they must be in the header.
641 if (!dependent_base_) { 576 if (!dependent_base_) {
642 return; 577 return;
643 } 578 }
644 std::map<string, string> variables(variables_); 579 map<string, string> variables(variables_);
645 variables["inline"] = "inline "; 580 variables["inline"] = "inline ";
646 variables["dependent_classname"] = 581 variables["dependent_classname"] =
647 DependentBaseClassTemplateName(descriptor_->containing_type()) + "<T>"; 582 DependentBaseClassTemplateName(descriptor_->containing_type()) + "<T>";
648 variables["this_message"] = "reinterpret_cast<T*>(this)->"; 583 variables["this_message"] = "reinterpret_cast<T*>(this)->";
649 // Const message access is needed for the dependent getter. 584 // Const message access is needed for the dependent getter.
650 variables["this_const_message"] = "reinterpret_cast<const T*>(this)->"; 585 variables["this_const_message"] = "reinterpret_cast<const T*>(this)->";
651 variables["tmpl"] = "template <class T>\n"; 586 variables["tmpl"] = "template <class T>\n";
652 variables["field_member"] = variables["this_message"] + 587 variables["field_member"] = variables["this_message"] +
653 variables["oneof_prefix"] + variables["name"] + 588 variables["oneof_prefix"] + variables["name"] +
654 "_"; 589 "_";
655 InternalGenerateInlineAccessorDefinitions(variables, printer); 590 InternalGenerateInlineAccessorDefinitions(variables, printer);
656 } 591 }
657 592
658 void MessageOneofFieldGenerator:: 593 void MessageOneofFieldGenerator::
659 GenerateInlineAccessorDefinitions(io::Printer* printer, 594 GenerateInlineAccessorDefinitions(io::Printer* printer,
660 bool is_inline) const { 595 bool is_inline) const {
661 if (dependent_base_) { 596 if (dependent_base_) {
662 return; 597 return;
663 } 598 }
664 std::map<string, string> variables(variables_); 599 map<string, string> variables(variables_);
665 variables["inline"] = is_inline ? "inline " : ""; 600 variables["inline"] = is_inline ? "inline " : "";
666 variables["dependent_classname"] = variables["classname"]; 601 variables["dependent_classname"] = variables["classname"];
667 variables["this_message"] = ""; 602 variables["this_message"] = "";
668 variables["this_const_message"] = ""; 603 variables["this_const_message"] = "";
669 variables["tmpl"] = ""; 604 variables["tmpl"] = "";
670 variables["field_member"] = 605 variables["field_member"] =
671 variables["oneof_prefix"] + variables["name"] + "_"; 606 variables["oneof_prefix"] + variables["name"] + "_";
672 variables["dependent_type"] = variables["type"]; 607 variables["dependent_type"] = variables["type"];
673 InternalGenerateInlineAccessorDefinitions(variables, printer); 608 InternalGenerateInlineAccessorDefinitions(variables, printer);
674 } 609 }
675 610
676 void MessageOneofFieldGenerator:: 611 void MessageOneofFieldGenerator::
677 GenerateNonInlineAccessorDefinitions(io::Printer* printer) const { 612 GenerateNonInlineAccessorDefinitions(io::Printer* printer) const {
678 std::map<string, string> variables(variables_); 613 map<string, string> variables(variables_);
679 variables["field_member"] = 614 variables["field_member"] =
680 variables["oneof_prefix"] + variables["name"] + "_"; 615 variables["oneof_prefix"] + variables["name"] + "_";
681 616
682 //printer->Print(variables, 617 //printer->Print(variables,
683 } 618 }
684 619
685 void MessageOneofFieldGenerator::InternalGenerateInlineAccessorDefinitions( 620 void MessageOneofFieldGenerator::
686 const std::map<string, string>& variables, io::Printer* printer) const { 621 InternalGenerateInlineAccessorDefinitions(const map<string, string>& variables,
622 io::Printer* printer) const {
687 printer->Print(variables, 623 printer->Print(variables,
688 "$tmpl$" 624 "$tmpl$"
689 "$inline$ " 625 "$inline$ "
690 "const $type$& $dependent_classname$::$name$() const {\n" 626 "const $type$& $dependent_classname$::$name$() const {\n"
691 " // @@protoc_insertion_point(field_get:$full_name$)\n" 627 " // @@protoc_insertion_point(field_get:$full_name$)\n"
692 " return $this_const_message$has_$name$()\n" 628 " return $this_const_message$has_$name$()\n"
693 " ? *$this_const_message$$oneof_prefix$$name$_\n" 629 " ? *$this_const_message$$oneof_prefix$$name$_\n"
694 " : $dependent_type$::default_instance();\n" 630 " : $dependent_type$::default_instance();\n"
695 "}\n"); 631 "}\n");
696 632
(...skipping 23 matching lines...) Expand all
720 "}\n" 656 "}\n"
721 "$tmpl$" 657 "$tmpl$"
722 "$inline$" 658 "$inline$"
723 "$type$* $dependent_classname$::$release_name$() {\n" 659 "$type$* $dependent_classname$::$release_name$() {\n"
724 " // @@protoc_insertion_point(field_release:$full_name$)\n" 660 " // @@protoc_insertion_point(field_release:$full_name$)\n"
725 " if ($this_message$has_$name$()) {\n" 661 " if ($this_message$has_$name$()) {\n"
726 " $this_message$clear_has_$oneof_name$();\n" 662 " $this_message$clear_has_$oneof_name$();\n"
727 " if ($this_message$GetArenaNoVirtual() != NULL) {\n" 663 " if ($this_message$GetArenaNoVirtual() != NULL) {\n"
728 // N.B.: safe to use the underlying field pointer here because we are sure 664 // N.B.: safe to use the underlying field pointer here because we are sure
729 // that it is non-NULL (because has_$name$() returned true). 665 // that it is non-NULL (because has_$name$() returned true).
730 " $dependent_typename$* temp = " 666 " $dependent_typename$* temp = new $dependent_typename$;\n"
731 "new $dependent_typename$(*$field_member$);\n" 667 " temp->MergeFrom(*$field_member$);\n"
732 " $field_member$ = NULL;\n" 668 " $field_member$ = NULL;\n"
733 " return temp;\n" 669 " return temp;\n"
734 " } else {\n" 670 " } else {\n"
735 " $dependent_typename$* temp = $field_member$;\n" 671 " $dependent_typename$* temp = $field_member$;\n"
736 " $field_member$ = NULL;\n" 672 " $field_member$ = NULL;\n"
737 " return temp;\n" 673 " return temp;\n"
738 " }\n" 674 " }\n"
739 " } else {\n" 675 " } else {\n"
740 " return NULL;\n" 676 " return NULL;\n"
741 " }\n" 677 " }\n"
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
846 " $this_message$set_has_$name$();\n" 782 " $this_message$set_has_$name$();\n"
847 " $field_member$ = $name$;\n" 783 " $field_member$ = $name$;\n"
848 " }\n" 784 " }\n"
849 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" 785 " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
850 "}\n"); 786 "}\n");
851 } 787 }
852 } 788 }
853 789
854 void MessageOneofFieldGenerator:: 790 void MessageOneofFieldGenerator::
855 GenerateClearingCode(io::Printer* printer) const { 791 GenerateClearingCode(io::Printer* printer) const {
856 std::map<string, string> variables(variables_); 792 map<string, string> variables(variables_);
857 variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : ""; 793 variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : "";
858 if (SupportsArenas(descriptor_)) { 794 if (SupportsArenas(descriptor_)) {
859 printer->Print(variables, 795 printer->Print(variables,
860 "if ($this_message$GetArenaNoVirtual() == NULL) {\n" 796 "if ($this_message$GetArenaNoVirtual() == NULL) {\n"
861 " delete $this_message$$oneof_prefix$$name$_;\n" 797 " delete $this_message$$oneof_prefix$$name$_;\n"
862 "}\n"); 798 "}\n");
863 } else { 799 } else {
864 printer->Print(variables, 800 printer->Print(variables,
865 "delete $this_message$$oneof_prefix$$name$_;\n"); 801 "delete $this_message$$oneof_prefix$$name$_;\n");
866 } 802 }
867 } 803 }
868 804
869 void MessageOneofFieldGenerator:: 805 void MessageOneofFieldGenerator::
870 GenerateMessageClearingCode(io::Printer* printer) const {
871 GenerateClearingCode(printer);
872 }
873
874 void MessageOneofFieldGenerator::
875 GenerateSwappingCode(io::Printer* printer) const { 806 GenerateSwappingCode(io::Printer* printer) const {
876 // Don't print any swapping code. Swapping the union will swap this field. 807 // Don't print any swapping code. Swapping the union will swap this field.
877 } 808 }
878 809
879 void MessageOneofFieldGenerator:: 810 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::
886 GenerateConstructorCode(io::Printer* printer) const { 811 GenerateConstructorCode(io::Printer* printer) const {
887 // Don't print any constructor code. The field is in a union. We allocate 812 // Don't print any constructor code. The field is in a union. We allocate
888 // space only when this field is used. 813 // space only when this field is used.
889 } 814 }
890 815
891 // =================================================================== 816 // ===================================================================
892 817
893 RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator( 818 RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator(
894 const FieldDescriptor* descriptor, const Options& options) 819 const FieldDescriptor* descriptor, const Options& options)
895 : FieldGenerator(options), 820 : FieldGenerator(options),
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
947 "$deprecated_attr$const ::google::protobuf::RepeatedPtrField< $type$ >&\n" 872 "$deprecated_attr$const ::google::protobuf::RepeatedPtrField< $type$ >&\n"
948 " $name$() const;\n"); 873 " $name$() const;\n");
949 } 874 }
950 } 875 }
951 876
952 void RepeatedMessageFieldGenerator:: 877 void RepeatedMessageFieldGenerator::
953 GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const { 878 GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const {
954 if (!dependent_field_) { 879 if (!dependent_field_) {
955 return; 880 return;
956 } 881 }
957 std::map<string, string> variables(variables_); 882 map<string, string> variables(variables_);
958 // For the CRTP base class, all mutation methods are dependent, and so 883 // For the CRTP base class, all mutation methods are dependent, and so
959 // they must be in the header. 884 // they must be in the header.
960 variables["dependent_classname"] = 885 variables["dependent_classname"] =
961 DependentBaseClassTemplateName(descriptor_->containing_type()) + "<T>"; 886 DependentBaseClassTemplateName(descriptor_->containing_type()) + "<T>";
962 variables["this_message"] = DependentBaseDownCast(); 887 variables["this_message"] = DependentBaseDownCast();
963 variables["this_const_message"] = DependentBaseConstDownCast(); 888 variables["this_const_message"] = DependentBaseConstDownCast();
964 889
965 if (dependent_getter_) { 890 if (dependent_getter_) {
966 printer->Print(variables, 891 printer->Print(variables,
967 "template <class T>\n" 892 "template <class T>\n"
(...skipping 10 matching lines...) Expand all
978 // TODO(dlj): move insertion points 903 // TODO(dlj): move insertion points
979 " // @@protoc_insertion_point(field_mutable:$full_name$)\n" 904 " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
980 " return $this_message$$name$_.Mutable(index);\n" 905 " return $this_message$$name$_.Mutable(index);\n"
981 "}\n" 906 "}\n"
982 "template <class T>\n" 907 "template <class T>\n"
983 "inline $type$* $dependent_classname$::add_$name$() {\n" 908 "inline $type$* $dependent_classname$::add_$name$() {\n"
984 " // @@protoc_insertion_point(field_add:$full_name$)\n" 909 " // @@protoc_insertion_point(field_add:$full_name$)\n"
985 " return $this_message$$name$_.Add();\n" 910 " return $this_message$$name$_.Add();\n"
986 "}\n"); 911 "}\n");
987 912
913
988 if (dependent_getter_) { 914 if (dependent_getter_) {
989 printer->Print(variables, 915 printer->Print(variables,
990 "template <class T>\n" 916 "template <class T>\n"
991 "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n" 917 "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n"
992 "$dependent_classname$::$name$() const {\n" 918 "$dependent_classname$::$name$() const {\n"
993 " // @@protoc_insertion_point(field_list:$full_name$)\n" 919 " // @@protoc_insertion_point(field_list:$full_name$)\n"
994 " return $this_const_message$$name$_;\n" 920 " return $this_const_message$$name$_;\n"
995 "}\n"); 921 "}\n");
996 } 922 }
997 923
998 // Generate mutable access to the entire list: 924 // Generate mutable access to the entire list:
999 printer->Print(variables, 925 printer->Print(variables,
1000 "template <class T>\n" 926 "template <class T>\n"
1001 "inline ::google::protobuf::RepeatedPtrField< $type$ >*\n" 927 "inline ::google::protobuf::RepeatedPtrField< $type$ >*\n"
1002 "$dependent_classname$::mutable_$name$() {\n" 928 "$dependent_classname$::mutable_$name$() {\n"
1003 " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" 929 " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n"
1004 " return &$this_message$$name$_;\n" 930 " return &$this_message$$name$_;\n"
1005 "}\n"); 931 "}\n");
1006 } 932 }
1007 933
1008 void RepeatedMessageFieldGenerator:: 934 void RepeatedMessageFieldGenerator::
1009 GenerateInlineAccessorDefinitions(io::Printer* printer, 935 GenerateInlineAccessorDefinitions(io::Printer* printer,
1010 bool is_inline) const { 936 bool is_inline) const {
1011 std::map<string, string> variables(variables_); 937 map<string, string> variables(variables_);
1012 variables["inline"] = is_inline ? "inline " : ""; 938 variables["inline"] = is_inline ? "inline " : "";
1013 939
1014 if (!dependent_getter_) { 940 if (!dependent_getter_) {
1015 printer->Print(variables, 941 printer->Print(variables,
1016 "$inline$" 942 "$inline$"
1017 "const $type$& $classname$::$name$(int index) const {\n" 943 "const $type$& $classname$::$name$(int index) const {\n"
1018 " // @@protoc_insertion_point(field_get:$full_name$)\n" 944 " // @@protoc_insertion_point(field_get:$full_name$)\n"
1019 " return $name$_.$cppget$(index);\n" 945 " return $name$_.$cppget$(index);\n"
1020 "}\n"); 946 "}\n");
1021 } 947 }
(...skipping 29 matching lines...) Expand all
1051 "const ::google::protobuf::RepeatedPtrField< $type$ >&\n" 977 "const ::google::protobuf::RepeatedPtrField< $type$ >&\n"
1052 "$classname$::$name$() const {\n" 978 "$classname$::$name$() const {\n"
1053 " // @@protoc_insertion_point(field_list:$full_name$)\n" 979 " // @@protoc_insertion_point(field_list:$full_name$)\n"
1054 " return $name$_;\n" 980 " return $name$_;\n"
1055 "}\n"); 981 "}\n");
1056 } 982 }
1057 } 983 }
1058 984
1059 void RepeatedMessageFieldGenerator:: 985 void RepeatedMessageFieldGenerator::
1060 GenerateClearingCode(io::Printer* printer) const { 986 GenerateClearingCode(io::Printer* printer) const {
1061 std::map<string, string> variables(variables_); 987 map<string, string> variables(variables_);
1062 variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : ""; 988 variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : "";
1063 printer->Print(variables, "$this_message$$name$_.Clear();\n"); 989 printer->Print(variables, "$this_message$$name$_.Clear();\n");
1064 } 990 }
1065 991
1066 void RepeatedMessageFieldGenerator:: 992 void RepeatedMessageFieldGenerator::
1067 GenerateMergingCode(io::Printer* printer) const { 993 GenerateMergingCode(io::Printer* printer) const {
1068 printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n"); 994 printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
1069 } 995 }
1070 996
1071 void RepeatedMessageFieldGenerator:: 997 void RepeatedMessageFieldGenerator::
(...skipping 28 matching lines...) Expand all
1100 " ::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n" 1026 " ::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n"
1101 " $number$, this->$name$(i), output);\n" 1027 " $number$, this->$name$(i), output);\n"
1102 "}\n"); 1028 "}\n");
1103 } 1029 }
1104 1030
1105 void RepeatedMessageFieldGenerator:: 1031 void RepeatedMessageFieldGenerator::
1106 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { 1032 GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
1107 printer->Print(variables_, 1033 printer->Print(variables_,
1108 "for (unsigned int i = 0, n = this->$name$_size(); i < n; i++) {\n" 1034 "for (unsigned int i = 0, n = this->$name$_size(); i < n; i++) {\n"
1109 " target = ::google::protobuf::internal::WireFormatLite::\n" 1035 " target = ::google::protobuf::internal::WireFormatLite::\n"
1110 " InternalWrite$declared_type$NoVirtualToArray(\n" 1036 " Write$declared_type$NoVirtualToArray(\n"
1111 " $number$, this->$name$(i), false, target);\n" 1037 " $number$, this->$name$(i), target);\n"
1112 "}\n"); 1038 "}\n");
1113 } 1039 }
1114 1040
1115 void RepeatedMessageFieldGenerator:: 1041 void RepeatedMessageFieldGenerator::
1116 GenerateByteSize(io::Printer* printer) const { 1042 GenerateByteSize(io::Printer* printer) const {
1117 printer->Print(variables_, 1043 printer->Print(variables_,
1118 "{\n" 1044 "total_size += $tag_size$ * this->$name$_size();\n"
1119 " unsigned int count = this->$name$_size();\n"); 1045 "for (int i = 0; i < this->$name$_size(); i++) {\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"
1124 " total_size +=\n" 1046 " total_size +=\n"
1125 " ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirt ual(\n" 1047 " ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirt ual(\n"
1126 " this->$name$(i));\n" 1048 " this->$name$(i));\n"
1127 "}\n"); 1049 "}\n");
1128 printer->Outdent();
1129 printer->Print("}\n");
1130 } 1050 }
1131 1051
1132 } // namespace cpp 1052 } // namespace cpp
1133 } // namespace compiler 1053 } // namespace compiler
1134 } // namespace protobuf 1054 } // namespace protobuf
1135 } // namespace google 1055 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698