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