Maki
Loading...
Searching...
No Matches
guard.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_GUARD_HPP
13#define MAKI_GUARD_HPP
14
15#include "detail/signature_macros.hpp"
16#include "detail/call.hpp"
17#include "null.hpp"
18
19namespace maki
20{
21
25enum class guard_signature: char
26{
29
32
35
38
41
44
47
50};
51
52namespace detail
53{
54 template
55 <
56 class Guard,
57 class Context,
58 class Machine,
59 class Event
60 >
61 bool call_guard
62 (
63 const Guard& grd,
64 const Context& ctx,
65 const Machine& mach,
66 const Event& event
67 )
68 {
69 return call_callable<guard_signature, Guard::signature>
70 (
71 grd.callable,
72 ctx,
73 mach,
74 event
75 );
76 }
77}
78
84template<guard_signature Sig, class Callable>
85struct guard
86{
87 static constexpr guard_signature signature = Sig;
88 Callable callable;
89};
90
91#define MAKI_DETAIL_X(name) /*NOLINT(cppcoreguidelines-macro-usage)*/ \
92 \
97 template<class Callable> \
98 constexpr guard<guard_signature::name, Callable> guard_##name(const Callable& callable) \
99 { \
100 return {callable}; \
101 }
102MAKI_DETAIL_GUARD_SIGNATURES
103#undef MAKI_DETAIL_X
104
110template
112 guard_signature LhsSignature, class LhsCallable,
113 guard_signature RhsSignature, class RhsCallable
114>
115constexpr auto operator&&
116(
119)
120{
121 return guard_cme
122 (
123 [lhs, rhs](const auto& ctx, const auto& mach, const auto& event)
124 {
125 return
126 detail::call_guard(lhs, ctx, mach, event) &&
127 detail::call_guard(rhs, ctx, mach, event)
128 ;
129 }
130 );
131}
132
138template
140 guard_signature LhsSignature, class LhsCallable,
141 guard_signature RhsSignature, class RhsCallable
142>
143constexpr auto operator||
144(
147)
148{
149 return guard_cme
150 (
151 [lhs, rhs](const auto& ctx, const auto& mach, const auto& event)
152 {
153 return
154 detail::call_guard(lhs, ctx, mach, event) ||
155 detail::call_guard(rhs, ctx, mach, event)
156 ;
157 }
158 );
159}
160
166template
168 guard_signature LhsSignature, class LhsCallable,
169 guard_signature RhsSignature, class RhsCallable
170>
171constexpr auto operator!=
172(
175)
176{
177 return guard_cme
178 (
179 [lhs, rhs](const auto& ctx, const auto& mach, const auto& event)
180 {
181 return
182 detail::call_guard(lhs, ctx, mach, event) !=
183 detail::call_guard(rhs, ctx, mach, event)
184 ;
185 }
186 );
187}
188
190@relates guard
191@brief Makes a `maki::guard` that returns `true` if `grd` returns `false`.
192*/
193template<guard_signature Signature, class Callable>
194constexpr auto operator!(const guard<Signature, Callable>& grd)
195{
196 return guard_cme
197 (
198 [grd](const auto& ctx, const auto& mach, const auto& event)
199 {
200 return !detail::call_guard(grd, ctx, mach, event);
201 }
202 );
203}
204
205namespace detail
206{
207 inline constexpr auto null_guard = guard_v([]{return true;});
208
209 template<guard_signature Sig, class Callable>
210 constexpr const auto& to_guard(const guard<Sig, Callable>& grd)
211 {
212 return grd;
213 }
214
215 constexpr const auto& to_guard(null_t /*ignored*/)
216 {
217 return null_guard;
218 }
219
220 template<class T>
221 struct is_guard
222 {
223 static constexpr auto value = false;
224 };
225
226 template<guard_signature Sig, class Callable>
227 struct is_guard<guard<Sig, Callable>>
228 {
229 static constexpr auto value = true;
230 };
231
232 template<class T>
233 constexpr bool is_guard_v = is_guard<T>::value;
234}
235
236} //namespace
237
238#endif
constexpr auto operator!(const event_set< Predicate > &evt_set)
Creates a maki::event_set that contains all the event types that are not contained in evt_set.
Definition event_set.hpp:117
The Maki library.
IMPLEMENTATION_DETAIL null_t
The type of maki::null
Definition null.hpp:26
guard_signature
The set of arguments taken by a guard callable.
Definition guard.hpp:26
@ c
void action(context&)
Definition action.hpp:31
@ m
void action(machine&)
Definition action.hpp:43
@ cm
void action(context&, machine&)
Definition action.hpp:34
@ cme
void action(context&, machine&, const event&)
Definition action.hpp:37
@ ce
void action(context&, const event&)
Definition action.hpp:40
@ v
void action()
Definition action.hpp:28
@ me
void action(machine&, const event&)
Definition action.hpp:46
@ e
void action(const event&)
Definition action.hpp:49
Represents a guard to be given to maki::transition_table. Use the builder functions (maki::guard_v() ...
Definition guard.hpp:86
constexpr guard< guard_signature::v, Callable > guard_v(const Callable &callable)
Makes a maki::guard with the indicated signature and given callable.
Definition guard.hpp:98
constexpr guard< guard_signature::cme, Callable > guard_cme(const Callable &callable)
Makes a maki::guard with the indicated signature and given callable.
Definition guard.hpp:98