qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
Actor.tpp
1/**
2 * @file qb/core/Actor.tpp
3 * @brief Template implementation for the Actor class
4 *
5 * This file contains the template implementation of the Actor class methods defined
6 * in Actor.h. It provides the actual implementation of event handling, actor creation,
7 * and inter-actor communication mechanisms.
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 "VirtualCore.h"
26#include "VirtualCore.tpp"
27
28#ifndef QB_ACTOR_TPL
29#define QB_ACTOR_TPL
30
31namespace qb {
32
33template <typename _Actor>
34void
35Actor::registerCallback(_Actor &actor) const noexcept {
36 VirtualCore::_handler->registerCallback(actor);
37}
38
39template <typename _Actor>
40void
41Actor::unregisterCallback(_Actor &actor) const noexcept {
42 VirtualCore::_handler->unregisterCallback(actor.id());
43}
44
45template <typename _Event, typename _Actor>
46void
47Actor::registerEvent(_Actor &actor) const noexcept {
48 VirtualCore::_handler->registerEvent<_Event>(actor);
49}
50
51template <typename _Event, typename _Actor>
52void
53Actor::unregisterEvent(_Actor &actor) const noexcept {
54 VirtualCore::_handler->unregisterEvent<_Event>(actor);
55}
56
57template <typename _Event>
58void
59Actor::unregisterEvent() const noexcept {
60 VirtualCore::_handler->unregisterEvent<_Event>(*this);
61}
62
63template <typename _Actor, typename... _Args>
64_Actor *
65Actor::addRefActor(_Args &&...args) const {
66 return VirtualCore::_handler->template addReferencedActor<_Actor>(
67 std::forward<_Args>(args)...);
68}
69
70template <typename _Event, typename... _Args>
71_Event &
72Actor::push(ActorId const &dest, _Args &&...args) const noexcept {
73 return VirtualCore::_handler->template push<_Event>(dest, id(),
74 std::forward<_Args>(args)...);
75}
76
77template <typename _Event, typename... _Args>
78void
79Actor::send(ActorId const &dest, _Args &&...args) const noexcept {
80 VirtualCore::_handler->template send<_Event, _Args...>(dest, id(),
81 std::forward<_Args>(args)...);
82}
83
84template <typename _Event, typename... _Args>
85_Event
86Actor::build_event(ActorId const source, _Args &&...args) const noexcept {
87 _Event event{std::forward<_Args>(args)...};
88 VirtualCore::fill_event(event, id(), source);
89 return event;
90}
91
92template <typename _Required>
93bool
94Actor::require_type() const noexcept {
95 broadcast<PingEvent>(type_id<_Required>());
96 return true;
97}
98
99template <typename... _Types>
100bool
101Actor::require() const noexcept {
102 return (require_type<_Types>() && ...);
103}
104
105template <typename _Event, typename... _Args>
106void
107Actor::broadcast(_Args &&...args) const noexcept {
108 VirtualCore::_handler->template broadcast<_Event, _Args...>(
109 id(), std::forward<_Args>(args)...);
110}
111
112template <typename Tag>
113ActorId
114Actor::getServiceId(CoreId const index) noexcept {
115 return {VirtualCore::getServices()[type_id<Tag>()], index};
116}
117
118template <typename _ServiceActor>
119_ServiceActor *
120Actor::getService() const noexcept {
121 return VirtualCore::_handler->getService<_ServiceActor>();
122}
123
124template <typename _Event, typename... _Args>
125Actor::EventBuilder &
126Actor::EventBuilder::push(_Args &&...args) noexcept {
127 dest_pipe.push<_Event>(std::forward<_Args>(args)...);
128 return *this;
129}
130
131template <typename Tag>
132ServiceId
133Actor::registerIndex() noexcept {
134 return VirtualCore::getServices()[type_id<Tag>()] = ++VirtualCore::_nb_service;
135}
136
137} // namespace qb
138
139#endif