aboutsummaryrefslogtreecommitdiffhomepage
path: root/tdactor/td/actor/impl/ActorId-decl.h
blob: 88c820470554e119bcb98fde9b2faa614d46fc72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
//
// 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/utils/ObjectPool.h"
#include "td/utils/Slice.h"

#include <type_traits>

namespace td {

class Actor;
class ActorInfo;

template <class ActorType = Actor>
class ActorId {
 public:
  using ActorT = ActorType;
  explicit ActorId(ObjectPool<ActorInfo>::WeakPtr ptr) : ptr_(ptr) {
  }
  ActorId() = default;
  ActorId(const ActorId &) = default;
  ActorId &operator=(const ActorId &) = default;
  ActorId(ActorId &&other) noexcept : ptr_(other.ptr_) {
    other.clear();
  }
  ActorId &operator=(ActorId &&other) noexcept {
    if (&other == this) {
      return *this;
    }
    ptr_ = other.ptr_;
    other.clear();
    return *this;
  }
  ~ActorId() = default;

  bool empty() const {
    return ptr_.empty();
  }
  void clear() {
    ptr_.clear();
  }

  bool is_alive() const {
    return ptr_.is_alive_unsafe();
  }

  ActorInfo *get_actor_info() const;

  ActorType *get_actor_unsafe() const;

  Slice get_name() const;

  template <class ToActorType, class = std::enable_if_t<std::is_base_of<ToActorType, ActorType>::value>>
  explicit operator ActorId<ToActorType>() const {
    return ActorId<ToActorType>(ptr_);
  }

 private:
  ObjectPool<ActorInfo>::WeakPtr ptr_;
};

// treat ActorId as pointer and ActorOwn as unique_ptr<ActorId>
template <class ActorType = Actor>
class ActorOwn {
 public:
  using ActorT = ActorType;
  ActorOwn() = default;
  explicit ActorOwn(ActorId<ActorType> id);
  template <class OtherActorType>
  explicit ActorOwn(ActorId<OtherActorType> id);
  template <class OtherActorType>
  explicit ActorOwn(ActorOwn<OtherActorType> &&other);
  template <class OtherActorType>
  ActorOwn &operator=(ActorOwn<OtherActorType> &&other);
  ActorOwn(ActorOwn &&other) noexcept;
  ActorOwn &operator=(ActorOwn &&other) noexcept;
  ActorOwn(const ActorOwn &) = delete;
  ActorOwn &operator=(const ActorOwn &) = delete;
  ~ActorOwn();

  bool empty() const;
  bool is_alive() const {
    return id_.is_alive();
  }
  ActorId<ActorType> get() const;
  ActorId<ActorType> release();
  void reset(ActorId<ActorType> other = ActorId<ActorType>());
  ActorType *get_actor_unsafe() const;

 private:
  ActorId<ActorType> id_;
};

template <class ActorType = Actor>
class ActorShared {
 public:
  using ActorT = ActorType;
  ActorShared() = default;
  template <class OtherActorType>
  ActorShared(ActorId<OtherActorType> id, uint64 token);
  template <class OtherActorType>
  ActorShared(ActorShared<OtherActorType> &&other);
  template <class OtherActorType>
  ActorShared(ActorOwn<OtherActorType> &&other);
  template <class OtherActorType>
  ActorShared &operator=(ActorShared<OtherActorType> &&other);
  ActorShared(ActorShared &&other) noexcept;
  ActorShared &operator=(ActorShared &&other) noexcept;
  ActorShared(const ActorShared &) = delete;
  ActorShared &operator=(const ActorShared &) = delete;
  ~ActorShared();

  uint64 token() const;
  bool empty() const;
  bool is_alive() const {
    return id_.is_alive();
  }
  ActorId<ActorType> get() const;
  ActorId<ActorType> release();
  void reset(ActorId<ActorType> other = ActorId<ActorType>());

 private:
  ActorId<ActorType> id_;
  uint64 token_ = 0;
};

class ActorRef {
 public:
  ActorRef() = default;
  template <class T>
  ActorRef(const ActorId<T> &actor_id);
  template <class T>
  ActorRef(ActorId<T> &&actor_id);
  template <class T>
  ActorRef(const ActorShared<T> &actor_id);
  template <class T>
  ActorRef(ActorShared<T> &&actor_id);
  template <class T>
  ActorRef(const ActorOwn<T> &actor_id);
  template <class T>
  ActorRef(ActorOwn<T> &&actor_id);
  ActorId<> get() const {
    return actor_id_;
  }
  uint64 token() const {
    return token_;
  }

 private:
  ActorId<> actor_id_;
  uint64 token_ = 0;
};

}  // namespace td