@@ -40,6 +40,10 @@ When you need to calculate value, call one of the *asyncXXX* functions:
40
40
41
41
}, "Loading...", ASYNC_CAN_REQUEST_STOP::YES);
42
42
```
43
+ The available functions are:
44
+ * ` asyncValueRunThread ` - creates QThread, does calculations and deletes QThread (don't use this function)
45
+ * ` asyncValueRunThreadPool ` - does calculation in a Qt thread pool
46
+ * ` asyncValueRunNetwork ` - waits QNetworkReply and does calculation from it.
43
47
44
48
Somewhere in GUI code declare async widget:
45
49
``` C++
@@ -91,6 +95,11 @@ User can initialize `AsyncValue` using value or error, `AsyncInitByValue` and `A
91
95
```
92
96
93
97
There is `stateChanged` signal that is emitted when async value's state changes between value, error and progress.
98
+ ```C++
99
+ QObject::connect(value, &AsyncValueBase::stateChanged, [](ASYNC_VALUE_STATE state) {
100
+ // async value state change handler
101
+ });
102
+ ```
94
103
95
104
To get the content of the async value use ` access ` functions and supply callables to access either value or error or progress:
96
105
``` C++
@@ -128,19 +137,22 @@ User can assign error in a similar way:
128
137
template <typename AsyncValueType, typename Func, typename... ProgressArgs>
129
138
bool asyncValueRunThreadPool(QThreadPool *pool, AsyncValueType& value, Func&& func, ProgressArgs&& ...progressArgs)
130
139
{
131
- // create progress object
140
+ // create progress
132
141
auto progress = std::make_unique<typename AsyncValueType::ProgressType>(std::forward<ProgressArgs>(progressArgs)...);
133
- // try to start progress
134
- if (!value.startProgress(progress.get()))
142
+ auto progressPtr = progress.get();
143
+
144
+ // try to switch async value to progress state
145
+ if (!value.startProgress(std::move(progress)))
135
146
return false;
136
147
137
- QtConcurrent::run(pool, [&value, progressPtr = progress.release(), func = std::forward<Func>(func)](){
138
- // make unique_ptr from raw ptr
139
- std::unique_ptr<typename AsyncValueType::ProgressType> progress(progressPtr);
148
+ QtConcurrent::run(pool, [&value, progressPtr, func = std::forward<Func>(func)](){
149
+ SCOPE_EXIT {
150
+ // post progress stuff
151
+ value.completeProgress(progressPtr);
152
+ };
153
+
140
154
// run calculation
141
- func(*progress, value);
142
- // post progress stuff
143
- value.completeProgress(progress.get());
155
+ func(*progressPtr, value);
144
156
});
145
157
146
158
return true;
@@ -323,3 +335,27 @@ The `ProgressType_t` parameter represented by `AsyncProgress` with the following
323
335
void incompleteProgress() const;
324
336
};
325
337
```
338
+
339
+ To use sdync values with different asynchronious API or framework you can create asynXXX like function.
340
+ The schema is simple:
341
+ ``` C++
342
+ template <typename AsyncValueType, ...>
343
+ bool asyncValueMyFramework (AsyncValueType& value, Routine func, ...)
344
+ {
345
+ // create progress
346
+ auto progress = std::make_unique<typename AsyncValueType::ProgressType >(...);
347
+ auto progressPtr = progress.get();
348
+
349
+ // try to switch value to progress state
350
+ if (!value.startProgress(std::move(progress)))
351
+ return false;
352
+
353
+ // move async value and calculation routine to MyFramework
354
+ MyFramework::AsyncCall([&value, progressPtr, func = std::forward<Func>(func)]() {
355
+ func(...);
356
+
357
+ // finalize progress
358
+ value..completeProgress(progressPtr);
359
+ });
360
+ }
361
+ ```
0 commit comments