Optimize automation-event process splitting
[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 namespace ARDOUR {
30
31
32 /** A count of channels, possibly with many types.
33  *
34  * Operators are defined so this may safely be used as if it were a simple
35  * (single-typed) integer count of channels.
36  */
37 class LIBARDOUR_API ChanCount {
38 public:
39         ChanCount(const XMLNode& node);
40         ChanCount() { reset(); }
41
42         /** Convenience constructor for making single-typed streams (mono, stereo, midi, etc)
43          * @param type data type
44          * @param count number of channels
45          */
46         ChanCount(DataType type, uint32_t count) {
47                 reset();
48                 set(type, count);
49         }
50
51         /** zero count of all data types */
52         void reset() {
53                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
54                         _counts[*t] = 0;
55                 }
56         }
57
58         /** set channel count for given type
59          * @param type data type
60          * @param count number of channels
61          */
62         void     set(DataType t, uint32_t count) { assert(t != DataType::NIL); _counts[t] = count; }
63         /** query channel count for given type
64          * @param type data type
65          * @returns channel count for given type
66          */
67         uint32_t get(DataType t) const { assert(t != DataType::NIL); return _counts[t]; }
68
69         inline uint32_t n (DataType t) const { return _counts[t]; }
70
71         /** query number of audio channels
72          * @returns number of audio channels
73          */
74         inline uint32_t n_audio() const { return _counts[DataType::AUDIO]; }
75         /** set number of audio channels
76          * @param a number of audio channels
77          */
78         inline void set_audio(uint32_t a) { _counts[DataType::AUDIO] = a; }
79
80         /** query number of midi channels
81          * @returns number of midi channels
82          */
83         inline uint32_t n_midi()  const { return _counts[DataType::MIDI]; }
84         /** set number of audio channels
85          * @param m number of midi channels
86          */
87         inline void set_midi(uint32_t m) { _counts[DataType::MIDI] = m; }
88
89         /** query total channel count of all data types
90          * @returns total channel count (audio + midi)
91          */
92         uint32_t n_total() const {
93                 uint32_t ret = 0;
94                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t)
95                         ret += _counts[*t];
96
97                 return ret;
98         }
99
100         bool operator==(const ChanCount& other) const {
101                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t)
102                         if (_counts[*t] != other._counts[*t])
103                                 return false;
104
105                 return true;
106         }
107
108         bool operator!=(const ChanCount& other) const {
109                 return ! (*this == other);
110         }
111
112         bool operator<(const ChanCount& other) const {
113                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
114                         if (_counts[*t] > other._counts[*t]) {
115                                 return false;
116                         }
117                 }
118                 return (*this != other);
119         }
120
121         bool operator<=(const ChanCount& other) const {
122                 return ( (*this < other) || (*this == other) );
123         }
124
125         bool operator>(const ChanCount& other) const {
126                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
127                         if (_counts[*t] < other._counts[*t]) {
128                                 return false;
129                         }
130                 }
131                 return (*this != other);
132         }
133
134         bool operator>=(const ChanCount& other) const {
135                 return ( (*this > other) || (*this == other) );
136         }
137
138         ChanCount operator+(const ChanCount& other) const {
139                 ChanCount ret;
140                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
141                         ret.set(*t, get(*t) + other.get(*t));
142                 }
143                 return ret;
144         }
145
146         /** underflow safe subtraction */
147         ChanCount operator-(const ChanCount& other) const {
148                 ChanCount ret;
149                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
150                         if (get(*t) < other.get(*t)) {
151                                 ret.set(*t, 0);
152                         } else {
153                                 ret.set(*t, get(*t) - other.get(*t));
154                         }
155                 }
156                 return ret;
157         }
158
159         ChanCount operator*(const unsigned int factor) const {
160                 ChanCount ret;
161                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
162                         ret.set(*t, get(*t) * factor );
163                 }
164                 return ret;
165         }
166
167         /** underflow safe subtraction */
168         ChanCount& operator-=(const ChanCount& other) {
169                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
170                         if (_counts[*t] < other._counts[*t]) {
171                                 _counts[*t] = 0;
172                         } else {
173                                 _counts[*t] -= other._counts[*t];
174                         }
175                 }
176                 return *this;
177         }
178
179         ChanCount& operator+=(const ChanCount& other) {
180                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
181                         _counts[*t] += other._counts[*t];
182                 }
183                 return *this;
184         }
185
186         static ChanCount min(const ChanCount& a, const ChanCount& b) {
187                 ChanCount ret;
188                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
189                         ret.set(*t, std::min(a.get(*t), b.get(*t)));
190                 }
191                 return ret;
192         }
193
194         static ChanCount max(const ChanCount& a, const ChanCount& b) {
195                 ChanCount ret;
196                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
197                         ret.set(*t, std::max(a.get(*t), b.get(*t)));
198                 }
199                 return ret;
200         }
201
202         XMLNode* state(const std::string& name) const;
203
204         static const ChanCount ZERO;
205
206 private:
207         uint32_t _counts[DataType::num_types];
208 };
209
210 } // namespace ARDOUR
211
212 LIBARDOUR_API std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanCount& c);
213
214 #endif // __ardour_chan_count_h__
215