OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
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
16namespace ossia
17{
18class expression_base;
19class state;
20class time_event;
21class scenario;
22struct 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
45struct 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
102class OSSIA_EXPORT time_sync final
103{
104 friend class ossia::scenario;
105
106public:
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 inline void observe_expression(bool b, auto&& cb)
178 {
179 if(!m_expression || m_expression->target<ossia::expressions::expression_bool>())
180 return;
181 return observe_expression_impl(b, std::move(cb));
182 }
183 void observe_expression_impl(bool, ossia::expressions::expression_result_callback cb);
184
186 void reset();
187
188 /* To be called before deletion, to break the shared_ptr cycle */
189 void cleanup();
190
191 void mute(bool b);
192 [[nodiscard]] bool muted() const noexcept { return m_muted; }
193
198 time_sync_callbacks callbacks;
199
200 enum class status : uint8_t
201 {
202 NOT_DONE,
203 DONE_TRIGGERED,
204 DONE_MAX_REACHED
205 };
206 [[nodiscard]] status get_status() const noexcept { return m_status; }
207
208 void set_sync_rate(double syncRatio) noexcept { m_sync_rate = syncRatio; }
209 [[nodiscard]] double get_sync_rate() const noexcept { return m_sync_rate; }
210 [[nodiscard]] bool has_sync_rate() const noexcept { return m_sync_rate > 0; }
211
212 void set_trigger_date(time_value v) noexcept
213 {
214 m_trigger_date = v;
215 callbacks.trigger_date_fixed(v);
216 }
217 [[nodiscard]] time_value get_trigger_date() const noexcept { return m_trigger_date; }
218 [[nodiscard]] bool has_trigger_date() const noexcept
219 {
220 return !m_trigger_date.infinite();
221 }
222
223 void set_is_being_triggered(bool v) noexcept;
224 [[nodiscard]] bool is_being_triggered() const noexcept { return m_is_being_triggered; }
225
226private:
227 ossia::expression_ptr m_expression;
228 container m_timeEvents;
229
230 std::optional<expressions::expression_callback_iterator> m_callback;
231
232 double m_sync_rate = 0.;
233
234 std::atomic_bool trigger_request{};
235 time_value m_trigger_date = Infinite;
236 status m_status : 2;
237 bool m_start : 1;
238 bool m_observe : 1;
239 bool m_evaluating : 1;
240 bool m_muted : 1;
241 bool m_autotrigger : 1;
242 bool m_is_being_triggered : 1;
243};
244
245}
expression_bool : a constant boolean value.
Definition expression_bool.hpp:16
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
time_sync_callbacks callbacks
Execution callbacks.
Definition time_sync.hpp:198
const auto & get_time_events() const noexcept
get the #time_events of the time_sync
Definition time_sync.hpp:151
auto & get_time_events() noexcept
get the #time_events of the time_sync
Definition time_sync.hpp:147
smallfun::function< void(bool), smallfun::DefaultSize *2 > expression_result_callback
Definition expression_fwd.hpp:25
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:30