rad 6.2.0
Loading...
Searching...
No Matches
coroActivity.hpp
Go to the documentation of this file.
1
7#ifndef RAD_COROACTIVITY_HPP
8#define RAD_COROACTIVITY_HPP
9
10#define BOOST_COROUTINES_NO_DEPRECATION_WARNING
11
12#include <tuple>
13
14#include <scxml4cpp/Activity.h>
15#include <boost/asio/io_context.hpp>
16#include <boost/asio/spawn.hpp>
17
18namespace rad {
19
20namespace detail {
21
22template <class T, class... Args, std::size_t... Is>
23std::shared_ptr<T> MakeSharedFromTupleImpl(std::tuple<Args...>& t, std::index_sequence<Is...>) {
24 return std::make_shared<T>(std::get<Is>(t)...);
25}
26
27} // namespace detail
28
29template <class T, class... Args>
30std::shared_ptr<T> MakeSharedFromTuple(std::tuple<Args...>& args) {
31 return detail::MakeSharedFromTupleImpl<T>(args, std::index_sequence_for<Args...>{});
32}
33
43template <class CORO, class... Args>
45 public:
49 explicit CoroActivity(std::string const& id, boost::asio::io_context& io_ctx, Args... args)
50 : scxml4cpp::Activity(id), m_io_ctx(io_ctx), m_args(args...) {}
51
56 void start(scxml4cpp::Context* c) override {
57 stop(c);
58 m_coro = MakeSharedFromTuple<CORO, Args...>(m_args);
59 // Launch coroutine
60 boost::asio::spawn(m_io_ctx,
61 [&](boost::asio::yield_context yield) { m_coro->operator()(yield); });
62 }
63
70 void stop(scxml4cpp::Context* c) override {
71 if (m_coro) {
72 // Cancel coroutine and detach it
73 m_coro->Cancel();
74 m_coro.reset();
75 }
76 }
77
78 private:
79 boost::asio::io_context& m_io_ctx;
80 std::tuple<Args...> m_args;
81 std::shared_ptr<CORO> m_coro;
82};
83
84} // namespace rad
85#endif // #ifndef RAD_COROACTIVITY_HPP
Activity header.
Definition coroActivity.hpp:44
void stop(scxml4cpp::Context *c) override
Definition coroActivity.hpp:70
CoroActivity(std::string const &id, boost::asio::io_context &io_ctx, Args... args)
Definition coroActivity.hpp:49
void start(scxml4cpp::Context *c) override
Definition coroActivity.hpp:56
Definition Activity.h:52
Activity(const std::string &id)
Definition Activity.cpp:37
Definition Context.h:58
std::shared_ptr< T > MakeSharedFromTupleImpl(std::tuple< Args... > &t, std::index_sequence< Is... >)
Definition coroActivity.hpp:23
Definition actionsApp.cpp:23
std::shared_ptr< T > MakeSharedFromTuple(std::tuple< Args... > &args)
Definition coroActivity.hpp:30
Definition Action.cpp:36