qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
Main.tpp
1/**
2 * @file qb/core/Main.tpp
3 * @brief Template implementation for the Main class
4 *
5 * This file contains the template implementation of the Main class methods and related
6 * classes defined in Main.h. It provides the actual implementation of actor creation,
7 * core initialization, and system configuration functionality.
8 *
9 * @author qb - C++ Actor Framework
10 * @copyright Copyright (c) 2011-2025 qb - isndev (cpp.actor)
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 * @ingroup Core
23 */
24
25#include "ActorId.h"
26#include "Main.h"
27#include "VirtualCore.h"
28
29#ifndef QB_MAIN_TPL
30#define QB_MAIN_TPL
31
32namespace qb {
33class Main;
34
35template <typename _Actor, typename... _Args>
36ActorId
37CoreInitializer::addActor(_Args &&...args) noexcept {
38 ActorId id = ActorId::NotFound;
39 if constexpr (std::is_base_of_v<Service, _Actor>) {
40 if (_registered_services.find(_Actor::ServiceIndex) ==
41 _registered_services.end()) {
42 _registered_services.insert(_Actor::ServiceIndex);
43 id = ActorId(_Actor::ServiceIndex, _index);
44 } else {
45 LOG_CRIT("[Start Sequence] Failed to add Service Actor("
46 << typeid(_Actor).name() << ")"
47 << " in Core(" << _index << ")"
48 << " : Already registered");
49 return id;
50 }
51 } else {
52 if (unlikely(_next_id == std::numeric_limits<ServiceId>::max())) {
53 LOG_CRIT("[Start Sequence] Failed to add Actor("
54 << typeid(_Actor).name() << ")"
55 << " in Core(" << _index << ")"
56 << " : Max number of Actors reached");
57 return id;
58 }
59 id = ActorId(_next_id++, _index);
60 }
61 _actor_factories.push_back(
62 new TActorFactory<_Actor, _Args...>(id, std::forward<_Args>(args)...));
63 return id;
64}
65
66template <typename _Actor, typename... _Args>
67CoreInitializer::ActorBuilder &
68CoreInitializer::ActorBuilder::addActor(_Args &&...args) noexcept {
69 auto id =
70 _initializer.template addActor<_Actor, _Args...>(std::forward<_Args>(args)...);
71 if (!id.is_valid())
72 _valid = false;
73
74 _ret_ids.push_back(id);
75 return *this;
76}
77
78template <typename _Actor, typename... _Args>
79ActorId
80Main::addActor(CoreId const cid, _Args &&...args) {
81 return core(cid).addActor<_Actor>(std::forward<_Args>(args)...);
82}
83
84} // namespace qb
85
86#endif // QB_MAIN_TPL