rotor
Event loop friendly C++ actor micro-framework
 
Loading...
Searching...
No Matches
actor_config.h
1#pragma once
2
3//
4// Copyright (c) 2019-2022 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
5//
6// Distributed under the MIT Software License
7//
8
9#include <deque>
10#include <tuple>
11#include <functional>
12#include <memory>
13#include "arc.hpp"
14#include "plugins.h"
15#include "policy.h"
16#include "forward.hpp"
17
18#if defined(_MSC_VER)
19#pragma warning(push)
20#pragma warning(disable : 4251)
21#endif
22
23namespace rotor {
24
26using plugins_t = std::deque<plugin::plugin_base_t *>;
27
31 virtual ~plugin_storage_base_t() {}
32
34 virtual plugins_t get_plugins() noexcept = 0;
35};
36
39
41template <typename PluginList> struct plugin_storage_t : plugin_storage_base_t {
42 plugins_t get_plugins() noexcept override {
43 plugins_t plugins;
44 add_plugin(plugins);
45 return plugins;
46 }
47
48 private:
49 PluginList plugin_list;
50
51 template <size_t Index = 0> void add_plugin(plugins_t &plugins) noexcept {
52 plugins.emplace_back(&std::get<Index>(plugin_list));
53 if constexpr (Index + 1 < std::tuple_size_v<PluginList>) {
54 add_plugin<Index + 1>(plugins);
55 }
56 }
57};
58
65
68
71
73 pt::time_duration init_timeout;
74
76 pt::time_duration shutdown_timeout;
77
80
82 bool escalate_failure = false;
83
85 bool autoshutdown_supervisor = false;
86
88 actor_config_t(supervisor_t *supervisor_) : supervisor{supervisor_} {}
89};
90
92template <typename Actor> struct actor_config_builder_t {
94 using builder_t = typename Actor::template config_builder_t<Actor>;
95
97 using config_t = typename Actor::config_t;
98
101
107 using install_action_t = std::function<void(actor_ptr_t &)>;
108
110 constexpr static const std::uint32_t INIT_TIMEOUT = 1 << 0;
111
113 constexpr static const std::uint32_t SHUTDOWN_TIMEOUT = 1 << 1;
114
116 constexpr static const std::uint32_t requirements_mask = INIT_TIMEOUT | SHUTDOWN_TIMEOUT;
117
120
123
126
129
131 std::uint32_t mask = builder_t::requirements_mask;
132
135
138 : install_action{std::move(action_)}, supervisor{nullptr}, system_context{system_context_}, config{nullptr} {
139 init_ctor();
140 }
141
143 builder_t &&timeout(const pt::time_duration &timeout) && noexcept {
144 config.init_timeout = config.shutdown_timeout = timeout;
145 mask = (mask & (~INIT_TIMEOUT & ~SHUTDOWN_TIMEOUT));
146 return std::move(*static_cast<builder_t *>(this));
147 }
148
150 builder_t &&init_timeout(const pt::time_duration &timeout) && noexcept {
151 config.init_timeout = timeout;
152 mask = (mask & ~INIT_TIMEOUT);
153 return std::move(*static_cast<builder_t *>(this));
154 }
155
157 builder_t &&shutdown_timeout(const pt::time_duration &timeout) && noexcept {
158 config.shutdown_timeout = timeout;
159 mask = (mask & ~SHUTDOWN_TIMEOUT);
160 return std::move(*static_cast<builder_t *>(this));
161 }
162
168 builder_t &&spawner_address(const address_ptr_t &value) && noexcept {
169 config.spawner_address = value;
170 return std::move(*static_cast<builder_t *>(this));
171 }
172
181 builder_t &&escalate_failure(bool value = true) && noexcept {
182 config.escalate_failure = value;
183 return std::move(*static_cast<builder_t *>(this));
184 }
185
191 builder_t &&autoshutdown_supervisor(bool value = true) && noexcept {
192 config.autoshutdown_supervisor = value;
193 return std::move(*static_cast<builder_t *>(this));
194 }
195
197 virtual bool validate() noexcept { return mask ? false : true; }
198
200 actor_ptr_t finish() &&;
201
202 virtual ~actor_config_builder_t() = default;
203
204 private:
205 void init_ctor() noexcept {
206 config.plugins_constructor = []() -> plugin_storage_ptr_t {
207 using plugins_list_t = typename Actor::plugins_list_t;
208 using storage_t = plugin_storage_t<plugins_list_t>;
209 return std::make_unique<storage_t>();
210 };
211 }
212};
213
214} // namespace rotor
215
216#if defined(_MSC_VER)
217#pragma warning(pop)
218#endif
Basic namespace for all rotor functionalities.
Definition rotor.hpp:21
intrusive_ptr_t< address_t > address_ptr_t
intrusive pointer for address
Definition address.hpp:57
std::unique_ptr< plugin_storage_base_t > plugin_storage_ptr_t
smart pointer for plugin_storage_base_t
Definition actor_config.h:38
intrusive_ptr_t< actor_base_t > actor_ptr_t
intrusive pointer for actor
Definition forward.hpp:23
std::deque< plugin::plugin_base_t * > plugins_t
list of raw plugin pointers
Definition actor_config.h:26
boost::intrusive_ptr< T > intrusive_ptr_t
alias for intrusive pointer
Definition arc.hpp:27
CRTP actor config builder.
Definition actor_config.h:92
builder_t && autoshutdown_supervisor(bool value=true) &&noexcept
shutdown supervisor when an actor is down
Definition actor_config.h:191
builder_t && init_timeout(const pt::time_duration &timeout) &&noexcept
setter for init timeout
Definition actor_config.h:150
builder_t && escalate_failure(bool value=true) &&noexcept
shutdown supervisor upon actor failure
Definition actor_config.h:181
typename Actor::config_t config_t
final config class
Definition actor_config.h:97
system_context_t & system_context
reference to system_context_t
Definition actor_config.h:125
virtual bool validate() noexcept
checks whether config is valid, i.e. all necessary fields are set
Definition actor_config.h:197
builder_t && spawner_address(const address_ptr_t &value) &&noexcept
spawner address
Definition actor_config.h:168
builder_t && shutdown_timeout(const pt::time_duration &timeout) &&noexcept
setter for shutdown timeout
Definition actor_config.h:157
config_t config
the currently build config
Definition actor_config.h:128
builder_t && timeout(const pt::time_duration &timeout) &&noexcept
setter for init and shutdown timeout
Definition actor_config.h:143
typename Actor::template config_builder_t< Actor > builder_t
final builder class
Definition actor_config.h:94
intrusive_ptr_t< Actor > actor_ptr_t
intrusive pointer to actor
Definition actor_config.h:100
actor_config_builder_t(install_action_t &&action_, system_context_t &system_context_)
ctor with install action and pointer to system_context_t
Definition actor_config.h:137
supervisor_t * supervisor
raw pointer to supervisor_t (is null for top-level supervisors)
Definition actor_config.h:122
std::function< void(actor_ptr_t &)> install_action_t
actor post-constructor callback type
Definition actor_config.h:107
install_action_t install_action
post-construction callback
Definition actor_config.h:119
basic actor configuration: init and shutdown timeouts, etc.
Definition actor_config.h:62
std::function< plugin_storage_ptr_t()> plugins_constructor_t
constructs plugin_storage_ptr_t (type)
Definition actor_config.h:64
pt::time_duration init_timeout
max actor initialization time
Definition actor_config.h:73
supervisor_t * supervisor
raw pointer to supervisor_t
Definition actor_config.h:70
actor_config_t(supervisor_t *supervisor_)
constructs actor_config_t from raw supervisor pointer
Definition actor_config.h:88
pt::time_duration shutdown_timeout
max actor shutdown time
Definition actor_config.h:76
plugins_constructor_t plugins_constructor
constructs plugin_storage_ptr_t
Definition actor_config.h:67
address_ptr_t spawner_address
address of the spawner
Definition actor_config.h:79
abstract item to store plugins inside actor
Definition actor_config.h:30
virtual plugins_t get_plugins() noexcept=0
returns list of plugins pointers from the storage
templated plugin storage implementation
Definition actor_config.h:41
plugins_t get_plugins() noexcept override
returns list of plugins pointers from the storage
Definition actor_config.h:42
supervisor is responsible for managing actors (workers) lifetime
Definition supervisor.h:69
The system context holds root supervisor_t (intrusive pointer) and may be loop-related details in der...
Definition system_context.h:32