-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathconfigurable.h
191 lines (157 loc) · 7 KB
/
configurable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright (C) 2006-2021 Music Technology Group - Universitat Pompeu Fabra
*
* This file is part of Essentia
*
* Essentia is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation (FSF), either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the Affero GNU General Public License
* version 3 along with this program. If not, see http://www.gnu.org/licenses/
*/
#ifndef ESSENTIA_CONFIGURABLE_H
#define ESSENTIA_CONFIGURABLE_H
#include "parameter.h"
namespace essentia {
/**
* A Configurable instance is an object that has a given name, and can be
* configured using a certain number of @c Parameters. These parameters have
* to be declared beforehand using the @c declareParameters method.
*
* Whenever a Configurable instance gets reconfigured with new parameters,
* it will first save them internally and then call the @c configure() method.
* You should reimplement this method and do anything necessary for your object
* to be up-to-date and synchronized with the new parameters. These are
* accessible using the @c parameter() method.
*/
class ESSENTIA_API Configurable {
public:
// NB: virtual destructor needed because of virtual methods.
virtual ~Configurable() {}
/**
* Return the name of this Configurable.
*/
const std::string& name() const { return _name; }
/**
* Set the name for this Configurable.
*/
void setName(const std::string& name) { _name = name; }
/**
* Declare the parameters that this @c Configurable can accept.
* You have to implement this method in derived classes, even though you don't
* need any parameters. In that case, just define it as empty.
*
* In this method you should only be calling the @c declareParameter method,
* once for each parameter, with optional default values.
*/
virtual void declareParameters() = 0;
/**
* Set the given parameters as the current ones. Parameters which are not
* redefined will keep their old values, while this method will throw an
* @c EssentiaException if passing it an unknown parameter (i.e.: not declared
* using @c declareParameters() ).
* As a general rule, it is better to use the configure(const ParameterMap&)
* method, but in certain cases you may want to set parameters _without_
* reconfiguring the object.
*/
virtual void setParameters(const ParameterMap& params);
/**
* Set the given parameters as the current ones and reconfigure the object.
* @see setParameters
*/
virtual void configure(const ParameterMap& params);
/**
* This function will be automatically called after some parameters have been
* set. This is the place where you should write your specific code which
* needs to be called when configuring this object.
*
* You can access the newly set parameters using the @c parameter() method.
*/
virtual void configure() {}
/**
* Return a map filled with the parameters that have been declared, along with
* their default value if defined.
*/
const ParameterMap& defaultParameters() const {
return _defaultParams;
}
/**
* Returns the parameter corresponding to the given name.
*/
const Parameter& parameter(const std::string& key) const { return _params[key]; }
protected:
/**
* Use this method to declare the list of parameters that this algorithm in
* the @c declareParameters() method.
* If a Parameter doesn't have a default value, use its type instead, e.g.:
* Parameter::STRING, or Parameter::VECTOR_REAL, etc...
*/
void declareParameter(const std::string& name, const std::string& desc,
const std::string& range,
const Parameter& defaultValue);
public:
// make doxygen skip all the following macros...
/// @cond
// These are conveniency functions, for configuring an algo with fewer
// characters per line
// WARNING: when declaring the function, you HAVE to omit the P(1) (because it
// is already included in the CONFIGURE macro (for comma ',' reasons)
#define CONFIGURE void configure(const std::string& name1, const Parameter& value1
#define P(n) , const std::string& name##n, const Parameter& value##n
#define AP(n) params.add(name##n, value##n);
#define PBEG ) { ParameterMap params;
#define PEND configure(params); }
CONFIGURE PBEG AP(1) PEND
CONFIGURE P(2) PBEG AP(1) AP(2) PEND
CONFIGURE P(2) P(3) PBEG AP(1) AP(2) AP(3) PEND
CONFIGURE P(2) P(3) P(4) PBEG AP(1) AP(2) AP(3) AP(4) PEND
CONFIGURE P(2) P(3) P(4) P(5) PBEG AP(1) AP(2) AP(3) AP(4) AP(5) PEND
CONFIGURE P(2) P(3) P(4) P(5) P(6) PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) PEND
CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) PEND
CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8)
PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) PEND
CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9)
PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) PEND
CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10)
PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) AP(10) PEND
CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11)
PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) AP(10) AP(11) PEND
CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11) P(12)
PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) AP(10) AP(11) AP(12) PEND
CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11) P(12) P(13)
PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) AP(10) AP(11) AP(12) AP(13) PEND
CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11) P(12) P(13) P(14)
PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) AP(10) AP(11) AP(12) AP(13) AP(14) PEND
CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11) P(12) P(13) P(14) P(15)
PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) AP(10) AP(11) AP(12) AP(13) AP(14) AP(15) PEND
CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11) P(12) P(13) P(14) P(15) P(16)
PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) AP(10) AP(11) AP(12) AP(13) AP(14) AP(15) AP(16) PEND
#undef PEND
#undef PBEG
#undef AP
#undef P
#undef CONFIGURE
/// @endcond
protected:
std::string _name;
ParameterMap _params;
ParameterMap _defaultParams;
public:
DescriptionMap parameterDescription;
DescriptionMap parameterRange;
};
// macro which is little useful, but allows to write configure() in a very clean
// and understandable way for AlgorithmComposite
#define INHERIT(x) x, parameter(x)
template <typename T> bool compareByName(const T* a, const T* b) {
return a->name() < b->name();
}
} // namespace essentia
#endif // ESSENTIA_CONFIGURABLE_H