-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTinywlInputServiceQueue.cpp
More file actions
61 lines (49 loc) · 1.59 KB
/
TinywlInputServiceQueue.cpp
File metadata and controls
61 lines (49 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <mutex>
#include <list>
#include <optional>
template<typename T>
class TinywlInputServiceQueue {
public:
struct Event {
T event;
long nativePtr;
Event(const T& event, long nativePtr)
: event(event), nativePtr(nativePtr) {}
};
TinywlInputServiceQueue() = default;
~TinywlInputServiceQueue() = default;
// Non-copyable, non-movable
TinywlInputServiceQueue(const TinywlInputServiceQueue&) = delete;
TinywlInputServiceQueue& operator=(const TinywlInputServiceQueue&) = delete;
TinywlInputServiceQueue(TinywlInputServiceQueue&&) = delete;
TinywlInputServiceQueue& operator=(TinywlInputServiceQueue&&) = delete;
// Pull an element from the queue
// Returns std::nullopt if queue is empty
std::optional<Event> pull() {
std::lock_guard<std::mutex> lock(mutex_);
if (queue_.empty()) {
return std::nullopt;
}
Event item = std::move(queue_.back());
queue_.pop_back();
return item;
}
// Emplace element in-place
void push(const T& event, long nativePtr) {
std::lock_guard<std::mutex> lock(mutex_);
queue_.emplace_front(event, nativePtr);
}
// Get current queue length
size_t length() const {
std::lock_guard<std::mutex> lock(mutex_);
return queue_.size();
}
// Check if queue is empty
bool empty() const {
std::lock_guard<std::mutex> lock(mutex_);
return queue_.empty();
}
private:
mutable std::mutex mutex_;
std::list<Event> queue_;
};