1 // MIT License
2 //
3 // Copyright (c) 2016 Microsoft Corporation
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22
23 // Source taken from https://github.com/microsoft/MixedRealityCompanionKit
24
25 #pragma once
26
27 #include <wrl.h>
28 #include <wrl\async.h>
29 #include <Windows.System.Threading.h>
30 #include <functional>
31
32 template <typename TDelegate, typename TOperation, typename TLambda>
StartAsyncThen(_In_ TOperation * pOperation,_In_ TLambda && tFunc)33 HRESULT StartAsyncThen(_In_ TOperation* pOperation, _In_ TLambda&& tFunc)
34 {
35 if (nullptr == pOperation)
36 {
37 return E_INVALIDARG;
38 }
39
40 auto spCallback = Microsoft::WRL::Callback<TDelegate>(
41 [tFunc](_In_ TOperation* pOperation, _In_ AsyncStatus status) -> HRESULT
42 {
43 HRESULT hr = S_OK;
44
45 // wrap the operation
46 if (status != AsyncStatus::Completed)
47 {
48 Microsoft::WRL::ComPtr<TOperation> spOperation(pOperation);
49 Microsoft::WRL::ComPtr<IAsyncInfo> spAsyncInfo;
50 hr = spOperation.As(&spAsyncInfo);
51 if (SUCCEEDED(hr))
52 {
53 spAsyncInfo->get_ErrorCode(&hr);
54 }
55 }
56
57 return tFunc(hr, pOperation, status);
58 });
59
60 // start
61 return (nullptr != spCallback) ? pOperation->put_Completed(spCallback.Get()) : E_OUTOFMEMORY;
62 }
63 template <typename TLambda>
StartAsyncThen(_In_ ABI::Windows::Foundation::IAsyncAction * pOperation,_In_ TLambda && tFunc)64 HRESULT StartAsyncThen(_In_ ABI::Windows::Foundation::IAsyncAction* pOperation, _In_ TLambda&& tFunc)
65 {
66 return StartAsyncThen<ABI::Windows::Foundation::IAsyncActionCompletedHandler, ABI::Windows::Foundation::IAsyncAction>(pOperation, static_cast<TLambda&&>(tFunc));
67 }
68 template <typename TProgress, typename TLambda>
StartAsyncThen(_In_ ABI::Windows::Foundation::IAsyncActionWithProgress<TProgress> * pOperation,_In_ TLambda && tFunc)69 HRESULT StartAsyncThen(_In_ ABI::Windows::Foundation::IAsyncActionWithProgress<TProgress>* pOperation, _In_ TLambda&& tFunc)
70 {
71 return StartAsyncThen<ABI::Windows::Foundation::IAsyncActionWithProgressCompletedHandler<TProgress>, Windows::Foundation::IAsyncActionWithProgress<TProgress>>(pOperation, static_cast<TLambda&&>(tFunc));
72 }
73 template <typename TResult, typename TLambda>
StartAsyncThen(_In_ ABI::Windows::Foundation::IAsyncOperation<TResult> * pOperation,_In_ TLambda && tFunc)74 HRESULT StartAsyncThen(_In_ ABI::Windows::Foundation::IAsyncOperation<TResult>* pOperation, _In_ TLambda&& tFunc)
75 {
76 return StartAsyncThen<ABI::Windows::Foundation::IAsyncOperationCompletedHandler<TResult>, ABI::Windows::Foundation::IAsyncOperation<TResult>>(pOperation, static_cast<TLambda&&>(tFunc));
77 }
78 template <typename TResult, typename TProgress, typename TLambda>
StartAsyncThen(_In_ ABI::Windows::Foundation::IAsyncOperationWithProgress<TResult,TProgress> * pOperation,_In_ TLambda && tFunc)79 HRESULT StartAsyncThen(_In_ ABI::Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>* pOperation, _In_ TLambda&& tFunc)
80 {
81 return StartAsyncThen<ABI::Windows::Foundation::IAsyncOperationWithProgressCompletedHandler<TResult, TProgress>, ABI::Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>>(pOperation, static_cast<TLambda&&>(tFunc));
82 }
83
84
85 // eg. TOperation = IAsyncOperationWithProgress<UINT32, UINT32>
86 // eg. THandler = IAsyncOperationWithProgressCompletedHandler<UINT, UINT>
87 template<typename TOperation, typename THandler>
88 class AsyncEventDelegate
89 : public Microsoft::WRL::RuntimeClass
90 < Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::RuntimeClassType::Delegate>
91 , THandler
92 , Microsoft::WRL::FtmBase >
93 {
94 public:
AsyncEventDelegate()95 AsyncEventDelegate()
96 : _completedEvent(CreateEventEx(nullptr, nullptr, 0, EVENT_ALL_ACCESS))
97 {
98 ComPtr<AsyncEventDelegate> spThis(this);
99 auto lambda = ([this, spThis](_In_ HRESULT hr, _In_ TOperation* pOperation)
100 {
101 SetEvent(_completedEvent.Get());
102 });
103 _func = std::move(lambda);
104 }
105
STDMETHOD(Invoke)106 STDMETHOD(Invoke)(
107 _In_ TOperation* pOperation,
108 _In_ AsyncStatus status)
109 {
110 HRESULT hr = S_OK;
111
112 // if we completed successfully, then there is no need for getting hresult
113 if (status != AsyncStatus::Completed)
114 {
115 Microsoft::WRL::ComPtr<TOperation> spOperation(pOperation);
116 Microsoft::WRL::ComPtr<IAsyncInfo> spAsyncInfo;
117 if (SUCCEEDED(spOperation.As(&spAsyncInfo)))
118 {
119 spAsyncInfo->get_ErrorCode(&hr);
120 }
121 }
122
123 _func(hr, pOperation);
124
125 return S_OK;
126 }
127
STDMETHOD(SyncWait)128 STDMETHOD(SyncWait)(_In_ TOperation* pOperation, _In_ DWORD dwMilliseconds)
129 {
130 HRESULT hr = pOperation->put_Completed(this);
131 if (FAILED(hr))
132 {
133 return hr;
134 }
135
136 DWORD dwWait = WaitForSingleObjectEx(_completedEvent.Get(), dwMilliseconds, TRUE);
137 if (WAIT_IO_COMPLETION == dwWait || WAIT_OBJECT_0 == dwWait)
138 return S_OK;
139
140 return HRESULT_FROM_WIN32(GetLastError());
141 }
142
143 private:
144 std::function<void(HRESULT, TOperation*)> _func;
145 Microsoft::WRL::Wrappers::Event _completedEvent;
146 };
147 template <typename TOperation, typename THandler>
SyncWait(_In_ TOperation * pOperation,_In_ DWORD dwMilliseconds)148 HRESULT SyncWait(_In_ TOperation* pOperation, _In_ DWORD dwMilliseconds)
149 {
150 auto spCallback = Microsoft::WRL::Make<AsyncEventDelegate<TOperation, THandler>>();
151
152 return spCallback->SyncWait(pOperation, dwMilliseconds);
153 }
154 template <typename TResult>
155 HRESULT SyncWait(_In_ ABI::Windows::Foundation::IAsyncAction* pOperation, _In_ DWORD dwMilliseconds = INFINITE)
156 {
157 return SyncWait<ABI::Windows::Foundation::IAsyncAction, ABI::Windows::Foundation::IAsyncActionCompletedHandler>(pOperation, dwMilliseconds);
158 }
159 template <typename TResult>
160 HRESULT SyncWait(_In_ ABI::Windows::Foundation::IAsyncOperation<TResult>* pOperation, _In_ DWORD dwMilliseconds = INFINITE)
161 {
162 return SyncWait<ABI::Windows::Foundation::IAsyncOperation<TResult>, ABI::Windows::Foundation::IAsyncOperationCompletedHandler<TResult>>(pOperation, dwMilliseconds);
163 }
164 template <typename TResult, typename TProgress>
165 HRESULT SyncWait(_In_ ABI::Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>* pOperation, _In_ DWORD dwMilliseconds = INFINITE)
166 {
167 return SyncWait<ABI::Windows::Foundation::IAsyncOperationWithProgress<TResult, TProgress>, ABI::Windows::Foundation::IAsyncOperationWithProgressCompletedHandler<TResult, TProgress>>(pOperation, dwMilliseconds);
168 }
169