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