csharp_wrapper_field.cc
Go to the documentation of this file.
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 
38 
43 
44 namespace google {
45 namespace protobuf {
46 namespace compiler {
47 namespace csharp {
48 
50  int presenceIndex, const Options *options)
51  : FieldGeneratorBase(descriptor, presenceIndex, options) {
52  variables_["has_property_check"] = name() + "_ != null";
53  variables_["has_not_property_check"] = name() + "_ == null";
54  const FieldDescriptor* wrapped_field = descriptor->message_type()->field(0);
55  is_value_type = wrapped_field->type() != FieldDescriptor::TYPE_STRING &&
56  wrapped_field->type() != FieldDescriptor::TYPE_BYTES;
57  if (is_value_type) {
58  variables_["nonnullable_type_name"] = type_name(wrapped_field);
59  }
60 }
61 
63 }
64 
66  printer->Print(
67  variables_,
68  "private static readonly pb::FieldCodec<$type_name$> _single_$name$_codec = ");
69  GenerateCodecCode(printer);
70  printer->Print(
71  variables_,
72  ";\n"
73  "private $type_name$ $name$_;\n");
76  printer->Print(
77  variables_,
78  "$access_level$ $type_name$ $property_name$ {\n"
79  " get { return $name$_; }\n"
80  " set {\n"
81  " $name$_ = value;\n"
82  " }\n"
83  "}\n\n");
84  if (IsProto2(descriptor_->file())) {
85  printer->Print(
86  variables_,
87  "/// <summary>Gets whether the $descriptor_name$ field is set</summary>\n");
89  printer->Print(
90  variables_,
91  "$access_level$ bool Has$property_name$ {\n"
92  " get { return $name$_ != null; }\n"
93  "}\n\n");
94  printer->Print(
95  variables_,
96  "/// <summary>Clears the value of the $descriptor_name$ field</summary>\n");
98  printer->Print(
99  variables_,
100  "$access_level$ void Clear$property_name$() {\n"
101  " $name$_ = null;\n"
102  "}\n");
103  }
104 }
105 
107  printer->Print(
108  variables_,
109  "if (other.$has_property_check$) {\n"
110  " if ($has_not_property_check$ || other.$property_name$ != $default_value$) {\n"
111  " $property_name$ = other.$property_name$;\n"
112  " }\n"
113  "}\n");
114 }
115 
117  printer->Print(
118  variables_,
119  "$type_name$ value = _single_$name$_codec.Read(input);\n"
120  "if ($has_not_property_check$ || value != $default_value$) {\n"
121  " $property_name$ = value;\n"
122  "}\n");
123 }
124 
126  printer->Print(
127  variables_,
128  "if ($has_property_check$) {\n"
129  " _single_$name$_codec.WriteTagAndValue(output, $property_name$);\n"
130  "}\n");
131 }
132 
134  printer->Print(
135  variables_,
136  "if ($has_property_check$) {\n"
137  " size += _single_$name$_codec.CalculateSizeWithTag($property_name$);\n"
138  "}\n");
139 }
140 
142  const char *text = "if ($has_property_check$) hash ^= $property_name$.GetHashCode();\n";
144  text = "if ($has_property_check$) hash ^= pbc::ProtobufEqualityComparers.BitwiseNullableSingleEqualityComparer.GetHashCode($property_name$);\n";
145  }
147  text = "if ($has_property_check$) hash ^= pbc::ProtobufEqualityComparers.BitwiseNullableDoubleEqualityComparer.GetHashCode($property_name$);\n";
148  }
149  printer->Print(variables_, text);
150 }
151 
153  const char *text = "if ($property_name$ != other.$property_name$) return false;\n";
155  text = "if (!pbc::ProtobufEqualityComparers.BitwiseNullableSingleEqualityComparer.Equals($property_name$, other.$property_name$)) return false;\n";
156  }
158  text = "if (!pbc::ProtobufEqualityComparers.BitwiseNullableDoubleEqualityComparer.Equals($property_name$, other.$property_name$)) return false;\n";
159  }
160  printer->Print(variables_, text);
161 }
162 
164  // TODO: Implement if we ever actually need it...
165 }
166 
168  printer->Print(variables_,
169  "$property_name$ = other.$property_name$;\n");
170 }
171 
173  if (is_value_type) {
174  printer->Print(
175  variables_,
176  "pb::FieldCodec.ForStructWrapper<$nonnullable_type_name$>($tag$)");
177  } else {
178  printer->Print(
179  variables_,
180  "pb::FieldCodec.ForClassWrapper<$type_name$>($tag$)");
181  }
182 }
183 
186  AddDeprecatedFlag(printer);
187  printer->Print(
188  variables_,
189  "$access_level$ static readonly pb::Extension<$extended_type$, $type_name$> $property_name$ =\n"
190  " new pb::Extension<$extended_type$, $type_name$>($number$, ");
191  GenerateCodecCode(printer);
192  printer->Print(");\n");
193 }
194 
196  const FieldDescriptor* descriptor, int presenceIndex, const Options *options)
197  : WrapperFieldGenerator(descriptor, presenceIndex, options) {
199 }
200 
202 }
203 
205  // Note: deliberately _oneof_$name$_codec, not _$oneof_name$_codec... we have one codec per field.
206  printer->Print(
207  variables_,
208  "private static readonly pb::FieldCodec<$type_name$> _oneof_$name$_codec = ");
209  GenerateCodecCode(printer);
210  printer->Print(";\n");
212  AddPublicMemberAttributes(printer);
213  printer->Print(
214  variables_,
215  "$access_level$ $type_name$ $property_name$ {\n"
216  " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : ($type_name$) null; }\n"
217  " set {\n"
218  " $oneof_name$_ = value;\n"
219  " $oneof_name$Case_ = value == null ? $oneof_property_name$OneofCase.None : $oneof_property_name$OneofCase.$property_name$;\n"
220  " }\n"
221  "}\n");
222  if (IsProto2(descriptor_->file())) {
223  printer->Print(
224  variables_,
225  "/// <summary>Gets whether the \"$descriptor_name$\" field is set</summary>\n");
226  AddPublicMemberAttributes(printer);
227  printer->Print(
228  variables_,
229  "$access_level$ bool Has$property_name$ {\n"
230  " get { return $oneof_name$Case_ == $oneof_property_name$OneofCase.$property_name$; }\n"
231  "}\n");
232  printer->Print(
233  variables_,
234  "/// <summary> Clears the value of the oneof if it's currently set to \"$descriptor_name$\" </summary>\n");
235  AddPublicMemberAttributes(printer);
236  printer->Print(
237  variables_,
238  "$access_level$ void Clear$property_name$() {\n"
239  " if ($has_property_check$) {\n"
240  " Clear$oneof_property_name$();\n"
241  " }\n"
242  "}\n");
243  }
244 }
245 
247  printer->Print(variables_, "$property_name$ = other.$property_name$;\n");
248 }
249 
251  printer->Print(
252  variables_,
253  "$property_name$ = _oneof_$name$_codec.Read(input);\n");
254 }
255 
257  // TODO: I suspect this is wrong...
258  printer->Print(
259  variables_,
260  "if ($has_property_check$) {\n"
261  " _oneof_$name$_codec.WriteTagAndValue(output, ($type_name$) $oneof_name$_);\n"
262  "}\n");
263 }
264 
266  // TODO: I suspect this is wrong...
267  printer->Print(
268  variables_,
269  "if ($has_property_check$) {\n"
270  " size += _oneof_$name$_codec.CalculateSizeWithTag($property_name$);\n"
271  "}\n");
272 }
273 
274 } // namespace csharp
275 } // namespace compiler
276 } // namespace protobuf
277 } // namespace google
zero_copy_stream.h
google::protobuf::io::Printer::Print
void Print(const std::map< std::string, std::string > &variables, const char *text)
Definition: printer.cc:112
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
google::protobuf::compiler::csharp::WrapperFieldGenerator::is_value_type
bool is_value_type
Definition: csharp_wrapper_field.h:70
google::protobuf::compiler::csharp::WrapperOneofFieldGenerator::GenerateSerializationCode
virtual void GenerateSerializationCode(io::Printer *printer)
Definition: csharp_wrapper_field.cc:256
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
google::protobuf::FieldDescriptor::TYPE_BYTES
@ TYPE_BYTES
Definition: src/google/protobuf/descriptor.h:538
google::protobuf::compiler::csharp::WrapperFieldGenerator::~WrapperFieldGenerator
~WrapperFieldGenerator()
Definition: csharp_wrapper_field.cc:62
google::protobuf::compiler::csharp::WrapperOneofFieldGenerator::GenerateMergingCode
virtual void GenerateMergingCode(io::Printer *printer)
Definition: csharp_wrapper_field.cc:246
google::protobuf::compiler::csharp::FieldGeneratorBase::SetCommonOneofFieldVariables
void SetCommonOneofFieldVariables(std::map< string, string > *variables)
Definition: csharp_field_base.cc:125
google::protobuf::compiler::csharp::WrapperFieldGenerator
Definition: csharp_wrapper_field.h:46
google::protobuf::compiler::csharp::FieldGeneratorBase::descriptor_
const FieldDescriptor * descriptor_
Definition: csharp_field_base.h:73
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::compiler::csharp::FieldGeneratorBase::name
std::string name()
Definition: csharp_field_base.cc:190
google::protobuf::compiler::csharp::IsProto2
bool IsProto2(const FileDescriptor *descriptor)
Definition: csharp_helpers.h:156
google::protobuf::compiler::csharp::FieldGeneratorBase::AddDeprecatedFlag
void AddDeprecatedFlag(io::Printer *printer)
Definition: csharp_field_base.cc:164
google::protobuf::compiler::csharp::FieldGeneratorBase::variables_
std::map< string, string > variables_
Definition: csharp_field_base.h:75
google::protobuf::compiler::csharp::WrapperFieldGenerator::WriteToString
virtual void WriteToString(io::Printer *printer)
Definition: csharp_wrapper_field.cc:163
google::protobuf::compiler::csharp::Options
Definition: csharp_options.h:42
google::protobuf::Descriptor::field
const FieldDescriptor * field(int index) const
google::protobuf::compiler::csharp::WrapperOneofFieldGenerator::~WrapperOneofFieldGenerator
~WrapperOneofFieldGenerator()
Definition: csharp_wrapper_field.cc:201
google::protobuf::compiler::csharp::WrapperFieldGenerator::WriteEquals
virtual void WriteEquals(io::Printer *printer)
Definition: csharp_wrapper_field.cc:152
google::protobuf::compiler::csharp::WrapperOneofFieldGenerator::GenerateParsingCode
virtual void GenerateParsingCode(io::Printer *printer)
Definition: csharp_wrapper_field.cc:250
google::protobuf::FieldDescriptor::file
const FileDescriptor * file() const
csharp_doc_comment.h
google::protobuf::compiler::csharp::FieldGeneratorBase::AddPublicMemberAttributes
void AddPublicMemberAttributes(io::Printer *printer)
Definition: csharp_field_base.cc:173
printer.h
google::protobuf::compiler::csharp::WrapperFieldGenerator::GenerateCodecCode
virtual void GenerateCodecCode(io::Printer *printer)
Definition: csharp_wrapper_field.cc:172
google::protobuf::FieldDescriptor::TYPE_STRING
@ TYPE_STRING
Definition: src/google/protobuf/descriptor.h:534
google::protobuf::compiler::csharp::WrapperFieldGenerator::GenerateSerializationCode
virtual void GenerateSerializationCode(io::Printer *printer)
Definition: csharp_wrapper_field.cc:125
csharp_wrapper_field.h
code_generator.h
google::protobuf::compiler::csharp::WrapperOneofFieldGenerator::GenerateSerializedSizeCode
virtual void GenerateSerializedSizeCode(io::Printer *printer)
Definition: csharp_wrapper_field.cc:265
google::protobuf::io::Printer
Definition: printer.h:181
google::protobuf::FieldDescriptor::TYPE_DOUBLE
@ TYPE_DOUBLE
Definition: src/google/protobuf/descriptor.h:522
csharp_helpers.h
google::protobuf::FieldDescriptor::TYPE_FLOAT
@ TYPE_FLOAT
Definition: src/google/protobuf/descriptor.h:523
google::protobuf::FieldDescriptor::type
Type type() const
Definition: src/google/protobuf/descriptor.h:2052
csharp_options.h
google::protobuf::compiler::csharp::WrapperFieldGenerator::GenerateParsingCode
virtual void GenerateParsingCode(io::Printer *printer)
Definition: csharp_wrapper_field.cc:116
descriptor.h
google::protobuf::compiler::csharp::WrapperOneofFieldGenerator::GenerateMembers
virtual void GenerateMembers(io::Printer *printer)
Definition: csharp_wrapper_field.cc:204
google::protobuf::compiler::csharp::WrapperFieldGenerator::WriteHash
virtual void WriteHash(io::Printer *printer)
Definition: csharp_wrapper_field.cc:141
google::protobuf::compiler::csharp::WritePropertyDocComment
void WritePropertyDocComment(io::Printer *printer, const FieldDescriptor *field)
Definition: csharp_doc_comment.cc:97
google::protobuf::compiler::csharp::WrapperOneofFieldGenerator::WrapperOneofFieldGenerator
WrapperOneofFieldGenerator(const FieldDescriptor *descriptor, int presenceIndex, const Options *options)
Definition: csharp_wrapper_field.cc:195
google::protobuf::FieldDescriptor::message_type
const Descriptor * message_type() const
Definition: src/google/protobuf/descriptor.cc:7228
google::protobuf::compiler::csharp::WrapperFieldGenerator::GenerateSerializedSizeCode
virtual void GenerateSerializedSizeCode(io::Printer *printer)
Definition: csharp_wrapper_field.cc:133
google::protobuf::compiler::csharp::WrapperFieldGenerator::GenerateMergingCode
virtual void GenerateMergingCode(io::Printer *printer)
Definition: csharp_wrapper_field.cc:106
google::protobuf::compiler::csharp::WrapperFieldGenerator::GenerateExtensionCode
virtual void GenerateExtensionCode(io::Printer *printer)
Definition: csharp_wrapper_field.cc:184
descriptor.pb.h
google::protobuf::compiler::csharp::WrapperFieldGenerator::GenerateMembers
virtual void GenerateMembers(io::Printer *printer)
Definition: csharp_wrapper_field.cc:65
google::protobuf::compiler::csharp::WrapperFieldGenerator::GenerateCloningCode
virtual void GenerateCloningCode(io::Printer *printer)
Definition: csharp_wrapper_field.cc:167
google::protobuf::compiler::csharp::FieldGeneratorBase
Definition: csharp_field_base.h:47
google::protobuf::compiler::csharp::FieldGeneratorBase::type_name
std::string type_name()
Definition: csharp_field_base.cc:194
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::compiler::csharp::WrapperFieldGenerator::WrapperFieldGenerator
WrapperFieldGenerator(const FieldDescriptor *descriptor, int presenceIndex, const Options *options)
Definition: csharp_wrapper_field.cc:49


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:49