OSSIA
Open Scenario System for Interactive Application
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
graph_edge_helpers.hpp
1#pragma once
2#include <ossia/dataflow/graph/graph_interface.hpp>
3#include <ossia/dataflow/graph_edge.hpp>
4#include <ossia/dataflow/graph_node.hpp>
5#include <ossia/dataflow/port.hpp>
6
7namespace ossia
8{
9
10inline auto make_strict_edge(
11 graph_interface& g, int pout, int pin, ossia::node_ptr nout, ossia::node_ptr nin)
12{
13 return g.allocate_edge(
14 ossia::immediate_strict_connection{}, nout->root_outputs()[pout],
15 nin->root_inputs()[pin], nout, nin);
16}
17
18inline auto make_glutton_edge(
19 graph_interface& g, int pout, int pin, ossia::node_ptr nout, ossia::node_ptr nin)
20{
21 return g.allocate_edge(
22 ossia::immediate_glutton_connection{}, nout->root_outputs()[pout],
23 nin->root_inputs()[pin], nout, nin);
24}
25
26inline auto make_delayed_strict_edge(
27 graph_interface& g, int pout, int pin, ossia::node_ptr nout, ossia::node_ptr nin)
28{
29 return g.allocate_edge(
30 ossia::delayed_strict_connection{}, nout->root_outputs()[pout],
31 nin->root_inputs()[pin], nout, nin);
32}
33
34inline auto make_delayed_glutton_edge(
35 graph_interface& g, int pout, int pin, ossia::node_ptr nout, ossia::node_ptr nin)
36{
37 return g.allocate_edge(
38 ossia::delayed_glutton_connection{}, nout->root_outputs()[pout],
39 nin->root_inputs()[pin], nout, nin);
40}
41
46{
47 std::shared_ptr<ossia::graph_node> node;
48 std::shared_ptr<ossia::graph_interface> graph;
49 ossia::inlets inls;
50 ossia::outlets outls;
51
52 void clear_cables(ossia::inlet& port)
53 {
54 // FIXME this allocates
55 auto cbl = port.cables();
56 for(ossia::graph_edge* cable : cbl)
57 {
58 graph->disconnect(cable);
59 cable->clear();
60 }
61 port.cables().clear();
62 }
63 void clear_cables(ossia::outlet& port)
64 {
65 auto cbl = port.cables();
66 for(ossia::graph_edge* cable : cbl)
67 {
68 graph->disconnect(cable);
69 cable->clear();
70 }
71 port.cables().clear();
72 }
73
74 template <typename T>
75 void copy_child_inlets(T* old_port, T* new_port)
76 {
77 for(std::size_t child_i = 0; child_i < old_port->child_inlets.size(); child_i++)
78 {
79 auto old_cld = old_port->child_inlets[child_i];
80 if(child_i < new_port->child_inlets.size())
81 {
82 auto new_cld = new_port->child_inlets[child_i];
83
84 copy_port(old_cld, new_cld);
85 }
86 else
87 {
88 clear_cables(*old_cld);
89 }
90 }
91 }
92
93 void copy_port(ossia::inlet* old_in, ossia::inlet* new_in)
94 {
95 copy_child_inlets(old_in, new_in);
96
97 if(old_in->which() == new_in->which())
98 {
99 new_in->address = old_in->address;
100 for(auto& cable : old_in->cables())
101 {
102 cable->in = new_in;
103 }
104 new_in->cables() = std::move(old_in->cables());
105 }
106 else
107 {
108 clear_cables(*old_in);
109 }
110 }
111
112 void copy_port(ossia::outlet* old_out, ossia::outlet* new_out)
113 {
114 copy_child_inlets(old_out, new_out);
115
116 if(old_out->which() == new_out->which())
117 {
118 new_out->address = old_out->address;
119 for(auto& cable : old_out->cables())
120 {
121 cable->out = new_out;
122 }
123 new_out->cables() = std::move(old_out->cables());
124 }
125 else
126 {
127 clear_cables(*old_out);
128 }
129 }
130
131 void operator()()
132 {
133 auto& old_inputs = node->root_inputs();
134 auto& old_outputs = node->root_outputs();
135 {
136 std::size_t k = 0;
137 for(auto& old_in : old_inputs)
138 {
139 if(k < inls.size())
140 {
141 auto& new_in = inls[k];
142 copy_port(old_in, new_in);
143 }
144 else
145 {
146 clear_cables(*old_in);
147 }
148
149 delete old_in;
150 k++;
151 }
152 old_inputs.clear();
153 }
154
155 {
156 std::size_t k = 0;
157 for(auto& old_out : old_outputs)
158 {
159 if(k < outls.size())
160 {
161 auto& new_out = outls[k];
162 copy_port(old_out, new_out);
163 }
164 else
165 {
166 clear_cables(*old_out);
167 }
168
169 delete old_out;
170 k++;
171 }
172
173 old_outputs.clear();
174 }
175
176 using namespace std;
177 swap(node->root_inputs(), inls);
178 swap(node->root_outputs(), outls);
179 }
180};
181
182}
Definition git_info.h:7
Definition graph_edge_helpers.hpp:46