| 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_Hcxx . . $ `src/LomiriGestures/pool_p.h:18:9`! |
| 19 | |
| 20 | #include <QtCore/QVector> |
| 21 | |
| 22 | #if QT_VERSIONcxx . . $ `<external>/15045578679651060896/qglobal.h:57:9`! >= QT_VERSION_CHECKcxx . . $ `<external>/15045578679651060896/qglobal.h:61:9`!(6, 0, 0) |
| 23 | #include <LomiriGestures-Qt6/lomirigesturesglobal.h> |
| 24 | #else |
| 25 | #include <LomiriGestures/lomirigesturesglobal.h>cxx . . $ `<file>/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 Poolcxx . . $ Pool# |
| 52 | { |
| 53 | public: |
| 54 | Poolcxx . . $ Pool#`Pool<ItemType>`(49f6e7a06ebc5aa8).() : m_lastUsedIndexcxx . . $ Pool#m_lastUsedIndex.(-1) { |
| 55 | } |
| 56 | |
| 57 | class Iteratorcxx . . $ Pool#Iterator# { |
| 58 | public: |
| 59 | Iteratorcxx . . $ Pool#Iterator#Iterator(49f6e7a06ebc5aa8).() : indexcxx . . $ Pool#Iterator#index.(-1), itemcxx . . $ Pool#Iterator#item.(nullptr) {} |
| 60 | Iteratorcxx . . $ Pool#Iterator#Iterator(a84ece1c90a7a294).(int index, ItemType *item) |
| 61 | : indexcxx . . $ Pool#Iterator#index.(index), itemcxx . . $ Pool#Iterator#item.(item) {} |
| 62 | Iteratorcxx . . $ Pool#Iterator#Iterator(47f082a56edc6126).(const Iteratorcxx . . $ Pool#Iterator#&) = default; |
| 63 | |
| 64 | ItemType *operatorcxx . . $ Pool#Iterator#`operator->`(5a2a78a048fb49a8).->() const { return itemcxx . . $ Pool#Iterator#item.; } |
| 65 | ItemType &operatorcxx . . $ Pool#Iterator#`operator*`(c93303b41ac66b6d).*() const { return *itemcxx . . $ Pool#Iterator#item.; } |
| 66 | ItemType &valuecxx . . $ Pool#Iterator#value(c93303b41ac66b6d).() const { return *itemcxx . . $ Pool#Iterator#item.; } |
| 67 | |
| 68 | Iteratorcxx . . $ Pool#Iterator# &operatorcxx . . $ Pool#Iterator#`operator=`(43678973e1b4f722).= (const Iteratorcxx . . $ Pool#Iterator#& other) { |
| 69 | indexcxx . . $ Pool#Iterator#index. = other.indexcxx . . $ Pool#Iterator#index.; |
| 70 | itemcxx . . $ Pool#Iterator#item. = other.itemcxx . . $ Pool#Iterator#item.; |
| 71 | |
| 72 | // by convention, always return *this |
| 73 | return *this; |
| 74 | } |
| 75 | |
| 76 | operatorcxx . . $ Pool#Iterator#`operator bool`(50ce9a9e25b4a850). bool() const { return itemcxx . . $ Pool#Iterator#item. !=cxx . . $ `operator!=`(abb235a625a921b3). nullptr; } |
| 77 | |
| 78 | int indexcxx . . $ Pool#Iterator#index.; |
| 79 | ItemType *itemcxx . . $ Pool#Iterator#item.; |
| 80 | }; |
| 81 | |
| 82 | ItemType &getEmptySlotcxx . . $ Pool#getEmptySlot(19b2b6f17af048b).() { |
| 83 | Q_ASSERTcxx . . $ `<external>/15045578679651060896/qglobal.h:859:13`!(m_lastUsedIndex < m_slots.size()); |
| 84 | |
| 85 | // Look for an in-between vacancy first |
| 86 | for (int i = 0; i < m_lastUsedIndexcxx . . $ Pool#m_lastUsedIndex.; ++i) { |
| 87 | ItemType &item = m_slotscxx . . $ Pool#m_slots.[i]; |
| 88 | if (!item.isValid()) { |
| 89 | return item; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | ++m_lastUsedIndexcxx . . $ Pool#m_lastUsedIndex.; |
| 94 | if (m_lastUsedIndexcxx . . $ Pool#m_lastUsedIndex. >=cxx . . $ `operator>=`(6fc8c8b9b6cf52c7). m_slotscxx . . $ Pool#m_slots..sizecxx . . $ QVector#size(455f465bc33b4cdf).()) { |
| 95 | m_slotscxx . . $ Pool#m_slots..resizecxx . . $ QVector#resize(d4f767463ce0a6b3).(m_lastUsedIndexcxx . . $ Pool#m_lastUsedIndex. + 1); |
| 96 | } |
| 97 | |
| 98 | return m_slotscxx . . $ Pool#m_slots.[m_lastUsedIndexcxx . . $ Pool#m_lastUsedIndex.]; |
| 99 | } |
| 100 | |
| 101 | void freeSlotcxx . . $ Pool#freeSlot(e4cf51fdf617a087).(Iteratorcxx . . $ Pool#Iterator# &iterator) { |
| 102 | m_slotscxx . . $ Pool#m_slots.[iterator.indexcxx . . $ Pool#Iterator#index.].reset(); |
| 103 | if (iterator.indexcxx . . $ Pool#Iterator#index. ==cxx . . $ `operator==`(e15ab626bd48f546). m_lastUsedIndexcxx . . $ Pool#m_lastUsedIndex.) { |
| 104 | do { |
| 105 | --m_lastUsedIndexcxx . . $ Pool#m_lastUsedIndex.; |
| 106 | } while (m_lastUsedIndexcxx . . $ Pool#m_lastUsedIndex. >= 0 && !m_slotscxx . . $ Pool#m_slots..atcxx . . $ QVector#at(e2681f9bb6b2f357).(m_lastUsedIndexcxx . . $ Pool#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 forEachcxx . . $ Pool#forEach(b07662a27bd562f9).(Func func) { |
| 118 | Iteratorcxx . . $ Pool#Iterator# it; |
| 119 | for (it.indexcxx . . $ Pool#Iterator#index. = 0; it.indexcxx . . $ Pool#Iterator#index. <=cxx . . $ `operator<=`(6fc8c8b9b6cf52c7). m_lastUsedIndexcxx . . $ Pool#m_lastUsedIndex.; ++it.indexcxx . . $ Pool#Iterator#index.) { |
| 120 | it.itemcxx . . $ Pool#Iterator#item. = &cxx . . $ `operator&`(fd3221fc99529ca8).m_slotscxx . . $ Pool#m_slots.[it.indexcxx . . $ Pool#Iterator#index.]; |
| 121 | if (!it.itemcxx . . $ Pool#Iterator#item.->isValid()) |
| 122 | continue; |
| 123 | |
| 124 | if (!func(it)) |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | bool isEmptycxx . . $ Pool#isEmpty(50ce9a9e25b4a850).() const { return m_lastUsedIndexcxx . . $ Pool#m_lastUsedIndex. == -1; } |
| 130 | |
| 131 | |
| 132 | private: |
| 133 | QVectorcxx . . $ QVector#<ItemType> m_slotscxx . . $ Pool#m_slots.; |
| 134 | int m_lastUsedIndexcxx . . $ Pool#m_lastUsedIndex.; |
| 135 | }; |
| 136 | |
| 137 | #endif // POOL_P_H |