CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/UWP/UWPHelpers/StorageAsync.h
Views: 1401
1
// Thanks to RetroArch/Libretro team for this idea
2
// This is improved version of the original idea
3
4
#pragma once
5
6
#include "pch.h"
7
#include <ppl.h>
8
#include <ppltasks.h>
9
#include <wrl.h>
10
#include <wrl/implements.h>
11
12
#include "Common/Log.h"
13
#include "UWPUtil.h"
14
15
using namespace Windows::UI::Core;
16
17
// Don't add 'using' 'Windows::Foundation'
18
// it might cause confilct with some types like 'Point'
19
20
#pragma region Async Handlers
21
22
template<typename T>
23
T TaskHandler(std::function<concurrency::task<T>()> wtask, T def)
24
{
25
T result = def;
26
bool done = false;
27
wtask().then([&](concurrency::task<T> t) {
28
try
29
{
30
result = t.get();
31
}
32
catch (Platform::Exception^ exception_)
33
{
34
ERROR_LOG(Log::FileSystem, FromPlatformString(exception_->Message).c_str());
35
}
36
done = true;
37
});
38
39
CoreWindow^ corewindow = CoreWindow::GetForCurrentThread();
40
while (!done)
41
{
42
try {
43
if (corewindow) {
44
corewindow->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
45
}
46
else {
47
corewindow = CoreWindow::GetForCurrentThread();
48
}
49
}
50
catch (...) {
51
52
}
53
}
54
55
return result;
56
};
57
58
template<typename T>
59
T TaskPass(Windows::Foundation::IAsyncOperation<T>^ task, T def)
60
{
61
return TaskHandler<T>([&]() {
62
return concurrency::create_task(task).then([](T res) {
63
return res;
64
});
65
}, def);
66
}
67
68
bool ActionPass(Windows::Foundation::IAsyncAction^ action);
69
70
#pragma endregion
71
72
// Now it's more simple to execute async task
73
// @out: output variable
74
// @task: async task
75
template<typename T>
76
void ExecuteTask(T& out, Windows::Foundation::IAsyncOperation<T>^ task)
77
{
78
try {
79
out = TaskPass<T>(task, T());
80
}
81
catch (...) {
82
out = T();
83
}
84
};
85
86
// For specific return default value
87
// @out: output variable
88
// @task: async task
89
// @def: default value when fail
90
template<typename T>
91
void ExecuteTask(T& out, Windows::Foundation::IAsyncOperation<T>^ task, T def)
92
{
93
try{
94
out = TaskPass<T>(task, def);
95
}
96
catch (...) {
97
out = def;
98
}
99
};
100
101
102
// Async action such as 'Delete' file
103
// @action: async action
104
// return false when action failed
105
bool ExecuteTask(Windows::Foundation::IAsyncAction^ action);
106
107