Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/QtCore/qresultstore.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (C) 2020 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QTCORE_RESULTSTORE_H
0005 #define QTCORE_RESULTSTORE_H
0006 
0007 #include <QtCore/qmap.h>
0008 
0009 #include <utility>
0010 
0011 QT_REQUIRE_CONFIG(future);
0012 
0013 QT_BEGIN_NAMESPACE
0014 
0015 /*
0016     ResultStore stores indexed results. Results can be added and retrieved
0017     either individually batched in a QList. Retriveing results and checking
0018     which indexes are in the store can be done either by iterating or by random
0019     access. In addition results can be removed from the front of the store,
0020     either individually or in batches.
0021 */
0022 
0023 namespace QtPrivate {
0024 
0025 class ResultItem
0026 {
0027 public:
0028     ResultItem(const void *_result, int _count) : m_count(_count), result(_result) { } // construct with vector of results
0029     ResultItem(const void *_result) : m_count(0), result(_result) { } // construct with result
0030     ResultItem() : m_count(0), result(nullptr) { }
0031     bool isValid() const { return result != nullptr; }
0032     bool isVector() const { return m_count != 0; }
0033     int count() const { return (m_count == 0) ?  1 : m_count; }
0034     int m_count;          // result is either a pointer to a result or to a vector of results,
0035     const void *result; // if count is 0 it's a result, otherwise it's a vector.
0036 };
0037 
0038 class Q_CORE_EXPORT ResultIteratorBase
0039 {
0040 public:
0041     ResultIteratorBase();
0042     ResultIteratorBase(QMap<int, ResultItem>::const_iterator _mapIterator, int _vectorIndex = 0);
0043     int vectorIndex() const;
0044     int resultIndex() const;
0045 
0046     ResultIteratorBase operator++();
0047     int batchSize() const;
0048     void batchedAdvance();
0049     bool operator==(const ResultIteratorBase &other) const;
0050     bool operator!=(const ResultIteratorBase &other) const;
0051     bool isVector() const;
0052     bool canIncrementVectorIndex() const;
0053     bool isValid() const;
0054 
0055 protected:
0056     QMap<int, ResultItem>::const_iterator mapIterator;
0057     int m_vectorIndex;
0058 public:
0059     template <typename T>
0060     const T &value() const
0061     {
0062         return *pointer<T>();
0063     }
0064 
0065     template<typename T>
0066     T &value()
0067     {
0068         return *pointer<T>();
0069     }
0070 
0071     template <typename T>
0072     T *pointer()
0073     {
0074         const T *p = std::as_const(*this).pointer<T>();
0075         return const_cast<T *>(p);
0076     }
0077 
0078     template <typename T>
0079     const T *pointer() const
0080     {
0081         if (mapIterator.value().isVector())
0082             return &(reinterpret_cast<const QList<T> *>(mapIterator.value().result)->at(m_vectorIndex));
0083         else
0084             return reinterpret_cast<const T *>(mapIterator.value().result);
0085     }
0086 };
0087 
0088 class Q_CORE_EXPORT ResultStoreBase final
0089 {
0090 public:
0091     ResultStoreBase();
0092     void setFilterMode(bool enable);
0093     bool filterMode() const;
0094     int addResult(int index, const void *result);
0095     int addResults(int index, const void *results, int vectorSize, int logicalCount);
0096     ResultIteratorBase begin() const;
0097     ResultIteratorBase end() const;
0098     bool hasNextResult() const;
0099     ResultIteratorBase resultAt(int index) const;
0100     bool contains(int index) const;
0101     int count() const;
0102     // ### Qt 7: 'virtual' isn't required, can be removed, along with renaming
0103     // the class to ResultStore and changing the members below to be private.
0104     virtual ~ResultStoreBase();
0105 
0106 protected:
0107     int insertResultItem(int index, ResultItem &resultItem);
0108     void insertResultItemIfValid(int index, ResultItem &resultItem);
0109     bool containsValidResultItem(int index) const;
0110     void syncPendingResults();
0111     void syncResultCount();
0112     int updateInsertIndex(int index, int _count);
0113 
0114     QMap<int, ResultItem> m_results;
0115     int insertIndex;     // The index where the next results(s) will be inserted.
0116     int resultCount;     // The number of consecutive results stored, starting at index 0.
0117 
0118     bool m_filterMode;
0119     QMap<int, ResultItem> pendingResults;
0120     int filteredResults;
0121 
0122     template <typename T>
0123     static void clear(QMap<int, ResultItem> &store)
0124     {
0125         QMap<int, ResultItem>::const_iterator mapIterator = store.constBegin();
0126         while (mapIterator != store.constEnd()) {
0127             if (mapIterator.value().isVector())
0128                 delete reinterpret_cast<const QList<T> *>(mapIterator.value().result);
0129             else
0130                 delete reinterpret_cast<const T *>(mapIterator.value().result);
0131             ++mapIterator;
0132         }
0133         store.clear();
0134     }
0135 
0136 public:
0137     template <typename T, typename...Args>
0138     int emplaceResult(int index, Args&&...args)
0139     {
0140         if (containsValidResultItem(index)) // reject if already present
0141             return -1;
0142         return addResult(index, static_cast<void *>(new T(std::forward<Args>(args)...)));
0143     }
0144 
0145     template <typename T>
0146     int addResult(int index, const T *result)
0147     {
0148         if (containsValidResultItem(index)) // reject if already present
0149             return -1;
0150 
0151         if (result == nullptr)
0152             return addResult(index, static_cast<void *>(nullptr));
0153 
0154         return addResult(index, static_cast<void *>(new T(*result)));
0155     }
0156 
0157     template <typename T>
0158     int moveResult(int index, T &&result)
0159     {
0160         static_assert(!std::is_reference_v<T>, "trying to move from an lvalue!");
0161 
0162         return emplaceResult<std::remove_cv_t<T>>(index, std::forward<T>(result));
0163     }
0164 
0165     template<typename T>
0166     int addResults(int index, const QList<T> *results)
0167     {
0168         if (results->empty()) // reject if results are empty
0169             return -1;
0170 
0171         if (containsValidResultItem(index)) // reject if already present
0172             return -1;
0173 
0174         return addResults(index, new QList<T>(*results), results->size(), results->size());
0175     }
0176 
0177     template<typename T>
0178     int addResults(int index, const QList<T> *results, int totalCount)
0179     {
0180         // reject if results are empty, and nothing is filtered away
0181         if ((m_filterMode == false || results->size() == totalCount) && results->empty())
0182             return -1;
0183 
0184         if (containsValidResultItem(index)) // reject if already present
0185             return -1;
0186 
0187         if (m_filterMode == true && results->size() != totalCount && 0 == results->size())
0188             return addResults(index, nullptr, 0, totalCount);
0189 
0190         return addResults(index, new QList<T>(*results), results->size(), totalCount);
0191     }
0192 
0193     int addCanceledResult(int index)
0194     {
0195         if (containsValidResultItem(index)) // reject if already present
0196             return -1;
0197 
0198         return addResult(index, static_cast<void *>(nullptr));
0199     }
0200 
0201     template <typename T>
0202     int addCanceledResults(int index, int _count)
0203     {
0204         if (containsValidResultItem(index)) // reject if already present
0205             return -1;
0206 
0207         QList<T> empty;
0208         return addResults(index, &empty, _count);
0209     }
0210 
0211     template <typename T>
0212     void clear()
0213     {
0214         ResultStoreBase::clear<T>(m_results);
0215         resultCount = 0;
0216         insertIndex = 0;
0217         ResultStoreBase::clear<T>(pendingResults);
0218         filteredResults = 0;
0219     }
0220 };
0221 
0222 } // namespace QtPrivate
0223 
0224 Q_DECLARE_TYPEINFO(QtPrivate::ResultItem, Q_PRIMITIVE_TYPE);
0225 
0226 
0227 QT_END_NAMESPACE
0228 
0229 #endif