improve cleanup of VCA related objects
[ardour.git] / libs / ardour / vca_manager.cc
1 /*
2   Copyright (C) 2016 Paul Davis
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "pbd/convert.h"
21 #include "pbd/error.h"
22 #include "pbd/replace_all.h"
23
24 #include "ardour/vca.h"
25 #include "ardour/vca_manager.h"
26
27 #include "i18n.h"
28
29 using namespace ARDOUR;
30 using namespace Glib::Threads;
31 using namespace PBD;
32 using std::string;
33
34 string VCAManager::xml_node_name (X_("VCAManager"));
35
36 VCAManager::VCAManager (Session& s)
37         : SessionHandleRef (s)
38         , _vcas_loaded (false)
39 {
40 }
41
42 VCAManager::~VCAManager ()
43 {
44         clear ();
45 }
46
47 void
48 VCAManager::clear ()
49 {
50         Mutex::Lock lm (lock);
51         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
52                 (*i)->DropReferences ();
53         }
54         _vcas.clear ();
55 }
56
57 VCAList
58 VCAManager::vcas () const
59 {
60         Mutex::Lock lm (lock);
61         return _vcas;
62 }
63
64 int
65 VCAManager::create_vca (uint32_t howmany, std::string const & name_template)
66 {
67         VCAList vcal;
68
69         {
70                 Mutex::Lock lm (lock);
71
72                 for (uint32_t n = 0; n < howmany; ++n) {
73
74                         int num = VCA::next_vca_number ();
75                         string name = name_template;
76
77                         if (name.find ("%n")) {
78                                 string sn = PBD::to_string (num, std::dec);
79                                 replace_all (name, "%n", sn);
80                         }
81
82                         boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, num, name));
83
84                         vca->init ();
85
86                         _vcas.push_back (vca);
87                         vcal.push_back (vca);
88                 }
89         }
90
91         VCAAdded (vcal); /* EMIT SIGNAL */
92
93         return 0;
94 }
95
96
97 void
98 VCAManager::remove_vca (boost::shared_ptr<VCA> vca)
99 {
100         {
101                 Mutex::Lock lm (lock);
102                 _vcas.remove (vca);
103         }
104
105         VCAList vcal;
106         vcal.push_back (vca);
107
108         VCARemoved (vcal); /* EMIT SIGNAL */
109 }
110
111 boost::shared_ptr<VCA>
112 VCAManager::vca_by_number (uint32_t n) const
113 {
114         Mutex::Lock lm (lock);
115
116         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
117                 if ((*i)->number() == n) {
118                         return *i;
119                 }
120         }
121
122         return boost::shared_ptr<VCA>();
123 }
124
125 XMLNode&
126 VCAManager::get_state ()
127 {
128         XMLNode* node = new XMLNode (xml_node_name);
129
130         {
131                 Mutex::Lock lm (lock);
132
133                 for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
134                         node->add_child_nocopy ((*i)->get_state());
135                 }
136         }
137
138         return *node;
139 }
140
141 int
142 VCAManager::set_state (XMLNode const& node, int version)
143 {
144         if (node.name() != xml_node_name) {
145                 return -1;
146         }
147
148         XMLNodeList const & children = node.children();
149         VCAList vcal;
150
151         _vcas_loaded = false;
152
153         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
154                 if ((*i)->name() == VCA::xml_node_name) {
155                         boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, 0, X_("tobereset")));
156
157                         if (vca->init() || vca->set_state (**i, version)) {
158                                 error << _("Cannot set state of a VCA") << endmsg;
159                                 return -1;
160                         }
161
162                         /* can't hold the lock for the entire loop,
163                          * because the new VCA maybe slaved and needs
164                          * to call back into us to set up its own
165                          * slave/master relationship
166                          */
167
168                         {
169                                 Mutex::Lock lm (lock);
170                                 _vcas.push_back (vca);
171                                 vcal.push_back (vca);
172                         }
173                 }
174         }
175
176         _vcas_loaded = true;
177
178         VCAsLoaded (); /* EMIT SIGNAL */
179         VCAAdded (vcal); /* EMIT SIGNAL */
180
181         return 0;
182 }
183
184 void
185 VCAManager::clear_all_solo_state ()
186 {
187         Mutex::Lock lm (lock);
188
189         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
190                 (*i)->clear_all_solo_state ();
191         }
192 }