Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
port_connections_manager.h
1// SPDX-FileCopyrightText: © 2021, 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "dsp/port_connection.h"
7#include "utils/icloneable.h"
8
9namespace zrythm::dsp
10{
11
15class PortConnectionsManager : public QObject
16{
17 Q_OBJECT
18 QML_ELEMENT
19
20public:
21 using PortConnection = dsp::PortConnection;
22 using ConnectionsVector = std::vector<PortConnection *>;
23
24 explicit PortConnectionsManager (QObject * parent = nullptr);
25
26private:
33 using ConnectionHashTable = std::unordered_map<PortUuid, ConnectionsVector>;
34
35 static const auto &connection_ref_predicate (const PortConnection * conn)
36 {
37 return *conn;
38 }
39
40public:
47
60 int
61 get_sources_or_dests (ConnectionsVector * arr, const PortUuid &id, bool sources)
62 const;
63
64 int get_sources (ConnectionsVector * arr, const PortUuid &id) const
65 {
66 return get_sources_or_dests (arr, id, true);
67 }
68
69 int get_dests (ConnectionsVector * arr, const PortUuid &id) const
70 {
71 return get_sources_or_dests (arr, id, false);
72 }
73
74 int get_num_sources (const PortUuid &id) const
75 {
76 return get_sources (nullptr, id);
77 }
78 int get_num_dests (const PortUuid &id) const
79 {
80 return get_dests (nullptr, id);
81 }
82
83 auto get_connection_count () const noexcept { return connections_.size (); }
84
98 ConnectionsVector * arr,
99 const PortUuid &id,
100 bool sources) const;
101
110 PortConnection * get_source_or_dest (const PortUuid &id, bool sources) const;
111
119 PortConnection *
120 get_connection (const PortUuid &src, const PortUuid &dest) const;
121
122 bool connection_exists (const PortUuid &src, const PortUuid &dest) const
123 {
124 return get_connection (src, dest) != nullptr;
125 }
126
132 bool
133 update_connection (const PortConnection &before, const PortConnection &after);
134
135 bool update_connection (
136 const PortUuid &src,
137 const PortUuid &dest,
138 float multiplier,
139 bool locked,
140 bool enabled);
141
150 const PortConnection * add_connection (
151 const PortUuid &src,
152 const PortUuid &dest,
153 float multiplier,
154 bool locked,
155 bool enabled);
156
160 const PortConnection *
161 add_default_connection (const PortUuid &src, const PortUuid &dest, bool locked)
162 {
163 return add_connection (src, dest, 1.0f, locked, true);
164 }
165
166 const PortConnection * add_connection (const PortConnection &conn)
167 {
168 return add_connection (
169 conn.src_id_, conn.dest_id_, conn.multiplier_, conn.locked_,
170 conn.enabled_);
171 }
172
180 bool remove_connection (const PortUuid &src, const PortUuid &dest);
181
187 void remove_all_connections (const PortUuid &pi);
188
194 void reset_connections_from_other (const PortConnectionsManager * other);
195
196 bool contains_connection (const PortConnection &conn) const;
197
198 void print_ht (const ConnectionHashTable &ht);
199
200 void print () const;
201
202 friend void init_from (
203 PortConnectionsManager &obj,
204 const PortConnectionsManager &other,
205 utils::ObjectCloneType clone_type);
206
207 void clear_all ()
208 {
209 connections_.clear ();
211 }
212
213 auto &get_connections () const { return connections_; }
214
215private:
216 static constexpr auto kConnectionsKey = "connections"sv;
217 friend void to_json (nlohmann::json &j, const PortConnectionsManager &pcm)
218 {
219 j[kConnectionsKey] = pcm.connections_;
220 }
221 friend void from_json (const nlohmann::json &j, PortConnectionsManager &pcm)
222 {
223 for (const auto &conn_json : j.at (kConnectionsKey))
224 {
225 auto * conn = new PortConnection (&pcm);
226 from_json (conn_json, *conn);
227 pcm.connections_.push_back (conn);
228 }
229 pcm.regenerate_hashtables ();
230 }
231
232 void add_or_replace_connection (
233 ConnectionHashTable &ht,
234 const PortUuid &id,
235 const PortConnection &conn);
236
237 void remove_connection (size_t idx);
238
239private:
241 std::vector<PortConnection *> connections_;
242
249 ConnectionHashTable src_ht_;
250
257 ConnectionHashTable dest_ht_;
258};
259
260} // namespace zrythm::dsp
A connection between two ports.
bool enabled_
Whether the connection is enabled.
float multiplier_
Multiplier to apply, where applicable.
bool locked_
Whether the connection can be removed or the multiplier edited by the user.
PortConnection * get_connection(const PortUuid &src, const PortUuid &dest) const
void regenerate_hashtables()
Regenerates the hash tables.
void reset_connections_from_other(const PortConnectionsManager *other)
Removes all connections from this.
bool update_connection(const PortConnection &before, const PortConnection &after)
Replaces the given before connection with after.
int get_sources_or_dests(ConnectionsVector *arr, const PortUuid &id, bool sources) const
Adds the sources/destinations of id in the given array.
PortConnection * get_source_or_dest(const PortUuid &id, bool sources) const
Wrapper over get_sources_or_dests() that returns the first connection.
bool remove_connection(const PortUuid &src, const PortUuid &dest)
Removes the connection for the given ports if it exists.
const PortConnection * add_default_connection(const PortUuid &src, const PortUuid &dest, bool locked)
Overload for default settings (multiplier = 1.0, enabled = true).
int get_unlocked_sources_or_dests(ConnectionsVector *arr, const PortUuid &id, bool sources) const
Adds the sources/destinations of id in the given array.
void remove_all_connections(const PortUuid &pi)
Disconnect all sources and dests of the given port identifier.
const PortConnection * add_connection(const PortUuid &src, const PortUuid &dest, float multiplier, bool locked, bool enabled)
Stores the connection for the given ports if it doesn't exist, otherwise updates the existing connect...