| 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_AtomicComparator_H |
| 51 | #define Patternist_AtomicComparator_H |
| 52 | |
| 53 | #include <QFlags> |
| 54 | |
| 55 | #include <private/qitem_p.h> |
| 56 | #include <private/qatomictypedispatch_p.h> |
| 57 | |
| 58 | QT_BEGIN_NAMESPACE |
| 59 | |
| 60 | class QString; |
| 61 | |
| 62 | namespace QPatternist |
| 63 | { |
| 64 | |
| 65 | /** |
| 66 | * @short Base class for classes responsible of comparing two atomic values. |
| 67 | * |
| 68 | * This class is also known as the AtomicParrot. |
| 69 | * |
| 70 | * @ingroup Patternist_xdm |
| 71 | * @author Frans Englich <frans.englich@nokia.com> |
| 72 | */ |
| 73 | class Q_AUTOTEST_EXPORT AtomicComparator : public AtomicTypeVisitorResult |
| 74 | { |
| 75 | public: |
| 76 | AtomicComparator(); |
| 77 | virtual ~AtomicComparator(); |
| 78 | |
| 79 | typedef QExplicitlySharedDataPointer<AtomicComparator> Ptr; |
| 80 | |
| 81 | /** |
| 82 | * Identifies operators used in value comparisons. |
| 83 | * |
| 84 | * The enum values are bit-significant. |
| 85 | * |
| 86 | * @see <a href="http://www.w3.org/TR/xpath20/#id-value-comparisons">W3C XML Path |
| 87 | * Language (XPath) 2.0, 3.5.1 Value Comparisons</a> |
| 88 | */ |
| 89 | enum Operator |
| 90 | { |
| 91 | /** |
| 92 | * Operator <tt>eq</tt> and <tt>=</tt>. |
| 93 | */ |
| 94 | OperatorEqual = 1, |
| 95 | |
| 96 | /** |
| 97 | * Operator <tt>ne</tt> and <tt>!=</tt>. |
| 98 | */ |
| 99 | OperatorNotEqual = 1 << 1, |
| 100 | |
| 101 | /** |
| 102 | * Operator <tt>gt</tt> and <tt>\></tt>. |
| 103 | */ |
| 104 | OperatorGreaterThan = 1 << 2, |
| 105 | |
| 106 | /** |
| 107 | * Operator <tt>lt</tt> and <tt>\<</tt>. |
| 108 | */ |
| 109 | OperatorLessThan = 1 << 3, |
| 110 | |
| 111 | /** |
| 112 | * One of the operators we use for sorting. The only difference from |
| 113 | * OperatorLessThan is that it sees NaN as ordered and smaller than |
| 114 | * other numbers. |
| 115 | */ |
| 116 | OperatorLessThanNaNLeast = 1 << 4, |
| 117 | |
| 118 | /** |
| 119 | * One of the operators we use for sorting. The only difference from |
| 120 | * OperatorLessThanLeast is that it sees NaN as ordered and larger than |
| 121 | * other numbers. |
| 122 | */ |
| 123 | OperatorLessThanNaNGreatest = 1 << 5, |
| 124 | |
| 125 | /** |
| 126 | * Operator <tt>ge</tt> and <tt>\>=</tt>. |
| 127 | */ |
| 128 | OperatorGreaterOrEqual = OperatorEqual | OperatorGreaterThan, |
| 129 | |
| 130 | /** |
| 131 | * Operator <tt>le</tt> and <tt>\<=</tt>. |
| 132 | */ |
| 133 | OperatorLessOrEqual = OperatorEqual | OperatorLessThan |
| 134 | }; |
| 135 | |
| 136 | typedef QFlags<Operator> Operators; |
| 137 | |
| 138 | /** |
| 139 | * Signifies the result of a value comparison. This is used for value comparisons, |
| 140 | * and in the future likely also for sorting. |
| 141 | * |
| 142 | * @see <a href="http://www.w3.org/TR/xpath20/#id-value-comparisons">W3C XML Path |
| 143 | * Language (XPath) 2.0, 3.5.1 Value Comparisons</a> |
| 144 | */ |
| 145 | enum ComparisonResult |
| 146 | { |
| 147 | LessThan = 1, |
| 148 | Equal = 2, |
| 149 | GreaterThan = 4, |
| 150 | Incomparable = 8 |
| 151 | }; |
| 152 | |
| 153 | /** |
| 154 | * Compares @p op1 and @p op2 and determines the relationship between the two. This |
| 155 | * is used for sorting and comparisons. The implementation performs an assert crash, |
| 156 | * and must therefore be re-implemented if comparing the relevant values should be |
| 157 | * possible. |
| 158 | * |
| 159 | * @param op1 the first operand |
| 160 | * @param op the operator. How a comparison is carried out shouldn't depend on what the |
| 161 | * operator is, but in some cases it is of interest. |
| 162 | * @param op2 the second operand |
| 163 | */ |
| 164 | virtual ComparisonResult compare(const Item &op1, |
| 165 | const AtomicComparator::Operator op, |
| 166 | const Item &op2) const; |
| 167 | |
| 168 | /** |
| 169 | * Determines whether @p op1 and @p op2 are equal. It is the same as calling compare() |
| 170 | * and checking whether the return value is Equal, but since comparison testing is such |
| 171 | * a common operation, this specialized function exists. |
| 172 | * |
| 173 | * @returns true if @p op1 and @p op2 are equal. |
| 174 | * |
| 175 | * @param op1 the first operand |
| 176 | * @param op2 the second operand |
| 177 | */ |
| 178 | virtual bool equals(const Item &op1, |
| 179 | const Item &op2) const = 0; |
| 180 | |
| 181 | /** |
| 182 | * Identifies the kind of comparison. |
| 183 | */ |
| 184 | enum ComparisonType |
| 185 | { |
| 186 | /** |
| 187 | * Identifies a general comparison; operator @c =, @c >, @c <=, and so on. |
| 188 | */ |
| 189 | AsGeneralComparison = 1, |
| 190 | |
| 191 | /** |
| 192 | * Identifies a value comparison; operator @c eq, @c lt, @c le, and so on. |
| 193 | */ |
| 194 | AsValueComparison |
| 195 | }; |
| 196 | |
| 197 | /** |
| 198 | * Utility function for getting the lexical representation for |
| 199 | * the comparison operator @p op. Depending on the @p type argument, |
| 200 | * the string returned is either a general comparison or a value comparison |
| 201 | * operator. |
| 202 | * |
| 203 | * @param op the operator which the display name should be determined for. |
| 204 | * @param type signifies whether the returned display name should be for |
| 205 | * a value comparison or a general comparison. For example, if @p op is |
| 206 | * OperatorEqual and @p type is AsValueComparision, "eq" is returned. |
| 207 | */ |
| 208 | static QString displayName(const AtomicComparator::Operator op, |
| 209 | const ComparisonType type); |
| 210 | |
| 211 | }; |
| 212 | Q_DECLARE_OPERATORS_FOR_FLAGS(AtomicComparator::Operators) |
| 213 | } |
| 214 | |
| 215 | QT_END_NAMESPACE |
| 216 | |
| 217 | #endif |
| 218 | |