Event.h
1 /*
2  * qb - C++ Actor Framework
3  * Copyright (C) 2011-2019 isndev (www.qbaf.io). All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef QB_EVENT_H
19 # define QB_EVENT_H
20 # include <utility>
21 # include <bitset>
22 // include from qb
23 # include "ActorId.h"
24 
25 namespace qb {
26 
27  template<typename T>
28  struct type {
29  constexpr static void id() {}
30  };
31 
32  template<typename T>
33  constexpr uint16_t type_id() { return static_cast<uint16_t>(reinterpret_cast<std::size_t>(&type<T>::id)); }
34 
40  class Event {
41  friend class Main;
42  friend class VirtualCore;
43  friend class Actor;
44  friend class ProxyPipe;
45  friend struct ServiceEvent;
46 
47  uint16_t id;
48  uint16_t bucket_size;
49  std::bitset<32> state;
50  // for users
51  ActorId dest;
52  ActorId source;
53 
54  public:
55  Event() = default;
56 
57  inline ActorId getDestination() const { return dest; }
58  inline ActorId getSource() const { return source; }
59  };
60 
68  struct ServiceEvent : public Event {
69  ActorId forward;
70  uint16_t service_event_id;
71 
72  inline void received() {
73  std::swap(dest, forward);
74  std::swap(id, service_event_id);
75  live(true);
76  }
77 
78  inline void live(bool flag) {
79  state[0] = flag;
80  }
81 
82  inline bool isLive() { return state[0]; }
83 
84  inline uint16_t bucketSize() const {
85  return bucket_size;
86  }
87  };
88 
93  struct KillEvent : public Event {};
94 
95 } // namespace qb
96 
97 #endif //QB_EVENT_H
Definition: Event.h:28
Event base class.
Definition: Event.h:40
Actor base class.
Definition: Actor.h:41
Definition: VirtualCore.h:50
Object returned by Actor::getPipe()
Definition: ProxyPipe.h:33
Actor unique identifier.
Definition: ActorId.h:35
Core main class.
Definition: Main.h:39
More flexible Event.
Definition: Event.h:68
Definition: Event.h:93