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_Numeric_H |
51 | #define Patternist_Numeric_H |
52 | |
53 | #include <private/qitem_p.h> |
54 | #include <private/qprimitives_p.h> |
55 | |
56 | QT_BEGIN_NAMESPACE |
57 | |
58 | /** |
59 | * @file |
60 | * @short Contains class Numeric. This file was originally called qnumeric_p.h, |
61 | * but various build systems cannot handle that that name happens to be |
62 | * identical to another one, the one in QtCore. |
63 | */ |
64 | |
65 | namespace QPatternist |
66 | { |
67 | /** |
68 | * @short Base class for all numeric values. |
69 | * |
70 | * @section creation Creating Instances |
71 | * |
72 | * @todo |
73 | * - Depending on what type of val |
74 | * - Numeric::createFromString |
75 | * - Various classes has ::Zero(), ::PosINF(), ::NaN(), NegINF() |
76 | * - Never use constructor, use createFromNative, or createFromString. |
77 | * |
78 | * @see <a href="http://www.w3.org/TR/xquery-operators/#numeric-functions">XQuery 1.0 |
79 | * and XPath 2.0 Functions and Operators, 6 Functions and Operators on Numerics</a> |
80 | * @see <a href="http://www.w3.org/TR/xquery-operators/#func-overloading">XQuery 1.0 |
81 | * and XPath 2.0 Functions and Operators, 1.2 Function Overloading</a> |
82 | * @author Frans Englich <frans.englich@nokia.com> |
83 | * @ingroup Patternist_xdm |
84 | * @todo discuss data hierarchy the non existatnt number data type |
85 | */ |
86 | class Numeric : public AtomicValue |
87 | { |
88 | public: |
89 | |
90 | typedef QExplicitlySharedDataPointer<Numeric> Ptr; |
91 | |
92 | /** |
93 | * Creates a Numeric sub-class that is appropriate for @p number. |
94 | * |
95 | * @note usages of e/E is not handled; Double::fromLexical should |
96 | * be used in that case. There is no function similar to fromLexical that also |
97 | * takes double values into account(because that distinction is done in the scanner). |
98 | * |
99 | * Currently used in the parser to create appropriate expressions. |
100 | */ |
101 | static AtomicValue::Ptr fromLexical(const QString &number); |
102 | |
103 | /** |
104 | * @returns the particular number's value as a native representation of |
105 | * the type xs:double. This can be considered that the value is cast to |
106 | * xs:double. |
107 | */ |
108 | virtual xsDouble toDouble() const = 0; |
109 | |
110 | /** |
111 | * @returns the particular number's value as a native representation of |
112 | * the type xs:integer. This can be considered that the value is cast to |
113 | * xs:integer. |
114 | */ |
115 | virtual xsInteger toInteger() const = 0; |
116 | |
117 | /** |
118 | * @returns the number as an unsigned integer. If the value is not |
119 | * unsigned, the code asserts and behavior is undefined. |
120 | */ |
121 | virtual qulonglong toUnsignedInteger() const = 0; |
122 | |
123 | /** |
124 | * @returns the particular number's value as a native representation of |
125 | * the type xs:float. This can be considered that the value is cast to |
126 | * xs:float. |
127 | */ |
128 | virtual xsFloat toFloat() const = 0; |
129 | |
130 | /** |
131 | * @returns the particular number's value as a native representation of |
132 | * the type xs:decimal. This can be considered that the value is cast to |
133 | * xs:decimal. |
134 | */ |
135 | virtual xsFloat toDecimal() const = 0; |
136 | |
137 | /** |
138 | * Performs the algorithm specified for the function fn:round on this Numeric, |
139 | * and whose result is returned. |
140 | * |
141 | * @see <a href="http://www.w3.org/TR/xpath-functions/#func-round">XQuery 1.0 |
142 | * and XPath 2.0 Functions and Operators, 6.4.4 fn:round</a> |
143 | */ |
144 | virtual Numeric::Ptr round() const = 0; |
145 | |
146 | /** |
147 | * Performs rounding as defined for the fn:round-half-to-even on this Numeric, |
148 | * and whose result is returned. |
149 | * |
150 | * @see <a href="http://www.w3.org/TR/xpath-functions/#func-round-half-to-even">XQuery 1.0 |
151 | * and XPath 2.0 Functions and Operators, 6.4.5 fn:round-half-to-even</a> |
152 | */ |
153 | virtual Numeric::Ptr roundHalfToEven(const xsInteger scale) const = 0; |
154 | |
155 | /** |
156 | * Performs the algorithm specified for the function fn:floor on this Numeric, |
157 | * and whose result is returned. |
158 | * |
159 | * @see <a href="http://www.w3.org/TR/xpath-functions/#func-floor">XQuery 1.0 |
160 | * and XPath 2.0 Functions and Operators, 6.4.3 fn:floor</a> |
161 | */ |
162 | virtual Numeric::Ptr floor() const = 0; |
163 | |
164 | /** |
165 | * Performs the algorithm specified for the function fn:ceiling on this Numeric, |
166 | * and whose result is returned. |
167 | * |
168 | * @see <a href="http://www.w3.org/TR/xpath-functions/#func-ceiling">XQuery 1.0 |
169 | * and XPath 2.0 Functions and Operators, 6.4.2 fn:ceiling</a> |
170 | */ |
171 | virtual Numeric::Ptr ceiling() const = 0; |
172 | |
173 | /** |
174 | * Performs the algorithm specified for the function fn:abs on this Numeric, |
175 | * and whose result is returned. |
176 | * |
177 | * @see <a href="http://www.w3.org/TR/xpath-functions/#func-ceiling">XQuery 1.0 |
178 | * and XPath 2.0 Functions and Operators, 6.4.1 fn:abs</a> |
179 | */ |
180 | virtual Numeric::Ptr abs() const = 0; |
181 | |
182 | /** |
183 | * Determines whether this Numeric is not-a-number, @c NaN. For numeric types |
184 | * that cannot represent @c NaN, this function should return @c false. |
185 | * |
186 | * @returns @c true if this Numeric is @c NaN |
187 | */ |
188 | virtual bool isNaN() const = 0; |
189 | |
190 | /** |
191 | * Determines whether this Numeric is an infinite number. Signedness |
192 | * is irrelevant, -INF as well as INF is considered infinity. |
193 | * |
194 | * For numeric types that cannot represent infinity, such as xs:integer |
195 | * , this function should return @c false. |
196 | * |
197 | * @returns @c true if this Numeric is an infinite number |
198 | */ |
199 | virtual bool isInf() const = 0; |
200 | |
201 | /** |
202 | * Unary minus. |
203 | */ |
204 | virtual Item toNegated() const = 0; |
205 | |
206 | /** |
207 | * @short Returns @c true if this value is signed. If @c false is |
208 | * returned, the value is unsigned. |
209 | * |
210 | * For float and decimal values, @c xs:double, @c xs:float and @c |
211 | * xs:decimal, the code asserts and behavior is undefined. |
212 | */ |
213 | virtual bool isSigned() const = 0; |
214 | |
215 | protected: |
216 | /** |
217 | * @short Implements @c fn:round() for types implemented with floating |
218 | * point. |
219 | * |
220 | * MS Windows and at least IRIX does not have C99's nearbyint() function(see the man |
221 | * page), so we reinvent it. |
222 | */ |
223 | static xsDouble roundFloat(const xsDouble val); |
224 | }; |
225 | } |
226 | |
227 | QT_END_NAMESPACE |
228 | |
229 | #endif |
230 | |