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 #ifndef __ardour_port_set_h__
20 #define __ardour_port_set_h__
23 #include "ardour/chan_count.h"
24 #include <boost/utility.hpp>
32 /** An ordered list of Ports, possibly of various types.
34 * This allows access to all the ports as a list, ignoring type, or accessing
35 * the nth port of a given type. Note that port(n) and nth_audio_port(n) may
36 * NOT return the same port.
38 class PortSet : public boost::noncopyable {
42 size_t num_ports() const;
43 size_t num_ports(DataType type) const { return _ports[type].size(); }
45 void add (boost::shared_ptr<Port> port);
46 bool remove (boost::shared_ptr<Port> port);
49 boost::shared_ptr<Port> port(size_t index) const;
51 /** nth port of type @a t, or nth port if t = NIL */
52 boost::shared_ptr<Port> port(DataType t, size_t index) const;
54 boost::shared_ptr<AudioPort> nth_audio_port(size_t n) const;
56 boost::shared_ptr<MidiPort> nth_midi_port(size_t n) const;
58 bool contains (boost::shared_ptr<const Port> port) const;
60 /** Remove all ports from the PortSet. Ports are not deregistered with
61 * the engine, it's the caller's responsibility to not leak here!
63 void clear() { _ports.clear(); }
65 const ChanCount& count() const { return _count; }
67 bool empty() const { return (_count.n_total() == 0); }
69 template<typename PS, typename P>
72 boost::shared_ptr<P> operator*() { return _set.port(_type, _index); }
73 boost::shared_ptr<P> operator->() { return _set.port(_type, _index); }
74 iterator_base<PS,P>& operator++() { ++_index; return *this; } // yes, prefix only
75 bool operator==(const iterator_base<PS,P>& other) { return (_index == other._index); }
76 bool operator!=(const iterator_base<PS,P>& other) { return (_index != other._index); }
81 iterator_base<PS,P>(PS& list, DataType type, size_t index)
82 : _set(list), _type(type), _index(index) {}
85 DataType _type; ///< Ignored if NIL (to iterator over entire set)
89 typedef iterator_base<PortSet, Port> iterator;
90 typedef iterator_base<const PortSet, const Port> const_iterator;
92 iterator begin(DataType type = DataType::NIL) {
93 return iterator(*this, type, 0);
96 iterator end(DataType type = DataType::NIL) {
97 return iterator(*this, type,
98 (type == DataType::NIL) ? _count.n_total() : _count.get(type));
101 const_iterator begin(DataType type = DataType::NIL) const {
102 return const_iterator(*this, type, 0);
105 const_iterator end(DataType type = DataType::NIL) const {
106 return const_iterator(*this, type,
107 (type == DataType::NIL) ? _count.n_total() : _count.get(type));
110 class audio_iterator {
112 boost::shared_ptr<AudioPort> operator*() { return _set.nth_audio_port(_index); }
113 boost::shared_ptr<AudioPort> operator->() { return _set.nth_audio_port(_index); }
114 audio_iterator& operator++() { ++_index; return *this; } // yes, prefix only
115 bool operator==(const audio_iterator& other) { return (_index == other._index); }
116 bool operator!=(const audio_iterator& other) { return (_index != other._index); }
119 friend class PortSet;
121 audio_iterator(PortSet& list, size_t index) : _set(list), _index(index) {}
127 audio_iterator audio_begin() { return audio_iterator(*this, 0); }
128 audio_iterator audio_end() { return audio_iterator(*this, _count.n_audio()); }
131 typedef std::vector<boost::shared_ptr<Port> > PortVec;
133 // Vector of vectors, indexed by DataType::to_index()
134 std::vector<PortVec> _ports;
140 } // namespace ARDOUR
142 #endif // __ardour_port_set_h__