QtTaskTree::RepeatIterator Class

class QtTaskTree::RepeatIterator

The repetitive iterator to be used inside For element. More...

Header: #include <qtasktree.h>
CMake: find_package(Qt6 REQUIRED COMPONENTS TaskTree)
target_link_libraries(mytarget PRIVATE Qt6::TaskTree)
qmake: QT += tasktree
Inherits: QtTaskTree::Iterator

Note: All functions in this class are reentrant.

Public Functions

RepeatIterator(int count)

Detailed Description

See also Iterator, ForeverIterator, UntilIterator, and ListIterator.

Member Function Documentation

RepeatIterator::RepeatIterator(int count)

Constructs the repetitive iterator for the For (RepeatIterator(count)) >> Do {} construct. The iterator will repeat count times.

Example usage:

 static const int maxTries = 5;
 static const int maxNumber = 10;
 const int luckyNumber = QRandomGenerator().global()->bounded(maxNumber);

 qDebug() << "Today's lucky number is:" << luckyNumber;
 qDebug() << "You have" << maxTries << "lottery tickets.";
 qDebug() << "Let's start the drawing...";

 const auto onNumberCheck = [luckyNumber] {
     const int drawnNumber = QRandomGenerator().global()->bounded(maxNumber);
     qDebug() << "You have drawn:" << drawnNumber;
     return drawnNumber == luckyNumber;
 };

 const auto onDone = [](DoneWith result) {
     if (result == DoneWith::Success)
         qDebug() << "You have won! Congratulations!";
     else if (result == DoneWith::Error)
         qDebug() << "You have lost. Try again.";
 };

 const Group recipe {
     For (RepeatIterator(maxTries)) >> Do {
         stopOnSuccess,
         timeoutTask(1s),
         QSyncTask(onNumberCheck)
     },
     onGroupDone(onDone)
 };

The possible output when the recipe is started by the QTaskTree, and finishes with success:

 Today's lucky number is: 6
 You have 5 lottery tickets.
 Let's start the drawing...
 You have drawn: 9
 You have drawn: 6
 You have won! Congratulations!

In case it finishes with an error, the output might look like:

 Today's lucky number is: 8
 You have 5 lottery tickets.
 Let's start the drawing...
 You have drawn: 1
 You have drawn: 7
 You have drawn: 6
 You have drawn: 7
 You have drawn: 2
 You have lost. Try again.