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 boost::signals2::signal<void(Controllable*)> Controllable::Destroyed;
30 boost::signals2::signal<bool(Controllable*)> Controllable::StartLearning;
31 boost::signals2::signal<void(Controllable*)> Controllable::StopLearning;
32 boost::signals2::signal<void(Controllable*,int,int)> Controllable::CreateBinding;
33 boost::signals2::signal<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;
39 Controllable::Controllable (const string& name, const string& uri)
48 Controllable::add (Controllable& ctl)
50 using namespace boost;
52 Glib::RWLock::WriterLock lm (registry_lock);
53 registry.insert (&ctl);
55 if (!ctl.uri().empty()) {
56 pair<string,Controllable*> newpair;
57 newpair.first = ctl.uri();
58 newpair.second = &ctl;
59 registry_by_uri.insert (newpair);
62 /* Controllable::remove() is static - no need to manage this connection */
64 ctl.GoingAway.connect (boost::bind (&Controllable::remove, ref (ctl)));
68 Controllable::remove (Controllable& ctl)
70 Glib::RWLock::WriterLock lm (registry_lock);
72 for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
79 if (!ctl.uri().empty()) {
80 ControllablesByURI::iterator i = registry_by_uri.find (ctl.uri());
81 if (i != registry_by_uri.end()) {
82 registry_by_uri.erase (i);
88 Controllable::set_uri (const string& new_uri)
90 Glib::RWLock::WriterLock lm (registry_lock);
93 ControllablesByURI::iterator i = registry_by_uri.find (_uri);
94 if (i != registry_by_uri.end()) {
95 registry_by_uri.erase (i);
102 pair<string,Controllable*> newpair;
103 newpair.first = _uri;
104 newpair.second = this;
105 registry_by_uri.insert (newpair);
110 Controllable::by_id (const ID& id)
112 Glib::RWLock::ReaderLock lm (registry_lock);
114 for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
115 if ((*i)->id() == id) {
123 Controllable::by_uri (const string& uri)
125 Glib::RWLock::ReaderLock lm (registry_lock);
126 ControllablesByURI::iterator i;
128 if ((i = registry_by_uri.find (uri)) != registry_by_uri.end()) {
135 Controllable::by_name (const string& str)
137 Glib::RWLock::ReaderLock lm (registry_lock);
139 for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
140 if ((*i)->_name == str) {
148 Controllable::get_state ()
150 XMLNode* node = new XMLNode (X_("Controllable"));
153 node->add_property (X_("name"), _name); // not reloaded from XML state, just there to look at
154 _id.print (buf, sizeof (buf));
155 node->add_property (X_("id"), buf);
158 node->add_property (X_("uri"), _uri);
165 Controllable::set_state (const XMLNode& node, int /*version*/)
167 const XMLProperty* prop;
169 if ((prop = node.property (X_("id"))) != 0) {
173 error << _("Controllable state node has no ID property") << endmsg;
177 if ((prop = node.property (X_("uri"))) != 0) {
178 set_uri (prop->value());