1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
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.
52 #ifndef Patternist_AtomicComparators_H
53 #define Patternist_AtomicComparators_H
55 #include <private/qabstractfloat_p.h>
56 #include <private/qatomiccomparator_p.h>
57 #include <private/qprimitives_p.h>
61 * @short Contains all the classes implementing comparisons between atomic values.
71 * @short Performs case @em sensitive string comparison
72 * between @c xs:anyUri, @c xs:string, and @c xs:untypedAtomic.
74 * @ingroup Patternist_xdm
75 * @author Frans Englich <frans.englich@nokia.com>
77 class StringComparator : public AtomicComparator
81 * Compares two strings, and returns the appropriate AtomicComparator::ComparisonResult enum. This
82 * is a bit simplified version of string comparison as defined in the XPath specifications,
83 * since this does not take any string collations into account(which an implementation neither
86 * @see <a href="http://www.w3.org/TR/xpath-functions/#string-compare">XQuery 1.0 and XPath
87 * 2.0 Functions and Operators, 7.3 ValueComparison::Equality and Comparison of Strings</a>
89 virtual ComparisonResult compare(const Item &op1,
90 const AtomicComparator::Operator op,
91 const Item &op2) const;
94 * Compares two strings, and returns @c true if they are considered equal as per
95 * an ordinary string comparison. In other words, this is an implementation with
96 * the Unicode code point collation.
98 * @see <a href="http://www.w3.org/TR/xpath-functions/#string-compare">XQuery 1.0 and XPath
99 * 2.0 Functions and Operators, 7.3 ValueComparison::Equality and Comparison of Strings</a>
101 virtual bool equals(const Item &op1,
102 const Item &op2) const;
106 * @short Performs case @em insensitive string comparison
107 * between @c xs:anyUri, @c xs:string, and @c xs:untypedAtomic.
109 * @ingroup Patternist_xdm
110 * @author Frans Englich <frans.englich@nokia.com>
112 class CaseInsensitiveStringComparator : public AtomicComparator
116 * Converts both string values to upper case and afterwards compare them.
118 virtual ComparisonResult compare(const Item &op1,
119 const AtomicComparator::Operator op,
120 const Item &op2) const;
123 * Converts both string values case insensitively.
125 virtual bool equals(const Item &op1,
126 const Item &op2) const;
130 * @short Compares @c xs:base64Binary and @c xs:hexBinary values.
132 * @author Frans Englich <frans.englich@nokia.com>
134 class BinaryDataComparator : public AtomicComparator
137 virtual bool equals(const Item &op1,
138 const Item &op2) const;
142 * @short Compares @c xs:boolean values.
144 * This is done via the object's Boolean::evaluteEBV() function.
146 * @author Frans Englich <frans.englich@nokia.com>
148 class BooleanComparator : public AtomicComparator
151 virtual ComparisonResult compare(const Item &op1,
152 const AtomicComparator::Operator op,
153 const Item &op2) const;
155 virtual bool equals(const Item &op1,
156 const Item &op2) const;
160 * @short Compares @c xs:double values.
162 * @todo Add docs about numeric promotion
164 * @author Frans Englich <frans.englich@nokia.com>
166 class AbstractFloatComparator : public AtomicComparator
169 virtual ComparisonResult compare(const Item &op1,
170 const AtomicComparator::Operator op,
171 const Item &op2) const;
173 virtual bool equals(const Item &op1,
174 const Item &op2) const;
178 * @short Compares @c xs:double values for the purpose of sorting.
180 * @todo Add docs about numeric promotion
182 * @author Frans Englich <frans.englich@nokia.com>
184 template<const AtomicComparator::Operator t_op>
185 class AbstractFloatSortComparator : public AbstractFloatComparator
188 virtual ComparisonResult compare(const Item &o1,
189 const AtomicComparator::Operator op,
190 const Item &o2) const
192 Q_ASSERT_X(t_op == OperatorLessThanNaNLeast || t_op == OperatorLessThanNaNGreatest, Q_FUNC_INFO,
193 "Can only be instantiated with those two.");
194 Q_ASSERT(op == t_op);
195 Q_UNUSED(op); /* Needed when building in release mode. */
197 const xsDouble v1 = o1.template as<Numeric>()->toDouble();
198 const xsDouble v2 = o2.template as<Numeric>()->toDouble();
200 if(qIsNaN(v1) && !qIsNaN(v2))
201 return t_op == OperatorLessThanNaNLeast ? LessThan : GreaterThan;
202 if(!qIsNaN(v1) && qIsNaN(v2))
203 return t_op == OperatorLessThanNaNLeast ? GreaterThan : LessThan;
205 if(Double::isEqual(v1, v2))
216 * @short Compares @c xs:decimal values.
218 * @author Frans Englich <frans.englich@nokia.com>
220 class DecimalComparator : public AtomicComparator
223 virtual ComparisonResult compare(const Item &op1,
224 const AtomicComparator::Operator op,
225 const Item &op2) const;
227 virtual bool equals(const Item &op1,
228 const Item &op2) const;
232 * @short Compares @c xs:integer values.
234 * @author Frans Englich <frans.englich@nokia.com>
236 class IntegerComparator : public AtomicComparator
239 virtual ComparisonResult compare(const Item &op1,
240 const AtomicComparator::Operator op,
241 const Item &op2) const;
243 virtual bool equals(const Item &op1,
244 const Item &op2) const;
248 * @short Compares @c xs:QName values.
250 * @author Frans Englich <frans.englich@nokia.com>
252 class QNameComparator : public AtomicComparator
255 virtual bool equals(const Item &op1,
256 const Item &op2) const;
260 * @short Compares sub-classes of AbstractDateTime.
262 * @author Frans Englich <frans.englich@nokia.com>
264 class AbstractDateTimeComparator : public AtomicComparator
267 virtual ComparisonResult compare(const Item &op1,
268 const AtomicComparator::Operator op,
269 const Item &op2) const;
270 virtual bool equals(const Item &op1,
271 const Item &op2) const;
275 * @short Compares sub-classes of AbstractDuration.
277 * @author Frans Englich <frans.englich@nokia.com>
279 class AbstractDurationComparator : public AtomicComparator
282 virtual ComparisonResult compare(const Item &op1,
283 const AtomicComparator::Operator op,
284 const Item &op2) const;
285 virtual bool equals(const Item &op1,
286 const Item &op2) const;
289 static inline QDateTime addDurationToDateTime(const QDateTime &dateTime,
290 const AbstractDuration *const duration);