rationale pathways that add notes to Sequence<T> so that there is only final insertio...
[ardour.git] / libs / evoral / src / ControlSet.cpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  *
5  * Evoral is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or (at your option) any later
8  * version.
9  *
10  * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 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 St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <limits>
20 #include "evoral/ControlSet.hpp"
21 #include "evoral/ControlList.hpp"
22 #include "evoral/Control.hpp"
23 #include "evoral/Event.hpp"
24
25 using namespace std;
26
27 namespace Evoral {
28
29
30 ControlSet::ControlSet()
31 {
32 }
33
34 ControlSet::ControlSet (const ControlSet& other)
35         : noncopyable ()
36 {
37         /* derived class must copy controls */
38 }
39
40 void
41 ControlSet::add_control(boost::shared_ptr<Control> ac)
42 {
43         _controls[ac->parameter()] = ac;
44 }
45
46 void
47 ControlSet::what_has_data (set<Parameter>& s) const
48 {
49         Glib::Mutex::Lock lm (_control_lock);
50         for (Controls::const_iterator li = _controls.begin(); li != _controls.end(); ++li) {
51                 s.insert(li->first);
52         }
53 }
54
55 /** If a control for the given parameter does not exist and \a create_if_missing is true,
56  * a control will be created, added to this set, and returned.
57  * If \a create_if_missing is false this function may return null.
58  */
59 boost::shared_ptr<Control>
60 ControlSet::control (const Parameter& parameter, bool create_if_missing)
61 {
62         Controls::iterator i = _controls.find(parameter);
63
64         if (i != _controls.end()) {
65                 return i->second;
66
67         } else if (create_if_missing) {
68                 boost::shared_ptr<Control> ac(control_factory(parameter));
69                 add_control(ac);
70                 return ac;
71
72         } else {
73                 return boost::shared_ptr<Control>();
74         }
75 }
76
77 bool
78 ControlSet::find_next_event (FrameTime now, FrameTime end, ControlEvent& next_event) const
79 {
80         Controls::const_iterator li;
81
82         next_event.when = std::numeric_limits<FrameTime>::max();
83
84         for (li = _controls.begin(); li != _controls.end(); ++li) {
85                 ControlList::const_iterator i;
86                 boost::shared_ptr<const ControlList> alist (li->second->list());
87                 ControlEvent cp (now, 0.0f);
88
89                 for (i = lower_bound (alist->begin(), alist->end(), &cp, ControlList::time_comparator);
90                                 i != alist->end() && (*i)->when < end; ++i) {
91                         if ((*i)->when > now) {
92                                 break;
93                         }
94                 }
95
96                 if (i != alist->end() && (*i)->when < end) {
97                         if ((*i)->when < next_event.when) {
98                                 next_event.when = (*i)->when;
99                         }
100                 }
101         }
102
103         return next_event.when != std::numeric_limits<FrameTime>::max();
104 }
105
106 void
107 ControlSet::clear_controls ()
108 {
109         Glib::Mutex::Lock lm (_control_lock);
110
111         for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
112                 li->second->list()->clear();
113 }
114
115
116 } // namespace Evoral