Skip to content

Commit

Permalink
Remove more closures in lambda expressions (#1588)
Browse files Browse the repository at this point in the history
* Remove unnecessary lambdas in ComWrappersSupport

* Remove closures in 'GetOrCreateTypeHelperData'

* Remove more stub lambdas

* Remove 'IWinRTObject.GetOrCreateTypeHelperData' overload
  • Loading branch information
Sergio0694 authored Apr 26, 2024
1 parent 5d2de50 commit 7551da2
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 20 deletions.
7 changes: 6 additions & 1 deletion src/WinRT.Runtime/ComWrappersSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,12 @@ private static Func<IInspectable, object> CreateCustomTypeMappingFactory(
};
}

internal static Func<IInspectable, object> CreateTypedRcwFactory(Type implementationType, string runtimeClassName = null)
internal static Func<IInspectable, object> CreateTypedRcwFactory(Type implementationType)
{
return CreateTypedRcwFactory(implementationType, null);
}

internal static Func<IInspectable, object> CreateTypedRcwFactory(Type implementationType, string runtimeClassName)
{
// If runtime class name is empty or "Object", then just use IInspectable.
if (implementationType == null || implementationType == typeof(object))
Expand Down
2 changes: 1 addition & 1 deletion src/WinRT.Runtime/ComWrappersSupport.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public static void InitializeComWrappers(ComWrappers wrappers = null)
ComWrappers = wrappers;
}

internal static Func<IInspectable, object> GetTypedRcwFactory(Type implementationType) => TypedObjectFactoryCacheForType.GetOrAdd(implementationType, classType => CreateTypedRcwFactory(classType));
internal static Func<IInspectable, object> GetTypedRcwFactory(Type implementationType) => TypedObjectFactoryCacheForType.GetOrAdd(implementationType, CreateTypedRcwFactory);

public static bool RegisterTypedRcwFactory(Type implementationType, Func<IInspectable, object> rcwFactory) => TypedObjectFactoryCacheForType.TryAdd(implementationType, rcwFactory);

Expand Down
6 changes: 3 additions & 3 deletions src/WinRT.Runtime/ComWrappersSupport.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static T CreateRcwForComObject<T>(IntPtr ptr)
return CreateRcwForComObject<T>(ptr, true);
}

internal static Func<IInspectable, object> GetTypedRcwFactory(Type implementationType) => TypedObjectFactoryCacheForType.GetOrAdd(implementationType, classType => CreateTypedRcwFactory(classType));
internal static Func<IInspectable, object> GetTypedRcwFactory(Type implementationType) => TypedObjectFactoryCacheForType.GetOrAdd(implementationType, CreateTypedRcwFactory);

private static T CreateRcwForComObject<T>(IntPtr ptr, bool tryUseCache)
{
Expand Down Expand Up @@ -63,7 +63,7 @@ private static T CreateRcwForComObject<T>(IntPtr ptr, bool tryUseCache)
else
{
Type runtimeClassType = GetRuntimeClassForTypeCreation(inspectable, typeof(T));
runtimeWrapper = runtimeClassType == null ? inspectable : TypedObjectFactoryCacheForType.GetOrAdd(runtimeClassType, classType => CreateTypedRcwFactory(classType))(inspectable);
runtimeWrapper = runtimeClassType == null ? inspectable : TypedObjectFactoryCacheForType.GetOrAdd(runtimeClassType, CreateTypedRcwFactory)(inspectable);
}
}
else if (identity.TryAs<ABI.WinRT.Interop.IWeakReference.Vftbl>(out var weakRef) == 0)
Expand Down Expand Up @@ -132,7 +132,7 @@ public static bool TryUnwrapObject(object o, out IObjectReference objRef)
return TryUnwrapObject(del.Target, out objRef);
}

