qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
Event.h
Go to the documentation of this file.
1
28
29#ifndef QB_EVENT_H
30#define QB_EVENT_H
31#include <bitset>
33#include <utility>
34// include from qb
35#include "ActorId.h"
36#include "ICallback.h"
37
38namespace qb {
39
52template <typename T>
53struct type {
54 constexpr static void
55 id() {}
56};
57
70template <typename T>
71constexpr TypeId
73 return static_cast<TypeId>(reinterpret_cast<std::size_t>(&type<T>::id));
74}
75
85class QB_LOCKFREE_CACHELINE_ALIGNMENT Event {
86 friend class SharedCoreCommunication;
87 friend class VirtualCore;
88 friend class Actor;
89 friend class Pipe;
90 friend struct EventQOS0;
91 friend struct ServiceEvent;
92
93public:
94 using id_handler_type = ActorId;
95
96#ifdef NDEBUG
97 using id_type = EventId;
103 template <typename T>
104 [[nodiscard]] constexpr static id_type
105 type_to_id() {
106 return static_cast<id_type>(reinterpret_cast<std::size_t>(&qb::type<T>::id));
107 }
108#else
109 using id_type = const char *;
115 template <typename T>
116 [[nodiscard]] constexpr static id_type
118 return typeid(T).name();
119 }
120#endif
121
122private:
123 union Header {
124 uint32_t : 16, : 8, alive : 1, qos : 2, factor : 5;
125 uint8_t prot[4] = {'q', 'b', '\0',
126 4 | ((QB_LOCKFREE_EVENT_BUCKET_BYTES / 16) << 3)};
127 } state;
128 uint16_t bucket_size;
129 id_type id;
130 // for users
131 id_handler_type dest;
132 id_handler_type source;
133
134 // Event &operator=(Event const &) = default;
135
136public:
137 Event() = default;
138
143 [[nodiscard]] inline bool
144 is_alive() const noexcept {
145 return state.alive;
146 }
147
151 [[nodiscard]] inline id_type
152 getID() const noexcept {
153 return id;
154 }
155
159 [[nodiscard]] inline uint8_t
160 getQOS() const noexcept {
161 return state.qos;
162 }
163
167 [[nodiscard]] inline id_handler_type
168 getDestination() const noexcept {
169 return dest;
170 }
171
175 [[nodiscard]] inline id_handler_type
176 getSource() const noexcept {
177 return source;
178 }
179
183 [[nodiscard]] inline std::size_t
184 getSize() const noexcept {
185 return static_cast<std::size_t>(bucket_size) * QB_LOCKFREE_EVENT_BUCKET_BYTES;
186 }
187};
188
198
208
214struct EventQOS0 : public Event {
215 EventQOS0() {
216 state.qos = 0;
217 }
218};
219
229struct ServiceEvent : public Event {
230 id_handler_type forward;
231 id_type service_event_id;
232
236 inline void
237 received() noexcept {
238 std::swap(dest, forward);
239 std::swap(id, service_event_id);
240 live(true);
241 }
242
247 inline void
248 live(bool flag) noexcept {
249 state.alive = flag;
250 }
251};
252
258struct KillEvent : public Event {};
259
265struct UnregisterCallbackEvent : public Event {};
266
272struct SignalEvent : public Event {
273 int signum;
274};
275
286enum class ActorStatus : uint32_t {
291};
292
298struct PingEvent : public Event {
299 const uint32_t type;
300
301 explicit PingEvent(uint32_t const actor_type) noexcept
302 : type(actor_type) {}
303};
304
310struct RequireEvent : public Event {
311 const uint32_t type;
312 const ActorStatus status;
313
314 explicit RequireEvent(uint32_t const actor_type,
315 ActorStatus const actor_status) noexcept
316 : type(actor_type)
317 , status(actor_status) {}
318};
319
326template <typename... _Args>
327struct WithData : public Event {
328 std::tuple<_Args...> data;
329
330 explicit WithData(_Args &&...args)
331 : data(std::forward<_Args>(args)...) {}
332};
333
340template <typename... _Args>
341class WithoutData : public Event {};
342
349template <typename... _Args>
350struct AskData : public WithoutData<_Args...> {};
351
358template <typename... _Args>
359struct FillEvent : public WithData<_Args...> {
360 using base_t = WithData<_Args...>;
361 FillEvent() = default;
362 explicit FillEvent(_Args &&...args)
363 : base_t(std::forward<_Args>(args)...) {}
364};
365
376
385using event = Event;
386
396
397} // namespace qb
398
399#endif // QB_EVENT_H
Unique identifier for actors.
Definition ActorId.h:374
Base class for all events in the actor system.
Definition Event.h:85
std::size_t getSize() const noexcept
Get the size of the event in bytes.
Definition Event.h:184
id_handler_type getSource() const noexcept
Get the source actor ID.
Definition Event.h:176
static constexpr id_type type_to_id()
Get the type identifier at runtime.
Definition Event.h:117
id_handler_type getDestination() const noexcept
Get the destination actor ID.
Definition Event.h:168
id_type getID() const noexcept
Get the event's type ID for event routing and handling.
Definition Event.h:152
uint8_t getQOS() const noexcept
Get the event's quality of service level.
Definition Event.h:160
bool is_alive() const noexcept
Check if the event is still alive and can be processed.
Definition Event.h:144
Event template without data payload.
Definition Event.h:341
Extensible buffer optimized for performance.
Definition pipe.h:552
Actor and core identification for the QB Actor Framework.
Callback interface for the QB Actor Framework.
uint16_t TypeId
Type definition for type identifiers.
Definition ActorId.h:67
TypeId EventId
Type definition for event identifiers.
Definition ActorId.h:75
Event event
Alias for the base Event class.
Definition Event.h:385
constexpr TypeId type_id()
Function to get a unique type identifier for a given type.
Definition Event.h:72
Event EventQOS2
Event with highest quality of service (priority level 2)
Definition Event.h:197
allocator::pipe< EventBucket > VirtualPipe
Pipe for event transmission in the actor system.
Definition Event.h:375
ActorStatus
Represents the current status of an actor in the system.
Definition Event.h:286
Event EventQOS1
Event with medium quality of service (priority level 1)
Definition Event.h:207
ServiceEvent service_event
Alias for the ServiceEvent class.
Definition Event.h:395
@ Dead
The actor has been terminated or removed from the system.
Definition Event.h:290
@ Alive
The actor is active and operational.
Definition Event.h:288
Event template for requesting data, typically without carrying data itself.
Definition Event.h:350
Event used to terminate an actor.
Definition Event.h:258
Event type for service-to-service communication.
Definition Event.h:229
void received() noexcept
Mark the event as received and swap source/destination.
Definition Event.h:237
void live(bool flag) noexcept
Set the event's alive status.
Definition Event.h:248
Event used to handle system signals.
Definition Event.h:272
Event used to unregister an actor's callback.
Definition Event.h:265
Template struct used for type identification in the event system.
Definition Event.h:53
Implementation of a dynamic extensible buffer.