master
h 97 lines 2.4 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 Helpers.h
8
9 Abstract:
10
11 This file contains helpers for the WinRT wrapper of WSLC SDK.
12
13 --*/
14
15 #pragma once
16
17 #define THROW_MSG_IF_FAILED(hr, msg) \
18 do \
19 { \
20 const auto _hr = (hr); \
21 if (FAILED(_hr)) \
22 { \
23 auto _msg = (msg).get(); \
24 if (_msg) \
25 { \
26 throw winrt::hresult_error(_hr, winrt::to_hstring(_msg)); \
27 } \
28 else \
29 { \
30 winrt::throw_hresult(_hr); \
31 } \
32 } \
33 } while (0)
34
35 template <typename T>
36 struct implementation_type;
37
38 #define DEFINE_TYPE_HELPERS(__type__) \
39 template <> \
40 struct implementation_type<winrt::Microsoft::WSL::Containers::__type__> \
41 { \
42 using type = winrt::Microsoft::WSL::Containers::implementation::__type__; \
43 };
44
45 namespace winrt::Microsoft::WSL::Containers::implementation {
46 template <typename T>
47 auto GetImplementation(const T& obj)
48 {
49 return winrt::get_self<typename implementation_type<T>::type>(obj);
50 }
51
52 template <typename T>
53 auto* GetStructPointer(const T& obj)
54 {
55 return obj ? GetImplementation(obj)->ToStructPointer() : nullptr;
56 }
57
58 template <typename T>
59 auto GetStruct(const T& obj)
60 {
61 return GetImplementation(obj)->ToStruct();
62 }
63
64 template <typename T>
65 auto GetHandle(const T& obj)
66 {
67 return obj ? GetImplementation(obj)->ToHandle() : nullptr;
68 }
69
70 template <typename T>
71 auto* GetStructPointer(const winrt::com_ptr<T>& obj)
72 {
73 return obj ? obj->ToStructPointer() : nullptr;
74 }
75
76 // Helper for forwarding C progress callbacks to a WinRT progress token.
77 // An instance of this struct is used as the context for the C callback.
78 // The callback is invoked synchronously during the blocking C call, so the context
79 // lives on the coroutine stack and is always valid for the duration of the call.
80 template <typename T>
81 struct ProgressCallbackHelper
82 {
83 template <typename ProgressTokenT>
84 ProgressCallbackHelper(ProgressTokenT progressToken) :
85 m_reportProgress([progressToken](T progress) { progressToken(progress); })
86 {
87 }
88
89 static void ReportProgress(PVOID context, T progress)
90 {
91 auto callbackContext = static_cast<ProgressCallbackHelper*>(context);
92 callbackContext->m_reportProgress(progress);
93 }
94
95 const std::function<void(T)> m_reportProgress;
96 };
97 } // namespace winrt::Microsoft::WSL::Containers::implementation