Loading...
Searching...
No Matches
ExecutionTransaction.hpp
1#pragma once
2#include <Process/ExecutionCommand.hpp>
3#include <Process/ExecutionContext.hpp>
4
5#include <ossia/detail/thread.hpp>
6
7#include <verdigris>
8
9namespace Execution
10{
11struct Context;
12template <typename... T>
13inline constexpr auto gc(T&&... t) noexcept
14{
15 return [... gced = std::move(t)] {
16 OSSIA_ENSURE_CURRENT_THREAD_KIND(ossia::thread_type::Ui);
17 };
18}
19
21{
22 const Context& context;
23 std::vector<ExecutionCommand> commands;
24 explicit Transaction(const Context& ctx) noexcept
25 : context{ctx}
26 {
27 }
28
29 Transaction(Transaction&& other) noexcept
30 : context{other.context}
31 , commands(std::move(other.commands))
32 {
33 }
34
35 Transaction& operator=(Transaction&& other) noexcept
36 {
37 commands = std::move(other.commands);
38 return *this;
39 }
40
42 {
43 if(commands.capacity() > 0)
44 {
45 OSSIA_ENSURE_CURRENT_THREAD_KIND(ossia::thread_type::Ui);
46 }
47 }
48
49 void reserve(std::size_t sz) noexcept { commands.reserve(sz); }
50 bool empty() const noexcept { return commands.empty(); }
51 template <typename T>
52 void push_back(T&& t) noexcept
53 {
54 commands.emplace_back(std::move(t));
55 }
56
57 void run_all()
58 {
59 OSSIA_ENSURE_CURRENT_THREAD_KIND(ossia::thread_type::Ui);
60 context.executionQueue.enqueue(
61 [t = std::move(*this)]() mutable { t.run_all_in_exec(); });
62 }
63
64 void run_all_in_ui()
65 {
66 // To be used only when we know for sure that all
67 // the data is still in the UI thread, e.g. during the constructor
68 // of a node
69 OSSIA_ENSURE_CURRENT_THREAD_KIND(ossia::thread_type::Ui);
70 for(auto& cmd : commands)
71 cmd();
72 }
73
74 void run_all_in_exec()
75 {
76 OSSIA_ENSURE_CURRENT_THREAD_KIND(ossia::thread_type::Audio);
77 for(auto& cmd : commands)
78 cmd();
79 }
80};
81
82}
83Q_DECLARE_METATYPE(Execution::Transaction*)
84W_REGISTER_ARGTYPE(Execution::Transaction*)
Components used for the execution of a score.
Definition ProcessComponent.cpp:14
Definition ExecutionContext.hpp:196
ExecutionCommandQueue & executionQueue
Definition ExecutionContext.hpp:242
Definition ExecutionTransaction.hpp:21