forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_processor.h
More file actions
43 lines (36 loc) · 1 KB
/
event_processor.h
File metadata and controls
43 lines (36 loc) · 1 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
#pragma once
#include <eventpp/eventqueue.h>
#include <thread>
#include "common/event.h"
namespace cortex::event {
using EventType = cortex::event::EventType;
using ExitEvent = cortex::event::ExitEvent;
using EventQueue =
eventpp::EventQueue<EventType, void(const eventpp::AnyData<eventMaxSize>&)>;
struct ExitingEvent {};
class EventProcessor {
public:
EventProcessor(std::shared_ptr<EventQueue> queue)
: event_queue_(std::move(queue)), running_(true) {
thread_ = std::thread([this]() {
while (running_) {
event_queue_->wait();
event_queue_->process();
}
});
}
~EventProcessor() {
running_ = false;
// to prevent blocking thread on wait
event_queue_->enqueue(EventType::ExitEvent,
ExitEvent{{}, "Event queue exitting.."});
if (thread_.joinable()) {
thread_.join();
}
}
private:
std::shared_ptr<EventQueue> event_queue_;
std::thread thread_;
std::atomic<bool> running_;
};
} // namespace cortex::event