| 1 | /* |
| 2 | * Copyright 2015 Canonical Ltd. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation; version 3. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #ifndef POOL_P_H |
| 18 | #define POOL_P_H |
| 19 | |
| 20 | #include <QtCore/QVector> |
| 21 | |
| 22 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 23 | #include <LomiriGestures-Qt6/lomirigesturesglobal.h> |
| 24 | #else |
| 25 | #include <LomiriGestures/lomirigesturesglobal.h> |
| 26 | #endif |
| 27 | |
| 28 | /* |
| 29 | An object pool. |
| 30 | Avoids unnecessary creations/initializations and deletions/destructions of items. Useful |
| 31 | in a scenario where items are created and destroyed very frequently but the total number |
| 32 | of items at any given time remains small. They're stored in a unordered fashion. |
| 33 | |
| 34 | To be used in Pool, ItemType needs to have the following methods: |
| 35 | |
| 36 | - ItemType(); |
| 37 | |
| 38 | A constructor that takes no parameters. An object contructed with it must return false if |
| 39 | isValid() is called. |
| 40 | |
| 41 | - bool isValid() const; |
| 42 | |
| 43 | Returns wheter the object holds a valid , "filled" state or is empty. |
| 44 | Used by Pool to check if the slot occupied by this object is actually available. |
| 45 | |
| 46 | - void reset(); |
| 47 | |
| 48 | Resets the object to its initial, empty, state. After calling this method, isValid() must |
| 49 | return false. |
| 50 | */ |
| 51 | template <class ItemType> class Pool |
| 52 | { |
| 53 | public: |
| 54 | Pool() : m_lastUsedIndex(-1) { |
| 55 | } |
| 56 | |
| 57 | class Iterator { |
| 58 | public: |
| 59 | Iterator() : index(-1), item(nullptr) {} |
| 60 | Iterator(int index, ItemType *item) |
| 61 | : index(index), item(item) {} |
| 62 | Iterator(const Iterator&) = default; |
| 63 | |
| 64 | ItemType *operator->() const { return item; } |
| 65 | ItemType &operator*() const { return *item; } |
| 66 | ItemType &value() const { return *item; } |
| 67 | |
| 68 | Iterator &operator= (const Iterator& other) { |
| 69 | index = other.index; |
| 70 | item = other.item; |
| 71 | |
| 72 | // by convention, always return *this |
| 73 | return *this; |
| 74 | } |
| 75 | |
| 76 | operator bool() const { return item != nullptr; } |
| 77 | |
| 78 | int index; |
| 79 | ItemType *item; |
| 80 | }; |
| 81 | |
| 82 | ItemType &getEmptySlot() { |
| 83 | Q_ASSERT(m_lastUsedIndex < m_slots.size()); |
| 84 | |
| 85 | // Look for an in-between vacancy first |
| 86 | for (int i = 0; i < m_lastUsedIndex; ++i) { |
| 87 | ItemType &item = m_slots[i]; |
| 88 | if (!item.isValid()) { |
| 89 | return item; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | ++m_lastUsedIndex; |
| 94 | if (m_lastUsedIndex >= m_slots.size()) { |
| 95 | m_slots.resize(m_lastUsedIndex + 1); |
| 96 | } |
| 97 | |
| 98 | return m_slots[m_lastUsedIndex]; |
| 99 | } |
| 100 | |
| 101 | void freeSlot(Iterator &iterator) { |
| 102 | m_slots[iterator.index].reset(); |
| 103 | if (iterator.index == m_lastUsedIndex) { |
| 104 | do { |
| 105 | --m_lastUsedIndex; |
| 106 | } while (m_lastUsedIndex >= 0 && !m_slots.at(m_lastUsedIndex).isValid()); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Iterates through all valid items (i.e. the occupied slots) |
| 111 | // calling the given function, with the option of ending the loop early. |
| 112 | // |
| 113 | // bool Func(Iterator& item) |
| 114 | // |
| 115 | // Returning true means it wants to continue the "for" loop, false |
| 116 | // terminates the loop. |
| 117 | template<typename Func> void forEach(Func func) { |
| 118 | Iterator it; |
| 119 | for (it.index = 0; it.index <= m_lastUsedIndex; ++it.index) { |
| 120 | it.item = &m_slots[it.index]; |
| 121 | if (!it.item->isValid()) |
| 122 | continue; |
| 123 | |
| 124 | if (!func(it)) |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | bool isEmpty() const { return m_lastUsedIndex == -1; } |
| 130 | |
| 131 | |
| 132 | private: |
| 133 | QVector<ItemType> m_slots; |
| 134 | int m_lastUsedIndex; |
| 135 | }; |
| 136 | |
| 137 | #endif // POOL_P_H |