• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_primitive_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 
RepeatedPrimitiveFieldGenerator(const FieldDescriptor * descriptor,int presenceIndex,const Options * options)26 RepeatedPrimitiveFieldGenerator::RepeatedPrimitiveFieldGenerator(
27     const FieldDescriptor* descriptor, int presenceIndex, const Options *options)
28     : FieldGeneratorBase(descriptor, presenceIndex, options) {
29 }
30 
~RepeatedPrimitiveFieldGenerator()31 RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {
32 
33 }
34 
GenerateMembers(io::Printer * printer)35 void RepeatedPrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) {
36   printer->Print(
37     variables_,
38     "private static readonly pb::FieldCodec<$type_name$> _repeated_$name$_codec\n"
39     "    = pb::FieldCodec.For$capitalized_type_name$($tag$);\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 RepeatedPrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) {
52   printer->Print(
53     variables_,
54     "$name$_.Add(other.$name$_);\n");
55 }
56 
GenerateParsingCode(io::Printer * printer)57 void RepeatedPrimitiveFieldGenerator::GenerateParsingCode(io::Printer* printer) {
58   GenerateParsingCode(printer, true);
59 }
60 
GenerateParsingCode(io::Printer * printer,bool use_parse_context)61 void RepeatedPrimitiveFieldGenerator::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 RepeatedPrimitiveFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
70   GenerateSerializationCode(printer, true);
71 }
72 
GenerateSerializationCode(io::Printer * printer,bool use_write_context)73 void RepeatedPrimitiveFieldGenerator::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 RepeatedPrimitiveFieldGenerator::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 RepeatedPrimitiveFieldGenerator::WriteHash(io::Printer* printer) {
88   printer->Print(
89     variables_,
90     "hash ^= $name$_.GetHashCode();\n");
91 }
WriteEquals(io::Printer * printer)92 void RepeatedPrimitiveFieldGenerator::WriteEquals(io::Printer* printer) {
93   printer->Print(
94     variables_,
95     "if(!$name$_.Equals(other.$name$_)) return false;\n");
96 }
WriteToString(io::Printer * printer)97 void RepeatedPrimitiveFieldGenerator::WriteToString(io::Printer* printer) {
98   printer->Print(variables_,
99     "PrintField(\"$descriptor_name$\", $name$_, writer);\n");
100 }
101 
GenerateCloningCode(io::Printer * printer)102 void RepeatedPrimitiveFieldGenerator::GenerateCloningCode(io::Printer* printer) {
103   printer->Print(variables_,
104     "$name$_ = other.$name$_.Clone();\n");
105 }
106 
GenerateFreezingCode(io::Printer * printer)107 void RepeatedPrimitiveFieldGenerator::GenerateFreezingCode(io::Printer* printer) {
108 }
109 
GenerateExtensionCode(io::Printer * printer)110 void RepeatedPrimitiveFieldGenerator::GenerateExtensionCode(io::Printer* printer) {
111   WritePropertyDocComment(printer, options(), descriptor_);
112   AddDeprecatedFlag(printer);
113   printer->Print(
114     variables_,
115     "$access_level$ static readonly pb::RepeatedExtension<$extended_type$, $type_name$> $property_name$ =\n"
116     "  new pb::RepeatedExtension<$extended_type$, $type_name$>($number$, pb::FieldCodec.For$capitalized_type_name$($tag$));\n");
117 }
118 
119 }  // namespace csharp
120 }  // namespace compiler
121 }  // namespace protobuf
122 }  // namespace google
123