forked from microsoft/cppwinrt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterop.cpp
More file actions
86 lines (74 loc) · 2.3 KB
/
interop.cpp
File metadata and controls
86 lines (74 loc) · 2.3 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
#include "pch.h"
#include <inspectable.h>
struct DECLSPEC_UUID("5040a5f4-796a-42ff-9f06-be89137a518f") IBase : IUnknown
{
};
struct DECLSPEC_UUID("529fed32-514f-4150-b1ba-15b47df700b7") IDerived : IBase
{
};
struct DECLSPEC_UUID("b81fb2a2-eab4-488a-96a7-434873c2c20b") IMoreDerived : IDerived
{
};
#ifdef __CRT_UUID_DECL
__CRT_UUID_DECL(IBase, 0x5040a5f4, 0x796a, 0x42ff, 0x9f, 0x06, 0xbe, 0x89, 0x13, 0x7a, 0x51, 0x8f)
__CRT_UUID_DECL(IDerived, 0x529fed32, 0x514f, 0x4150, 0xb1, 0xba, 0x15, 0xb4, 0x7d, 0xf7, 0x00, 0xb7)
__CRT_UUID_DECL(IMoreDerived, 0xb81fb2a2, 0xeab4, 0x488a, 0x96, 0xa7, 0x43, 0x48, 0x73, 0xc2, 0xc2, 0x0b)
#endif
namespace winrt
{
template<> bool is_guid_of<IDerived>(guid const& id) noexcept
{
return is_guid_of<IDerived, IBase>(id);
}
template<> bool is_guid_of<IMoreDerived>(guid const& id) noexcept
{
return is_guid_of<IMoreDerived, IDerived, IBase>(id);
}
}
using namespace winrt;
struct MyBase : implements<MyBase, IBase>
{
};
struct MyDerived : implements<MyDerived, IDerived>
{
};
struct MyMoreDerived : implements<MyMoreDerived, IMoreDerived, Windows::Foundation::IInspectable>
{
};
Windows::Foundation::IAsyncAction Async()
{
co_return;
}
TEST_CASE("interop")
{
{
Windows::Foundation::IAsyncAction a = Async();
com_ptr<::IInspectable> b = a.as<::IInspectable>();
Windows::Foundation::IAsyncAction c = b.as<Windows::Foundation::IAsyncAction>();
REQUIRE(a == c);
}
{
com_ptr<IBase> a = make<MyBase>();
REQUIRE(a);
REQUIRE(a.try_as<IBase>() != nullptr);
REQUIRE(a.try_as<IDerived>() == nullptr);
REQUIRE(a.try_as<IMoreDerived>() == nullptr);
REQUIRE(a.try_as<::IInspectable>() == nullptr);
}
{
com_ptr<IDerived> a = make<MyDerived>();
REQUIRE(a);
REQUIRE(a.try_as<IBase>() != nullptr);
REQUIRE(a.try_as<IDerived>() != nullptr);
REQUIRE(a.try_as<IMoreDerived>() == nullptr);
REQUIRE(a.try_as<::IInspectable>() == nullptr);
}
{
com_ptr<IMoreDerived> a = make<MyMoreDerived>();
REQUIRE(a);
REQUIRE(a.try_as<IBase>() != nullptr);
REQUIRE(a.try_as<IDerived>() != nullptr);
REQUIRE(a.try_as<IMoreDerived>() != nullptr);
REQUIRE(a.try_as<::IInspectable>() != nullptr);
}
}