Essentia  2.1-beta5-dev
algorithmfactory.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2016 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 
155  CreatorMap _map;
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 
195 
196 #define CREATEI BaseAlgorithm* create_i(const std::string& id
197 #define CENDI ) const;
198 
199  protected:
200 
201  CREATEI P(1) CENDI
202  CREATEI P(1) P(2) CENDI
203  CREATEI P(1) P(2) P(3) CENDI
204  CREATEI P(1) P(2) P(3) P(4) CENDI
205  CREATEI P(1) P(2) P(3) P(4) P(5) CENDI
206  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) CENDI
207  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) P(7) CENDI
208  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) CENDI
209  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) CENDI
210  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) CENDI
211  CREATEI P(1) P(2) P(3) P(4) P(5) P(6) P(7) P(8) P(9) P(10) P(11) CENDI
212  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
213  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
214  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
215  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
216  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
217 
218 #undef CENDI
219 #undef CREATEI
220 #undef CEND
221 #undef AP
222 #undef P
223 #undef CBEG
224 #undef CREATE
225 
226 };
227 
228 } // namespace essentia
229 
230 
231 // include these here because most likely a user of the AlgorithmFactory would want to use the
232 // returned algorithm :)
233 #include "algorithm.h"
235 
236 namespace essentia {
237 
238 namespace standard {
240 }
241 
242 namespace streaming {
244 }
245 
246 } // namespace essentia
247 
248 
249 // include implementation, because the factory is now templated
250 #include "algorithmfactory_impl.h"
251 
252 #endif // ESSENTIA_ALGORITHMFACTORY_H
static BaseAlgorithm * create()
Definition: algorithmfactory.h:139
#define CENDI
Definition: algorithmfactory.h:197
std::pair< typename BaseClass::iterator, bool > insert(const KeyType &key, const ValueType &value)
Definition: types.h:218
#define E_WARNING(msg)
Definition: debugging.h:164
std::string name
Definition: algorithmfactory.h:47
static BaseAlgorithm * create(const std::string &id)
Definition: algorithmfactory.h:86
#define P(n)
Definition: algorithmfactory.h:163
static void free(BaseAlgorithm *algo)
Definition: algorithmfactory.h:94
static const AlgorithmInfo< BaseAlgorithm > & getInfo(const std::string &id)
Definition: algorithmfactory.h:109
static void shutdown()
Definition: algorithmfactory.h:70
#define CREATEI
Definition: algorithmfactory.h:196
Definition: algorithmfactory.h:42
#define CEND
Definition: algorithmfactory.h:165
#define AP(n)
Definition: algorithmfactory.h:164
EssentiaFactory()
Definition: algorithmfactory.h:149
CreatorMap _map
Definition: algorithmfactory.h:155
Definition: algorithmfactory.h:58
#define CREATE
Definition: algorithmfactory.h:161
Definition: algorithm.h:28
EssentiaFactory< Algorithm > AlgorithmFactory
Definition: algorithmfactory.h:243
AlgorithmCreator create
Definition: algorithmfactory.h:46
#define CBEG
Definition: algorithmfactory.h:162
static EssentiaFactory< BaseAlgorithm > * _instance
Definition: algorithmfactory.h:60
Definition: algorithmfactory.h:115
std::string description
Definition: algorithmfactory.h:48
Registrar()
Definition: algorithmfactory.h:118
#define E_DEBUG(module, msg)
Definition: debugging.h:157
std::string category
Definition: algorithmfactory.h:49
static void init()
Definition: algorithmfactory.h:64
EssentiaMap< std::string, AlgorithmInfo< BaseAlgorithm >, string_cmp > CreatorMap
Definition: algorithmfactory.h:154
static EssentiaFactory & instance()
Definition: algorithmfactory_impl.h:26
Definition: debugging.h:44