Skip to content

Commit

Permalink
Modernize connection pool code
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 16, 2018
1 parent f3b5838 commit 6158236
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
11 changes: 6 additions & 5 deletions src/core/qgsconnectionpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#define SIP_NO_FILE

#include "qgis.h"
#include <QCoreApplication>
#include <QMap>
#include <QMutex>
Expand All @@ -34,7 +35,7 @@

/**
* \ingroup core
* Template that stores data related to one server.
* Template that stores data related to a connection to a single server or datasource.
*
* It is assumed that following functions exist:
* - void qgsConnectionPool_ConnectionCreate(QString name, T& c) ... create a new connection
Expand Down Expand Up @@ -74,7 +75,7 @@ class QgsConnectionPoolGroup

~QgsConnectionPoolGroup()
{
Q_FOREACH ( Item item, conns )
for ( const Item &item : qgis::as_const( conns ) )
{
qgsConnectionPool_ConnectionDestroy( item.c );
}
Expand Down Expand Up @@ -180,12 +181,12 @@ class QgsConnectionPoolGroup
void invalidateConnections()
{
connMutex.lock();
Q_FOREACH ( Item i, conns )
for ( const Item &i : qgis::as_const( conns ) )
{
qgsConnectionPool_ConnectionDestroy( i.c );
}
conns.clear();
Q_FOREACH ( T c, acquiredConns )
for ( T c : qgis::as_const( acquiredConns ) )
qgsConnectionPool_InvalidateConnection( c );
connMutex.unlock();
}
Expand Down Expand Up @@ -269,7 +270,7 @@ class QgsConnectionPool
virtual ~QgsConnectionPool()
{
mMutex.lock();
Q_FOREACH ( T_Group *group, mGroups )
for ( T_Group *group : qgis::as_const( mGroups ) )
{
delete group;
}
Expand Down
26 changes: 17 additions & 9 deletions src/providers/ogr/qgsogrconnpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,16 @@ class QgsOgrConnPoolGroup : public QObject, public QgsConnectionPoolGroup<QgsOgr
public:
explicit QgsOgrConnPoolGroup( const QString &name )
: QgsConnectionPoolGroup<QgsOgrConn*>( name )
, mRefCount( 0 )
{ initTimer( this ); }
{
initTimer( this );
}

//! QgsOgrConnPoolGroup cannot be copied
QgsOgrConnPoolGroup( const QgsOgrConnPoolGroup &other ) = delete;

//! QgsOgrConnPoolGroup cannot be copied
QgsOgrConnPoolGroup &operator=( const QgsOgrConnPoolGroup &other ) = delete;

void ref() { ++mRefCount; }
bool unref()
{
Expand All @@ -79,11 +87,8 @@ class QgsOgrConnPoolGroup : public QObject, public QgsConnectionPoolGroup<QgsOgr
void startExpirationTimer() { expirationTimer->start(); }
void stopExpirationTimer() { expirationTimer->stop(); }

protected:
Q_DISABLE_COPY( QgsOgrConnPoolGroup )

private:
int mRefCount;
int mRefCount = 0;

};

Expand All @@ -110,6 +115,12 @@ class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn *, QgsOgrConnPoolGrou
//
static void cleanupInstance();

//! QgsOgrConnPool cannot be copied
QgsOgrConnPool( const QgsOgrConnPool &other ) = delete;

//! QgsOgrConnPool cannot be copied
QgsOgrConnPool &operator=( const QgsOgrConnPool &other ) = delete;

/**
* \brief Increases the reference count on the connection pool for the specified connection.
* \param connInfo The connection string.
Expand Down Expand Up @@ -151,9 +162,6 @@ class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn *, QgsOgrConnPoolGrou
mMutex.unlock();
}

protected:
Q_DISABLE_COPY( QgsOgrConnPool )

private:
QgsOgrConnPool();
~QgsOgrConnPool() override;
Expand Down

0 comments on commit 6158236

Please sign in to comment.