Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Store wkb pointer in QgsGeometry and move wkb, geos to GeosGeometryPr…
…ivate
  • Loading branch information
mhugent committed Jun 11, 2015
1 parent 1749733 commit ed39970
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
58 changes: 30 additions & 28 deletions src/core/geometry/qgsgeometry.cpp
Expand Up @@ -52,14 +52,17 @@ email : morb at ozemail dot com dot au

struct QgsGeometryPrivate
{
QgsGeometryPrivate(): ref( 1 ), geometry( 0 ), mWkb( 0 ), mWkbSize( 0 ), mGeos( 0 ) {}
~QgsGeometryPrivate() { delete geometry; delete[] mWkb; GEOSGeom_destroy_r( QgsGeos::getGEOSHandler(), mGeos ); }
QAtomicInt ref;
QgsAbstractGeometryV2* geometry;
mutable const unsigned char* mWkb; //store wkb pointer for backward compatibility
mutable int mWkbSize;
mutable GEOSGeometry* mGeos;
};

QgsGeometry::QgsGeometry(): d( new QgsGeometryPrivate() ), mWkb( 0 ), mWkbSize( 0 ), mGeos( 0 )
QgsGeometry::QgsGeometry(): d( new QgsGeometryPrivate() )
{
d->geometry = 0;
d->ref = QAtomicInt( 1 );
}

QgsGeometry::~QgsGeometry()
Expand All @@ -68,20 +71,18 @@ QgsGeometry::~QgsGeometry()
{
if ( !d->ref.deref() )
{
delete d->geometry;
delete d;
}
}
removeWkbGeos();
}

QgsGeometry::QgsGeometry( QgsAbstractGeometryV2* geom ): d( new QgsGeometryPrivate() ), mWkb( 0 ), mWkbSize( 0 ), mGeos( 0 )
QgsGeometry::QgsGeometry( QgsAbstractGeometryV2* geom ): d( new QgsGeometryPrivate() )
{
d->geometry = geom;
d->ref = QAtomicInt( 1 );
}

QgsGeometry::QgsGeometry( const QgsGeometry& other ): mWkb( 0 ), mWkbSize( 0 ), mGeos( 0 )
QgsGeometry::QgsGeometry( const QgsGeometry& other )
{
d = other.d;
d->ref.ref();
Expand All @@ -91,12 +92,9 @@ QgsGeometry& QgsGeometry::operator=( QgsGeometry const & other )
{
if ( !d->ref.deref() )
{
delete d->geometry;
delete d;
}

removeWkbGeos();

d = other.d;
d->ref.ref();
return *this;
Expand All @@ -109,29 +107,31 @@ void QgsGeometry::detach( bool cloneGeom )
return;
}

removeWkbGeos();

if ( d->ref > 1 )
{
d->ref.deref();
QgsAbstractGeometryV2* cGeom = 0;

if ( d->geometry && cloneGeom )
{
cGeom = d->geometry->clone();
}

d = new QgsGeometryPrivate();
d->geometry = cGeom;
d->ref = QAtomicInt( 1 );
}
}

void QgsGeometry::removeWkbGeos()
{
delete[] mWkb;
mWkb = 0;
mWkbSize = 0;
GEOSGeom_destroy( mGeos );
mGeos = 0;
delete[] d->mWkb;
d->mWkb = 0;
d->mWkbSize = 0;
if ( d->mGeos )
{
GEOSGeom_destroy_r( QgsGeos::getGEOSHandler(), d->mGeos );
d->mGeos = 0;
}
}

const QgsAbstractGeometryV2* QgsGeometry::geometry() const
Expand Down Expand Up @@ -244,7 +244,8 @@ void QgsGeometry::fromWkb( unsigned char *wkb, size_t length )
removeWkbGeos();
}
d->geometry = QgsGeometryImport::geomFromWkb( wkb );
delete[] wkb;
d->mWkb = wkb;
d->mWkbSize = length;
}

const unsigned char *QgsGeometry::asWkb() const
Expand All @@ -254,11 +255,11 @@ const unsigned char *QgsGeometry::asWkb() const
return 0;
}

if ( !mWkb )
if ( !d->mWkb )
{
mWkb = d->geometry->asWkb( mWkbSize );
d->mWkb = d->geometry->asWkb( d->mWkbSize );
}
return mWkb;
return d->mWkb;
}

size_t QgsGeometry::wkbSize() const
Expand All @@ -268,11 +269,11 @@ size_t QgsGeometry::wkbSize() const
return 0;
}

if ( !mWkb )
if ( !d->mWkb )
{
mWkb = d->geometry->asWkb( mWkbSize );
d->mWkb = d->geometry->asWkb( d->mWkbSize );
}
return mWkbSize;
return d->mWkbSize;
}

const GEOSGeometry* QgsGeometry::asGeos() const
Expand All @@ -282,11 +283,11 @@ const GEOSGeometry* QgsGeometry::asGeos() const
return 0;
}

if ( !mGeos )
if ( !d->mGeos )
{
mGeos = QgsGeos::asGeos( d->geometry );
d->mGeos = QgsGeos::asGeos( d->geometry );
}
return mGeos;
return d->mGeos;
}


Expand Down Expand Up @@ -328,6 +329,7 @@ void QgsGeometry::fromGeos( GEOSGeometry *geos )
detach( false );
delete d->geometry;
d->geometry = QgsGeos::fromGeos( geos );
d->mGeos = geos;
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/core/geometry/qgsgeometry.h
Expand Up @@ -705,9 +705,6 @@ class CORE_EXPORT QgsGeometry
private:

QgsGeometryPrivate* d; //implicitely shared data pointer
mutable const unsigned char* mWkb; //store wkb pointer for backward compatibility
mutable int mWkbSize;
mutable GEOSGeometry* mGeos;

void detach( bool cloneGeom = true ); //make sure mGeometry only referenced from this instance
void removeWkbGeos();
Expand Down

0 comments on commit ed39970

Please sign in to comment.