QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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_sip.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 QgsRectangle;
37 class QgsFeature;
38 class QgsFeaturePrivate;
39 
40 // key = field index, value = field value
41 typedef QMap<int, QVariant> QgsAttributeMap;
42 
43 // key = field index, value = field name
44 typedef QMap<int, QString> QgsFieldNameMap;
45 
46 #ifdef SIP_RUN
47 typedef QMap<int, QgsField> QgsFieldMap;
48 #endif
49 
50 
56 #ifndef SIP_RUN
57 class CORE_EXPORT QgsAttributes : public QVector<QVariant>
58 {
59  public:
60 
62  QgsAttributes() = default;
63 
69  QgsAttributes( int size )
70  : QVector<QVariant>( size )
71  {}
72 
78  QgsAttributes( int size, const QVariant &v )
79  : QVector<QVariant>( size, v )
80  {}
81 
86  QgsAttributes( const QVector<QVariant> &v )
87  : QVector<QVariant>( v )
88  {}
89 
99  bool operator==( const QgsAttributes &v ) const
100  {
101  if ( size() != v.size() )
102  return false;
103  const QVariant *b = constData();
104  const QVariant *i = b + size();
105  const QVariant *j = v.constData() + size();
106  while ( i != b )
107  if ( !( *--i == *--j && i->isNull() == j->isNull() ) )
108  return false;
109  return true;
110  }
111 
118  QgsAttributeMap toMap() const SIP_SKIP;
119 
120  inline bool operator!=( const QgsAttributes &v ) const { return !( *this == v ); }
121 };
122 
124 CORE_EXPORT uint qHash( const QgsAttributes &attributes );
125 
126 #else
127 typedef QVector<QVariant> QgsAttributes;
128 
129 % MappedType QgsAttributes
130 {
131  % TypeHeaderCode
132 #include "qgsfeature.h"
133  % End
134 
135  % ConvertFromTypeCode
136  // Create the list.
137  PyObject *l;
138 
139  if ( ( l = PyList_New( sipCpp->size() ) ) == NULL )
140  return NULL;
141 
142  // Set the list elements.
143  for ( int i = 0; i < sipCpp->size(); ++i )
144  {
145  QVariant *v = new QVariant( sipCpp->at( i ) );
146  PyObject *tobj;
147 
148  if ( ( tobj = sipConvertFromNewType( v, sipType_QVariant, Py_None ) ) == NULL )
149  {
150  Py_DECREF( l );
151  delete v;
152 
153  return NULL;
154  }
155 
156  PyList_SET_ITEM( l, i, tobj );
157  }
158 
159  return l;
160  % End
161 
162  % ConvertToTypeCode
163  // Check the type if that is all that is required.
164  if ( sipIsErr == NULL )
165  {
166  if ( !PyList_Check( sipPy ) )
167  return 0;
168 
169  for ( SIP_SSIZE_T i = 0; i < PyList_GET_SIZE( sipPy ); ++i )
170  if ( !sipCanConvertToType( PyList_GET_ITEM( sipPy, i ), sipType_QVariant, SIP_NOT_NONE ) )
171  return 0;
172 
173  return 1;
174  }
175 
176  QgsAttributes *qv = new QgsAttributes;
177  SIP_SSIZE_T listSize = PyList_GET_SIZE( sipPy );
178  qv->reserve( listSize );
179 
180  for ( SIP_SSIZE_T i = 0; i < listSize; ++i )
181  {
182  PyObject *obj = PyList_GET_ITEM( sipPy, i );
183  if ( obj == Py_None )
184  {
185  qv->append( QVariant( QVariant::Int ) );
186  }
187  else
188  {
189  int state;
190  QVariant *t = reinterpret_cast<QVariant *>( sipConvertToType( obj, sipType_QVariant, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr ) );
191 
192  if ( *sipIsErr )
193  {
194  sipReleaseType( t, sipType_QVariant, state );
195 
196  delete qv;
197  return 0;
198  }
199 
200  qv->append( *t );
201  sipReleaseType( t, sipType_QVariant, state );
202  }
203  }
204 
205  *sipCppPtr = qv;
206 
207  return sipGetState( sipTransferObj );
208  % End
209 };
210 #endif
211 
212 
213 #endif // QGSATTRIBUTES_H
qgsfields.h
qHash
CORE_EXPORT uint qHash(const QgsAttributes &attributes)
Hash for QgsAttributes.
Definition: qgsattributes.cpp:33
QgsAttributes::QgsAttributes
QgsAttributes(const QVector< QVariant > &v)
Copies another vector of attributes.
Definition: qgsattributes.h:86
QgsAttributes::QgsAttributes
QgsAttributes(int size, const QVariant &v)
Constructs a vector with an initial size of size elements.
Definition: qgsattributes.h:78
qgsfeature.h
QgsAttributes::QgsAttributes
QgsAttributes(int size)
Create a new vector of attributes with the given size.
Definition: qgsattributes.h:69
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsAttributes::QgsAttributes
QgsAttributes()=default
Constructor for QgsAttributes.
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
qgis_sip.h
QgsAttributeMap
QMap< int, QVariant > QgsAttributeMap
Definition: qgsattributes.h:38
QgsFieldNameMap
QMap< int, QString > QgsFieldNameMap
Definition: qgsattributes.h:44
QgsAttributes
A vector of attributes.
Definition: qgsattributes.h:58
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsAttributes::operator==
bool operator==(const QgsAttributes &v) const
Compares two vectors of attributes.
Definition: qgsattributes.h:99