|
|
@ -7,6 +7,9 @@ |
|
|
|
#include <thread> |
|
|
|
#include <thread> |
|
|
|
#include <QApplication> |
|
|
|
#include <QApplication> |
|
|
|
#include <mutex> |
|
|
|
#include <mutex> |
|
|
|
|
|
|
|
#include <unordered_set> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_QUEUE_SIZE 10 |
|
|
|
|
|
|
|
|
|
|
|
template<typename T> |
|
|
|
template<typename T> |
|
|
|
class Queue { |
|
|
|
class Queue { |
|
|
@ -14,21 +17,31 @@ class Queue { |
|
|
|
std::mutex mutex; |
|
|
|
std::mutex mutex; |
|
|
|
void run() { |
|
|
|
void run() { |
|
|
|
while (!QApplication::closingDown()){ |
|
|
|
while (!QApplication::closingDown()){ |
|
|
|
if (!container.empty()) |
|
|
|
if (!isEmpty()) |
|
|
|
update(); |
|
|
|
update(); |
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
virtual void update() {} |
|
|
|
virtual void update() {} |
|
|
|
std::vector<T *> container; |
|
|
|
|
|
|
|
|
|
|
|
// First in
|
|
|
|
|
|
|
|
size_t tail = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// First out
|
|
|
|
|
|
|
|
size_t head = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
T * container[MAX_QUEUE_SIZE]; |
|
|
|
protected: |
|
|
|
protected: |
|
|
|
|
|
|
|
bool isEmpty() const { |
|
|
|
|
|
|
|
return tail == head; |
|
|
|
|
|
|
|
} |
|
|
|
size_t size() const { |
|
|
|
size_t size() const { |
|
|
|
return container.size(); |
|
|
|
return container.size(); |
|
|
|
} |
|
|
|
} |
|
|
|
T * pop(){ |
|
|
|
T * pop(){ |
|
|
|
mutex.lock(); |
|
|
|
mutex.lock(); |
|
|
|
T * element = container.back(); |
|
|
|
T * element = container[head]; |
|
|
|
container.pop_back(); |
|
|
|
head = (head + 1) % MAX_QUEUE_SIZE; |
|
|
|
mutex.unlock(); |
|
|
|
mutex.unlock(); |
|
|
|
return element; |
|
|
|
return element; |
|
|
|
} |
|
|
|
} |
|
|
@ -38,7 +51,8 @@ public: |
|
|
|
} |
|
|
|
} |
|
|
|
void push(T * element){ |
|
|
|
void push(T * element){ |
|
|
|
mutex.lock(); |
|
|
|
mutex.lock(); |
|
|
|
container.insert(container.begin(), element); |
|
|
|
container[tail] = element; |
|
|
|
|
|
|
|
tail = (tail + 1) % MAX_QUEUE_SIZE; |
|
|
|
mutex.unlock(); |
|
|
|
mutex.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
@ -46,7 +60,7 @@ public: |
|
|
|
|
|
|
|
|
|
|
|
template <typename EventType> |
|
|
|
template <typename EventType> |
|
|
|
class EventQueue : protected Queue<EventType>{ |
|
|
|
class EventQueue : protected Queue<EventType>{ |
|
|
|
std::vector<Listener<EventType> *> listeners; |
|
|
|
std::unordered_set<Listener<EventType> *> listeners; |
|
|
|
void update() override { |
|
|
|
void update() override { |
|
|
|
EventType * event = this->pop(); |
|
|
|
EventType * event = this->pop(); |
|
|
|
|
|
|
|
|
|
|
@ -60,10 +74,10 @@ public: |
|
|
|
this->push(event); |
|
|
|
this->push(event); |
|
|
|
} |
|
|
|
} |
|
|
|
void registerListener(Listener<EventType> * listener){ |
|
|
|
void registerListener(Listener<EventType> * listener){ |
|
|
|
listeners.push_back(listener); |
|
|
|
listeners.insert(listener); |
|
|
|
} |
|
|
|
} |
|
|
|
void unregisterListener(Listener<EventType> * listener){ |
|
|
|
void unregisterListener(Listener<EventType> * listener){ |
|
|
|
std::remove(listeners.begin(), listeners.end(), listener); |
|
|
|
listeners.erase(listener); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|