closing in on pin management.
[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 (stereo, mono, etc)
47         ChanCount(DataType type, uint32_t channels) {
48                 reset();
49                 set(type, channels);
50         }
51
52         void reset() {
53                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
54                         _counts[*t] = 0;
55                 }
56         }
57
58         void     set(DataType t, uint32_t count) { assert(t != DataType::NIL); _counts[t] = count; }
59         uint32_t get(DataType t) const { assert(t != DataType::NIL); return _counts[t]; }
60
61         inline uint32_t n (DataType t) const { return _counts[t]; }
62
63         inline uint32_t n_audio() const { return _counts[DataType::AUDIO]; }
64         inline void set_audio(uint32_t a) { _counts[DataType::AUDIO] = a; }
65
66         inline uint32_t n_midi()  const { return _counts[DataType::MIDI]; }
67         inline void set_midi(uint32_t m) { _counts[DataType::MIDI] = m; }
68
69         uint32_t n_total() const {
70                 uint32_t ret = 0;
71                 for (uint32_t i=0; i < DataType::num_types; ++i)
72                         ret += _counts[i];
73
74                 return ret;
75         }
76
77         bool operator==(const ChanCount& other) const {
78                 for (uint32_t i=0; i < DataType::num_types; ++i)
79                         if (_counts[i] != other._counts[i])
80                                 return false;
81
82                 return true;
83         }
84
85         bool operator!=(const ChanCount& other) const {
86                 return ! (*this == other);
87         }
88
89         bool operator<(const ChanCount& other) const {
90                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
91                         if (_counts[*t] > other._counts[*t]) {
92                                 return false;
93                         }
94                 }
95                 return (*this != other);
96         }
97
98         bool operator<=(const ChanCount& other) const {
99                 return ( (*this < other) || (*this == other) );
100         }
101
102         bool operator>(const ChanCount& other) const {
103                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
104                         if (_counts[*t] < other._counts[*t]) {
105                                 return false;
106                         }
107                 }
108                 return (*this != other);
109         }
110
111         bool operator>=(const ChanCount& other) const {
112                 return ( (*this > other) || (*this == other) );
113         }
114
115         ChanCount operator+(const ChanCount& other) const {
116                 ChanCount ret;
117                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
118                         ret.set(*t, get(*t) + other.get(*t));
119                 }
120                 return ret;
121         }
122
123         ChanCount operator*(const unsigned int factor) const {
124                 ChanCount ret;
125                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
126                         ret.set(*t, get(*t) * factor );
127                 }
128                 return ret;
129         }
130
131         ChanCount& operator+=(const ChanCount& other) {
132                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
133                         _counts[*t] += other._counts[*t];
134                 }
135                 return *this;
136         }
137
138         static ChanCount min(const ChanCount& a, const ChanCount& b) {
139                 ChanCount ret;
140                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
141                         ret.set(*t, std::min(a.get(*t), b.get(*t)));
142                 }
143                 return ret;
144         }
145
146         static ChanCount max(const ChanCount& a, const ChanCount& b) {
147                 ChanCount ret;
148                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
149                         ret.set(*t, std::max(a.get(*t), b.get(*t)));
150                 }
151                 return ret;
152         }
153
154         XMLNode* state(const std::string& name) const;
155
156         static const ChanCount INFINITE;
157         static const ChanCount ZERO;
158
159 private:
160         uint32_t _counts[DataType::num_types];
161 };
162
163 } // namespace ARDOUR
164
165 LIBARDOUR_API std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanCount& c);
166
167 #endif // __ardour_chan_count_h__
168