fsm
state_container.h
Go to the documentation of this file.
1 
12 #pragma once
13 
14 #include <variant>
15 
16 #include "../../base/type_traits.h"
17 
18 #include "../access_context.h"
19 #include "traits.h"
20 
21 namespace escad::detail
22 {
23 
29  template <class States>
31  {
32  private:
33  using type_list = typename States::type_list;
34 
35  public:
41  template <class State>
42  void enter()
43  {
44  states_.template emplace<State>();
45  }
46 
53  template <class State, class Context>
54  void enter(Context &ctx)
55  {
56  states_.template emplace<State>(ctx);
57  }
58 
62  void exit()
63  {
64  states_.template emplace<std::monostate>();
65  }
66 
67  template <class F>
68  auto visit(F &&fun)
69  {
70  std::visit(std::forward<F>(fun), states_);
71  }
72 
73  template <class State>
74  auto is_in() const
75  {
76  return std::holds_alternative<State>(states_);
77  }
78 
79  template <class State>
80  auto &state()
81  {
82  return std::get<State>(states_);
83  }
84 
85  private:
86  // Prepend a list of states with std::monostate. We want to avoid a situation
87  // that State will be constructed by defaulted when created instance of this class.
88  // 1. maybe we want to defer creation of State object
89  // 2. first state does not necessarily have default constructor
90  using states_variant_list = typename mpl::type_list_push_front<type_list, std::monostate>::result;
91 
92  // transform a type list to a corresponding variant
94 
95  states_variant states_;
96  };
97 
98 } // namespace fsm::detail
void enter()
Enter &#39;State&#39; state.
Definition: state_container.h:42
Definition: testJsonContexts.cpp:18
Append a type at the begining of the type_list.
Definition: type_traits.h:405
Definition: handle_result.h:14
Denotes a state.
Definition: states.h:61
void enter(Context &ctx)
Enter &#39;State&#39; state.
Definition: state_container.h:54
reanme typelist
Definition: type_traits.h:393
void exit()
Exit current state.
Definition: state_container.h:62
Wrapper around std::variant, which is current implementation of states container. ...
Definition: state_container.h:30