Line data Source code
1 : // Protocol Buffers - Google's data interchange format
2 : // Copyright 2008 Google Inc. All rights reserved.
3 : // https://developers.google.com/protocol-buffers/
4 : //
5 : // Redistribution and use in source and binary forms, with or without
6 : // modification, are permitted provided that the following conditions are
7 : // met:
8 : //
9 : // * Redistributions of source code must retain the above copyright
10 : // notice, this list of conditions and the following disclaimer.
11 : // * Redistributions in binary form must reproduce the above
12 : // copyright notice, this list of conditions and the following disclaimer
13 : // in the documentation and/or other materials provided with the
14 : // distribution.
15 : // * Neither the name of Google Inc. nor the names of its
16 : // contributors may be used to endorse or promote products derived from
17 : // this software without specific prior written permission.
18 : //
19 : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 : // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 : // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 : // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 : // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 : // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 : // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 : // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 : // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 :
31 : #include <sstream>
32 :
33 : #include <google/protobuf/compiler/code_generator.h>
34 : #include <google/protobuf/compiler/plugin.h>
35 : #include <google/protobuf/descriptor.h>
36 : #include <google/protobuf/descriptor.pb.h>
37 : #include <google/protobuf/io/printer.h>
38 : #include <google/protobuf/io/zero_copy_stream.h>
39 :
40 : #include <google/protobuf/compiler/csharp/csharp_helpers.h>
41 : #include <google/protobuf/compiler/csharp/csharp_repeated_message_field.h>
42 : #include <google/protobuf/compiler/csharp/csharp_message_field.h>
43 : #include <google/protobuf/compiler/csharp/csharp_wrapper_field.h>
44 :
45 : namespace google {
46 : namespace protobuf {
47 : namespace compiler {
48 : namespace csharp {
49 :
50 0 : RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator(
51 : const FieldDescriptor* descriptor, int fieldOrdinal)
52 0 : : FieldGeneratorBase(descriptor, fieldOrdinal) {
53 0 : }
54 :
55 0 : RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {
56 :
57 0 : }
58 :
59 0 : void RepeatedMessageFieldGenerator::GenerateMembers(io::Printer* printer) {
60 : printer->Print(
61 : variables_,
62 : "private static readonly pb::FieldCodec<$type_name$> _repeated_$name$_codec\n"
63 0 : " = ");
64 : // Don't want to duplicate the codec code here... maybe we should have a
65 : // "create single field generator for this repeated field"
66 : // function, but it doesn't seem worth it for just this.
67 0 : if (IsWrapperType(descriptor_)) {
68 0 : scoped_ptr<FieldGeneratorBase> single_generator(new WrapperFieldGenerator(descriptor_, fieldOrdinal_));
69 0 : single_generator->GenerateCodecCode(printer);
70 : } else {
71 0 : scoped_ptr<FieldGeneratorBase> single_generator(new MessageFieldGenerator(descriptor_, fieldOrdinal_));
72 0 : single_generator->GenerateCodecCode(printer);
73 : }
74 0 : printer->Print(";\n");
75 : printer->Print(
76 : variables_,
77 0 : "private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n");
78 0 : AddDeprecatedFlag(printer);
79 : printer->Print(
80 : variables_,
81 : "$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n"
82 : " get { return $name$_; }\n"
83 0 : "}\n");
84 0 : }
85 :
86 0 : void RepeatedMessageFieldGenerator::GenerateMergingCode(io::Printer* printer) {
87 : printer->Print(
88 : variables_,
89 0 : "$name$_.Add(other.$name$_);\n");
90 0 : }
91 :
92 0 : void RepeatedMessageFieldGenerator::GenerateParsingCode(io::Printer* printer) {
93 : printer->Print(
94 : variables_,
95 0 : "$name$_.AddEntriesFrom(input, _repeated_$name$_codec);\n");
96 0 : }
97 :
98 0 : void RepeatedMessageFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
99 : printer->Print(
100 : variables_,
101 0 : "$name$_.WriteTo(output, _repeated_$name$_codec);\n");
102 0 : }
103 :
104 0 : void RepeatedMessageFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
105 : printer->Print(
106 : variables_,
107 0 : "size += $name$_.CalculateSize(_repeated_$name$_codec);\n");
108 0 : }
109 :
110 0 : void RepeatedMessageFieldGenerator::WriteHash(io::Printer* printer) {
111 : printer->Print(
112 : variables_,
113 0 : "hash ^= $name$_.GetHashCode();\n");
114 0 : }
115 :
116 0 : void RepeatedMessageFieldGenerator::WriteEquals(io::Printer* printer) {
117 : printer->Print(
118 : variables_,
119 0 : "if(!$name$_.Equals(other.$name$_)) return false;\n");
120 0 : }
121 :
122 0 : void RepeatedMessageFieldGenerator::WriteToString(io::Printer* printer) {
123 0 : variables_["field_name"] = GetFieldName(descriptor_);
124 : printer->Print(
125 : variables_,
126 0 : "PrintField(\"$field_name$\", $name$_, writer);\n");
127 0 : }
128 :
129 0 : void RepeatedMessageFieldGenerator::GenerateCloningCode(io::Printer* printer) {
130 : printer->Print(variables_,
131 0 : "$name$_ = other.$name$_.Clone();\n");
132 0 : }
133 :
134 0 : void RepeatedMessageFieldGenerator::GenerateFreezingCode(io::Printer* printer) {
135 0 : }
136 :
137 : } // namespace csharp
138 : } // namespace compiler
139 : } // namespace protobuf
140 : } // namespace google
|