2 * Copyright (C) 2006-2012 David Robillard <d@drobilla.net>
3 * Copyright (C) 2008-2011 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "ardour/audio_port.h"
25 #include "ardour/midi_port.h"
26 #include "ardour/port.h"
27 #include "ardour/port_set.h"
35 for (size_t i=0; i < DataType::num_types; ++i)
36 _ports.push_back( PortVec() );
39 static bool sort_ports_by_name (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
41 string aname (a->name());
42 string bname (b->name());
44 string::size_type last_digit_position_a = aname.size();
45 string::reverse_iterator r_iterator = aname.rbegin();
47 while (r_iterator!= aname.rend() && Glib::Unicode::isdigit(*r_iterator)) {
49 last_digit_position_a--;
52 string::size_type last_digit_position_b = bname.size();
53 r_iterator = bname.rbegin();
55 while (r_iterator != bname.rend() && Glib::Unicode::isdigit(*r_iterator)) {
57 last_digit_position_b--;
60 // if some of the names don't have a number as posfix, compare as strings
62 if (last_digit_position_a == aname.size() || last_digit_position_b == bname.size()) {
66 const std::string prefix_a = aname.substr(0, last_digit_position_a - 1);
67 const unsigned int posfix_a = std::atoi(aname.substr(last_digit_position_a, aname.size() - last_digit_position_a).c_str());
68 const std::string prefix_b = bname.substr(0, last_digit_position_b - 1);
69 const unsigned int posfix_b = std::atoi(bname.substr(last_digit_position_b, bname.size() - last_digit_position_b).c_str());
71 if (prefix_a != prefix_b) {
74 return posfix_a < posfix_b;
79 static bool sort_ports_by_type_and_name (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
81 if (a->type() != b->type()) {
82 return a->type() < b->type();
85 return sort_ports_by_name (a, b);
89 PortSet::add (boost::shared_ptr<Port> port)
91 PortVec& v = _ports[port->type()];
94 _all_ports.push_back(port);
96 sort(v.begin(), v.end(), sort_ports_by_name);
97 sort(_all_ports.begin(), _all_ports.end(), sort_ports_by_type_and_name);
99 _count.set(port->type(), _count.get(port->type()) + 1);
100 assert(_count.get(port->type()) == _ports[port->type()].size());
104 PortSet::remove (boost::shared_ptr<Port> port)
106 PortVec::iterator i = find(_all_ports.begin(), _all_ports.end(), port);
107 if (i != _all_ports.end()) {
111 for (std::vector<PortVec>::iterator l = _ports.begin(); l != _ports.end(); ++l) {
112 PortVec::iterator i = find(l->begin(), l->end(), port);
115 _count.set(port->type(), _count.get(port->type()) - 1);
123 /** Get the total number of ports (of all types) in the PortSet
126 PortSet::num_ports() const
128 return _all_ports.size();
132 PortSet::contains (boost::shared_ptr<const Port> port) const
134 return find(_all_ports.begin(), _all_ports.end(), port) != _all_ports.end();
137 boost::shared_ptr<Port>
138 PortSet::port(size_t n) const
140 assert(n < _all_ports.size());
141 return _all_ports[n];
144 boost::shared_ptr<Port>
145 PortSet::port(DataType type, size_t n) const
147 if (type == DataType::NIL) {
150 const PortVec& v = _ports[type];
155 return boost::shared_ptr<Port>();
158 boost::shared_ptr<AudioPort>
159 PortSet::nth_audio_port(size_t n) const
161 return boost::dynamic_pointer_cast<AudioPort> (port (DataType::AUDIO, n));
164 boost::shared_ptr<MidiPort>
165 PortSet::nth_midi_port(size_t n) const
167 return boost::dynamic_pointer_cast<MidiPort> (port (DataType::MIDI, n));