state_machine_cpp
build.h
Go to the documentation of this file.
1 //
2 // Created by henri on 26/04/21.
3 //
4 
5 #ifndef STATE_MACHINE_CPP_BUILD_H
6 #define STATE_MACHINE_CPP_BUILD_H
7 
11 #include "builder/builder_types.h"
12 
14  namespace Impl {
15  template<typename T> struct has_INITIAL_STATE;
16  template<typename T> struct has_FINAL_STATE;
17  template<typename T> struct has_TIME_LIMIT_REACHED_STATE;
18  }
19  class Instance;
20 }
21 
22 
23 template <typename T>
25  template <class, class> class checker;
26  template <typename C> static std::true_type test(checker<C, decltype(&C::INITIAL_STATE)> *) { return {}; }
27  template <typename C> static std::false_type test(...) { return {}; }
28  typedef decltype(test<T>(nullptr)) type;
29  static constexpr bool value = std::is_same<std::true_type, decltype(test<T>(nullptr))>::value;
30 };
31 
32 template <typename T>
33 struct state_machine_cpp::Algorithm::Impl::has_FINAL_STATE {
34  template <class, class> class checker;
35  template <typename C> static std::true_type test(checker<C, decltype(&C::FINAL_STATE)> *) { return {}; }
36  template <typename C> static std::false_type test(...) { return {}; }
37  typedef decltype(test<T>(nullptr)) type;
38  static constexpr bool value = std::is_same<std::true_type, decltype(test<T>(nullptr))>::value;
39 };
40 
41 template <typename T>
42 struct state_machine_cpp::Algorithm::Impl::has_TIME_LIMIT_REACHED_STATE {
43  template <class, class> class checker;
44  template <typename C> static std::true_type test(checker<C, decltype(&C::TIME_LIMIT_REACHED)> *) { return {}; }
45  template <typename C> static std::false_type test(...) { return {}; }
46  typedef decltype(test<T>(nullptr)) type;
47  static constexpr bool value = std::is_same<std::true_type, decltype(test<T>(nullptr))>::value;
48 };
49 
50 template<class T>
51 void state_machine_cpp::Algorithm::build(Algorithm::Instance& t_destination, Algorithm::Mode t_build_mode) {
52 
53  Impl::Build::Layers layers;
54  Impl::Build::States states(t_destination, &layers, t_build_mode);
55  Impl::Build::Transitions transitions(t_destination, &layers, t_build_mode);
56 
57  Algorithm::Builder::import<T>(states, transitions, layers);
58 
59  if (layers.current() != 0) {
60  throw std::runtime_error("There were some opened layers which failed to be closed.");
61  }
62 
63  if constexpr(Impl::has_INITIAL_STATE<T>::value) {
64  if (!t_destination.transitions().has(T::INITIAL_STATE)) {
65  throw std::runtime_error("INITIAL_STATE have been auto-detected yet the state is not created within the algorithm."
66  "Call states.create.");
67  }
68  t_destination.set_initial_state(T::INITIAL_STATE);
69  }
70 
71  if constexpr(Impl::has_FINAL_STATE<T>::value) {
72  if (!t_destination.transitions().has(T::FINAL_STATE)) {
73  throw std::runtime_error("FINAL_STATE have been auto-detected yet the state is not created within the algorithm."
74  "Call states.create.");
75  }
76  t_destination.set_final_state(T::FINAL_STATE);
77  }
78 
80  if (!t_destination.transitions().has(T::TIME_LIMIT_REACHED)) {
81  throw std::runtime_error("TIME_LIMIT_REACHED have been auto-detected yet the state is not created within the algorithm."
82  "Call states.create.");
83  }
84  t_destination.set_time_limit_reached_state(T::TIME_LIMIT_REACHED);
85  }
86 
87 }
88 
89 #endif //STATE_MACHINE_CPP_BUILD_H
state_machine_cpp::Algorithm::Impl::Build::States
Definition: builder_impl_states.h:17
state_machine_cpp::Algorithm::Impl::has_TIME_LIMIT_REACHED_STATE::test
static std::false_type test(...)
Definition: build.h:45
state_machine_cpp::Algorithm::Impl::Build::Layers::current
unsigned int current() const override
state_machine_cpp::Algorithm::Mode
Mode
Definition: builder_types.h:9
state_machine_cpp::Algorithm::Impl::has_INITIAL_STATE::checker
Definition: build.h:25
state_machine_cpp::Algorithm::Impl::has_INITIAL_STATE
Definition: build.h:15
state_machine_cpp::Algorithm::Impl::has_INITIAL_STATE::type
decltype(test< T >(nullptr)) typedef type
Definition: build.h:28
state_machine_cpp::Algorithm::Impl::has_FINAL_STATE::test
static std::false_type test(...)
Definition: build.h:36
state_machine_cpp::Algorithm::Impl::has_INITIAL_STATE::test
static std::false_type test(...)
Definition: build.h:27
builder_types.h
state_machine_cpp::Algorithm::Instance
Definition: algorithm.h:23
state_machine_cpp::Algorithm::Impl::has_FINAL_STATE::checker
Definition: build.h:34
state_machine_cpp
Definition: algorithm.h:14
state_machine_cpp::Algorithm::Impl::has_INITIAL_STATE::test
static std::true_type test(checker< C, decltype(&C::INITIAL_STATE)> *)
Definition: build.h:26
builder_impl_layers.h
state_machine_cpp::Algorithm::Impl::Build::Transitions
Definition: builder_impl_transitions.h:21
builder_impl_states.h
state_machine_cpp::Algorithm::Impl::Build::Layers
Definition: builder_impl_layers.h:15
state_machine_cpp::Algorithm::Impl::has_FINAL_STATE::test
static std::true_type test(checker< C, decltype(&C::FINAL_STATE)> *)
Definition: build.h:35
state_machine_cpp::Algorithm::Impl::has_INITIAL_STATE::value
static constexpr bool value
Definition: build.h:29
state_machine_cpp::Algorithm::Impl::has_FINAL_STATE
Definition: build.h:16
state_machine_cpp::Algorithm::Impl::has_TIME_LIMIT_REACHED_STATE::checker
Definition: build.h:43
state_machine_cpp::Algorithm
Definition: algorithm.h:14
state_machine_cpp::Algorithm::Impl::has_TIME_LIMIT_REACHED_STATE
Definition: build.h:17
state_machine_cpp::Algorithm::build
void build(Algorithm::Instance &t_destination, Algorithm::Mode t_build_mode=Algorithm::Mode::Release)
Definition: build.h:51
builder_impl_transitions.h
state_machine_cpp::Algorithm::Impl::has_TIME_LIMIT_REACHED_STATE::test
static std::true_type test(checker< C, decltype(&C::TIME_LIMIT_REACHED)> *)
Definition: build.h:44