Skip to content

Commit 8582517

Browse files
committedFeb 28, 2018
avoid required call to leaveCategory by adding a RAII private class
1 parent 8989292 commit 8582517

File tree

6 files changed

+46
-32
lines changed

6 files changed

+46
-32
lines changed
 

‎python/core/qgsreadwritecontext.sip.in

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111

12+
1213
class QgsReadWriteContext
1314
{
1415
%Docstring
@@ -63,26 +64,13 @@ Returns path resolver for conversion between relative and absolute paths
6364
Sets up path resolver for conversion between relative and absolute paths
6465
%End
6566

66-
void pushMessage( const QString &message, Qgis::MessageLevel level );
67+
void pushMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning );
6768
%Docstring
6869
Append a message to the context
6970

7071
.. versionadded:: 3.2
7172
%End
7273

73-
void enterCategory( const QString &category, const QString &details = QString() );
74-
%Docstring
75-
Push a category to the stack
76-
77-
.. versionadded:: 3.2
78-
%End
79-
80-
void leaveCategory();
81-
%Docstring
82-
Pop the last category
83-
84-
.. versionadded:: 3.2
85-
%End
8674

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

82+
9483
};
9584

85+
9686
/************************************************************************
9787
* This file has been generated automatically from *
9888
* *

‎src/core/qgseditformconfig.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ void QgsEditFormConfig::setSuppress( QgsEditFormConfig::FeatureFormSuppress s )
260260

261261
void QgsEditFormConfig::readXml( const QDomNode &node, QgsReadWriteContext &context )
262262
{
263-
context.enterCategory( QObject::tr( "Edit form config" ) );
263+
MAYBE_UNUSED QgsReadWriteContextCategoryPopper p = context.enterCategory( QObject::tr( "Edit form config" ) );
264+
264265
d.detach();
265266

266267
QDomNode editFormNode = node.namedItem( QStringLiteral( "editform" ) );
@@ -388,8 +389,6 @@ void QgsEditFormConfig::readXml( const QDomNode &node, QgsReadWriteContext &cont
388389
onRelationsLoaded();
389390
}
390391
}
391-
392-
context.leaveCategory();
393392
}
394393

395394
void QgsEditFormConfig::writeXml( QDomNode &node, const QgsReadWriteContext &context ) const

‎src/core/qgsmaplayer.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteCo
430430
// the subclass can also read custom properties
431431
readCustomProperties( layerElement );
432432

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

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

560-
context.leaveCategory();
561-
562560
return true;
563561
} // bool QgsMapLayer::readLayerXML
564562

‎src/core/qgsreadwritecontext.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ void QgsReadWriteContext::pushMessage( const QString &message, Qgis::MessageLeve
3535
mMessages.append( ReadWriteMessage( message, level, mCategories ) );
3636
}
3737

38-
void QgsReadWriteContext::enterCategory( const QString &category, const QString &details )
38+
QgsReadWriteContextCategoryPopper QgsReadWriteContext::enterCategory( const QString &category, const QString &details )
3939
{
4040
QString message = category;
4141
if ( !details.isEmpty() )
4242
message.append( QString( " :: %1" ).arg( details ) );
4343
mCategories.push_back( message );
44+
return QgsReadWriteContextCategoryPopper( this );
4445
}
4546

4647
void QgsReadWriteContext::leaveCategory()

‎src/core/qgsreadwritecontext.h

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "qgspathresolver.h"
2222
#include "qgis.h"
2323

24+
class QgsReadWriteContextCategoryPopper;
25+
2426
/**
2527
* \class QgsReadWriteContext
2628
* \ingroup core
@@ -76,30 +78,56 @@ class CORE_EXPORT QgsReadWriteContext
7678
* Append a message to the context
7779
* \since QGIS 3.2
7880
*/
79-
void pushMessage( const QString &message, Qgis::MessageLevel level );
81+
void pushMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning );
8082

81-
/**
82-
* Push a category to the stack
83-
* \since QGIS 3.2
84-
*/
85-
void enterCategory( const QString &category, const QString &details = QString() );
83+
#ifndef SIP_RUN
8684

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

9394
/**
9495
* Return the stored messages and remove them
9596
* \since QGIS 3.2
9697
*/
9798
QList<QgsReadWriteContext::ReadWriteMessage> takeMessages();
9899

100+
99101
private:
102+
//! Pop the last category
103+
void leaveCategory();
104+
100105
QgsPathResolver mPathResolver;
101106
QList<ReadWriteMessage> mMessages;
102107
QStringList mCategories = QStringList();
108+
109+
friend class QgsReadWriteContextCategoryPopper;
110+
};
111+
112+
#ifndef SIP_RUN
113+
///@cond PRIVATE
114+
class QgsReadWriteContextCategoryPopper
115+
{
116+
public:
117+
118+
QgsReadWriteContextCategoryPopper( QgsReadWriteContext *context )
119+
: mContext( context )
120+
{}
121+
122+
~QgsReadWriteContextCategoryPopper()
123+
{
124+
if ( mContext )
125+
mContext->leaveCategory();
126+
}
127+
128+
QgsReadWriteContext *mContext;
103129
};
130+
///@endcond PRIVATE
131+
#endif
104132

105133
#endif // QGSREADWRITECONTEXT_H

‎src/core/qgsvectorlayer.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,7 @@ void QgsVectorLayer::resolveReferences( QgsProject *project )
16881688

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

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

18761876
updateFields();
18771877

1878-
context.leaveCategory();
1879-
18801878
return true;
18811879
}
18821880

0 commit comments

Comments
 (0)
Please sign in to comment.