dmpwkbptr.h 3.0 KB
/**************************************************************************
* file:              dmpwkbptr.h

* Author:            qingxiongf
* Date:              2021-08-09 09:55:29
* Email:             qingxiongf@chinadci.com
* copyright:         广州城市信息研究所有限公司
***************************************************************************/

#ifndef __dmpwktptr_h__
#define __dmpwktptr_h__

#include "dmap_core.h"

class CORE_EXPORT DmpWktPtr
{
  private:
    mutable unsigned char *p_;
    unsigned char *start_;
    unsigned char *end_;

    void verifyBound( int size ) const;

    template<typename T> void read( T &v ) const
    {
      verifyBound( sizeof v );
      memcpy( &v, mP, sizeof v );
      mP += sizeof v;
    }

    template<typename T> void write( T &v ) const
    {
      verifyBound( sizeof v );
      memcpy( mP, &v, sizeof v );
      mP += sizeof v;
    }

    void write( const QByteArray &data ) const
    {
      verifyBound( data.length() );
      memcpy( mP, data.constData(), data.length() );
      mP += data.length();
    }

  public:
    //! Construct WKB pointer from QByteArray
    QgsWkbPtr( QByteArray &wkb);

    QgsWkbPtr( unsigned char *p SIP_ARRAY, int size SIP_ARRAYSIZE );

    inline const DmpWktPtr &operator>>( double &v ) const { read( v ); return *this; } 
    inline const DmpWktPtr &operator>>( float &r ) const { double v; read( v ); r = v; return *this; } 
    inline const DmpWktPtr &operator>>( int &v ) const { read( v ); return *this; } 
    inline const DmpWktPtr &operator>>( unsigned int &v ) const { read( v ); return *this; } 
    inline const DmpWktPtr &operator>>( char &v ) const { read( v ); return *this; } 
    inline const DmpWktPtr &operator>>( QgsWkbTypes::Type &v ) const { read( v ); return *this; } 

    inline DmpWktPtr &operator<<( double v ) { write( v ); return *this; } 
    inline DmpWktPtr &operator<<( float r ) { double v = r; write( v ); return *this; } 
    inline DmpWktPtr &operator<<( int v ) { write( v ); return *this; } 
    inline DmpWktPtr &operator<<( unsigned int v ) { write( v ); return *this; } 
    inline DmpWktPtr &operator<<( char v ) { write( v ); return *this; } 
    inline DmpWktPtr &operator<<( QgsWkbTypes::Type v ) { write( v ); return *this; } 
    inline DmpWktPtr &operator<<( const QByteArray &data ) { write( data ); return *this; } 

    inline void operator+=( int n ) { verifyBound( n ); p_ += n; } 
    inline operator unsigned char *() const { return p_; } 

    /**
     * \brief size
     * \note not available in Python bindings
     */
    inline int size() const { return mEnd - mStart; } SIP_SKIP

    /**
     * \brief remaining
     * \note not available in Python bindings
     */
    inline int remaining() const { return mEnd - mP; } SIP_SKIP

    /**
     * \brief writtenSize
     * \note not available in Python bindings
     */
    inline int writtenSize() const { return mP - mStart; } SIP_SKIP

}; 

#endif // __dmpwktptr_h__