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