aboutsummaryrefslogtreecommitdiffstats
path: root/utils/LazyFactory/LazyFactory.cs
blob: ab780317105d7cb18c52b2c70d772d4d65cfd4b5 (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
/***************************************************************************************************
 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;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;

namespace Qt.DotNet.Utils
{
    public class LazyFactory : INotifyPropertyChanged
    {
        private ConcurrentDictionary<(object, PropertyInfo), object> Objs { get; } = new();

        public event PropertyChangedEventHandler PropertyChanged;

        private readonly object criticalSection = new();

        public T Get<T>(Expression<Func<T>> propertyRef, Func<T> initFunc = null)
        {
            if (propertyRef?.Body is not MemberExpression lazyPropertyExpr)
                throw new ArgumentException("Expected member lambda", nameof(propertyRef));
            if (lazyPropertyExpr?.Member is not PropertyInfo lazyProperty)
                throw new ArgumentException("Invalid property reference", nameof(propertyRef));
            object owner = lazyProperty.DeclaringType;
            if (lazyPropertyExpr.Expression is ConstantExpression { Value: not null } lazyThis)
                owner = lazyThis.Value;
            lock (criticalSection) {
                if (initFunc != null)
                    return (T)Objs.GetOrAdd((owner, lazyProperty), k => initFunc());
                else
                    return (T)Objs.GetOrAdd((owner, lazyProperty), k => default(T));
            }
        }

        public void Set<T>(Expression<Func<T>> propertyRef, T value)
        {
            if (propertyRef?.Body is not MemberExpression lazyPropertyExpr)
                throw new ArgumentException("Expected member lambda", nameof(propertyRef));
            if (lazyPropertyExpr?.Member is not PropertyInfo lazyProperty)
                throw new ArgumentException("Invalid property reference", nameof(propertyRef));
            object owner = lazyProperty.DeclaringType;
            if (lazyPropertyExpr.Expression is ConstantExpression { Value: not null } lazyThis)
                owner = lazyThis.Value;
            if (Objs.TryGetValue((owner, lazyProperty), out var current) && current.Equals(value))
                return;
            Objs[(owner, lazyProperty)] = value;
            PropertyChanged?.Invoke(owner, new PropertyChangedEventArgs(lazyProperty.Name));
        }
    }
}