SourceGenerators.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://github.com/jskeet/dotnet-protobufs/
  4. // Original C++/Java/Python code:
  5. // http://code.google.com/p/protobuf/
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. //
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Google Inc. nor the names of its
  18. // contributors may be used to endorse or promote products derived from
  19. // this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. using System;
  33. using System.Collections.Generic;
  34. using Google.ProtocolBuffers.Descriptors;
  35. namespace Google.ProtocolBuffers.ProtoGen {
  36. internal static class SourceGenerators {
  37. private static readonly Dictionary<Type, Func<IDescriptor, ISourceGenerator>> GeneratorFactories = new Dictionary<Type, Func<IDescriptor, ISourceGenerator>> {
  38. { typeof(FileDescriptor), descriptor => new UmbrellaClassGenerator((FileDescriptor) descriptor) },
  39. { typeof(EnumDescriptor), descriptor => new EnumGenerator((EnumDescriptor) descriptor) },
  40. { typeof(ServiceDescriptor), descriptor => new ServiceGenerator((ServiceDescriptor) descriptor) },
  41. { typeof(MessageDescriptor), descriptor => new MessageGenerator((MessageDescriptor) descriptor) },
  42. // For other fields, we have IFieldSourceGenerators.
  43. { typeof(FieldDescriptor), descriptor => new ExtensionGenerator((FieldDescriptor) descriptor) }
  44. };
  45. public static IFieldSourceGenerator CreateFieldGenerator(FieldDescriptor field) {
  46. switch (field.MappedType) {
  47. case MappedType.Message :
  48. return field.IsRepeated
  49. ? (IFieldSourceGenerator) new RepeatedMessageFieldGenerator(field)
  50. : new MessageFieldGenerator(field);
  51. case MappedType.Enum:
  52. return field.IsRepeated
  53. ? (IFieldSourceGenerator)new RepeatedEnumFieldGenerator(field)
  54. : new EnumFieldGenerator(field);
  55. default:
  56. return field.IsRepeated
  57. ? (IFieldSourceGenerator)new RepeatedPrimitiveFieldGenerator(field)
  58. : new PrimitiveFieldGenerator(field);
  59. }
  60. }
  61. public static ISourceGenerator CreateGenerator<T>(T descriptor) where T : IDescriptor {
  62. Func<IDescriptor, ISourceGenerator> factory;
  63. if (!GeneratorFactories.TryGetValue(typeof(T), out factory)) {
  64. throw new ArgumentException("No generator registered for " + typeof(T).Name);
  65. }
  66. return factory(descriptor);
  67. }
  68. }
  69. }