summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qresultstore.h
blob: f633ed78019db636d07b69b099f14804cdb90b70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QTCORE_RESULTSTORE_H
#define QTCORE_RESULTSTORE_H

#include <QtCore/qmap.h>

#include <utility>

QT_REQUIRE_CONFIG(future);

QT_BEGIN_NAMESPACE

/*
    ResultStore stores indexed results. Results can be added and retrieved
    either individually batched in a QList. Retriveing results and checking
    which indexes are in the store can be done either by iterating or by random
    access. In addition results can be removed from the front of the store,
    either individually or in batches.
*/

namespace QtPrivate {

class ResultItem
{
public:
    ResultItem(const void *_result, int _count) : m_count(_count), result(_result) { } // construct with vector of results
    ResultItem(const void *_result) : m_count(0), result(_result) { } // construct with result
    ResultItem() : m_count(0), result(nullptr) { }
    bool isValid() const { return result != nullptr; }
    bool isVector() const { return m_count != 0; }
    int count() const { return (m_count == 0) ?  1 : m_count; }
    int m_count;          // result is either a pointer to a result or to a vector of results,
    const void *result; // if count is 0 it's a result, otherwise it's a vector.
};

class Q_CORE_EXPORT ResultIteratorBase
{
public:
    ResultIteratorBase();
    ResultIteratorBase(QMap<int, ResultItem>::const_iterator _mapIterator, int _vectorIndex = 0);
    int vectorIndex() const;
    int resultIndex() const;

    ResultIteratorBase operator++();
    int batchSize() const;
    void batchedAdvance();
    bool operator==(const ResultIteratorBase &other) const;
    bool operator!=(const ResultIteratorBase &other) const;
    bool isVector() const;
    bool canIncrementVectorIndex() const;
    bool isValid() const;

protected:
    QMap<int, ResultItem>::const_iterator mapIterator;
    int m_vectorIndex;
public:
    template <typename T>
    const T &value() const
    {
        return *pointer<T>();
    }

    template<typename T>
    T &value()
    {
        return *pointer<T>();
    }

    template <typename T>
    T *pointer()
    {
        const T *p = qAsConst(*this).pointer<T>();
        return const_cast<T *>(p);
    }

    template <typename T>
    const T *pointer() const
    {
        if (mapIterator.value().isVector())
            return &(reinterpret_cast<const QList<T> *>(mapIterator.value().result)->at(m_vectorIndex));
        else
            return reinterpret_cast<const T *>(mapIterator.value().result);
    }
};

class Q_CORE_EXPORT ResultStoreBase final
{
public:
    ResultStoreBase();
    void setFilterMode(bool enable);
    bool filterMode() const;
    int addResult(int index, const void *result);
    int addResults(int index, const void *results, int vectorSize, int logicalCount);
    ResultIteratorBase begin() const;
    ResultIteratorBase end() const;
    bool hasNextResult() const;
    ResultIteratorBase resultAt(int index) const;
    bool contains(int index) const;
    int count() const;
    // ### Qt 7: 'virtual' isn't required, can be removed, along with renaming
    // the class to ResultStore and changing the members below to be private.
    virtual ~ResultStoreBase();

protected:
    int insertResultItem(int index, ResultItem &resultItem);
    void insertResultItemIfValid(int index, ResultItem &resultItem);
    bool containsValidResultItem(int index) const;
    void syncPendingResults();
    void syncResultCount();
    int updateInsertIndex(int index, int _count);

    QMap<int, ResultItem> m_results;
    int insertIndex;     // The index where the next results(s) will be inserted.
    int resultCount;     // The number of consecutive results stored, starting at index 0.

    bool m_filterMode;
    QMap<int, ResultItem> pendingResults;
    int filteredResults;

    template <typename T>
    static void clear(QMap<int, ResultItem> &store)
    {
        QMap<int, ResultItem>::const_iterator mapIterator = store.constBegin();
        while (mapIterator != store.constEnd()) {
            if (mapIterator.value().isVector())
                delete reinterpret_cast<const QList<T> *>(mapIterator.value().result);
            else
                delete reinterpret_cast<const T *>(mapIterator.value().result);
            ++mapIterator;
        }
        store.clear();
    }

public:
    template <typename T>
    int addResult(int index, const T *result)
    {
        if (containsValidResultItem(index)) // reject if already present
            return -1;

        if (result == nullptr)
            return addResult(index, static_cast<void *>(nullptr));

        return addResult(index, static_cast<void *>(new T(*result)));
    }

    template <typename T>
    int moveResult(int index, T &&result)
    {
        if (containsValidResultItem(index)) // reject if already present
            return -1;

        return addResult(index, static_cast<void *>(new T(std::move_if_noexcept(result))));
    }

    template<typename T>
    int addResults(int index, const QList<T> *results)
    {
        if (results->empty()) // reject if results are empty
            return -1;

        if (containsValidResultItem(index)) // reject if already present
            return -1;

        return addResults(index, new QList<T>(*results), results->count(), results->count());
    }

    template<typename T>
    int addResults(int index, const QList<T> *results, int totalCount)
    {
        // reject if results are empty, and nothing is filtered away
        if ((m_filterMode == false || results->count() == totalCount) && results->empty())
            return -1;

        if (containsValidResultItem(index)) // reject if already present
            return -1;

        if (m_filterMode == true && results->count() != totalCount && 0 == results->count())
            return addResults(index, nullptr, 0, totalCount);

        return addResults(index, new QList<T>(*results), results->count(), totalCount);
    }

    int addCanceledResult(int index)
    {
        if (containsValidResultItem(index)) // reject if already present
            return -1;

        return addResult(index, static_cast<void *>(nullptr));
    }

    template <typename T>
    int addCanceledResults(int index, int _count)
    {
        if (containsValidResultItem(index)) // reject if already present
            return -1;

        QList<T> empty;
        return addResults(index, &empty, _count);
    }

    template <typename T>
    void clear()
    {
        ResultStoreBase::clear<T>(m_results);
        resultCount = 0;
        insertIndex = 0;
        ResultStoreBase::clear<T>(pendingResults);
        filteredResults = 0;
    }
};

} // namespace QtPrivate

Q_DECLARE_TYPEINFO(QtPrivate::ResultItem, Q_PRIMITIVE_TYPE);


QT_END_NAMESPACE

#endif