Ardour  9.0-pre0-582-g084a23a80d
chan_count.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2011 David Robillard <d@drobilla.net>
3  * Copyright (C) 2008-2013 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2013-2015 John Emmas <john@creativepost.co.uk>
5  * Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #pragma once
23 
24 #include <cassert>
25 #include <ostream>
26 
27 #include "pbd/xml++.h"
28 #include "ardour/data_type.h"
29 
30 namespace ARDOUR {
31 
32 
39 public:
40  ChanCount(const XMLNode& node);
41  ChanCount() { reset(); }
42 
47  ChanCount(DataType type, uint32_t count) {
48  reset();
49  set(type, count);
50  }
51 
53  void reset() {
54  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
55  _counts[*t] = 0;
56  }
57  }
58 
63  void set(DataType t, uint32_t count) { assert(t != DataType::NIL); _counts[t] = count; }
64 
69  uint32_t get(DataType t) const { assert(t != DataType::NIL); return _counts[t]; }
70 
71  inline uint32_t n (DataType t) const { return _counts[t]; }
72 
76  inline uint32_t n_audio() const { return _counts[DataType::AUDIO]; }
77 
81  inline void set_audio(uint32_t a) { _counts[DataType::AUDIO] = a; }
82 
86  inline uint32_t n_midi() const { return _counts[DataType::MIDI]; }
87 
91  inline void set_midi(uint32_t m) { _counts[DataType::MIDI] = m; }
92 
96  uint32_t n_total() const {
97  uint32_t ret = 0;
98  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t)
99  ret += _counts[*t];
100 
101  return ret;
102  }
103 
104  bool operator==(const ChanCount& other) const {
105  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t)
106  if (_counts[*t] != other._counts[*t])
107  return false;
108 
109  return true;
110  }
111 
112  bool operator!=(const ChanCount& other) const {
113  return ! (*this == other);
114  }
115 
116  bool operator<(const ChanCount& other) const {
117  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
118  if (_counts[*t] > other._counts[*t]) {
119  return false;
120  }
121  }
122  return (*this != other);
123  }
124 
125  bool operator<=(const ChanCount& other) const {
126  return ( (*this < other) || (*this == other) );
127  }
128 
129  bool operator>(const ChanCount& other) const {
130  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
131  if (_counts[*t] < other._counts[*t]) {
132  return false;
133  }
134  }
135  return (*this != other);
136  }
137 
138  bool operator>=(const ChanCount& other) const {
139  return ( (*this > other) || (*this == other) );
140  }
141 
142  ChanCount operator+(const ChanCount& other) const {
143  ChanCount ret;
144  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
145  ret.set(*t, get(*t) + other.get(*t));
146  }
147  return ret;
148  }
149 
151  ChanCount operator-(const ChanCount& other) const {
152  ChanCount ret;
153  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
154  if (get(*t) < other.get(*t)) {
155  ret.set(*t, 0);
156  } else {
157  ret.set(*t, get(*t) - other.get(*t));
158  }
159  }
160  return ret;
161  }
162 
163  ChanCount operator*(const unsigned int factor) const {
164  ChanCount ret;
165  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
166  ret.set(*t, get(*t) * factor );
167  }
168  return ret;
169  }
170 
172  ChanCount& operator-=(const ChanCount& other) {
173  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
174  if (_counts[*t] < other._counts[*t]) {
175  _counts[*t] = 0;
176  } else {
177  _counts[*t] -= other._counts[*t];
178  }
179  }
180  return *this;
181  }
182 
183  ChanCount& operator+=(const ChanCount& other) {
184  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
185  _counts[*t] += other._counts[*t];
186  }
187  return *this;
188  }
189 
190  static ChanCount min(const ChanCount& a, const ChanCount& b) {
191  ChanCount ret;
192  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
193  ret.set(*t, std::min(a.get(*t), b.get(*t)));
194  }
195  return ret;
196  }
197 
198  static ChanCount max(const ChanCount& a, const ChanCount& b) {
199  ChanCount ret;
200  for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
201  ret.set(*t, std::max(a.get(*t), b.get(*t)));
202  }
203  return ret;
204  }
205 
206  XMLNode* state(const std::string& name) const;
207 
208  static const ChanCount ZERO;
209 
210 private:
211  uint32_t _counts[DataType::num_types];
212 };
213 
214 } // namespace ARDOUR
215 
216 LIBARDOUR_API std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanCount& c);
217 
218 
std::ostream & operator<<(std::ostream &o, const ARDOUR::ChanCount &c)
ChanCount operator-(const ChanCount &other) const
Definition: chan_count.h:151
ChanCount(const XMLNode &node)
uint32_t n(DataType t) const
Definition: chan_count.h:71
uint32_t n_midi() const
Definition: chan_count.h:86
ChanCount(DataType type, uint32_t count)
Definition: chan_count.h:47
ChanCount operator*(const unsigned int factor) const
Definition: chan_count.h:163
void set_midi(uint32_t m)
Definition: chan_count.h:91
XMLNode * state(const std::string &name) const
bool operator>=(const ChanCount &other) const
Definition: chan_count.h:138
uint32_t get(DataType t) const
Definition: chan_count.h:69
ChanCount & operator+=(const ChanCount &other)
Definition: chan_count.h:183
ChanCount & operator-=(const ChanCount &other)
Definition: chan_count.h:172
void set_audio(uint32_t a)
Definition: chan_count.h:81
uint32_t n_total() const
Definition: chan_count.h:96
uint32_t _counts[DataType::num_types]
Definition: chan_count.h:211
static const ChanCount ZERO
Definition: chan_count.h:208
bool operator<(const ChanCount &other) const
Definition: chan_count.h:116
static ChanCount min(const ChanCount &a, const ChanCount &b)
Definition: chan_count.h:190
void set(DataType t, uint32_t count)
Definition: chan_count.h:63
bool operator<=(const ChanCount &other) const
Definition: chan_count.h:125
static ChanCount max(const ChanCount &a, const ChanCount &b)
Definition: chan_count.h:198
uint32_t n_audio() const
Definition: chan_count.h:76
bool operator!=(const ChanCount &other) const
Definition: chan_count.h:112
ChanCount operator+(const ChanCount &other) const
Definition: chan_count.h:142
bool operator==(const ChanCount &other) const
Definition: chan_count.h:104
bool operator>(const ChanCount &other) const
Definition: chan_count.h:129
static iterator end()
Definition: data_type.h:114
static const uint32_t num_types
Definition: data_type.h:61
static iterator begin()
Definition: data_type.h:113
Definition: xml++.h:114
GtkImageIconNameData name
Definition: gtkimage.h:6
#define LIBARDOUR_API