Ardour  9.0-pre0-350-gf17a656217
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 #pragma once
22 
23 #include <vector>
24 #include "ardour/chan_count.h"
25 namespace ARDOUR {
26 
27 class Port;
28 class AudioPort;
29 class MidiPort;
30 
42 public:
44 
45  size_t num_ports() const;
46  size_t num_ports(DataType type) const { return _ports[type].size(); }
47 
48  void add (std::shared_ptr<Port> port);
49  bool remove (std::shared_ptr<Port> port);
50 
54  std::shared_ptr<Port> port(size_t index) const;
55 
60  std::shared_ptr<Port> port(DataType t, size_t index) const;
61 
62  std::shared_ptr<AudioPort> nth_audio_port(size_t n) const;
63 
64  std::shared_ptr<MidiPort> nth_midi_port(size_t n) const;
65 
66  bool contains (std::shared_ptr<const Port> port) const;
67 
71  void clear();
72 
73  const ChanCount& count() const { return _count; }
74 
75  bool empty() const { return (_count.n_total() == 0); }
76 
77  template<typename PS, typename P>
78  class iterator_base {
79  public:
80  std::shared_ptr<P> operator*() { return _set.port(_type, _index); }
81  std::shared_ptr<P> operator->() { return _set.port(_type, _index); }
82  iterator_base<PS,P>& operator++() { ++_index; return *this; } // yes, prefix only
83  bool operator==(const iterator_base<PS,P>& other) { return (_index == other._index); }
84  bool operator!=(const iterator_base<PS,P>& other) { return (_index != other._index); }
85 
86  private:
87  friend class PortSet;
88 
89  iterator_base (PS& list, DataType type, size_t index)
90  : _set (list)
91  , _type (type)
92  , _index (index)
93  {
94  }
95 
96  PS& _set;
98  size_t _index;
99  };
100 
103 
105  return iterator(*this, type, 0);
106  }
107 
109  return iterator(*this, type,
110  (type == DataType::NIL) ? _count.n_total() : _count.get(type));
111  }
112 
114  return const_iterator(*this, type, 0);
115  }
116 
118  return const_iterator(*this, type,
119  (type == DataType::NIL) ? _count.n_total() : _count.get(type));
120  }
121 
123  public:
124  std::shared_ptr<AudioPort> operator*() { return _set.nth_audio_port(_index); }
125  std::shared_ptr<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 
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<std::shared_ptr<Port> > PortVec;
144 
145  // Vector of vectors, indexed by DataType::to_index()
146  std::vector<PortVec> _ports;
147  // All ports in _ports in one vector, to speed some operations
149 
151 };
152 
153 
154 } // namespace ARDOUR
155 
std::shared_ptr< AudioPort > operator->()
Definition: port_set.h:125
std::shared_ptr< AudioPort > operator*()
Definition: port_set.h:124
audio_iterator(PortSet &list, size_t index)
Definition: port_set.h:133
bool operator!=(const audio_iterator &other)
Definition: port_set.h:128
audio_iterator & operator++()
Definition: port_set.h:126
bool operator==(const audio_iterator &other)
Definition: port_set.h:127
DataType _type
Ignored if NIL (to iterator over entire set)
Definition: port_set.h:97
bool operator!=(const iterator_base< PS, P > &other)
Definition: port_set.h:84
std::shared_ptr< P > operator->()
Definition: port_set.h:81
std::shared_ptr< P > operator*()
Definition: port_set.h:80
iterator_base(PS &list, DataType type, size_t index)
Definition: port_set.h:89
iterator_base< PS, P > & operator++()
Definition: port_set.h:82
bool operator==(const iterator_base< PS, P > &other)
Definition: port_set.h:83
iterator begin(DataType type=DataType::NIL)
Definition: port_set.h:104
size_t num_ports() const
bool remove(std::shared_ptr< Port > port)
std::vector< PortVec > _ports
Definition: port_set.h:146
iterator_base< const PortSet, const Port > const_iterator
Definition: port_set.h:102
size_t num_ports(DataType type) const
Definition: port_set.h:46
audio_iterator audio_begin()
Definition: port_set.h:139
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:143
iterator end(DataType type=DataType::NIL)
Definition: port_set.h:108
std::shared_ptr< Port > port(size_t index) const
const_iterator end(DataType type=DataType::NIL) const
Definition: port_set.h:117
const_iterator begin(DataType type=DataType::NIL) const
Definition: port_set.h:113
bool contains(std::shared_ptr< const Port > port) const
bool empty() const
Definition: port_set.h:75
const ChanCount & count() const
Definition: port_set.h:73
ChanCount _count
Definition: port_set.h:150
std::shared_ptr< Port > port(DataType t, size_t index) const
iterator_base< PortSet, Port > iterator
Definition: port_set.h:101
PortVec _all_ports
Definition: port_set.h:148
audio_iterator audio_end()
Definition: port_set.h:140
#define LIBARDOUR_API