Process/Execution/ProcessComponent.hpp
1 #pragma once
2 #include <Process/ExecutionComponent.hpp>
3 #include <Process/ExecutionContext.hpp>
4 #include <Process/ExecutionTransaction.hpp>
5 #include <Process/Process.hpp>
6 #include <Process/ProcessComponent.hpp>
7 
8 #include <score/model/ComponentFactory.hpp>
9 #include <score/plugins/ModelFactory.hpp>
10 
11 #include <ossia/dataflow/graph_node.hpp>
12 #include <ossia/editor/scenario/time_process.hpp>
13 
14 #include <QObject>
15 
16 #include <score_lib_process_export.h>
17 
18 #include <memory>
19 #include <verdigris>
20 
21 #if __cpp_lib_concepts >= 202002L
22 #include <concepts>
23 #endif
24 
25 namespace ossia
26 {
27 class scenario;
28 class time_process;
29 }
30 
31 namespace Execution
32 {
33 struct Context;
34 struct Transaction;
35 template <typename T>
36 class InvalidProcessException : public std::runtime_error
37 {
38 public:
39  InvalidProcessException(const QString& s)
40  : std::runtime_error{(Metadata<PrettyName_k, T>::get() + ": " + s).toStdString()}
41  {
42  }
43 };
44 
45 class SCORE_LIB_PROCESS_EXPORT ProcessComponent
46  : public Process::GenericProcessComponent<const Context>
47  , public std::enable_shared_from_this<ProcessComponent>
48 {
49  W_OBJECT(ProcessComponent)
50  ABSTRACT_COMPONENT_METADATA(
51  Execution::ProcessComponent, "d0f714de-c832-42d8-a605-60f5ffd0b7af")
52 
53 public:
54  static constexpr bool is_unique = true;
55 
57  Process::ProcessModel& proc, const Context& ctx, const QString& name,
58  QObject* parent);
59 
61  virtual void lazy_init();
62 
63  virtual ~ProcessComponent();
64 
65  virtual void cleanup();
66  virtual void stop() { process().stopExecution(); }
67 
68  const std::shared_ptr<ossia::time_process>& OSSIAProcessPtr()
69  {
70  return m_ossia_process;
71  }
72  ossia::time_process& OSSIAProcess() const { return *m_ossia_process; }
73 
74  std::shared_ptr<ossia::graph_node> node;
75 
76 public:
77  void nodeChanged(
78  const ossia::node_ptr& old_node, const ossia::node_ptr& new_node,
79  Execution::Transaction* commands)
80  E_SIGNAL(SCORE_LIB_PROCESS_EXPORT, nodeChanged, old_node, new_node, commands)
81 
82 protected:
83  std::shared_ptr<ossia::time_process> m_ossia_process;
84 };
85 
86 template <typename Process_T, typename OSSIA_Process_T>
88  : public Process::GenericProcessComponent_T<ProcessComponent, Process_T>
89 {
91  ProcessComponent, Process_T>::GenericProcessComponent_T;
92 
93  OSSIA_Process_T& OSSIAProcess() const
94  {
95  return static_cast<OSSIA_Process_T&>(ProcessComponent::OSSIAProcess());
96  }
97 };
98 
99 class SCORE_LIB_PROCESS_EXPORT ProcessComponentFactory
101  Process::ProcessModel, Execution::Context, Execution::ProcessComponentFactory>
102 {
103  SCORE_ABSTRACT_COMPONENT_FACTORY(Execution::ProcessComponent)
104 public:
105  virtual ~ProcessComponentFactory() override;
106  virtual std::shared_ptr<ProcessComponent>
107  make(Process::ProcessModel& proc, const Context& ctx, QObject* parent) const = 0;
108 };
109 
110 template <typename ProcessComponent_T>
111 #if __cpp_lib_concepts >= 202002L
112 requires std::constructible_from<
113  ProcessComponent_T, typename ProcessComponent_T::model_type&, const Context&,
114  QObject*>
115 #endif
118  ProcessComponent_T, ProcessComponentFactory>
119 {
120 public:
121  using model_type = typename ProcessComponent_T::model_type;
122  std::shared_ptr<ProcessComponent> make(
123  Process::ProcessModel& proc, const Context& ctx,
124  QObject* parent) const final override
125  {
126  try
127  {
128 #if defined(SCORE_DEBUG)
129  // A bit slower but really makes backtraces simpler
130  auto comp = std::shared_ptr<ProcessComponent_T>(
131  new ProcessComponent_T{static_cast<model_type&>(proc), ctx, parent});
132 #else
133  auto comp = std::make_shared<ProcessComponent_T>(
134  static_cast<model_type&>(proc), ctx, parent);
135 #endif
136  comp->lazy_init();
137  return comp;
138  }
139  catch(const std::runtime_error& e)
140  {
141  qDebug() << "Error during plug-in creation: " << e.what();
142  return {};
143  }
144  catch(...)
145  {
146  return {};
147  }
148  }
149 };
150 
151 class SCORE_LIB_PROCESS_EXPORT ProcessComponentFactoryList final
153  Process::ProcessModel, Execution::Context, Execution::ProcessComponentFactory>
154 {
155 public:
157 };
158 }
159 
160 W_REGISTER_ARGTYPE(ossia::node_ptr)
161 Q_DECLARE_METATYPE(std::shared_ptr<Execution::ProcessComponent>)
162 W_REGISTER_ARGTYPE(std::shared_ptr<Execution::ProcessComponent>)
163 W_REGISTER_ARGTYPE(const std::shared_ptr<Execution::ProcessComponent>&)
Definition: Process/Execution/ProcessComponent.hpp:37
Definition: Process/Execution/ProcessComponent.hpp:119
Definition: Process/Execution/ProcessComponent.hpp:102
Definition: Process/Execution/ProcessComponent.hpp:154
Definition: Process/Execution/ProcessComponent.hpp:48
Definition: Process/ProcessComponent.hpp:34
Definition: Process/ProcessComponent.hpp:10
The Process class.
Definition: score-lib-process/Process/Process.hpp:61
Definition: ComponentFactory.hpp:29
Definition: ModelFactory.hpp:8
Definition: ComponentFactory.hpp:48
Components used for the execution of a score.
Definition: ProcessComponent.cpp:12
Definition: ExecutionContext.hpp:76
Definition: Process/Execution/ProcessComponent.hpp:89
Definition: ExecutionTransaction.hpp:18
Static metadata implementation.
Definition: lib/score/tools/Metadata.hpp:36