Fix a few hundred doxygen warnings..
[ardour.git] / libs / ardour / ardour / chan_count.h
1 /*
2  * Copyright (C) 2006-2011 David Robillard <d@drobilla.net>
3  * Copyright (C) 2008-2013 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2013-2015 John Emmas <john@creativepost.co.uk>
5  * Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifndef __ardour_chan_count_h__
23 #define __ardour_chan_count_h__
24
25 #include <cassert>
26 #include <ostream>
27
28 #include "pbd/xml++.h"
29 #include "ardour/data_type.h"
30
31 namespace ARDOUR {
32
33
34 /** A count of channels, possibly with many types.
35  *
36  * Operators are defined so this may safely be used as if it were a simple
37  * (single-typed) integer count of channels.
38  */
39 class LIBARDOUR_API ChanCount {
40 public:
41         ChanCount(const XMLNode& node);
42         ChanCount() { reset(); }
43
44         /** Convenience constructor for making single-typed streams (mono, stereo, midi, etc)
45          * @param type data type
46          * @param count number of channels
47          */
48         ChanCount(DataType type, uint32_t count) {
49                 reset();
50                 set(type, count);
51         }
52
53         /** zero count of all data types */
54         void reset() {
55                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
56                         _counts[*t] = 0;
57                 }
58         }
59
60         /** set channel count for given type
61          * @param t data type
62          * @param count number of channels
63          */
64         void     set(DataType t, uint32_t count) { assert(t != DataType::NIL); _counts[t] = count; }
65
66         /** query channel count for given type
67          * @param t data type
68          * @returns channel count for given type
69          */
70         uint32_t get(DataType t) const { assert(t != DataType::NIL); return _counts[t]; }
71
72         inline uint32_t n (DataType t) const { return _counts[t]; }
73
74         /** query number of audio channels
75          * @returns number of audio channels
76          */
77         inline uint32_t n_audio() const { return _counts[DataType::AUDIO]; }
78
79         /** set number of audio channels
80          * @param a number of audio channels
81          */
82         inline void set_audio(uint32_t a) { _counts[DataType::AUDIO] = a; }
83
84         /** query number of midi channels
85          * @returns number of midi channels
86          */
87         inline uint32_t n_midi()  const { return _counts[DataType::MIDI]; }
88
89         /** set number of audio channels
90          * @param m number of midi channels
91          */
92         inline void set_midi(uint32_t m) { _counts[DataType::MIDI] = m; }
93
94         /** query total channel count of all data types
95          * @returns total channel count (audio + midi)
96          */
97         uint32_t n_total() const {
98                 uint32_t ret = 0;
99                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t)
100                         ret += _counts[*t];
101
102                 return ret;
103         }
104
105         bool operator==(const ChanCount& other) const {
106                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t)
107                         if (_counts[*t] != other._counts[*t])
108                                 return false;
109
110                 return true;
111         }
112
113         bool operator!=(const ChanCount& other) const {
114                 return ! (*this == other);
115         }
116
117         bool operator<(const ChanCount& other) const {
118                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
119                         if (_counts[*t] > other._counts[*t]) {
120                                 return false;
121                         }
122                 }
123                 return (*this != other);
124         }
125
126         bool operator<=(const ChanCount& other) const {
127                 return ( (*this < other) || (*this == other) );
128         }
129
130         bool operator>(const ChanCount& other) const {
131                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
132                         if (_counts[*t] < other._counts[*t]) {
133                                 return false;
134                         }
135                 }
136                 return (*this != other);
137         }
138
139         bool operator>=(const ChanCount& other) const {
140                 return ( (*this > other) || (*this == other) );
141         }
142
143         ChanCount operator+(const ChanCount& other) const {
144                 ChanCount ret;
145                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
146                         ret.set(*t, get(*t) + other.get(*t));
147                 }
148                 return ret;
149         }
150
151         /** underflow safe subtraction */
152         ChanCount operator-(const ChanCount& other) const {
153                 ChanCount ret;
154                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
155                         if (get(*t) < other.get(*t)) {
156                                 ret.set(*t, 0);
157                         } else {
158                                 ret.set(*t, get(*t) - other.get(*t));
159                         }
160                 }
161                 return ret;
162         }
163
164         ChanCount operator*(const unsigned int factor) const {
165                 ChanCount ret;
166                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
167                         ret.set(*t, get(*t) * factor );
168                 }
169                 return ret;
170         }
171
172         /** underflow safe subtraction */
173         ChanCount& operator-=(const ChanCount& other) {
174                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
175                         if (_counts[*t] < other._counts[*t]) {
176                                 _counts[*t] = 0;
177                         } else {
178                                 _counts[*t] -= other._counts[*t];
179                         }
180                 }
181                 return *this;
182         }
183
184         ChanCount& operator+=(const ChanCount& other) {
185                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
186                         _counts[*t] += other._counts[*t];
187                 }
188                 return *this;
189         }
190
191         static ChanCount min(const ChanCount& a, const ChanCount& b) {
192                 ChanCount ret;
193                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
194                         ret.set(*t, std::min(a.get(*t), b.get(*t)));
195                 }
196                 return ret;
197         }
198
199         static ChanCount max(const ChanCount& a, const ChanCount& b) {
200                 ChanCount ret;
201                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
202                         ret.set(*t, std::max(a.get(*t), b.get(*t)));
203                 }
204                 return ret;
205         }
206
207         XMLNode* state(const std::string& name) const;
208
209         static const ChanCount ZERO;
210
211 private:
212         uint32_t _counts[DataType::num_types];
213 };
214
215 } // namespace ARDOUR
216
217 LIBARDOUR_API std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanCount& c);
218
219 #endif // __ardour_chan_count_h__
220