fix crash when copy'ing latent plugins
[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 <string>
20
21 #include "ardour/audio_port.h"
22 #include "ardour/midi_port.h"
23 #include "ardour/port.h"
24 #include "ardour/port_set.h"
25
26 using std::string;
27
28 namespace ARDOUR {
29
30 PortSet::PortSet()
31 {
32         for (size_t i=0; i < DataType::num_types; ++i)
33                 _ports.push_back( PortVec() );
34 }
35
36 static bool sort_ports_by_name (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
37 {
38         string aname (a->name());
39         string bname (b->name());
40
41         string::size_type last_digit_position_a = aname.size();
42         string::reverse_iterator r_iterator = aname.rbegin();
43
44         while (r_iterator!= aname.rend() && Glib::Unicode::isdigit(*r_iterator)) {
45                 r_iterator++;
46                 last_digit_position_a--;
47         }
48
49         string::size_type last_digit_position_b = bname.size();
50         r_iterator = bname.rbegin();
51
52         while (r_iterator != bname.rend() && Glib::Unicode::isdigit(*r_iterator)) {
53                 r_iterator++;
54                 last_digit_position_b--;
55         }
56
57         // if some of the names don't have a number as posfix, compare as strings
58
59         if (last_digit_position_a == aname.size() || last_digit_position_b == bname.size()) {
60                 return aname < bname;
61         }
62
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());
67
68         if (prefix_a != prefix_b) {
69                 return aname < bname;
70         } else {
71                 return posfix_a < posfix_b;
72         }
73 }
74
75
76 static bool sort_ports_by_type_and_name (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
77 {
78         if (a->type() != b->type()) {
79                 return a->type() < b->type();
80         }
81
82         return sort_ports_by_name (a, b);
83 }
84
85 void
86 PortSet::add (boost::shared_ptr<Port> port)
87 {
88         PortVec& v = _ports[port->type()];
89
90         v.push_back(port);
91         _all_ports.push_back(port);
92
93         sort(v.begin(), v.end(), sort_ports_by_name);
94         sort(_all_ports.begin(), _all_ports.end(), sort_ports_by_type_and_name);
95
96         _count.set(port->type(), _count.get(port->type()) + 1);
97         assert(_count.get(port->type()) == _ports[port->type()].size());
98 }
99
100 bool
101 PortSet::remove (boost::shared_ptr<Port> port)
102 {
103         PortVec::iterator i = find(_all_ports.begin(), _all_ports.end(), port);
104         if (i != _all_ports.end()) {
105                 _all_ports.erase(i);
106         }
107
108         for (std::vector<PortVec>::iterator l = _ports.begin(); l != _ports.end(); ++l) {
109                 PortVec::iterator i = find(l->begin(), l->end(), port);
110                 if (i != l->end()) {
111                         l->erase(i);
112                         _count.set(port->type(), _count.get(port->type()) - 1);
113                         return true;
114                 }
115         }
116
117         return false;
118 }
119
120 /** Get the total number of ports (of all types) in the PortSet
121  */
122 size_t
123 PortSet::num_ports() const
124 {
125         return _all_ports.size();
126 }
127
128 bool
129 PortSet::contains (boost::shared_ptr<const Port> port) const
130 {
131         return find(_all_ports.begin(), _all_ports.end(), port) != _all_ports.end();
132 }
133
134 boost::shared_ptr<Port>
135 PortSet::port(size_t n) const
136 {
137         assert(n < _all_ports.size());
138         return _all_ports[n];
139 }
140
141 boost::shared_ptr<Port>
142 PortSet::port(DataType type, size_t n) const
143 {
144         if (type == DataType::NIL) {
145                 return port(n);
146         } else {
147                 const PortVec& v = _ports[type];
148                 if (n < v.size()) {
149                         return v[n];
150                 }
151         }
152         return boost::shared_ptr<Port>();
153 }
154
155 boost::shared_ptr<AudioPort>
156 PortSet::nth_audio_port(size_t n) const
157 {
158         return boost::dynamic_pointer_cast<AudioPort> (port (DataType::AUDIO, n));
159 }
160
161 boost::shared_ptr<MidiPort>
162 PortSet::nth_midi_port(size_t n) const
163 {
164         return boost::dynamic_pointer_cast<MidiPort> (port (DataType::MIDI, n));
165 }
166
167 void
168 PortSet::clear()
169 {
170         _ports.clear();
171         _all_ports.clear();
172 }
173
174 } // namepace ARDOUR