- Fixed port adding/removing/connecting and related bugs with route signal path
[ardour.git] / libs / ardour / port_set.cc
1 /*
2     Copyright (C) 2006 Paul Davis 
3     
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.
8     
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.
13     
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.
17 */
18
19 #include <ardour/port_set.h>
20
21 namespace ARDOUR {
22
23 PortSet::PortSet()
24 {
25         for (size_t i=0; i < DataType::num_types; ++i)
26                 _ports.push_back( PortVec() );
27 }
28
29 static bool sort_ports_by_name (Port* a, Port* b)
30 {
31         return (a->name() < b->name());
32 }
33
34 void
35 PortSet::add(Port* port)
36 {
37         const size_t list_index = port->type().to_index();
38         assert(list_index < _ports.size());
39         
40         PortVec& v = _ports[list_index];
41         
42         v.push_back(port);
43         sort(v.begin(), v.end(), sort_ports_by_name);
44
45         _count.set(port->type(), _count.get(port->type()) + 1);
46
47         assert(_count.get(port->type()) == _ports[port->type().to_index()].size());
48 }
49
50 bool
51 PortSet::remove(Port* port)
52 {
53         for (std::vector<PortVec>::iterator l = _ports.begin(); l != _ports.end(); ++l) {
54                 PortVec::iterator i = find(l->begin(), l->end(), port);
55                 if (i != l->end()) {
56                         l->erase(i);
57                         _count.set(port->type(), _count.get(port->type()) - 1);
58                         return true;
59                 }
60         }
61
62         return false;
63 }
64
65 /** Get the total number of ports (of all types) in the PortSet
66  */
67 size_t
68 PortSet::num_ports() const
69 {
70         size_t ret = 0;
71
72         for (std::vector<PortVec>::const_iterator l = _ports.begin(); l != _ports.end(); ++l)
73                 ret += (*l).size();
74
75         return ret;
76 }
77
78 bool
79 PortSet::contains(const Port* port) const
80 {
81         for (std::vector<PortVec>::const_iterator l = _ports.begin(); l != _ports.end(); ++l)
82                 if (find((*l).begin(), (*l).end(), port) != (*l).end())
83                         return true;
84
85         return false;
86 }
87
88 Port*
89 PortSet::port(size_t n) const
90 {
91         // This is awesome.  Awesomely slow.
92         
93         size_t size_so_far = 0;
94
95         for (std::vector<PortVec>::const_iterator l = _ports.begin(); l != _ports.end(); ++l) {
96                 if (n < size_so_far + (*l).size())
97                         return (*l)[n - size_so_far];
98                 else
99                         size_so_far += (*l).size();
100         }
101
102         return NULL; // n out of range
103 }
104
105 Port*
106 PortSet::port(DataType type, size_t n) const
107 {
108         if (type == DataType::NIL) {
109                 return port(n);
110         } else {
111                 const PortVec& v = _ports[type.to_index()];
112                 assert(n < v.size());
113                 return v[n];
114         }
115 }
116
117 AudioPort*
118 PortSet::nth_audio_port(size_t n) const
119 {
120         return dynamic_cast<AudioPort*>(port(DataType::AUDIO, n));
121 }
122
123 MidiPort*
124 PortSet::nth_midi_port(size_t n) const
125 {
126         return dynamic_cast<MidiPort*>(port(DataType::MIDI, n));
127 }
128
129 } // namepace ARDOUR