forked from microsoft/CsWinRT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleInterfaceOptimizedObject.net5.cs
More file actions
87 lines (76 loc) · 3.2 KB
/
SingleInterfaceOptimizedObject.net5.cs
File metadata and controls
87 lines (76 loc) · 3.2 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using WinRT.Interop;
namespace WinRT
{
#if EMBED
internal
#else
public
#endif
class SingleInterfaceOptimizedObject : IWinRTObject, IDynamicInterfaceCastable
{
private Type _type;
private IObjectReference _obj;
public SingleInterfaceOptimizedObject(Type type, IObjectReference objRef)
: this(type, objRef, true)
{
}
internal SingleInterfaceOptimizedObject(Type type, IObjectReference objRef, bool requireQI)
{
_type = type;
if (requireQI)
{
Type helperType = type.FindHelperType();
var vftblType = helperType.FindVftblType();
if (vftblType is null)
{
_obj = objRef.As<IUnknownVftbl>(GuidGenerator.GetIID(helperType));
}
else
{
_obj = (IObjectReference)typeof(IObjectReference).GetMethod("As", Type.EmptyTypes).MakeGenericMethod(vftblType).Invoke(objRef, null);
}
}
else
{
_obj = objRef;
}
}
IObjectReference IWinRTObject.NativeObject => _obj;
bool IWinRTObject.HasUnwrappableNativeObject => false;
private volatile ConcurrentDictionary<RuntimeTypeHandle, IObjectReference> _queryInterfaceCache;
private ConcurrentDictionary<RuntimeTypeHandle, IObjectReference> MakeQueryInterfaceCache()
{
System.Threading.Interlocked.CompareExchange(ref _queryInterfaceCache, new ConcurrentDictionary<RuntimeTypeHandle, IObjectReference>(), null);
return _queryInterfaceCache;
}
ConcurrentDictionary<RuntimeTypeHandle, IObjectReference> IWinRTObject.QueryInterfaceCache => _queryInterfaceCache ?? MakeQueryInterfaceCache();
private volatile ConcurrentDictionary<RuntimeTypeHandle, object> _additionalTypeData;
private ConcurrentDictionary<RuntimeTypeHandle, object> MakeAdditionalTypeData()
{
System.Threading.Interlocked.CompareExchange(ref _additionalTypeData, new ConcurrentDictionary<RuntimeTypeHandle, object>(), null);
return _additionalTypeData;
}
ConcurrentDictionary<RuntimeTypeHandle, object> IWinRTObject.AdditionalTypeData => _additionalTypeData ?? MakeAdditionalTypeData();
bool IDynamicInterfaceCastable.IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented)
{
if (_type.Equals(Type.GetTypeFromHandle(interfaceType)))
{
return true;
}
return (this as IWinRTObject).IsInterfaceImplementedFallback(interfaceType, throwIfNotImplemented);
}
IObjectReference IWinRTObject.GetObjectReferenceForType(RuntimeTypeHandle interfaceType)
{
if (_type.Equals(Type.GetTypeFromHandle(interfaceType)))
{
return _obj;
}
return (this as IWinRTObject).GetObjectReferenceForTypeFallback(interfaceType);
}
}
}