QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgsexpressionnode.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsexpressionnode.h
3 -------------------
4 begin : May 2017
5 copyright : (C) 2017 Matthias Kuhn
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
17#ifndef QGSEXPRESSIONNODE_H
18#define QGSEXPRESSIONNODE_H
19
20#include "qgis.h"
21
22#include <QCoreApplication>
23#include <QSet>
24#include <QVariant>
25
26class QgsExpression;
28
35{
36 // clang-format off
37#ifdef SIP_RUN
39 switch ( sipCpp->nodeType() )
40 {
42 sipType = sipType_QgsExpressionNodeUnaryOperator;
43 break;
45 sipType = sipType_QgsExpressionNodeBinaryOperator;
46 break;
48 sipType = sipType_QgsExpressionNodeInOperator;
49 break;
51 sipType = sipType_QgsExpressionNodeFunction;
52 break;
54 sipType = sipType_QgsExpressionNodeLiteral;
55 break;
57 sipType = sipType_QgsExpressionNodeColumnRef;
58 break;
60 sipType = sipType_QgsExpressionNodeCondition;
61 break;
63 sipType = sipType_QgsExpressionNodeBetweenOperator;
64 break;
65 default:
66 sipType = 0;
67 break;
68 }
70#endif
71
72 Q_DECLARE_TR_FUNCTIONS( QgsExpressionNode )
73
74 public:
88 // clang-format on
89
90
95 struct NamedNode
96 {
97 public:
104 : name( name )
105 , node( node )
106 {}
107
109 QString name;
110
113 };
114
119 class CORE_EXPORT NodeList
120 {
121 public:
122 virtual ~NodeList();
125 {
126 mList.append( node );
127 mNameList.append( QString() );
128 }
129
133 void append( QgsExpressionNode::NamedNode *node SIP_TRANSFER );
134
138 int count() const { return mList.count(); }
139
145 void reserve( int size ) { mList.reserve( size ); }
146
150 bool hasNamedNodes() const { return mHasNamedNodes; }
151
155 QList<QgsExpressionNode *> list() { return mList; }
156
161 QgsExpressionNode *at( int i ) { return mList.at( i ); }
162
166 QStringList names() const { return mNameList; }
167
170
174 virtual QString dump() const;
175
176 private:
177 QList<QgsExpressionNode *> mList;
178 QStringList mNameList;
179
180 bool mHasNamedNodes = false;
181
185 static QString cleanNamedNodeName( const QString &name );
186
187 public:
188 };
189
190 virtual ~QgsExpressionNode() = default;
191
197 virtual QgsExpressionNode::NodeType nodeType() const = 0;
198
204 virtual QString dump() const = 0;
205
212 QVariant eval( QgsExpression *parent, const QgsExpressionContext *context );
213
220 virtual QgsExpressionNode *clone() const = 0;
221
240 virtual QSet<QString> referencedColumns() const = 0;
241
248 virtual QSet<QString> referencedVariables() const = 0;
249
256 virtual QSet<QString> referencedFunctions() const = 0;
257
264 virtual QList<const QgsExpressionNode *> nodes() const SIP_SKIP = 0;
265
274 virtual bool needsGeometry() const = 0;
275
283 virtual bool isStatic( QgsExpression *parent, const QgsExpressionContext *context ) const = 0;
284
292 bool prepare( QgsExpression *parent, const QgsExpressionContext *context );
293
300
307
314
321
328 bool hasCachedStaticValue() const { return mHasCachedValue; }
329
336 QVariant cachedStaticValue() const { return mCachedStaticValue; }
337
345 void setCachedStaticValue( const QVariant &value ) const SIP_SKIP;
346
360
361 protected:
362 QgsExpressionNode() = default;
363 QgsExpressionNode( const QgsExpressionNode &other );
365
374 void cloneTo( QgsExpressionNode *target ) const SIP_SKIP;
375
376#ifndef SIP_RUN
377
383 mutable bool mHasCachedValue = false;
384
390 mutable QVariant mCachedStaticValue;
391
402 mutable std::unique_ptr< QgsExpressionNode > mCompiledSimplifiedNode;
403#endif
404
405 private:
410 virtual bool prepareNode( QgsExpression *parent, const QgsExpressionContext *context ) = 0;
411
416 virtual QVariant evalNode( QgsExpression *parent, const QgsExpressionContext *context ) = 0;
417};
418
420
421#endif // QGSEXPRESSIONNODE_H
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
A list of expression nodes.
bool hasNamedNodes() const
Returns true if list contains any named nodes.
QStringList names() const
Returns a list of names for nodes.
void append(QgsExpressionNode *node)
Takes ownership of the provided node.
void reserve(int size)
Reserves size for the node list.
QList< QgsExpressionNode * > list()
Gets a list of all the nodes.
QgsExpressionNode * at(int i)
Gets the node at position i in the list.
int count() const
Returns the number of nodes in the list.
Abstract base class for all nodes that can appear in an expression.
bool hasCachedStaticValue() const
Returns true if the node can be replaced by a static cached value.
virtual QString dump() const =0
Dump this node into a serialized (part) of an expression.
QVariant eval(QgsExpression *parent, const QgsExpressionContext *context)
Evaluate this node with the given context and parent.
virtual QgsExpressionNode * clone() const =0
Generate a clone of this node.
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.
virtual QSet< QString > referencedColumns() const =0
Abstract virtual method which returns a list of columns required to evaluate this node.
virtual QgsExpressionNode::NodeType nodeType() const =0
Gets the type of this node.
bool mHasCachedValue
true if the node has a static, precalculated value.
const QgsExpressionNode * effectiveNode() const
Returns a reference to the simplest node which represents this node, after any compilation optimizati...
QVariant cachedStaticValue() const
Returns the node's static cached value.
virtual QSet< QString > referencedVariables() const =0
Returns a set of all variables which are used in this expression.
QVariant mCachedStaticValue
Contains the static, precalculated value for the node if mHasCachedValue is true.
QgsExpressionNode()=default
std::unique_ptr< QgsExpressionNode > mCompiledSimplifiedNode
Contains a compiled node which represents a simplified version of this node as a result of compilatio...
NodeType
Known node types.
@ ntBetweenOperator
Between operator.
@ ntIndexOperator
Index operator.
int parserFirstLine
First line in the parser this node was found.
QgsExpressionNode & operator=(const QgsExpressionNode &other)
int parserLastLine
Last line in the parser this node was found.
virtual bool needsGeometry() const =0
Abstract virtual method which returns if the geometry is required to evaluate this expression.
virtual QList< const QgsExpressionNode * > nodes() const =0
Returns a list of all nodes which are used in this expression.
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.
virtual QSet< QString > referencedFunctions() const =0
Returns a set of all functions which are used in this expression.
Handles parsing and evaluation of expressions (formerly called "search strings").
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:198
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_ABSTRACT
Definition qgis_sip.h:220
#define SIP_FACTORY
Definition qgis_sip.h:83
#define SIP_END
Definition qgis_sip.h:215
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
QgsExpressionNode * node
Node.
NamedNode(const QString &name, QgsExpressionNode *node)
Constructor for NamedNode.