Ardour  8.12
port_set.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2009 David Robillard <d@drobilla.net>
3  * Copyright (C) 2008-2013 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #ifndef __ardour_port_set_h__
22 #define __ardour_port_set_h__
23 
24 #include <vector>
25 #include "ardour/chan_count.h"
26 namespace ARDOUR {
27 
28 class Port;
29 class AudioPort;
30 class MidiPort;
31 
43 public:
45 
46  size_t num_ports() const;
47  size_t num_ports(DataType type) const { return _ports[type].size(); }
48 
49  void add (std::shared_ptr<Port> port);
50  bool remove (std::shared_ptr<Port> port);
51 
55  std::shared_ptr<Port> port(size_t index) const;
56 
61  std::shared_ptr<Port> port(DataType t, size_t index) const;
62 
63  std::shared_ptr<AudioPort> nth_audio_port(size_t n) const;
64 
65  std::shared_ptr<MidiPort> nth_midi_port(size_t n) const;
66 
67  bool contains (std::shared_ptr<const Port> port) const;
68 
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  std::shared_ptr<P> operator*() { return _set.port(_type, _index); }
82  std::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& list, DataType type, size_t index)
91  : _set (list)
92  , _type (type)
93  , _index (index)
94  {
95  }
96 
97  PS& _set;
99  size_t _index;
100  };
101 
104 
106  return iterator(*this, type, 0);
107  }
108 
110  return iterator(*this, type,
111  (type == DataType::NIL) ? _count.n_total() : _count.get(type));
112  }
113 
115  return const_iterator(*this, type, 0);
116  }
117 
119  return const_iterator(*this, type,
120  (type == DataType::NIL) ? _count.n_total() : _count.get(type));
121  }
122 
124  public:
125  std::shared_ptr<AudioPort> operator*() { return _set.nth_audio_port(_index); }
126  std::shared_ptr<AudioPort> operator->() { return _set.nth_audio_port(_index); }
127  audio_iterator& operator++() { ++_index; return *this; } // yes, prefix only
128  bool operator==(const audio_iterator& other) { return (_index == other._index); }
129  bool operator!=(const audio_iterator& other) { return (_index != other._index); }
130 
131  private:
132  friend class PortSet;
133 
134  audio_iterator(PortSet& list, size_t index) : _set(list), _index(index) {}
135 
137  size_t _index;
138  };
139 
140  audio_iterator audio_begin() { return audio_iterator(*this, 0); }
141  audio_iterator audio_end() { return audio_iterator(*this, _count.n_audio()); }
142 
143 private:
144  typedef std::vector<std::shared_ptr<Port> > PortVec;
145 
146  // Vector of vectors, indexed by DataType::to_index()
147  std::vector<PortVec> _ports;
148  // All ports in _ports in one vector, to speed some operations
150 
152 };
153 
154 
155 } // namespace ARDOUR
156 
157 #endif // __ardour_port_set_h__
std::shared_ptr< AudioPort > operator->()
Definition: port_set.h:126
std::shared_ptr< AudioPort > operator*()
Definition: port_set.h:125
audio_iterator(PortSet &list, size_t index)
Definition: port_set.h:134
bool operator!=(const audio_iterator &other)
Definition: port_set.h:129
audio_iterator & operator++()
Definition: port_set.h:127
bool operator==(const audio_iterator &other)
Definition: port_set.h:128
DataType _type
Ignored if NIL (to iterator over entire set)
Definition: port_set.h:98
bool operator!=(const iterator_base< PS, P > &other)
Definition: port_set.h:85
std::shared_ptr< P > operator->()
Definition: port_set.h:82
std::shared_ptr< P > operator*()
Definition: port_set.h:81
iterator_base(PS &list, DataType type, size_t index)
Definition: port_set.h:90
iterator_base< PS, P > & operator++()
Definition: port_set.h:83
bool operator==(const iterator_base< PS, P > &other)
Definition: port_set.h:84
iterator begin(DataType type=DataType::NIL)
Definition: port_set.h:105
size_t num_ports() const
bool remove(std::shared_ptr< Port > port)
std::vector< PortVec > _ports
Definition: port_set.h:147
iterator_base< const PortSet, const Port > const_iterator
Definition: port_set.h:103
size_t num_ports(DataType type) const
Definition: port_set.h:47
audio_iterator audio_begin()
Definition: port_set.h:140
std::shared_ptr< AudioPort > nth_audio_port(size_t n) const
std::shared_ptr< MidiPort > nth_midi_port(size_t n) const
void add(std::shared_ptr< Port > port)
std::vector< std::shared_ptr< Port > > PortVec
Definition: port_set.h:144
iterator end(DataType type=DataType::NIL)
Definition: port_set.h:109
std::shared_ptr< Port > port(size_t index) const
const_iterator end(DataType type=DataType::NIL) const
Definition: port_set.h:118
const_iterator begin(DataType type=DataType::NIL) const
Definition: port_set.h:114
bool contains(std::shared_ptr< const Port > port) const
bool empty() const
Definition: port_set.h:76
const ChanCount & count() const
Definition: port_set.h:74
ChanCount _count
Definition: port_set.h:151
std::shared_ptr< Port > port(DataType t, size_t index) const
iterator_base< PortSet, Port > iterator
Definition: port_set.h:102
PortVec _all_ports
Definition: port_set.h:149
audio_iterator audio_end()
Definition: port_set.h:141
#define LIBARDOUR_API