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
{
/// <summary>
/// Factory class for protobuf compilers.
/// </summary>
public static class CompilerFactory
{
/// <summary>
/// Creates a protobuf compiler instance by the specified language.
/// </summary>
/// <param name="language">The language.</param>
/// <returns></returns>
/// <exception cref="ArgumentException">Could not locate protobuf compiler for language " + language.ToString()</exception>
public static IProtoCompiler CreateCompiler(CompilerLanguage language)
{
LogManager.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.Log(new ArgumentException("Could not locate protobuf compiler for language " + language.ToString()));
}
/// <summary>
/// Gets a collection of available compilers instance.
/// </summary>
/// <returns></returns>
public static IEnumerable<IProtoCompiler> GetAvailableCompilers()
{
List<IProtoCompiler> compilers = new List<IProtoCompiler>();
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;
}
/// <summary>
/// Creates a protobuf compiler instance by the specified language string.
/// </summary>
/// <param name="language">The language.</param>
/// <returns></returns>
/// <exception cref="ArgumentException">Could not locate protobuf compiler for language " + language.ToString()</exception>
public static IProtoCompiler CreateCompiler(String language)
{
LogManager.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.Log(new ArgumentException("Could not locate protobuf compiler for language " + language.ToString()));
}
}
}