part 2 of 3 of the 2.8 -> 3.0 merge
[ardour.git] / libs / ardour / ardour / chan_count.h
1 /*
2     Copyright (C) 2006 Paul Davis 
3         Author: Dave 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 <ostream>
24
25 #include "ardour/data_type.h"
26 #include <cassert>
27
28 namespace ARDOUR {
29
30
31 /** A count of channels, possibly with many types.
32  *
33  * Operators are defined so this may safely be used as if it were a simple
34  * (single-typed) integer count of channels.
35  */
36 class ChanCount {
37 public:
38         ChanCount() { reset(); }
39         
40         // Convenience constructor for making single-typed streams (stereo, mono, etc)
41         ChanCount(DataType type, uint32_t channels) {
42                 reset();
43                 set(type, channels);
44         }
45
46         void reset() {
47                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
48                         _counts[*t] = 0;
49                 }
50         }
51         
52         void     set(DataType t, uint32_t count) { assert(t != DataType::NIL); _counts[t] = count; }
53         uint32_t get(DataType t) const { assert(t != DataType::NIL); return _counts[t]; }
54         
55         inline uint32_t n_audio() const { return _counts[DataType::AUDIO]; }
56         inline void set_audio(uint32_t a) { _counts[DataType::AUDIO] = a; }
57         
58         inline uint32_t n_midi()  const { return _counts[DataType::MIDI]; }
59         inline void set_midi(uint32_t m) { _counts[DataType::MIDI] = m; }
60         
61         uint32_t n_total() const {
62                 uint32_t ret = 0;
63                 for (uint32_t i=0; i < DataType::num_types; ++i)
64                         ret += _counts[i];
65
66                 return ret;
67         }
68
69         bool operator==(const ChanCount& other) const {
70                 for (uint32_t i=0; i < DataType::num_types; ++i)
71                         if (_counts[i] != other._counts[i])
72                                 return false;
73
74                 return true;
75         }
76         
77         bool operator!=(const ChanCount& other) const {
78                 return ! (*this == other);
79         }
80
81         bool operator<(const ChanCount& other) const {
82                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
83                         if (_counts[*t] > other._counts[*t]) {
84                                 return false;
85                         }
86                 }
87                 return (*this != other);
88         }
89
90         bool operator<=(const ChanCount& other) const {
91                 return ( (*this < other) || (*this == other) );
92         }
93         
94         bool operator>(const ChanCount& other) const {
95                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
96                         if (_counts[*t] < other._counts[*t]) {
97                                 return false;
98                         }
99                 }
100                 return (*this != other);
101         }
102
103         bool operator>=(const ChanCount& other) const {
104                 return ( (*this > other) || (*this == other) );
105         }
106
107         static const ChanCount INFINITE;
108         static const ChanCount ZERO;
109
110 private:
111         uint32_t _counts[DataType::num_types];
112 };
113
114 } // namespace ARDOUR
115
116 std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanCount& c);
117
118 #endif // __ardour_chan_count_h__
119