Cleanup
[carla.git] / source / utils / CarlaPatchbayUtils.hpp
blobc1b1dc906603ae1d55b0dfa1cfd667588d1d0739
1 /*
2 * Carla patchbay utils
3 * Copyright (C) 2011-2020 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #ifndef CARLA_PATCHBAY_UTILS_HPP_INCLUDED
19 #define CARLA_PATCHBAY_UTILS_HPP_INCLUDED
21 #include "CarlaMutex.hpp"
22 #include "LinkedList.hpp"
24 #define STR_MAX 0xFF
26 // -----------------------------------------------------------------------
28 struct GroupNameToId {
29 uint group;
30 char name[STR_MAX]; // globally unique
32 void clear() noexcept
34 group = 0;
35 name[0] = '\0';
38 void setData(const uint g, const char n[]) noexcept
40 group = g;
41 rename(n);
44 void rename(const char n[]) noexcept
46 std::strncpy(name, n, STR_MAX-1);
47 name[STR_MAX-1] = '\0';
50 bool operator==(const GroupNameToId& groupNameToId) const noexcept
52 if (groupNameToId.group != group)
53 return false;
54 if (std::strncmp(groupNameToId.name, name, STR_MAX-1) != 0)
55 return false;
56 return true;
59 bool operator!=(const GroupNameToId& groupNameToId) const noexcept
61 return !operator==(groupNameToId);
65 struct PatchbayGroupList {
66 uint lastId;
67 LinkedList<GroupNameToId> list;
68 CarlaMutex mutex;
70 PatchbayGroupList() noexcept
71 : lastId(0),
72 list(),
73 mutex() {}
75 void clear() noexcept
77 lastId = 0;
78 list.clear();
81 uint getGroupId(const char* const groupName) const noexcept;
83 // always returns valid pointer (non-null)
84 const char* getGroupName(const uint groupId) const noexcept;
87 // -----------------------------------------------------------------------
89 struct PortNameToId {
90 uint group;
91 uint port;
92 char name[STR_MAX]; // locally unique (within the same group)
93 char fullName[STR_MAX]; // globally unique
94 char identifier[STR_MAX]; // globally unique, if used
96 void clear() noexcept
98 group = 0;
99 port = 0;
100 name[0] = '\0';
101 fullName[0] = '\0';
102 identifier[0] = '\0';
105 void setData(const uint g, const uint p, const char n[], const char fn[]) noexcept
107 group = g;
108 port = p;
109 rename(n, fn);
112 void setDataWithIdentifier(const uint g, const uint p, const char n[], const char id[]) noexcept
114 group = g;
115 port = p;
117 std::strncpy(name, n, STR_MAX-1);
118 name[STR_MAX-1] = '\0';
120 fullName[0] = '\0';
122 std::strncpy(identifier, id, STR_MAX-1);
123 identifier[STR_MAX-1] = '\0';
126 void setFullName(const char fn[]) noexcept
128 std::strncpy(fullName, fn, STR_MAX-1);
129 fullName[STR_MAX-1] = '\0';
132 void rename(const char n[], const char fn[]) noexcept
134 std::strncpy(name, n, STR_MAX-1);
135 name[STR_MAX-1] = '\0';
137 std::strncpy(fullName, fn, STR_MAX-1);
138 fullName[STR_MAX-1] = '\0';
141 bool operator==(const PortNameToId& portNameToId) noexcept
143 if (portNameToId.group != group || portNameToId.port != port)
144 return false;
145 if (std::strncmp(portNameToId.name, name, STR_MAX-1) != 0)
146 return false;
147 if (std::strncmp(portNameToId.fullName, fullName, STR_MAX-1) != 0)
148 return false;
149 return true;
152 bool operator!=(const PortNameToId& portNameToId) noexcept
154 return !operator==(portNameToId);
158 struct PatchbayPortList {
159 uint lastId;
160 LinkedList<PortNameToId> list;
161 CarlaMutex mutex;
163 PatchbayPortList() noexcept
164 : lastId(0),
165 list(),
166 mutex() {}
168 void clear() noexcept
170 lastId = 0;
171 list.clear();
174 // always returns valid pointer (non-null)
175 const char* getFullPortName(const uint groupId, const uint portId) const noexcept;
177 const PortNameToId& getPortNameToId(const char* const fullPortName) const noexcept;
180 // -----------------------------------------------------------------------
182 struct ConnectionToId {
183 uint id;
184 uint groupA, portA;
185 uint groupB, portB;
187 void clear() noexcept
189 // needed for apple GCC4.2
190 this->id = 0;
192 groupA = 0;
193 portA = 0;
194 groupB = 0;
195 portB = 0;
198 void setData(const uint i, const uint gA, const uint pA, const uint gB, const uint pB) noexcept
200 // needed for apple GCC4.2
201 this->id = i;
203 groupA = gA;
204 portA = pA;
205 groupB = gB;
206 portB = pB;
209 bool operator==(const ConnectionToId& connectionToId) const noexcept
211 if (connectionToId.id != id)
212 return false;
213 if (connectionToId.groupA != groupA || connectionToId.portA != portA)
214 return false;
215 if (connectionToId.groupB != groupB || connectionToId.portB != portB)
216 return false;
217 return true;
220 bool operator!=(const ConnectionToId& connectionToId) const noexcept
222 return !operator==(connectionToId);
226 struct PatchbayConnectionList {
227 uint lastId;
228 LinkedList<ConnectionToId> list;
229 CarlaMutex mutex;
231 PatchbayConnectionList() noexcept
232 : lastId(0),
233 list(),
234 mutex() {}
236 void clear() noexcept
238 lastId = 0;
239 list.clear();
243 // -----------------------------------------------------------------------
245 #endif // CARLA_PATCHBAY_UTILS_HPP_INCLUDED