Plasma Engine  2.0
Loading...
Searching...
No Matches
UWPUtils.h
1#pragma once
2
3#if PL_DISABLED(PL_PLATFORM_WINDOWS_UWP)
4# error "uwp util header should only be included in UWP builds!"
5#endif
6
7#include <Foundation/Basics/Platform/Win/HResultUtils.h>
8#include <Foundation/Types/Types.h>
9
10#include <Foundation/Basics/Platform/Win/IncludeWindows.h>
11
12#include <asyncinfo.h>
13#include <guiddef.h>
14#include <windows.foundation.collections.h>
15#include <windows.foundation.h>
16#include <windows.foundation.numerics.h>
17#include <wrl/client.h>
18#include <wrl/event.h>
19#include <wrl/implements.h>
20#include <wrl/wrappers/corewrappers.h>
21
22class plUuid;
23
24using namespace Microsoft::WRL::Wrappers;
25using namespace Microsoft::WRL;
26
27namespace plUwpUtils
28{
32 template <typename ElementQueryType, typename ElementType, typename Callback>
33 HRESULT plWinRtIterateIVectorView(
34 const ComPtr<ABI::Windows::Foundation::Collections::IVectorView<ElementType>>& pVectorView, const Callback& callback)
35 {
36 UINT numElements = 0;
37 HRESULT result = pVectorView->get_Size(&numElements);
38 if (FAILED(result))
39 return result;
40
41 for (UINT i = 0; i < numElements; ++i)
42 {
43 ElementQueryType pElement;
44 result = pVectorView->GetAt(i, &pElement);
45 if (FAILED(result))
46 return result;
47
48 if (!callback(i, pElement))
49 return S_OK;
50 }
51
52 return result;
53 }
54
58 template <typename ValueInterfaceType, typename KeyType, typename ValueType, typename Callback>
59 void plWinRtIterateIMapView(const ComPtr<ABI::Windows::Foundation::Collections::IMapView<KeyType, ValueType>>& pMapView, const Callback& callback)
60 {
61 using namespace ABI::Windows::Foundation::Collections;
62 using namespace ABI::Windows::Foundation::Internal;
63
64 using MapInterface = IIterable<IKeyValuePair<KeyType, GetLogicalType<ValueType>::type>*>;
65
66 ComPtr<MapInterface> pIterable;
67 pMapView->QueryInterface<MapInterface>(&pIterable);
68
69 PL_ASSERT_DEBUG(pIterable != nullptr, "Invalid map iteration interface");
70
71 ComPtr<IIterator<IKeyValuePair<KeyType, ValueType>*>> iterator;
72 pIterable->First(&iterator);
73
74 boolean hasCurrent = FALSE;
75 iterator->get_HasCurrent(&hasCurrent);
76
77 while (hasCurrent == TRUE)
78 {
79 IKeyValuePair<KeyType, ValueType>* pair;
80 iterator->get_Current(&pair);
81
82 KeyType key;
83 pair->get_Key(&key);
84
85 ValueInterfaceType value;
86 pair->get_Value(&value);
87
88 if (!callback(key, value))
89 return;
90
91 iterator->MoveNext(&hasCurrent);
92 }
93 }
94
104 template <typename AsyncCompletedType, typename AsyncResultType, typename Callback, typename ObjectType>
105 HRESULT plWinRtPutCompleted(ObjectType& object, Callback callback)
106 {
107 object->put_Completed(Microsoft::WRL::Callback<ABI::Windows::Foundation::IAsyncOperationCompletedHandler<AsyncCompletedType>>(
108 [callback](ABI::Windows::Foundation::IAsyncOperation<AsyncCompletedType>* pCompletion, AsyncStatus status)
109 {
110 if (status != ABI::Windows::Foundation::AsyncStatus::Completed)
111 return S_OK;
112
113 AsyncResultType pResult;
114 pCompletion->GetResults(&pResult);
115
116 callback(pResult);
117
118 return S_OK;
119 }).Get());
120
121 return S_OK;
122 }
123
125 template <typename T>
126 void RetrieveStatics(const WCHAR* szRuntimeClassName, ComPtr<T>& out_Ptr)
127 {
128 if (FAILED(ABI::Windows::Foundation::GetActivationFactory(HStringReference(szRuntimeClassName).Get(), &out_Ptr)))
129 {
130 PL_REPORT_FAILURE("Failed to retrieve activation factory (statics) for '{0}'", plStringUtf8(szRuntimeClassName).GetData());
131 out_Ptr = nullptr;
132 }
133 }
134
139 template <typename T>
140 void CreateInstance(const WCHAR* szRuntimeClassName, ComPtr<T>& out_Ptr)
141 {
142 ::RoActivateInstance(HStringReference(szRuntimeClassName).Get(), &out_Ptr);
143 }
144
145 plMat4 PL_FOUNDATION_DLL ConvertMat4(const ABI::Windows::Foundation::Numerics::Matrix4x4& in);
146
147 plVec3 PL_FOUNDATION_DLL ConvertVec3(const ABI::Windows::Foundation::Numerics::Vector3& in);
148 void PL_FOUNDATION_DLL ConvertVec3(const plVec3& in, ABI::Windows::Foundation::Numerics::Vector3& out);
149
150 plQuat PL_FOUNDATION_DLL ConvertQuat(const ABI::Windows::Foundation::Numerics::Quaternion& in);
151 void PL_FOUNDATION_DLL ConvertQuat(const plQuat& in, ABI::Windows::Foundation::Numerics::Quaternion& out);
152
153 plUuid PL_FOUNDATION_DLL ConvertGuid(const GUID& in);
154 void PL_FOUNDATION_DLL ConvertGuid(const plUuid& in, GUID& out);
155} // namespace plUwpUtils
A small string class that converts any other encoding to Utf8.
Definition StringConversion.h:47
This data type is the abstraction for 128-bit Uuid (also known as GUID) instances.
Definition Uuid.h:11