forked from qt-creator/qt-creator
TaskTree: Introduce LoopList
A wrapper around container with convenient "const T *operator->() const" and "const T &operator*() const" methods. They mimic the const_iterator API. Simplifies the usages, as it enough not to capture just LoopList element inside task handlers without a container itself. For now it handles just QList container. To be generalized later. Change-Id: I10ea3903985cf51529ec5909ac7c19c524de7c68 Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -1249,6 +1249,7 @@ public:
|
||||
}
|
||||
|
||||
const std::optional<int> m_loopCount = {};
|
||||
const Loop::ValueGetter m_valueGetter = {};
|
||||
const Loop::Condition m_condition = {};
|
||||
std::mutex m_threadDataMutex = {};
|
||||
// Use std::map on purpose, so that it doesn't invalidate references on modifications.
|
||||
@@ -1260,12 +1261,12 @@ Loop::Loop()
|
||||
: m_loopData(new LoopData)
|
||||
{}
|
||||
|
||||
Loop::Loop(int count)
|
||||
: m_loopData(new LoopData{count})
|
||||
Loop::Loop(int count, const ValueGetter &valueGetter)
|
||||
: m_loopData(new LoopData{count, valueGetter})
|
||||
{}
|
||||
|
||||
Loop::Loop(const Condition &condition)
|
||||
: m_loopData(new LoopData{{}, condition})
|
||||
: m_loopData(new LoopData{{}, {}, condition})
|
||||
{}
|
||||
|
||||
int Loop::iteration() const
|
||||
@@ -1273,6 +1274,11 @@ int Loop::iteration() const
|
||||
return m_loopData->threadData().iteration();
|
||||
}
|
||||
|
||||
const void *Loop::valuePtr() const
|
||||
{
|
||||
return m_loopData->m_valueGetter(iteration());
|
||||
}
|
||||
|
||||
using StoragePtr = void *;
|
||||
|
||||
class StorageThreadData
|
||||
|
@@ -6,6 +6,7 @@
|
||||
#include "tasking_global.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QList>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include <memory>
|
||||
@@ -101,14 +102,17 @@ class TASKING_EXPORT Loop
|
||||
{
|
||||
public:
|
||||
using Condition = std::function<bool(int)>; // Takes iteration, called prior to each iteration.
|
||||
using ValueGetter = std::function<const void *(int)>; // Takes iteration, returns ptr to ref.
|
||||
|
||||
int iteration() const;
|
||||
|
||||
protected:
|
||||
Loop(); // LoopForever
|
||||
Loop(int count); // LoopRepeat
|
||||
Loop(int count, const ValueGetter &valueGetter = {}); // LoopRepeat, LoopList
|
||||
Loop(const Condition &condition); // LoopUntil
|
||||
|
||||
const void *valuePtr() const;
|
||||
|
||||
private:
|
||||
friend class ExecutionContextActivator;
|
||||
friend class TaskTreePrivate;
|
||||
@@ -133,6 +137,15 @@ public:
|
||||
LoopUntil(const Condition &condition) : Loop(condition) {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class LoopList final : public Loop
|
||||
{
|
||||
public:
|
||||
LoopList(const QList<T> &list) : Loop(list.size(), [list](int i) { return &list.at(i); }) {}
|
||||
const T *operator->() const { return static_cast<const T *>(valuePtr()); }
|
||||
const T &operator*() const { return *static_cast<const T *>(valuePtr()); }
|
||||
};
|
||||
|
||||
class TASKING_EXPORT StorageBase
|
||||
{
|
||||
private:
|
||||
|
Reference in New Issue
Block a user