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