1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "google/protobuf/compiler/csharp/csharp_repeated_message_field.h"
9
10 #include <sstream>
11
12 #include "google/protobuf/compiler/code_generator.h"
13 #include "google/protobuf/descriptor.h"
14 #include "google/protobuf/compiler/csharp/csharp_doc_comment.h"
15 #include "google/protobuf/compiler/csharp/csharp_helpers.h"
16 #include "google/protobuf/compiler/csharp/csharp_message_field.h"
17 #include "google/protobuf/compiler/csharp/csharp_wrapper_field.h"
18 #include "google/protobuf/descriptor.pb.h"
19 #include "google/protobuf/io/printer.h"
20 #include "google/protobuf/io/zero_copy_stream.h"
21
22 namespace google {
23 namespace protobuf {
24 namespace compiler {
25 namespace csharp {
26
RepeatedMessageFieldGenerator(const FieldDescriptor * descriptor,int presenceIndex,const Options * options)27 RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator(
28 const FieldDescriptor* descriptor, int presenceIndex, const Options *options)
29 : FieldGeneratorBase(descriptor, presenceIndex, options) {
30 }
31
~RepeatedMessageFieldGenerator()32 RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {
33
34 }
35
GenerateMembers(io::Printer * printer)36 void RepeatedMessageFieldGenerator::GenerateMembers(io::Printer* printer) {
37 printer->Print(
38 variables_,
39 "private static readonly pb::FieldCodec<$type_name$> _repeated_$name$_codec\n"
40 " = ");
41 // Don't want to duplicate the codec code here... maybe we should have a
42 // "create single field generator for this repeated field"
43 // function, but it doesn't seem worth it for just this.
44 if (IsWrapperType(descriptor_)) {
45 std::unique_ptr<FieldGeneratorBase> single_generator(
46 new WrapperFieldGenerator(descriptor_, presenceIndex_, this->options()));
47 single_generator->GenerateCodecCode(printer);
48 } else {
49 std::unique_ptr<FieldGeneratorBase> single_generator(
50 new MessageFieldGenerator(descriptor_, presenceIndex_, this->options()));
51 single_generator->GenerateCodecCode(printer);
52 }
53 printer->Print(";\n");
54 printer->Print(
55 variables_,
56 "private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n");
57 WritePropertyDocComment(printer, options(), descriptor_);
58 AddPublicMemberAttributes(printer);
59 printer->Print(
60 variables_,
61 "$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n"
62 " get { return $name$_; }\n"
63 "}\n");
64 }
65
GenerateMergingCode(io::Printer * printer)66 void RepeatedMessageFieldGenerator::GenerateMergingCode(io::Printer* printer) {
67 printer->Print(
68 variables_,
69 "$name$_.Add(other.$name$_);\n");
70 }
71
GenerateParsingCode(io::Printer * printer)72 void RepeatedMessageFieldGenerator::GenerateParsingCode(io::Printer* printer) {
73 GenerateParsingCode(printer, true);
74 }
75
GenerateParsingCode(io::Printer * printer,bool use_parse_context)76 void RepeatedMessageFieldGenerator::GenerateParsingCode(io::Printer* printer, bool use_parse_context) {
77 printer->Print(
78 variables_,
79 use_parse_context
80 ? "$name$_.AddEntriesFrom(ref input, _repeated_$name$_codec);\n"
81 : "$name$_.AddEntriesFrom(input, _repeated_$name$_codec);\n");
82 }
83
GenerateSerializationCode(io::Printer * printer)84 void RepeatedMessageFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
85 GenerateSerializationCode(printer, true);
86 }
87
GenerateSerializationCode(io::Printer * printer,bool use_write_context)88 void RepeatedMessageFieldGenerator::GenerateSerializationCode(io::Printer* printer, bool use_write_context) {
89 printer->Print(
90 variables_,
91 use_write_context
92 ? "$name$_.WriteTo(ref output, _repeated_$name$_codec);\n"
93 : "$name$_.WriteTo(output, _repeated_$name$_codec);\n");
94 }
95
GenerateSerializedSizeCode(io::Printer * printer)96 void RepeatedMessageFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
97 printer->Print(
98 variables_,
99 "size += $name$_.CalculateSize(_repeated_$name$_codec);\n");
100 }
101
WriteHash(io::Printer * printer)102 void RepeatedMessageFieldGenerator::WriteHash(io::Printer* printer) {
103 printer->Print(
104 variables_,
105 "hash ^= $name$_.GetHashCode();\n");
106 }
107
WriteEquals(io::Printer * printer)108 void RepeatedMessageFieldGenerator::WriteEquals(io::Printer* printer) {
109 printer->Print(
110 variables_,
111 "if(!$name$_.Equals(other.$name$_)) return false;\n");
112 }
113
WriteToString(io::Printer * printer)114 void RepeatedMessageFieldGenerator::WriteToString(io::Printer* printer) {
115 variables_["field_name"] = GetFieldName(descriptor_);
116 printer->Print(
117 variables_,
118 "PrintField(\"$field_name$\", $name$_, writer);\n");
119 }
120
GenerateCloningCode(io::Printer * printer)121 void RepeatedMessageFieldGenerator::GenerateCloningCode(io::Printer* printer) {
122 printer->Print(variables_,
123 "$name$_ = other.$name$_.Clone();\n");
124 }
125
GenerateFreezingCode(io::Printer * printer)126 void RepeatedMessageFieldGenerator::GenerateFreezingCode(io::Printer* printer) {
127 }
128
GenerateExtensionCode(io::Printer * printer)129 void RepeatedMessageFieldGenerator::GenerateExtensionCode(io::Printer* printer) {
130 WritePropertyDocComment(printer, options(), descriptor_);
131 AddDeprecatedFlag(printer);
132 printer->Print(
133 variables_,
134 "$access_level$ static readonly pb::RepeatedExtension<$extended_type$, $type_name$> $property_name$ =\n"
135 " new pb::RepeatedExtension<$extended_type$, $type_name$>($number$, ");
136 if (IsWrapperType(descriptor_)) {
137 std::unique_ptr<FieldGeneratorBase> single_generator(
138 new WrapperFieldGenerator(descriptor_, -1, this->options()));
139 single_generator->GenerateCodecCode(printer);
140 } else {
141 std::unique_ptr<FieldGeneratorBase> single_generator(
142 new MessageFieldGenerator(descriptor_, -1, this->options()));
143 single_generator->GenerateCodecCode(printer);
144 }
145 printer->Print(");\n");
146 }
147
148 } // namespace csharp
149 } // namespace compiler
150 } // namespace protobuf
151 } // namespace google
152