载入中...
搜索中...
未找到
PlatformEvent.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include <cstdint>
6#include <functional>
7#include <memory>
8#include <mutex>
9#include <queue>
10#include <string>
11#include <utility>
12#include <vector>
13
14#include "common/Module.h"
15#include "common/Subscription.h"
16
17namespace ssq {
18class Table;
19class Class;
20} // namespace ssq
21
22namespace eve {
23namespace platform_event {
24
26struct Variant {
28 enum class Type { Nil, Int, String, Ptr };
30 int64_t i = 0;
31 std::string s;
32 void* p = nullptr;
33 std::shared_ptr<void> owner;
34
36 static Variant makeNil() { return {}; }
38 static Variant makeInt(int64_t v) {
39 Variant x;
41 x.i = v;
42 return x;
43 }
45 static Variant makeString(std::string v) {
46 Variant x;
48 x.s = std::move(v);
49 return x;
50 }
52 static Variant makePtr(void* v) {
53 Variant x;
55 x.p = v;
56 return x;
57 }
59 template <class T>
61 static Variant makeOwnedPtr(T* v) {
62 Variant x;
64 x.p = v;
65 if (v) x.owner = std::shared_ptr<void>(v, [](void* p) { delete static_cast<T*>(p); });
66 return x;
67 }
69 bool ownsPointer() const { return static_cast<bool>(owner); }
70};
71
78public:
84 Message(const std::string& name, const std::vector<Variant>& vargs = {});
86 ~Message();
87
88 const std::string name;
89 const std::vector<Variant> args;
90};
91
94public:
97 virtual ~PlatformEvent();
98
103 void push(Message* msg);
105 void push(std::unique_ptr<Message> msg);
111 void pushData(std::string name, std::string data = "");
119 Message* poll();
121 std::unique_ptr<Message> pollOwned();
123 std::string pollName();
125 std::string getLastData() const;
130 std::string pollData();
132 virtual void clear();
133
142 void setPollObserver(std::function<void(const Message&)> observer) {
143 legacyPollSubscription_.dispose();
144 pollObserver_ = std::move(observer);
145 if (pollObserver_) legacyPollSubscription_ = pollObservers_.subscribe(pollObserver_);
146 }
147
149 const std::function<void(const Message&)>& pollObserver() const { return pollObserver_; }
150
158 [[nodiscard("retain Subscription or explicitly dispose it")]] eve::Subscription subscribePoll(
159 std::function<void(const Message&)> callback);
160
162 [[nodiscard]] std::uint64_t pollObserverFailureCount() const noexcept { return pollObserverFailures_; }
163
165 virtual void pump() = 0;
173 virtual Message* wait() = 0;
175 std::unique_ptr<Message> waitOwned() { return std::unique_ptr<Message>(wait()); }
176
177protected:
179 virtual void wakeWaiters() {}
180
181 std::mutex queueMu_;
182 std::queue<std::unique_ptr<Message>> queue;
183 std::string lastData_;
185 std::function<void(const Message&)> pollObserver_;
188 std::uint64_t pollObserverFailures_ = 0;
189};
190
191} // namespace platform_event
192} // namespace eve
float x
Definition AnimClip.cpp:738
int observer
Definition AnimSmr.cpp:162
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
float v
std::string name
ViewPreparation callback
EVENGINE_API_FOUNDATION public API.
Definition Module.h:46
Thread-affine callback registry with mutation-safe dispatch.
Move-only RAII token that owns one observer registration.
A named event carrying an ordered list of Variant payloads. Pushed messages are heap-allocated; the q...
const std::vector< Variant > args
EVENGINE_API_FOUNDATION public API.
std::function< void(const Message &)> pollObserver_
Void.
virtual void pump()=0
Platform hook: pumps the native event queue into this module.
std::queue< std::unique_ptr< Message > > queue
eve::Observer< Message > pollObservers_
const std::function< void(const Message &)> & pollObserver() const
Current poll observer, or empty if none.
std::unique_ptr< Message > waitOwned()
Blocking wait returning an RAII handle.
virtual Message * wait()=0
Platform hook: blocks until a message is available, then polls it. @ownership Ownership transfers to ...
eve::Subscription legacyPollSubscription_
void setPollObserver(std::function< void(const Message &)> observer)
Sets an observer invoked for every message consumed via poll*().
virtual void wakeWaiters()
Wake a platform-specific blocking wait after a message is queued.
std::uint64_t pollObserverFailureCount() const noexcept
Number of contained poll-listener exceptions since construction.
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
enum EVENGINE_API_FOUNDATION Table
OT_TABLE (editable via Runtime table APIs / ReflectedPropertyModel).
Definition Runtime.h:121
EVEngine ECS 集成层:底层实现使用 sunxfancy/ECS.hpp https://github.com/sunxfancy/ECS.hpp
Definition ECS.h:14
static Variant makePtr(void *v)
Constructs a pointer variant (borrowed, not owned).
bool ownsPointer() const
Whether this pointer payload owns the pointed-to object.
static Variant makeNil()
Constructs a nil variant.
std::shared_ptr< void > owner
static Variant makeOwnedPtr(T *v)
Constructs an owning pointer variant deleted with the message.
static Variant makeInt(int64_t v)
Constructs an integer variant.
static Variant makeString(std::string v)
Constructs a string variant (takes ownership of the value).