44 lines
1.0 KiB
C++
44 lines
1.0 KiB
C++
#pragma once
|
|
|
|
#include <extra2d/core/ring_buffer.h>
|
|
#include <extra2d/core/types.h>
|
|
#include <extra2d/event/event.h>
|
|
#include <mutex>
|
|
|
|
namespace extra2d {
|
|
|
|
// ============================================================================
|
|
// 事件队列 - 线程安全的事件队列
|
|
// ============================================================================
|
|
class EventQueue {
|
|
public:
|
|
static constexpr size_t DEFAULT_CAPACITY = 1024;
|
|
|
|
EventQueue();
|
|
~EventQueue() = default;
|
|
|
|
// 添加事件到队列
|
|
bool push(const Event &event);
|
|
bool push(Event &&event);
|
|
|
|
// 从队列取出事件
|
|
bool poll(Event &event);
|
|
|
|
// 查看队列头部事件(不移除)
|
|
bool peek(Event &event) const;
|
|
|
|
// 清空队列
|
|
void clear();
|
|
|
|
// 队列状态
|
|
bool empty() const;
|
|
size_t size() const;
|
|
size_t capacity() const { return buffer_.capacity(); }
|
|
|
|
private:
|
|
RingBuffer<Event, DEFAULT_CAPACITY> buffer_;
|
|
mutable std::mutex mutex_; // 用于peek和clear的互斥
|
|
};
|
|
|
|
} // namespace extra2d
|