00638e6c06cc3312b159723913b3671523ef49aa
[ardour.git] / libs / pbd / controllable.cc
1 #include <pbd/controllable.h>
2 #include <pbd/xml++.h>
3 #include <pbd/error.h>
4
5 #include "i18n.h"
6
7 using namespace PBD;
8
9 sigc::signal<void,Controllable*> Controllable::Destroyed;
10 sigc::signal<bool,Controllable*> Controllable::StartLearning;
11 sigc::signal<void,Controllable*> Controllable::StopLearning;
12
13 Glib::Mutex* Controllable::registry_lock = 0;
14 Controllable::Controllables Controllable::registry;
15
16 Controllable::Controllable (std::string name)
17         : _name (name)
18 {
19         if (registry_lock == 0) {
20                 registry_lock = new Glib::Mutex;
21         }
22
23         add ();
24 }
25
26 void
27 Controllable::add ()
28 {
29         Glib::Mutex::Lock lm (*registry_lock);
30         registry.insert (this);
31         this->GoingAway.connect (mem_fun (this, &Controllable::remove));
32 }
33
34 void
35 Controllable::remove ()
36 {
37         Glib::Mutex::Lock lm (*registry_lock);
38         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
39                 if ((*i) == this) {
40                         registry.erase (i);
41                         break;
42                 }
43         }
44 }
45
46 Controllable*
47 Controllable::by_id (const ID& id)
48 {
49         Glib::Mutex::Lock lm (*registry_lock);
50
51         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
52                 if ((*i)->id() == id) {
53                         return (*i);
54                 }
55         }
56         return 0;
57 }
58
59
60 Controllable*
61 Controllable::by_name (const std::string& str)
62 {
63         Glib::Mutex::Lock lm (*registry_lock);
64
65         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
66                 if ((*i)->_name == str) {
67                         return (*i);
68                 }
69         }
70         return 0;
71 }
72
73 XMLNode&
74 Controllable::get_state ()
75 {
76         XMLNode* node = new XMLNode (X_("controllable"));
77         char buf[64];
78
79         node->add_property (X_("name"), _name); // not reloaded from XML state, just there to look at
80         _id.print (buf, sizeof (buf));
81         node->add_property (X_("id"), buf);
82         return *node;
83 }
84
85 int
86 Controllable::set_state (const XMLNode& node)
87 {
88         const XMLProperty* prop = node.property (X_("id"));
89
90         if (prop) {
91                 _id = prop->value();
92                 return 0;
93         } else {
94                 error << _("Controllable state node has no ID property") << endmsg;
95                 return -1;
96         }
97 }