aboutsummaryrefslogtreecommitdiff
path: root/src/ProtoGen/SourceGeneratorBase.cs
blob: 563c64a1560a4b92a11ea78d88b64a188e5217f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Collections.Generic;
using Google.ProtocolBuffers.DescriptorProtos;
using Google.ProtocolBuffers.Descriptors;

namespace Google.ProtocolBuffers.ProtoGen {
  internal abstract class SourceGeneratorBase<T> where T : IDescriptor {

    private readonly T descriptor;

    protected SourceGeneratorBase(T descriptor) {
      this.descriptor = descriptor;
    }

    protected T Descriptor {
      get { return descriptor; }
    }

    protected string ClassAccessLevel {
      get { 
        // Default to public
        return !descriptor.File.Options.HasExtension(CSharpOptions.CSharpPublicClasses)
            || descriptor.File.Options.GetExtension(CSharpOptions.CSharpPublicClasses) ? "public" : "internal";
      }
    }

    public bool MultipleFiles {
      get { return descriptor.File.Options.GetExtension(CSharpOptions.CSharpMultipleFiles); }
    }

    protected static void WriteChildren<TChild>(TextGenerator writer, string region, IEnumerable<TChild> children) 
        where TChild : IDescriptor {
      // Copy the set of children; makes access easier
      List<TChild> copy = new List<TChild>(children);
      if (copy.Count == 0) {
        return;
      }

      if (region != null) {
        writer.WriteLine("#region {0}", region);
      }
      foreach (TChild child in children) {
        SourceGenerators.CreateGenerator(child).Generate(writer);
      }
      if (region != null) {
        writer.WriteLine("#endregion");
        writer.WriteLine();
      }
    }
  }
}