use (possibly null) macros to mark VCAs for boost shared ptr debugging
[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/boost_debug.h"
25 #include "ardour/slavable.h"
26 #include "ardour/vca.h"
27 #include "ardour/vca_manager.h"
28
29 #include "i18n.h"
30
31 using namespace ARDOUR;
32 using namespace Glib::Threads;
33 using namespace PBD;
34 using std::string;
35
36 string VCAManager::xml_node_name (X_("VCAManager"));
37
38 VCAManager::VCAManager (Session& s)
39         : SessionHandleRef (s)
40         , _vcas_loaded (false)
41 {
42 }
43
44 VCAManager::~VCAManager ()
45 {
46         clear ();
47 }
48
49 void
50 VCAManager::clear ()
51 {
52         Mutex::Lock lm (lock);
53         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
54                 (*i)->DropReferences ();
55         }
56         _vcas.clear ();
57 }
58
59 VCAList
60 VCAManager::vcas () const
61 {
62         Mutex::Lock lm (lock);
63         return _vcas;
64 }
65
66 int
67 VCAManager::create_vca (uint32_t howmany, std::string const & name_template)
68 {
69         VCAList vcal;
70
71         {
72                 Mutex::Lock lm (lock);
73
74                 for (uint32_t n = 0; n < howmany; ++n) {
75
76                         int num = VCA::next_vca_number ();
77                         string name = name_template;
78
79                         if (name.find ("%n")) {
80                                 string sn = PBD::to_string (num, std::dec);
81                                 replace_all (name, "%n", sn);
82                         }
83
84                         boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, num, name));
85                         BOOST_MARK_VCA (vca);
86
87                         vca->init ();
88
89                         _vcas.push_back (vca);
90                         vcal.push_back (vca);
91                 }
92         }
93
94         VCAAdded (vcal); /* EMIT SIGNAL */
95
96         return 0;
97 }
98
99
100 void
101 VCAManager::remove_vca (boost::shared_ptr<VCA> vca)
102 {
103         {
104                 Mutex::Lock lm (lock);
105                 _vcas.remove (vca);
106         }
107
108         /* this should cause deassignment and deletion */
109
110         vca->DropReferences ();
111         std::cerr << "After DR, uc = " << vca.use_count() << std::endl;
112 }
113
114 boost::shared_ptr<VCA>
115 VCAManager::vca_by_number (uint32_t n) const
116 {
117         Mutex::Lock lm (lock);
118
119         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
120                 if ((*i)->number() == n) {
121                         return *i;
122                 }
123         }
124
125         return boost::shared_ptr<VCA>();
126 }
127
128 XMLNode&
129 VCAManager::get_state ()
130 {
131         XMLNode* node = new XMLNode (xml_node_name);
132
133         {
134                 Mutex::Lock lm (lock);
135
136                 for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
137                         node->add_child_nocopy ((*i)->get_state());
138                 }
139         }
140
141         return *node;
142 }
143
144 int
145 VCAManager::set_state (XMLNode const& node, int version)
146 {
147         if (node.name() != xml_node_name) {
148                 return -1;
149         }
150
151         XMLNodeList const & children = node.children();
152         VCAList vcal;
153
154         _vcas_loaded = false;
155
156         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
157                 if ((*i)->name() == VCA::xml_node_name) {
158                         boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, 0, X_("tobereset")));
159                         BOOST_MARK_VCA (vca);
160
161                         if (vca->init() || vca->set_state (**i, version)) {
162                                 error << _("Cannot set state of a VCA") << endmsg;
163                                 return -1;
164                         }
165
166                         /* can't hold the lock for the entire loop,
167                          * because the new VCA maybe slaved and needs
168                          * to call back into us to set up its own
169                          * slave/master relationship
170                          */
171
172                         {
173                                 Mutex::Lock lm (lock);
174                                 _vcas.push_back (vca);
175                                 vcal.push_back (vca);
176                         }
177                 }
178         }
179
180         _vcas_loaded = true;
181
182         VCAAdded (vcal); /* EMIT SIGNAL */
183
184         return 0;
185 }
186
187 void
188 VCAManager::clear_all_solo_state ()
189 {
190         Mutex::Lock lm (lock);
191
192         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
193                 (*i)->clear_all_solo_state ();
194         }
195 }