use new syntax for connecting to backend signals that enforces explicit connection...
[ardour.git] / libs / pbd / controllable.cc
1 /*
2     Copyright (C) 2000-2007 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/controllable.h"
21 #include "pbd/xml++.h"
22 #include "pbd/error.h"
23
24 #include "i18n.h"
25
26 using namespace PBD;
27 using namespace std;
28
29 PBD::Signal1<void,Controllable*> Controllable::Destroyed;
30 PBD::Signal1<bool,Controllable*> Controllable::StartLearning;
31 PBD::Signal1<void,Controllable*> Controllable::StopLearning;
32 PBD::Signal3<void,Controllable*,int,int> Controllable::CreateBinding;
33 PBD::Signal1<void,Controllable*> Controllable::DeleteBinding;
34
35 Glib::StaticRWLock Controllable::registry_lock = GLIBMM_STATIC_RW_LOCK_INIT;
36 Controllable::Controllables Controllable::registry;
37 Controllable::ControllablesByURI Controllable::registry_by_uri;
38 PBD::ScopedConnectionList registry_connections;
39
40 Controllable::Controllable (const string& name, const string& uri)
41         : _name (name)
42         , _uri (uri)
43         , _touching (false)
44 {
45         add (*this);
46 }
47
48 void
49 Controllable::add (Controllable& ctl)
50 {
51         using namespace boost;
52
53         Glib::RWLock::WriterLock lm (registry_lock);
54         registry.insert (&ctl);
55
56         if (!ctl.uri().empty()) {
57                 pair<string,Controllable*> newpair;
58                 newpair.first = ctl.uri();
59                 newpair.second = &ctl;
60                 registry_by_uri.insert (newpair);
61         }
62
63         /* Controllable::remove() is static - no need to manage this connection */
64
65         ctl.GoingAway.connect (registry_connections, boost::bind (&Controllable::remove, ref (ctl)));
66 }
67
68 void
69 Controllable::remove (Controllable& ctl)
70 {
71         Glib::RWLock::WriterLock lm (registry_lock);
72
73         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
74                 if ((*i) == &ctl) {
75                         registry.erase (i);
76                         break;
77                 }
78         }
79
80         if (!ctl.uri().empty()) {
81                 ControllablesByURI::iterator i = registry_by_uri.find (ctl.uri());
82                 if (i != registry_by_uri.end()) {
83                         registry_by_uri.erase (i);
84                 }
85         }
86 }
87
88 void
89 Controllable::set_uri (const string& new_uri)
90 {
91         Glib::RWLock::WriterLock lm (registry_lock);
92
93         if (!_uri.empty()) {
94                 ControllablesByURI::iterator i = registry_by_uri.find (_uri);
95                 if (i != registry_by_uri.end()) {
96                         registry_by_uri.erase (i);
97                 }
98         }
99
100         _uri = new_uri;
101
102         if (!_uri.empty()) {
103                 pair<string,Controllable*> newpair;
104                 newpair.first = _uri;
105                 newpair.second = this;
106                 registry_by_uri.insert (newpair);
107         }
108 }
109
110 Controllable*
111 Controllable::by_id (const ID& id)
112 {
113         Glib::RWLock::ReaderLock lm (registry_lock);
114
115         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
116                 if ((*i)->id() == id) {
117                         return (*i);
118                 }
119         }
120         return 0;
121 }
122
123 Controllable*
124 Controllable::by_uri (const string& uri)
125 {
126         Glib::RWLock::ReaderLock lm (registry_lock);
127         ControllablesByURI::iterator i;
128
129         if ((i = registry_by_uri.find (uri)) != registry_by_uri.end()) {
130                 return i->second;
131         }
132         return 0;
133 }
134
135 Controllable*
136 Controllable::by_name (const string& str)
137 {
138         Glib::RWLock::ReaderLock lm (registry_lock);
139
140         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
141                 if ((*i)->_name == str) {
142                         return (*i);
143                 }
144         }
145         return 0;
146 }
147
148 XMLNode&
149 Controllable::get_state ()
150 {
151         XMLNode* node = new XMLNode (X_("Controllable"));
152         char buf[64];
153
154         node->add_property (X_("name"), _name); // not reloaded from XML state, just there to look at
155         _id.print (buf, sizeof (buf));
156         node->add_property (X_("id"), buf);
157
158         if (!_uri.empty()) {
159                 node->add_property (X_("uri"), _uri);
160         }
161                 
162         return *node;
163 }
164
165 int
166 Controllable::set_state (const XMLNode& node, int /*version*/)
167 {
168         const XMLProperty* prop;
169
170         if ((prop = node.property (X_("id"))) != 0) {
171                 _id = prop->value();
172                 return 0;
173         } else {
174                 error << _("Controllable state node has no ID property") << endmsg;
175                 return -1;
176         }
177
178         if ((prop = node.property (X_("uri"))) != 0) {
179                 set_uri (prop->value());
180         }
181 }