add new sigc++2 directory
[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/port.h>
24 #include <ardour/audio_port.h>
25 #include <ardour/midi_port.h>
26 #include <ardour/chan_count.h>
27
28 namespace ARDOUR {
29
30
31 /** An ordered list of Ports, possibly of various types.
32  *
33  * This allows access to all the ports as a list, ignoring type, or accessing
34  * the nth port of a given type.  Note that port(n) and nth_audio_port(n) may
35  * NOT return the same port.
36  */
37 class PortSet {
38 public:
39         PortSet();
40
41         size_t num_ports() const;
42         size_t num_ports(DataType type) const { return _ports[type].size(); }
43
44         void add(Port* port);
45         bool remove(Port* port);
46
47         /** nth port */
48         Port* port(size_t index) const;
49         
50         /** nth port of type @a t, or nth port if t = NIL */
51         Port* port(DataType t, size_t index) const;
52
53         AudioPort* nth_audio_port(size_t n) const;
54         
55         MidiPort* nth_midi_port(size_t n) const;
56
57         bool contains(const Port* port) const;
58         
59         /** Remove all ports from the PortSet.  Ports are not deregistered with
60          * the engine, it's the caller's responsibility to not leak here!
61          */
62         void clear() { _ports.clear(); }
63
64         const ChanCount& count() const { return _count; }
65
66         bool empty() const { return (_count.n_total() == 0); }
67
68         // ITERATORS
69         
70         // FIXME: this is a filthy copy-and-paste mess
71         
72         class iterator {
73         public:
74
75                 Port& operator*()  { return *_set.port(_type, _index); }
76                 Port* operator->() { return _set.port(_type, _index); }
77                 iterator& operator++() { ++_index; return *this; } // yes, prefix only
78                 bool operator==(const iterator& other) { return (_index == other._index); }
79                 bool operator!=(const iterator& other) { return (_index != other._index); }
80
81         private:
82                 friend class PortSet;
83
84                 iterator(PortSet& list, DataType type, size_t index)
85                         : _set(list), _type(type), _index(index) {}
86
87                 PortSet& _set;
88                 DataType _type; ///< Ignored if NIL (to iterator over entire set)
89                 size_t   _index;
90         };
91
92         iterator begin(DataType type = DataType::NIL)
93                 { return iterator(*this, type, 0); }
94         
95         iterator end(DataType type = DataType::NIL)
96         {
97                 return iterator(*this, type,
98                         (type == DataType::NIL) ? _count.n_total() : _count.get(type));
99         }
100         
101         // FIXME: typeify
102         class const_iterator {
103         public:
104
105                 const Port& operator*()  { return *_set.port(_index); }
106                 const Port* operator->() { return _set.port(_index); }
107                 const_iterator& operator++() { ++_index; return *this; } // yes, prefix only
108                 bool operator==(const const_iterator& other) { return (_index == other._index); }
109                 bool operator!=(const const_iterator& other) { return (_index != other._index); }
110
111         private:
112                 friend class PortSet;
113
114                 const_iterator(const PortSet& list, size_t index) : _set(list), _index(index) {}
115
116                 const PortSet& _set;
117                 size_t          _index;
118         };
119
120         const_iterator begin() const { return const_iterator(*this, 0); }
121         const_iterator end()   const { return const_iterator(*this, _count.n_total()); }
122
123
124         class audio_iterator {
125         public:
126
127                 AudioPort& operator*()  { return *_set.nth_audio_port(_index); }
128                 AudioPort* operator->()  { return _set.nth_audio_port(_index); }
129                 audio_iterator& operator++() { ++_index; return *this; } // yes, prefix only
130                 bool operator==(const audio_iterator& other) { return (_index == other._index); }
131                 bool operator!=(const audio_iterator& other) { return (_index != other._index); }
132
133         private:
134                 friend class PortSet;
135
136                 audio_iterator(PortSet& list, size_t index) : _set(list), _index(index) {}
137
138                 PortSet& _set;
139                 size_t    _index;
140         };
141
142         audio_iterator audio_begin() { return audio_iterator(*this, 0); }
143         audio_iterator audio_end()   { return audio_iterator(*this, _count.n_audio()); }
144
145 private:        
146         // Prevent copies (undefined)
147         PortSet(const PortSet& copy);
148         void operator=(const PortSet& other);
149
150         typedef std::vector<Port*> PortVec;
151         
152         // Vector of vectors, indexed by DataType::to_index()
153         std::vector<PortVec> _ports;
154
155         ChanCount _count;
156 };
157
158
159 } // namespace ARDOUR
160
161 #endif // __ardour_port_set_h__