csharp_primitive_field.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/stubs/strutil.h>
  38. #include <google/protobuf/compiler/csharp/csharp_doc_comment.h>
  39. #include <google/protobuf/compiler/csharp/csharp_helpers.h>
  40. #include <google/protobuf/compiler/csharp/csharp_options.h>
  41. #include <google/protobuf/compiler/csharp/csharp_primitive_field.h>
  42. namespace google {
  43. namespace protobuf {
  44. namespace compiler {
  45. namespace csharp {
  46. PrimitiveFieldGenerator::PrimitiveFieldGenerator(
  47. const FieldDescriptor* descriptor, int fieldOrdinal, const Options *options)
  48. : FieldGeneratorBase(descriptor, fieldOrdinal, options) {
  49. // TODO(jonskeet): Make this cleaner...
  50. is_value_type = descriptor->type() != FieldDescriptor::TYPE_STRING
  51. && descriptor->type() != FieldDescriptor::TYPE_BYTES;
  52. if (!is_value_type) {
  53. variables_["has_property_check"] = variables_["property_name"] + ".Length != 0";
  54. variables_["other_has_property_check"] = "other." + variables_["property_name"] + ".Length != 0";
  55. }
  56. }
  57. PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {
  58. }
  59. void PrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) {
  60. // TODO(jonskeet): Work out whether we want to prevent the fields from ever being
  61. // null, or whether we just handle it, in the cases of bytes and string.
  62. // (Basically, should null-handling code be in the getter or the setter?)
  63. printer->Print(
  64. variables_,
  65. "private $type_name$ $name_def_message$;\n");
  66. WritePropertyDocComment(printer, descriptor_);
  67. AddPublicMemberAttributes(printer);
  68. printer->Print(
  69. variables_,
  70. "$access_level$ $type_name$ $property_name$ {\n"
  71. " get { return $name$_; }\n"
  72. " set {\n");
  73. if (is_value_type) {
  74. printer->Print(
  75. variables_,
  76. " $name$_ = value;\n");
  77. } else {
  78. printer->Print(
  79. variables_,
  80. " $name$_ = pb::ProtoPreconditions.CheckNotNull(value, \"value\");\n");
  81. }
  82. printer->Print(
  83. " }\n"
  84. "}\n");
  85. }
  86. void PrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) {
  87. printer->Print(
  88. variables_,
  89. "if ($other_has_property_check$) {\n"
  90. " $property_name$ = other.$property_name$;\n"
  91. "}\n");
  92. }
  93. void PrimitiveFieldGenerator::GenerateParsingCode(io::Printer* printer) {
  94. // Note: invoke the property setter rather than writing straight to the field,
  95. // so that we can normalize "null to empty" for strings and bytes.
  96. printer->Print(
  97. variables_,
  98. "$property_name$ = input.Read$capitalized_type_name$();\n");
  99. }
  100. void PrimitiveFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
  101. printer->Print(
  102. variables_,
  103. "if ($has_property_check$) {\n"
  104. " output.WriteRawTag($tag_bytes$);\n"
  105. " output.Write$capitalized_type_name$($property_name$);\n"
  106. "}\n");
  107. }
  108. void PrimitiveFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
  109. printer->Print(
  110. variables_,
  111. "if ($has_property_check$) {\n");
  112. printer->Indent();
  113. int fixedSize = GetFixedSize(descriptor_->type());
  114. if (fixedSize == -1) {
  115. printer->Print(
  116. variables_,
  117. "size += $tag_size$ + pb::CodedOutputStream.Compute$capitalized_type_name$Size($property_name$);\n");
  118. } else {
  119. printer->Print(
  120. "size += $tag_size$ + $fixed_size$;\n",
  121. "fixed_size", SimpleItoa(fixedSize),
  122. "tag_size", variables_["tag_size"]);
  123. }
  124. printer->Outdent();
  125. printer->Print("}\n");
  126. }
  127. void PrimitiveFieldGenerator::WriteHash(io::Printer* printer) {
  128. const char *text = "if ($has_property_check$) hash ^= $property_name$.GetHashCode();\n";
  129. if (descriptor_->type() == FieldDescriptor::TYPE_FLOAT) {
  130. text = "if ($has_property_check$) hash ^= pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.GetHashCode($property_name$);\n";
  131. } else if (descriptor_->type() == FieldDescriptor::TYPE_DOUBLE) {
  132. text = "if ($has_property_check$) hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode($property_name$);\n";
  133. }
  134. printer->Print(variables_, text);
  135. }
  136. void PrimitiveFieldGenerator::WriteEquals(io::Printer* printer) {
  137. const char *text = "if ($property_name$ != other.$property_name$) return false;\n";
  138. if (descriptor_->type() == FieldDescriptor::TYPE_FLOAT) {
  139. text = "if (!pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.Equals($property_name$, other.$property_name$)) return false;\n";
  140. } else if (descriptor_->type() == FieldDescriptor::TYPE_DOUBLE) {
  141. text = "if (!pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.Equals($property_name$, other.$property_name$)) return false;\n";
  142. }
  143. printer->Print(variables_, text);
  144. }
  145. void PrimitiveFieldGenerator::WriteToString(io::Printer* printer) {
  146. printer->Print(
  147. variables_,
  148. "PrintField(\"$descriptor_name$\", $has_property_check$, $property_name$, writer);\n");
  149. }
  150. void PrimitiveFieldGenerator::GenerateCloningCode(io::Printer* printer) {
  151. printer->Print(variables_,
  152. "$name$_ = other.$name$_;\n");
  153. }
  154. void PrimitiveFieldGenerator::GenerateCodecCode(io::Printer* printer) {
  155. printer->Print(
  156. variables_,
  157. "pb::FieldCodec.For$capitalized_type_name$($tag$)");
  158. }
  159. PrimitiveOneofFieldGenerator::PrimitiveOneofFieldGenerator(
  160. const FieldDescriptor* descriptor, int fieldOrdinal, const Options *options)
  161. : PrimitiveFieldGenerator(descriptor, fieldOrdinal, options) {
  162. SetCommonOneofFieldVariables(&variables_);
  163. }
  164. PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() {
  165. }
  166. void PrimitiveOneofFieldGenerator::GenerateMembers(io::Printer* printer) {
  167. WritePropertyDocComment(printer, descriptor_);
  168. AddPublicMemberAttributes(printer);
  169. printer->Print(
  170. variables_,
  171. "$access_level$ $type_name$ $property_name$ {\n"
  172. " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : $default_value$; }\n"
  173. " set {\n");
  174. if (is_value_type) {
  175. printer->Print(
  176. variables_,
  177. " $oneof_name$_ = value;\n");
  178. } else {
  179. printer->Print(
  180. variables_,
  181. " $oneof_name$_ = pb::ProtoPreconditions.CheckNotNull(value, \"value\");\n");
  182. }
  183. printer->Print(
  184. variables_,
  185. " $oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
  186. " }\n"
  187. "}\n");
  188. }
  189. void PrimitiveOneofFieldGenerator::GenerateMergingCode(io::Printer* printer) {
  190. printer->Print(variables_, "$property_name$ = other.$property_name$;\n");
  191. }
  192. void PrimitiveOneofFieldGenerator::WriteToString(io::Printer* printer) {
  193. printer->Print(variables_,
  194. "PrintField(\"$descriptor_name$\", $has_property_check$, $oneof_name$_, writer);\n");
  195. }
  196. void PrimitiveOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) {
  197. printer->Print(
  198. variables_,
  199. "$property_name$ = input.Read$capitalized_type_name$();\n");
  200. }
  201. void PrimitiveOneofFieldGenerator::GenerateCloningCode(io::Printer* printer) {
  202. printer->Print(variables_,
  203. "$property_name$ = other.$property_name$;\n");
  204. }
  205. } // namespace csharp
  206. } // namespace compiler
  207. } // namespace protobuf
  208. } // namespace google