Ardour  9.0-pre0-582-g084a23a80d
item_counts.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 David Robillard <d@drobilla.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #pragma once
20 
21 #include <cstddef>
22 #include <map>
23 #include <utility>
24 
25 #include "ardour/data_type.h"
26 #include "evoral/Parameter.h"
27 
34 {
35 public:
36  ItemCounts() : _notes(0) {}
37 
38  size_t n_playlists(ARDOUR::DataType t) const { return get_n(t, _playlists); }
39  size_t n_regions(ARDOUR::DataType t) const { return get_n(t, _regions); }
40  size_t n_lines(Evoral::Parameter t) const { return get_n(t, _lines); }
41  size_t n_notes() const { return _notes; }
42 
43  void increase_n_playlists(ARDOUR::DataType t, size_t delta=1) {
44  increase_n(t, _playlists, delta);
45  }
46 
47  void increase_n_regions(ARDOUR::DataType t, size_t delta=1) {
48  increase_n(t, _regions, delta);
49  }
50 
51  void increase_n_lines(Evoral::Parameter t, size_t delta=1) {
52  increase_n(t, _lines, delta);
53  }
54 
55  void increase_n_notes(size_t delta=1) { _notes += delta; }
56 
57 private:
58  template<typename Key>
59  size_t
60  get_n(const Key& key, const typename std::map<Key, size_t>& counts) const {
61  typename std::map<Key, size_t>::const_iterator i = counts.find(key);
62  return (i == counts.end()) ? 0 : i->second;
63  }
64 
65  template<typename Key>
66  void
67  increase_n(const Key& key, typename std::map<Key, size_t>& counts, size_t delta) {
68  typename std::map<Key, size_t>::iterator i = counts.find(key);
69  if (i != counts.end()) {
70  i->second += delta;
71  } else {
72  counts.insert(std::make_pair(key, delta));
73  }
74  }
75 
76  std::map<ARDOUR::DataType, size_t> _playlists;
77  std::map<ARDOUR::DataType, size_t> _regions;
78  std::map<Evoral::Parameter, size_t> _lines;
79  size_t _notes;
80 };
81 
void increase_n(const Key &key, typename std::map< Key, size_t > &counts, size_t delta)
Definition: item_counts.h:67
void increase_n_notes(size_t delta=1)
Definition: item_counts.h:55
size_t get_n(const Key &key, const typename std::map< Key, size_t > &counts) const
Definition: item_counts.h:60
size_t n_lines(Evoral::Parameter t) const
Definition: item_counts.h:40
std::map< Evoral::Parameter, size_t > _lines
Definition: item_counts.h:78
size_t n_regions(ARDOUR::DataType t) const
Definition: item_counts.h:39
std::map< ARDOUR::DataType, size_t > _regions
Definition: item_counts.h:77
void increase_n_playlists(ARDOUR::DataType t, size_t delta=1)
Definition: item_counts.h:43
void increase_n_lines(Evoral::Parameter t, size_t delta=1)
Definition: item_counts.h:51
void increase_n_regions(ARDOUR::DataType t, size_t delta=1)
Definition: item_counts.h:47
std::map< ARDOUR::DataType, size_t > _playlists
Definition: item_counts.h:76
size_t _notes
Definition: item_counts.h:79
size_t n_playlists(ARDOUR::DataType t) const
Definition: item_counts.h:38
size_t n_notes() const
Definition: item_counts.h:41