QGIS API Documentation  3.6.0-Noosa (5873452)
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 
42 class CORE_EXPORT QgsWkbPtr
43 {
44  mutable unsigned char *mP;
45  unsigned char *mStart;
46  unsigned char *mEnd;
47 
48  void verifyBound( int size ) const;
49 
50  template<typename T> void read( T &v ) const
51  {
52  verifyBound( sizeof v );
53  memcpy( &v, mP, sizeof v );
54  mP += sizeof v;
55  }
56 
57  template<typename T> void write( T &v ) const
58  {
59  verifyBound( sizeof v );
60  memcpy( mP, &v, sizeof v );
61  mP += sizeof v;
62  }
63 
64  void write( const QByteArray &data ) const
65  {
66  verifyBound( data.length() );
67  memcpy( mP, data.constData(), data.length() );
68  mP += data.length();
69  }
70 
71  public:
73  QgsWkbPtr( QByteArray &wkb ) SIP_SKIP;
74 
75  QgsWkbPtr( unsigned char *p SIP_ARRAY, int size SIP_ARRAYSIZE );
76 
77  inline const QgsWkbPtr &operator>>( double &v ) const { read( v ); return *this; } SIP_SKIP
78  inline const QgsWkbPtr &operator>>( float &r ) const { double v; read( v ); r = v; return *this; } SIP_SKIP
79  inline const QgsWkbPtr &operator>>( int &v ) const { read( v ); return *this; } SIP_SKIP
80  inline const QgsWkbPtr &operator>>( unsigned int &v ) const { read( v ); return *this; } SIP_SKIP
81  inline const QgsWkbPtr &operator>>( char &v ) const { read( v ); return *this; } SIP_SKIP
82  inline const QgsWkbPtr &operator>>( QgsWkbTypes::Type &v ) const { read( v ); return *this; } SIP_SKIP
83 
85  inline QgsWkbPtr &operator<<( double v ) { write( v ); return *this; } SIP_SKIP
87  inline QgsWkbPtr &operator<<( float r ) { double v = r; write( v ); return *this; } SIP_SKIP
89  inline QgsWkbPtr &operator<<( int v ) { write( v ); return *this; } SIP_SKIP
91  inline QgsWkbPtr &operator<<( unsigned int v ) { write( v ); return *this; } SIP_SKIP
93  inline QgsWkbPtr &operator<<( char v ) { write( v ); return *this; } SIP_SKIP
95  inline QgsWkbPtr &operator<<( QgsWkbTypes::Type v ) { write( v ); return *this; } SIP_SKIP
97  inline QgsWkbPtr &operator<<( const QByteArray &data ) { write( data ); return *this; } SIP_SKIP
98 
99  inline void operator+=( int n ) { verifyBound( n ); mP += n; } SIP_SKIP
100 
101  inline operator unsigned char *() const { return mP; } SIP_SKIP
102 
107  inline int size() const { return mEnd - mStart; } SIP_SKIP
108 
113  inline int remaining() const { return mEnd - mP; } SIP_SKIP
114 
119  inline int writtenSize() const { return mP - mStart; } SIP_SKIP
120 };
121 
127 class CORE_EXPORT QgsConstWkbPtr
128 {
129  protected:
130  mutable unsigned char *mP;
131  unsigned char *mEnd;
132  mutable bool mEndianSwap;
134 
139  void verifyBound( int size ) const SIP_SKIP;
140 
145  template<typename T> void read( T &v ) const SIP_SKIP
146  {
147  verifyBound( sizeof v );
148  memcpy( &v, mP, sizeof( v ) );
149  mP += sizeof( v );
150  if ( mEndianSwap )
151  endian_swap( v );
152  }
153 
154  public:
156  explicit QgsConstWkbPtr( const QByteArray &wkb ) SIP_SKIP;
157  QgsConstWkbPtr( const unsigned char *p SIP_ARRAY, int size SIP_ARRAYSIZE );
158 
163  QgsWkbTypes::Type readHeader() const SIP_SKIP;
164 
165  inline const QgsConstWkbPtr &operator>>( double &v ) const { read( v ); return *this; } SIP_SKIP
166  inline const QgsConstWkbPtr &operator>>( float &r ) const { double v; read( v ); r = v; return *this; } SIP_SKIP
167  inline const QgsConstWkbPtr &operator>>( int &v ) const { read( v ); return *this; } SIP_SKIP
168  inline const QgsConstWkbPtr &operator>>( unsigned int &v ) const { read( v ); return *this; } SIP_SKIP
169  inline const QgsConstWkbPtr &operator>>( char &v ) const { read( v ); return *this; } SIP_SKIP
170 
172  virtual const QgsConstWkbPtr &operator>>( QPointF &point ) const; SIP_SKIP
174  virtual const QgsConstWkbPtr &operator>>( QPolygonF &points ) const; SIP_SKIP
175 
176  inline void operator+=( int n ) { verifyBound( n ); mP += n; } SIP_SKIP
177  inline void operator-=( int n ) { mP -= n; } SIP_SKIP
178 
179  inline operator const unsigned char *() const { return mP; } SIP_SKIP
180 
185  inline int remaining() const { return mEnd - mP; } SIP_SKIP
186 
187  private:
188  template<typename T> void endian_swap( T &value ) const SIP_SKIP
189  {
190  char *data = reinterpret_cast<char *>( &value );
191  std::size_t n = sizeof( value );
192  for ( std::size_t i = 0, m = n / 2; i < m; ++i )
193  {
194  std::swap( data[i], data[n - 1 - i] );
195  }
196  }
197 };
198 
199 #endif // QGSWKBPTR_H
const QgsWkbPtr & operator>>(int &v) const
Definition: qgswkbptr.h:79
const QgsWkbPtr & operator>>(double &v) const
Definition: qgswkbptr.h:77
int remaining() const
remaining
Definition: qgswkbptr.h:113
QgsWkbException(QString const &what)
Definition: qgswkbptr.h:33
unsigned char * mP
Definition: qgswkbptr.h:130
const QgsConstWkbPtr & operator>>(char &v) const
Definition: qgswkbptr.h:169
QgsWkbPtr & operator<<(const QByteArray &data)
Append data from a byte array.
Definition: qgswkbptr.h:97
QgsWkbPtr & operator<<(QgsWkbTypes::Type v)
Writes a WKB type value to the pointer.
Definition: qgswkbptr.h:95
void operator+=(int n)
Definition: qgswkbptr.h:99
const QgsConstWkbPtr & operator>>(float &r) const
Definition: qgswkbptr.h:166
QDataStream & operator>>(QDataStream &in, QgsFeature &feature)
Reads a feature from stream in into feature. QGIS version compatibility is not guaranteed.
Definition: qgsfeature.cpp:305
const QgsWkbPtr & operator>>(float &r) const
Definition: qgswkbptr.h:78
QgsWkbPtr & operator<<(unsigned int v)
Writes an unsigned int to the pointer.
Definition: qgswkbptr.h:91
QgsWkbPtr & operator<<(int v)
Writes an int to the pointer.
Definition: qgswkbptr.h:89
int writtenSize() const
writtenSize
Definition: qgswkbptr.h:119
const QgsWkbPtr & operator>>(char &v) const
Definition: qgswkbptr.h:81
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
const QgsConstWkbPtr & operator>>(double &v) const
Definition: qgswkbptr.h:165
#define SIP_ARRAYSIZE
Definition: qgis_sip.h:89
#define SIP_SKIP
Definition: qgis_sip.h:119
bool mEndianSwap
Definition: qgswkbptr.h:132
QgsWkbTypes::Type mWkbType
Definition: qgswkbptr.h:133
unsigned char * mEnd
Definition: qgswkbptr.h:131
Custom exception class for Wkb related exceptions.
Definition: qgswkbptr.h:30
QgsWkbPtr & operator<<(float r)
Writes a float to the pointer.
Definition: qgswkbptr.h:87
#define SIP_ARRAY
Definition: qgis_sip.h:84
int remaining() const
remaining
Definition: qgswkbptr.h:185
const QgsConstWkbPtr & operator>>(int &v) const
Definition: qgswkbptr.h:167
const QgsConstWkbPtr & operator>>(unsigned int &v) const
Definition: qgswkbptr.h:168
QgsWkbPtr & operator<<(char v)
Writes a char to the pointer.
Definition: qgswkbptr.h:93
int size() const
size
Definition: qgswkbptr.h:107
const QgsWkbPtr & operator>>(unsigned int &v) const
Definition: qgswkbptr.h:80
void operator+=(int n)
Definition: qgswkbptr.h:176
void operator-=(int n)
Definition: qgswkbptr.h:177
void read(T &v) const
Read a value.
Definition: qgswkbptr.h:145
Defines a QGIS exception class.
Definition: qgsexception.h:34
const QgsWkbPtr & operator>>(QgsWkbTypes::Type &v) const
Definition: qgswkbptr.h:82
QgsWkbPtr & operator<<(double v)
Writes a double to the pointer.
Definition: qgswkbptr.h:85