1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef _UI_INPUT_INPUTDISPATCHER_QUEUE_H 18 #define _UI_INPUT_INPUTDISPATCHER_QUEUE_H 19 20 namespace android::inputdispatcher { 21 22 // Generic queue implementation. 23 template <typename T> 24 struct Queue { 25 T* head; 26 T* tail; 27 uint32_t entryCount; 28 QueueQueue29 inline Queue() : head(nullptr), tail(nullptr), entryCount(0) {} 30 isEmptyQueue31 inline bool isEmpty() const { return !head; } 32 enqueueAtTailQueue33 inline void enqueueAtTail(T* entry) { 34 entryCount++; 35 entry->prev = tail; 36 if (tail) { 37 tail->next = entry; 38 } else { 39 head = entry; 40 } 41 entry->next = nullptr; 42 tail = entry; 43 } 44 enqueueAtHeadQueue45 inline void enqueueAtHead(T* entry) { 46 entryCount++; 47 entry->next = head; 48 if (head) { 49 head->prev = entry; 50 } else { 51 tail = entry; 52 } 53 entry->prev = nullptr; 54 head = entry; 55 } 56 dequeueQueue57 inline void dequeue(T* entry) { 58 entryCount--; 59 if (entry->prev) { 60 entry->prev->next = entry->next; 61 } else { 62 head = entry->next; 63 } 64 if (entry->next) { 65 entry->next->prev = entry->prev; 66 } else { 67 tail = entry->prev; 68 } 69 } 70 dequeueAtHeadQueue71 inline T* dequeueAtHead() { 72 entryCount--; 73 T* entry = head; 74 head = entry->next; 75 if (head) { 76 head->prev = nullptr; 77 } else { 78 tail = nullptr; 79 } 80 return entry; 81 } 82 countQueue83 uint32_t count() const { return entryCount; } 84 }; 85 86 } // namespace android::inputdispatcher 87 88 #endif // _UI_INPUT_INPUTDISPATCHER_QUEUE_H 89