actor-framework
anon_mail.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_cast.hpp"
9 #include "caf/actor_clock.hpp"
10 #include "caf/detail/core_export.hpp"
11 #include "caf/detail/implicit_conversions.hpp"
12 #include "caf/detail/send_type_check.hpp"
13 #include "caf/disposable.hpp"
14 #include "caf/mailbox_element.hpp"
15 #include "caf/message.hpp"
16 #include "caf/message_priority.hpp"
17 #include "caf/none.hpp"
18 #include "caf/ref.hpp"
19 
20 namespace caf {
21 
24 template <message_priority Priority, class... Args>
26 public:
28  : content_(std::move(content)), timeout_(timeout) {
29  // nop
30  }
31 
33 
34  anon_scheduled_mail_t& operator=(const anon_scheduled_mail_t&) = delete;
35 
44  template <class Handle, class RefTag = strong_ref_t>
45  disposable send(const Handle& receiver, RefTag ref_tag = {}) && {
46  static_assert(is_ref_tag<RefTag>);
47  detail::send_type_check<none_t, Handle, Args...>();
48  if (!receiver)
49  return {};
50  auto* ptr = actor_cast<abstract_actor*>(receiver);
51  auto& clock = ptr->home_system().clock();
52  return clock.schedule_message(nullptr, actor_cast(receiver, ref_tag),
53  timeout_, make_message_id(Priority),
54  std::move(content_));
55  }
56 
57 private:
58  message content_;
59  actor_clock::time_point timeout_;
60 };
61 
63 template <message_priority Priority, class... Args>
64 class anon_mail_t {
65 public:
66  explicit anon_mail_t(message&& content) : content_(std::move(content)) {
67  // nop
68  }
69 
70  anon_mail_t(const anon_mail_t&) = delete;
71 
72  anon_mail_t& operator=(const anon_mail_t&) = delete;
73 
75  template <message_priority P = Priority,
76  class E = std::enable_if_t<P == message_priority::normal>>
77  [[nodiscard]] auto urgent() && {
78  using result_t = anon_mail_t<message_priority::high, Args...>;
79  return result_t{std::move(content_)};
80  }
81 
83  [[nodiscard]] auto schedule(actor_clock::time_point timeout) && {
84  using result_t = anon_scheduled_mail_t<Priority, Args...>;
85  return result_t{std::move(content_), timeout};
86  }
87 
89  [[nodiscard]] auto delay(actor_clock::duration_type timeout) && {
90  using clock = actor_clock::clock_type;
91  using result_t = anon_scheduled_mail_t<Priority, Args...>;
92  return result_t{std::move(content_), clock::now() + timeout};
93  }
94 
96  template <class Handle>
97  void send(const Handle& receiver) && {
98  detail::send_type_check<none_t, Handle, Args...>();
99  if (!receiver)
100  return;
101  auto* ptr = actor_cast<abstract_actor*>(receiver);
102  ptr->enqueue(make_mailbox_element(nullptr, make_message_id(Priority),
103  std::move(content_)),
104  nullptr);
105  }
106 
107 private:
108  message content_;
109 };
110 
112 template <class... Args>
113 [[nodiscard]] auto anon_mail(Args&&... args) {
114  using result_t = anon_mail_t<message_priority::normal,
115  detail::strip_and_convert_t<Args>...>;
116  return result_t{make_message_nowrap(std::forward<Args>(args)...)};
117 }
118 
119 } // namespace caf
actor_system & home_system() const noexcept
Returns the system that created this actor (or proxy).
Definition: abstract_actor.cpp:127
Provides a fluent interface for sending anonymous messages to actors.
Definition: anon_mail.hpp:64
std::chrono::steady_clock clock_type
Underlying clock type.
Definition: actor_clock.hpp:22
auto schedule(actor_clock::time_point timeout) &&
Schedules the message for delivery with an absolute timeout.
Definition: anon_mail.hpp:83
Describes a fixed-length, copy-on-write, type-erased tuple with elements of any type.
Definition: message.hpp:32
disposable send(const Handle &receiver, RefTag ref_tag={}) &&
Sends the message to receiver.
Definition: anon_mail.hpp:45
Provides a fluent interface for sending anonymous messages to actors at a specific point in time...
Definition: anon_mail.hpp:25
actor_clock & clock() noexcept
Returns the system-wide clock.
Definition: actor_system.cpp:649
typename clock_type::duration duration_type
Time interval.
Definition: actor_clock.hpp:28
T actor_cast(U &&what)
Converts the actor handle what to a different actor handle or raw pointer of type T...
Definition: actor_cast.hpp:148
typename clock_type::time_point time_point
Discrete point in time.
Definition: actor_clock.hpp:25
auto delay(actor_clock::duration_type timeout) &&
Schedules the message for delivery with a relative timeout.
Definition: anon_mail.hpp:89
Represents "nothing", e.g., for clearing an optional by assigning none.
Definition: none.hpp:14
auto anon_mail(Args &&... args)
Entry point for sending an anonymous message to an actor.
Definition: anon_mail.hpp:113
void send(const Handle &receiver) &&
Sends the message to receiver.
Definition: anon_mail.hpp:97
Base class for all actor implementations.
Definition: abstract_actor.hpp:48
auto urgent() &&
Tags the message as urgent, i.e., sends it with high priority.
Definition: anon_mail.hpp:77
const settings & content(const actor_system_config &cfg)
Returns all user-provided configuration parameters.
Definition: actor_system_config.cpp:594
virtual bool enqueue(mailbox_element_ptr what, scheduler *sched)=0
Enqueues a new message wrapped in a mailbox_element to the actor.
Represents a disposable resource.
Definition: disposable.hpp:15
Root namespace of libcaf.
Definition: custom_types_4.cpp:139
disposable schedule_message(time_point t, strong_actor_ptr receiver, mailbox_element_ptr content)
Schedules an arbitrary message to receiver for time point t.
Definition: actor_clock.cpp:139