QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsattributes.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsattributes.h - QgsAttributes
3 
4  ---------------------
5  begin : 29.3.2017
6  copyright : (C) 2017 by Denis Rouzaud
7  email : [email protected]
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 
18 #ifndef QGSATTRIBUTES_H
19 #define QGSATTRIBUTES_H
20 
21 #include "qgis_core.h"
22 #include "qgis.h"
23 
24 #include <QMap>
25 #include <QString>
26 #include <QVariant>
27 #include <QList>
28 #include <QVector>
29 #include <QSet>
30 #include <QExplicitlySharedDataPointer>
31 
32 
33 #include "qgsfields.h"
34 
35 
36 class QgsGeometry;
37 class QgsRectangle;
38 class QgsFeature;
39 class QgsFeaturePrivate;
40 
41 // key = field index, value = field value
42 typedef QMap<int, QVariant> QgsAttributeMap;
43 
44 // key = field index, value = field name
45 typedef QMap<int, QString> QgsFieldNameMap;
46 
47 #ifdef SIP_RUN
48 typedef QMap<int, QgsField> QgsFieldMap;
49 #endif
50 
51 
57 #ifndef SIP_RUN
58 class CORE_EXPORT QgsAttributes : public QVector<QVariant>
59 {
60  public:
61 
63  QgsAttributes() = default;
64 
70  QgsAttributes( int size )
71  : QVector<QVariant>( size )
72  {}
73 
79  QgsAttributes( int size, const QVariant &v )
80  : QVector<QVariant>( size, v )
81  {}
82 
87  QgsAttributes( const QVector<QVariant> &v )
88  : QVector<QVariant>( v )
89  {}
90 
100  bool operator==( const QgsAttributes &v ) const
101  {
102  if ( size() != v.size() )
103  return false;
104  const QVariant *b = constData();
105  const QVariant *i = b + size();
106  const QVariant *j = v.constData() + size();
107  while ( i != b )
108  if ( !( *--i == *--j && i->isNull() == j->isNull() ) )
109  return false;
110  return true;
111  }
112 
119  QgsAttributeMap toMap() const SIP_SKIP;
120 
121  inline bool operator!=( const QgsAttributes &v ) const { return !( *this == v ); }
122 };
123 
125 CORE_EXPORT uint qHash( const QgsAttributes &attributes );
126 
127 #else
128 typedef QVector<QVariant> QgsAttributes;
129 
130 % MappedType QgsAttributes
131 {
132  % TypeHeaderCode
133 #include <qgsfeature.h>
134  % End
135 
136  % ConvertFromTypeCode
137  // Create the list.
138  PyObject *l;
139 
140  if ( ( l = PyList_New( sipCpp->size() ) ) == NULL )
141  return NULL;
142 
143  // Set the list elements.
144  for ( int i = 0; i < sipCpp->size(); ++i )
145  {
146  QVariant *v = new QVariant( sipCpp->at( i ) );
147  PyObject *tobj;
148 
149  if ( ( tobj = sipConvertFromNewType( v, sipType_QVariant, Py_None ) ) == NULL )
150  {
151  Py_DECREF( l );
152  delete v;
153 
154  return NULL;
155  }
156 
157  PyList_SET_ITEM( l, i, tobj );
158  }
159 
160  return l;
161  % End
162 
163  % ConvertToTypeCode
164  // Check the type if that is all that is required.
165  if ( sipIsErr == NULL )
166  {
167  if ( !PyList_Check( sipPy ) )
168  return 0;
169 
170  for ( SIP_SSIZE_T i = 0; i < PyList_GET_SIZE( sipPy ); ++i )
171  if ( !sipCanConvertToType( PyList_GET_ITEM( sipPy, i ), sipType_QVariant, SIP_NOT_NONE ) )
172  return 0;
173 
174  return 1;
175  }
176 
177  QgsAttributes *qv = new QgsAttributes;
178  SIP_SSIZE_T listSize = PyList_GET_SIZE( sipPy );
179  qv->reserve( listSize );
180 
181  for ( SIP_SSIZE_T i = 0; i < listSize; ++i )
182  {
183  PyObject *obj = PyList_GET_ITEM( sipPy, i );
184  if ( obj == Py_None )
185  {
186  qv->append( QVariant( QVariant::Int ) );
187  }
188  else
189  {
190  int state;
191  QVariant *t = reinterpret_cast<QVariant *>( sipConvertToType( obj, sipType_QVariant, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr ) );
192 
193  if ( *sipIsErr )
194  {
195  sipReleaseType( t, sipType_QVariant, state );
196 
197  delete qv;
198  return 0;
199  }
200 
201  qv->append( *t );
202  sipReleaseType( t, sipType_QVariant, state );
203  }
204  }
205 
206  *sipCppPtr = qv;
207 
208  return sipGetState( sipTransferObj );
209  % End
210 };
211 #endif
212 
213 
214 #endif // QGSATTRIBUTES_H
CORE_EXPORT uint qHash(const QgsAttributes &attributes)
Hash for QgsAttributes.
A rectangle specified with double values.
Definition: qgsrectangle.h:40
QgsAttributes(const QVector< QVariant > &v)
Copies another vector of attributes.
Definition: qgsattributes.h:87
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
#define SIP_SKIP
Definition: qgis_sip.h:119
QMap< int, QVariant > QgsAttributeMap
Definition: qgsattributes.h:39
QgsAttributes(int size, const QVariant &v)
Constructs a vector with an initial size of size elements.
Definition: qgsattributes.h:79
QgsAttributes(int size)
Create a new vector of attributes with the given size.
Definition: qgsattributes.h:70
QMap< int, QString > QgsFieldNameMap
Definition: qgsattributes.h:45
bool operator!=(const QgsAttributes &v) const
A vector of attributes.
Definition: qgsattributes.h:58
bool operator==(const QgsAttributes &v) const
Compares two vectors of attributes.