StateMachineUtils.hpp
1 #pragma once
2 #include <score/model/path/Path.hpp>
3 
4 #include <QAbstractTransition>
5 #include <QEvent>
6 
13 namespace score
14 {
15 
16 template <int N>
17 struct NumberedEvent : public QEvent
18 {
19  static constexpr const int user_type = N;
21  : QEvent{QEvent::Type(QEvent::User + N)}
22  {
23  }
24 };
25 
26 template <typename Element, int N>
27 struct NumberedWithPath_Event final : public NumberedEvent<N>
28 {
29  explicit NumberedWithPath_Event(const Path<Element>& p)
31  , path(p)
32  {
33  }
34 
37  , path(std::move(p))
38  {
39  }
40 
41  Path<Element> path;
42 };
43 
44 template <typename PointType>
45 struct PositionedEvent : public QEvent
46 {
47  PositionedEvent(const PointType& pt, QEvent::Type type)
48  : QEvent{type}
49  , point{pt}
50  {
51  }
52 
53  ~PositionedEvent() override = default;
54 
55  PointType point;
56 };
57 
58 template <typename Event>
59 class MatchedTransition : public QAbstractTransition
60 {
61 public:
62  using event_type = Event;
63  using QAbstractTransition::QAbstractTransition;
64 
65 protected:
66  bool eventTest(QEvent* e) override
67  {
68  return e->type() == QEvent::Type(QEvent::User + Event::user_type);
69  }
70 
71  void onTransition(QEvent* event) override { }
72 };
73 
74 template <typename State, typename T>
75 class StateAwareTransition : public T
76 {
77 public:
78  explicit StateAwareTransition(State& state)
79  : m_state{state}
80  {
81  }
82 
83  State& state() const { return m_state; }
84 
85 private:
86  State& m_state;
87 };
88 
89 template <
90  typename Transition, typename SourceState, typename TargetState, typename... Args>
91 Transition* make_transition(SourceState source, TargetState dest, Args&&... args)
92 {
93  Transition* t = new Transition{std::forward<Args>(args)...};
94  t->setTargetState(dest);
95  source->addTransition(t);
96  return t;
97 }
98 
99 namespace Modifier
100 {
101 struct Click_tag
102 {
103  static constexpr const int value = 100;
104 };
105 struct Move_tag
106 {
107  static constexpr const int value = 200;
108 };
110 {
111  static constexpr const int value = 300;
112 };
113 }
114 enum Modifier_tagme
115 {
116  Click = 100,
117  Move = 200,
118  Release = 300
119 };
120 
121 using Press_Event = NumberedEvent<1>;
122 using Move_Event = NumberedEvent<2>;
123 using Release_Event = NumberedEvent<3>;
124 using Cancel_Event = NumberedEvent<4>;
125 using Shift_Event = NumberedEvent<5>;
126 
127 using Press_Transition = MatchedTransition<Press_Event>;
128 using Move_Transition = MatchedTransition<Move_Event>;
129 using Release_Transition = MatchedTransition<Release_Event>;
130 using Cancel_Transition = MatchedTransition<Cancel_Event>;
131 using ShiftTransition = MatchedTransition<Shift_Event>;
132 }
Definition: StateMachineUtils.hpp:60
Definition: StateMachineUtils.hpp:76
Utilities for OSSIA data structures.
Definition: DeviceInterface.hpp:33
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Definition: StateMachineUtils.hpp:102
Definition: StateMachineUtils.hpp:106
Definition: StateMachineUtils.hpp:110
Definition: StateMachineUtils.hpp:18
Definition: StateMachineUtils.hpp:28
Definition: StateMachineUtils.hpp:46