aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Protobuf/CompilerFactory.cs
blob: d0a81a5c624f399c0f1fa8d48f4962f763bc9fc0 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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.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()));
        }

        /// <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.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()));
        }
    }
}