载入中...
搜索中...
未找到
Event.h
浏览该文件的文档.
1#pragma once
2
3#include <cstdint>
4#include <mutex>
5#include <queue>
6#include <string>
7#include <vector>
8
9#include "common/Module.h"
10
11namespace ssq {
12class Table;
13class Class;
14} // namespace ssq
15
16namespace eve {
17namespace event {
18
19struct Variant {
20 enum class Type { Nil, Int, String, Ptr };
22 int64_t i = 0;
23 std::string s;
24 void* p = nullptr;
25
27 static Variant makeNil() { return {}; }
29 static Variant makeInt(int64_t v) {
30 Variant x;
32 x.i = v;
33 return x;
34 }
36 static Variant makeString(std::string v) {
37 Variant x;
39 x.s = std::move(v);
40 return x;
41 }
43 static Variant makePtr(void* v) {
44 Variant x;
46 x.p = v;
47 return x;
48 }
49};
50
56class Message {
57public:
63 Message(const std::string& name, const std::vector<Variant>& vargs = {});
64 ~Message();
65
66 const std::string name;
67 const std::vector<Variant> args;
68};
69
70class Event : public Module {
71public:
73 virtual ~Event();
74
79 void push(Message* msg);
85 void pushData(std::string name, std::string data = "");
87 Message* poll();
89 std::string pollName();
91 std::string getLastData() const;
96 std::string pollData();
98 virtual void clear();
99
101 virtual void pump() = 0;
103 virtual Message* wait() = 0;
104
105protected:
107 virtual void wakeWaiters() {}
108
109 std::mutex queueMu_;
110 std::queue<Message*> queue;
111 std::string lastData_;
112};
113
114} // namespace event
115} // namespace eve
int x
Definition Grass.cpp:135
const char * name
Definition RockMesh.cpp:21
int v
void push(Message *msg)
Queues a message. Thread-safe: workers may push completions for the main loop to poll.
Definition Event.cpp:31
virtual void wakeWaiters()
Wake a platform-specific blocking wait after a message is queued.
Definition Event.h:107
virtual void pump()=0
Platform hook: pumps the native event queue into this module.
std::mutex queueMu_
Definition Event.h:109
std::string pollName()
Script helper: pops one message and returns its name, or "" if none.
Definition Event.cpp:58
void pushData(std::string name, std::string data="")
Script helper: pushes an event with an optional string payload.
Definition Event.cpp:42
std::string pollData()
Pops one message and returns its first string arg (or ""). Name is discarded — use pollName when you ...
Definition Event.cpp:76
virtual void clear()
Drops all queued messages and frees them.
Definition Event.cpp:93
std::string getLastData() const
First string arg of the message most recently returned by pollName/pollData.
Definition Event.cpp:74
virtual ~Event()
Definition Event.cpp:29
std::string lastData_
Definition Event.h:111
virtual Message * wait()=0
Platform hook: blocks until a message is available, then polls it.
std::queue< Message * > queue
Definition Event.h:110
Message * poll()
Pops the oldest message, or nullptr if the queue is empty (caller must delete).
Definition Event.cpp:49
A named event carrying an ordered list of Variant payloads. Pushed messages are heap-allocated; the q...
Definition Event.h:56
const std::vector< Variant > args
Definition Event.h:67
const std::string name
Definition Event.h:66
Definition Build.cpp:11
EVEngine ECS 集成层:底层实现使用 sunxfancy/ECS.hpp https://github.com/sunxfancy/ECS.hpp
Definition ECS.h:12
std::string s
Definition Event.h:23
static Variant makePtr(void *v)
Constructs a pointer variant (borrowed, not owned).
Definition Event.h:43
static Variant makeInt(int64_t v)
Constructs an integer variant.
Definition Event.h:29
static Variant makeString(std::string v)
Constructs a string variant (takes ownership of the value).
Definition Event.h:36
static Variant makeNil()
Constructs a nil variant.
Definition Event.h:27