ardour
port_set.h
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 #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 
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 
53  boost::shared_ptr<Port> port(size_t index) const;
54 
56  boost::shared_ptr<Port> port(DataType t, size_t index) const;
57 
58  boost::shared_ptr<AudioPort> nth_audio_port(size_t n) const;
59 
60  boost::shared_ptr<MidiPort> nth_midi_port(size_t n) const;
61 
62  bool contains (boost::shared_ptr<const Port> port) const;
63 
67  void clear();
68 
69  const ChanCount& count() const { return _count; }
70 
71  bool empty() const { return (_count.n_total() == 0); }
72 
73  template<typename PS, typename P>
74  class iterator_base {
75  public:
76  boost::shared_ptr<P> operator*() { return _set.port(_type, _index); }
77  boost::shared_ptr<P> operator->() { return _set.port(_type, _index); }
78  iterator_base<PS,P>& operator++() { ++_index; return *this; } // yes, prefix only
79  bool operator==(const iterator_base<PS,P>& other) { return (_index == other._index); }
80  bool operator!=(const iterator_base<PS,P>& other) { return (_index != other._index); }
81 
82  private:
83  friend class PortSet;
84 
85  iterator_base<PS,P>(PS& list, DataType type, size_t index)
86  : _set(list), _type(type), _index(index) {}
87 
88  PS& _set;
90  size_t _index;
91  };
92 
95 
96  iterator begin(DataType type = DataType::NIL) {
97  return iterator(*this, type, 0);
98  }
99 
100  iterator end(DataType type = DataType::NIL) {
101  return iterator(*this, type,
102  (type == DataType::NIL) ? _count.n_total() : _count.get(type));
103  }
104 
105  const_iterator begin(DataType type = DataType::NIL) const {
106  return const_iterator(*this, type, 0);
107  }
108 
109  const_iterator end(DataType type = DataType::NIL) const {
110  return const_iterator(*this, type,
111  (type == DataType::NIL) ? _count.n_total() : _count.get(type));
112  }
113 
115  public:
116  boost::shared_ptr<AudioPort> operator*() { return _set.nth_audio_port(_index); }
117  boost::shared_ptr<AudioPort> operator->() { return _set.nth_audio_port(_index); }
118  audio_iterator& operator++() { ++_index; return *this; } // yes, prefix only
119  bool operator==(const audio_iterator& other) { return (_index == other._index); }
120  bool operator!=(const audio_iterator& other) { return (_index != other._index); }
121 
122  private:
123  friend class PortSet;
124 
125  audio_iterator(PortSet& list, size_t index) : _set(list), _index(index) {}
126 
128  size_t _index;
129  };
130 
131  audio_iterator audio_begin() { return audio_iterator(*this, 0); }
132  audio_iterator audio_end() { return audio_iterator(*this, _count.n_audio()); }
133 
134 private:
135  typedef std::vector<boost::shared_ptr<Port> > PortVec;
136 
137  // Vector of vectors, indexed by DataType::to_index()
138  std::vector<PortVec> _ports;
139  // All ports in _ports in one vector, to speed some operations
140  PortVec _all_ports;
141 
143 };
144 
145 
146 } // namespace ARDOUR
147 
148 #endif // __ardour_port_set_h__
boost::shared_ptr< P > operator*()
Definition: port_set.h:76
iterator_base< PS, P > & operator++()
Definition: port_set.h:78
iterator end(DataType type=DataType::NIL)
Definition: port_set.h:100
boost::shared_ptr< AudioPort > operator*()
Definition: port_set.h:116
std::vector< boost::shared_ptr< Port > > PortVec
Definition: port_set.h:135
const_iterator end(DataType type=DataType::NIL) const
Definition: port_set.h:109
bool operator==(const iterator_base< PS, P > &other)
Definition: port_set.h:79
boost::shared_ptr< P > operator->()
Definition: port_set.h:77
audio_iterator & operator++()
Definition: port_set.h:118
audio_iterator audio_end()
Definition: port_set.h:132
audio_iterator audio_begin()
Definition: port_set.h:131
size_t num_ports(DataType type) const
Definition: port_set.h:47
bool operator==(const audio_iterator &other)
Definition: port_set.h:119
audio_iterator(PortSet &list, size_t index)
Definition: port_set.h:125
bool empty() const
Definition: port_set.h:71
Definition: amp.h:29
bool operator!=(const iterator_base< PS, P > &other)
Definition: port_set.h:80
PortVec _all_ports
Definition: port_set.h:140
boost::shared_ptr< AudioPort > operator->()
Definition: port_set.h:117
#define LIBARDOUR_API
iterator_base< PortSet, Port > iterator
Definition: port_set.h:93
DataType _type
Ignored if NIL (to iterator over entire set)
Definition: port_set.h:89
bool operator!=(const audio_iterator &other)
Definition: port_set.h:120
iterator begin(DataType type=DataType::NIL)
Definition: port_set.h:96
ChanCount _count
Definition: port_set.h:142
iterator_base< const PortSet, const Port > const_iterator
Definition: port_set.h:94
const ChanCount & count() const
Definition: port_set.h:69
std::vector< PortVec > _ports
Definition: port_set.h:138
const_iterator begin(DataType type=DataType::NIL) const
Definition: port_set.h:105