aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Core
diff options
context:
space:
mode:
authorMiguel Costa <miguel.costa@qt.io>2022-02-24 12:40:57 +0100
committerKarsten Heimrich <karsten.heimrich@qt.io>2022-03-01 12:15:43 +0000
commiteecaac55920f16f8855e7230bbc79f13530a19d0 (patch)
treef3400c92207964656bcc9e5bfed7bfe215b74011 /QtVsTools.Core
parent2462e97dcd0ddaab4a80aae5f7c5ed81d6562332 (diff)
Add LazyFactory class
This class eliminates the need for auxiliary variables (prefixed with '_') associated to lazy-initialized properties. Change-Id: Ie2899151f4c70a3532923dcacb1debcd01509c97 Reviewed-by: Karsten Heimrich <karsten.heimrich@qt.io>
Diffstat (limited to 'QtVsTools.Core')
-rw-r--r--QtVsTools.Core/Common/EnumExt.cs7
-rw-r--r--QtVsTools.Core/Common/LazyFactory.cs57
-rw-r--r--QtVsTools.Core/QtVsTools.Core.csproj1
3 files changed, 62 insertions, 3 deletions
diff --git a/QtVsTools.Core/Common/EnumExt.cs b/QtVsTools.Core/Common/EnumExt.cs
index d7e705d6..f685267b 100644
--- a/QtVsTools.Core/Common/EnumExt.cs
+++ b/QtVsTools.Core/Common/EnumExt.cs
@@ -40,6 +40,8 @@ namespace QtVsTools.Common
/// </summary>
public static class EnumExt
{
+ static LazyFactory StaticLazy { get; } = new LazyFactory();
+
/// <summary>
/// Wrapper for enum cast values.
/// </summary>
@@ -200,15 +202,14 @@ namespace QtVsTools.Common
.FirstOrDefault();
}
- static IEnumerable<Type> _CastAttribTypes;
/// <summary>
/// List of cast attribute types.
/// </summary>
/// <remarks>
/// Future cast attribute types need to be added to this list.
/// </remarks>
- static IEnumerable<Type> CastAttribTypes => _CastAttribTypes
- ?? (_CastAttribTypes = new[]
+ static IEnumerable<Type> CastAttribTypes => StaticLazy.Get(() =>
+ CastAttribTypes, () => new[]
{
typeof(StringAttribute)
});
diff --git a/QtVsTools.Core/Common/LazyFactory.cs b/QtVsTools.Core/Common/LazyFactory.cs
new file mode 100644
index 00000000..93d096c4
--- /dev/null
+++ b/QtVsTools.Core/Common/LazyFactory.cs
@@ -0,0 +1,57 @@
+/****************************************************************************
+**
+** Copyright (C) 2022 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt VS Tools.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+using System;
+using System.Collections.Concurrent;
+using System.Diagnostics;
+using System.Linq.Expressions;
+using System.Reflection;
+
+namespace QtVsTools.Common
+{
+ public class LazyFactory
+ {
+ private Lazy<ConcurrentDictionary<PropertyInfo, Lazy<object>>> LazyObjs { get; }
+ private ConcurrentDictionary<PropertyInfo, Lazy<object>> Objs => LazyObjs.Value;
+
+ public LazyFactory()
+ {
+ LazyObjs = new Lazy<ConcurrentDictionary<PropertyInfo, Lazy<object>>>();
+ }
+
+ public T Get<T>(Expression<Func<T>> propertyRef, Func<T> initFunc) where T : class
+ {
+ var lazyPropertyExpr = propertyRef?.Body as MemberExpression;
+ var lazyProperty = lazyPropertyExpr?.Member as PropertyInfo;
+ if (lazyProperty == null)
+ throw new ArgumentException("Invalid property reference", "propertyRef");
+ var lazyObj = Objs.GetOrAdd(lazyProperty, (_) => new Lazy<object>(() => initFunc()));
+ return lazyObj.Value as T;
+ }
+ }
+}
diff --git a/QtVsTools.Core/QtVsTools.Core.csproj b/QtVsTools.Core/QtVsTools.Core.csproj
index 9ac5f6fa..dd20d370 100644
--- a/QtVsTools.Core/QtVsTools.Core.csproj
+++ b/QtVsTools.Core/QtVsTools.Core.csproj
@@ -124,6 +124,7 @@
<Compile Include="BuildConfig.cs" />
<Compile Include="CommandLineParser.cs" />
<Compile Include="Common\EnumExt.cs" />
+ <Compile Include="Common\LazyFactory.cs" />
<Compile Include="CompilerToolWrapper.cs" />
<Compile Include="CxxStreamReader.cs" />
<Compile Include="ExportProjectDialog.cs">