Actor.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_ACTOR_H
19 # define QB_ACTOR_H
20 # include <vector>
21 # include <map>
22 # include <unordered_map>
23 // include from qb
24 # include <qb/utility/nocopy.h>
25 # include "ICallback.h"
26 # include "ProxyPipe.h"
27 # include "Event.h"
28 
29 namespace qb {
30 
31  class VirtualCore;
32 
41  class Actor
42  : nocopy
43  , ActorId
44  {
45  friend class VirtualCore;
46 
47  mutable bool _alive = true;
48  VirtualCore * _handler = nullptr;
49  void __set_id(ActorId const &id);
50  protected:
54  void __set_id(uint16_t const sid, uint16_t const cid);
58  template <typename Tag>
59  static uint16_t registerIndex();
60 
68  Actor() = default;
69 
72  virtual ~Actor() = default;
73 
90  virtual bool onInit() = 0;
91 
92  public:
96  void kill() const;
97 
102  protected:
126  void on(KillEvent const &event);
127 
132  public:
133 
138  class EventBuilder {
139  friend class Actor;
140  ProxyPipe dest_pipe;
141 
142  EventBuilder() = delete;
143  EventBuilder(ProxyPipe const &pipe);
144  public:
145  EventBuilder(EventBuilder const &rhs) = default;
146 
165  template<typename _Event, typename ..._Args>
166  EventBuilder &push(_Args &&...args);
167  };
168 
178  ActorId id() const { return *this; }
179 
184  uint16_t getIndex() const;
185 
189  template <typename T>
190  static ActorId getServiceId(uint16_t const index);
191 
206  uint64_t time() const;
207 
212  bool isAlive() const;
213 
249  template <typename _Actor>
250  void registerCallback(_Actor &actor) const;
251 
261  template <typename _Actor>
262  void unregisterCallback(_Actor &actor) const;
263 
267  void unregisterCallback() const;
268 
269 
290  template<typename _Event, typename _Actor>
291  void registerEvent(_Actor &actor);
292 
303  template<typename _Event, typename _Actor>
304  void unregisterEvent(_Actor &actor);
305 
310  template<typename _Event>
311  void unregisterEvent();
312 
334  EventBuilder to(ActorId const dest) const;
335 
358  template<typename _Event, typename ..._Args>
359  _Event &push(ActorId const &dest, _Args &&...args) const;
360 
382  template<typename _Event, typename ..._Args>
383  void send(ActorId const &dest, _Args &&...args) const;
384 
400  void reply(Event &event) const;
401 
418  void forward(ActorId const dest, Event &event) const;
419 
420  // OpenApi : internal future use
421  // void send(Event const &event) const;
422  // void push(Event const &event) const;
423  // bool try_send(Event const &event) const;
424 
434  ProxyPipe getPipe(ActorId const dest) const;
435 
456  template<typename _Actor, typename ..._Args>
457  _Actor *addRefActor(_Args &&...args) const;
458 
463  };
464 
469  class Service {};
470 
481  template <typename Tag>
482  class ServiceActor : public Service, public Actor {
483  static const uint16_t ServiceIndex;
484  public:
485  ServiceActor() {
486  __set_id(ServiceIndex, 0);
487  }
488  };
489 
490 }
491 
492 qb::io::stream &operator<<(qb::io::stream &os, qb::Actor const &actor);
493 
494 #endif //QB_ACTOR_H
_Actor * addRefActor(_Args &&...args) const
Create new referenced _Actor.
Event base class.
Definition: Event.h:40
EventBuilder & push(_Args &&...args)
Send a new ordered event.
EventBuilder to(ActorId const dest) const
Get EventBuilder for ActorId destination.
void kill() const
ProxyPipe getPipe(ActorId const dest) const
Get access to unidirectional out events pipe.
void reply(Event &event) const
Reply an event.
Actor base class.
Definition: Actor.h:41
Helper to build Events.
Definition: Actor.h:138
Definition: VirtualCore.h:50
Object returned by Actor::getPipe()
Definition: ProxyPipe.h:33
void send(ActorId const &dest, _Args &&...args) const
Send a new unordered event.
void registerEvent(_Actor &actor)
Actor will listen on new _Event.
Actor unique identifier.
Definition: ActorId.h:35
uint16_t sid() const
void forward(ActorId const dest, Event &event) const
Forward an event.
ActorId id() const
Definition: Actor.h:178
uint16_t index() const
internal
Definition: Actor.h:469
_Event & push(ActorId const &dest, _Args &&...args) const
Send a new ordered event.
SingletonActor base class.
Definition: Actor.h:482
void registerCallback(_Actor &actor) const
Register a looped callback.
uint16_t getIndex() const
bool isAlive() const
Check if Actor is alive.
Actor()=default
virtual ~Actor()=default
Definition: nocopy.h:22
uint64_t time() const
Get current time.
Definition: Event.h:93
virtual bool onInit()=0
DerivedActor should implement this method.
void on(KillEvent const &event)
Receiving this event will kill the Actor.