| OLD | NEW |
| (Empty) | |
| 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 |
| 33 #include <google/protobuf/compiler/code_generator.h> |
| 34 #include <google/protobuf/compiler/plugin.h> |
| 35 #include <google/protobuf/descriptor.h> |
| 36 #include <google/protobuf/descriptor.pb.h> |
| 37 #include <google/protobuf/io/printer.h> |
| 38 #include <google/protobuf/io/zero_copy_stream.h> |
| 39 |
| 40 #include <google/protobuf/compiler/csharp/csharp_doc_comment.h> |
| 41 #include <google/protobuf/compiler/csharp/csharp_helpers.h> |
| 42 #include <google/protobuf/compiler/csharp/csharp_wrapper_field.h> |
| 43 |
| 44 namespace google { |
| 45 namespace protobuf { |
| 46 namespace compiler { |
| 47 namespace csharp { |
| 48 |
| 49 WrapperFieldGenerator::WrapperFieldGenerator(const FieldDescriptor* descriptor, |
| 50 int fieldOrdinal) |
| 51 : FieldGeneratorBase(descriptor, fieldOrdinal) { |
| 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 |
| 62 WrapperFieldGenerator::~WrapperFieldGenerator() { |
| 63 } |
| 64 |
| 65 void WrapperFieldGenerator::GenerateMembers(io::Printer* printer) { |
| 66 printer->Print( |
| 67 variables_, |
| 68 "private static readonly pb::FieldCodec<$type_name$> _single_$name$_code
c = "); |
| 69 GenerateCodecCode(printer); |
| 70 printer->Print( |
| 71 variables_, |
| 72 ";\n" |
| 73 "private $type_name$ $name$_;\n"); |
| 74 WritePropertyDocComment(printer, descriptor_); |
| 75 AddDeprecatedFlag(printer); |
| 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"); |
| 84 } |
| 85 |
| 86 void WrapperFieldGenerator::GenerateMergingCode(io::Printer* printer) { |
| 87 printer->Print( |
| 88 variables_, |
| 89 "if (other.$has_property_check$) {\n" |
| 90 " if ($has_not_property_check$ || other.$property_name$ != $default_value$)
{\n" |
| 91 " $property_name$ = other.$property_name$;\n" |
| 92 " }\n" |
| 93 "}\n"); |
| 94 } |
| 95 |
| 96 void WrapperFieldGenerator::GenerateParsingCode(io::Printer* printer) { |
| 97 printer->Print( |
| 98 variables_, |
| 99 "$type_name$ value = _single_$name$_codec.Read(input);\n" |
| 100 "if ($has_not_property_check$ || value != $default_value$) {\n" |
| 101 " $property_name$ = value;\n" |
| 102 "}\n"); |
| 103 } |
| 104 |
| 105 void WrapperFieldGenerator::GenerateSerializationCode(io::Printer* printer) { |
| 106 printer->Print( |
| 107 variables_, |
| 108 "if ($has_property_check$) {\n" |
| 109 " _single_$name$_codec.WriteTagAndValue(output, $property_name$);\n" |
| 110 "}\n"); |
| 111 } |
| 112 |
| 113 void WrapperFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { |
| 114 printer->Print( |
| 115 variables_, |
| 116 "if ($has_property_check$) {\n" |
| 117 " size += _single_$name$_codec.CalculateSizeWithTag($property_name$);\n" |
| 118 "}\n"); |
| 119 } |
| 120 |
| 121 void WrapperFieldGenerator::WriteHash(io::Printer* printer) { |
| 122 printer->Print( |
| 123 variables_, |
| 124 "if ($has_property_check$) hash ^= $property_name$.GetHashCode();\n"); |
| 125 } |
| 126 |
| 127 void WrapperFieldGenerator::WriteEquals(io::Printer* printer) { |
| 128 printer->Print( |
| 129 variables_, |
| 130 "if ($property_name$ != other.$property_name$) return false;\n"); |
| 131 } |
| 132 |
| 133 void WrapperFieldGenerator::WriteToString(io::Printer* printer) { |
| 134 // TODO: Implement if we ever actually need it... |
| 135 } |
| 136 |
| 137 void WrapperFieldGenerator::GenerateCloningCode(io::Printer* printer) { |
| 138 printer->Print(variables_, |
| 139 "$property_name$ = other.$property_name$;\n"); |
| 140 } |
| 141 |
| 142 void WrapperFieldGenerator::GenerateCodecCode(io::Printer* printer) { |
| 143 if (is_value_type) { |
| 144 printer->Print( |
| 145 variables_, |
| 146 "pb::FieldCodec.ForStructWrapper<$nonnullable_type_name$>($tag$)"); |
| 147 } else { |
| 148 printer->Print( |
| 149 variables_, |
| 150 "pb::FieldCodec.ForClassWrapper<$type_name$>($tag$)"); |
| 151 } |
| 152 } |
| 153 |
| 154 WrapperOneofFieldGenerator::WrapperOneofFieldGenerator(const FieldDescriptor* de
scriptor, |
| 155 int fieldOrdinal) |
| 156 : WrapperFieldGenerator(descriptor, fieldOrdinal) { |
| 157 SetCommonOneofFieldVariables(&variables_); |
| 158 } |
| 159 |
| 160 WrapperOneofFieldGenerator::~WrapperOneofFieldGenerator() { |
| 161 } |
| 162 |
| 163 void WrapperOneofFieldGenerator::GenerateMembers(io::Printer* printer) { |
| 164 // Note: deliberately _oneof_$name$_codec, not _$oneof_name$_codec... we have
one codec per field. |
| 165 printer->Print( |
| 166 variables_, |
| 167 "private static readonly pb::FieldCodec<$type_name$> _oneof_$name$_codec
= "); |
| 168 GenerateCodecCode(printer); |
| 169 printer->Print(";\n"); |
| 170 WritePropertyDocComment(printer, descriptor_); |
| 171 AddDeprecatedFlag(printer); |
| 172 printer->Print( |
| 173 variables_, |
| 174 "$access_level$ $type_name$ $property_name$ {\n" |
| 175 " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : ($type_
name$) null; }\n" |
| 176 " set {\n" |
| 177 " $oneof_name$_ = value;\n" |
| 178 " $oneof_name$Case_ = value == null ? $oneof_property_name$OneofCase.None
: $oneof_property_name$OneofCase.$property_name$;\n" |
| 179 " }\n" |
| 180 "}\n"); |
| 181 } |
| 182 |
| 183 void WrapperOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) { |
| 184 printer->Print( |
| 185 variables_, |
| 186 "$property_name$ = _oneof_$name$_codec.Read(input);\n"); |
| 187 } |
| 188 |
| 189 void WrapperOneofFieldGenerator::GenerateSerializationCode(io::Printer* printer)
{ |
| 190 // TODO: I suspect this is wrong... |
| 191 printer->Print( |
| 192 variables_, |
| 193 "if ($has_property_check$) {\n" |
| 194 " _oneof_$name$_codec.WriteTagAndValue(output, ($type_name$) $oneof_name$_)
;\n" |
| 195 "}\n"); |
| 196 } |
| 197 |
| 198 void WrapperOneofFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer
) { |
| 199 // TODO: I suspect this is wrong... |
| 200 printer->Print( |
| 201 variables_, |
| 202 "if ($has_property_check$) {\n" |
| 203 " size += _oneof_$name$_codec.CalculateSizeWithTag($property_name$);\n" |
| 204 "}\n"); |
| 205 } |
| 206 |
| 207 } // namespace csharp |
| 208 } // namespace compiler |
| 209 } // namespace protobuf |
| 210 } // namespace google |
| OLD | NEW |