2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/actor/impl/ActorId-decl.h"
|
|
|
|
#include "td/actor/impl/ActorInfo-decl.h"
|
|
|
|
#include "td/actor/impl/Scheduler-decl.h"
|
|
|
|
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
// If actor is on our scheduler(thread) result will be valid
|
|
|
|
// If actor is on another scheduler we will see it in migrate_dest_flags
|
|
|
|
template <class ActorType>
|
|
|
|
ActorInfo *ActorId<ActorType>::get_actor_info() const {
|
|
|
|
if (ptr_.is_alive()) {
|
|
|
|
return &*ptr_;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ActorType>
|
|
|
|
ActorType *ActorId<ActorType>::get_actor_unsafe() const {
|
|
|
|
return static_cast<ActorType *>(ptr_->get_actor_unsafe());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ActorType>
|
|
|
|
Slice ActorId<ActorType>::get_name() const {
|
|
|
|
return ptr_->get_name();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ActorType>
|
|
|
|
ActorOwn<ActorType>::ActorOwn(ActorId<ActorType> id) : id_(std::move(id)) {
|
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
|
|
|
template <class OtherActorType>
|
|
|
|
ActorOwn<ActorType>::ActorOwn(ActorId<OtherActorType> id) : id_(std::move(id)) {
|
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
|
|
|
template <class OtherActorType>
|
|
|
|
ActorOwn<ActorType>::ActorOwn(ActorOwn<OtherActorType> &&other) : id_(other.release()) {
|
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
|
|
|
template <class OtherActorType>
|
|
|
|
ActorOwn<ActorType> &ActorOwn<ActorType>::operator=(ActorOwn<OtherActorType> &&other) {
|
|
|
|
reset(static_cast<ActorId<ActorType>>(other.release()));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ActorType>
|
2021-10-18 13:36:15 +02:00
|
|
|
ActorOwn<ActorType>::ActorOwn(ActorOwn &&other) noexcept : id_(other.release()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
2021-10-18 13:36:15 +02:00
|
|
|
ActorOwn<ActorType> &ActorOwn<ActorType>::operator=(ActorOwn &&other) noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
reset(other.release());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ActorType>
|
|
|
|
ActorOwn<ActorType>::~ActorOwn() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ActorType>
|
|
|
|
bool ActorOwn<ActorType>::empty() const {
|
|
|
|
return id_.empty();
|
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
|
|
|
ActorId<ActorType> ActorOwn<ActorType>::get() const {
|
|
|
|
return id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ActorType>
|
|
|
|
ActorId<ActorType> ActorOwn<ActorType>::release() {
|
|
|
|
return std::move(id_);
|
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
|
|
|
void ActorOwn<ActorType>::reset(ActorId<ActorType> other) {
|
|
|
|
static_assert(sizeof(ActorType) > 0, "Can't use ActorOwn with incomplete type");
|
|
|
|
if (!id_.empty()) {
|
|
|
|
send_event(id_, Event::hangup());
|
|
|
|
}
|
2022-10-01 14:58:27 +02:00
|
|
|
id_ = std::move(other);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
2022-10-01 10:29:09 +02:00
|
|
|
ActorType *ActorOwn<ActorType>::get_actor_unsafe() const {
|
|
|
|
return id_.get_actor_unsafe();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ActorType>
|
|
|
|
template <class OtherActorType>
|
|
|
|
ActorShared<ActorType>::ActorShared(ActorId<OtherActorType> id, uint64 token) : id_(std::move(id)), token_(token) {
|
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
|
|
|
template <class OtherActorType>
|
|
|
|
ActorShared<ActorType>::ActorShared(ActorShared<OtherActorType> &&other) : id_(other.release()), token_(other.token()) {
|
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
|
|
|
template <class OtherActorType>
|
|
|
|
ActorShared<ActorType>::ActorShared(ActorOwn<OtherActorType> &&other) : id_(other.release()), token_(0) {
|
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
|
|
|
template <class OtherActorType>
|
|
|
|
ActorShared<ActorType> &ActorShared<ActorType>::operator=(ActorShared<OtherActorType> &&other) {
|
|
|
|
reset(other.release());
|
|
|
|
token_ = other.token();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ActorType>
|
2021-10-18 13:36:15 +02:00
|
|
|
ActorShared<ActorType>::ActorShared(ActorShared &&other) noexcept : id_(other.release()), token_(other.token_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
2021-10-18 13:36:15 +02:00
|
|
|
ActorShared<ActorType> &ActorShared<ActorType>::operator=(ActorShared &&other) noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
reset(other.release());
|
|
|
|
token_ = other.token_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ActorType>
|
|
|
|
ActorShared<ActorType>::~ActorShared() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ActorType>
|
|
|
|
uint64 ActorShared<ActorType>::token() const {
|
|
|
|
return token_;
|
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
|
|
|
bool ActorShared<ActorType>::empty() const {
|
|
|
|
return id_.empty();
|
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
|
|
|
ActorId<ActorType> ActorShared<ActorType>::get() const {
|
|
|
|
return id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ActorType>
|
|
|
|
ActorId<ActorType> ActorShared<ActorType>::release() {
|
|
|
|
return std::move(id_);
|
|
|
|
}
|
2022-10-01 10:29:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ActorType>
|
|
|
|
void ActorShared<ActorType>::reset(ActorId<ActorType> other) {
|
|
|
|
static_assert(sizeof(ActorType) > 0, "Can't use ActorShared with incomplete type");
|
|
|
|
if (!id_.empty()) {
|
|
|
|
send_event(*this, Event::hangup());
|
|
|
|
}
|
2022-10-01 15:29:56 +02:00
|
|
|
id_ = std::move(other);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
ActorRef::ActorRef(const ActorId<T> &actor_id) : actor_id_(actor_id) {
|
|
|
|
}
|
2022-10-01 22:48:03 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
2018-09-26 14:48:10 +02:00
|
|
|
ActorRef::ActorRef(ActorId<T> &&actor_id) : actor_id_(actor_id) {
|
2018-09-26 11:01:50 +02:00
|
|
|
actor_id.clear();
|
|
|
|
}
|
2022-10-01 22:48:03 +02:00
|
|
|
|
2018-09-26 11:01:50 +02:00
|
|
|
template <class T>
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorRef::ActorRef(const ActorShared<T> &actor_id) : actor_id_(actor_id.get()), token_(actor_id.token()) {
|
|
|
|
}
|
2022-10-01 22:48:03 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
|
|
|
ActorRef::ActorRef(ActorShared<T> &&actor_id) : actor_id_(actor_id.release()), token_(actor_id.token()) {
|
|
|
|
}
|
2022-10-01 22:48:03 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
|
|
|
ActorRef::ActorRef(const ActorOwn<T> &actor_id) : actor_id_(actor_id.get()) {
|
|
|
|
}
|
2022-10-01 22:48:03 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
|
|
|
ActorRef::ActorRef(ActorOwn<T> &&actor_id) : actor_id_(actor_id.release()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|