aboutsummaryrefslogtreecommitdiffhomepage
path: root/tdactor/td/actor/impl/EventFull-decl.h
blob: 4137765111345d8cb6422ed9d3e77ceb3041f37e (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
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// 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/Event.h"

#include "td/utils/type_traits.h"

#include <type_traits>
#include <utility>

namespace td {

class EventFull {
 public:
  EventFull() = default;

  bool empty() const {
    return data_.empty();
  }

  void clear() {
    data_.clear();
  }

  ActorId<> actor_id() const {
    return actor_id_;
  }
  Event &data() {
    return data_;
  }

  void try_emit_later();
  void try_emit();

 private:
  friend class EventCreator;

  EventFull(ActorRef actor_ref, Event &&data) : actor_id_(actor_ref.get()), data_(std::move(data)) {
    data_.link_token = actor_ref.token();
  }
  template <class T>
  EventFull(ActorId<T> actor_id, Event &&data) : actor_id_(std::move(actor_id)), data_(std::move(data)) {
  }

  ActorId<> actor_id_;

  Event data_;
};

class EventCreator {
 public:
  template <class ActorIdT, class FunctionT, class... ArgsT>
  static EventFull closure(ActorIdT &&actor_id, FunctionT function, ArgsT &&...args) {
    using ActorT = typename std::decay_t<ActorIdT>::ActorT;
    using FunctionClassT = member_function_class_t<FunctionT>;
    static_assert(std::is_base_of<FunctionClassT, ActorT>::value, "unsafe send_closure");

    return EventFull(std::forward<ActorIdT>(actor_id), Event::delayed_closure(function, std::forward<ArgsT>(args)...));
  }

  template <class LambdaT>
  static EventFull lambda(ActorRef actor_ref, LambdaT &&lambda) {
    return EventFull(actor_ref, Event::lambda(std::forward<LambdaT>(lambda)));
  }

  static EventFull yield(ActorRef actor_ref) {
    return EventFull(actor_ref, Event::yield());
  }
  static EventFull raw(ActorRef actor_ref, uint64 data) {
    return EventFull(actor_ref, Event::raw(data));
  }
  static EventFull raw(ActorRef actor_ref, void *ptr) {
    return EventFull(actor_ref, Event::raw(ptr));
  }

  static EventFull event_unsafe(ActorId<> actor_id, Event &&event) {
    return EventFull(actor_id, std::move(event));
  }
};

}  // namespace td