379a3d4c2be687e7698b64ed7b966fe135dace21
[ardour.git] / libs / ardour / bundle.cc
1 /*
2     Copyright (C) 2002 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 <algorithm>
21
22 #include <pbd/failed_constructor.h>
23 #include <ardour/ardour.h>
24 #include <ardour/bundle.h>
25 #include <pbd/xml++.h>
26
27 #include "i18n.h"
28
29 using namespace ARDOUR;
30 using namespace PBD;
31
32 uint32_t
33 Bundle::nchannels () const
34 {
35         Glib::Mutex::Lock lm (_channel_mutex);
36         return _channel.size ();
37 }
38
39 Bundle::PortList const &
40 Bundle::channel_ports (uint32_t c) const
41 {
42         assert (c < nchannels());
43
44         Glib::Mutex::Lock lm (_channel_mutex);
45         return _channel[c].ports;
46 }
47
48 /** Add an association between one of our channels and a port.
49  *  @param ch Channel index.
50  *  @param portname port name to associate with.
51  */
52 void
53 Bundle::add_port_to_channel (uint32_t ch, string portname)
54 {
55         assert (ch < nchannels());
56
57         {
58                 Glib::Mutex::Lock lm (_channel_mutex);
59                 _channel[ch].ports.push_back (portname);
60         }
61         
62         PortsChanged (ch); /* EMIT SIGNAL */
63 }
64
65 /** Disassociate a port from one of our channels.
66  *  @param ch Channel index.
67  *  @param portname port name to disassociate from.
68  */
69 void
70 Bundle::remove_port_from_channel (uint32_t ch, string portname)
71 {
72         assert (ch < nchannels());
73
74         bool changed = false;
75
76         {
77                 Glib::Mutex::Lock lm (_channel_mutex);
78                 PortList& pl = _channel[ch].ports;
79                 PortList::iterator i = find (pl.begin(), pl.end(), portname);
80                 
81                 if (i != pl.end()) {
82                         pl.erase (i);
83                         changed = true;
84                 }
85         }
86
87         if (changed) {
88                  PortsChanged (ch); /* EMIT SIGNAL */
89         }
90 }
91
92 /** operator== for Bundles; they are equal if their channels are the same.
93  * @param other Bundle to compare with this one.
94  */
95 bool
96 Bundle::operator== (const Bundle& other) const
97 {
98         return other._channel == _channel;
99 }
100
101
102 void
103 Bundle::set_port (uint32_t ch, string portname)
104 {
105         assert (ch < nchannels());
106
107         {
108                 Glib::Mutex::Lock lm (_channel_mutex);
109                 _channel[ch].ports.clear ();
110                 _channel[ch].ports.push_back (portname);
111         }
112
113         PortsChanged (ch); /* EMIT SIGNAL */
114 }
115
116 /** @param n Channel name */
117 void
118 Bundle::add_channel (std::string const & n)
119 {
120         {
121                 Glib::Mutex::Lock lm (_channel_mutex);
122                 _channel.push_back (Channel (n));
123         }
124
125         ConfigurationChanged (); /* EMIT SIGNAL */
126 }
127
128 bool
129 Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
130 {
131         assert (ch < nchannels());
132         
133         Glib::Mutex::Lock lm (_channel_mutex);
134         return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
135 }
136
137 void
138 Bundle::remove_channel (uint32_t ch)
139 {
140         assert (ch < nchannels ());
141
142         Glib::Mutex::Lock lm (_channel_mutex);
143         _channel.erase (_channel.begin () + ch);
144 }
145
146 void
147 Bundle::remove_channels ()
148 {
149         Glib::Mutex::Lock lm (_channel_mutex);
150
151         _channel.clear ();
152 }
153
154 bool
155 Bundle::uses_port (std::string p) const
156 {
157         Glib::Mutex::Lock lm (_channel_mutex);
158
159         for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
160                 for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
161                         if (*j == p) {
162                                 return true;
163                         }
164                 }
165         }
166
167         return false;
168 }
169
170 std::string
171 Bundle::channel_name (uint32_t ch) const
172 {
173         assert (ch < nchannels());
174
175         Glib::Mutex::Lock lm (_channel_mutex);
176         return _channel[ch].name;
177 }
178
179 void
180 Bundle::set_channel_name (uint32_t ch, std::string const & n)
181 {
182         assert (ch < nchannels());
183
184         {
185                 Glib::Mutex::Lock lm (_channel_mutex);
186                 _channel[ch].name = n;
187         }
188
189         NameChanged (); /* EMIT SIGNAL */
190 }