Gaia
pointlayout.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_POINTLAYOUT_H
21 #define GAIA_POINTLAYOUT_H
22 
23 #include <QSharedData>
24 #include <QDataStream>
25 #include <QString>
26 #include "descriptortree.h"
27 #include "descriptor.h"
28 
29 namespace gaia2 {
30 
31 
32 typedef QMap<Enum, QString> EnumMap;
33 typedef QMap<QString, Enum> ReverseEnumMap;
34 
35 class PointLayoutData : public QSharedData {
36  public:
37  QString name; // only used for debugging
38 
39  // each node is a QString, each leaf is a segment.
40  // an entire region doesn't have to be stored in each node, as it can be
41  // created dynamically on demand by merging all leaves children of the
42  // specified node
43  // contains real, label, etc... which are in fact encoded in each individual
44  // segment
45  DescriptorTree* tree;
46 
47  // global maps that hold the conversions enum<->string for all descriptors
48  QMap<QString, EnumMap> enum2str;
49  QMap<QString, ReverseEnumMap> str2enum;
50 
52  PointLayoutData(const PointLayoutData& rhs);
53  ~PointLayoutData();
54 };
55 
60 class PointLayout {
61 
62  public:
63 
64  PointLayout();
65  PointLayout(const PointLayout& other);
66  ~PointLayout();
67 
72  PointLayout copy() const;
73 
78  Region descriptorLocation(const QString& name) const;
79 
84  Region descriptorLocation(const QStringList& names) const;
85 
89  QString descriptorName(DescriptorType type, DescriptorLengthType ltype, int index) const;
90 
94  QStringList descriptorNames() const;
95 
99  QStringList descriptorNames(const Region& region) const;
100 
105  QStringList descriptorNames(const QStringList& patterns) const;
106 
113  QStringList descriptorNames(DescriptorType type, const QString& pattern) const;
114 
124  QStringList descriptorNames(DescriptorType type,
125  const QStringList& patterns = QStringList("*"),
126  bool failOnUnmatched = true) const;
127 
131  StringDescriptor enumToString(const QString& name, const EnumDescriptor& value) const;
132 
136  QString enumToString(const QString& name, const Enum& value) const;
137 
142  EnumDescriptor stringToEnum(const QString& name, const StringDescriptor& str) const;
143 
148  Enum stringToEnum(const QString& name, const QString& str) const;
149 
154  bool isEmpty() const { return _d->tree->isLeaf(); }
155 
159  bool subsetOf(const PointLayout& layout) const;
160 
166  void add(const QString& nodeName, DescriptorType type = UndefinedType,
167  DescriptorLengthType ltype = VariableLength, int size = 1);
168 
174  void add(const QString& parentName, const QString& childName,
175  DescriptorType type = UndefinedType,
176  DescriptorLengthType ltype = VariableLength, int size = 1);
177 
178 
182  void remove(const QString& name);
183 
188  void remove(const QStringList& names);
189 
196  void fixLength(const QString& name, int size);
197 
202  void fixLength(const QList<QPair<QString, int> >& descs);
203 
209  void enumerate(const QString& name);
210 
215  PointLayout operator&(const PointLayout& other) const;
216 
222  QStringList symmetricDifferenceWith(const PointLayout& layout) const;
223 
229  QStringList differenceWith(const PointLayout& layout) const;
230 
242  bool canMorphInto(const PointLayout& targetLayout) const;
243 
250  Point* morphPoint(const Point* point) const;
251 
258  void filter(const QStringList& select, const QStringList& exclude);
259 
260  void debug() const;
261 
267  QString toYaml() const;
268 
272  QString fullName(const QString& name) const;
273 
278  int regionSize(DescriptorType type, DescriptorLengthType ltype) const;
279 
280  Region correspondingRegion() const;
281 
286  bool operator==(const PointLayout& rhs) const;
287  bool operator!=(const PointLayout& rhs) const;
288 
294  bool sameObjectAs(const PointLayout& layout) const;
295 
296  bool sameEnumMap(const PointLayout& layout, const QString& enumName) const;
297 
301  int ref() const { return _d->ref; }
302 
303  friend QDataStream& operator<<(QDataStream& out, const PointLayout& layout);
304  friend QDataStream& operator>>(QDataStream& in, PointLayout& layout);
305 
306  protected:
307 
308  QExplicitlySharedDataPointer<PointLayoutData> _d;
309 
310  friend class Point; // for access to *NoUpdate methods
311  friend PointLayout mergeLayouts(const PointLayout& layout1, const PointLayout& layout2);
312 
313  void addNoUpdate(const QString& parentName, const QString& childName,
314  DescriptorType type = UndefinedType,
315  DescriptorLengthType ltype = VariableLength, int size = 1);
316 
317  void addNoUpdateFromRoot(const QString& parentName, const QString& childName,
318  DescriptorType type = UndefinedType,
319  DescriptorLengthType ltype = VariableLength, int size = 1);
320 
321  void removeNoUpdate(const QString& name);
322 
323  void update() { _d->tree->updateLayout(); }
324 
325 };
326 
327 } // namespace gaia2
328 
329 #endif // GAIA_POINTLAYOUT_H
A region is a physical location in the point layout which consists in a list of segments.
Definition: region.h:125
PointLayout mergeLayouts(const PointLayout &layout1, const PointLayout &layout2)
Merges two PointLayouts together, provided that they don&#39;t overlap.
Definition: utils.cpp:351
This class describes the layout of a point.
Definition: pointlayout.h:60
Definition: descriptortree.h:37
DescriptorLengthType
Either fixed-length or variable-length.
Definition: region.h:46
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
Definition: pointlayout.h:35
bool isEmpty() const
Returns true when this layout is the empty layout (eg: just contains a root node without children)...
Definition: pointlayout.h:154
DescriptorType
The possible types of descriptors accepted.
Definition: region.h:36
Definition: point.h:106
int ref() const
Reserved for internal testing.
Definition: pointlayout.h:301