1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | //
|
---|
43 | // W A R N I N G
|
---|
44 | // -------------
|
---|
45 | //
|
---|
46 | // This file is not part of the Qt API. It exists purely as an
|
---|
47 | // implementation detail. This header file may change from version to
|
---|
48 | // version without notice, or even be removed.
|
---|
49 | //
|
---|
50 | // We mean it.
|
---|
51 |
|
---|
52 | #ifndef Patternist_ComparisonPlatform_H
|
---|
53 | #define Patternist_ComparisonPlatform_H
|
---|
54 |
|
---|
55 | #include "qatomiccomparators_p.h"
|
---|
56 | #include "qitem_p.h"
|
---|
57 | #include "qcommonsequencetypes_p.h"
|
---|
58 | #include "qdynamiccontext_p.h"
|
---|
59 | #include "qbuiltintypes_p.h"
|
---|
60 | #include "qitemtype_p.h"
|
---|
61 | #include "qpatternistlocale_p.h"
|
---|
62 |
|
---|
63 | QT_BEGIN_HEADER
|
---|
64 |
|
---|
65 | QT_BEGIN_NAMESPACE
|
---|
66 |
|
---|
67 | namespace QPatternist
|
---|
68 | {
|
---|
69 | /**
|
---|
70 | * @short Provides comparison functionality for classes that compare Items,
|
---|
71 | * such as ValueComparison or MaxFN.
|
---|
72 | *
|
---|
73 | * Classes which need comparison functionalities should inherit from this class.
|
---|
74 | *
|
---|
75 | * The parameter of this template class is the class inheriting from ComparisonPlatform.
|
---|
76 | *
|
---|
77 | * The class inheriting ComparisonPlatform must implement the following function:
|
---|
78 | * @code
|
---|
79 | * AtomicComparator::Operator operatorID() const
|
---|
80 | * @endcode
|
---|
81 | *
|
---|
82 | * @author Vincent Ricard <[email protected]>
|
---|
83 | * @ingroup Patternist_expressions
|
---|
84 | */
|
---|
85 | template <typename TSubClass,
|
---|
86 | bool issueError,
|
---|
87 | AtomicComparator::ComparisonType comparisonType = AtomicComparator::AsValueComparison,
|
---|
88 | ReportContext::ErrorCode errorCode = ReportContext::XPTY0004>
|
---|
89 | class ComparisonPlatform
|
---|
90 | {
|
---|
91 | protected:
|
---|
92 | /**
|
---|
93 | * Makes ComparisonPlatform use the AtomicComparator @p comparator.
|
---|
94 | */
|
---|
95 | void prepareComparison(const AtomicComparator::Ptr &comparator);
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Default constructor. Does nothing. It is implemented in order make template
|
---|
99 | * instantiation easier.
|
---|
100 | */
|
---|
101 | inline ComparisonPlatform()
|
---|
102 | {
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Utility function for fetching the appropriate AtomicComparator
|
---|
107 | * for two atomic values of type @p type1 and @p type2, for the operator @p op.
|
---|
108 | *
|
---|
109 | * This function is used throughout the implementation, ranging from the ValueComparison
|
---|
110 | * itself, to for example the aggregate functions.
|
---|
111 | *
|
---|
112 | * @param context the ordinary ReportContext, used for issuing errors.
|
---|
113 | * @param type1 the type of the first operand value in a comparison for which the
|
---|
114 | * returned AtomicComparator is intended for
|
---|
115 | * @param type2 the type of the second operand value in a comparison for which the
|
---|
116 | * returned AtomicComparator is intended for. Whether @p type1 and @p type2 corresponds
|
---|
117 | * to what is the first second operand type does not have significance, the order
|
---|
118 | * can be arbitrary
|
---|
119 | */
|
---|
120 | AtomicComparator::Ptr
|
---|
121 | fetchComparator(const ItemType::Ptr &type1,
|
---|
122 | const ItemType::Ptr &type2,
|
---|
123 | const ReportContext::Ptr &context) const;
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * @short Compares @p i1 and @p i2 with operator @p op, using comparator @p
|
---|
127 | * comp. All input arguments must be valid, and not @c null.
|
---|
128 | *
|
---|
129 | * This is a fast, raw function which has the requirement that the
|
---|
130 | * caller knows what to compare and with what.
|
---|
131 | */
|
---|
132 | bool compare(const Item &i1,
|
---|
133 | const Item &i2,
|
---|
134 | const AtomicComparator::Ptr &comp,
|
---|
135 | const AtomicComparator::Operator op) const;
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * @short Compares @p it1 against @p it2, using comparator() and operatorID().
|
---|
139 | *
|
---|
140 | * If the comparator wasn't looked up at compile time, it will be
|
---|
141 | * attempted before comparing. If this fails, errors are reported via
|
---|
142 | * @p context.
|
---|
143 | */
|
---|
144 | bool
|
---|
145 | flexibleCompare(const Item &it1,
|
---|
146 | const Item &it2,
|
---|
147 | const DynamicContext::Ptr &context) const;
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * @short like flexibleCompare(), but returns the result
|
---|
151 | * as an AtomicComparator::Operator instead of @c bool.
|
---|
152 | *
|
---|
153 | * This is useful when it is significant how a less than comparison
|
---|
154 | * fails; whether the two values are equal or greater than.
|
---|
155 | */
|
---|
156 | AtomicComparator::ComparisonResult
|
---|
157 | detailedFlexibleCompare(const Item &it1,
|
---|
158 | const Item &it2,
|
---|
159 | const DynamicContext::Ptr &context) const;
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * @returns the AtomicComparator that has been allocated at compile time,
|
---|
163 | * with prepareComparison(). If no AtomicComparator has been allocated
|
---|
164 | * for some reason, this function returns @c null.
|
---|
165 | */
|
---|
166 | inline const AtomicComparator::Ptr &comparator() const
|
---|
167 | {
|
---|
168 | return m_comparator;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Calling this function makes ComparisonPlatform use a comparator that
|
---|
173 | * compares strings case insensitively.
|
---|
174 | *
|
---|
175 | * @see ValueComparison::isCaseInsensitiveCompare()
|
---|
176 | */
|
---|
177 | inline void useCaseInsensitiveComparator()
|
---|
178 | {
|
---|
179 | m_comparator = AtomicComparator::Ptr(new CaseInsensitiveStringComparator());
|
---|
180 | }
|
---|
181 |
|
---|
182 | private:
|
---|
183 | /**
|
---|
184 | * @returns the operator that is used.
|
---|
185 | */
|
---|
186 | inline AtomicComparator::Operator operatorID() const
|
---|
187 | {
|
---|
188 | Q_ASSERT(static_cast<const TSubClass *>(this)->operatorID());
|
---|
189 | return static_cast<const TSubClass *>(this)->operatorID();
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * The comparator that is used for comparing atomic values. The AtomicComparator
|
---|
194 | * that is used, depends on the static type of the operands. m_comparator can be
|
---|
195 | * @c null if it wasn't possible to determine what comparator to use at compile time.
|
---|
196 | */
|
---|
197 | AtomicComparator::Ptr m_comparator;
|
---|
198 | };
|
---|
199 |
|
---|
200 | #include "qcomparisonplatform.cpp"
|
---|
201 |
|
---|
|
---|