merge (w/fix) with master
[ardour.git] / libs / evoral / evoral / Parameter.hpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 David 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 #ifndef EVORAL_PARAMETER_HPP
20 #define EVORAL_PARAMETER_HPP
21
22 #include <string>
23 #include <map>
24 #include <stdint.h>
25 #include <boost/shared_ptr.hpp>
26
27 #include "evoral/visibility.h"
28
29 namespace Evoral {
30
31
32 /** ID of a [play|record|automate]able parameter.
33  *
34  * A parameter is defined by (type, id, channel).  Type is an integer which
35  * can be used in any way by the application (e.g. cast to a custom enum,
36  * map to/from a URI, etc).  ID is type specific (e.g. MIDI controller #).
37  *
38  * This class defines a < operator which is a strict weak ordering, so
39  * Parameter may be stored in a std::set, used as a std::map key, etc.
40  */
41 class LIBEVORAL_API Parameter
42 {
43 public:
44         Parameter(uint32_t type, uint8_t channel=0, uint32_t id=0)
45                 : _type(type), _id(id), _channel(channel)
46         {}
47
48         virtual ~Parameter() {}
49
50         inline uint32_t type()    const { return _type; }
51         inline uint8_t  channel() const { return _channel; }
52         inline uint32_t id()      const { return _id; }
53
54         /** Equivalence operator
55          * It is obvious from the definition that this operator
56          * is transitive, as required by stict weak ordering
57          * (see: http://www.sgi.com/tech/stl/StrictWeakOrdering.html)
58          */
59         inline bool operator==(const Parameter& id) const {
60                 return (_type == id._type && _channel == id._channel && _id == id._id );
61         }
62
63         /** Strict weak ordering
64          * See: http://www.sgi.com/tech/stl/StrictWeakOrdering.html
65          * Sort Parameters first according to type then to channel and lastly to ID.
66          */
67         inline bool operator<(const Parameter& other) const {
68                 if (_type < other._type) {
69                         return true;
70                 } else if (_type == other._type && _channel < other._channel) {
71                         return true;
72                 } else if (_type == other._type && _channel == other._channel && _id < other._id ) {
73                         return true;
74                 }
75
76                 return false;
77         }
78
79         inline operator bool() const { return (_type != 0); }
80
81         /** Not used in indentity/comparison */
82         struct Metadata {
83                 Metadata(double low=0.0, double high=1.0, double mid=0.0, bool tog=false)
84                         : min(low), max(high), normal(mid), toggled(tog)
85                 {}
86                 double min;
87                 double max;
88                 double normal;
89                 bool   toggled;
90         };
91
92         inline static void set_range(uint32_t type, double min, double max, double normal, bool toggled) {
93                 _type_metadata[type] = Metadata(min, max, normal, toggled);
94         }
95
96         inline void set_range(double min, double max, double normal, bool toggled) {
97                 _metadata = boost::shared_ptr<Metadata>(new Metadata(min, max, normal, toggled));
98         }
99
100         inline Metadata& metadata() const {
101                 if (_metadata)
102                         return *_metadata.get();
103                 else
104                         return _type_metadata[_type];
105         }
106
107         inline double min()     const { return metadata().min; }
108         inline double max()     const { return metadata().max; }
109         inline double normal()  const { return metadata().normal; }
110         inline double toggled() const { return metadata().toggled; }
111
112 protected:
113         // Default copy constructor is ok
114
115         // ID (used in comparison)
116         uint32_t _type;
117         uint32_t _id;
118         uint8_t  _channel;
119
120         boost::shared_ptr<Metadata> _metadata;
121
122         typedef std::map<uint32_t, Metadata> TypeMetadata;
123         static TypeMetadata _type_metadata;
124 };
125
126
127 } // namespace Evoral
128
129 #endif // EVORAL_PARAMETER_HPP
130