Skip to content

Commit

Permalink
Ensure that accessing QgsFieldFormatterRegistry is thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 7, 2020
1 parent 28b49d5 commit 344bc5f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
Expand Up @@ -9,6 +9,7 @@




class QgsFieldFormatterRegistry : QObject
{
%Docstring
Expand Down
7 changes: 6 additions & 1 deletion src/core/qgsfieldformatterregistry.cpp
Expand Up @@ -26,7 +26,7 @@
#include "qgsrangefieldformatter.h"
#include "qgscheckboxfieldformatter.h"
#include "qgsfallbackfieldformatter.h"

#include "qgsreadwritelocker.h"

QgsFieldFormatterRegistry::QgsFieldFormatterRegistry( QObject *parent )
: QObject( parent )
Expand All @@ -45,13 +45,16 @@ QgsFieldFormatterRegistry::QgsFieldFormatterRegistry( QObject *parent )

QgsFieldFormatterRegistry::~QgsFieldFormatterRegistry()
{
QgsReadWriteLocker locker( mLock, QgsReadWriteLocker::Write );
qDeleteAll( mFieldFormatters );
delete mFallbackFieldFormatter;
}

void QgsFieldFormatterRegistry::addFieldFormatter( QgsFieldFormatter *formatter )
{
QgsReadWriteLocker locker( mLock, QgsReadWriteLocker::Write );
mFieldFormatters.insert( formatter->id(), formatter );
locker.unlock();
emit fieldFormatterAdded( formatter );
}

Expand All @@ -62,6 +65,7 @@ void QgsFieldFormatterRegistry::removeFieldFormatter( QgsFieldFormatter *formatt

void QgsFieldFormatterRegistry::removeFieldFormatter( const QString &id )
{
QgsReadWriteLocker locker( mLock, QgsReadWriteLocker::Write );
if ( QgsFieldFormatter *formatter = mFieldFormatters.take( id ) )
{
emit fieldFormatterRemoved( formatter );
Expand All @@ -71,6 +75,7 @@ void QgsFieldFormatterRegistry::removeFieldFormatter( const QString &id )

QgsFieldFormatter *QgsFieldFormatterRegistry::fieldFormatter( const QString &id ) const
{
QgsReadWriteLocker locker( mLock, QgsReadWriteLocker::Read );
return mFieldFormatters.value( id, mFallbackFieldFormatter );
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsfieldformatterregistry.h
Expand Up @@ -23,6 +23,8 @@
#include "qgis_sip.h"
#include "qgis_core.h"

#include <QReadWriteLock>

class QgsFieldFormatter;
class QgsVectorLayer;

Expand Down Expand Up @@ -94,6 +96,7 @@ class CORE_EXPORT QgsFieldFormatterRegistry : public QObject
private:
QHash<QString, QgsFieldFormatter *> mFieldFormatters;
QgsFieldFormatter *mFallbackFieldFormatter = nullptr;
mutable QReadWriteLock mLock;
};

#endif // QGSFIELDKITREGISTRY_H

0 comments on commit 344bc5f

Please sign in to comment.