Essentia  2.1-beta6-dev
algorithmfactory.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_ALGORITHMFACTORY_H
21 #define ESSENTIA_ALGORITHMFACTORY_H
22 
23 #include <map>
24 #include <sstream>
25 #include <iostream>
26 #include "types.h"
27 #include "essentia.h"
28 #include "parameter.h"
29 
30 
31 namespace essentia {
32 
41 template <typename BaseAlgorithm>
42 class ESSENTIA_API AlgorithmInfo {
43  public:
44  typedef BaseAlgorithm* (*AlgorithmCreator)();
45 
46  AlgorithmCreator create;
47  std::string name; // do we need this one or is it redundant
48  std::string description;
49  std::string category;
50 };
51 
52 
57 template <typename BaseAlgorithm>
58 class ESSENTIA_API EssentiaFactory {
59 
61 
62  public:
63 
64  static void init() {
65  if (!_instance) {
66  _instance = new EssentiaFactory<BaseAlgorithm>();
67  }
68  }
69 
70  static void shutdown() {
71  delete _instance;
72  _instance = 0;
73  }
74 
86  static BaseAlgorithm* create(const std::string& id) {
87  return instance().create_i(id);
88  }
89 
94  static void free(BaseAlgorithm* algo) {
95  delete algo;
96  }
97 
103  static std::vector<std::string> keys();
104 
109  static const AlgorithmInfo<BaseAlgorithm>& getInfo(const std::string& id) { return instance()._map[id]; }
110 
114  template <typename ConcreteProduct, typename ReferenceConcreteProduct = ConcreteProduct>
115  class Registrar {
116 
117  public:
119  // create the object to be inserted into the factory
120  // with all the necessary information
122  entry.create = &create;
123  entry.name = ReferenceConcreteProduct::name;
124  entry.description = ReferenceConcreteProduct::description;
125  entry.category = ReferenceConcreteProduct::category;
126 
127  // insert object into the factory, or overwrite the existing one if any
129  if (algoMap.find(entry.name) != algoMap.end()) {
130  E_WARNING("Overwriting registered algorithm " << entry.name);
131  algoMap[entry.name] = entry;
132  }
133  else {
134  algoMap.insert(entry.name, entry);
135  E_DEBUG(EFactory, "Registered algorithm " << entry.name);
136  }
137  }
138 
139  static BaseAlgorithm* create() {
140  return new ConcreteProduct;
141  }
142  };
143 
144 
145  static EssentiaFactory& instance();
146 
147  protected:
148  // protected constructor to ensure singleton.
151 
152  BaseAlgorithm* create_i(const std::string& id) const;
153 
156 
157 
158 
159  // conveniency functions that allow to configure an algorithm directly at
160  // creation time
161 #define CREATE static BaseAlgorithm* create(const std::string& id
162 #define CBEG ) { return instance().create_i(id
163 #define P(n) , const std::string& name##n, const Parameter& value##n
164 #define AP(n) , name##n, value##n
165 #define CEND ); }
166 
167  public:
168 
170  CREATE P(1) P(2) CBEG AP(1) AP(2) CEND
171  CREATE P(1) P(2) P(3) CBEG AP(1) AP(2) AP(3) CEND
172  CREATE P(1) P(2) P(3) P(4) CBEG AP(1) AP(2) AP(3) AP(4) CEND
173  CREATE P(1) P(2) P(3) P(4) P(5) CBEG AP(1) AP(2) AP(3) AP(4) AP(5) CEND
174  CREATE P(1) P(2) P(3) P(4) P(5) P(6) CBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) CEND
175  CREATE P(1) P(2) P(3) P(4) P(5) P(6) P(7) CBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) CEND
176  CREATE P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8)
177  CBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) CEND
178  CREATE P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9)
179  CBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) CEND
180  CREATE P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10)
181  CBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) AP(10) CEND
182  CREATE P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11)
183  CBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) AP(10) AP(11) CEND
184  CREATE P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11) P(12)
185  CBEG AP(1) AP(2) AP(3) AP(4) AP(5) AP(6) AP(7) AP(8) AP(9) AP(10) AP(11) AP(12) CEND
186  CREATE P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11) P(12) P(13)
187  CBEG 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) CEND
188  CREATE P(1) 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)
189  CBEG 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) CEND
190  CREATE P(1) 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)
191  CBEG 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) CEND
192  CREATE P(1) 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)
193  CBEG 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) CEND
194  CREATE P(1) 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) P(17)
195  CBEG 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) AP(17) CEND
196  CREATE P(1) 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) P(17) P(18)
197  CBEG 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) AP(17) AP(18) CEND
198  CREATE P(1) 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) P(17) P(18) P(19)
199  CBEG 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) AP(17) AP(18) AP(19) CEND
200  CREATE P(1) 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) P(17) P(18) P(19) P(20)
201  CBEG 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) AP(17) AP(18) AP(19) AP(20) CEND
202  CREATE P(1) 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) P(17) P(18) P(19) P(20) P(21)
203  CBEG 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) AP(17) AP(18) AP(19) AP(20) AP(21) CEND
204  CREATE P(1) 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) P(17) P(18) P(19) P(20) P(21) P(22)
205  CBEG 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) AP(17) AP(18) AP(19) AP(20) AP(21) AP(22) CEND
206  CREATE P(1) 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) P(17) P(18) P(19) P(20) P(21) P(22) P(23)
207  CBEG 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) AP(17) AP(18) AP(19) AP(20) AP(21) AP(22) AP(23) CEND
208  CREATE P(1) 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) P(17) P(18) P(19) P(20) P(21) P(22) P(23) P(24)
209  CBEG 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) AP(17) AP(18) AP(19) AP(20) AP(21) AP(22) AP(23) AP(24) CEND
210  CREATE P(1) 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) P(17) P(18) P(19) P(20) P(21) P(22) P(23) P(24) P(25)
211  CBEG 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) AP(17) AP(18) AP(19) AP(20) AP(21) AP(22) AP(23) AP(24) AP(25) CEND
212 
213 
214 
215 #define CREATEI BaseAlgorithm* create_i(const std::string& id
216 #define CENDI ) const;
217 
218  protected:
219 
220  CREATEI P(1) CENDI
221  CREATEI P(1) P(2) CENDI
222  CREATEI P(1) P(2) P(3) CENDI
223  CREATEI P(1) P(2) P(3) P(4) CENDI
224  CREATEI P(1) P(2) P(3) P(4) P(5) CENDI
225  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) CENDI
226  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) P(7) CENDI
227  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) CENDI
228  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) CENDI
229  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) CENDI
230  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11) CENDI
231  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11) P(12) CENDI
232  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11) P(12) P(13) CENDI
233  CREATEI P(1) 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) CENDI
234  CREATEI P(1) 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) CENDI
235  CREATEI P(1) 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) CENDI
236  CREATEI P(1) 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) P(17) CENDI
237  CREATEI P(1) 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) P(17) P(18) CENDI
238  CREATEI P(1) 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) P(17) P(18) P(19) CENDI
239  CREATEI P(1) 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) P(17) P(18) P(19) P(20) CENDI
240  CREATEI P(1) 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) P(17) P(18) P(19) P(20) P(21) CENDI
241  CREATEI P(1) 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) P(17) P(18) P(19) P(20) P(21) P(22) CENDI
242  CREATEI P(1) 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) P(17) P(18) P(19) P(20) P(21) P(22) P(23) CENDI
243  CREATEI P(1) 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) P(17) P(18) P(19) P(20) P(21) P(22) P(23) P(24) CENDI
244  CREATEI P(1) 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) P(17) P(18) P(19) P(20) P(21) P(22) P(23) P(24) P(25) CENDI
245 
246 #undef CENDI
247 #undef CREATEI
248 #undef CEND
249 #undef AP
250 #undef P
251 #undef CBEG
252 #undef CREATE
253 
254 };
255 
256 } // namespace essentia
257 
258 
259 // include these here because most likely a user of the AlgorithmFactory would want to use the
260 // returned algorithm :)
261 #include "algorithm.h"
263 
264 namespace essentia {
265 
266 namespace standard {
268 }
269 
270 namespace streaming {
272 }
273 
274 } // namespace essentia
275 
276 
277 // include implementation, because the factory is now templated
278 #include "algorithmfactory_impl.h"
279 
280 #endif // ESSENTIA_ALGORITHMFACTORY_H
#define CEND
Definition: algorithmfactory.h:165
#define CREATE
Definition: algorithmfactory.h:161
#define CREATEI
Definition: algorithmfactory.h:215
#define AP(n)
Definition: algorithmfactory.h:164
#define P(n)
Definition: algorithmfactory.h:163
#define CENDI
Definition: algorithmfactory.h:216
#define CBEG
Definition: algorithmfactory.h:162
Definition: algorithmfactory.h:42
std::string description
Definition: algorithmfactory.h:48
AlgorithmCreator create
Definition: algorithmfactory.h:46
std::string name
Definition: algorithmfactory.h:47
std::string category
Definition: algorithmfactory.h:49
Definition: algorithmfactory.h:115
Registrar()
Definition: algorithmfactory.h:118
static BaseAlgorithm * create()
Definition: algorithmfactory.h:139
Definition: algorithmfactory.h:58
EssentiaFactory()
Definition: algorithmfactory.h:149
static void shutdown()
Definition: algorithmfactory.h:70
static const AlgorithmInfo< BaseAlgorithm > & getInfo(const std::string &id)
Definition: algorithmfactory.h:109
static void free(BaseAlgorithm *algo)
Definition: algorithmfactory.h:94
static BaseAlgorithm * create(const std::string &id)
Definition: algorithmfactory.h:86
EssentiaFactory(EssentiaFactory &)
static EssentiaFactory & instance()
Definition: algorithmfactory_impl.h:26
static EssentiaFactory< BaseAlgorithm > * _instance
Definition: algorithmfactory.h:60
EssentiaMap< std::string, AlgorithmInfo< BaseAlgorithm >, string_cmp > CreatorMap
Definition: algorithmfactory.h:154
static void init()
Definition: algorithmfactory.h:64
CreatorMap _map
Definition: algorithmfactory.h:155
std::pair< typename BaseClass::iterator, bool > insert(const KeyType &key, const ValueType &value)
Definition: types.h:219
#define E_DEBUG(module, msg)
Definition: debugging.h:157
#define E_WARNING(msg)
Definition: debugging.h:164
EssentiaFactory< Algorithm > AlgorithmFactory
Definition: algorithmfactory.h:267
EssentiaFactory< Algorithm > AlgorithmFactory
Definition: algorithmfactory.h:271
Definition: algorithm.h:28
@ EFactory
Definition: debugging.h:44