Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame^] | 1 | using System.Collections.Generic; |
| 2 | using Google.ProtocolBuffers.DescriptorProtos; |
| 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 | |
| 18 | protected string ClassAccessLevel { |
| 19 | get { |
| 20 | // Default to public |
| 21 | return !descriptor.File.Options.HasExtension(CSharpOptions.CSharpPublicClasses) |
| 22 | || descriptor.File.Options.GetExtension(CSharpOptions.CSharpPublicClasses) ? "public" : "internal"; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | public bool MultipleFiles { |
| 27 | get { return descriptor.File.Options.GetExtension(CSharpOptions.CSharpMultipleFiles); } |
| 28 | } |
| 29 | |
| 30 | protected static void WriteChildren<TChild>(TextGenerator writer, string region, IEnumerable<TChild> children) |
| 31 | where TChild : IDescriptor { |
| 32 | // Copy the set of children; makes access easier |
| 33 | List<TChild> copy = new List<TChild>(children); |
| 34 | if (copy.Count == 0) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | if (region != null) { |
| 39 | writer.WriteLine("#region {0}", region); |
| 40 | } |
| 41 | foreach (TChild child in children) { |
| 42 | SourceGenerators.CreateGenerator(child).Generate(writer); |
| 43 | } |
| 44 | if (region != null) { |
| 45 | writer.WriteLine("#endregion"); |
| 46 | writer.WriteLine(); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |