state_machine_cpp
context.h
Go to the documentation of this file.
1 //
2 // Created by henri on 10/11/21.
3 //
4 
5 #ifndef STATE_MACHINE_CPP_CONTEXT_H
6 #define STATE_MACHINE_CPP_CONTEXT_H
7 
8 #include <limits>
9 #include <memory>
10 #include "impl/__timer.h"
11 #include "impl/__pointer.h"
12 #include "states/state_instance.h"
13 #include "states/state_any.h"
14 #include "abstract_layer.h"
15 #include "merge.h"
16 
17 namespace state_machine_cpp {
18  class Context;
19  namespace Algorithm {
20  class Instance;
21  void run(const Algorithm::Instance& t_instance, Context& t_context);
22  }
23 }
24 
26  std::shared_ptr<AbstractAttributeTree> m_underlying_context;
27 
28  State::Instance m_state;
29  double m_time_limit_in_seconds = std::numeric_limits<double>::max();
30  Timer m_timer;
31 
32  friend void ::state_machine_cpp::Algorithm::run(const Algorithm::Instance& t_instance, Context& t_context);
33  void set_state(const State::Any& t_state) { m_state = t_state.as_instance(0); }
34 
35  void start_timer() { m_timer.start(); }
36  void stop_timer() { m_timer.stop(); }
37 public:
38 
39  explicit Context(AbstractAttributeTree* t_underlying_context) : m_underlying_context(t_underlying_context) {}
40 
41  [[nodiscard]] unsigned int layer() const { return m_state.layer(); }
42  [[nodiscard]] const State::Instance& state() const { return m_state; }
43  [[nodiscard]] double execution_time_in_seconds() const { return m_timer.time_in_seconds(); }
44  [[nodiscard]] double remaining_time_in_seconds() const { return m_time_limit_in_seconds - m_timer.time_in_seconds(); }
45  [[nodiscard]] bool has_reached_time_limit() const { return remaining_time_in_seconds() <= 0; }
46  void set_time_limit_in_seconds(double t_time_limit) { m_time_limit_in_seconds = t_time_limit; }
47 
48  template<class T> T& get(int t_layer = -1) {
49  const unsigned int index = t_layer == -1 ? layer() : t_layer;
50  auto& layer = m_underlying_context->layer(index);
51  return *dynamic_cast<std::shared_ptr<T>&>(layer).get();
52  }
53 
54  template<class T> const T& get(int t_layer = -1) const {
55  return const_cast<Context&>(*this).get<T>(t_layer);
56  }
57 
58  template<class ...X, class ...Y>
60  auto* result = new merge_t<AttributeTree<X...>, AttributeTree<Y...>>();
61  Impl::merge<std::tuple<X...>, AttributeTree<X...>>::to(&t_x, *result);
62  Impl::merge<std::tuple<Y...>, AttributeTree<Y...>>::to(&t_y, *result);
63  return result;
64  }
65 
66  template<class T> T& get_relative(int t_offset) { return get<T>(layer() + t_offset); }
67  template<class T> const T& get_relative(int t_offset) const { return get<T>(layer() + t_offset); }
68 
69  template<class T, class ...X> static Context create(X... t_args) {
70  return Context(T::create_attributes(t_args...));
71  }
72 
73 };
74 
75 #endif //STATE_MACHINE_CPP_CONTEXT_H
state_machine_cpp::Context::has_reached_time_limit
bool has_reached_time_limit() const
Definition: context.h:45
state_machine_cpp::Context::remaining_time_in_seconds
double remaining_time_in_seconds() const
Definition: context.h:44
state_machine_cpp::Context::create
static Context create(X... t_args)
Definition: context.h:69
state_machine_cpp::Context::state
const State::Instance & state() const
Definition: context.h:42
state_machine_cpp::State::Any
Definition: state_any.h:19
state_instance.h
state_machine_cpp::Algorithm::run
void run(const Algorithm::Instance &t_instance, Context &t_context)
state_machine_cpp::AbstractAttributeTree
Definition: abstract_attribute_tree.h:17
state_machine_cpp::Context::Context
Context(AbstractAttributeTree *t_underlying_context)
Definition: context.h:39
state_any.h
state_machine_cpp::Algorithm::Instance
Definition: algorithm.h:23
state_machine_cpp::State::Instance
Definition: state_instance.h:23
__timer.h
state_machine_cpp::Context::layer
unsigned int layer() const
Definition: context.h:41
state_machine_cpp::Context::get_relative
const T & get_relative(int t_offset) const
Definition: context.h:67
state_machine_cpp::Timer::start
void start()
state_machine_cpp
Definition: algorithm.h:14
state_machine_cpp::Context::execution_time_in_seconds
double execution_time_in_seconds() const
Definition: context.h:43
state_machine_cpp::Impl::merge
Definition: abstract_attribute_tree.h:13
state_machine_cpp::merge_t
typename Impl::merge_t< AttributeTree<>, T... >::type merge_t
Definition: merge.h:127
abstract_layer.h
state_machine_cpp::Timer::time_in_seconds
double time_in_seconds() const
state_machine_cpp::AttributeTree
Definition: attribute_tree.h:15
state_machine_cpp::State::Instance::layer
unsigned int layer() const
Definition: state_instance.h:52
state_machine_cpp::Context::get_relative
T & get_relative(int t_offset)
Definition: context.h:66
__pointer.h
state_machine_cpp::Timer
Definition: __timer.h:14
state_machine_cpp::Context::get
const T & get(int t_layer=-1) const
Definition: context.h:54
state_machine_cpp::Context::get
T & get(int t_layer=-1)
Definition: context.h:48
state_machine_cpp::Timer::stop
void stop()
merge.h
state_machine_cpp::Context
Definition: context.h:25
state_machine_cpp::State::Any::as_instance
State::Instance as_instance(unsigned int t_layer_if_not_set) const
state_machine_cpp::Context::join
static merge_t< AttributeTree< X... >, AttributeTree< Y... > > * join(AttributeTree< X... > &&t_x, AttributeTree< Y... > &&t_y)
Definition: context.h:59
state_machine_cpp::Context::set_time_limit_in_seconds
void set_time_limit_in_seconds(double t_time_limit)
Definition: context.h:46