ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FiniteStateMachine.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FiniteStateMachine.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
11 #include <optional>
12 #include <string_view>
13 #include <variant>
14 
16 
17 namespace Acts {
18 
101 template <typename Derived, typename... States>
103  public:
106  struct Terminated {
108  constexpr static std::string_view name = "Terminated";
109  };
110 
113  using StateVariant = std::variant<Terminated, States...>;
114 
115  protected:
116  using fsm_base = FiniteStateMachine<Derived, States...>;
117 
118  using event_return = std::optional<StateVariant>;
119 
120  public:
124  : m_state(
125  typename std::tuple_element<0, std::tuple<States...>>::type{}){};
126 
129  FiniteStateMachine(StateVariant state) : m_state(std::move(state)){};
130 
133  const StateVariant& getState() const noexcept { return m_state; }
134 
135  public:
142  template <typename State, typename... Args>
143  void setState(State state, Args&&... args) {
144  Derived& child = static_cast<Derived&>(*this);
145 
146  // call on exit function
147  std::visit([&](auto& s) { child.on_exit(s, std::forward<Args>(args)...); },
148  m_state);
149 
150  m_state = std::move(state);
151 
152  // call on enter function, the type is known from the template argument.
153  child.on_enter(std::get<State>(m_state), std::forward<Args>(args)...);
154  }
155 
160  template <typename S>
161  bool is(const S& /*state*/) const noexcept {
162  return is<S>();
163  }
164 
169  template <typename S>
170  bool is() const noexcept {
171  if (std::get_if<S>(&m_state)) {
172  return true;
173  }
174  return false;
175  }
176 
179  bool terminated() const noexcept { return is<Terminated>(); }
180 
181  protected:
190  template <typename Event, typename... Args>
191  event_return process_event(Event&& event, Args&&... args) {
192  Derived& child = static_cast<Derived&>(*this);
193 
194  child.on_process(event);
195 
196  auto new_state = std::visit(
197  [&](auto& s) -> std::optional<StateVariant> {
198  auto s2 = child.on_event(s, std::forward<Event>(event),
199  std::forward<Args>(args)...);
200 
201  if (s2) {
202  std::visit([&](auto& s2_) { child.on_process(s, event, s2_); },
203  *s2);
204  } else {
205  child.on_process(s, event);
206  }
207  return std::move(s2);
208  },
209  m_state);
210  return std::move(new_state);
211  }
212 
213  public:
220  template <typename Event, typename... Args>
221  void dispatch(Event&& event, Args&&... args) {
222  auto new_state = process_event(std::forward<Event>(event), args...);
223  if (new_state) {
224  std::visit(
225  [&](auto& s) { setState(std::move(s), std::forward<Args>(args)...); },
226  *new_state);
227  }
228  }
229 
230  private:
232 };
233 
234 } // namespace Acts