QtTaskTree::Do Class | Extending Qt Creator Manual
Description: A body element used with For and When constructs.

- Extending Qt Creator Manual
- TaskTree
- Do
- Extending Qt Creator Manual 19.0.0

# QtTaskTree::Do Class

class QtTaskTree::Do

A body element used with For and When constructs. More...

Header: | `#include <qtasktree.h>`

Note: All functions in this class are reentrant.

## Detailed Description

A body element, holding a list of tasks to be executed on each For iteration or after When's barrier was advanced.

     For (RepeatIterator(5)) >> Do {
         task1,
         task2
     };

In case of For loop, the Do's body will be executed multiple times. If onGroupSetup() or onGroupDone() handlers are a part of passed children to Do's constructor, they will be executed just once - before the whole loop starts, and after the whole loop is finished:

     const RepeatIterator iterator(3);

     const auto onSetup = [] { qDebug() << "Setup"; };

     const auto onSync = [iterator] { qDebug() << "Current iteration:" << iterator->iteration(); };

     const auto onDone = [] { qDebug() << "Done"; };

     const Group recipe = For (iterator) >> Do {
         onGroupSetup(onSetup),
         QSyncTask(onSync),
         onDone(onDone)
     };

The above recipe, when executed, will output:

     Setup
     Current iteration: 0
     Current iteration: 1
     Current iteration: 2
     Done

If the intention is to invoke group handlers on each iteration, enclose the Do body with an extra Group, like:

     const Group recipe = For (iterator) >> Do {
         Group {
             onGroupSetup(onSetup),
             QSyncTask(onSync),
             onDone(onDone)
         }
     };

The latter recipe, when executed, will output:

     Setup
     Current iteration: 0
     Done
     Setup
     Current iteration: 1
     Done
     Setup
     Current iteration: 2
     Done

The similar happens when the Storage passed as a direct child of the body. The storage structure is instantiated just once, before the whole loop starts, and is destroyed just after the whole loop is finished. If the intention is to have separate instance of the Storage for each iteration, enclose the Do body with an extra Group.

Be careful with placing parallel element in the Do's body of the For loop, as all iterations will start in parallel. This might not be desired, especially in case of ForeverIterator.

See also For and When.