Skip to content

Commit eb8eb59

Browse files
committedJul 8, 2017
Code dedup
1 parent 66d562a commit eb8eb59

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed
 

‎src/core/expression/qgsexpressionfunction.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4302,20 +4302,17 @@ bool QgsWithVariableExpressionFunction::isStatic( const QgsExpressionNodeFunctio
43024302
if ( args->count() < 3 )
43034303
return false;
43044304

4305+
// We only need to check if the node evaluation is static, if both - name and value - are static.
43054306
if ( args->at( 0 )->isStatic( parent, context ) && args->at( 1 )->isStatic( parent, context ) )
43064307
{
43074308
QVariant name = args->at( 0 )->eval( parent, context );
43084309
QVariant value = args->at( 1 )->eval( parent, context );
43094310

4310-
QgsExpressionContextScope *scope = new QgsExpressionContextScope();
4311-
scope->setVariable( name.toString(), value );
4312-
4313-
QgsExpressionContext *updatedContext = const_cast<QgsExpressionContext *>( context );
4314-
updatedContext->appendScope( scope );
4315-
4316-
if ( args->at( 2 )->isStatic( parent, updatedContext ) )
4311+
// Temporarily append a new scope to provide the variable
4312+
appendTemporaryVariable( context, name.toString(), value );
4313+
if ( args->at( 2 )->isStatic( parent, context ) )
43174314
isStatic = true;
4318-
delete updatedContext->popScope();
4315+
popTemporaryVariable( context );
43194316
}
43204317

43214318
return false;
@@ -4332,15 +4329,13 @@ QVariant QgsWithVariableExpressionFunction::run( QgsExpressionNode::NodeList *ar
43324329
QVariant name = args->at( 0 )->eval( parent, context );
43334330
QVariant value = args->at( 1 )->eval( parent, context );
43344331

4335-
QgsExpressionContextScope *scope = new QgsExpressionContextScope();
4336-
scope->setVariable( name.toString(), value );
4337-
43384332
QgsExpressionContext *updatedContext = const_cast<QgsExpressionContext *>( context );
43394333
if ( !context )
43404334
updatedContext = new QgsExpressionContext();
4341-
updatedContext->appendScope( scope );
4335+
4336+
appendTemporaryVariable( updatedContext, name.toString(), value );
43424337
result = args->at( 2 )->eval( parent, updatedContext );
4343-
delete updatedContext->popScope();
4338+
popTemporaryVariable( updatedContext );
43444339
if ( !context )
43454340
delete updatedContext;
43464341

@@ -4369,13 +4364,24 @@ bool QgsWithVariableExpressionFunction::prepare( const QgsExpressionNodeFunction
43694364
QVariant name = args->at( 0 )->prepare( parent, context );
43704365
QVariant value = args->at( 1 )->prepare( parent, context );
43714366

4372-
QgsExpressionContextScope *scope = new QgsExpressionContextScope();
4373-
scope->setVariable( name.toString(), value );
4367+
appendTemporaryVariable( context, name.toString(), value );
4368+
args->at( 2 )->prepare( parent, context );
4369+
popTemporaryVariable( context );
4370+
4371+
return true;
4372+
}
43744373

4374+
void QgsWithVariableExpressionFunction::popTemporaryVariable( const QgsExpressionContext *context ) const
4375+
{
43754376
QgsExpressionContext *updatedContext = const_cast<QgsExpressionContext *>( context );
4376-
updatedContext->appendScope( scope );
4377-
args->at( 2 )->prepare( parent, updatedContext );
43784377
delete updatedContext->popScope();
4378+
}
43794379

4380-
return true;
4380+
void QgsWithVariableExpressionFunction::appendTemporaryVariable( const QgsExpressionContext *context, const QString &name, const QVariant &value ) const
4381+
{
4382+
QgsExpressionContextScope *scope = new QgsExpressionContextScope();
4383+
scope->setVariable( name, value );
4384+
4385+
QgsExpressionContext *updatedContext = const_cast<QgsExpressionContext *>( context );
4386+
updatedContext->appendScope( scope );
43814387
}

‎src/core/expression/qgsexpressionfunction.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
class QgsExpressionNodeFunction;
3030
class QgsExpression;
3131
class QgsExpressionContext;
32+
class QgsExpressionContextScope;
3233

3334
/** \ingroup core
3435
* A abstract base class for defining QgsExpression functions.
@@ -475,6 +476,18 @@ class QgsWithVariableExpressionFunction : public QgsExpressionFunction
475476
QVariant func( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent ) override;
476477

477478
bool prepare( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const override;
479+
480+
private:
481+
482+
/**
483+
* Append a scope with a single variable definition (``name``=``value``)
484+
*/
485+
void appendTemporaryVariable( const QgsExpressionContext *context, const QString &name, const QVariant &value ) const;
486+
487+
/**
488+
* Pop the temporary scope again
489+
*/
490+
void popTemporaryVariable( const QgsExpressionContext *context ) const;
478491
};
479492

480493
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.