aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Protobuf/CompilerFactory.cs
blob: 7c0ee54a4f43391a8f40a5866631bd9b75c39024 (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
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #
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()));
        }
    }
}