Essentia  2.1-beta5-dev
types.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_TYPES_H
21 #define ESSENTIA_TYPES_H
22 
23 #include <map>
24 #include <vector>
25 #include <cctype>
26 #include <cassert>
27 #include <sstream>
28 #include <typeinfo>
29 #include <string.h>
30 #include "config.h"
31 #include "debugging.h"
32 #include "streamutil.h"
33 
34 
35 // fixed-size int types
36 
37 #ifndef OS_WIN32
38 
39 #include <inttypes.h>
40 
41 typedef uint16_t uint16;
42 typedef uint32_t uint32;
43 typedef uint64_t uint64;
44 typedef int16_t sint16;
45 typedef int32_t sint32;
46 typedef int64_t sint64;
47 
48 typedef unsigned int uint;
49 
50 #else // OS_WIN32
51 
52 typedef unsigned __int16 uint16;
53 typedef unsigned __int32 uint32;
54 typedef unsigned __int64 uint64;
55 typedef __int16 sint16;
56 typedef __int32 sint32;
57 typedef __int64 sint64;
58 
59 #endif // OS_WIN32
60 
61 
62 
63 namespace essentia {
64 
68 typedef float Real;
69 
70 
76 class EssentiaException : public std::exception {
77 
78  public:
79  EssentiaException(const char* msg) : exception(), _msg(msg) {}
80  EssentiaException(const std::string& msg) : exception(), _msg(msg) {}
81  EssentiaException(const std::ostringstream& msg) : exception(), _msg(msg.str()) {}
82 
83  template <typename T, typename U>
84  EssentiaException(const T& a, const U& b) : exception() {
85  std::ostringstream oss; oss << a << b; _msg = oss.str();
86  }
87 
88  template <typename T, typename U, typename V>
89  EssentiaException(const T& a, const U& b, const V& c) : exception() {
90  std::ostringstream oss; oss << a << b << c; _msg = oss.str();
91  }
92 
93  template <typename T, typename U, typename V, typename W>
94  EssentiaException(const T& a, const U& b, const V& c, const W& d) : exception() {
95  std::ostringstream oss; oss << a << b << c << d; _msg = oss.str();
96  }
97 
98  virtual ~EssentiaException() throw() {}
99  virtual const char* what() const throw() { return _msg.c_str(); }
100 
101  protected:
102  std::string _msg;
103 
104 };
105 
106 
110 inline bool case_insensitive_char_cmp(char a, char b) {
111  return std::tolower(a) < std::tolower(b);
112 }
113 
118  : public std::binary_function<const std::string&, const std::string&, bool> {
119  bool operator()(const std::string& str1, const std::string& str2) const {
120  return std::lexicographical_compare(str1.begin(), str1.end(),
121  str2.begin(), str2.end(),
123  }
124 };
125 
126 
127 template <class T>
128 class OrderedMap : public std::vector<std::pair<std::string, T*> > {
129  public:
130  typedef typename std::vector<std::pair<std::string, T*> > BaseClass;
131 
132  int size() const { return (int)BaseClass::size(); }
133 
134  const std::pair<std::string, T*>& operator[](uint idx) const {
135  return BaseClass::operator[](idx);
136  }
137 
138  std::pair<std::string, T*>& operator[](uint idx) {
139  return BaseClass::operator[](idx);
140  }
141 
142  const T* operator[](const char* str) const {
143  const uint size = this->size();
144  for (uint i=0; i<size; i++) {
145  if (charptr_cmp((*this)[i].first.c_str(), str) == 0) {
146  return (*this)[i].second;
147  }
148  }
149 
150  throw EssentiaException("Value not found: '", str, "'\nAvailable keys: ", keys());
151  }
152 
153  T* operator[](const char* str) {
154  return const_cast<T*>(const_cast<const OrderedMap<T>*>(this)->operator[](str));
155  }
156 
157  const T* operator[](const std::string& str) const {
158  return operator[](str.c_str());
159  }
160 
161  T* operator[](const std::string& str) {
162  return operator[](str.c_str());
163  }
164 
165  std::vector<std::string> keys() const {
166  std::vector<std::string> result(this->size());
167  for (int i=0; i<this->size(); i++) {
168  result[i] = this->at(i).first;
169  }
170  return result;
171  }
172 
173  void insert(const std::string& key, T* value) {
174  this->push_back(make_pair(key, value));
175  }
176 };
177 
178 
179 
188 template <typename KeyType, typename ValueType, typename Compare = std::less<KeyType> >
189 class EssentiaMap : public std::map<KeyType, ValueType, Compare> {
190 
191  public:
192  typedef std::map<KeyType, ValueType, Compare> BaseClass;
193 
197  ValueType& operator[](const KeyType& key) {
198  typename BaseClass::iterator it = this->find(key);
199  if (it == BaseClass::end()) {
200  throw EssentiaException("Value not found: '", key, "'\nAvailable keys: ", keys());
201  }
202  return it->second;
203  }
204 
210  const ValueType& operator[](const KeyType& key) const {
211  typename BaseClass::const_iterator it = this->find(key);
212  if (it == BaseClass::end()) {
213  throw EssentiaException("Value not found: '", key, "'\nAvailable keys: ", keys());
214  }
215  return it->second;
216  }
217 
218  std::pair<typename BaseClass::iterator, bool> insert(const KeyType& key, const ValueType& value) {
219  return BaseClass::insert(std::make_pair(key, value));
220  }
221 
222  std::vector<std::string> keys() const {
223  std::vector<std::string> result;
224  result.reserve(BaseClass::size());
225  std::ostringstream stream;
226  typename BaseClass::const_iterator it = this->begin();
227  for (; it != this->end(); ++it) {
228  stream.str("");
229  stream << it->first;
230  result.push_back(stream.str());
231  }
232  return result;
233  }
234 
235 };
236 
237 
242 
243 
244 
245 
249 #if SAFE_TYPE_COMPARISONS
250 
251 // comparison of the type is done using the name() method, because type_info
252 // are not shared between different linking units.
253 inline bool sameType(const std::type_info& t1, const std::type_info& t2) {
254  return strcmp(t1.name(), t2.name()) == 0;
255 }
256 
257 #else // SAFE_TYPE_COMPARISONS
258 
259 inline bool sameType(const std::type_info& t1, const std::type_info& t2) {
260  return t1 == t2;
261 }
262 
263 #endif // SAFE_TYPE_COMPARISONS
264 
265 
266 // defined in src/base/essentia.cpp
267 std::string nameOfType(const std::type_info& type);
268 
273 class TypeProxy {
274  protected:
275  std::string _name;
276 
277  public:
279  TypeProxy(const std::string& name) : _name(name) {}
280 
281  virtual ~TypeProxy() {}
282 
283  const std::string& name() const { return _name; }
284  void setName(const std::string& name) { _name = name; }
285 
286  inline void checkType(const std::type_info& received,
287  const std::type_info& expected) const {
288  if (!sameType(received, expected)) {
289  std::ostringstream msg;
290  msg << "Error when checking types. Expected: " << nameOfType(expected)
291  << ", received: " << nameOfType(received);
292  throw EssentiaException(msg);
293  }
294  }
295 
296  template <typename Type>
297  void checkType() const {
298  checkType(typeid(Type), typeInfo());
299  }
300 
301  void checkSameTypeAs(const TypeProxy& obj) const {
302  checkType(obj.typeInfo(), typeInfo());
303  }
304 
305  void checkVectorSameTypeAs(const TypeProxy& obj) const {
306  checkType(obj.vectorTypeInfo(), typeInfo());
307  }
308 
309  virtual const std::type_info& typeInfo() const = 0;
310  virtual const std::type_info& vectorTypeInfo() const = 0;
311 };
312 
316 inline std::string nameOfType(const TypeProxy& tproxy) {
317  return nameOfType(tproxy.typeInfo());
318 }
319 
323 inline bool sameType(const TypeProxy& lhs, const TypeProxy& rhs) {
324  return sameType(lhs.typeInfo(), rhs.typeInfo());
325 }
326 
331 #define USE_TYPE_INFO(TokenType) \
332  virtual const std::type_info& typeInfo() const { \
333  return typeid(TokenType); \
334  } \
335  virtual const std::type_info& vectorTypeInfo() const { \
336  return typeid(std::vector<TokenType>); \
337  }
338 
339 
343 typedef int ReaderID;
344 
348 typedef Real AudioSample;
349 
350 template <typename T>
351 class Tuple2 {
352  public:
353  T first;
355 
356  const T& left() const { return first; }
357  const T& right() const { return second; }
358  const T& x() const { return first; }
359  const T& y() const { return second; }
360 
361  T& left() { return first; }
362  T& right() { return second; }
363  T& x() { return first; }
364  T& y() { return second; }
365 };
366 
371 
372 
373 
374 namespace streaming {
375 
381 class BufferInfo {
382  public:
383  int size;
385 
386  BufferInfo(int size = 0, int contiguous = 0) :
387  size(size), maxContiguousElements(contiguous) {}
388 };
389 
390 namespace BufferUsage {
391 
402 };
403 
404 } // namespace BufferUsage
405 
406 } // namespace streaming
407 } // namespace essentia
408 
409 #endif // ESSENTIA_TYPES_H
bool operator()(const std::string &str1, const std::string &str2) const
Definition: types.h:119
int maxContiguousElements
Definition: types.h:384
TypeProxy(const std::string &name)
Definition: types.h:279
EssentiaException(const T &a, const U &b, const V &c, const W &d)
Definition: types.h:94
Definition: types.h:128
int ReaderID
Definition: types.h:343
T * operator[](const char *str)
Definition: types.h:153
virtual const char * what() const
Definition: types.h:99
BufferInfo(int size=0, int contiguous=0)
Definition: types.h:386
std::pair< typename BaseClass::iterator, bool > insert(const KeyType &key, const ValueType &value)
Definition: types.h:218
const ValueType & operator[](const KeyType &key) const
Definition: types.h:210
virtual ~TypeProxy()
Definition: types.h:281
bool case_insensitive_char_cmp(char a, char b)
Definition: types.h:110
T & y()
Definition: types.h:364
std::map< KeyType, ValueType, Compare > BaseClass
Definition: types.h:192
std::string nameOfType(const std::type_info &type)
Definition: types.h:189
void checkType() const
Definition: types.h:297
void checkVectorSameTypeAs(const TypeProxy &obj) const
Definition: types.h:305
uint16_t uint16
Definition: types.h:41
std::string _name
Definition: types.h:275
TypeProxy()
Definition: types.h:278
BufferUsageType
Definition: types.h:397
bool sameType(const std::type_info &t1, const std::type_info &t2)
Definition: types.h:259
T & left()
Definition: types.h:361
EssentiaMap< std::string, std::string, string_cmp > DescriptionMap
Definition: types.h:241
virtual const std::type_info & vectorTypeInfo() const =0
uint32_t uint32
Definition: types.h:42
Tuple2< Real > StereoSample
Definition: types.h:370
std::pair< std::string, T * > & operator[](uint idx)
Definition: types.h:138
const T * operator[](const char *str) const
Definition: types.h:142
std::vector< std::string > keys() const
Definition: types.h:165
float Real
Definition: types.h:68
const T * operator[](const std::string &str) const
Definition: types.h:157
virtual const std::type_info & typeInfo() const =0
T second
Definition: types.h:354
EssentiaException(const std::ostringstream &msg)
Definition: types.h:81
Definition: types.h:117
Definition: types.h:381
Definition: types.h:351
T * operator[](const std::string &str)
Definition: types.h:161
std::vector< std::pair< std::string, T * > > BaseClass
Definition: types.h:130
uint64_t uint64
Definition: types.h:43
Definition: algorithm.h:28
Definition: types.h:76
T first
Definition: types.h:353
Definition: types.h:273
EssentiaException(const std::string &msg)
Definition: types.h:80
EssentiaException(const T &a, const U &b)
Definition: types.h:84
unsigned int uint
Definition: types.h:48
virtual ~EssentiaException()
Definition: types.h:98
const std::pair< std::string, T * > & operator[](uint idx) const
Definition: types.h:134
T & x()
Definition: types.h:363
int64_t sint64
Definition: types.h:46
const T & x() const
Definition: types.h:358
void checkType(const std::type_info &received, const std::type_info &expected) const
Definition: types.h:286
int size() const
Definition: types.h:132
EssentiaException(const char *msg)
Definition: types.h:79
T & right()
Definition: types.h:362
void checkSameTypeAs(const TypeProxy &obj) const
Definition: types.h:301
std::vector< std::string > keys() const
Definition: types.h:222
int16_t sint16
Definition: types.h:44
const T & right() const
Definition: types.h:357
const std::string & name() const
Definition: types.h:283
EssentiaException(const T &a, const U &b, const V &c)
Definition: types.h:89
const T & left() const
Definition: types.h:356
ValueType & operator[](const KeyType &key)
Definition: types.h:197
const T & y() const
Definition: types.h:359
Real AudioSample
Definition: types.h:348
void setName(const std::string &name)
Definition: types.h:284
int32_t sint32
Definition: types.h:45
void insert(const std::string &key, T *value)
Definition: types.h:173
std::string _msg
Definition: types.h:102
int size
Definition: types.h:383