ardour
port_set.cc
Go to the documentation of this file.
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 
31 {
32  for (size_t i=0; i < DataType::num_types; ++i)
33  _ports.push_back( PortVec() );
34 }
35 
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 
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
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);
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
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 
122 size_t
124 {
125  return _all_ports.size();
126 }
127 
128 bool
130 {
131  return find(_all_ports.begin(), _all_ports.end(), port) != _all_ports.end();
132 }
133 
135 PortSet::port(size_t n) const
136 {
137  assert(n < _all_ports.size());
138  return _all_ports[n];
139 }
140 
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 
156 PortSet::nth_audio_port(size_t n) const
157 {
159 }
160 
162 PortSet::nth_midi_port(size_t n) const
163 {
165 }
166 
167 void
169 {
170  _ports.clear();
171  _all_ports.clear();
172 }
173 
174 } // namepace ARDOUR
virtual DataType type() const =0
boost::shared_ptr< MidiPort > nth_midi_port(size_t n) const
Definition: port_set.cc:162
int atoi(const string &s)
Definition: convert.cc:140
shared_ptr< T > dynamic_pointer_cast(shared_ptr< U > const &r)
Definition: shared_ptr.hpp:396
std::vector< boost::shared_ptr< Port > > PortVec
Definition: port_set.h:135
std::string name() const
Definition: port.h:54
void add(boost::shared_ptr< Port > port)
Definition: port_set.cc:86
size_t num_ports() const
Definition: port_set.cc:123
static bool sort_ports_by_type_and_name(boost::shared_ptr< Port > a, boost::shared_ptr< Port > b)
Definition: port_set.cc:76
Definition: amp.h:29
static const uint32_t num_types
Definition: data_type.h:58
PortVec _all_ports
Definition: port_set.h:140
bool remove(boost::shared_ptr< Port > port)
Definition: port_set.cc:101
uint32_t get(DataType t) const
Definition: chan_count.h:59
bool contains(boost::shared_ptr< const Port > port) const
Definition: port_set.cc:129
void set(DataType t, uint32_t count)
Definition: chan_count.h:58
boost::shared_ptr< Port > port(size_t index) const
Definition: port_set.cc:135
boost::shared_ptr< AudioPort > nth_audio_port(size_t n) const
Definition: port_set.cc:156
static bool sort_ports_by_name(boost::shared_ptr< Port > a, boost::shared_ptr< Port > b)
Definition: port_set.cc:36
ChanCount _count
Definition: port_set.h:142
std::vector< PortVec > _ports
Definition: port_set.h:138