Factor out sequencing related things into an independant new library: "evoral".
[ardour.git] / libs / evoral / evoral / Parameter.hpp
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 #ifndef EVORAL_PARAMETER_HPP
20 #define EVORAL_PARAMETER_HPP
21
22 #include <string>
23 #include <boost/format.hpp>
24
25 namespace Evoral {
26
27
28 /** ID of a [play|record|automate]able parameter.
29  *
30  * A parameter is defined by (type, id, channel).  Type is an integer which
31  * can be used in any way by the application (e.g. cast to a custom enum,
32  * map to/from a URI, etc).  ID is type specific (e.g. MIDI controller #).
33  *
34  * This class defines a < operator which is a strict weak ordering, so
35  * Parameter may be stored in a std::set, used as a std::map key, etc.
36  */
37 class Parameter
38 {
39 public:
40         Parameter(uint32_t type, uint32_t id, int8_t channel=0,
41                         double min=0.0f, double max=0.0f, double def=0.0f)
42                 : _type(type), _id(id), _channel(channel), _min(min), _max(max), _normal(def)
43         {}
44         
45         //Parameter(const std::string& str);
46
47         inline uint32_t type()    const { return _type; }
48         inline uint32_t id()      const { return _id; }
49         inline uint8_t  channel() const { return _channel; }
50
51         /**
52          * Equivalence operator
53          * It is obvious from the definition that this operator
54          * is transitive, as required by stict weak ordering
55          * (see: http://www.sgi.com/tech/stl/StrictWeakOrdering.html)
56          */
57         inline bool operator==(const Parameter& id) const {
58                 return (_type == id._type && _id == id._id && _channel == id._channel);
59         }
60         
61         /** Strict weak ordering
62          * See: http://www.sgi.com/tech/stl/StrictWeakOrdering.html
63          * Sort Parameters first according to type then to id and lastly to channel.
64          *  
65          * Proof:
66          * <ol>
67          * <li>Irreflexivity: f(x, x) is false because of the irreflexivity of \c < in each branch.</li>
68          * <li>Antisymmetry: given x != y, f(x, y) implies !f(y, x) because of the same 
69          *     property of \c < in each branch and the symmetry of operator==. </li>
70          * <li>Transitivity: let f(x, y) and f(y, z) be true.
71          *    We prove by contradiction, assuming the contrary (f(x, z) is false).
72          *    That would imply exactly one of the following:
73          *        <ol>
74          *      <li> x == z which contradicts the assumption f(x, y) and f(y, x)
75          *                 because of antisymmetry.
76          *      </li>
77          *      <li> f(z, x) is true. That would imply that one of the ivars (we call it i) 
78          *           of x is greater than the same ivar in z while all "previous" ivars
79          *           are equal. That would imply that also in y all those "previous"
80          *           ivars are equal and because if x.i > z.i it is impossible
81          *           that there is an y that satisfies x.i < y.i < z.i at the same
82          *           time which contradicts the assumption.
83          *      </li>
84          *      Therefore f(x, z) is true (transitivity)
85          *    </ol> 
86          * </li>
87          * </ol>
88          */
89         inline bool operator<(const Parameter& id) const {
90                 if (_type < id._type) {
91                         return true;
92                 } else if (_type == id._type && _id < id._id) {
93                         return true;
94                 } else if (_id == id._id && _channel < id._channel) {
95                         return true;
96                 }
97                 
98                 return false;
99         }
100         
101         inline operator bool() const { return (_type != 0); }
102         
103         virtual std::string symbol() const {
104                 return (boost::format("%1%_c%2%_n%3%\n") % _type % _channel % _id).str();
105         }
106         
107         inline void set_range(double min, double max, double normal) {
108                 _min = min;
109                 _max = max;
110                 _normal = normal;
111         }
112         
113         inline const double min()    const { return _min; }
114         inline const double max()    const { return _max; }
115         inline const double normal() const { return _normal; }
116
117 protected:
118         // Default copy constructor is ok
119         
120         // ID (used in comparison)
121         uint32_t _type;
122         uint32_t _id;
123         uint8_t  _channel;
124
125         // Metadata (not used in comparison)
126         double _min;
127         double _max;
128         double _normal;
129 };
130
131
132 } // namespace Evoral
133
134 #endif // EVORAL_PARAMETER_HPP
135