actor-framework
actor_addr.hpp
1 // This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
2 // the main distribution directory for license terms and copyright or visit
3 // https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
4 
5 #pragma once
6 
7 #include "caf/abstract_actor.hpp"
8 #include "caf/actor_control_block.hpp"
9 #include "caf/detail/comparable.hpp"
10 #include "caf/detail/core_export.hpp"
11 #include "caf/fwd.hpp"
12 
13 #include <cstddef>
14 #include <cstdint>
15 #include <functional>
16 #include <type_traits>
17 
18 namespace caf {
19 
21 class CAF_CORE_EXPORT actor_addr
22  : detail::comparable<actor_addr>,
23  detail::comparable<actor_addr, weak_actor_ptr>,
24  detail::comparable<actor_addr, strong_actor_ptr>,
25  detail::comparable<actor_addr, abstract_actor*>,
26  detail::comparable<actor_addr, actor_control_block*> {
27 public:
28  // -- friend types that need access to private ctors
29 
30  friend class abstract_actor;
31 
32  // allow conversion via actor_cast
33  template <class, class, int>
34  friend class actor_cast_access;
35 
36  // tell actor_cast which semantic this type uses
37  static constexpr bool has_weak_ptr_semantics = true;
38 
39  actor_addr() = default;
40  actor_addr(actor_addr&&) = default;
41  actor_addr(const actor_addr&) = default;
42  actor_addr& operator=(actor_addr&&) = default;
43  actor_addr& operator=(const actor_addr&) = default;
44 
45  actor_addr(std::nullptr_t);
46 
47  actor_addr& operator=(std::nullptr_t);
48 
50  actor_id id() const noexcept {
51  return ptr_->id();
52  }
53 
55  node_id node() const noexcept {
56  return ptr_->node();
57  }
58 
60  actor_system& home_system() const noexcept {
61  return *ptr_->home_system;
62  }
63 
65  void swap(actor_addr& other) noexcept;
66 
67  explicit operator bool() const {
68  return static_cast<bool>(ptr_);
69  }
70 
72 
73  static intptr_t compare(const actor_control_block* lhs,
74  const actor_control_block* rhs);
75 
76  intptr_t compare(const actor_addr& other) const noexcept;
77 
78  intptr_t compare(const abstract_actor* other) const noexcept;
79 
80  intptr_t compare(const actor_control_block* other) const noexcept;
81 
82  intptr_t compare(const weak_actor_ptr& other) const noexcept {
83  return compare(other.get());
84  }
85 
86  intptr_t compare(const strong_actor_ptr& other) const noexcept {
87  return compare(other.get());
88  }
89 
90  friend std::string to_string(const actor_addr& x) {
91  return to_string(x.ptr_);
92  }
93 
94  friend void append_to_string(std::string& x, const actor_addr& y) {
95  return append_to_string(x, y.ptr_);
96  }
97 
98  template <class Inspector>
99  friend bool inspect(Inspector& f, actor_addr& x) {
100  return inspect(f, x.ptr_);
101  }
102 
105  friend void destroy(actor_addr& x) {
106  x.ptr_.reset();
107  }
108 
110 
111  actor_control_block* get() const noexcept {
112  return ptr_.get();
113  }
114 
116 
117 private:
118  actor_control_block* release() noexcept {
119  return ptr_.release();
120  }
121 
122  actor_control_block* get_locked() const noexcept {
123  return ptr_.get_locked();
124  }
125 
127 
128  weak_actor_ptr ptr_;
129 };
130 
131 inline bool operator==(const actor_addr& x, std::nullptr_t) {
132  return x.get() == nullptr;
133 }
134 
135 inline bool operator==(std::nullptr_t, const actor_addr& x) {
136  return x.get() == nullptr;
137 }
138 
139 inline bool operator!=(const actor_addr& x, std::nullptr_t) {
140  return x.get() != nullptr;
141 }
142 
143 inline bool operator!=(std::nullptr_t, const actor_addr& x) {
144  return x.get() != nullptr;
145 }
146 
147 } // namespace caf
148 
149 // allow actor_addr to be used in hash maps
150 namespace std {
151 template <>
152 struct hash<caf::actor_addr> {
153  size_t operator()(const caf::actor_addr& ref) const {
154  return static_cast<size_t>(ref.id());
155  }
156 };
157 } // namespace std
node_id node() const noexcept
Returns the origin node of this actor.
Definition: actor_addr.hpp:55
Barton–Nackman trick implementation.
Definition: comparable.hpp:16
abstract_actor * get()
Returns a pointer to the actual actor instance.
Definition: actor_control_block.hpp:92
bool operator==(const action &lhs, const action &rhs) noexcept
Checks whether two actions are equal by comparing their pointers.
Definition: action.hpp:131
Definition: actor.hpp:178
Indicates that the thread has been launched by request of a user without using any of the default mec...
Actor environment including scheduler, registry, and optional components such as a middleman...
Definition: actor_system.hpp:87
Stores the address of typed as well as untyped actors.
Definition: actor_addr.hpp:21
bool operator!=(const action &lhs, const action &rhs) noexcept
Checks whether two actions are not equal by comparing their pointers.
Definition: action.hpp:136
actor_system & home_system() const noexcept
Returns the hosting actor system.
Definition: actor_addr.hpp:60
actor_id id() const noexcept
Returns the ID of this actor.
Definition: actor_addr.hpp:50
Definition: actor_cast.hpp:64
Base class for all actor implementations.
Definition: abstract_actor.hpp:48
A node ID is an opaque value for representing CAF instances in the network.
Definition: node_id.hpp:111
Actors are always allocated with a control block that stores its identity as well as strong and weak ...
Definition: actor_control_block.hpp:51
Root namespace of libcaf.
Definition: custom_types_4.cpp:139