SourceGeneratorBase.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.Collections.Generic;
  2. using Google.ProtocolBuffers.DescriptorProtos;
  3. using Google.ProtocolBuffers.Descriptors;
  4. namespace Google.ProtocolBuffers.ProtoGen {
  5. internal abstract class SourceGeneratorBase<T> where T : IDescriptor {
  6. private readonly T descriptor;
  7. protected SourceGeneratorBase(T descriptor) {
  8. this.descriptor = descriptor;
  9. }
  10. protected T Descriptor {
  11. get { return descriptor; }
  12. }
  13. protected string ClassAccessLevel {
  14. get {
  15. // Default to public
  16. return !descriptor.File.Options.HasExtension(CSharpOptions.CSharpPublicClasses)
  17. || descriptor.File.Options.GetExtension(CSharpOptions.CSharpPublicClasses) ? "public" : "internal";
  18. }
  19. }
  20. public bool MultipleFiles {
  21. get { return descriptor.File.Options.GetExtension(CSharpOptions.CSharpMultipleFiles); }
  22. }
  23. protected static void WriteChildren<TChild>(TextGenerator writer, string region, IEnumerable<TChild> children)
  24. where TChild : IDescriptor {
  25. // Copy the set of children; makes access easier
  26. List<TChild> copy = new List<TChild>(children);
  27. if (copy.Count == 0) {
  28. return;
  29. }
  30. if (region != null) {
  31. writer.WriteLine("#region {0}", region);
  32. }
  33. foreach (TChild child in children) {
  34. SourceGenerators.CreateGenerator(child).Generate(writer);
  35. }
  36. if (region != null) {
  37. writer.WriteLine("#endregion");
  38. writer.WriteLine();
  39. }
  40. }
  41. }
  42. }