|
| 1 | +using CppSharp.AST; |
| 2 | +using CppSharp.Generators.C; |
| 3 | +using CppSharp.Generators.CLI; |
| 4 | +using CppSharp.Generators.Cpp; |
| 5 | +using CppSharp.Generators.CSharp; |
| 6 | +using CppSharp.Generators.Emscripten; |
| 7 | +using CppSharp.Generators.TS; |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Linq; |
| 11 | + |
| 12 | +namespace CppSharp.Generators |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Kinds of language generators. |
| 16 | + /// </summary> |
| 17 | + public class GeneratorKind : IEquatable<GeneratorKind> |
| 18 | + { |
| 19 | + public static readonly HashSet<GeneratorKind> Registered = new(); |
| 20 | + |
| 21 | + public string ID { get; } |
| 22 | + public string Name { get; } |
| 23 | + public System.Type Type { get; } |
| 24 | + public string[] CLIOptions { get; } |
| 25 | + |
| 26 | + public GeneratorKind(string id, string name, System.Type type, string[] cLIOptions = null) |
| 27 | + { |
| 28 | + if (Registered.Any(kind => kind.ID == id)) |
| 29 | + { |
| 30 | + throw new Exception($"GeneratorKind has an already registered ID: {ID}"); |
| 31 | + } |
| 32 | + ID = id; |
| 33 | + Name = name; |
| 34 | + Type = type; |
| 35 | + CLIOptions = cLIOptions; |
| 36 | + Registered.Add(this); |
| 37 | + } |
| 38 | + |
| 39 | + public Generator CreateGenerator(BindingContext context) |
| 40 | + { |
| 41 | + return (Generator)Activator.CreateInstance(Type, context); |
| 42 | + } |
| 43 | + |
| 44 | + public bool IsCLIOptionMatch(string cliOption) |
| 45 | + { |
| 46 | + if (CLIOptions == null) |
| 47 | + { |
| 48 | + return false; |
| 49 | + } |
| 50 | + return CLIOptions.Any(cliOption.Contains); |
| 51 | + } |
| 52 | + |
| 53 | + public static bool operator ==(GeneratorKind obj1, GeneratorKind obj2) |
| 54 | + { |
| 55 | + if (ReferenceEquals(obj1, obj2)) |
| 56 | + { |
| 57 | + return true; |
| 58 | + } |
| 59 | + if (obj1 is null) |
| 60 | + { |
| 61 | + return false; |
| 62 | + } |
| 63 | + if (obj2 is null) |
| 64 | + { |
| 65 | + return false; |
| 66 | + } |
| 67 | + return obj1.Equals(obj2); |
| 68 | + } |
| 69 | + |
| 70 | + public static bool operator !=(GeneratorKind obj1, GeneratorKind obj2) => !(obj1 == obj2); |
| 71 | + |
| 72 | + public bool Equals(GeneratorKind other) |
| 73 | + { |
| 74 | + if (other is null) |
| 75 | + { |
| 76 | + return false; |
| 77 | + } |
| 78 | + if (ReferenceEquals(this, other)) |
| 79 | + { |
| 80 | + return true; |
| 81 | + } |
| 82 | + return ID.Equals(other.ID); |
| 83 | + } |
| 84 | + |
| 85 | + public override bool Equals(object obj) => Equals(obj as GeneratorKind); |
| 86 | + |
| 87 | + public override int GetHashCode() |
| 88 | + { |
| 89 | + unchecked |
| 90 | + { |
| 91 | + return ID.GetHashCode(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public const string CLI_ID = "CLI"; |
| 96 | + public static readonly GeneratorKind CLI = new(CLI_ID, "C++/CLI", typeof(CLIGenerator), new[] { "cli" }); |
| 97 | + |
| 98 | + public const string CSharp_ID = "CSharp"; |
| 99 | + public static readonly GeneratorKind CSharp = new(CSharp_ID, "C#", typeof(CSharpGenerator), new[] { "csharp" }); |
| 100 | + |
| 101 | + public const string C_ID = "C"; |
| 102 | + public static readonly GeneratorKind C = new(C_ID, "C", typeof(CGenerator), new[] { "c" }); |
| 103 | + |
| 104 | + public const string CPlusPlus_ID = "CPlusPlus"; |
| 105 | + public static readonly GeneratorKind CPlusPlus = new(CPlusPlus_ID, "CPlusPlus", typeof(CppGenerator), new[] { "cpp" }); |
| 106 | + |
| 107 | + public const string Emscripten_ID = "Emscripten"; |
| 108 | + public static readonly GeneratorKind Emscripten = new(Emscripten_ID, "Emscripten", typeof(EmscriptenGenerator), new[] { "emscripten" }); |
| 109 | + |
| 110 | + public const string ObjectiveC_ID = "ObjectiveC"; |
| 111 | + public static readonly GeneratorKind ObjectiveC = new(ObjectiveC_ID, "ObjectiveC", typeof(NotImplementedGenerator)); |
| 112 | + |
| 113 | + public const string Java_ID = "Java"; |
| 114 | + public static readonly GeneratorKind Java = new(Java_ID, "Java", typeof(NotImplementedGenerator)); |
| 115 | + |
| 116 | + public const string Swift_ID = "Swift"; |
| 117 | + public static readonly GeneratorKind Swift = new(Swift_ID, "Swift", typeof(NotImplementedGenerator)); |
| 118 | + |
| 119 | + public const string QuickJS_ID = "QuickJS"; |
| 120 | + public static readonly GeneratorKind QuickJS = new(QuickJS_ID, "QuickJS", typeof(QuickJSGenerator), new[] { "qjs" }); |
| 121 | + |
| 122 | + public const string NAPI_ID = "NAPI"; |
| 123 | + public static readonly GeneratorKind NAPI = new(NAPI_ID, "N-API", typeof(NAPIGenerator), new[] { "napi" }); |
| 124 | + |
| 125 | + public const string TypeScript_ID = "TypeScript"; |
| 126 | + public static readonly GeneratorKind TypeScript = new(TypeScript_ID, "TypeScript", typeof(TSGenerator), new[] { "ts", "typescript" }); |
| 127 | + } |
| 128 | + |
| 129 | + public class NotImplementedGenerator : Generator |
| 130 | + { |
| 131 | + public NotImplementedGenerator(BindingContext context) : base(context) |
| 132 | + { |
| 133 | + throw new NotImplementedException(); |
| 134 | + } |
| 135 | + |
| 136 | + public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units) |
| 137 | + { |
| 138 | + throw new NotImplementedException(); |
| 139 | + } |
| 140 | + |
| 141 | + public override bool SetupPasses() |
| 142 | + { |
| 143 | + throw new NotImplementedException(); |
| 144 | + } |
| 145 | + |
| 146 | + protected override string TypePrinterDelegate(CppSharp.AST.Type type) |
| 147 | + { |
| 148 | + throw new NotImplementedException(); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments