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)
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
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.
21 #include "ardour/audio_port.h"
22 #include "ardour/midi_port.h"
23 #include "ardour/port.h"
24 #include "ardour/port_set.h"
32 for (size_t i=0; i < DataType::num_types; ++i)
33 _ports.push_back( PortVec() );
36 static bool sort_ports_by_name (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
38 string aname (a->name());
39 string bname (b->name());
41 string::size_type last_digit_position_a = aname.size();
42 string::reverse_iterator r_iterator = aname.rbegin();
44 while (r_iterator!= aname.rend() && Glib::Unicode::isdigit(*r_iterator)) {
46 last_digit_position_a--;
49 string::size_type last_digit_position_b = bname.size();
50 r_iterator = bname.rbegin();
52 while (r_iterator != bname.rend() && Glib::Unicode::isdigit(*r_iterator)) {
54 last_digit_position_b--;
57 // if some of the names don't have a number as posfix, compare as strings
59 if (last_digit_position_a == aname.size() || last_digit_position_b == bname.size()) {
63 const std::string prefix_a = aname.substr(0, last_digit_position_a - 1);
64 const unsigned int posfix_a = std::atoi(aname.substr(last_digit_position_a, aname.size() - last_digit_position_a).c_str());
65 const std::string prefix_b = bname.substr(0, last_digit_position_b - 1);
66 const unsigned int posfix_b = std::atoi(bname.substr(last_digit_position_b, bname.size() - last_digit_position_b).c_str());
68 if (prefix_a != prefix_b) {
71 return posfix_a < posfix_b;
76 static bool sort_ports_by_type_and_name (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
78 if (a->type() != b->type()) {
79 return a->type() < b->type();
82 return sort_ports_by_name (a, b);
86 PortSet::add (boost::shared_ptr<Port> port)
88 PortVec& v = _ports[port->type()];
91 _all_ports.push_back(port);
93 sort(v.begin(), v.end(), sort_ports_by_name);
94 sort(_all_ports.begin(), _all_ports.end(), sort_ports_by_type_and_name);
96 _count.set(port->type(), _count.get(port->type()) + 1);
97 assert(_count.get(port->type()) == _ports[port->type()].size());
101 PortSet::remove (boost::shared_ptr<Port> port)
103 PortVec::iterator i = find(_all_ports.begin(), _all_ports.end(), port);
104 if (i != _all_ports.end()) {
108 for (std::vector<PortVec>::iterator l = _ports.begin(); l != _ports.end(); ++l) {
109 PortVec::iterator i = find(l->begin(), l->end(), port);
112 _count.set(port->type(), _count.get(port->type()) - 1);
120 /** Get the total number of ports (of all types) in the PortSet
123 PortSet::num_ports() const
125 return _all_ports.size();
129 PortSet::contains (boost::shared_ptr<const Port> port) const
131 return find(_all_ports.begin(), _all_ports.end(), port) != _all_ports.end();
134 boost::shared_ptr<Port>
135 PortSet::port(size_t n) const
137 assert(n < _all_ports.size());
138 return _all_ports[n];
141 boost::shared_ptr<Port>
142 PortSet::port(DataType type, size_t n) const
144 if (type == DataType::NIL) {
147 const PortVec& v = _ports[type];
152 return boost::shared_ptr<Port>();
155 boost::shared_ptr<AudioPort>
156 PortSet::nth_audio_port(size_t n) const
158 return boost::dynamic_pointer_cast<AudioPort> (port (DataType::AUDIO, n));
161 boost::shared_ptr<MidiPort>
162 PortSet::nth_midi_port(size_t n) const
164 return boost::dynamic_pointer_cast<MidiPort> (port (DataType::MIDI, n));