载入中...
搜索中...
未找到
Event.cpp
浏览该文件的文档.
1#include "event/Event.h"
2#include "event/sdl/Event.h"
3
4#include "common/Assert.h"
5
6#include <simplesquirrel/simplesquirrel.hpp>
7
8namespace eve::event {
9
10Message::Message(const std::string& name, const std::vector<Variant>& vargs) : name(name), args(vargs) {}
12
14
15void Event::expose(ssq::Table& table) {
16 auto cls = table.addClass(name, Event::create, false);
17 expose(cls);
18}
19
20void Event::expose(ssq::Class& cls) {
21 cls.addFunc("getName", &Event::getName);
22 cls.addFunc("pump", &Event::pump);
23 cls.addFunc("poll", &Event::pollName);
24 cls.addFunc("pollData", &Event::pollData);
25 cls.addFunc("getLastData", &Event::getLastData);
26 cls.addFunc("pushData", &Event::pushData);
27}
28
30
31void Event::push(Message* msg) {
32 EV_PARAM_CHECK(msg != nullptr, "event message must not be null");
33 if (!msg)
34 return;
35 {
36 std::lock_guard<std::mutex> lock(queueMu_);
37 queue.push(msg);
38 }
40}
41
42void Event::pushData(std::string name, std::string data) {
43 std::vector<Variant> args;
44 if (!data.empty())
45 args.push_back(Variant::makeString(std::move(data)));
46 push(new Message(std::move(name), args));
47}
48
50 std::lock_guard<std::mutex> lock(queueMu_);
51 if (queue.empty())
52 return nullptr;
53 auto msg = queue.front();
54 queue.pop();
55 return msg;
56}
57
58std::string Event::pollName() {
59 Message* msg = poll();
60 lastData_.clear();
61 if (!msg)
62 return {};
63 for (const auto& a : msg->args) {
64 if (a.type == Variant::Type::String) {
65 lastData_ = a.s;
66 break;
67 }
68 }
69 std::string n = msg->name;
70 delete msg;
71 return n;
72}
73
74std::string Event::getLastData() const { return lastData_; }
75
76std::string Event::pollData() {
77 Message* msg = poll();
78 lastData_.clear();
79 if (!msg)
80 return {};
81 std::string data;
82 for (const auto& a : msg->args) {
83 if (a.type == Variant::Type::String) {
84 data = a.s;
85 break;
86 }
87 }
89 delete msg;
90 return data;
91}
92
94 std::lock_guard<std::mutex> lock(queueMu_);
95 while (!queue.empty()) {
96 delete queue.front();
97 queue.pop();
98 }
99}
100
101} // namespace eve::event
EVEngine assertion entry point, backed by zeroerr.
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Definition Assert.h:30
HSQOBJECT cls
Definition ECS.cpp:21
glm::vec3 n
Definition Grass.cpp:64
uint32_t a
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:24
Light2D::Data * data
const char * name
Definition RockMesh.cpp:21
virtual std::string getName() const =0
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
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
Message(const std::string &name, const std::vector< Variant > &vargs={})
Creates an event message.
Definition Event.cpp:10
const std::string name
Definition Event.h:66
static Variant makeString(std::string v)
Constructs a string variant (takes ownership of the value).
Definition Event.h:36