125using TEvent = std::pair<unsigned, TTime>;
141 using TPtr = std::unique_ptr<TActorContext>;
175 template<
typename T,
typename... Args>
184 template<
typename T,
typename... Args>
207 template<
typename T,
typename... Args>
214 void Cancel(TEvent event);
230 template<
typename Rep,
typename Period>
241 template<
typename T,
typename TQuestion>
253 : ActorSystem_(actorSystem)
260 TLocalActorId ActorId_;
270 static void*
operator new(
size_t size,
TActorSystem* actorSystem);
271 static void operator delete(
void* ptr);
277 , ActorSystem(actorSystem)
313 using TPtr = std::unique_ptr<IActor>;
329 virtual void Receive(TMessageId messageId,
TBlob blob, TActorContext::TPtr ctx) = 0;
348 void Receive(TMessageId messageId,
TBlob blob, TActorContext::TPtr ctx)
override;
380 virtual void Receive(TMessageId messageId,
TBlob blob, TActorContext::TPtr ctx) = 0;
411template<
typename TBaseBehavior,
typename... TMessages>
424 void Receive(TMessageId messageId,
TBlob blob, TActorContext::TPtr ctx)
override {
425 bool handled = (TryHandleMessage<TMessages>(
432 static_cast<TBaseBehavior*
>(
this)->HandleUnknownMessage(messageId, std::move(blob), std::move(ctx));
437 template<
typename TMessage>
438 bool TryHandleMessage(TMessageId messageId,
TBlob& blob, TActorContext::TPtr& ctx) {
439 if (TMessage::MessageId == messageId) {
440 if (blob.Type == TBlob::PointerType::Near) {
441 auto&& mes = DeserializeNear<TMessage>(blob);
442 HandleMessage<TMessage>(
448 auto&& mes = DeserializeFar<TMessage>(blob);
449 HandleMessage<TMessage>(
460 template<
typename TMessage>
461 void HandleMessage(TMessage&& message, TBlob blob, TActorContext::TPtr ctx)
463 using ReturnType =
decltype(
static_cast<TBaseBehavior*
>(
this)->
Receive(
464 std::declval<TMessage>(),
465 std::declval<TBlob>(),
466 std::declval<TActorContext::TPtr>()
469 if constexpr (std::is_same_v<ReturnType, void>) {
470 static_cast<TBaseBehavior*
>(
this)->
Receive(
476 auto async = ctx->StartAsync();
477 auto future =
static_cast<TBaseBehavior*
>(
this)->
Receive(
482 if (!future.done()) {
483 async.Commit(std::move(future));
532 CurrentBehavior_ = behavior;
541 void Receive(TMessageId messageId,
TBlob blob, TActorContext::TPtr ctx)
override {
542 CurrentBehavior_->
Receive(messageId, std::move(blob), std::move(ctx));
Base interface for all actors in the system.
Definition actor.hpp:311
virtual void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx)=0
Process an incoming message.
Actor that delegates message handling to a pluggable behavior.
Definition actor.hpp:522
void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx) override
Delegate message handling to the current behavior.
Definition actor.hpp:541
void Become(IBehavior *behavior)
Switch to a new behavior.
Definition actor.hpp:531
Base interface for actor behaviors.
Definition actor.hpp:370
virtual void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx)=0
Process an incoming message according to this behavior.
Coroutine-based actor interface for asynchronous message processing.
Definition actor.hpp:340
void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx) override
Synchronous receive method (calls CoReceive internally)
Definition actor.cpp:7
virtual TFuture< void > CoReceive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx)=0
Asynchronous message processing method.
Context object providing actor communication and scheduling capabilities.
Definition actor.hpp:139
TActorId Self() const
Get this actor's ID.
Definition actor.hpp:149
void Forward(TActorId to, TMessageId messageId, TBlob blob)
Forward a message to another actor (preserves original sender)
Definition actorsystem.cpp:21
TFuture< void > Sleep(TTime until)
Sleep until a specific time.
Definition actorsystem.hpp:341
TFuture< T > Ask(TActorId recipient, TQuestion &&question)
Send a message and wait for a response.
Definition actorsystem.hpp:351
void Cancel(TEvent event)
Cancel a previously scheduled message.
Definition actorsystem.cpp:31
void Send(TActorId to, TMessageId messageId, TBlob blob)
Send a message to another actor.
Definition actorsystem.cpp:16
TAsync StartAsync()
Start an asynchronous operation context.
Definition actor.cpp:16
TEvent Schedule(TTime when, TActorId sender, TActorId recipient, TMessageId messageId, TBlob blob)
Schedule a message to be delivered at a specific time.
Definition actorsystem.cpp:26
TActorId Sender() const
Get the sender of the current message.
Definition actor.hpp:144
Unique identifier for actors in the system.
Definition actorid.hpp:26
Definition actorsystem.hpp:63
Template for type-safe behavior implementations.
Definition actor.hpp:413
void Receive(TMessageId messageId, TBlob blob, TActorContext::TPtr ctx) override
Process incoming message with automatic type dispatch.
Definition actor.hpp:424
Message envelope containing routing and payload information.
Definition actor.hpp:117
TActorId Recipient
Actor that should receive the message.
Definition actor.hpp:120
TBlob Blob
Serialized message data.
Definition actor.hpp:122
TActorId Sender
Actor that sent the message.
Definition actor.hpp:119
TMessageId MessageId
Type identifier of the message.
Definition actor.hpp:121
Mock actor context for testing purposes.
Definition actor.hpp:294
Implementation of a promise/future system for coroutines.
Helper class for managing asynchronous operations in actors.
Definition actor.hpp:251
Definition messages.hpp:84
Future type for coroutines returning a value of type T.
Definition corochain.hpp:182