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.
19 #include "ardour/port_set.h"
20 #include "ardour/midi_port.h"
21 #include "ardour/audio_port.h"
27 for (size_t i=0; i < DataType::num_types; ++i)
28 _ports.push_back( PortVec() );
31 static bool sort_ports_by_name (Port* a, Port* b)
33 return (a->name() < b->name());
37 PortSet::add(Port* port)
39 PortVec& v = _ports[port->type()];
42 sort(v.begin(), v.end(), sort_ports_by_name);
44 _count.set(port->type(), _count.get(port->type()) + 1);
46 assert(_count.get(port->type()) == _ports[port->type()].size());
50 PortSet::remove(Port* port)
52 for (std::vector<PortVec>::iterator l = _ports.begin(); l != _ports.end(); ++l) {
53 PortVec::iterator i = find(l->begin(), l->end(), port);
56 _count.set(port->type(), _count.get(port->type()) - 1);
64 /** Get the total number of ports (of all types) in the PortSet
67 PortSet::num_ports() const
71 for (std::vector<PortVec>::const_iterator l = _ports.begin(); l != _ports.end(); ++l)
78 PortSet::contains(const Port* port) const
80 for (std::vector<PortVec>::const_iterator l = _ports.begin(); l != _ports.end(); ++l)
81 if (find((*l).begin(), (*l).end(), port) != (*l).end())
88 PortSet::port(size_t n) const
90 // This is awesome. Awesomely slow.
92 size_t size_so_far = 0;
94 for (std::vector<PortVec>::const_iterator l = _ports.begin(); l != _ports.end(); ++l) {
95 if (n < size_so_far + (*l).size())
96 return (*l)[n - size_so_far];
98 size_so_far += (*l).size();
101 return NULL; // n out of range
105 PortSet::port(DataType type, size_t n) const
107 if (type == DataType::NIL) {
110 const PortVec& v = _ports[type];
111 assert(n < v.size());
117 PortSet::nth_audio_port(size_t n) const
119 return dynamic_cast<AudioPort*>(port(DataType::AUDIO, n));
123 PortSet::nth_midi_port(size_t n) const
125 return dynamic_cast<MidiPort*>(port(DataType::MIDI, n));