Skip to content

Commit

Permalink
Simplify factory caching by using a FactoryObjectReference type that …
Browse files Browse the repository at this point in the history
…tracks the context.
  • Loading branch information
jlaanstra committed Nov 5, 2023
1 parent 0258835 commit a2b04df
Show file tree
Hide file tree
Showing 9 changed files with 587 additions and 440 deletions.
12 changes: 1 addition & 11 deletions src/WinRT.Runtime/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

namespace WinRT
{
static class Context
static partial class Context
{
[DllImport("api-ms-win-core-com-l1-1-0.dll")]
private static extern unsafe int CoGetContextToken(IntPtr* contextToken);

[DllImport("api-ms-win-core-com-l1-1-0.dll")]
private static extern int CoGetObjectContext(ref Guid riid, out IntPtr ppv);

Expand All @@ -24,13 +21,6 @@ public static IntPtr GetContextCallback()
return contextCallbackPtr;
}

public unsafe static IntPtr GetContextToken()
{
IntPtr contextToken;
Marshal.ThrowExceptionForHR(CoGetContextToken(&contextToken));
return contextToken;
}

// Calls the given callback in the right context.
// On any exception, calls onFail callback if any set.
// If not set, exception is handled due to today we don't
Expand Down
14 changes: 3 additions & 11 deletions src/WinRT.Runtime/Projections/DataErrorsChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,7 @@ namespace ABI.System.ComponentModel
#endif
unsafe struct DataErrorsChangedEventArgs
{
private sealed class ActivationFactory : BaseActivationFactory
{
public ActivationFactory() : base("Microsoft.UI.Xaml.Data", "Microsoft.UI.Xaml.Data.DataErrorsChangedEventArgs")
{
}

internal static ABI.Microsoft.UI.Xaml.Data.WinRTDataErrorsChangedEventArgsRuntimeClassFactory Instance =
new ActivationFactory()._As<ABI.Microsoft.UI.Xaml.Data.WinRTDataErrorsChangedEventArgsRuntimeClassFactory.Vftbl>();
}
private static ABI.Microsoft.UI.Xaml.Data.WinRTDataErrorsChangedEventArgsRuntimeClassFactory Instance = ActivationFactory.Get("Microsoft.UI.Xaml.Data.DataErrorsChangedEventArgs");

public static IObjectReference CreateMarshaler(global::System.ComponentModel.DataErrorsChangedEventArgs value)
{
Expand All @@ -108,7 +100,7 @@ public static IObjectReference CreateMarshaler(global::System.ComponentModel.Dat
return null;
}

return ActivationFactory.Instance.CreateInstance(value.PropertyName);
return Instance.CreateInstance(value.PropertyName);
}

public static ObjectReferenceValue CreateMarshaler2(global::System.ComponentModel.DataErrorsChangedEventArgs value)
Expand All @@ -118,7 +110,7 @@ public static ObjectReferenceValue CreateMarshaler2(global::System.ComponentMode
return new ObjectReferenceValue();
}

return ActivationFactory.Instance.CreateInstanceForMarshaling(value.PropertyName);
return Instance.CreateInstanceForMarshaling(value.PropertyName);
}

public static IntPtr GetAbi(IObjectReference m) => m?.ThisPtr ?? IntPtr.Zero;
Expand Down
16 changes: 4 additions & 12 deletions src/WinRT.Runtime/Projections/NotifyCollectionChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,8 @@ namespace ABI.System.Collections.Specialized
public
#endif
struct NotifyCollectionChangedEventArgs
{
private sealed class ActivationFactory : BaseActivationFactory
{
public ActivationFactory() : base("Microsoft.UI.Xaml.Interop", "Microsoft.UI.Xaml.Interop.NotifyCollectionChangedEventArgs")
{
}

internal static WinRTNotifyCollectionChangedEventArgsRuntimeClassFactory Instance =
new ActivationFactory()._As<WinRTNotifyCollectionChangedEventArgsRuntimeClassFactory.Vftbl>();
}
{
private static WinRTNotifyCollectionChangedEventArgsRuntimeClassFactory Instance = ActivationFactory.Get("Microsoft.UI.Xaml.Interop.NotifyCollectionChangedEventArgs");

public static IObjectReference CreateMarshaler(global::System.Collections.Specialized.NotifyCollectionChangedEventArgs value)
{
Expand All @@ -212,7 +204,7 @@ public static IObjectReference CreateMarshaler(global::System.Collections.Specia
return null;
}

return ActivationFactory.Instance.CreateInstanceWithAllParameters(value.Action, value.NewItems, value.OldItems, value.NewStartingIndex, value.OldStartingIndex, null, out _);
return Instance.CreateInstanceWithAllParameters(value.Action, value.NewItems, value.OldItems, value.NewStartingIndex, value.OldStartingIndex, null, out _);
}

public static ObjectReferenceValue CreateMarshaler2(global::System.Collections.Specialized.NotifyCollectionChangedEventArgs value)
Expand All @@ -222,7 +214,7 @@ public static ObjectReferenceValue CreateMarshaler2(global::System.Collections.S
return new ObjectReferenceValue();
}

return ActivationFactory.Instance.CreateInstanceWithAllParameters(value.Action, value.NewItems, value.OldItems, value.NewStartingIndex, value.OldStartingIndex);
return Instance.CreateInstanceWithAllParameters(value.Action, value.NewItems, value.OldItems, value.NewStartingIndex, value.OldStartingIndex);
}

public static IntPtr GetAbi(IObjectReference m) => m?.ThisPtr ?? IntPtr.Zero;
Expand Down
14 changes: 3 additions & 11 deletions src/WinRT.Runtime/Projections/PropertyChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,7 @@ namespace ABI.System.ComponentModel
#endif
unsafe struct PropertyChangedEventArgs
{
private sealed class ActivationFactory : BaseActivationFactory
{
public ActivationFactory() : base("Microsoft.UI.Xaml.Data", "Microsoft.UI.Xaml.Data.PropertyChangedEventArgs")
{
}

internal static ABI.Microsoft.UI.Xaml.Data.WinRTPropertyChangedEventArgsRuntimeClassFactory Instance =
new ActivationFactory()._As<ABI.Microsoft.UI.Xaml.Data.WinRTPropertyChangedEventArgsRuntimeClassFactory.Vftbl>();
}
private static ABI.Microsoft.UI.Xaml.Data.WinRTPropertyChangedEventArgsRuntimeClassFactory Instance = ActivationFactory.Get("Microsoft.UI.Xaml.Data.PropertyChangedEventArgs");

public static IObjectReference CreateMarshaler(global::System.ComponentModel.PropertyChangedEventArgs value)
{
Expand All @@ -119,7 +111,7 @@ public static IObjectReference CreateMarshaler(global::System.ComponentModel.Pro
return null;
}

return ActivationFactory.Instance.CreateInstance(value.PropertyName, null, out _);
return Instance.CreateInstance(value.PropertyName, null, out _);
}

public static ObjectReferenceValue CreateMarshaler2(global::System.ComponentModel.PropertyChangedEventArgs value)
Expand All @@ -129,7 +121,7 @@ public static ObjectReferenceValue CreateMarshaler2(global::System.ComponentMode
return new ObjectReferenceValue();
}

return ActivationFactory.Instance.CreateInstance(value.PropertyName);
return Instance.CreateInstance(value.PropertyName);
}

public static IntPtr GetAbi(IObjectReference m) => m?.ThisPtr ?? IntPtr.Zero;
Expand Down
14 changes: 3 additions & 11 deletions src/WinRT.Runtime/Projections/Uri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,7 @@ public unsafe ObjectReferenceValue CreateUriForMarshaling(string uri)
#endif
unsafe struct Uri
{
private sealed class ActivationFactory : BaseActivationFactory
{
public ActivationFactory() : base("Windows.Foundation", "Windows.Foundation.Uri")
{
}

internal static WinRTUriRuntimeClassFactory Instance =
new ActivationFactory()._As<WinRTUriRuntimeClassFactory.Vftbl>();
}
private static WinRTUriRuntimeClassFactory Instance = ActivationFactory.Get("Windows.Foundation.Uri");

public static IObjectReference CreateMarshaler(global::System.Uri value)
{
Expand All @@ -113,7 +105,7 @@ public static IObjectReference CreateMarshaler(global::System.Uri value)
return null;
}

return ActivationFactory.Instance.CreateUri(value.OriginalString);
return Instance.CreateUri(value.OriginalString);
}

public static ObjectReferenceValue CreateMarshaler2(global::System.Uri value)
Expand All @@ -123,7 +115,7 @@ public static ObjectReferenceValue CreateMarshaler2(global::System.Uri value)
return new ObjectReferenceValue();
}

return ActivationFactory.Instance.CreateUriForMarshaling(value.OriginalString);
return Instance.CreateUriForMarshaling(value.OriginalString);
}

public static IntPtr GetAbi(IObjectReference m) => m?.ThisPtr ?? IntPtr.Zero;
Expand Down
Loading

0 comments on commit a2b04df

Please sign in to comment.