2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
20 #include "pbd/controllable.h"
21 #include "pbd/xml++.h"
22 #include "pbd/error.h"
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;
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;
40 Controllable::Controllable (const string& name, const string& uri)
49 Controllable::add (Controllable& ctl)
51 using namespace boost;
53 Glib::RWLock::WriterLock lm (registry_lock);
54 registry.insert (&ctl);
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);
63 /* Controllable::remove() is static - no need to manage this connection */
65 ctl.GoingAway.connect (registry_connections, boost::bind (&Controllable::remove, ref (ctl)));
69 Controllable::remove (Controllable& ctl)
71 Glib::RWLock::WriterLock lm (registry_lock);
73 for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
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);
89 Controllable::set_uri (const string& new_uri)
91 Glib::RWLock::WriterLock lm (registry_lock);
94 ControllablesByURI::iterator i = registry_by_uri.find (_uri);
95 if (i != registry_by_uri.end()) {
96 registry_by_uri.erase (i);
103 pair<string,Controllable*> newpair;
104 newpair.first = _uri;
105 newpair.second = this;
106 registry_by_uri.insert (newpair);
111 Controllable::by_id (const ID& id)
113 Glib::RWLock::ReaderLock lm (registry_lock);
115 for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
116 if ((*i)->id() == id) {
124 Controllable::by_uri (const string& uri)
126 Glib::RWLock::ReaderLock lm (registry_lock);
127 ControllablesByURI::iterator i;
129 if ((i = registry_by_uri.find (uri)) != registry_by_uri.end()) {
136 Controllable::by_name (const string& str)
138 Glib::RWLock::ReaderLock lm (registry_lock);
140 for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
141 if ((*i)->_name == str) {
149 Controllable::get_state ()
151 XMLNode* node = new XMLNode (X_("Controllable"));
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);
159 node->add_property (X_("uri"), _uri);
166 Controllable::set_state (const XMLNode& node, int /*version*/)
168 const XMLProperty* prop;
170 if ((prop = node.property (X_("id"))) != 0) {
174 error << _("Controllable state node has no ID property") << endmsg;
178 if ((prop = node.property (X_("uri"))) != 0) {
179 set_uri (prop->value());