Move panner bypass state up to the PannerShell so that it is preserved even when...
[ardour2.git] / libs / ardour / port_set.cc
blob2105d184fb9e3921b27f3d6b0b082ae96675f08f
1 /*
2 Copyright (C) 2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2 of the License, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <string>
21 #include "ardour/port_set.h"
22 #include "ardour/midi_port.h"
23 #include "ardour/audio_port.h"
25 using std::string;
27 namespace ARDOUR {
29 PortSet::PortSet()
31 for (size_t i=0; i < DataType::num_types; ++i)
32 _ports.push_back( PortVec() );
35 static bool sort_ports_by_name (Port* a, Port* b)
37 string aname (a->name());
38 string bname (b->name());
40 string::size_type last_digit_position_a = aname.size();
41 string::reverse_iterator r_iterator = aname.rbegin();
43 while (r_iterator!= aname.rend() && Glib::Unicode::isdigit(*r_iterator)) {
44 r_iterator++;
45 last_digit_position_a--;
48 string::size_type last_digit_position_b = bname.size();
49 r_iterator = bname.rbegin();
51 while (r_iterator != bname.rend() && Glib::Unicode::isdigit(*r_iterator)) {
52 r_iterator++;
53 last_digit_position_b--;
56 // if some of the names don't have a number as posfix, compare as strings
58 if (last_digit_position_a == aname.size() or last_digit_position_b == bname.size()) {
59 return aname < bname;
62 const std::string prefix_a = aname.substr(0, last_digit_position_a - 1);
63 const unsigned int posfix_a = std::atoi(aname.substr(last_digit_position_a, aname.size() - last_digit_position_a).c_str());
64 const std::string prefix_b = bname.substr(0, last_digit_position_b - 1);
65 const unsigned int posfix_b = std::atoi(bname.substr(last_digit_position_b, bname.size() - last_digit_position_b).c_str());
67 if (prefix_a != prefix_b) {
68 return aname < bname;
69 } else {
70 return posfix_a < posfix_b;
74 void
75 PortSet::add(Port* port)
77 PortVec& v = _ports[port->type()];
79 v.push_back(port);
80 sort(v.begin(), v.end(), sort_ports_by_name);
82 _count.set(port->type(), _count.get(port->type()) + 1);
84 assert(_count.get(port->type()) == _ports[port->type()].size());
87 bool
88 PortSet::remove(Port* port)
90 for (std::vector<PortVec>::iterator l = _ports.begin(); l != _ports.end(); ++l) {
91 PortVec::iterator i = find(l->begin(), l->end(), port);
92 if (i != l->end()) {
93 l->erase(i);
94 _count.set(port->type(), _count.get(port->type()) - 1);
95 return true;
99 return false;
102 /** Get the total number of ports (of all types) in the PortSet
104 size_t
105 PortSet::num_ports() const
107 size_t ret = 0;
109 for (std::vector<PortVec>::const_iterator l = _ports.begin(); l != _ports.end(); ++l)
110 ret += (*l).size();
112 return ret;
115 bool
116 PortSet::contains(const Port* port) const
118 for (std::vector<PortVec>::const_iterator l = _ports.begin(); l != _ports.end(); ++l)
119 if (find((*l).begin(), (*l).end(), port) != (*l).end())
120 return true;
122 return false;
125 Port*
126 PortSet::port(size_t n) const
128 // This is awesome. Awesomely slow.
130 size_t size_so_far = 0;
132 for (std::vector<PortVec>::const_iterator l = _ports.begin(); l != _ports.end(); ++l) {
133 if (n < size_so_far + (*l).size())
134 return (*l)[n - size_so_far];
135 else
136 size_so_far += (*l).size();
139 return NULL; // n out of range
142 Port*
143 PortSet::port(DataType type, size_t n) const
145 if (type == DataType::NIL) {
146 return port(n);
147 } else {
148 const PortVec& v = _ports[type];
149 assert(n < v.size());
150 return v[n];
154 AudioPort*
155 PortSet::nth_audio_port(size_t n) const
157 return dynamic_cast<AudioPort*>(port(DataType::AUDIO, n));
160 MidiPort*
161 PortSet::nth_midi_port(size_t n) const
163 return dynamic_cast<MidiPort*>(port(DataType::MIDI, n));
166 } // namepace ARDOUR