*** NEW CODING POLICY ***
[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 class PortSet : public boost::noncopyable {
39 public:
40         PortSet();
41
42         size_t num_ports() const;
43         size_t num_ports(DataType type) const { return _ports[type].size(); }
44
45         void add(Port* port);
46         bool remove(Port* port);
47
48         /** nth port */
49         Port* port(size_t index) const;
50         
51         /** nth port of type @a t, or nth port if t = NIL */
52         Port* port(DataType t, size_t index) const;
53
54         AudioPort* nth_audio_port(size_t n) const;
55         
56         MidiPort* nth_midi_port(size_t n) const;
57
58         bool contains(const Port* port) const;
59         
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!
62          */
63         void clear() { _ports.clear(); }
64
65         const ChanCount& count() const { return _count; }
66
67         bool empty() const { return (_count.n_total() == 0); }
68
69         // ITERATORS
70         // FIXME: possible to combine these?  templates?
71         
72         class iterator {
73         public:
74                 Port& operator*()  { return *_set.port(_type, _index); }
75                 Port* operator->() { return _set.port(_type, _index); }
76                 iterator& operator++() { ++_index; return *this; } // yes, prefix only
77                 bool operator==(const iterator& other) { return (_index == other._index); }
78                 bool operator!=(const iterator& other) { return (_index != other._index); }
79
80         private:
81                 friend class PortSet;
82
83                 iterator(PortSet& list, DataType type, size_t index)
84                         : _set(list), _type(type), _index(index) {}
85
86                 PortSet& _set;
87                 DataType _type; ///< Ignored if NIL (to iterator over entire set)
88                 size_t   _index;
89         };
90
91         iterator begin(DataType type = DataType::NIL) {
92                 return iterator(*this, type, 0);
93         }
94         
95         iterator end(DataType type = DataType::NIL) {
96                 return iterator(*this, type,
97                         (type == DataType::NIL) ? _count.n_total() : _count.get(type));
98         }
99         
100
101         class const_iterator {
102         public:
103                 const Port& operator*()  { return *_set.port(_index); }
104                 const Port* operator->() { return _set.port(_index); }
105                 const_iterator& operator++() { ++_index; return *this; } // yes, prefix only
106                 bool operator==(const const_iterator& other) { return (_index == other._index); }
107                 bool operator!=(const const_iterator& other) { return (_index != other._index); }
108
109         private:
110                 friend class PortSet;
111
112                 const_iterator(const PortSet& list, size_t index) : _set(list), _index(index) {}
113
114                 const PortSet& _set;
115                 size_t         _index;
116         };
117
118         const_iterator begin() const { return const_iterator(*this, 0); }
119         const_iterator end()   const { return const_iterator(*this, _count.n_total()); }
120
121
122         class audio_iterator {
123         public:
124                 AudioPort& operator*()  { return *_set.nth_audio_port(_index); }
125                 AudioPort* operator->()  { return _set.nth_audio_port(_index); }
126                 audio_iterator& operator++() { ++_index; return *this; } // yes, prefix only
127                 bool operator==(const audio_iterator& other) { return (_index == other._index); }
128                 bool operator!=(const audio_iterator& other) { return (_index != other._index); }
129
130         private:
131                 friend class PortSet;
132
133                 audio_iterator(PortSet& list, size_t index) : _set(list), _index(index) {}
134
135                 PortSet& _set;
136                 size_t   _index;
137         };
138
139         audio_iterator audio_begin() { return audio_iterator(*this, 0); }
140         audio_iterator audio_end()   { return audio_iterator(*this, _count.n_audio()); }
141
142 private:        
143         typedef std::vector<Port*> PortVec;
144         
145         // Vector of vectors, indexed by DataType::to_index()
146         std::vector<PortVec> _ports;
147
148         ChanCount _count;
149 };
150
151
152 } // namespace ARDOUR
153
154 #endif // __ardour_port_set_h__