-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathdelegate_deducing_this.cpp
More file actions
129 lines (105 loc) · 2.93 KB
/
delegate_deducing_this.cpp
File metadata and controls
129 lines (105 loc) · 2.93 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "pch.h"
using namespace winrt;
using namespace Windows::Foundation::Collections;
#if defined(__cpp_explicit_this_parameter) && __cpp_explicit_this_parameter >= 202110L
TEST_CASE("delegate")
{
// <>
{
bool invoked = false;
delegate<> d = [&](this auto) { invoked = true; };
d();
REQUIRE(invoked);
}
// <int>
{
int result = 0;
delegate<int> d = [&](this auto, int a) { result = a; };
d(123);
REQUIRE(result == 123);
}
// <int,int>
{
int result = 0;
delegate<int, int> d = [&](this auto, int a, int b) { result = a + b; };
d(4,5);
REQUIRE(result == 9);
}
// void()
{
bool invoked = false;
delegate<void()> d = [&](this auto) { invoked = true; };
d();
REQUIRE(invoked);
}
// void(int)
{
int result = 0;
delegate<void(int)> d = [&](this auto, int a) { result = a; };
d(123);
REQUIRE(result == 123);
}
// void(int,int)
{
int result = 0;
delegate<void(int,int)> d = [&](this auto, int a, int b) { result = a + b; };
d(4, 5);
REQUIRE(result == 9);
}
// int()
{
delegate<int()> d = [](this auto) { return 123; };
REQUIRE(d() == 123);
}
// int(int)
{
delegate<int(int)> d = [](this auto, int a) { return a; };
REQUIRE(d(123) == 123);
}
// int(int,int)
{
delegate<int(int, int)> d = [](this auto, int a, int b) { return a + b; };
REQUIRE(d(4, 5) == 9);
}
}
TEST_CASE("typedeventhandler")
{
using Handler = winrt::Windows::Foundation::TypedEventHandler<int, int>;
int called{};
int sum{};
Handler handler = [&called, &sum](this auto, int sender, int args)
{
++called;
sum = sender + args;
};
handler(1, 2);
REQUIRE(called == 1);
REQUIRE(sum == 3);
}
#if !(defined(_M_IX86) && !defined(_M_X64) && defined(__clang__))
// Clang 20 target x86 generates incorrect code for this test.
// In observed_sender ctor (#1), this = X; in assignment operator (#2), this = X+16 (bytes).
// FIXME: Enable the test after Clang is fixed
TEST_CASE("observablevector_vectorchanged")
{
IObservableVector<int> vector = single_threaded_observable_vector<int>();
int called{};
IObservableVector<int> observed_sender{ nullptr }; // #1
CollectionChange change{};
uint32_t index{};
auto token = vector.VectorChanged([&](this auto, IObservableVector<int> sender, IVectorChangedEventArgs args)
{
++called;
observed_sender = sender; // #2
change = args.CollectionChange();
index = args.Index();
});
vector.Append(123);
REQUIRE(called == 1);
REQUIRE(observed_sender == vector);
REQUIRE(change == CollectionChange::ItemInserted);
REQUIRE(index == 0);
vector.VectorChanged(token);
}
#endif
#endif