rotor
Event loop friendly C++ actor micro-framework
 
Loading...
Searching...
No Matches
plugin_base.h
1#pragma once
2
3//
4// Copyright (c) 2019-2024 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
5//
6// Distributed under the MIT Software License
7//
8
9#include "../subscription.h"
10#include "rotor/messages.hpp"
11#include <typeinfo>
12#include <cstdint>
13
14namespace rotor::plugin {
15
16enum class config_phase_t { PREINIT = 0b01, INITIALIZING = 0b10 };
17
23struct ROTOR_API plugin_base_t {
24
26 // clang-format off
27 enum reaction_t: unsigned {
28 INIT = 0b00000001,
29 SHUTDOWN = 0b00000010,
30 SUBSCRIPTION = 0b00000100,
31 START = 0b00001000,
32 };
33 // clang-format on
35 using reaction_underlying_t = std::underlying_type_t<reaction_t>;
36
38 static constexpr reaction_underlying_t REACTION_MASK = 0b00001111;
39
41 plugin_base_t() = default;
42
44 plugin_base_t(const plugin_base_t &) = delete;
45 virtual ~plugin_base_t() = default;
46
48 virtual const std::type_index &identity() const noexcept = 0;
49
61 virtual void activate(actor_base_t *actor) noexcept;
62
73 virtual void deactivate() noexcept;
74
83 virtual bool handle_init(message::init_request_t *message) noexcept;
84
93 virtual bool handle_shutdown(message::shutdown_request_t *message) noexcept;
94
103 virtual void handle_start(message::start_trigger_t *message) noexcept;
104
113 virtual bool handle_subscription(message::subscription_t &message) noexcept;
114
124 virtual bool handle_unsubscription(const subscription_point_t &point, bool external) noexcept;
125
130 virtual bool forget_subscription(const subscription_point_t &point) noexcept;
131
137 virtual void forget_subscription(const subscription_info_ptr_t &info) noexcept;
138
146 template <typename Plugin, typename Fn>
147 void with_casted(Fn &&fn, config_phase_t desired_phase = config_phase_t::INITIALIZING) noexcept {
148 int phase_match = static_cast<int>(phase) & static_cast<int>(desired_phase);
149 if (phase_match && identity() == Plugin::class_identity) {
150 auto &final = static_cast<Plugin &>(*this);
151 fn(final);
152 }
153 }
154
156 reaction_underlying_t get_reaction() const noexcept { return reaction; }
157
159 void reaction_on(reaction_t value) noexcept { reaction = reaction | value; }
160
162 void reaction_off(reaction_t value) noexcept { reaction = reaction & (REACTION_MASK ^ value); }
164 template <typename T> auto &access() noexcept;
165
166 protected:
168 template <typename Handler>
169 subscription_info_ptr_t subscribe(Handler &&handler, const address_ptr_t &address) noexcept;
170
172 template <typename Handler> subscription_info_ptr_t subscribe(Handler &&handler) noexcept;
173
180
182 extended_error_ptr_t make_error(const std::error_code &ec, const extended_error_ptr_t &next = {},
183 const message_ptr_t &request = {}) noexcept;
184
185 private:
186 subscription_container_t own_subscriptions;
187 reaction_underlying_t reaction = 0;
188 config_phase_t phase = config_phase_t::PREINIT;
189};
190
191} // namespace rotor::plugin
intrusive_ptr_t< message_base_t > message_ptr_t
intrusive pointer for message
Definition message.h:115
intrusive_ptr_t< address_t > address_ptr_t
intrusive pointer for address
Definition address.hpp:57
intrusive_ptr_t< extended_error_t > extended_error_ptr_t
intrusive pointer to extended error type
Definition extended_error.h:25
intrusive_ptr_t< subscription_info_t > subscription_info_ptr_t
intrusive pointer for subscription_info_t
Definition subscription_point.h:127
universal primitive of concurrent computation
Definition actor_base.h:47
base class for all actor plugins
Definition plugin_base.h:23
std::underlying_type_t< reaction_t > reaction_underlying_t
the underlying type of reaction_t
Definition plugin_base.h:35
reaction_t
possible plugin's reactions on actor lifetime events
Definition plugin_base.h:27
reaction_underlying_t get_reaction() const noexcept
returns the current set of plugin reactions
Definition plugin_base.h:156
auto & access() noexcept
generic non-public fields accessor
plugin_base_t()=default
default plugin ctor
void reaction_on(reaction_t value) noexcept
turns on the specified reaction of the plugin
Definition plugin_base.h:159
virtual const std::type_index & identity() const noexcept=0
returns pointer, which uniquely identifiess plugin type
void reaction_off(reaction_t value) noexcept
turns off the specified reaction of the plugin
Definition plugin_base.h:162
plugin_base_t(const plugin_base_t &)=delete
copy ctor is not allowed
list of subscription_info_ptr_t with possibility to find via subscription_point_t
Definition subscription_point.h:132
pair of handler_base_t linked to particular address_t
Definition subscription_point.h:50
Holds and classifies message handlers on behalf of supervisor.
Definition subscription.h:30