ardour
chan_count.h
Go to the documentation of this file.
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 
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 ChanCount& other) {
124  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
125  _counts[*t] += other._counts[*t];
126  }
127  return *this;
128  }
129 
130  static ChanCount min(const ChanCount& a, const ChanCount& b) {
131  ChanCount ret;
132  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
133  ret.set(*t, std::min(a.get(*t), b.get(*t)));
134  }
135  return ret;
136  }
137 
138  static ChanCount max(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::max(a.get(*t), b.get(*t)));
142  }
143  return ret;
144  }
145 
146  XMLNode* state(const std::string& name) const;
147 
148  static const ChanCount INFINITE;
149  static const ChanCount ZERO;
150 
151 private:
152  uint32_t _counts[DataType::num_types];
153 };
154 
155 } // namespace ARDOUR
156 
157 std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanCount& c);
158 
159 #endif // __ardour_chan_count_h__
160 
std::ostream & operator<<(std::ostream &o, const ARDOUR::ChanCount &c)
Definition: chan_count.cc:82
uint32_t n_audio() const
Definition: chan_count.h:63
bool operator>(const ChanCount &other) const
Definition: chan_count.h:102
bool operator!=(const ChanCount &other) const
Definition: chan_count.h:85
static ChanCount min(const ChanCount &a, const ChanCount &b)
Definition: chan_count.h:130
ChanCount(DataType type, uint32_t channels)
Definition: chan_count.h:47
uint32_t n_midi() const
Definition: chan_count.h:66
uint32_t n_total() const
Definition: chan_count.h:69
static iterator end()
Definition: data_type.h:109
bool operator==(const ChanCount &other) const
Definition: chan_count.h:77
Definition: amp.h:29
uint32_t _counts[DataType::num_types]
Definition: chan_count.h:152
bool operator>=(const ChanCount &other) const
Definition: chan_count.h:111
static ChanCount max(const ChanCount &a, const ChanCount &b)
Definition: chan_count.h:138
static const uint32_t num_types
Definition: data_type.h:58
void set_audio(uint32_t a)
Definition: chan_count.h:64
#define LIBARDOUR_API
ChanCount operator+(const ChanCount &other) const
Definition: chan_count.h:115
const char * name
uint32_t get(DataType t) const
Definition: chan_count.h:59
void set_midi(uint32_t m)
Definition: chan_count.h:67
Definition: xml++.h:95
void set(DataType t, uint32_t count)
Definition: chan_count.h:58
bool operator<(const ChanCount &other) const
Definition: chan_count.h:89
ChanCount & operator+=(const ChanCount &other)
Definition: chan_count.h:123
bool operator<=(const ChanCount &other) const
Definition: chan_count.h:98
static const ChanCount INFINITE
Definition: chan_count.h:148
static const ChanCount ZERO
Definition: chan_count.h:149
uint32_t n(DataType t) const
Definition: chan_count.h:61
static iterator begin()
Definition: data_type.h:108