Gaia
types.h
1 /*
2  * Copyright (C) 2006-2013 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Gaia
5  *
6  * Gaia 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 GAIA_TYPES_H
21 #define GAIA_TYPES_H
22 
23 #include <QMap>
24 #include <QStringList>
25 #include <QPointF>
26 #include "Eigen/Dense"
27 #include "osdetect.h"
28 #include "gaia.h"
29 #include "gaiaexception.h"
30 #include "debugging.h"
31 #include "gvarlengtharray.h"
32 
34 // ----[ Fixed-length integer types ]---------------------------------------//
35 
36 typedef unsigned int uint;
37 typedef unsigned char uint8;
38 typedef signed int sint;
39 typedef signed char sint8;
40 
41 #ifndef OS_WIN32
42 
43 #include <inttypes.h>
44 
45 typedef uint16_t uint16;
46 typedef uint32_t uint32;
47 typedef uint64_t uint64;
48 typedef int16_t sint16;
49 typedef int32_t sint32;
50 typedef int64_t sint64;
51 
52 #else // OS_WIN32
53 
54 typedef unsigned __int16 uint16;
55 typedef unsigned __int32 uint32;
56 typedef unsigned __int64 uint64;
57 typedef __int16 sint16;
58 typedef __int32 sint32;
59 typedef __int64 sint64;
60 
61 #endif // OS_WIN32
62 
63 
64 namespace gaia2 {
65 
67 // ----[ Base types used mostly everywhere ]--------------------------------//
68 
69 typedef float Real;
70 typedef int Enum;
71 
72 template <typename T>
73 class Array : public GVarLengthArray<T, 1> {
74  public:
76  Array(int size) : GVarLengthArray<T, 1>(size) {}
77  Array(int size, const T& value) : GVarLengthArray<T, 1>(size) {
78  for (int i=0; i<size; i++) (*this)[i] = value;
79  }
80  //friend QDataStream& operator<< <>(QDataStream& out, const Array<T>& array);
81  //friend QDataStream& operator>> <>(QDataStream& in, Array<T>& array);
82 
83  const T& at(int i) const { return (*this)[i]; }
84  Array& operator<<(const T& x) { this->append(x); return *this; }
85  T& front() { return (*this)[0]; }
86  const T& front() const { return at(0); }
87  Array& fill(const T& val, int size) {
88  this->resize(size);
89  for (int i=0; i<size; i++) {
90  (*this)[i] = val;
91  }
92  return *this;
93  }
94 
95  int size() const { return GVarLengthArray<T, 1>::size(); }
96 
97  const T* constBegin() const { return GVarLengthArray<T, 1>::constData(); }
98  const T* begin() const { return GVarLengthArray<T, 1>::constData(); }
99  T* begin() { return GVarLengthArray<T, 1>::data(); }
100 
101  const T* constEnd() const { return GVarLengthArray<T, 1>::constData() + size(); }
102  const T* end() const { return GVarLengthArray<T, 1>::constData() + size(); }
103  T* end() { return GVarLengthArray<T, 1>::data() + size(); }
104 
105  bool operator==(const Array<T>& rhs) const {
106  if (this->size() != rhs.size()) return false;
107  for (int i=0; i<size(); i++) {
108  if (at(i) != rhs.at(i)) return false;
109  }
110  return true;
111  }
112 
113  bool operator!=(const Array<T>& rhs) const { return !operator==(rhs); }
114 };
115 
116 
117 template <typename T>
118 QDataStream& operator<<(QDataStream& out, const Array<T>& array) {
119  out << (qint32)array.size();
120  for (int i=0; i<array.size(); i++) {
121  out << array[i];
122  }
123  return out;
124 }
125 
126 template <typename T>
127 QDataStream& operator>>(QDataStream& in, Array<T>& array) {
128  qint32 size;
129  in >> size;
130  array.resize(size);
131  for (int i=0; i<size; i++) {
132  in >> array[i];
133  }
134  return in;
135 }
136 
137 
142 template <typename Key, typename Value, typename ExceptionType = GaiaException>
143 class GaiaMap : public QMap<Key, Value> {
144  public:
145  const Value& value(const Key& key) const {
146  typename QMap<Key, Value>::const_iterator it = this->find(key);
147  if (it == this->end()) {
148  throw ExceptionType("Key '", key, "' not found in map");
149  }
150  return it.value();
151  }
152 
153  // NB: this function has no non-const counterpart, because it makes no sense
154  const Value& value(const Key& key, const Value& defaultValue) const {
155  try {
156  return value(key);
157  }
158  catch (ExceptionType&) {
159  return defaultValue;
160  }
161  }
162 
163  inline const Value& operator[](const Key& key) const {
164  return value(key);
165  }
166 
167  Value& value(const Key& key) {
168  typename QMap<Key, Value>::iterator it = this->find(key);
169  if (it == this->end()) {
170  throw ExceptionType("Key '", key, "' not found in map");
171  }
172  return it.value();
173  }
174 
175  inline Value& operator[](const Key& key) {
176  return value(key);
177  }
178 };
179 
180 template <typename Key, typename Value, typename ExceptionType>
181 Stringifier& operator<<(Stringifier& out, const GaiaMap<Key, Value, ExceptionType>& gmap) {
182  out << "{";
183  if (gmap.isEmpty()) return out << "}";
184  typename GaiaMap<Key, Value, ExceptionType>::const_iterator it = gmap.constBegin();
185  out << " " << it.key() << ": " << it.value();
186  for (++it; it != gmap.constEnd(); ++it) {
187  out << ", " << it.key() << ": " << it.value();
188  }
189  return out << " }";
190 }
191 
192 
193 
194 typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> MatrixRXf;
195 typedef Eigen::RowVectorXf FrozenPoint;
196 
197 
198 } // namespace gaia2
199 
200 #endif // GAIA_TYPES_H
Definition: debugging.h:82
Map class (derived from QMap) but that throws a custom Exception instead of returning a default const...
Definition: types.h:143
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
Definition: gvarlengtharray.h:77
Definition: types.h:73
Definition: keydistance.cpp:24