PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ProtoGen/ExtensionGenerator.cs

https://code.google.com/p/protobuf-csharp-port/
C# | 183 lines | 139 code | 13 blank | 31 comment | 24 complexity | cebfa334c64e67b367238b12e2bc6f2f MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, GPL-2.0
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using System;
  35. using Google.ProtocolBuffers.Descriptors;
  36. namespace Google.ProtocolBuffers.ProtoGen
  37. {
  38. internal class ExtensionGenerator : FieldGeneratorBase, ISourceGenerator
  39. {
  40. private readonly string extends;
  41. private readonly string scope;
  42. private readonly string type;
  43. private readonly string name;
  44. internal ExtensionGenerator(FieldDescriptor descriptor)
  45. : base(descriptor, 0)
  46. {
  47. if (Descriptor.ExtensionScope != null)
  48. {
  49. scope = GetClassName(Descriptor.ExtensionScope);
  50. }
  51. else
  52. {
  53. scope = DescriptorUtil.GetFullUmbrellaClassName(Descriptor.File);
  54. }
  55. switch (Descriptor.MappedType)
  56. {
  57. case MappedType.Message:
  58. type = GetClassName(Descriptor.MessageType);
  59. break;
  60. case MappedType.Enum:
  61. type = GetClassName(Descriptor.EnumType);
  62. break;
  63. default:
  64. type = DescriptorUtil.GetMappedTypeName(Descriptor.MappedType);
  65. break;
  66. }
  67. extends = GetClassName(Descriptor.ContainingType);
  68. name = Descriptor.CSharpOptions.PropertyName;
  69. }
  70. public void Generate(TextGenerator writer)
  71. {
  72. if (Descriptor.File.CSharpOptions.ClsCompliance && GetFieldConstantName(Descriptor).StartsWith("_"))
  73. {
  74. writer.WriteLine("[global::System.CLSCompliant(false)]");
  75. }
  76. writer.WriteLine("public const int {0} = {1};", GetFieldConstantName(Descriptor), Descriptor.FieldNumber);
  77. if (UseLiteRuntime)
  78. {
  79. if (Descriptor.MappedType == MappedType.Message && Descriptor.MessageType.Options.MessageSetWireFormat)
  80. {
  81. throw new ArgumentException(
  82. "option message_set_wire_format = true; is not supported in Lite runtime extensions.");
  83. }
  84. if (!Descriptor.IsCLSCompliant && Descriptor.File.CSharpOptions.ClsCompliance)
  85. {
  86. writer.WriteLine("[global::System.CLSCompliant(false)]");
  87. }
  88. writer.WriteLine("{0} static pb::{4}<{1}, {2}> {3};", ClassAccessLevel, extends, type, name,
  89. Descriptor.IsRepeated ? "GeneratedRepeatExtensionLite" : "GeneratedExtensionLite");
  90. }
  91. else if (Descriptor.IsRepeated)
  92. {
  93. if (!Descriptor.IsCLSCompliant && Descriptor.File.CSharpOptions.ClsCompliance)
  94. {
  95. writer.WriteLine("[global::System.CLSCompliant(false)]");
  96. }
  97. writer.WriteLine("{0} static pb::GeneratedExtensionBase<scg::IList<{1}>> {2};", ClassAccessLevel, type,
  98. name);
  99. }
  100. else
  101. {
  102. if (!Descriptor.IsCLSCompliant && Descriptor.File.CSharpOptions.ClsCompliance)
  103. {
  104. writer.WriteLine("[global::System.CLSCompliant(false)]");
  105. }
  106. writer.WriteLine("{0} static pb::GeneratedExtensionBase<{1}> {2};", ClassAccessLevel, type, name);
  107. }
  108. }
  109. internal void GenerateStaticVariableInitializers(TextGenerator writer)
  110. {
  111. if (UseLiteRuntime)
  112. {
  113. writer.WriteLine("{0}.{1} = ", scope, name);
  114. writer.Indent();
  115. writer.WriteLine("new pb::{0}<{1}, {2}>(",
  116. Descriptor.IsRepeated ? "GeneratedRepeatExtensionLite" : "GeneratedExtensionLite",
  117. extends, type);
  118. writer.Indent();
  119. writer.WriteLine("\"{0}\",", Descriptor.FullName);
  120. writer.WriteLine("{0}.DefaultInstance,", extends);
  121. if (!Descriptor.IsRepeated)
  122. {
  123. writer.WriteLine("{0},",
  124. Descriptor.HasDefaultValue
  125. ? DefaultValue
  126. : IsNullableType ? "null" : "default(" + type + ")");
  127. }
  128. writer.WriteLine("{0},",
  129. (Descriptor.MappedType == MappedType.Message) ? type + ".DefaultInstance" : "null");
  130. writer.WriteLine("{0},",
  131. (Descriptor.MappedType == MappedType.Enum) ? "new EnumLiteMap<" + type + ">()" : "null");
  132. writer.WriteLine("{0}.{1}FieldNumber,", scope, name);
  133. writer.Write("pbd::FieldType.{0}", Descriptor.FieldType);
  134. if (Descriptor.IsRepeated)
  135. {
  136. writer.WriteLine(",");
  137. writer.Write(Descriptor.IsPacked ? "true" : "false");
  138. }
  139. writer.Outdent();
  140. writer.WriteLine(");");
  141. writer.Outdent();
  142. }
  143. else if (Descriptor.IsRepeated)
  144. {
  145. writer.WriteLine(
  146. "{0}.{1} = pb::GeneratedRepeatExtension<{2}>.CreateInstance({0}.Descriptor.Extensions[{3}]);", scope,
  147. name, type, Descriptor.Index);
  148. }
  149. else
  150. {
  151. writer.WriteLine(
  152. "{0}.{1} = pb::GeneratedSingleExtension<{2}>.CreateInstance({0}.Descriptor.Extensions[{3}]);", scope,
  153. name, type, Descriptor.Index);
  154. }
  155. }
  156. internal void GenerateExtensionRegistrationCode(TextGenerator writer)
  157. {
  158. writer.WriteLine("registry.Add({0}.{1});", scope, name);
  159. }
  160. public override void WriteHash(TextGenerator writer)
  161. {
  162. }
  163. public override void WriteEquals(TextGenerator writer)
  164. {
  165. }
  166. public override void WriteToString(TextGenerator writer)
  167. {
  168. }
  169. }
  170. }