using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Tango.Logging; namespace Tango.Protobuf { /// /// Factory class for protobuf compilers. /// public static class CompilerFactory { /// /// Creates a protobuf compiler instance by the specified language. /// /// The language. /// /// Could not locate protobuf compiler for language " + language.ToString() public static IProtoCompiler CreateCompiler(CompilerLanguage language) { LogManager.Default.Log("Generating protobuf compiler for " + language.ToString() + "..."); foreach (var cType in typeof(CompilerFactory).Assembly.GetTypes().Where(x => x.IsClass && !x.IsAbstract && typeof(IProtoCompiler).IsAssignableFrom(x))) { var instance = Activator.CreateInstance(cType) as IProtoCompiler; if (instance.Language == language) return instance; } throw LogManager.Default.Log(new ArgumentException("Could not locate protobuf compiler for language " + language.ToString())); } /// /// Gets a collection of available compilers instance. /// /// public static IEnumerable GetAvailableCompilers() { List compilers = new List(); foreach (var cType in typeof(CompilerFactory).Assembly.GetTypes().Where(x => x.IsClass && !x.IsAbstract && typeof(IProtoCompiler).IsAssignableFrom(x))) { var instance = Activator.CreateInstance(cType) as IProtoCompiler; compilers.Add(instance); } return compilers; } /// /// Creates a protobuf compiler instance by the specified language string. /// /// The language. /// /// Could not locate protobuf compiler for language " + language.ToString() public static IProtoCompiler CreateCompiler(String language) { LogManager.Default.Log("Generating protobuf compiler for " + language.ToString() + "..."); CompilerLanguage lan = (CompilerLanguage)Enum.Parse(typeof(CompilerLanguage), language, true); foreach (var cType in typeof(CompilerFactory).Assembly.GetTypes().Where(x => x.IsClass && !x.IsAbstract && typeof(IProtoCompiler).IsAssignableFrom(x))) { var instance = Activator.CreateInstance(cType) as IProtoCompiler; if (instance.Language == lan) return instance; } throw LogManager.Default.Log(new ArgumentException("Could not locate protobuf compiler for language " + language.ToString())); } } }