var objRefFunc = TypeObjectRefFuncCache.GetOrAdd(o.GetType(), (type) =>
var objRefFunc = TypeObjectRefFuncCache.GetOrAdd(o.GetType(), static (type) =>
{
ObjectReferenceWrapperAttribute objRefWrapper = type.GetCustomAttribute<ObjectReferenceWrapperAttribute>();
if (objRefWrapper is object)
Expand Down
5 changes: 4 additions & 1 deletion src/WinRT.Runtime/IWinRTObject.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -318,9 +319,11 @@ IObjectReference GetObjectReferenceForTypeFallback(RuntimeTypeHandle type)

ConcurrentDictionary<RuntimeTypeHandle, object> AdditionalTypeData { get; }

[Obsolete("Use the 'AdditionalTypeData' property instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
object GetOrCreateTypeHelperData(RuntimeTypeHandle type, Func<object> helperDataFactory)
{
return AdditionalTypeData.GetOrAdd(type, (type) => helperDataFactory());
throw new NotSupportedException("'GetOrCreateTypeHelperData(RuntimeTypeHandle, Func<object>)' is not supported, use 'AdditionalTypeData' instead.");
}
}
}
4 changes: 3 additions & 1 deletion src/WinRT.Runtime/MatchingRefApiCompatBaseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,6 @@ CannotRemoveAttribute : Attribute 'System.Diagnostics.CodeAnalysis.Unconditional
TypesMustExist : Type 'WinRT.WinRTRuntimeClassNameAttribute' does not exist in the reference but it does exist in the implementation.
MembersMustExist : Member 'public void WinRT.ComWrappersSupport.RegisterTypeRuntimeClassNameLookup(System.Func<System.Type, System.String>)' does not exist in the reference but it does exist in the implementation.
CannotRemoveAttribute : Attribute 'System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute' exists on 'WinRT.Projections.FindCustomHelperTypeMapping(System.Type, System.Boolean)' in the implementation but not the reference.
Total Issues: 237
CannotRemoveAttribute : Attribute 'System.ComponentModel.EditorBrowsableAttribute' exists on 'WinRT.IWinRTObject.GetOrCreateTypeHelperData(System.RuntimeTypeHandle, System.Func<System.Object>)' in the implementation but not the reference.
CannotRemoveAttribute : Attribute 'System.ObsoleteAttribute' exists on 'WinRT.IWinRTObject.GetOrCreateTypeHelperData(System.RuntimeTypeHandle, System.Func<System.Object>)' in the implementation but not the reference.
Total Issues: 239
8 changes: 4 additions & 4 deletions src/WinRT.Runtime/Projections/Bindable.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,8 @@ internal static ObjectReference<IUnknownVftbl> ObjRefFromAbi(IntPtr thisPtr)

private static FromAbiHelper _AbiHelper(IWinRTObject _this)
{
return (FromAbiHelper)_this.GetOrCreateTypeHelperData(typeof(global::System.Collections.IEnumerable).TypeHandle,
() => new FromAbiHelper((global::System.Collections.IEnumerable)_this));
return (FromAbiHelper)_this.AdditionalTypeData.GetOrAdd(typeof(global::System.Collections.IEnumerable).TypeHandle,
static (_, _this) => new FromAbiHelper((global::System.Collections.IEnumerable)_this), _this);
}

unsafe global::Microsoft.UI.Xaml.Interop.IBindableIterator global::Microsoft.UI.Xaml.Interop.IBindableIterable.First()
Expand Down Expand Up @@ -1286,8 +1286,8 @@ internal static ObjectReference<IUnknownVftbl> ObjRefFromAbi(IntPtr thisPtr)

internal static FromAbiHelper _VectorToList(IWinRTObject _this)
{
return (FromAbiHelper)_this.GetOrCreateTypeHelperData(typeof(global::System.Collections.IList).TypeHandle,
() => new FromAbiHelper((global::Microsoft.UI.Xaml.Interop.IBindableVector)_this));
return (FromAbiHelper)_this.AdditionalTypeData.GetOrAdd(typeof(global::System.Collections.IList).TypeHandle,
static (_, _this) => new FromAbiHelper((global::Microsoft.UI.Xaml.Interop.IBindableVector)_this), _this);
}

unsafe object global::Microsoft.UI.Xaml.Interop.IBindableVector.GetAt(uint index)
Expand Down
18 changes: 9 additions & 9 deletions src/WinRT.Runtime/Projections/CollectionHybrid.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ABI.System.Collections.Generic
[DynamicInterfaceCastableImplementation]
interface IReadOnlyCollection<T> : global::System.Collections.Generic.IReadOnlyCollection<T>
{
private static global::System.Collections.Generic.IReadOnlyCollection<T> CreateHelper(IWinRTObject _this)
private static global::System.Collections.Generic.IReadOnlyCollection<T> CreateHelper(RuntimeTypeHandle type, IWinRTObject _this)
{
var genericType = typeof(T);
if (genericType.IsGenericType && genericType.GetGenericTypeDefinition() == typeof(global::System.Collections.Generic.KeyValuePair<,>))
Expand Down Expand Up @@ -51,9 +51,9 @@ interface IReadOnlyCollection<T> : global::System.Collections.Generic.IReadOnlyC

private static global::System.Collections.Generic.IReadOnlyCollection<T> GetHelper(IWinRTObject _this)
{
return (global::System.Collections.Generic.IReadOnlyCollection<T>)_this.GetOrCreateTypeHelperData(
return (global::System.Collections.Generic.IReadOnlyCollection<T>)_this.AdditionalTypeData.GetOrAdd(
typeof(global::System.Collections.Generic.IReadOnlyCollection<T>).TypeHandle,
() => CreateHelper(_this));
CreateHelper, _this);
}

int global::System.Collections.Generic.IReadOnlyCollection<T>.Count
Expand All @@ -69,7 +69,7 @@ interface IReadOnlyCollection<T> : global::System.Collections.Generic.IReadOnlyC
[DynamicInterfaceCastableImplementation]
interface ICollection<T> : global::System.Collections.Generic.ICollection<T>
{
private static global::System.Collections.Generic.ICollection<T> CreateHelper(IWinRTObject _this)
private static global::System.Collections.Generic.ICollection<T> CreateHelper(RuntimeTypeHandle type, IWinRTObject _this)
{
var genericType = typeof(T);
if (genericType.IsGenericType && genericType.GetGenericTypeDefinition() == typeof(global::System.Collections.Generic.KeyValuePair<,>))
Expand Down Expand Up @@ -104,9 +104,9 @@ interface ICollection<T> : global::System.Collections.Generic.ICollection<T>

private static global::System.Collections.Generic.ICollection<T> GetHelper(IWinRTObject _this)
{
return (global::System.Collections.Generic.ICollection<T>)_this.GetOrCreateTypeHelperData(
return (global::System.Collections.Generic.ICollection<T>)_this.AdditionalTypeData.GetOrAdd(
typeof(global::System.Collections.Generic.ICollection<T>).TypeHandle,
() => CreateHelper(_this));
CreateHelper, _this);
}

int global::System.Collections.Generic.ICollection<T>.Count
Expand Down Expand Up @@ -143,7 +143,7 @@ namespace ABI.System.Collections
[DynamicInterfaceCastableImplementation]
interface ICollection : global::System.Collections.ICollection
{
private static global::System.Collections.ICollection CreateHelper(IWinRTObject _this)
private static global::System.Collections.ICollection CreateHelper(RuntimeTypeHandle type, IWinRTObject _this)
{
var iList = typeof(global::System.Collections.IList);
if (_this.IsInterfaceImplemented(iList.TypeHandle, false))
Expand All @@ -156,9 +156,9 @@ interface ICollection : global::System.Collections.ICollection

private static global::System.Collections.ICollection GetHelper(IWinRTObject _this)
{
return (global::System.Collections.ICollection)_this.GetOrCreateTypeHelperData(
return (global::System.Collections.ICollection)_this.AdditionalTypeData.GetOrAdd(
typeof(global::System.Collections.ICollection).TypeHandle,
() => CreateHelper(_this));
CreateHelper, _this);
}

int global::System.Collections.ICollection.Count
Expand Down

0 comments on commit 7551da2

Please sign in to comment.