Analysis Software
Documentation for sPHENIX simulation 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 
12 
13 #include <optional>
14 #include <string_view>
15 #include <variant>
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(typename std::tuple_element<0, std::tuple<States...>>::type{}) {
125  }
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  (void)state;
163  return is<S>();
164  }
165 
170  template <typename S>
171  bool is() const noexcept {
172  if (std::get_if<S>(&m_state)) {
173  return true;
174  }
175  return false;
176  }
177 
180  bool terminated() const noexcept { return is<Terminated>(); }
181 
182  protected:
191  template <typename Event, typename... Args>
193  Derived& child = static_cast<Derived&>(*this);
194 
195  child.on_process(event);
196 
197  auto new_state = std::visit(
198  [&](auto& s) -> std::optional<StateVariant> {
199  auto s2 = child.on_event(s, std::forward<Event>(event),
200  std::forward<Args>(args)...);
201 
202  if (s2) {
203  std::visit([&](auto& s2_) { child.on_process(s, event, s2_); },
204  *s2);
205  } else {
206  child.on_process(s, event);
207  }
208  return s2;
209  },
210  m_state);
211  return new_state;
212  }
213 
214  public:
221  template <typename Event, typename... Args>
222  void dispatch(Event&& event, Args&&... args) {
223  auto new_state = process_event(std::forward<Event>(event), args...);
224  if (new_state) {
225  std::visit(
226  [&](auto& s) { setState(std::move(s), std::forward<Args>(args)...); },
227  *new_state);
228  }
229  }
230 
231  private:
232  StateVariant m_state;
233 };
234 
235 } // namespace Acts