forked from KiwiMusic/KiwiPatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KiwiPatch.h
296 lines (248 loc) · 11.2 KB
/
KiwiPatch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
==============================================================================
This file is part of the KIWI library.
Copyright (c) 2014 Pierre Guillot & Eliott Paris.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
KIWI is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses KIWI, contact : guillotpierre6@gmail.com
==============================================================================
*/
#ifndef __DEF_KIWI_PATCH__
#define __DEF_KIWI_PATCH__
#include "KiwiLink.h"
namespace Kiwi
{
// ================================================================================ //
// PATCHER //
// ================================================================================ //
//! The patcher manages objects and links.
/**
The patcher is... ??
*/
class Patcher : public GuiModel, public DspChain, public Attr::Manager
{
public:
class Window;
typedef shared_ptr<Window> sWindow;
typedef weak_ptr<Window> wWindow;
typedef shared_ptr<const Window> scWindow;
typedef weak_ptr<const Window> wcWindow;
class Controller;
typedef shared_ptr<Controller> sController;
typedef weak_ptr<Controller> wController;
typedef shared_ptr<const Controller> scController;
typedef weak_ptr<const Controller> wcController;
class Listener;
typedef shared_ptr<Listener> sListener;
typedef weak_ptr<Listener> wListener;
typedef shared_ptr<const Listener> scListener;
typedef weak_ptr<const Listener> wcListener;
private:
const wInstance m_instance;
vector<sObject> m_objects;
vector<sLink> m_links;
vector<ulong> m_free_ids;
mutable mutex m_mutex;
ListenerSet<Listener> m_listeners;
//! @internal Object and link creation.
void createObject(Dico& dico);
void createLink(Dico const& dico);
public:
//! Constructor.
/** You should never call this method except if you really know what you're doing.
*/
Patcher(sInstance instance) noexcept;
//! Destructor.
/** You should never call this method except if you really know what you're doing.
*/
~Patcher();
//! The patcher creation method.
/** The function allocates a patcher and initialize the defaults objects.
@param instance The instance that will manage the patcher.
@param dico The dico that will initialize the patcher.
@return The patcher.
*/
static sPatcher create(sInstance instance, Dico& dico);
//! Add a patcher's listener.
/** The function adds a patcher's listener.
If the listener was already listening the patcher, the function has no effect.
@param listener The listener to add.
*/
void addListener(sListener listener) noexcept
{
m_listeners.add(listener);
}
//! Remove a patcher's listener.
/** The function removes a patcher's listener.
If the listener wasn't listening the patcher, the function has no effect.
@param listener The listener to add.
*/
void removeListener(sListener listener) noexcept
{
m_listeners.remove(listener);
}
//! Retrieve the instance that manages the patcher.
/** The function retrieves the instance that manages the patcher.
@return The instance that manages the patcher.
*/
inline sInstance getInstance() const noexcept
{
return m_instance.lock();
}
//! Retrieve the shared pointer of the patcher.
/** The function retrieves the shared pointer of the patcher.
@return The shared pointer of the patcher.
*/
inline scPatcher getShared() const noexcept
{
return static_pointer_cast<const Patcher>(DspChain::shared_from_this());
}
//! Retrieve the shared pointer of the patcher.
/** The function retrieves the shared pointer of the patcher.
@return The shared pointer of the patcher.
*/
inline sPatcher getShared() noexcept
{
return static_pointer_cast<Patcher>(DspChain::shared_from_this());
}
//! Get the objects.
/** The function retrieves the objects from the patcher.
@return A vector with the objects.
*/
inline vector<sObject> getObjects() const noexcept
{
lock_guard<mutex> guard(m_mutex);
return m_objects;
}
//! Get an object with the id.
/** The function retrieves an object with an id.
@param id The id of the object.
*/
inline sObject getObjectWithId(ulong const _id) const noexcept
{
lock_guard<mutex> guard(m_mutex);
for(vector<sObject>::size_type i = 0; i < m_objects.size(); i++)
{
if(m_objects[i]->getId() == _id)
{
return m_objects[i];
}
}
return nullptr;
}
//! Get the links.
/** The function retrieves the links from the patcher.
@return A vector of links.
*/
vector<sLink> getLinks() const noexcept
{
lock_guard<mutex> guard(m_mutex);
return m_links;
}
//! Append a dico.
/** The function reads a dico and add the objects and links to the patcher.
@param dico The dico.
*/
void add(Dico const& dico);
//! Free a object.
/** The function removes a object from the patcher.
@param object The pointer to the object.
*/
void remove(sObject object);
//! Free a link.
/** The function removes a link from the patcher.
@param link The pointer to the link.
*/
void remove(sLink link);
//! Bring a object to the front of the patcher.
/** The function brings a object to the front of the patcher. The object will be setted as if it was the last object created and will be the last object of the vector of objects.
@param object The pointer to the object.
*/
void toFront(sObject object);
//! Bring a object to the back of the patcher.
/** The function brings a object to the back of the patcher. The object will be setted as if it was the first object created and will be the first object of the vector of objects.
@param object The pointer to the object.
*/
void toBack(sObject object);
//! Write the patcher in a dico.
/** The function writes the patcher in a dico.
@param dico The dico.
*/
void write(Dico& dico) const;
//! Create a new window for the patcher.
/** The function creates a new window for the patcher.
@return The window.
*/
sGuiWindow createWindow();
//! Retrieve the "gridsize" attribute value of the patcher.
/** The function retrieves the "gridsize" attribute value of the patcher.
@return The "gridsize" attribute value of the patcher.
*/
inline long getGridSize() const noexcept
{
return getAttrValue<long>(Tags::gridsize);
}
//! Retrieve the "locked_bgcolor" attribute value of the patcher.
/** The function retrieves the "locked_bgcolor" attribute value of the patcher.
@return The "locked_bgcolor" attribute value of the patcher.
*/
inline Color getLockedBackgroundColor() const noexcept
{
return getAttrValue<Color>(Tags::locked_bgcolor);
}
//! Retrieve the "locked_bgcolor" attribute value of the patcher.
/** The function retrieves the "locked_bgcolor" attribute value of the patcher.
@return The "locked_bgcolor" attribute value of the patcher.
*/
inline Color getUnlockedBackgroundColor() const noexcept
{
return getAttrValue<Color>(Tags::unlocked_bgcolor);
}
private:
//! Create the controller.
/** The function creates a controller of the patcher.
@return The controller.
*/
sGuiController createController() override;
};
// ================================================================================ //
// PATCHER LISTENER //
// ================================================================================ //
//! The patcher listener is an abstract class that subclasses should inherit from to receive notifications.
/**
The patcher listener is an abstract class that subclasses should inherit from to receive notifications related to object and link creation and deletion
*/
class Patcher::Listener
{
public:
//! The destructor.
virtual ~Listener() {}
//! Receive the notification that an object has been created.
/** The function is called by the patcher when an object has been created.
@param object The object.
*/
virtual void objectCreated(sPatcher patcher, sObject object) = 0;
//! Receive the notification that an object has been removed.
/** The function is called by the patcher when an object has been removed.
@param object The object.
*/
virtual void objectRemoved(sPatcher patcher, sObject object) = 0;
//! Receive the notification that a link has been created.
/** The function is called by the patcher when a link has been created.
@param link The link.
*/
virtual void linkCreated(sPatcher patcher, sLink link) = 0;
//! Receive the notification that a link has been removed.
/** The function is called by the patcher when a link has been removed.
@param link The link.
*/
virtual void linkRemoved(sPatcher patcher, sLink link) = 0;
};
}
#endif