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_enum_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/wire_format.h"
15 #include "google/protobuf/compiler/csharp/csharp_doc_comment.h"
16 #include "google/protobuf/compiler/csharp/csharp_helpers.h"
17 #include "google/protobuf/descriptor.pb.h"
18 #include "google/protobuf/io/printer.h"
19 #include "google/protobuf/io/zero_copy_stream.h"
20
21 namespace google {
22 namespace protobuf {
23 namespace compiler {
24 namespace csharp {
25
RepeatedEnumFieldGenerator(const FieldDescriptor * descriptor,int presenceIndex,const Options * options)26 RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator(
27 const FieldDescriptor* descriptor, int presenceIndex, const Options *options)
28 : FieldGeneratorBase(descriptor, presenceIndex, options) {
29 }
30
~RepeatedEnumFieldGenerator()31 RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {
32
33 }
34
GenerateMembers(io::Printer * printer)35 void RepeatedEnumFieldGenerator::GenerateMembers(io::Printer* printer) {
36 printer->Print(
37 variables_,
38 "private static readonly pb::FieldCodec<$type_name$> _repeated_$name$_codec\n"
39 " = pb::FieldCodec.ForEnum($tag$, x => (int) x, x => ($type_name$) x);\n");
40 printer->Print(variables_,
41 "private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n");
42 WritePropertyDocComment(printer, options(), descriptor_);
43 AddPublicMemberAttributes(printer);
44 printer->Print(
45 variables_,
46 "$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n"
47 " get { return $name$_; }\n"
48 "}\n");
49 }
50
GenerateMergingCode(io::Printer * printer)51 void RepeatedEnumFieldGenerator::GenerateMergingCode(io::Printer* printer) {
52 printer->Print(
53 variables_,
54 "$name$_.Add(other.$name$_);\n");
55 }
56
GenerateParsingCode(io::Printer * printer)57 void RepeatedEnumFieldGenerator::GenerateParsingCode(io::Printer* printer) {
58 GenerateParsingCode(printer, true);
59 }
60
GenerateParsingCode(io::Printer * printer,bool use_parse_context)61 void RepeatedEnumFieldGenerator::GenerateParsingCode(io::Printer* printer, bool use_parse_context) {
62 printer->Print(
63 variables_,
64 use_parse_context
65 ? "$name$_.AddEntriesFrom(ref input, _repeated_$name$_codec);\n"
66 : "$name$_.AddEntriesFrom(input, _repeated_$name$_codec);\n");
67 }
68
GenerateSerializationCode(io::Printer * printer)69 void RepeatedEnumFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
70 GenerateSerializationCode(printer, true);
71 }
72
GenerateSerializationCode(io::Printer * printer,bool use_write_context)73 void RepeatedEnumFieldGenerator::GenerateSerializationCode(io::Printer* printer, bool use_write_context) {
74 printer->Print(
75 variables_,
76 use_write_context
77 ? "$name$_.WriteTo(ref output, _repeated_$name$_codec);\n"
78 : "$name$_.WriteTo(output, _repeated_$name$_codec);\n");
79 }
80
GenerateSerializedSizeCode(io::Printer * printer)81 void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
82 printer->Print(
83 variables_,
84 "size += $name$_.CalculateSize(_repeated_$name$_codec);\n");
85 }
86
WriteHash(io::Printer * printer)87 void RepeatedEnumFieldGenerator::WriteHash(io::Printer* printer) {
88 printer->Print(
89 variables_,
90 "hash ^= $name$_.GetHashCode();\n");
91 }
92
WriteEquals(io::Printer * printer)93 void RepeatedEnumFieldGenerator::WriteEquals(io::Printer* printer) {
94 printer->Print(
95 variables_,
96 "if(!$name$_.Equals(other.$name$_)) return false;\n");
97 }
98
WriteToString(io::Printer * printer)99 void RepeatedEnumFieldGenerator::WriteToString(io::Printer* printer) {
100 printer->Print(variables_,
101 "PrintField(\"$descriptor_name$\", $name$_, writer);\n");
102 }
103
GenerateCloningCode(io::Printer * printer)104 void RepeatedEnumFieldGenerator::GenerateCloningCode(io::Printer* printer) {
105 printer->Print(variables_,
106 "$name$_ = other.$name$_.Clone();\n");
107 }
108
GenerateExtensionCode(io::Printer * printer)109 void RepeatedEnumFieldGenerator::GenerateExtensionCode(io::Printer* printer) {
110 WritePropertyDocComment(printer, options(), descriptor_);
111 AddDeprecatedFlag(printer);
112 printer->Print(
113 variables_,
114 "$access_level$ static readonly pb::RepeatedExtension<$extended_type$, $type_name$> $property_name$ =\n"
115 " new pb::RepeatedExtension<$extended_type$, $type_name$>($number$, "
116 "pb::FieldCodec.ForEnum($tag$, x => (int) x, x => ($type_name$) x));\n");
117 }
118
GenerateFreezingCode(io::Printer * printer)119 void RepeatedEnumFieldGenerator::GenerateFreezingCode(io::Printer* printer) {
120 }
121
122 } // namespace csharp
123 } // namespace compiler
124 } // namespace protobuf
125 } // namespace google
126