Skip to content
Permalink
Browse files

avoid required call to leaveCategory by adding a RAII private class

  • Loading branch information
3nids committed Feb 28, 2018
1 parent 8989292 commit 8582517966bde714f4b8b8423f0c53c4a878b5b2
@@ -9,6 +9,7 @@




class QgsReadWriteContext
{
%Docstring
@@ -63,26 +64,13 @@ Returns path resolver for conversion between relative and absolute paths
Sets up path resolver for conversion between relative and absolute paths
%End

void pushMessage( const QString &message, Qgis::MessageLevel level );
void pushMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning );
%Docstring
Append a message to the context

.. versionadded:: 3.2
%End

void enterCategory( const QString &category, const QString &details = QString() );
%Docstring
Push a category to the stack

.. versionadded:: 3.2
%End

void leaveCategory();
%Docstring
Pop the last category

.. versionadded:: 3.2
%End

QList<QgsReadWriteContext::ReadWriteMessage> takeMessages();
%Docstring
@@ -91,8 +79,10 @@ Return the stored messages and remove them
.. versionadded:: 3.2
%End


};


/************************************************************************
* This file has been generated automatically from *
* *
@@ -260,7 +260,8 @@ void QgsEditFormConfig::setSuppress( QgsEditFormConfig::FeatureFormSuppress s )

void QgsEditFormConfig::readXml( const QDomNode &node, QgsReadWriteContext &context )
{
context.enterCategory( QObject::tr( "Edit form config" ) );
MAYBE_UNUSED QgsReadWriteContextCategoryPopper p = context.enterCategory( QObject::tr( "Edit form config" ) );

d.detach();

QDomNode editFormNode = node.namedItem( QStringLiteral( "editform" ) );
@@ -388,8 +389,6 @@ void QgsEditFormConfig::readXml( const QDomNode &node, QgsReadWriteContext &cont
onRelationsLoaded();
}
}

context.leaveCategory();
}

void QgsEditFormConfig::writeXml( QDomNode &node, const QgsReadWriteContext &context ) const
@@ -430,7 +430,7 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteCo
// the subclass can also read custom properties
readCustomProperties( layerElement );

context.enterCategory( tr( "Layer" ), mne.text() );
MAYBE_UNUSED QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Layer" ), mne.text() );

// now let the children grab what they need from the Dom node.
layerError = !readXml( layerElement, context );
@@ -557,8 +557,6 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteCo
QDomElement metadataElem = layerElement.firstChildElement( QStringLiteral( "resourceMetadata" ) );
mMetadata.readMetadataXml( metadataElem );

context.leaveCategory();

return true;
} // bool QgsMapLayer::readLayerXML

@@ -35,12 +35,13 @@ void QgsReadWriteContext::pushMessage( const QString &message, Qgis::MessageLeve
mMessages.append( ReadWriteMessage( message, level, mCategories ) );
}

void QgsReadWriteContext::enterCategory( const QString &category, const QString &details )
QgsReadWriteContextCategoryPopper QgsReadWriteContext::enterCategory( const QString &category, const QString &details )
{
QString message = category;
if ( !details.isEmpty() )
message.append( QString( " :: %1" ).arg( details ) );
mCategories.push_back( message );
return QgsReadWriteContextCategoryPopper( this );
}

void QgsReadWriteContext::leaveCategory()
@@ -21,6 +21,8 @@
#include "qgspathresolver.h"
#include "qgis.h"

class QgsReadWriteContextCategoryPopper;

/**
* \class QgsReadWriteContext
* \ingroup core
@@ -76,30 +78,56 @@ class CORE_EXPORT QgsReadWriteContext
* Append a message to the context
* \since QGIS 3.2
*/
void pushMessage( const QString &message, Qgis::MessageLevel level );
void pushMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning );

/**
* Push a category to the stack
* \since QGIS 3.2
*/
void enterCategory( const QString &category, const QString &details = QString() );
#ifndef SIP_RUN

/**
* Pop the last category
* Push a category to the stack
* \note The return value should always be used so category can be automatically left.
* \note Not available in the Python bindings.
* \since QGIS 3.2
*/
void leaveCategory();
NODISCARD QgsReadWriteContextCategoryPopper enterCategory( const QString &category, const QString &details = QString() );
#endif

/**
* Return the stored messages and remove them
* \since QGIS 3.2
*/
QList<QgsReadWriteContext::ReadWriteMessage> takeMessages();


private:
//! Pop the last category
void leaveCategory();

QgsPathResolver mPathResolver;
QList<ReadWriteMessage> mMessages;
QStringList mCategories = QStringList();

friend class QgsReadWriteContextCategoryPopper;
};

#ifndef SIP_RUN
///@cond PRIVATE
class QgsReadWriteContextCategoryPopper
{
public:

QgsReadWriteContextCategoryPopper( QgsReadWriteContext *context )
: mContext( context )
{}

~QgsReadWriteContextCategoryPopper()
{
if ( mContext )
mContext->leaveCategory();
}

QgsReadWriteContext *mContext;
};
///@endcond PRIVATE
#endif

#endif // QGSREADWRITECONTEXT_H
@@ -1688,7 +1688,7 @@ void QgsVectorLayer::resolveReferences( QgsProject *project )

bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMessage, QgsReadWriteContext &context )
{
context.enterCategory( tr( "Symbology" ) );
MAYBE_UNUSED QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Symbology" ) );

if ( !mExpressionFieldBuffer )
mExpressionFieldBuffer = new QgsExpressionFieldBuffer();
@@ -1875,8 +1875,6 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes

updateFields();

context.leaveCategory();

return true;
}

0 comments on commit 8582517

Please sign in to comment.
You can’t perform that action at this time.