Skip to content

Commit

Permalink
Avoid ugly const casts
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 10, 2019
1 parent 3ae1b27 commit fb5aa9b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
13 changes: 5 additions & 8 deletions src/core/pal/pal.cpp
Expand Up @@ -146,10 +146,8 @@ std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeom
QMutexLocker locker( &layer->mMutex );

// find features within bounding box and generate candidates list
layer->mFeatureIndex.intersects( QgsRectangle( amin[ 0], amin[1], amax[0], amax[1] ), [&features, &obstacles, &prob, &mapBoundaryPrepared, this]( const FeaturePart * constFeaturePart )->bool
layer->mFeatureIndex.intersects( QgsRectangle( amin[ 0], amin[1], amax[0], amax[1] ), [&features, &obstacles, &prob, &mapBoundaryPrepared, this]( FeaturePart * featurePart )->bool
{
FeaturePart *featurePart = const_cast< FeaturePart * >( constFeaturePart );

if ( isCanceled() )
return false;

Expand Down Expand Up @@ -213,7 +211,7 @@ std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeom
return nullptr;

// find obstacles within bounding box
layer->mObstacleIndex.intersects( QgsRectangle( amin[0], amin[1], amax[0], amax[1] ), [&obstacles, &obstacleCount, this]( const FeaturePart * featurePart )->bool
layer->mObstacleIndex.intersects( QgsRectangle( amin[0], amin[1], amax[0], amax[1] ), [&obstacles, &obstacleCount, this]( FeaturePart * featurePart )->bool
{
if ( isCanceled() )
return false; // do not continue searching
Expand Down Expand Up @@ -254,14 +252,13 @@ std::unique_ptr<Problem> Pal::extract( const QgsRectangle &extent, const QgsGeom
// Filtering label positions against obstacles
amin[0] = amin[1] = std::numeric_limits<double>::lowest();
amax[0] = amax[1] = std::numeric_limits<double>::max();
obstacles.intersects( QgsRectangle( amin[0], amin[1], amax[0], amax[1] ), [&prob, this]( const FeaturePart * part )->bool
obstacles.intersects( QgsRectangle( amin[0], amin[1], amax[0], amax[1] ), [&prob, this]( FeaturePart * obstaclePart )->bool
{
if ( isCanceled() )
return false; // do not continue searching

prob->allCandidatesIndex().intersects( part->boundingBox(), [part, this]( const LabelPosition * candidatePosition ) -> bool{
FeaturePart *obstaclePart = const_cast< FeaturePart * >( part );

prob->allCandidatesIndex().intersects( obstaclePart->boundingBox(), [obstaclePart, this]( const LabelPosition * candidatePosition ) -> bool
{
// test whether we should ignore this obstacle for the candidate. We do this if:
// 1. it's not a hole, and the obstacle belongs to the same label feature as the candidate (e.g.,
// features aren't obstacles for their own labels)
Expand Down
14 changes: 7 additions & 7 deletions src/core/qgsgenericspatialindex.cpp
Expand Up @@ -41,7 +41,7 @@ template <typename T>
class GenericIndexVisitor : public SpatialIndex::IVisitor
{
public:
explicit GenericIndexVisitor( const std::function< bool( const T *data )> &callback, const QHash< qint64, const T * > &data )
explicit GenericIndexVisitor( const std::function< bool( T *data )> &callback, const QHash< qint64, T * > &data )
: mCallback( callback )
, mData( data )
{}
Expand All @@ -52,16 +52,16 @@ class GenericIndexVisitor : public SpatialIndex::IVisitor
void visitData( const SpatialIndex::IData &d ) override
{
qint64 id = d.getIdentifier();
const T *data = mData.value( id );
T *data = mData.value( id );
mCallback( data );
}

void visitData( std::vector<const SpatialIndex::IData *> &v ) override
{ Q_UNUSED( v ) }

private:
const std::function< bool( const T *data )> &mCallback;
QHash< qint64, const T * > mData;
const std::function< bool( T *data )> &mCallback;
QHash< qint64, T * > mData;
};

///@endcond
Expand All @@ -79,7 +79,7 @@ template<typename T>
QgsGenericSpatialIndex<T>::~QgsGenericSpatialIndex() = default;

template<typename T>
bool QgsGenericSpatialIndex<T>::insert( const T *data, const QgsRectangle &bounds )
bool QgsGenericSpatialIndex<T>::insert( T *data, const QgsRectangle &bounds )
{
SpatialIndex::Region r( QgsSpatialIndexUtils::rectangleToRegion( bounds ) );

Expand Down Expand Up @@ -112,7 +112,7 @@ bool QgsGenericSpatialIndex<T>::insert( const T *data, const QgsRectangle &bound
}

template<typename T>
bool QgsGenericSpatialIndex<T>::remove( const T *data, const QgsRectangle &bounds )
bool QgsGenericSpatialIndex<T>::remove( T *data, const QgsRectangle &bounds )
{
SpatialIndex::Region r = QgsSpatialIndexUtils::rectangleToRegion( bounds );

Expand All @@ -130,7 +130,7 @@ bool QgsGenericSpatialIndex<T>::remove( const T *data, const QgsRectangle &bound
}

template<typename T>
bool QgsGenericSpatialIndex<T>::intersects( const QgsRectangle &bounds, const std::function<bool ( const T * )> &callback ) const
bool QgsGenericSpatialIndex<T>::intersects( const QgsRectangle &bounds, const std::function<bool ( T * )> &callback ) const
{
GenericIndexVisitor<T> visitor( callback, mIdToData );
SpatialIndex::Region r = QgsSpatialIndexUtils::rectangleToRegion( bounds );
Expand Down
10 changes: 5 additions & 5 deletions src/core/qgsgenericspatialindex.h
Expand Up @@ -51,22 +51,22 @@ class CORE_EXPORT QgsGenericSpatialIndex
* Ownership of \a data is not transferred, and it is the caller's responsibility to ensure that
* it exists for the lifetime of the spatial index.
*/
bool insert( const T *data, const QgsRectangle &bounds );
bool insert( T *data, const QgsRectangle &bounds );

/**
* Removes existing \a data from the spatial index, with the specified \a bounds.
*
* \a data is not deleted, and it is the caller's responsibility to ensure that
* it is appropriately cleaned up.
*/
bool remove( const T *data, const QgsRectangle &bounds );
bool remove( T *data, const QgsRectangle &bounds );

/**
* Performs an intersection check against the index, for data intersecting the specified \a bounds.
*
* The \a callback function will be called once for each matching data object encountered.
*/
bool intersects( const QgsRectangle &bounds, const std::function< bool( const T *data )> &callback ) const;
bool intersects( const QgsRectangle &bounds, const std::function< bool( T *data )> &callback ) const;

private:

Expand All @@ -76,8 +76,8 @@ class CORE_EXPORT QgsGenericSpatialIndex
mutable QMutex mMutex;

qint64 mNextId = 1;
QHash< qint64, const T * > mIdToData;
QHash< const T *, qint64 > mDataToId;
QHash< qint64, T * > mIdToData;
QHash< T *, qint64 > mDataToId;

};

Expand Down

0 comments on commit fb5aa9b

Please sign in to comment.