Skip to content

Commit a8455cb

Browse files
authored
Update README.md
1 parent d4d5b4e commit a8455cb

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

README.md

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ When you need to calculate value, call one of the *asyncXXX* functions:
4040
4141
}, "Loading...", ASYNC_CAN_REQUEST_STOP::YES);
4242
```
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.
4347

4448
Somewhere in GUI code declare async widget:
4549
```C++
@@ -91,6 +95,11 @@ User can initialize `AsyncValue` using value or error, `AsyncInitByValue` and `A
9195
```
9296
9397
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+
```
94103

95104
To get the content of the async value use `access` functions and supply callables to access either value or error or progress:
96105
```C++
@@ -128,19 +137,22 @@ User can assign error in a similar way:
128137
template <typename AsyncValueType, typename Func, typename... ProgressArgs>
129138
bool asyncValueRunThreadPool(QThreadPool *pool, AsyncValueType& value, Func&& func, ProgressArgs&& ...progressArgs)
130139
{
131-
// create progress object
140+
// create progress
132141
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)))
135146
return false;
136147
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+
140154
// run calculation
141-
func(*progress, value);
142-
// post progress stuff
143-
value.completeProgress(progress.get());
155+
func(*progressPtr, value);
144156
});
145157
146158
return true;
@@ -323,3 +335,27 @@ The `ProgressType_t` parameter represented by `AsyncProgress` with the following
323335
void incompleteProgress() const;
324336
};
325337
```
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

Comments
 (0)