Normalize API rename part 2
[ardour.git] / libs / ardour / ardour / port_set.h
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 #ifndef __ardour_port_set_h__
20 #define __ardour_port_set_h__
21
22 #include <vector>
23 #include "ardour/chan_count.h"
24 #include <boost/utility.hpp>
25
26 namespace ARDOUR {
27
28 class Port;
29 class AudioPort;
30 class MidiPort;
31
32 /** An ordered list of Ports, possibly of various types.
33  *
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.
37  *
38  * Each port is held twice; once in a per-type vector of vectors (_ports)
39  * and once in a vector of all port (_all_ports).  This is to speed up the
40  * fairly common case of iterating over all ports.
41  */
42 class LIBARDOUR_API PortSet : public boost::noncopyable {
43 public:
44         PortSet();
45
46         size_t num_ports() const;
47         size_t num_ports(DataType type) const { return _ports[type].size(); }
48
49         void add (boost::shared_ptr<Port> port);
50         bool remove (boost::shared_ptr<Port> port);
51
52         /** nth port
53          * @param index port index
54          */
55         boost::shared_ptr<Port> port(size_t index) const;
56
57         /** nth port of type @a t, or nth port if t = NIL
58          * @param t data type
59          * @param index port index
60          */
61         boost::shared_ptr<Port> port(DataType t, size_t index) const;
62
63         boost::shared_ptr<AudioPort> nth_audio_port(size_t n) const;
64
65         boost::shared_ptr<MidiPort> nth_midi_port(size_t n) const;
66
67         bool contains (boost::shared_ptr<const Port> port) const;
68
69         /** Remove all ports from the PortSet.  Ports are not deregistered with
70          * the engine, it's the caller's responsibility to not leak here!
71          */
72         void clear();
73
74         const ChanCount& count() const { return _count; }
75
76         bool empty() const { return (_count.n_total() == 0); }
77
78         template<typename PS, typename P>
79         class iterator_base {
80         public:
81                 boost::shared_ptr<P> operator*()  { return _set.port(_type, _index); }
82                 boost::shared_ptr<P> operator->() { return _set.port(_type, _index); }
83                 iterator_base<PS,P>& operator++() { ++_index; return *this; } // yes, prefix only
84                 bool operator==(const iterator_base<PS,P>& other) { return (_index == other._index); }
85                 bool operator!=(const iterator_base<PS,P>& other) { return (_index != other._index); }
86
87         private:
88                 friend class PortSet;
89
90                 iterator_base<PS,P>(PS& list, DataType type, size_t index)
91                         : _set(list), _type(type), _index(index) {}
92
93                 PS&      _set;
94                 DataType _type; ///< Ignored if NIL (to iterator over entire set)
95                 size_t   _index;
96         };
97
98         typedef iterator_base<PortSet, Port>             iterator;
99         typedef iterator_base<const PortSet, const Port> const_iterator;
100
101         iterator begin(DataType type = DataType::NIL) {
102                 return iterator(*this, type, 0);
103         }
104
105         iterator end(DataType type = DataType::NIL) {
106                 return iterator(*this, type,
107                         (type == DataType::NIL) ? _count.n_total() : _count.get(type));
108         }
109
110         const_iterator begin(DataType type = DataType::NIL) const {
111                 return const_iterator(*this, type, 0);
112         }
113
114         const_iterator end(DataType type = DataType::NIL) const {
115                 return const_iterator(*this, type,
116                         (type == DataType::NIL) ? _count.n_total() : _count.get(type));
117         }
118
119         class audio_iterator {
120         public:
121                 boost::shared_ptr<AudioPort> operator*()  { return _set.nth_audio_port(_index); }
122                 boost::shared_ptr<AudioPort> operator->() { return _set.nth_audio_port(_index); }
123                 audio_iterator& operator++() { ++_index; return *this; } // yes, prefix only
124                 bool operator==(const audio_iterator& other) { return (_index == other._index); }
125                 bool operator!=(const audio_iterator& other) { return (_index != other._index); }
126
127         private:
128                 friend class PortSet;
129
130                 audio_iterator(PortSet& list, size_t index) : _set(list), _index(index) {}
131
132                 PortSet& _set;
133                 size_t   _index;
134         };
135
136         audio_iterator audio_begin() { return audio_iterator(*this, 0); }
137         audio_iterator audio_end()   { return audio_iterator(*this, _count.n_audio()); }
138
139 private:
140         typedef std::vector<boost::shared_ptr<Port> > PortVec;
141
142         // Vector of vectors, indexed by DataType::to_index()
143         std::vector<PortVec> _ports;
144         // All ports in _ports in one vector, to speed some operations
145         PortVec _all_ports;
146
147         ChanCount _count;
148 };
149
150
151 } // namespace ARDOUR
152
153 #endif // __ardour_port_set_h__