Main.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_MAIN_H
19 #define QB_MAIN_H
20 # include <iostream>
21 # include <vector>
22 # include <unordered_map>
23 // include from qb
24 # include <qb/system/lockfree/mpsc.h>
25 # include "Event.h"
26 # include "CoreSet.h"
27 
28 namespace qb {
29 
30  class VirtualCore;
31 
39  class Main {
40  friend class VirtualCore;
41  constexpr static const uint64_t MaxRingEvents =
42  (((std::numeric_limits<uint16_t>::max)()) / QB_LOCKFREE_CACHELINE_BYTES);
45 
46  static std::atomic<uint64_t> sync_start;
47  static bool is_running;
48  static void onSignal(int signal);
49 
50  private:
51  CoreSet _core_set;
52  std::vector<MPSCBuffer *> _mail_boxes;
53  std::unordered_map<uint8_t, VirtualCore *> _cores;
54 
55  void __init__();
56  bool send(Event const &event) const;
57  MPSCBuffer &getMailBox(uint8_t const id) const;
58  std::size_t getNbCore() const;
59  public:
60 
65  class CoreBuilder {
66  public:
67  using ActorIdList = std::vector<ActorId>;
68  private:
69  friend class Main;
70 
71  const uint16_t _index;
72  Main &_main;
73  ActorIdList _ret_ids;
74  bool _valid;
75 
76  CoreBuilder(Main &main, uint16_t const index)
77  : _index(index)
78  , _main(main)
79  , _valid(true)
80  {}
81 
82  CoreBuilder() = delete;
83  public:
84  CoreBuilder(CoreBuilder const &rhs);
85 
104  template<typename _Actor, typename ..._Args>
105  CoreBuilder &addActor(_Args &&...args);
106 
107  bool valid() const;
108  operator bool() const;
109 
114  ActorIdList const &idList() const;
115  };
116 
117  Main() = delete;
118  explicit Main(CoreSet const &core_set);
119  explicit Main(std::unordered_set<uint8_t> const &core_set);
120  ~Main();
121 
128  void start(bool async = true) const;
129 
130  static bool hasError();
131 
137  static void stop();
138 
144  void join() const;
145 
146  public:
147 
163  template<typename _Actor, typename ..._Args>
164  ActorId addActor(std::size_t index, _Args &&...args);
165 
178  CoreBuilder core(uint16_t const index);
179 
180  };
181 
182 } // namespace qb
183 
184 #endif //QB_MAIN_H
Event base class.
Definition: Event.h:40
void start(bool async=true) const
Start the engine.
void join() const
Wait until engine terminates.
Definition: VirtualCore.h:50
static void stop()
Stop the engine.
CoreBuilder & addActor(_Args &&...args)
Create new _Actor.
Helper to build Actors in VirtualCore.
Definition: Main.h:65
CoreBuilder core(uint16_t const index)
Get CoreBuilder from index.
Definition: mpsc.h:29
Actor unique identifier.
Definition: ActorId.h:35
ActorId addActor(std::size_t index, _Args &&...args)
Create new _Actor.
Core main class.
Definition: Main.h:39
Main initializer.
Definition: CoreSet.h:31
ActorIdList const & idList() const
Get list of created ActorId by the CoreBuilder.