Maki
Loading...
Searching...
No Matches
machine_ref.hpp
1//Copyright Florian Goujeon 2021 - 2025.
2//Distributed under the Boost Software License, Version 1.0.
3//(See accompanying file LICENSE or copy at
4//https://www.boost.org/LICENSE_1_0.txt)
5//Official repository: https://github.com/fgoujeon/maki
6
11
12#ifndef MAKI_MACHINE_REF_HPP
13#define MAKI_MACHINE_REF_HPP
14
15#include "machine_ref_conf.hpp"
16#include "machine.hpp"
17#include "detail/tlu/apply.hpp"
18#include "detail/tlu/contains.hpp"
19#include <type_traits>
20
21namespace maki
22{
23
24namespace detail
25{
26 template<class... Events>
27 class machine_ref_event_impl;
28
29 template<class Event, class... Events>
30 class machine_ref_event_impl<Event, Events...>: machine_ref_event_impl<Events...>
31 {
32 public:
33 template<const auto& MachineConf>
34 machine_ref_event_impl(machine<MachineConf>& mach):
35 machine_ref_event_impl<Events...>{mach},
36 pprocess_event_
37 {
38 [](void* const vpsm, const Event& evt)
39 {
40 using machine_t = machine<MachineConf>;
41 const auto psm = reinterpret_cast<machine_t*>(vpsm); //NOLINT
42 psm->process_event(evt);
43 }
44 }
45 {
46 }
47
48 using machine_ref_event_impl<Events...>::process_event;
49
50 void process_event(const Event& evt) const
51 {
52 (*pprocess_event_)(get_vpsm(), evt);
53 }
54
55 protected:
56 using machine_ref_event_impl<Events...>::get_vpsm;
57
58 private:
59 void(*pprocess_event_)(void*, const Event&) = nullptr;
60 };
61
62 template<>
63 class machine_ref_event_impl<>
64 {
65 public:
66 template<const auto& MachineConf>
67 machine_ref_event_impl(machine<MachineConf>& mach):
68 vpsm_(&mach)
69 {
70 }
71
72 void process_event() const
73 {
74 }
75
76 protected:
77 [[nodiscard]] void* get_vpsm() const
78 {
79 return vpsm_;
80 }
81
82 private:
83 void* vpsm_ = nullptr;
84 };
85}
86
92template<const auto& Conf>
93class machine_ref
94{
95public:
96 template<const auto& MachineConf>
97 machine_ref(machine<MachineConf>& mach):
98 impl_{mach}
99 {
100 }
101
102 machine_ref(const machine_ref&) noexcept = default;
103 machine_ref(machine_ref&&) noexcept = default;
104 machine_ref& operator=(const machine_ref&) noexcept = default;
105 machine_ref& operator=(machine_ref&&) noexcept = default;
106 ~machine_ref() = default;
107
108 template<class Event>
109 void process_event(const Event& evt) const
110 {
111 static_assert
112 (
113 detail::tlu::contains_v
114 <
115 event_type_list,
116 Event
117 >,
118 "Given event type must be part of the type list given to `events()`"
119 );
120 impl_.process_event(evt);
121 }
122
123private:
124 using event_type_list = typename std::decay_t<decltype(Conf)>::event_type_list;
125
126 using event_impl_type = detail::tlu::apply_t
127 <
128 event_type_list,
129 detail::machine_ref_event_impl
130 >;
131
132 event_impl_type impl_;
133};
134
135template<class... Events>
136inline constexpr auto machine_ref_e_conf = machine_ref_conf{}
137 .events<Events...>()
138;
139
145template<class... Events>
146using machine_ref_e = machine_ref<machine_ref_e_conf<Events...>>;
147
148} //namespace
149
150#endif
machine_ref< machine_ref_e_conf< Events... > > machine_ref_e
A convenient alias for machine_ref that only takes a list of event types.
Definition machine_ref.hpp:146
The state machine implementation template.
Definition machine.hpp:78
The Maki library.
The configuration for maki::machine_ref
Definition machine_ref_conf.hpp:29
constexpr auto events() const
Sets the event types that can be passed to maki::machine_ref::process_event().
Definition machine_ref_conf.hpp:47