Jon Skeet | d6343be | 2008-11-12 23:39:44 +0000 | [diff] [blame] | 1 | using System; |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 2 | using System.Collections.Generic; |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 3 | using Google.ProtocolBuffers.Descriptors; |
| 4 | |
| 5 | namespace Google.ProtocolBuffers.ProtoGen { |
| 6 | internal abstract class SourceGeneratorBase<T> where T : IDescriptor { |
| 7 | |
| 8 | private readonly T descriptor; |
| 9 | |
| 10 | protected SourceGeneratorBase(T descriptor) { |
| 11 | this.descriptor = descriptor; |
| 12 | } |
| 13 | |
| 14 | protected T Descriptor { |
| 15 | get { return descriptor; } |
| 16 | } |
| 17 | |
Jon Skeet | d6343be | 2008-11-12 23:39:44 +0000 | [diff] [blame] | 18 | internal static string GetClassName(IDescriptor descriptor) { |
| 19 | return ToCSharpName(descriptor.FullName, descriptor.File); |
| 20 | } |
| 21 | |
| 22 | // Groups are hacky: The name of the field is just the lower-cased name |
| 23 | // of the group type. In C#, though, we would like to retain the original |
| 24 | // capitalization of the type name. |
| 25 | internal static string GetFieldName(FieldDescriptor descriptor) { |
| 26 | if (descriptor.FieldType == FieldType.Group) { |
| 27 | return descriptor.MessageType.Name; |
| 28 | } else { |
| 29 | return descriptor.Name; |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
Jon Skeet | 7ee85c4 | 2009-05-28 21:11:15 +0100 | [diff] [blame] | 33 | internal static string GetFieldConstantName(FieldDescriptor field) { |
Jon Skeet | df67f14 | 2009-06-05 19:29:36 +0100 | [diff] [blame^] | 34 | return field.CSharpOptions.PropertyName + "FieldNumber"; |
Jon Skeet | 7ee85c4 | 2009-05-28 21:11:15 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Jon Skeet | d6343be | 2008-11-12 23:39:44 +0000 | [diff] [blame] | 37 | private static string ToCSharpName(string name, FileDescriptor file) { |
| 38 | string result = file.CSharpOptions.Namespace; |
| 39 | if (file.CSharpOptions.NestClasses) { |
| 40 | if (result != "") { |
| 41 | result += "."; |
| 42 | } |
| 43 | result += file.CSharpOptions.UmbrellaClassname; |
| 44 | } |
| 45 | if (result != "") { |
| 46 | result += '.'; |
| 47 | } |
| 48 | string classname; |
| 49 | if (file.Package == "") { |
| 50 | classname = name; |
| 51 | } else { |
| 52 | // Strip the proto package from full_name since we've replaced it with |
| 53 | // the C# namespace. |
| 54 | classname = name.Substring(file.Package.Length + 1); |
| 55 | } |
| 56 | result += classname.Replace(".", ".Types."); |
| 57 | return "global::" + result; |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Jon Skeet | d6343be | 2008-11-12 23:39:44 +0000 | [diff] [blame] | 60 | protected string ClassAccessLevel { |
| 61 | get { |
| 62 | return descriptor.File.CSharpOptions.PublicClasses ? "public" : "internal"; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | protected void WriteChildren<TChild>(TextGenerator writer, string region, IEnumerable<TChild> children) |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 67 | where TChild : IDescriptor { |
| 68 | // Copy the set of children; makes access easier |
| 69 | List<TChild> copy = new List<TChild>(children); |
| 70 | if (copy.Count == 0) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | if (region != null) { |
| 75 | writer.WriteLine("#region {0}", region); |
| 76 | } |
| 77 | foreach (TChild child in children) { |
| 78 | SourceGenerators.CreateGenerator(child).Generate(writer); |
| 79 | } |
| 80 | if (region != null) { |
| 81 | writer.WriteLine("#endregion"); |
| 82 | writer.WriteLine(); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |