-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathNonAgileClass.cpp
94 lines (78 loc) · 3.36 KB
/
NonAgileClass.cpp
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
#include "pch.h"
#include "NonAgileClass.h"
#include "NonAgileClass.g.cpp"
using namespace winrt;
namespace
{
struct __declspec(uuid("624cd4e1-d007-43b1-9c03-af4d3e6258c4")) __declspec(novtable)
INonAgileBindableVectorChangedEventHandler : ::IUnknown
{
virtual int32_t __stdcall Invoke(void*, void*) noexcept = 0;
};
struct NonAgileDelegate : implements<NonAgileDelegate, non_agile, INonAgileBindableVectorChangedEventHandler, IMarshal>
{
NonAgileDelegate()
{
}
int32_t __stdcall Invoke(void* p1, void* p2) noexcept override
{
VectorChanged(*reinterpret_cast<Microsoft::UI::Xaml::Interop::IBindableObservableVector const*>(&p1),
*reinterpret_cast<Windows::Foundation::IInspectable const*>(&p2));
return S_OK;
}
HRESULT __stdcall GetUnmarshalClass(REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, CLSID* pCid) noexcept final
{
return m_marshaler->GetUnmarshalClass(riid, pv, dwDestContext, pvDestContext, mshlflags, pCid);
}
HRESULT __stdcall GetMarshalSizeMax(REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags, DWORD* pSize) noexcept final
{
return m_marshaler->GetMarshalSizeMax(riid, pv, dwDestContext, pvDestContext, mshlflags, pSize);
}
HRESULT __stdcall MarshalInterface(IStream* pStm, REFIID riid, void* pv, DWORD dwDestContext, void* pvDestContext, DWORD mshlflags) noexcept final
{
return m_marshaler->MarshalInterface(pStm, riid, pv, dwDestContext, pvDestContext, mshlflags);
}
HRESULT __stdcall UnmarshalInterface(IStream* pStm, REFIID riid, void** ppv) noexcept final
{
return m_marshaler->UnmarshalInterface(pStm, riid, ppv);
}
HRESULT __stdcall ReleaseMarshalData(IStream* pStm) noexcept final
{
return m_marshaler->ReleaseMarshalData(pStm);
}
HRESULT __stdcall DisconnectObject(DWORD dwReserved) noexcept final
{
return m_marshaler->DisconnectObject(dwReserved);
}
void VectorChanged(Microsoft::UI::Xaml::Interop::IBindableObservableVector vector, Windows::Foundation::IInspectable e)
{
int32_t sum = 0;
auto view = vector.GetView();
for (uint32_t i = 0; i < view.Size(); i++)
{
sum += winrt::unbox_value<int32_t>(view.GetAt(i));
}
e.as<winrt::TestComponentCSharp::IProperties2>().ReadWriteProperty(sum);
}
private:
static com_ptr<::IMarshal> get_marshaler() noexcept
{
com_ptr<::IUnknown> unknown;
WINRT_VERIFY_(S_OK, CoCreateFreeThreadedMarshaler(nullptr, unknown.put()));
return unknown ? unknown.try_as<::IMarshal>() : nullptr;
}
com_ptr<::IMarshal> m_marshaler{ get_marshaler() };
};
}
namespace winrt::TestComponentCSharp::implementation
{
NonAgileClass::NonAgileClass()
{
}
void NonAgileClass::Observe(Microsoft::UI::Xaml::Interop::IBindableObservableVector vector)
{
Microsoft::UI::Xaml::Interop::BindableVectorChangedEventHandler handler;
*put_abi(handler) = make<NonAgileDelegate>().detach();
vector.VectorChanged(handler);
}
}