Fix crash when X11 is not available for VST UIs
[ardour.git] / gtk2_ardour / item_counts.h
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 #ifndef __ardour_item_counts_h__
20 #define __ardour_item_counts_h__
21
22 #include <cstddef>
23 #include <map>
24 #include <utility>
25
26 #include "ardour/data_type.h"
27 #include "evoral/Parameter.h"
28
29 /** A count of various GUI items.
30  *
31  * This is used to keep track of 'consumption' of a selection when pasting, but
32  * may be useful elsewhere.
33  */
34 class ItemCounts
35 {
36 public:
37         ItemCounts() : _notes(0) {}
38
39         size_t n_playlists(ARDOUR::DataType t) const { return get_n(t, _playlists); }
40         size_t n_regions(ARDOUR::DataType t)   const { return get_n(t, _regions); }
41         size_t n_lines(Evoral::Parameter t)    const { return get_n(t, _lines); }
42         size_t n_notes()                       const { return _notes; }
43
44         void increase_n_playlists(ARDOUR::DataType t, size_t delta=1) {
45                 increase_n(t, _playlists, delta);
46         }
47
48         void increase_n_regions(ARDOUR::DataType t, size_t delta=1) {
49                 increase_n(t, _regions, delta);
50         }
51
52         void increase_n_lines(Evoral::Parameter t, size_t delta=1) {
53                 increase_n(t, _lines, delta);
54         }
55
56         void increase_n_notes(size_t delta=1) { _notes += delta; }
57
58 private:
59         template<typename Key>
60         size_t
61         get_n(const Key& key, const typename std::map<Key, size_t>& counts) const {
62                 typename std::map<Key, size_t>::const_iterator i = counts.find(key);
63                 return (i == counts.end()) ? 0 : i->second;
64         }
65
66         template<typename Key>
67         void
68         increase_n(const Key& key, typename std::map<Key, size_t>& counts, size_t delta) {
69                 typename std::map<Key, size_t>::iterator i = counts.find(key);
70                 if (i != counts.end()) {
71                         i->second += delta;
72                 } else {
73                         counts.insert(std::make_pair(key, delta));
74                 }
75         }
76
77         std::map<ARDOUR::DataType,  size_t> _playlists;
78         std::map<ARDOUR::DataType,  size_t> _regions;
79         std::map<Evoral::Parameter, size_t> _lines;
80         size_t                              _notes;
81 };
82
83 #endif /* __ardour_item_counts_h__ */