1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 //
41 //  W A R N I N G
42 //  -------------
43 //
44 // This file is not part of the Qt API.  It exists purely as an
45 // implementation detail.  This header file may change from version to
46 // version without notice, or even be removed.
47 //
48 // We mean it.
49 
50 #ifndef Patternist_EvaluationCache_H
51 #define Patternist_EvaluationCache_H
52 
53 #include <private/qcachingiterator_p.h>
54 #include <private/qcommonsequencetypes_p.h>
55 #include <private/qnodebuilder_p.h>
56 #include <private/qoperandsiterator_p.h>
57 #include <private/qsinglecontainer_p.h>
58 #include <private/qvariabledeclaration_p.h>
59 
60 QT_BEGIN_NAMESPACE
61 
62 namespace QPatternist
63 {
64     /**
65      * @short Evaluates to the same result as its operand, but ensures the
66      * operand is evaluated once even if this Expression is evaluated several
67      * times.
68      *
69      * EvaluationCache does this in a pipelined way, by delivering items from
70      * its cache, which is stored in the DynamicContext. If the cache has less
71      * items than what the caller requests, EvaluationCache continues to
72      * deliver but this time from the source, which it also populates into the
73      * cache.
74      *
75      * EvaluationCache is used as an optimization in order to avoid running
76      * expensive code paths multiple times, but also is sometimes a necessity:
77      * for instance, when objects must be unique, such as potentially in the
78      * case of node identity.
79      *
80      * EvaluationCache is in particular used for variables, whose sole purpose
81      * is to store it once(at least conceptually) and then use it in multiple
82      * places.
83      *
84      * In some cases an EvaluationCache isn't necessary. For instance, when a
85      * variable is only referenced once. In those cases EvaluationCache removes
86      * itself as an optimization; implemented in compress().
87      *
88      * @author Frans Englich <frans.englich@nokia.com>
89      * @ingroup Patternist_expressions
90      */
91     template<bool IsForGlobal>
92     class EvaluationCache : public SingleContainer
93     {
94     public:
95         EvaluationCache(const Expression::Ptr &operand,
96                         const VariableDeclaration::Ptr &varDecl,
97                         const VariableSlotID slot);
98 
99         virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const;
100         virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const;
101         virtual Expression::Ptr compress(const StaticContext::Ptr &context);
102 
103         virtual SequenceType::Ptr staticType() const;
104 
105         /**
106          * The first operand must be exactly one @c xs:string.
107          */
108         virtual SequenceType::List expectedOperandTypes() const;
109 
110         virtual ExpressionVisitorResult::Ptr accept(const ExpressionVisitor::Ptr &visitor) const;
111         virtual Properties properties() const;
112         virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context,
113                                           const SequenceType::Ptr &reqType);
114         virtual const SourceLocationReflection *actualReflection() const;
115 
slot()116         inline VariableSlotID slot() const
117         {
118             return m_varSlot;
119         }
120 
121     private:
122         static DynamicContext::Ptr topFocusContext(const DynamicContext::Ptr &context);
123         const VariableDeclaration::Ptr m_declaration;
124         /**
125          * This variable must not be called m_slot. If it so, a compiler bug on
126          * HP-UX-aCC-64 is triggered in the constructor initializor. See the
127          * preprocessor output.
128          *
129          * Note that this is the cache slot, and is disjoint to any variable's
130          * regular slot.
131          */
132         const VariableSlotID            m_varSlot;
133     };
134 
135 #include "qevaluationcache_tpl_p.h"
136 }
137 
138 QT_END_NAMESPACE
139 
140 #endif
141