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 std::optional<int> m_loopCount = {};
|
||||||
|
const Loop::ValueGetter m_valueGetter = {};
|
||||||
const Loop::Condition m_condition = {};
|
const Loop::Condition m_condition = {};
|
||||||
std::mutex m_threadDataMutex = {};
|
std::mutex m_threadDataMutex = {};
|
||||||
// Use std::map on purpose, so that it doesn't invalidate references on modifications.
|
// Use std::map on purpose, so that it doesn't invalidate references on modifications.
|
||||||
@@ -1260,12 +1261,12 @@ Loop::Loop()
|
|||||||
: m_loopData(new LoopData)
|
: m_loopData(new LoopData)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Loop::Loop(int count)
|
Loop::Loop(int count, const ValueGetter &valueGetter)
|
||||||
: m_loopData(new LoopData{count})
|
: m_loopData(new LoopData{count, valueGetter})
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Loop::Loop(const Condition &condition)
|
Loop::Loop(const Condition &condition)
|
||||||
: m_loopData(new LoopData{{}, condition})
|
: m_loopData(new LoopData{{}, {}, condition})
|
||||||
{}
|
{}
|
||||||
|
|
||||||
int Loop::iteration() const
|
int Loop::iteration() const
|
||||||
@@ -1273,6 +1274,11 @@ int Loop::iteration() const
|
|||||||
return m_loopData->threadData().iteration();
|
return m_loopData->threadData().iteration();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const void *Loop::valuePtr() const
|
||||||
|
{
|
||||||
|
return m_loopData->m_valueGetter(iteration());
|
||||||
|
}
|
||||||
|
|
||||||
using StoragePtr = void *;
|
using StoragePtr = void *;
|
||||||
|
|
||||||
class StorageThreadData
|
class StorageThreadData
|
||||||
|
@@ -6,6 +6,7 @@
|
|||||||
#include "tasking_global.h"
|
#include "tasking_global.h"
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include <QList>
|
||||||
#include <QSharedPointer>
|
#include <QSharedPointer>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -101,14 +102,17 @@ class TASKING_EXPORT Loop
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using Condition = std::function<bool(int)>; // Takes iteration, called prior to each iteration.
|
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;
|
int iteration() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Loop(); // LoopForever
|
Loop(); // LoopForever
|
||||||
Loop(int count); // LoopRepeat
|
Loop(int count, const ValueGetter &valueGetter = {}); // LoopRepeat, LoopList
|
||||||
Loop(const Condition &condition); // LoopUntil
|
Loop(const Condition &condition); // LoopUntil
|
||||||
|
|
||||||
|
const void *valuePtr() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class ExecutionContextActivator;
|
friend class ExecutionContextActivator;
|
||||||
friend class TaskTreePrivate;
|
friend class TaskTreePrivate;
|
||||||
@@ -133,6 +137,15 @@ public:
|
|||||||
LoopUntil(const Condition &condition) : Loop(condition) {}
|
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
|
class TASKING_EXPORT StorageBase
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
Reference in New Issue
Block a user