aboutsummaryrefslogtreecommitdiffstats
path: root/src/Qt.DotNet.Generator/Qt/DotNet/CodeGeneration/MetaFunction.cs
blob: f9f31307646d3cf149be9da46aa93fbbeeb5c051 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/***************************************************************************************************
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using System.Collections.Concurrent;

namespace Qt.DotNet.CodeGeneration
{
    using System.Diagnostics;
    using Utils.Collections.Concurrent;

    public abstract class MetaFunction : IPrioritizable<int>
    {
        protected abstract string Eval(object src, Enum traits);
        public virtual int Priority => 0;
        protected virtual string Sanitize(string evalResult) => evalResult;

        private ConcurrentSet<string> Exceptions { get; } = new();

        protected string Unsafe(string value)
        {
            Exceptions.Add(value);
            return value;
        }

        private static ConcurrentPriorityList<MetaFunction, int> MetaFunctions { get; } = new();

        public static void Register<T>(T mFn) where T : MetaFunction
        {
            MetaFunctions.Add(mFn);
        }
        public static void Register<T>() where T : MetaFunction, new() => Register(new T());

        private static ConcurrentDictionary<
            (object Source, Enum Traits), string> Cache { get; } = new();

        private enum DefaultTrait { Default = 0 }

        public static string Evaluate<T>(T src, Enum traits = null)
            where T : class
        {
            traits ??= DefaultTrait.Default;
            if (Cache.TryGetValue((src, traits), out var cachedValue))
                return cachedValue;
            foreach (var mFn in MetaFunctions) {
                if (mFn.Eval(src, traits) is not { } value)
                    continue;
                if (!mFn.Exceptions.Contains(value))
                    value = mFn.Sanitize(value);
                Cache.TryAdd((src, traits), value);
                return value;
            }
            Debug.Assert(false, $"No MetaFunction matched for '{src}' with traits '{traits}'");
            return src.ToString();
        }

        public static Type TypeOf(string name) => Rule.TypeOf(name);
        public static Type TypeOf(Type t) => Rule.TypeOf(t);
        public static Type TypeOf<T>() => Rule.TypeOf<T>();

        protected const char Wrap = Placeholder.Wrap;
    }

    public static class MetaFunctionExtensions
    {
        public static bool HasTraits<T>(this Enum self, T traits)
            where T : struct, Enum
        {
            if (self.GetType() == typeof(T))
                return ((T)self).HasFlag(traits);
            return ((T)Enum.ToObject(typeof(T), Convert.ToInt64(self))).HasFlag(traits);
        }

        public static bool TryRegisterAsMetaFunction(this Type mFnType)
        {
            if (!mFnType.IsAssignableTo(typeof(MetaFunction)))
                return false;
            if (!mFnType.IsClass || mFnType.IsAbstract)
                return false;
            if (Activator.CreateInstance(mFnType) is not MetaFunction mFn)
                return false;
            MetaFunction.Register(mFn);
            return true;
        }

        public static string MFn(this object self, Enum traits = null)
        {
            return MetaFunction.Evaluate(self, traits);
        }
    }
}