ardour
controllable.cc
Go to the documentation of this file.
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/enumwriter.h"
22 #include "pbd/xml++.h"
23 #include "pbd/error.h"
24 #include "pbd/locale_guard.h"
25 
26 #include "i18n.h"
27 
28 using namespace PBD;
29 using namespace std;
30 
31 PBD::Signal1<void,Controllable*> Controllable::Destroyed;
32 PBD::Signal1<bool,Controllable*> Controllable::StartLearning;
33 PBD::Signal1<void,Controllable*> Controllable::StopLearning;
34 PBD::Signal3<void,Controllable*,int,int> Controllable::CreateBinding;
35 PBD::Signal1<void,Controllable*> Controllable::DeleteBinding;
36 
37 Glib::Threads::RWLock Controllable::registry_lock;
40 const std::string Controllable::xml_node_name = X_("Controllable");
41 
43  : _name (name)
44  , _flags (f)
45  , _touching (false)
46 {
47  add (*this);
48 }
49 
50 void
52 {
53  using namespace boost;
54 
55  Glib::Threads::RWLock::WriterLock lm (registry_lock);
56  registry.insert (&ctl);
57 
58  if (!registry_connections) {
59  registry_connections = new ScopedConnectionList;
60  }
61 
62  /* Controllable::remove() is static - no need to manage this connection */
63 
64  ctl.DropReferences.connect_same_thread (*registry_connections, boost::bind (&Controllable::remove, &ctl));
65 }
66 
67 void
69 {
70  Glib::Threads::RWLock::WriterLock lm (registry_lock);
71 
72  for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
73  if ((*i) == ctl) {
74  registry.erase (i);
75  break;
76  }
77  }
78 }
79 
82 {
83  Glib::Threads::RWLock::ReaderLock lm (registry_lock);
84 
85  for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
86  if ((*i)->id() == id) {
87  return (*i);
88  }
89  }
90  return 0;
91 }
92 
94 Controllable::by_name (const string& str)
95 {
96  Glib::Threads::RWLock::ReaderLock lm (registry_lock);
97 
98  for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
99  if ((*i)->_name == str) {
100  return (*i);
101  }
102  }
103  return 0;
104 }
105 
106 XMLNode&
108 {
109  XMLNode* node = new XMLNode (xml_node_name);
110  LocaleGuard lg (X_("C"));
111  char buf[64];
112 
113  node->add_property (X_("name"), _name); // not reloaded from XML state, just there to look at
114  id().print (buf, sizeof (buf));
115  node->add_property (X_("id"), buf);
116  node->add_property (X_("flags"), enum_2_string (_flags));
117  snprintf (buf, sizeof (buf), "%2.12f", get_value());
118  node->add_property (X_("value"), buf);
119 
120  if (_extra_xml) {
121  node->add_child_copy (*_extra_xml);
122  }
123 
124  return *node;
125 }
126 
127 
128 int
129 Controllable::set_state (const XMLNode& node, int /*version*/)
130 {
131  LocaleGuard lg (X_("C"));
132  const XMLProperty* prop;
133 
135 
136  set_id (node);
137 
138  if ((prop = node.property (X_("flags"))) != 0) {
139  _flags = (Flag) string_2_enum (prop->value(), _flags);
140  }
141 
142  if ((prop = node.property (X_("value"))) != 0) {
143  float val;
144 
145  if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
146  set_value (val);
147  }
148  }
149 
150  return 0;
151 }
152 
153 void
155 {
156  _flags = f;
157 }
static PBD::Signal3< void, PBD::Controllable *, int, int > CreateBinding
Definition: controllable.h:86
PBD::Signal0< void > DropReferences
Definition: destructible.h:34
const std::string & value() const
Definition: xml++.h:159
XMLNode & get_state()
void save_extra_xml(const XMLNode &)
Definition: stateful.cc:94
static Controllables registry
Definition: controllable.h:130
#define enum_2_string(e)
Definition: enumwriter.h:97
tuple f
Definition: signals.py:35
XMLNode * add_child_copy(const XMLNode &)
Definition: xml++.cc:363
Definition: Beats.hpp:239
static PBD::Signal1< bool, PBD::Controllable * > StartLearning
Definition: controllable.h:89
Definition: id.h:32
XMLNode * _extra_xml
Definition: stateful.h:109
static Controllable * by_id(const PBD::ID &)
Definition: controllable.cc:81
#define X_(Text)
Definition: i18n.h:13
static void add(Controllable &)
Definition: controllable.cc:51
XMLProperty * property(const char *)
Definition: xml++.cc:413
#define string_2_enum(str, e)
Definition: enumwriter.h:98
static PBD::Signal1< void, PBD::Controllable * > StopLearning
Definition: controllable.h:90
const PBD::ID & id() const
Definition: stateful.h:68
void print(char *buf, uint32_t bufsize) const
Definition: id.cc:73
bool set_id(const XMLNode &)
Definition: stateful.cc:381
static Glib::Threads::RWLock registry_lock
Definition: controllable.h:129
int set_state(const XMLNode &, int version)
static PBD::Signal1< void, Controllable * > Destroyed
Definition: controllable.h:92
virtual double get_value(void) const =0
static const std::string xml_node_name
Definition: controllable.h:116
XMLProperty * add_property(const char *name, const std::string &value)
const char * name
std::string _name
Definition: controllable.h:118
virtual void set_value(double)=0
Definition: xml++.h:95
static void remove(Controllable *)
Definition: controllable.cc:68
static Controllable * by_name(const std::string &)
Definition: controllable.cc:94
Definition: debug.h:30
void set_flags(Flag f)
std::set< PBD::Controllable * > Controllables
Definition: controllable.h:128
PBD::ScopedConnectionList * registry_connections
Definition: controllable.cc:39
Controllable(const std::string &name, Flag f=Flag(0))
Definition: controllable.cc:42
static PBD::Signal1< void, PBD::Controllable * > DeleteBinding
Definition: controllable.h:87