csharp_primitive_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 
39 
44 
45 namespace google {
46 namespace protobuf {
47 namespace compiler {
48 namespace csharp {
49 
51  const FieldDescriptor* descriptor, int presenceIndex, const Options *options)
52  : FieldGeneratorBase(descriptor, presenceIndex, options) {
53  // TODO(jonskeet): Make this cleaner...
56  if (!is_value_type && !IsProto2(descriptor_->file())) {
57  variables_["has_property_check"] = variables_["property_name"] + ".Length != 0";
58  variables_["other_has_property_check"] = "other." + variables_["property_name"] + ".Length != 0";
59  }
60 }
61 
63 }
64 
66  // TODO(jonskeet): Work out whether we want to prevent the fields from ever being
67  // null, or whether we just handle it, in the cases of bytes and string.
68  // (Basically, should null-handling code be in the getter or the setter?)
69  if (IsProto2(descriptor_->file())) {
70  printer->Print(
71  variables_,
72  "private readonly static $type_name$ $property_name$DefaultValue = $default_value$;\n\n");
73  }
74 
75  printer->Print(
76  variables_,
77  "private $type_name$ $name_def_message$;\n");
78 
81  if (IsProto2(descriptor_->file())) {
82  if (presenceIndex_ == -1) {
83  printer->Print(
84  variables_,
85  "$access_level$ $type_name$ $property_name$ {\n"
86  " get { return $name$_ ?? $property_name$DefaultValue; }\n"
87  " set {\n");
88  } else {
89  printer->Print(
90  variables_,
91  "$access_level$ $type_name$ $property_name$ {\n"
92  " get { if ($has_field_check$) { return $name$_; } else { return $property_name$DefaultValue; } }\n"
93  " set {\n");
94  }
95  } else {
96  printer->Print(
97  variables_,
98  "$access_level$ $type_name$ $property_name$ {\n"
99  " get { return $name$_; }\n"
100  " set {\n");
101  }
102  if (presenceIndex_ != -1) {
103  printer->Print(
104  variables_,
105  " $set_has_field$;\n");
106  }
107  if (is_value_type) {
108  printer->Print(
109  variables_,
110  " $name$_ = value;\n");
111  } else {
112  printer->Print(
113  variables_,
114  " $name$_ = pb::ProtoPreconditions.CheckNotNull(value, \"value\");\n");
115  }
116  printer->Print(
117  " }\n"
118  "}\n");
119  if (IsProto2(descriptor_->file())) {
120  printer->Print(variables_, "/// <summary>Gets whether the \"$descriptor_name$\" field is set</summary>\n");
121  AddPublicMemberAttributes(printer);
122  printer->Print(
123  variables_,
124  "$access_level$ bool Has$property_name$ {\n"
125  " get { return ");
126  if (IsNullable(descriptor_)) {
127  printer->Print(
128  variables_,
129  "$name$_ != null; }\n}\n");
130  } else {
131  printer->Print(
132  variables_,
133  "$has_field_check$; }\n}\n");
134  }
135  }
136  if (IsProto2(descriptor_->file())) {
137  printer->Print(variables_, "/// <summary>Clears the value of the \"$descriptor_name$\" field</summary>\n");
138  AddPublicMemberAttributes(printer);
139  printer->Print(
140  variables_,
141  "$access_level$ void Clear$property_name$() {\n");
142  if (IsNullable(descriptor_)) {
143  printer->Print(variables_, " $name$_ = null;\n");
144  } else {
145  printer->Print(variables_, " $clear_has_field$;\n");
146  }
147  printer->Print("}\n");
148  }
149 }
150 
152  printer->Print(
153  variables_,
154  "if ($other_has_property_check$) {\n"
155  " $property_name$ = other.$property_name$;\n"
156  "}\n");
157 }
158 
160  // Note: invoke the property setter rather than writing straight to the field,
161  // so that we can normalize "null to empty" for strings and bytes.
162  printer->Print(
163  variables_,
164  "$property_name$ = input.Read$capitalized_type_name$();\n");
165 }
166 
168  printer->Print(
169  variables_,
170  "if ($has_property_check$) {\n"
171  " output.WriteRawTag($tag_bytes$);\n"
172  " output.Write$capitalized_type_name$($property_name$);\n"
173  "}\n");
174 }
175 
177  printer->Print(
178  variables_,
179  "if ($has_property_check$) {\n");
180  printer->Indent();
181  int fixedSize = GetFixedSize(descriptor_->type());
182  if (fixedSize == -1) {
183  printer->Print(
184  variables_,
185  "size += $tag_size$ + pb::CodedOutputStream.Compute$capitalized_type_name$Size($property_name$);\n");
186  } else {
187  printer->Print(
188  "size += $tag_size$ + $fixed_size$;\n",
189  "fixed_size", StrCat(fixedSize),
190  "tag_size", variables_["tag_size"]);
191  }
192  printer->Outdent();
193  printer->Print("}\n");
194 }
195 
197  const char *text = "if ($has_property_check$) hash ^= $property_name$.GetHashCode();\n";
199  text = "if ($has_property_check$) hash ^= pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.GetHashCode($property_name$);\n";
200  } else if (descriptor_->type() == FieldDescriptor::TYPE_DOUBLE) {
201  text = "if ($has_property_check$) hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode($property_name$);\n";
202  }
203  printer->Print(variables_, text);
204 }
206  const char *text = "if ($property_name$ != other.$property_name$) return false;\n";
208  text = "if (!pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.Equals($property_name$, other.$property_name$)) return false;\n";
209  } else if (descriptor_->type() == FieldDescriptor::TYPE_DOUBLE) {
210  text = "if (!pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.Equals($property_name$, other.$property_name$)) return false;\n";
211  }
212  printer->Print(variables_, text);
213 }
215  printer->Print(
216  variables_,
217  "PrintField(\"$descriptor_name$\", $has_property_check$, $property_name$, writer);\n");
218 }
219 
221  printer->Print(variables_,
222  "$name$_ = other.$name$_;\n");
223 }
224 
226  printer->Print(
227  variables_,
228  "pb::FieldCodec.For$capitalized_type_name$($tag$, $default_value$)");
229 }
230 
233  AddDeprecatedFlag(printer);
234  printer->Print(
235  variables_,
236  "$access_level$ static readonly pb::Extension<$extended_type$, $type_name$> $property_name$ =\n"
237  " new pb::Extension<$extended_type$, $type_name$>($number$, ");
238  GenerateCodecCode(printer);
239  printer->Print(");\n");
240 }
241 
243  const FieldDescriptor* descriptor, int presenceIndex, const Options *options)
244  : PrimitiveFieldGenerator(descriptor, presenceIndex, options) {
246 }
247 
249 }
250 
253  AddPublicMemberAttributes(printer);
254  printer->Print(
255  variables_,
256  "$access_level$ $type_name$ $property_name$ {\n"
257  " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : $default_value$; }\n"
258  " set {\n");
259  if (is_value_type) {
260  printer->Print(
261  variables_,
262  " $oneof_name$_ = value;\n");
263  } else {
264  printer->Print(
265  variables_,
266  " $oneof_name$_ = pb::ProtoPreconditions.CheckNotNull(value, \"value\");\n");
267  }
268  printer->Print(
269  variables_,
270  " $oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
271  " }\n"
272  "}\n");
273  if (IsProto2(descriptor_->file())) {
274  printer->Print(
275  variables_,
276  "/// <summary>Gets whether the \"$descriptor_name$\" field is set</summary>\n");
277  AddPublicMemberAttributes(printer);
278  printer->Print(
279  variables_,
280  "$access_level$ bool Has$property_name$ {\n"
281  " get { return $oneof_name$Case_ == $oneof_property_name$OneofCase.$property_name$; }\n"
282  "}\n");
283  printer->Print(
284  variables_,
285  "/// <summary> Clears the value of the oneof if it's currently set to \"$descriptor_name$\" </summary>\n");
286  AddPublicMemberAttributes(printer);
287  printer->Print(
288  variables_,
289  "$access_level$ void Clear$property_name$() {\n"
290  " if ($has_property_check$) {\n"
291  " Clear$oneof_property_name$();\n"
292  " }\n"
293  "}\n");
294  }
295 }
296 
298  printer->Print(variables_, "$property_name$ = other.$property_name$;\n");
299 }
300 
302  printer->Print(variables_,
303  "PrintField(\"$descriptor_name$\", $has_property_check$, $oneof_name$_, writer);\n");
304 }
305 
307  printer->Print(
308  variables_,
309  "$property_name$ = input.Read$capitalized_type_name$();\n");
310 }
311 
313  printer->Print(variables_,
314  "$property_name$ = other.$property_name$;\n");
315 }
316 
317 } // namespace csharp
318 } // namespace compiler
319 } // namespace protobuf
320 } // namespace google
google::protobuf::compiler::csharp::PrimitiveFieldGenerator::GenerateSerializedSizeCode
virtual void GenerateSerializedSizeCode(io::Printer *printer)
Definition: csharp_primitive_field.cc:176
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
csharp_primitive_field.h
google::protobuf::compiler::csharp::PrimitiveOneofFieldGenerator::GenerateCloningCode
virtual void GenerateCloningCode(io::Printer *printer)
Definition: csharp_primitive_field.cc:312
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
google::protobuf::compiler::csharp::PrimitiveFieldGenerator::WriteHash
virtual void WriteHash(io::Printer *printer)
Definition: csharp_primitive_field.cc:196
google::protobuf::compiler::csharp::PrimitiveFieldGenerator
Definition: csharp_primitive_field.h:46
google::protobuf::compiler::csharp::PrimitiveOneofFieldGenerator::GenerateMergingCode
virtual void GenerateMergingCode(io::Printer *printer)
Definition: csharp_primitive_field.cc:297
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
google::protobuf::compiler::csharp::PrimitiveFieldGenerator::GenerateMembers
virtual void GenerateMembers(io::Printer *printer)
Definition: csharp_primitive_field.cc:65
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::PrimitiveOneofFieldGenerator::PrimitiveOneofFieldGenerator
PrimitiveOneofFieldGenerator(const FieldDescriptor *descriptor, int presenceIndex, const Options *options)
Definition: csharp_primitive_field.cc:242
google::protobuf::compiler::csharp::PrimitiveFieldGenerator::PrimitiveFieldGenerator
PrimitiveFieldGenerator(const FieldDescriptor *descriptor, int presenceIndex, const Options *options)
Definition: csharp_primitive_field.cc:50
google::protobuf::compiler::csharp::FieldGeneratorBase::SetCommonOneofFieldVariables
void SetCommonOneofFieldVariables(std::map< string, string > *variables)
Definition: csharp_field_base.cc:125
google::protobuf::compiler::csharp::GetFixedSize
int GetFixedSize(FieldDescriptor::Type type)
Definition: csharp_helpers.cc:407
google::protobuf::compiler::csharp::PrimitiveFieldGenerator::GenerateMergingCode
virtual void GenerateMergingCode(io::Printer *printer)
Definition: csharp_primitive_field.cc:151
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::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::PrimitiveFieldGenerator::GenerateParsingCode
virtual void GenerateParsingCode(io::Printer *printer)
Definition: csharp_primitive_field.cc:159
google::protobuf::compiler::csharp::Options
Definition: csharp_options.h:42
google::protobuf::io::Printer::Indent
void Indent()
Definition: printer.cc:185
google::protobuf::compiler::csharp::PrimitiveFieldGenerator::GenerateSerializationCode
virtual void GenerateSerializationCode(io::Printer *printer)
Definition: csharp_primitive_field.cc:167
strutil.h
google::protobuf::compiler::csharp::PrimitiveFieldGenerator::~PrimitiveFieldGenerator
~PrimitiveFieldGenerator()
Definition: csharp_primitive_field.cc:62
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
google::protobuf::compiler::csharp::PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator
~PrimitiveOneofFieldGenerator()
Definition: csharp_primitive_field.cc:248
google::protobuf::compiler::csharp::PrimitiveOneofFieldGenerator::GenerateParsingCode
virtual void GenerateParsingCode(io::Printer *printer)
Definition: csharp_primitive_field.cc:306
printer.h
google::protobuf::compiler::csharp::PrimitiveFieldGenerator::GenerateExtensionCode
virtual void GenerateExtensionCode(io::Printer *printer)
Definition: csharp_primitive_field.cc:231
google::protobuf::compiler::csharp::PrimitiveFieldGenerator::GenerateCloningCode
virtual void GenerateCloningCode(io::Printer *printer)
Definition: csharp_primitive_field.cc:220
google::protobuf::FieldDescriptor::TYPE_STRING
@ TYPE_STRING
Definition: src/google/protobuf/descriptor.h:534
code_generator.h
google::protobuf::compiler::csharp::FieldGeneratorBase::presenceIndex_
const int presenceIndex_
Definition: csharp_field_base.h:74
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::IsNullable
bool IsNullable(const FieldDescriptor *descriptor)
Definition: csharp_helpers.cc:529
descriptor.h
google::protobuf::compiler::csharp::PrimitiveFieldGenerator::GenerateCodecCode
virtual void GenerateCodecCode(io::Printer *printer)
Definition: csharp_primitive_field.cc:225
google::protobuf::compiler::csharp::WritePropertyDocComment
void WritePropertyDocComment(io::Printer *printer, const FieldDescriptor *field)
Definition: csharp_doc_comment.cc:97
google::protobuf::compiler::csharp::PrimitiveFieldGenerator::WriteToString
virtual void WriteToString(io::Printer *printer)
Definition: csharp_primitive_field.cc:214
google::protobuf::compiler::csharp::PrimitiveOneofFieldGenerator::WriteToString
virtual void WriteToString(io::Printer *printer)
Definition: csharp_primitive_field.cc:301
descriptor.pb.h
google::protobuf::compiler::csharp::FieldGeneratorBase
Definition: csharp_field_base.h:47
google::protobuf::io::Printer::Outdent
void Outdent()
Definition: printer.cc:187
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::compiler::csharp::PrimitiveFieldGenerator::WriteEquals
virtual void WriteEquals(io::Printer *printer)
Definition: csharp_primitive_field.cc:205
google::protobuf::compiler::csharp::PrimitiveFieldGenerator::is_value_type
bool is_value_type
Definition: csharp_primitive_field.h:70
google::protobuf::compiler::csharp::PrimitiveOneofFieldGenerator::GenerateMembers
virtual void GenerateMembers(io::Printer *printer)
Definition: csharp_primitive_field.cc:251


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