csharp_message_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 
41 
46 
47 namespace google {
48 namespace protobuf {
49 namespace compiler {
50 namespace csharp {
51 
53  int presenceIndex,
54  const Options *options)
55  : FieldGeneratorBase(descriptor, presenceIndex, options) {
56  if (!IsProto2(descriptor_->file())) {
57  variables_["has_property_check"] = name() + "_ != null";
58  variables_["has_not_property_check"] = name() + "_ == null";
59  }
60 }
61 
63 
64 }
65 
67  printer->Print(
68  variables_,
69  "private $type_name$ $name$_;\n");
72  printer->Print(
73  variables_,
74  "$access_level$ $type_name$ $property_name$ {\n"
75  " get { return $name$_; }\n"
76  " set {\n"
77  " $name$_ = value;\n"
78  " }\n"
79  "}\n");
80  if (IsProto2(descriptor_->file())) {
81  printer->Print(
82  variables_,
83  "/// <summary>Gets whether the $descriptor_name$ field is set</summary>\n");
85  printer->Print(
86  variables_,
87  "$access_level$ bool Has$property_name$ {\n"
88  " get { return $name$_ != null; }\n"
89  "}\n");
90  printer->Print(
91  variables_,
92  "/// <summary>Clears the value of the $descriptor_name$ field</summary>\n");
94  printer->Print(
95  variables_,
96  "$access_level$ void Clear$property_name$() {\n"
97  " $name$_ = null;\n"
98  "}\n");
99  }
100 }
101 
103  printer->Print(
104  variables_,
105  "if (other.$has_property_check$) {\n"
106  " if ($has_not_property_check$) {\n"
107  " $property_name$ = new $type_name$();\n"
108  " }\n"
109  " $property_name$.MergeFrom(other.$property_name$);\n"
110  "}\n");
111 }
112 
114  printer->Print(
115  variables_,
116  "if ($has_not_property_check$) {\n"
117  " $property_name$ = new $type_name$();\n"
118  "}\n");
119  if (descriptor_->type() == FieldDescriptor::Type::TYPE_MESSAGE) {
120  printer->Print(variables_, "input.ReadMessage($property_name$);\n");
121  } else {
122  printer->Print(variables_, "input.ReadGroup($property_name$);\n");
123  }
124 }
125 
127  if (descriptor_->type() == FieldDescriptor::Type::TYPE_MESSAGE) {
128  printer->Print(
129  variables_,
130  "if ($has_property_check$) {\n"
131  " output.WriteRawTag($tag_bytes$);\n"
132  " output.WriteMessage($property_name$);\n"
133  "}\n");
134  } else {
135  printer->Print(
136  variables_,
137  "if ($has_property_check$) {\n"
138  " output.WriteRawTag($tag_bytes$);\n"
139  " output.WriteGroup($property_name$);\n"
140  " output.WriteRawTag($end_tag_bytes$);\n"
141  "}\n");
142  }
143 }
144 
146  if (descriptor_->type() == FieldDescriptor::Type::TYPE_MESSAGE) {
147  printer->Print(
148  variables_,
149  "if ($has_property_check$) {\n"
150  " size += $tag_size$ + pb::CodedOutputStream.ComputeMessageSize($property_name$);\n"
151  "}\n");
152  } else {
153  printer->Print(
154  variables_,
155  "if ($has_property_check$) {\n"
156  " size += $tag_size$ + pb::CodedOutputStream.ComputeGroupSize($property_name$);\n"
157  "}\n");
158  }
159 }
160 
162  printer->Print(
163  variables_,
164  "if ($has_property_check$) hash ^= $property_name$.GetHashCode();\n");
165 }
167  printer->Print(
168  variables_,
169  "if (!object.Equals($property_name$, other.$property_name$)) return false;\n");
170 }
172  variables_["field_name"] = GetFieldName(descriptor_);
173  printer->Print(
174  variables_,
175  "PrintField(\"$field_name$\", has$property_name$, $name$_, writer);\n");
176 }
179  AddDeprecatedFlag(printer);
180  printer->Print(
181  variables_,
182  "$access_level$ static readonly pb::Extension<$extended_type$, $type_name$> $property_name$ =\n"
183  " new pb::Extension<$extended_type$, $type_name$>($number$, ");
184  GenerateCodecCode(printer);
185  printer->Print(");\n");
186 }
188  printer->Print(variables_,
189  "$name$_ = other.$has_property_check$ ? other.$name$_.Clone() : null;\n");
190 }
191 
193 }
194 
196  if (descriptor_->type() == FieldDescriptor::Type::TYPE_MESSAGE) {
197  printer->Print(
198  variables_,
199  "pb::FieldCodec.ForMessage($tag$, $type_name$.Parser)");
200  } else {
201  printer->Print(
202  variables_,
203  "pb::FieldCodec.ForGroup($tag$, $end_tag$, $type_name$.Parser)");
204  }
205 }
206 
209  int presenceIndex,
210  const Options *options)
211  : MessageFieldGenerator(descriptor, presenceIndex, options) {
213 }
214 
216 
217 }
218 
221  AddPublicMemberAttributes(printer);
222  printer->Print(
223  variables_,
224  "$access_level$ $type_name$ $property_name$ {\n"
225  " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : null; }\n"
226  " set {\n"
227  " $oneof_name$_ = value;\n"
228  " $oneof_name$Case_ = value == null ? $oneof_property_name$OneofCase.None : $oneof_property_name$OneofCase.$property_name$;\n"
229  " }\n"
230  "}\n");
231  if (IsProto2(descriptor_->file())) {
232  printer->Print(
233  variables_,
234  "/// <summary>Gets whether the \"$descriptor_name$\" field is set</summary>\n");
235  AddPublicMemberAttributes(printer);
236  printer->Print(
237  variables_,
238  "$access_level$ bool Has$property_name$ {\n"
239  " get { return $oneof_name$Case_ == $oneof_property_name$OneofCase.$property_name$; }\n"
240  "}\n");
241  printer->Print(
242  variables_,
243  "/// <summary> Clears the value of the oneof if it's currently set to \"$descriptor_name$\" </summary>\n");
244  AddPublicMemberAttributes(printer);
245  printer->Print(
246  variables_,
247  "$access_level$ void Clear$property_name$() {\n"
248  " if ($has_property_check$) {\n"
249  " Clear$oneof_property_name$();\n"
250  " }\n"
251  "}\n");
252  }
253 }
254 
256  printer->Print(variables_,
257  "if ($property_name$ == null) {\n"
258  " $property_name$ = new $type_name$();\n"
259  "}\n"
260  "$property_name$.MergeFrom(other.$property_name$);\n");
261 }
262 
264  // TODO(jonskeet): We may be able to do better than this
265  printer->Print(
266  variables_,
267  "$type_name$ subBuilder = new $type_name$();\n"
268  "if ($has_property_check$) {\n"
269  " subBuilder.MergeFrom($property_name$);\n"
270  "}\n");
271  if (descriptor_->type() == FieldDescriptor::Type::TYPE_MESSAGE) {
272  printer->Print("input.ReadMessage(subBuilder);\n");
273  } else {
274  printer->Print("input.ReadGroup(subBuilder);\n");
275  }
276  printer->Print(variables_, "$property_name$ = subBuilder;\n");
277 }
278 
280  printer->Print(
281  variables_,
282  "PrintField(\"$descriptor_name$\", $has_property_check$, $oneof_name$_, writer);\n");
283 }
284 
286  printer->Print(variables_,
287  "$property_name$ = other.$property_name$.Clone();\n");
288 }
289 
290 } // namespace csharp
291 } // namespace compiler
292 } // namespace protobuf
293 } // 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::compiler::csharp::MessageFieldGenerator::WriteEquals
virtual void WriteEquals(io::Printer *printer)
Definition: csharp_message_field.cc:166
google::protobuf::FieldDescriptor
Definition: src/google/protobuf/descriptor.h:515
wire_format_lite.h
csharp_message_field.h
google::protobuf::compiler::csharp::MessageFieldGenerator::GenerateMergingCode
virtual void GenerateMergingCode(io::Printer *printer)
Definition: csharp_message_field.cc:102
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
google::protobuf::compiler::csharp::MessageFieldGenerator::GenerateSerializationCode
virtual void GenerateSerializationCode(io::Printer *printer)
Definition: csharp_message_field.cc:126
google::protobuf::compiler::csharp::FieldGeneratorBase::SetCommonOneofFieldVariables
void SetCommonOneofFieldVariables(std::map< string, string > *variables)
Definition: csharp_field_base.cc:125
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::MessageFieldGenerator::~MessageFieldGenerator
~MessageFieldGenerator()
Definition: csharp_message_field.cc:62
google::protobuf::compiler::csharp::MessageOneofFieldGenerator::GenerateMergingCode
virtual void GenerateMergingCode(io::Printer *printer)
Definition: csharp_message_field.cc:255
google::protobuf::compiler::csharp::FieldGeneratorBase::name
std::string name()
Definition: csharp_field_base.cc:190
google::protobuf::compiler::csharp::MessageFieldGenerator::WriteToString
virtual void WriteToString(io::Printer *printer)
Definition: csharp_message_field.cc:171
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::MessageFieldGenerator
Definition: csharp_message_field.h:44
google::protobuf::compiler::csharp::Options
Definition: csharp_options.h:42
google::protobuf::compiler::csharp::MessageFieldGenerator::GenerateFreezingCode
virtual void GenerateFreezingCode(io::Printer *printer)
Definition: csharp_message_field.cc:192
google::protobuf::compiler::csharp::GetFieldName
std::string GetFieldName(const FieldDescriptor *descriptor)
Definition: csharp_helpers.cc:344
google::protobuf::compiler::csharp::MessageFieldGenerator::GenerateParsingCode
virtual void GenerateParsingCode(io::Printer *printer)
Definition: csharp_message_field.cc:113
google::protobuf::compiler::csharp::MessageFieldGenerator::GenerateSerializedSizeCode
virtual void GenerateSerializedSizeCode(io::Printer *printer)
Definition: csharp_message_field.cc:145
strutil.h
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::MessageOneofFieldGenerator::~MessageOneofFieldGenerator
~MessageOneofFieldGenerator()
Definition: csharp_message_field.cc:215
code_generator.h
google::protobuf::io::Printer
Definition: printer.h:181
google::protobuf::compiler::csharp::MessageFieldGenerator::GenerateCodecCode
virtual void GenerateCodecCode(io::Printer *printer)
Definition: csharp_message_field.cc:195
google::protobuf::compiler::csharp::MessageOneofFieldGenerator::MessageOneofFieldGenerator
MessageOneofFieldGenerator(const FieldDescriptor *descriptor, int presenceIndex, const Options *options)
Definition: csharp_message_field.cc:207
csharp_helpers.h
google::protobuf::FieldDescriptor::type
Type type() const
Definition: src/google/protobuf/descriptor.h:2052
csharp_options.h
google::protobuf::compiler::csharp::MessageFieldGenerator::GenerateExtensionCode
virtual void GenerateExtensionCode(io::Printer *printer)
Definition: csharp_message_field.cc:177
google::protobuf::compiler::csharp::MessageOneofFieldGenerator::GenerateCloningCode
virtual void GenerateCloningCode(io::Printer *printer)
Definition: csharp_message_field.cc:285
wire_format.h
descriptor.h
google::protobuf::compiler::csharp::MessageFieldGenerator::MessageFieldGenerator
MessageFieldGenerator(const FieldDescriptor *descriptor, int presenceIndex, const Options *options)
Definition: csharp_message_field.cc:52
google::protobuf::compiler::csharp::MessageFieldGenerator::GenerateCloningCode
virtual void GenerateCloningCode(io::Printer *printer)
Definition: csharp_message_field.cc:187
google::protobuf::compiler::csharp::MessageOneofFieldGenerator::WriteToString
virtual void WriteToString(io::Printer *printer)
Definition: csharp_message_field.cc:279
google::protobuf::compiler::csharp::WritePropertyDocComment
void WritePropertyDocComment(io::Printer *printer, const FieldDescriptor *field)
Definition: csharp_doc_comment.cc:97
descriptor.pb.h
google::protobuf::compiler::csharp::MessageOneofFieldGenerator::GenerateMembers
virtual void GenerateMembers(io::Printer *printer)
Definition: csharp_message_field.cc:219
google::protobuf::compiler::csharp::FieldGeneratorBase
Definition: csharp_field_base.h:47
google::protobuf::compiler::csharp::MessageFieldGenerator::GenerateMembers
virtual void GenerateMembers(io::Printer *printer)
Definition: csharp_message_field.cc:66
google::protobuf::compiler::csharp::MessageFieldGenerator::WriteHash
virtual void WriteHash(io::Printer *printer)
Definition: csharp_message_field.cc:161
google::protobuf::compiler::csharp::MessageOneofFieldGenerator::GenerateParsingCode
virtual void GenerateParsingCode(io::Printer *printer)
Definition: csharp_message_field.cc:263
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11


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