QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsexpressionnode.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexpressionnode.cpp
3  -------------------
4  begin : May 2017
5  copyright : (C) 2017 Matthias Kuhn
6  email : [email protected]
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsexpressionnode.h"
17 #include "qgsexpression.h"
18 
19 
20 QVariant QgsExpressionNode::eval( QgsExpression *parent, const QgsExpressionContext *context )
21 {
22  if ( mHasCachedValue )
23  {
24  return mCachedStaticValue;
25  }
26  else if ( mCompiledSimplifiedNode )
27  {
28  return mCompiledSimplifiedNode->eval( parent, context );
29  }
30  else
31  {
32  QVariant res = evalNode( parent, context );
33  return res;
34  }
35 }
36 
38 {
39  mHasCachedValue = false;
41  if ( isStatic( parent, context ) )
42  {
43  // some calls to isStatic already evaluate the node to a cached value, so if that's
44  // happened then don't re-evaluate again
45  if ( !mHasCachedValue )
46  {
47  mCachedStaticValue = evalNode( parent, context );
48  if ( !parent->hasEvalError() )
49  mHasCachedValue = true;
50  }
51  return true;
52  }
53  else
54  {
55  return prepareNode( parent, context );
56  }
57 }
58 
60  : parserFirstLine( other.parserFirstLine )
61  , parserFirstColumn( other.parserFirstColumn )
62  , parserLastLine( other.parserLastLine )
63  , parserLastColumn( other.parserLastColumn )
64  , mHasCachedValue( other.mHasCachedValue )
65  , mCachedStaticValue( other.mCachedStaticValue )
66  , mCompiledSimplifiedNode( other.mCompiledSimplifiedNode ? other.mCompiledSimplifiedNode->clone() : nullptr )
67 {
68 
69 }
70 
72 {
79  mCompiledSimplifiedNode.reset( other.mCompiledSimplifiedNode ? other.mCompiledSimplifiedNode->clone() : nullptr );
80  return *this;
81 }
82 
84 {
88  target->mCompiledSimplifiedNode.reset( mCompiledSimplifiedNode->clone() );
93 }
94 
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Abstract base class for all nodes that can appear in an expression.
QVariant eval(QgsExpression *parent, const QgsExpressionContext *context)
Evaluate this node with the given context and parent.
virtual bool isStatic(QgsExpression *parent, const QgsExpressionContext *context) const =0
Returns true if this node can be evaluated for a static value.
bool prepare(QgsExpression *parent, const QgsExpressionContext *context)
Prepare this node for evaluation.
bool mHasCachedValue
true if the node has a static, precalculated value.
QVariant mCachedStaticValue
Contains the static, precalculated value for the node if mHasCachedValue is true.
QgsExpressionNode()=default
Constructor.
std::unique_ptr< QgsExpressionNode > mCompiledSimplifiedNode
Contains a compiled node which represents a simplified version of this node as a result of compilatio...
int parserFirstLine
First line in the parser this node was found.
QgsExpressionNode & operator=(const QgsExpressionNode &other)
Assignment operator.
int parserLastLine
Last line in the parser this node was found.
void cloneTo(QgsExpressionNode *target) const
Copies the members of this node to the node provided in target.
int parserLastColumn
Last column in the parser this node was found.
int parserFirstColumn
First column in the parser this node was found.
Class for parsing and evaluation of expressions (formerly called "search strings").
bool hasEvalError() const
Returns true if an error occurred when evaluating last input.