QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgswkbptr.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgswkbptr.h
3  ---------------------
4  begin : January 2014
5  copyright : (C) 2014 by Juergen E. Fischer
6  email : jef at norbit dot de
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSWKBPTR_H
16 #define QGSWKBPTR_H
17 
18 #include "qgis_core.h"
19 #include "qgswkbtypes.h"
20 #include "qgis_sip.h"
21 #include "qgsexception.h"
22 #include "qpolygon.h"
23 
29 #ifndef SIP_RUN
30 class CORE_EXPORT QgsWkbException : public QgsException
31 {
32  public:
33  QgsWkbException( QString const &what ) : QgsException( what ) {}
34 };
35 #endif
36 
37 
43 class CORE_EXPORT QgsWkbPtr
44 {
45  mutable unsigned char *mP;
46  unsigned char *mStart;
47  unsigned char *mEnd;
48 
49  void verifyBound( int size ) const;
50 
51  template<typename T> void read( T &v ) const
52  {
53  verifyBound( sizeof v );
54  memcpy( &v, mP, sizeof v );
55  mP += sizeof v;
56  }
57 
58  template<typename T> void write( T &v ) const
59  {
60  verifyBound( sizeof v );
61  memcpy( mP, &v, sizeof v );
62  mP += sizeof v;
63  }
64 
65  void write( const QByteArray &data ) const
66  {
67  verifyBound( data.length() );
68  memcpy( mP, data.constData(), data.length() );
69  mP += data.length();
70  }
71 
72  public:
74  QgsWkbPtr( QByteArray &wkb ) SIP_SKIP;
75 
76  QgsWkbPtr( unsigned char *p SIP_ARRAY, int size SIP_ARRAYSIZE );
77 
78  inline const QgsWkbPtr &operator>>( double &v ) const { read( v ); return *this; } SIP_SKIP
79  inline const QgsWkbPtr &operator>>( float &r ) const { double v; read( v ); r = v; return *this; } SIP_SKIP
80  inline const QgsWkbPtr &operator>>( int &v ) const { read( v ); return *this; } SIP_SKIP
81 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
83  inline const QgsWkbPtr &operator>>( qsizetype &r ) const { int v; read( v ); r = v; return *this; } SIP_SKIP
84 #endif
85  inline const QgsWkbPtr &operator>>( unsigned int &v ) const { read( v ); return *this; } SIP_SKIP
86  inline const QgsWkbPtr &operator>>( char &v ) const { read( v ); return *this; } SIP_SKIP
87  inline const QgsWkbPtr &operator>>( QgsWkbTypes::Type &v ) const { read( v ); return *this; } SIP_SKIP
88 
90  inline QgsWkbPtr &operator<<( double v ) { write( v ); return *this; } SIP_SKIP
92  inline QgsWkbPtr &operator<<( float r ) { double v = r; write( v ); return *this; } SIP_SKIP
94  inline QgsWkbPtr &operator<<( int v ) { write( v ); return *this; } SIP_SKIP
95 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
97  inline QgsWkbPtr &operator<<( qsizetype r ) { int v = r; write( v ); return *this; } SIP_SKIP
98 #endif
100  inline QgsWkbPtr &operator<<( unsigned int v ) { write( v ); return *this; } SIP_SKIP
102  inline QgsWkbPtr &operator<<( char v ) { write( v ); return *this; } SIP_SKIP
104  inline QgsWkbPtr &operator<<( QgsWkbTypes::Type v ) { write( v ); return *this; } SIP_SKIP
106  inline QgsWkbPtr &operator<<( const QByteArray &data ) { write( data ); return *this; } SIP_SKIP
107 
108  inline void operator+=( int n ) { verifyBound( n ); mP += n; } SIP_SKIP
109 
110  inline operator unsigned char *() const { return mP; } SIP_SKIP
111 
116  inline int size() const { return mEnd - mStart; } SIP_SKIP
117 
122  inline int remaining() const { return mEnd - mP; } SIP_SKIP
123 
128  inline int writtenSize() const { return mP - mStart; } SIP_SKIP
129 };
130 
137 class CORE_EXPORT QgsConstWkbPtr
138 {
139  protected:
140  mutable unsigned char *mP;
141  unsigned char *mEnd;
142  mutable bool mEndianSwap;
144 
149  void verifyBound( int size ) const SIP_SKIP;
150 
155  template<typename T> void read( T &v ) const SIP_SKIP
156  {
157  verifyBound( sizeof v );
158  memcpy( &v, mP, sizeof( v ) );
159  mP += sizeof( v );
160  if ( mEndianSwap )
161  endian_swap( v );
162  }
163 
164  public:
166  explicit QgsConstWkbPtr( const QByteArray &wkb ) SIP_SKIP;
167  QgsConstWkbPtr( const unsigned char *p SIP_ARRAY, int size SIP_ARRAYSIZE );
168 
173  QgsWkbTypes::Type readHeader() const SIP_SKIP;
174 
175  inline const QgsConstWkbPtr &operator>>( double &v ) const { read( v ); return *this; } SIP_SKIP
176  inline const QgsConstWkbPtr &operator>>( float &r ) const { double v; read( v ); r = v; return *this; } SIP_SKIP
177  inline const QgsConstWkbPtr &operator>>( int &v ) const { read( v ); return *this; } SIP_SKIP
178  inline const QgsConstWkbPtr &operator>>( unsigned int &v ) const { read( v ); return *this; } SIP_SKIP
179  inline const QgsConstWkbPtr &operator>>( char &v ) const { read( v ); return *this; } SIP_SKIP
180 
182  const QgsConstWkbPtr &operator>>( QPointF &point ) const; SIP_SKIP
184  const QgsConstWkbPtr &operator>>( QPolygonF &points ) const; SIP_SKIP
185 
186  inline void operator+=( int n ) { verifyBound( n ); mP += n; } SIP_SKIP
187  inline void operator-=( int n ) { mP -= n; } SIP_SKIP
188 
189  inline operator const unsigned char *() const { return mP; } SIP_SKIP
190 
195  inline int remaining() const { return mEnd - mP; } SIP_SKIP
196 
197  private:
198  template<typename T> void endian_swap( T &value ) const SIP_SKIP
199  {
200  char *data = reinterpret_cast<char *>( &value );
201  const std::size_t n = sizeof( value );
202  for ( std::size_t i = 0, m = n / 2; i < m; ++i )
203  {
204  std::swap( data[i], data[n - 1 - i] );
205  }
206  }
207 };
208 
209 #endif // QGSWKBPTR_H
A const WKB pointer.
Definition: qgswkbptr.h:138
QgsWkbTypes::Type mWkbType
Definition: qgswkbptr.h:143
const QgsConstWkbPtr & operator>>(float &r) const
Definition: qgswkbptr.h:176
int remaining() const
remaining
Definition: qgswkbptr.h:195
void read(T &v) const
Read a value.
Definition: qgswkbptr.h:155
const QgsConstWkbPtr & operator>>(char &v) const
Definition: qgswkbptr.h:179
bool mEndianSwap
Definition: qgswkbptr.h:142
void operator+=(int n)
Definition: qgswkbptr.h:186
void operator-=(int n)
Definition: qgswkbptr.h:187
unsigned char * mP
Definition: qgswkbptr.h:140
const QgsConstWkbPtr & operator>>(unsigned int &v) const
Definition: qgswkbptr.h:178
const QgsConstWkbPtr & operator>>(int &v) const
Definition: qgswkbptr.h:177
unsigned char * mEnd
Definition: qgswkbptr.h:141
Defines a QGIS exception class.
Definition: qgsexception.h:35
Custom exception class for Wkb related exceptions.
Definition: qgswkbptr.h:31
QgsWkbException(QString const &what)
Definition: qgswkbptr.h:33
WKB pointer handler.
Definition: qgswkbptr.h:44
const QgsWkbPtr & operator>>(QgsWkbTypes::Type &v) const
Definition: qgswkbptr.h:87
const QgsWkbPtr & operator>>(unsigned int &v) const
Definition: qgswkbptr.h:85
QgsWkbPtr & operator<<(char v)
Writes a char to the pointer.
Definition: qgswkbptr.h:102
QgsWkbPtr & operator<<(int v)
Writes an int to the pointer.
Definition: qgswkbptr.h:94
const QgsWkbPtr & operator>>(float &r) const
Definition: qgswkbptr.h:79
QgsWkbPtr & operator<<(unsigned int v)
Writes an unsigned int to the pointer.
Definition: qgswkbptr.h:100
QgsWkbPtr & operator<<(float r)
Writes a float to the pointer.
Definition: qgswkbptr.h:92
int remaining() const
remaining
Definition: qgswkbptr.h:122
const QgsWkbPtr & operator>>(int &v) const
Definition: qgswkbptr.h:80
const QgsWkbPtr & operator>>(double &v) const
Definition: qgswkbptr.h:78
QgsWkbPtr & operator<<(QgsWkbTypes::Type v)
Writes a WKB type value to the pointer.
Definition: qgswkbptr.h:104
QgsWkbPtr & operator<<(qsizetype r)
Writes a size as int to the pointer.
Definition: qgswkbptr.h:97
const QgsWkbPtr & operator>>(qsizetype &r) const
Reads an integer value into a qsizetype.
Definition: qgswkbptr.h:83
int writtenSize() const
writtenSize
Definition: qgswkbptr.h:128
QgsWkbPtr & operator<<(double v)
Writes a double to the pointer.
Definition: qgswkbptr.h:90
QgsWkbPtr & operator<<(const QByteArray &data)
Append data from a byte array.
Definition: qgswkbptr.h:106
int size() const
size
Definition: qgswkbptr.h:116
void operator+=(int n)
Definition: qgswkbptr.h:108
const QgsWkbPtr & operator>>(char &v) const
Definition: qgswkbptr.h:86
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
#define SIP_ARRAY
Definition: qgis_sip.h:91
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_ARRAYSIZE
Definition: qgis_sip.h:96
QDataStream & operator>>(QDataStream &in, QgsFeature &feature)
Reads a feature from stream in into feature. QGIS version compatibility is not guaranteed.
Definition: qgsfeature.cpp:415