Gaia
region.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_REGION_H
21 #define GAIA_REGION_H
22 
23 #include <QString>
24 #include <QList>
25 #include <QDataStream>
26 #include "gaia.h"
27 
28 namespace gaia2 {
29 
30 class Point;
31 class PointLayout;
32 
37  UndefinedType,
38  RealType,
39  StringType,
40  EnumType
41 };
42 
47  FixedLength,
48  VariableLength
49 };
50 
51 inline QString typeToString(DescriptorType type) {
52  switch (type) {
53  case UndefinedType: return "Undefined";
54  case RealType: return "Real";
55  case StringType: return "String";
56  case EnumType: return "Enum";
57  }
58  return QString("Error in type conversion");
59 }
60 
61 inline QString lengthTypeToString(DescriptorLengthType ltype) {
62  switch (ltype) {
63  case FixedLength: return "fixed-length";
64  case VariableLength: return "variable-length";
65  }
66  return QString("Error in length type conversion");
67 }
68 
75 class Segment {
76  public:
77  QString name;
78  DescriptorType type;
80  int begin;
81  int end;
82 
83  Segment() : name(""), type(UndefinedType), ltype(VariableLength), begin(-1), end(-1) {}
84 
85  Segment(QString name, DescriptorType type) :
86  name(name), type(type), ltype(VariableLength),
87  begin(-1), end(-1) {}
88 
89  Segment(QString name, DescriptorType type, DescriptorLengthType ltype,
90  int begin, int end) :
91  name(name), type(type), ltype(ltype), begin(begin), end(end) {}
92 
93  bool operator==(const Segment& rhs) const {
94  return (type == rhs.type) && (begin == rhs.begin) && (ltype == rhs.ltype) &&
95  (end == rhs.end);
96  }
97 
98  bool operator<(const Segment& rhs) const {
99  return ((this->type < rhs.type) ||
100  ((this->type == rhs.type) && (this->ltype < rhs.ltype)) ||
101  ((this->type == rhs.type) && (this->ltype == rhs.ltype) && (this->begin < rhs.begin)) ||
102  ((this->type == rhs.type) && (this->ltype == rhs.ltype) && (this->begin == rhs.begin) && (this->end < rhs.end)));
103  }
104 
105  inline int size() const {
106  return end - begin;
107  }
108 
109  QString toString() const;
110 
115 
116  friend QDataStream& operator<<(QDataStream& out, const Segment& segment);
117  friend QDataStream& operator>>(QDataStream& in, Segment& segment);
118 };
119 
120 
125 class Region {
126  public:
127  QString name;
128  QList<Segment> segments;
129 
133  Region& merge(const Region& r);
134 
141  DescriptorType type() const;
142 
149  DescriptorLengthType lengthType() const;
150 
157  int index() const;
158 
164  int index(DescriptorType type, DescriptorLengthType ltype) const;
165 
170  int index(DescriptorLengthType ltype) const;
171 
179  void checkTypeOnlyFrom(DescriptorType type, const PointLayout* layout = 0) const;
180 
184  bool isTypeOnlyFrom(DescriptorType type) const;
185 
194  void checkLengthTypeOnlyFrom(DescriptorLengthType ltype, const PointLayout* layout = 0) const;
195 
199  bool isLengthTypeOnlyFrom(DescriptorLengthType type) const;
200 
205  void checkSingleDescriptor() const;
206 
212  int dimension(DescriptorType type = UndefinedType, const Point* p = 0) const;
213 
214 
219  const Segment& segment() const;
220 
225  const Segment& segment(DescriptorType type) const;
226 
231  int size(DescriptorType type, DescriptorLengthType ltype) const;
232 
233  // isn't it better to access directly to the segments member variable?
234  QVector<int> listIndices(DescriptorType type, DescriptorLengthType ltype) const;
235 
240  Region select(DescriptorType type) const;
241 
246  Region select(DescriptorType type, DescriptorLengthType ltype) const;
247 
248  QString toString() const;
249 
250 
251  // debug purposes only
252  void debug();
253 
258  void canonical();
259 
260 };
261 
262 
263 } // namespace gaia2
264 
265 #endif // GAIA_REGION_H
A region is a physical location in the point layout which consists in a list of segments.
Definition: region.h:125
static Segment undefined
Defines a default undefined Segment.
Definition: region.h:114
This class describes the layout of a point.
Definition: pointlayout.h:60
A segment is a physical contiguous location of descriptors of the same type in the point layout...
Definition: region.h:75
DescriptorLengthType
Either fixed-length or variable-length.
Definition: region.h:46
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
DescriptorType
The possible types of descriptors accepted.
Definition: region.h:36
Definition: point.h:106