csharp_wrapper_field.cc 8.6 KB

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