Essentia  2.1-beta6-dev
configurable.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Essentia
5  *
6  * Essentia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef ESSENTIA_CONFIGURABLE_H
21 #define ESSENTIA_CONFIGURABLE_H
22 
23 #include "parameter.h"
24 
25 namespace essentia {
26 
38 class ESSENTIA_API Configurable {
39 
40  public:
41 
42  // NB: virtual destructor needed because of virtual methods.
43  virtual ~Configurable() {}
44 
48  const std::string& name() const { return _name; }
49 
53  void setName(const std::string& name) { _name = name; }
54 
55 
64  virtual void declareParameters() = 0;
65 
75  virtual void setParameters(const ParameterMap& params);
76 
81  virtual void configure(const ParameterMap& params);
82 
90  virtual void configure() {}
91 
92 
98  return _defaultParams;
99  }
100 
101 
105  const Parameter& parameter(const std::string& key) const { return _params[key]; }
106 
107  protected:
108 
115  void declareParameter(const std::string& name, const std::string& desc,
116  const std::string& range,
117  const Parameter& defaultValue);
118 
119 
120  public:
121 
122  // make doxygen skip all the following macros...
124 
125  // These are conveniency functions, for configuring an algo with fewer
126  // characters per line
127  // WARNING: when declaring the function, you HAVE to omit the P(1) (because it
128  // is already included in the CONFIGURE macro (for comma ',' reasons)
129 #define CONFIGURE void configure(const std::string& name1, const Parameter& value1
130 #define P(n) , const std::string& name##n, const Parameter& value##n
131 #define AP(n) params.add(name##n, value##n);
132 #define PBEG ) { ParameterMap params;
133 #define PEND configure(params); }
134 
135  CONFIGURE PBEG AP(1) PEND
136  CONFIGURE P(2) PBEG AP(1) AP(2) PEND
137  CONFIGURE P(2) P(3) PBEG AP(1) AP(2) AP(3) PEND
138  CONFIGURE P(2) P(3) P(4) PBEG AP(1) AP(2) AP(3) AP(4) PEND
139  CONFIGURE P(2) P(3) P(4) P(5) PBEG AP(1) AP(2) AP(3) AP(4) AP(5) PEND
140  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
141  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
142  CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8)
143  PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) PEND
144  CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9)
145  PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) PEND
146  CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10)
147  PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) AP(10) PEND
148  CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11)
149  PBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) AP(10) AP(11) PEND
150  CONFIGURE P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11) P(12)
151  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
152  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)
153  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
154  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)
155  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
156  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)
157  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
158  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)
159  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
160 
161 
162 #undef PEND
163 #undef PBEG
164 #undef AP
165 #undef P
166 #undef CONFIGURE
167 
169 
170  protected:
171  std::string _name;
174 
175  public:
178 
179 };
180 
181 // macro which is little useful, but allows to write configure() in a very clean
182 // and understandable way for AlgorithmComposite
183 #define INHERIT(x) x, parameter(x)
184 
185 template <typename T> bool compareByName(const T* a, const T* b) {
186  return a->name() < b->name();
187 }
188 
189 } // namespace essentia
190 
191 #endif // ESSENTIA_CONFIGURABLE_H
#define AP(n)
Definition: algorithmfactory.h:164
#define P(n)
Definition: algorithmfactory.h:163
Definition: configurable.h:38
const Parameter & parameter(const std::string &key) const
Definition: configurable.h:105
virtual void declareParameters()=0
DescriptionMap parameterRange
Definition: configurable.h:177
void declareParameter(const std::string &name, const std::string &desc, const std::string &range, const Parameter &defaultValue)
virtual void configure()
Definition: configurable.h:90
ParameterMap _params
Definition: configurable.h:172
virtual ~Configurable()
Definition: configurable.h:43
DescriptionMap parameterDescription
Definition: configurable.h:176
void setName(const std::string &name)
Definition: configurable.h:53
std::string _name
Definition: configurable.h:171
virtual void configure(const ParameterMap &params)
const std::string & name() const
Definition: configurable.h:48
virtual void setParameters(const ParameterMap &params)
ParameterMap _defaultParams
Definition: configurable.h:173
const ParameterMap & defaultParameters() const
Definition: configurable.h:97
Definition: parameter.h:276
Definition: parameter.h:30
Definition: algorithm.h:28
bool compareByName(const T *a, const T *b)
Definition: configurable.h:185