107 friend class VirtualCore;
108 friend class ActorProxy;
109 friend class Service;
111 const char *name =
"unnamed";
113 mutable bool _alive =
true;
114 std::uint32_t id_type = 0u;
124 template <
typename _Type>
125 bool require_type()
const noexcept;
143 template <
typename Tag>
144 static ServiceId registerIndex()
noexcept;
165 virtual ~Actor() noexcept = default;
327 explicit EventBuilder(
Pipe const &
pipe)
noexcept;
330 EventBuilder() =
delete;
331 EventBuilder(EventBuilder
const &rhs)
noexcept =
default;
354 template <
typename _Event,
typename... _Args>
355 EventBuilder &
push(_Args &&...args)
noexcept;
368 id() const noexcept {
383 [[nodiscard]] std::string_view
getName() const noexcept;
408 [[nodiscard]] uint64_t
time() const noexcept;
413 template <typename T>
414 [[nodiscard]] static
ActorId getServiceId(
CoreId index) noexcept;
420 template <typename _ServiceActor>
478 template <typename _Actor>
496 template <typename _Actor>
528 template <typename _Event, typename _Actor>
545 template <typename _Event, typename _Actor>
554 template <typename _Event>
604 template <typename _Event, typename... _Args>
605 _Event &
push(
ActorId const &dest, _Args &&...args) const noexcept;
627 template <typename _Event, typename... _Args>
651 template <typename _Event, typename... _Args>
653 _Args &&...args) const noexcept;
661 template <typename _Type>
662 [[nodiscard]] inline
bool
663 is(uint32_t const
id) const noexcept {
674 template <
typename _Type>
675 [[nodiscard]]
inline bool
706 template <
typename... _Actors>
724 template <typename _Event, typename... _Args>
777 bool try_send(
Event const &
event) const noexcept;
831 template <typename _Actor, typename... _Args>
845class Service : public Actor {
860template <
typename Tag>
861class ServiceActor :
public Service {
863 friend class CoreInitializer;
864 friend class VirtualCore;
869 : Service(ServiceIndex) {}
902 ActorProxy() =
default;
903 template <
typename _Type>
906 actor.id_type = ActorProxy::getType<_Type>();
908 template <
typename _Type>
911 actor.name = ActorProxy::getName<_Type>();
915 template <
typename _Type>
920 template <
typename _Type>
924 static std::unique_ptr<char, void (*)(
void *)> res{
925 abi::__cxa_demangle(
typeid(_Type).name(),
nullptr,
nullptr,
nullptr),
930 return typeid(_Type).name();
937template <
template <
typename...>
class Template,
typename T>
940template <
template <
typename...>
class Template,
typename... Args>
957 using no_ref = std::remove_reference_t<T>;
964 using type = std::conditional_t<
969 std::is_array_v<T> && std::is_same_v<std::remove_extent_t<T>,
const char>,
994 if constexpr (std::is_same_v<Target, std::string>) {
995 return std::string(std::forward<T>(val));
997 return std::forward<T>(val);
1010template <
typename _Actor,
typename... _Args>
1012 using Tuple = std::tuple<typename actor_factory_param<_Args>::type...>;
1018 explicit TActorFactory(
ActorId const id, _Args&&... args)
1023 return create_impl(std::index_sequence_for<_Args...>{});
1027 return std::is_base_of_v<Service, _Actor>;
1031 template <std::size_t... Is>
1032 Actor* create_impl(std::index_sequence<Is...>) {
1033 auto actor =
new _Actor(std::get<Is>(_parameters)...);
1034 ActorProxy::setType<_Actor>(*
actor);
1035 ActorProxy::setName<_Actor>(*
actor);
1055template <
typename Tag>
Helper class for building and sending events to actors.
Definition Actor.h:318
EventBuilder & push(_Args &&...args) noexcept
Send a new event to the target actor.
Base class for all actors in the qb framework.
Definition Actor.h:106
bool is(uint32_t const id) const noexcept
Check if a given ID matches the type ID of _Type.
Definition Actor.h:663
void registerEvent(_Actor &actor) const noexcept
Subscribe this actor to listen for a specific event type.
_ServiceActor * getService() const noexcept
Get direct access to ServiceActor* in same core.
_Event build_event(qb::ActorId const source, _Args &&...args) const noexcept
Construct an event locally, intended for immediate self-processing or direct calls.
void unregisterCallback(_Actor &actor) const noexcept
Unregister a previously registered looped callback for this actor.
void on(KillEvent const &event) noexcept
Handler for KillEvent.
_Actor * addRefActor(_Args &&...args) const
Create and initialize a new referenced actor on the same VirtualCore.
uint64_t time() const noexcept
Get current time from the VirtualCore's perspective (nanoseconds since epoch).
Pipe getPipe(ActorId dest) const noexcept
Get direct access to the underlying communication pipe for a destination actor.
_Event & push(ActorId const &dest, _Args &&...args) const noexcept
Send a new event in an ordered fashion to a destination actor, returning a reference to it.
const CoreIdSet & getCoreSet() const noexcept
Get the set of cores that this actor's VirtualCore can communicate with.
std::string_view getName() const noexcept
Get derived class name.
void registerCallback(_Actor &actor) const noexcept
Register a looped callback for this actor.
void forward(ActorId dest, Event &event) const noexcept
Forward a received event to a new destination, reusing the event object.
Actor() noexcept
Default constructor.
ActorId id() const noexcept
Get ActorId.
Definition Actor.h:368
CoreId getIndex() const noexcept
Get core index.
void reply(Event &event) const noexcept
Reply to the source of a received event, reusing the event object.
EventBuilder to(ActorId dest) const noexcept
Get an EventBuilder for sending chained events to a destination actor.
void broadcast(_Args &&...args) const noexcept
Broadcast an event to all actors on all cores.
void send(ActorId const &dest, _Args &&...args) const noexcept
Send a new event in an unordered fashion to a destination actor.
void kill() const noexcept
Terminate this actor and mark it for removal from the system.
bool is(RequireEvent const &event) const noexcept
Check if a RequireEvent is for a specific actor type.
Definition Actor.h:676
bool is_alive() const noexcept
Check if Actor is alive and processing events.
void unregisterEvent(_Actor &actor) const noexcept
Unsubscribe this actor from listening to a specific event type.
virtual bool onInit()
Initialization callback, called once after construction and ID assignment.
Definition Actor.h:198
bool require() const noexcept
Request discovery of other actors of specified types.
Unique identifier for actors.
Definition ActorId.h:374
Internal helper class for actor type and name management.
Definition Actor.h:900
Base class for all events in the actor system.
Definition Event.h:85
Interface for actor factory classes.
Definition Actor.h:878
virtual bool isService() const =0
Checks if the factory creates a service actor.
virtual Actor * create()=0
Creates an actor instance.
Represents a communication channel between actors.
Definition Pipe.h:46
SingletonActor base class, ensuring one instance per VirtualCore per Tag.
Definition Actor.h:861
Internal base class for services.
Definition Actor.h:845
Actor * create() final
Creates an actor instance.
Definition Actor.h:1022
bool isService() const final
Checks if the factory creates a service actor.
Definition Actor.h:1026
Manages a virtual processing core (worker thread) in the actor system.
Definition VirtualCore.h:75
Event system for the QB Actor Framework.
Callback interface for the QB Actor Framework.
Actor communication channel for the QB Actor Framework.
auto actor_factory_forward(T &&val)
Utility function for forwarding and transforming arguments to actor factory.
Definition Actor.h:991
Actor actor
Alias for the Actor class.
Definition Actor.h:1046
std::ostream & operator<<(std::ostream &os, qb::Actor const &actor)
Stream output operator for Actor objects.
ServiceActor< Tag > service_actor
Alias for the ServiceActor template class.
Definition Actor.h:1056
uint16_t CoreId
Type definition for core identifiers.
Definition ActorId.h:51
uint16_t ServiceId
Type definition for service identifiers.
Definition ActorId.h:59
CoreIdBitSet CoreIdSet
Efficient set implementation for storing CoreId values.
Definition ActorId.h:363
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
Pipe pipe
Alias for the Pipe class.
Definition Pipe.h:117
Defines a base class to make derived classes non-copyable.
Event used to terminate an actor.
Definition Event.h:258
Event used for actor health checks.
Definition Event.h:298
Event used to query actor status.
Definition Event.h:310
Event used to handle system signals.
Definition Event.h:272
Event used to unregister an actor's callback.
Definition Event.h:265
Utility struct for processing actor factory constructor arguments.
Definition Actor.h:955
std::remove_reference_t< T > no_ref
Type with references removed.
Definition Actor.h:957
std::conditional_t< is_ref_wrapper, no_ref, std::conditional_t< std::is_array_v< T > &&std::is_same_v< std::remove_extent_t< T >, const char >, std::string, std::decay_t< T > > > type
The resulting type after transformation.
Definition Actor.h:964
static constexpr bool is_ref_wrapper
Whether the type is a reference wrapper.
Definition Actor.h:960
nocopy()=default
Default constructor.
Advanced type traits and metaprogramming utilities for the QB Framework.
Optimized unordered map implementations.