| 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 | #include "qbuiltintypes_p.h" |
| 41 | #include "qitem_p.h" |
| 42 | #include "qvalidationerror_p.h" |
| 43 | |
| 44 | #include "qinteger_p.h" |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | using namespace QPatternist; |
| 49 | |
| 50 | Item Integer::fromValue(const xsInteger num) |
| 51 | { |
| 52 | return toItem(atomicValue: Integer::Ptr(new Integer(num))); |
| 53 | } |
| 54 | |
| 55 | AtomicValue::Ptr Integer::fromLexical(const QString &strNumeric) |
| 56 | { |
| 57 | bool conversionOk = false; |
| 58 | const xsInteger num = strNumeric.toLongLong(ok: &conversionOk); |
| 59 | |
| 60 | if(conversionOk) |
| 61 | return AtomicValue::Ptr(new Integer(num)); |
| 62 | else |
| 63 | return ValidationError::createError(); |
| 64 | } |
| 65 | |
| 66 | Integer::Integer(const xsInteger num) : m_value(num) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | bool Integer::evaluateEBV(const QExplicitlySharedDataPointer<DynamicContext> &) const |
| 71 | { |
| 72 | return m_value != 0; |
| 73 | } |
| 74 | |
| 75 | QString Integer::stringValue() const |
| 76 | { |
| 77 | return QString::number(m_value); |
| 78 | } |
| 79 | |
| 80 | ItemType::Ptr Integer::type() const |
| 81 | { |
| 82 | return BuiltinTypes::xsInteger; |
| 83 | } |
| 84 | |
| 85 | xsDouble Integer::toDouble() const |
| 86 | { |
| 87 | return static_cast<xsDouble>(m_value); |
| 88 | } |
| 89 | |
| 90 | xsInteger Integer::toInteger() const |
| 91 | { |
| 92 | return m_value; |
| 93 | } |
| 94 | |
| 95 | xsFloat Integer::toFloat() const |
| 96 | { |
| 97 | return static_cast<xsFloat>(m_value); |
| 98 | } |
| 99 | |
| 100 | xsDecimal Integer::toDecimal() const |
| 101 | { |
| 102 | return static_cast<xsDecimal>(m_value); |
| 103 | } |
| 104 | |
| 105 | Numeric::Ptr Integer::round() const |
| 106 | { |
| 107 | /* xs:integerS never has a mantissa. */ |
| 108 | return Numeric::Ptr(const_cast<Integer *>(this)); |
| 109 | } |
| 110 | |
| 111 | Numeric::Ptr Integer::roundHalfToEven(const xsInteger /*scale*/) const |
| 112 | { |
| 113 | return Numeric::Ptr(const_cast<Integer *>(this)); |
| 114 | } |
| 115 | |
| 116 | Numeric::Ptr Integer::floor() const |
| 117 | { |
| 118 | return Numeric::Ptr(const_cast<Integer *>(this)); |
| 119 | } |
| 120 | |
| 121 | Numeric::Ptr Integer::ceiling() const |
| 122 | { |
| 123 | return Numeric::Ptr(const_cast<Integer *>(this)); |
| 124 | } |
| 125 | |
| 126 | Numeric::Ptr Integer::abs() const |
| 127 | { |
| 128 | /* No reason to allocate an Integer if we're already absolute. */ |
| 129 | if(m_value < 0) |
| 130 | return Numeric::Ptr(new Integer(qAbs(t: m_value))); |
| 131 | else |
| 132 | return Numeric::Ptr(const_cast<Integer *>(this)); |
| 133 | } |
| 134 | |
| 135 | bool Integer::isNaN() const |
| 136 | { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | bool Integer::isInf() const |
| 141 | { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | Item Integer::toNegated() const |
| 146 | { |
| 147 | return fromValue(num: -m_value); |
| 148 | } |
| 149 | |
| 150 | bool Integer::isSigned() const |
| 151 | { |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | qulonglong Integer::toUnsignedInteger() const |
| 156 | { |
| 157 | Q_ASSERT_X(false, Q_FUNC_INFO, |
| 158 | "It makes no sense to call this function, see Numeric::toUnsignedInteger()."); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | QT_END_NAMESPACE |
| 163 |
