Qt 4.8
qrangeexpression.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 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.
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 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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qbuiltintypes_p.h"
43 #include "qcommonsequencetypes_p.h"
44 #include "qcommonvalues_p.h"
45 #include "qgenericsequencetype_p.h"
46 #include "qinteger_p.h"
47 #include "qliteral_p.h"
48 #include "qrangeiterator_p.h"
49 
50 #include "qrangeexpression_p.h"
51 
53 
54 using namespace QPatternist;
55 
57  const Expression::Ptr &operand2) : PairContainer(operand1, operand2)
58 {
59 }
60 
62 {
63  const Item s(m_operand1->evaluateSingleton(context));
64 
65  if(!s)
67 
68  const Item e(m_operand2->evaluateSingleton(context));
69  if(!e)
71 
72  const xsInteger start = s.as<Numeric>()->toInteger();
73  const xsInteger end = e.as<Numeric>()->toInteger();
74 
75  if(start > end)
77  else if(start == end)
78  return makeSingletonIterator(s);
79  else
81 }
82 
84 {
85  return m_operand1->evaluateSingleton(context);
86 }
87 
89 {
90  SequenceType::List result;
93  return result;
94 }
95 
97 {
98  /* This logic makes the Cardinality more specific. */
99  Cardinality::Count from;
100  bool hasFrom;
101 
103  {
104  from = m_operand1->as<Literal>()->item().as<Integer>()->toInteger();
105  hasFrom = true;
106  }
107  else
108  {
109  hasFrom = false;
110  from = 0;
111  }
112 
113  /* We can't check whether to is -1 since maybe the user wrote -1. Hence
114  * hasTo is required. */
115  bool hasTo;
117 
119  {
120  const xsInteger asInt = m_operand2->as<Literal>()->item().as<Integer>()->toInteger();
121  to = asInt;
122 
123  if(to == asInt)
124  hasTo = true;
125  else
126  {
127  /* Cardinality::Count is not the same as type xsInteger. We had overflow. */
128  to = -1;
129  hasTo = false;
130  }
131  }
132  else
133  {
134  to = -1;
135  hasTo = false;
136  }
137 
138  if(hasTo && hasFrom)
139  {
140  if(from > to)
141  {
142  /* The query is incorrectly written, we'll evaluate to the empty sequence.
143  * Just return what's correct. */
145  }
146  else
147  {
148  Cardinality::Count count = (to - from) + 1; /* + 1, since it's inclusive. */
149  return makeGenericSequenceType(BuiltinTypes::xsInteger, Cardinality::fromExact(count));
150  }
151  }
152  else
153  {
154  /* We can't do fromExact(from, -1) since the latter can evaluate to a value that actually is
155  * lower than from, although that unfortunately is very unlikely. */
157  }
158 }
159 
161 {
163 }
164 
166 {
167  return visitor->visit(this);
168 }
169 
RangeExpression(const Expression::Ptr &operand1, const Expression::Ptr &operand2)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
qint64 xsInteger
static const EmptyIterator< Item >::Ptr emptyIterator
static Cardinality fromExact(const Count count)
static const SequenceType::Ptr ZeroOrOneInteger
QExplicitlySharedDataPointer< QAbstractXmlForwardIterator< Item > > Ptr
A smart pointer wrapping an instance of a QAbstractXmlForwardIterator subclass.
virtual Expression::Properties properties() const
static const SequenceType::Ptr ZeroOrMoreIntegers
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
const TCastTarget * as() const
The namespace for the internal API of QtXmlPatterns.
virtual SequenceType::Ptr staticType() const
RangeIterator represents a sequence of integers between a start and end value.
virtual Item evaluateSingleton(const DynamicContext::Ptr &) const
Houses an AtomicValue, making it available as an Expression.
Definition: qliteral_p.h:74
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
virtual SequenceType::List expectedOperandTypes() const
Base class for all numeric values.
virtual ExpressionVisitorResult::Ptr accept(const ExpressionVisitor::Ptr &visitor) const
static const AtomicType::Ptr xsInteger
Implements the value instance of the xs:integer type.
Definition: qinteger_p.h:70
The QFlags class provides a type-safe way of storing OR-combinations of enum values.
Definition: qglobal.h:2313
static const KeyPair *const end
bool is(const ID id) const
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
Base class for expressions that has exactly two operands.
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &) const