fsm
transition.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include "../base/type_traits.h"
14 
15 namespace escad {
16 
17 namespace new_fsm {
18 
19 namespace detail {
20 
24 struct none {};
25 
30 template <class T> struct sibling {
31  using type = T;
32 };
33 
34 template <class T> struct inner {
35  using type = T;
36 };
37 
38 template <class T> struct inner_entry {
39  using type = T;
40 };
41 
42 enum class result { none, sibling, inner, inner_entry };
43 
44 } // namespace detail
45 
50 template <class... S> class transitions {
51  using result = detail::result;
52 
53 public:
59  template <class... T>
60  constexpr transitions(transitions<T...> const &rhs) noexcept
61  : idx{0}, outcome{rhs.outcome} {}
62 
68  template <class T>
70  : idx{mpl::type_list_index_v<T, list>}, outcome{result::sibling} {}
71 
72  template <class T>
74  : idx{mpl::type_list_index_v<T, list>}, outcome{result::inner} {}
75 
76  template <class T>
78  : idx{mpl::type_list_index_v<T, list>}, outcome{result::inner_entry} {}
79 
85  template <class T>
86  transitions(transitions<T> const &rhs) noexcept
87  : idx{mpl::type_list_index_v<T, list>}, outcome{rhs.outcome} {}
88 
95  : idx{mpl::type_list_index_v<detail::none, list>}, outcome{result::none} {
96  }
97 
104  : idx{mpl::type_list_index_v<detail::none, list>}, outcome{result::none} {
105  }
106 
111  bool is_transition() const { return outcome == result::sibling; }
112 
113  bool is_sibling() const { return outcome == result::sibling; }
114 
115  bool is_inner() const { return outcome == result::inner; }
116 
117  bool is_inner_entry() const { return outcome == result::inner_entry; }
118 
123  bool is_none() const { return outcome == result::none; }
124 
125  using list = mpl::type_list<S...>;
126 
127  std::size_t const idx;
128  result const outcome;
129 };
130 
136 template <class S> inline auto sibling() {
138 }
139 
140 template <class S> inline auto inner() {
142 }
143 
144 template <class S> inline auto inner_entry() {
146 }
147 
153 inline auto none() { return transitions<detail::none>{detail::none{}}; }
154 
160 template <std::size_t Index, class Transition>
161 using transition_t = mpl::type_list_element_t<Index, typename Transition::list>;
162 
170 template <class Transition, class Func, std::size_t... Is>
171 constexpr void for_each_transition_impl(Transition &&t, Func &&func,
172  std::index_sequence<Is...>) {
173  (func(std::integral_constant<std::size_t, Is>{}, t), ...);
174 }
175 
184 template <class... T, class Func>
185 constexpr void for_each_transition(transitions<T...> const &trans,
186  Func &&func) {
187  for_each_transition_impl(trans, std::forward<Func>(func),
188  std::make_index_sequence<sizeof...(T)>{});
189 }
190 
191 } // namespace new_fsm
192 } // namespace escad
transitions(transitions< T > const &rhs) noexcept
Constructs a new transitions object from another transitions object.
Definition: transition.h:86
Tag type indicating no transition.
Definition: transition.h:24
transitions(detail::sibling< T >) noexcept
Constructs a new transitions object from a detail::transition type.
Definition: transition.h:69
Definition: transition.h:38
constexpr transitions(transitions< T... > const &rhs) noexcept
Constructs a new transitions object from another transitions object.
Definition: transition.h:60
Definition: transition.h:34
Class representing a collection of transitions.
Definition: transition.h:50
mpl::type_list_element_t< Index, typename Transition::list > transition_t
Type alias for the transition type at a specific index.
Definition: transition.h:161
auto sibling()
Helper function for creating a transition object.
Definition: transition.h:136
transitions(transitions< detail::none >) noexcept
Constructs a new transitions object indicating that the event was handled.
Definition: transition.h:103
Helper struct for defining a transition type.
Definition: transition.h:30
bool is_transition() const
Checks if this object represents a transition.
Definition: transition.h:111
bool is_none() const
Checks if the event was handled.
Definition: transition.h:123
constexpr void for_each_transition(transitions< T... > const &trans, Func &&func)
Helper function for iterating over each transition in a transitions object.
Definition: transition.h:185
transitions(detail::none) noexcept
Constructs a new transitions object with no transition.
Definition: transition.h:94
Definition: compressed_pair.h:10
Alias template to facilitate the creation of named values.
Definition: type_traits.h:142
constexpr void for_each_transition_impl(Transition &&t, Func &&func, std::index_sequence< Is... >)
Helper function for getting the transition type at a specific index.
Definition: transition.h:171
auto none()
Helper function for creating a transitions object indicating that the event was not handled...
Definition: transition.h:153