| 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_ComparisonPlatform_H |
| 51 | #define Patternist_ComparisonPlatform_H |
| 52 | |
| 53 | #include <private/qatomiccomparators_p.h> |
| 54 | #include <private/qitem_p.h> |
| 55 | #include <private/qcommonsequencetypes_p.h> |
| 56 | #include <private/qdynamiccontext_p.h> |
| 57 | #include <private/qbuiltintypes_p.h> |
| 58 | #include <private/qitemtype_p.h> |
| 59 | #include <private/qpatternistlocale_p.h> |
| 60 | |
| 61 | QT_BEGIN_NAMESPACE |
| 62 | |
| 63 | namespace QPatternist |
| 64 | { |
| 65 | /** |
| 66 | * @short Provides comparison functionality for classes that compare Items, |
| 67 | * such as ValueComparison or MaxFN. |
| 68 | * |
| 69 | * Classes which need comparison functionalities should inherit from this class. |
| 70 | * |
| 71 | * The parameter of this template class is the class inheriting from ComparisonPlatform. |
| 72 | * |
| 73 | * The class inheriting ComparisonPlatform must implement the following function: |
| 74 | * @code |
| 75 | * AtomicComparator::Operator operatorID() const |
| 76 | * @endcode |
| 77 | * |
| 78 | * @author Vincent Ricard <magic@magicninja.org> |
| 79 | * @ingroup Patternist_expressions |
| 80 | */ |
| 81 | template <typename TSubClass, |
| 82 | bool issueError, |
| 83 | AtomicComparator::ComparisonType comparisonType = AtomicComparator::AsValueComparison, |
| 84 | ReportContext::ErrorCode errorCode = ReportContext::XPTY0004> |
| 85 | class ComparisonPlatform |
| 86 | { |
| 87 | protected: |
| 88 | /** |
| 89 | * Makes ComparisonPlatform use the AtomicComparator @p comparator. |
| 90 | */ |
| 91 | void prepareComparison(const AtomicComparator::Ptr &comparator); |
| 92 | |
| 93 | /** |
| 94 | * Default constructor. Does nothing. It is implemented in order make template |
| 95 | * instantiation easier. |
| 96 | */ |
| 97 | inline ComparisonPlatform() |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Utility function for fetching the appropriate AtomicComparator |
| 103 | * for two atomic values of type @p type1 and @p type2, for the operator @p op. |
| 104 | * |
| 105 | * This function is used throughout the implementation, ranging from the ValueComparison |
| 106 | * itself, to for example the aggregate functions. |
| 107 | * |
| 108 | * @param context the ordinary ReportContext, used for issuing errors. |
| 109 | * @param type1 the type of the first operand value in a comparison for which the |
| 110 | * returned AtomicComparator is intended for |
| 111 | * @param type2 the type of the second operand value in a comparison for which the |
| 112 | * returned AtomicComparator is intended for. Whether @p type1 and @p type2 corresponds |
| 113 | * to what is the first second operand type does not have significance, the order |
| 114 | * can be arbitrary |
| 115 | */ |
| 116 | AtomicComparator::Ptr |
| 117 | fetchComparator(const ItemType::Ptr &type1, |
| 118 | const ItemType::Ptr &type2, |
| 119 | const ReportContext::Ptr &context) const; |
| 120 | |
| 121 | /** |
| 122 | * @short Compares @p i1 and @p i2 with operator @p op, using comparator @p |
| 123 | * comp. All input arguments must be valid, and not @c null. |
| 124 | * |
| 125 | * This is a fast, raw function which has the requirement that the |
| 126 | * caller knows what to compare and with what. |
| 127 | */ |
| 128 | bool compare(const Item &i1, |
| 129 | const Item &i2, |
| 130 | const AtomicComparator::Ptr &comp, |
| 131 | const AtomicComparator::Operator op) const; |
| 132 | |
| 133 | /** |
| 134 | * @short Compares @p it1 against @p it2, using comparator() and operatorID(). |
| 135 | * |
| 136 | * If the comparator wasn't looked up at compile time, it will be |
| 137 | * attempted before comparing. If this fails, errors are reported via |
| 138 | * @p context. |
| 139 | */ |
| 140 | bool |
| 141 | flexibleCompare(const Item &it1, |
| 142 | const Item &it2, |
| 143 | const DynamicContext::Ptr &context) const; |
| 144 | |
| 145 | /** |
| 146 | * @short like flexibleCompare(), but returns the result |
| 147 | * as an AtomicComparator::Operator instead of @c bool. |
| 148 | * |
| 149 | * This is useful when it is significant how a less than comparison |
| 150 | * fails; whether the two values are equal or greater than. |
| 151 | */ |
| 152 | AtomicComparator::ComparisonResult |
| 153 | detailedFlexibleCompare(const Item &it1, |
| 154 | const Item &it2, |
| 155 | const DynamicContext::Ptr &context) const; |
| 156 | |
| 157 | /** |
| 158 | * @returns the AtomicComparator that has been allocated at compile time, |
| 159 | * with prepareComparison(). If no AtomicComparator has been allocated |
| 160 | * for some reason, this function returns @c null. |
| 161 | */ |
| 162 | inline const AtomicComparator::Ptr &comparator() const |
| 163 | { |
| 164 | return m_comparator; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Calling this function makes ComparisonPlatform use a comparator that |
| 169 | * compares strings case insensitively. |
| 170 | * |
| 171 | * @see ValueComparison::isCaseInsensitiveCompare() |
| 172 | */ |
| 173 | inline void useCaseInsensitiveComparator() |
| 174 | { |
| 175 | m_comparator = AtomicComparator::Ptr(new CaseInsensitiveStringComparator()); |
| 176 | } |
| 177 | |
| 178 | private: |
| 179 | /** |
| 180 | * @returns the operator that is used. |
| 181 | */ |
| 182 | inline AtomicComparator::Operator operatorID() const |
| 183 | { |
| 184 | Q_ASSERT(static_cast<const TSubClass *>(this)->operatorID()); |
| 185 | return static_cast<const TSubClass *>(this)->operatorID(); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * The comparator that is used for comparing atomic values. The AtomicComparator |
| 190 | * that is used, depends on the static type of the operands. m_comparator can be |
| 191 | * @c null if it wasn't possible to determine what comparator to use at compile time. |
| 192 | */ |
| 193 | AtomicComparator::Ptr m_comparator; |
| 194 | }; |
| 195 | |
| 196 | #include "qcomparisonplatform_tpl_p.h" |
| 197 | |
| 198 | } |
| 199 | |
| 200 | QT_END_NAMESPACE |
| 201 | |
| 202 | #endif |
| 203 | |