blob: 563c64a1560a4b92a11ea78d88b64a188e5217f0 [file] [log] [blame]
Jon Skeet68036862008-10-22 13:30:34 +01001using System.Collections.Generic;
2using Google.ProtocolBuffers.DescriptorProtos;
3using Google.ProtocolBuffers.Descriptors;
4
5namespace 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}