-
Notifications
You must be signed in to change notification settings - Fork 247
/
base_delegate.h
236 lines (196 loc) · 7.09 KB
/
base_delegate.h
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
namespace winrt::impl
{
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4458) // declaration hides class member (okay because we do not use named members of base class)
#endif
struct implements_delegate_base
{
WINRT_IMPL_NOINLINE uint32_t increment_reference() noexcept
{
return ++m_references;
}
WINRT_IMPL_NOINLINE uint32_t decrement_reference() noexcept
{
return --m_references;
}
WINRT_IMPL_NOINLINE uint32_t query_interface(guid const& id, void** result, unknown_abi* derivedAbiPtr, guid const& derivedId) noexcept
{
if (id == derivedId || is_guid_of<Windows::Foundation::IUnknown>(id) || is_guid_of<IAgileObject>(id))
{
*result = derivedAbiPtr;
increment_reference();
return 0;
}
if (is_guid_of<IMarshal>(id))
{
return make_marshaler(derivedAbiPtr, result);
}
*result = nullptr;
return error_no_interface;
}
private:
atomic_ref_count m_references{ 1 };
};
template <typename T, typename H>
struct implements_delegate : abi_t<T>, implements_delegate_base, H, update_module_lock
{
implements_delegate(H&& handler) : H(std::forward<H>(handler))
{
}
int32_t __stdcall QueryInterface(guid const& id, void** result) noexcept final
{
return query_interface(id, result, static_cast<abi_t<T>*>(this), guid_of<T>());
}
uint32_t __stdcall AddRef() noexcept final
{
return increment_reference();
}
uint32_t __stdcall Release() noexcept final
{
auto const remaining = decrement_reference();
if (remaining == 0)
{
delete static_cast<delegate<T, H>*>(this);
}
return remaining;
}
};
template <typename T, typename H>
T make_delegate(H&& handler)
{
return { static_cast<void*>(static_cast<abi_t<T>*>(new delegate<T, H>(std::forward<H>(handler)))), take_ownership_from_abi };
}
template <typename T>
T make_agile_delegate(T const& delegate) noexcept
{
if constexpr (!has_category_v<T>)
{
return delegate;
}
else
{
if (delegate.template try_as<IAgileObject>())
{
return delegate;
}
const auto id = guid_of<T>();
com_ptr<IAgileReference> ref;
get_agile_reference(id, get_abi(delegate), ref.put_void());
if (ref)
{
return [ref = std::move(ref), id](auto&& ... args)
{
T delegate;
ref->Resolve(id, put_abi(delegate));
return delegate(args...);
};
}
return delegate;
}
}
template <typename R, typename... Args>
struct WINRT_IMPL_NOVTABLE variadic_delegate_abi : unknown_abi
{
virtual R invoke(Args const& ...) = 0;
};
template <typename H, typename R, typename... Args>
struct variadic_delegate final : variadic_delegate_abi<R, Args...>, implements_delegate_base, H, update_module_lock
{
variadic_delegate(H&& handler) : H(std::forward<H>(handler))
{
}
R invoke(Args const& ... args) final
{
if constexpr (std::is_void_v<R>)
{
(*this)(args...);
}
else
{
return (*this)(args...);
}
}
int32_t __stdcall QueryInterface(guid const& id, void** result) noexcept final
{
return query_interface(id, result, static_cast<unknown_abi*>(this), guid_of<Windows::Foundation::IUnknown>());
}
uint32_t __stdcall AddRef() noexcept final
{
return increment_reference();
}
uint32_t __stdcall Release() noexcept final
{
auto const remaining = decrement_reference();
if (remaining == 0)
{
delete this;
}
return remaining;
}
};
template <typename R, typename... Args>
struct WINRT_IMPL_EMPTY_BASES delegate_base : Windows::Foundation::IUnknown
{
delegate_base(std::nullptr_t = nullptr) noexcept {}
delegate_base(void* ptr, take_ownership_from_abi_t) noexcept : IUnknown(ptr, take_ownership_from_abi) {}
template <typename L>
delegate_base(L handler) :
delegate_base(make(std::forward<L>(handler)))
{}
template <typename F> delegate_base(F* handler) :
delegate_base([=](auto&& ... args) { return handler(args...); })
{}
template <typename O, typename M> delegate_base(O* object, M method) :
delegate_base([=](auto&& ... args) { return ((*object).*(method))(args...); })
{}
template <typename O, typename M> delegate_base(com_ptr<O>&& object, M method) :
delegate_base([o = std::move(object), method](auto&& ... args) { return ((*o).*(method))(args...); })
{
}
template <typename O, typename LM> delegate_base(winrt::weak_ref<O>&& object, LM&& lambda_or_method) :
delegate_base([o = std::move(object), lm = std::forward<LM>(lambda_or_method)](auto&&... args) { if (auto s = o.get()) {
if constexpr (std::is_member_function_pointer_v<LM>) ((*s).*(lm))(args...);
else lm(args...);
}})
{
}
template <typename O, typename M> delegate_base(std::shared_ptr<O>&& object, M method) :
delegate_base([o = std::move(object), method](auto&& ... args) { return ((*o).*(method))(args...); })
{
}
template <typename O, typename LM> delegate_base(std::weak_ptr<O>&& object, LM&& lambda_or_method) :
delegate_base([o = std::move(object), lm = std::forward<LM>(lambda_or_method)](auto&&... args) { if (auto s = o.lock()) {
if constexpr (std::is_member_function_pointer_v<LM>) ((*s).*(lm))(args...);
else lm(args...);
}})
{
}
auto operator()(Args const& ... args) const
{
return (*(variadic_delegate_abi<R, Args...> * *)this)->invoke(args...);
}
private:
template <typename H>
static delegate_base<R, Args...> make(H&& handler)
{
return { static_cast<void*>(new variadic_delegate<H, R, Args...>(std::forward<H>(handler))), take_ownership_from_abi };
}
};
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
}
WINRT_EXPORT namespace winrt
{
template <typename... Args>
struct WINRT_IMPL_EMPTY_BASES delegate : impl::delegate_base<void, Args...>
{
using impl::delegate_base<void, Args...>::delegate_base;
};
template <typename R, typename... Args>
struct WINRT_IMPL_EMPTY_BASES delegate<R(Args...)> : impl::delegate_base<R, Args...>
{
using impl::delegate_base<R, Args...>::delegate_base;
};
}