OSSIA
Open Scenario System for Interactive Application
time_sync.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <ossia/detail/config.hpp>
3 
4 #include <ossia/detail/flicks.hpp>
9 
10 #include <atomic>
11 #include <memory>
12 
16 namespace ossia
17 {
18 class expression_base;
19 class state;
20 class time_event;
21 class scenario;
22 struct OSSIA_EXPORT time_sync_callback
23 {
24  virtual ~time_sync_callback();
25 
26  virtual void triggered();
27 
29  virtual void entered_evaluation();
30 
32  virtual void entered_triggering();
33 
36  virtual void trigger_date_fixed(ossia::time_value);
37 
39  virtual void left_evaluation();
40 
42  virtual void finished_evaluation(bool);
43 };
44 
45 struct OSSIA_EXPORT time_sync_callbacks
46 {
47  ossia::small_vector<time_sync_callback*, 2> callbacks;
48 
49  void clear()
50  {
51  for(auto cb : callbacks)
52  delete cb;
53  callbacks.clear();
54  }
55 
56  void triggered()
57  {
58  for(auto v : callbacks)
59  v->triggered();
60  }
61 
62  void entered_evaluation()
63  {
64  for(auto v : callbacks)
65  v->entered_evaluation();
66  }
67 
68  void entered_triggering()
69  {
70  for(auto v : callbacks)
71  v->entered_triggering();
72  }
73 
74  void trigger_date_fixed(ossia::time_value t)
75  {
76  for(auto v : callbacks)
77  v->trigger_date_fixed(t);
78  }
79 
80  void left_evaluation()
81  {
82  for(auto v : callbacks)
83  v->left_evaluation();
84  }
85 
86  void finished_evaluation(bool b)
87  {
88  for(auto v : callbacks)
89  v->finished_evaluation(b);
90  }
91 };
92 
102 class OSSIA_EXPORT time_sync final
103 {
104  friend class ossia::scenario;
105 
106 public:
107  using container = ossia::small_vector<std::shared_ptr<time_event>, 2>;
108  using iterator = container::iterator;
109  using const_iterator = container::const_iterator;
110 
111  time_sync();
112  ~time_sync();
113 
119  [[nodiscard]] time_value get_date() const noexcept;
120 
122  [[nodiscard]] const expression& get_expression() const noexcept;
123 
131  time_sync& set_expression(expression_ptr) noexcept;
132 
139  iterator emplace(
140  const_iterator, time_event::exec_callback,
141  expression_ptr = expressions::make_expression_true());
142  iterator insert(const_iterator, std::shared_ptr<time_event>);
143  void remove(const std::shared_ptr<time_event>&);
144 
147  auto& get_time_events() noexcept { return m_timeEvents; }
148 
151  [[nodiscard]] const auto& get_time_events() const noexcept { return m_timeEvents; }
152 
153  // Interface to be used for set-up by other time processes
154  [[nodiscard]] bool is_observing_expression() const noexcept;
155  [[nodiscard]] bool is_evaluating() const noexcept;
156 
159  void start_trigger_request() noexcept;
160  void end_trigger_request() noexcept;
161 
169  [[nodiscard]] bool is_autotrigger() const noexcept;
170  void set_autotrigger(bool) noexcept;
171 
172  [[nodiscard]] bool is_start() const noexcept;
173  void set_start(bool) noexcept;
174 
176  void observe_expression(bool);
177  void observe_expression(bool, ossia::expressions::expression_result_callback cb);
178 
180  void reset();
181 
182  /* To be called before deletion, to break the shared_ptr cycle */
183  void cleanup();
184 
185  void mute(bool b);
186  [[nodiscard]] bool muted() const noexcept { return m_muted; }
187 
192  time_sync_callbacks callbacks;
193 
194  enum class status : uint8_t
195  {
196  NOT_DONE,
197  DONE_TRIGGERED,
198  DONE_MAX_REACHED
199  };
200  [[nodiscard]] status get_status() const noexcept { return m_status; }
201 
202  void set_sync_rate(double syncRatio) noexcept { m_sync_rate = syncRatio; }
203  [[nodiscard]] double get_sync_rate() const noexcept { return m_sync_rate; }
204  [[nodiscard]] bool has_sync_rate() const noexcept { return m_sync_rate > 0; }
205 
206  void set_trigger_date(time_value v) noexcept
207  {
208  m_trigger_date = v;
209  callbacks.trigger_date_fixed(v);
210  }
211  [[nodiscard]] time_value get_trigger_date() const noexcept { return m_trigger_date; }
212  [[nodiscard]] bool has_trigger_date() const noexcept
213  {
214  return !m_trigger_date.infinite();
215  }
216 
217  void set_is_being_triggered(bool v) noexcept;
218  [[nodiscard]] bool is_being_triggered() const noexcept { return m_is_being_triggered; }
219 
220 private:
221  ossia::expression_ptr m_expression;
222  container m_timeEvents;
223 
224  std::optional<expressions::expression_callback_iterator> m_callback;
225 
226  double m_sync_rate = 0.;
227 
228  std::atomic_bool trigger_request{};
229  time_value m_trigger_date = Infinite;
230  status m_status : 2;
231  bool m_start : 1;
232  bool m_observe : 1;
233  bool m_evaluating : 1;
234  bool m_muted : 1;
235  bool m_autotrigger : 1;
236  bool m_is_being_triggered : 1;
237 };
238 
239 }
std::function< void(status)> exec_callback
to get the event status back
Definition: time_event.hpp:63
#time_sync is use to describe temporal structure to synchronize each attached #time_event evaluation.
Definition: time_sync.hpp:103
auto & get_time_events() noexcept
get the #time_events of the #time_sync
Definition: time_sync.hpp:147
const auto & get_time_events() const noexcept
get the #time_events of the #time_sync
Definition: time_sync.hpp:151
time_sync_callbacks callbacks
Execution callbacks.
Definition: time_sync.hpp:192
Definition: git_info.h:7
expressions::expression_base expression
Definition: expression_fwd.hpp:222
The time_value class.
Definition: ossia/editor/scenario/time_value.hpp:28
constexpr bool infinite() const noexcept
is the time value infinite ?
Definition: ossia/editor/scenario/time_value.hpp:256