Qt 4.8
qderivedinteger_p.h
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 //
43 // W A R N I N G
44 // -------------
45 //
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.
49 //
50 // We mean it.
51 
52 #ifndef Patternist_DerivedInteger_H
53 #define Patternist_DerivedInteger_H
54 
55 #include "qbuiltintypes_p.h"
56 #include "qinteger_p.h"
57 #include "qpatternistlocale_p.h"
58 #include "qvalidationerror_p.h"
59 
61 
63 
64 namespace QPatternist
65 {
70  {
71  None = 1,
75  };
76 
77  enum
78  {
81  };
82 
83  template<TypeOfDerivedInteger DerivedType> class DerivedInteger;
84 
85  template<TypeOfDerivedInteger DerivedType> class DerivedIntegerDetails;
86 
87  template<>
89  {
90  private:
91  friend class DerivedInteger<TypeByte>;
92  typedef qint8 StorageType;
94  static const StorageType maxInclusive = 127;
95  static const StorageType minInclusive = -128;
96  static const DerivedIntegerLimitsUsage limitsUsage = LimitBoth;
97 
102 
104  };
105 
106  template<>
108  {
109  private:
110  friend class DerivedInteger<TypeInt>;
113  static const StorageType maxInclusive = Q_INT64_C(2147483647);
114  static const StorageType minInclusive = Q_INT64_C(-2147483648);
115  static const DerivedIntegerLimitsUsage limitsUsage = LimitBoth;
116 
121 
123  };
124 
125  template<>
127  {
128  private:
129  friend class DerivedInteger<TypeLong>;
131  typedef StorageType TemporaryStorageType;
132  static const StorageType maxInclusive = Q_INT64_C(9223372036854775807);
133 
138  static const StorageType minInclusive = -(Q_INT64_C(9223372036854775807)) - 1;
139 
140  static const DerivedIntegerLimitsUsage limitsUsage = LimitBoth;
141 
146 
148  };
149 
150  template<>
152  {
153  private:
156  typedef StorageType TemporaryStorageType;
157  static const StorageType maxInclusive = -1;
158  static const StorageType minInclusive = IgnorableSignedValue;
159  static const DerivedIntegerLimitsUsage limitsUsage = LimitUpwards;
160 
165 
167  };
168 
169  template<>
171  {
172  private:
175  typedef StorageType TemporaryStorageType;
176  static const StorageType maxInclusive = IgnorableSignedValue;
177  static const StorageType minInclusive = 0;
178  static const DerivedIntegerLimitsUsage limitsUsage = LimitDownwards;
179 
184 
186  };
187 
188  template<>
190  {
191  private:
194  typedef StorageType TemporaryStorageType;
195  static const StorageType maxInclusive = 0;
196  static const StorageType minInclusive = IgnorableSignedValue;
197  static const DerivedIntegerLimitsUsage limitsUsage = LimitUpwards;
198 
203 
205  };
206 
207  template<>
209  {
210  private:
213  typedef StorageType TemporaryStorageType;
214  static const StorageType maxInclusive = IgnorableSignedValue;
215  static const StorageType minInclusive = 1;
216  static const DerivedIntegerLimitsUsage limitsUsage = LimitDownwards;
217 
222 
224  };
225 
226  template<>
228  {
229  private:
230  friend class DerivedInteger<TypeShort>;
233  static const StorageType maxInclusive = 32767;
234  static const StorageType minInclusive = -32768;
235  static const DerivedIntegerLimitsUsage limitsUsage = LimitBoth;
236 
241 
243  };
244 
245  template<>
247  {
248  private:
252  static const StorageType maxInclusive = 255;
253  static const StorageType minInclusive = 0;
254  static const DerivedIntegerLimitsUsage limitsUsage = LimitBoth;
255 
260 
262  };
263 
264  template<>
266  {
267  private:
271  static const StorageType maxInclusive = Q_UINT64_C(4294967295);
272  static const StorageType minInclusive = 0;
273  static const DerivedIntegerLimitsUsage limitsUsage = LimitBoth;
274 
279 
281  };
282 
283  template<>
285  {
286  private:
289  typedef StorageType TemporaryStorageType;
290  static const StorageType maxInclusive = Q_UINT64_C(18446744073709551615);
291  static const StorageType minInclusive = 0;
292  static const DerivedIntegerLimitsUsage limitsUsage = LimitBoth;
293 
298 
300  };
301 
302  template<>
304  {
305  private:
309  static const StorageType maxInclusive = 65535;
310  static const StorageType minInclusive = 0;
311  static const DerivedIntegerLimitsUsage limitsUsage = LimitBoth;
312 
317 
319  };
320 
328  template<TypeOfDerivedInteger DerivedType>
329  class DerivedInteger : public Numeric
330  {
331  private:
334 
335  static const StorageType maxInclusive = DerivedIntegerDetails<DerivedType>::maxInclusive;
336  static const StorageType minInclusive = DerivedIntegerDetails<DerivedType>::minInclusive;
338 
339  const StorageType m_value;
340 
341  inline DerivedInteger(const StorageType num) : m_value(num)
342  {
343  }
344 
353  template<typename A, typename B>
354  static bool lessThan(const A &a, const B &b)
355  {
356  return a < b;
357  }
358 
362  template<typename A, typename B>
363  static bool largerOrEqual(const A &a, const B &b)
364  {
365  return qint64(a) >= b;
366  }
367 
368  public:
369 
371  {
372  switch(DerivedType)
373  {
374  case TypeByte: return BuiltinTypes::xsByte;
375  case TypeInt: return BuiltinTypes::xsInt;
376  case TypeLong: return BuiltinTypes::xsLong;
381  case TypeShort: return BuiltinTypes::xsShort;
386  }
387 
388  Q_ASSERT(false);
389  return ItemType::Ptr();
390  }
391 
392  static AtomicValue::Ptr fromValue(const NamePool::Ptr &np, const TemporaryStorageType num)
393  {
394  /* If we use minInclusive when calling lessThan(), we for some
395  * reason get a linker error with GCC. Using this temporary
396  * variable solves it. */
397  const StorageType minimum = minInclusive;
398 
399  if((limitsUsage & LimitUpwards) &&
400  num > maxInclusive)
401  {
402  return ValidationError::createError(QtXmlPatterns::tr(
403  "Value %1 of type %2 exceeds maximum (%3).")
404  .arg(QPatternist::formatData(static_cast<xsInteger>(num)))
405  .arg(formatType(np, itemType()))
406  .arg(QPatternist::formatData(static_cast<xsInteger>(maxInclusive))));
407  }
408  else if((limitsUsage & LimitDownwards) &&
409  lessThan(num, minimum))
410  {
411  return ValidationError::createError(QtXmlPatterns::tr(
412  "Value %1 of type %2 is below minimum (%3).")
413  .arg(QPatternist::formatData(static_cast<xsInteger>(num)))
414  .arg(formatType(np, itemType()))
415  .arg(QPatternist::formatData(static_cast<xsInteger>(minInclusive))));
416  }
417  else
418  return AtomicValue::Ptr(new DerivedInteger(num));
419  }
420 
421  static AtomicValue::Ptr fromValueUnchecked(const TemporaryStorageType num)
422  {
423  return AtomicValue::Ptr(new DerivedInteger(num));
424  }
425 
430  static AtomicValue::Ptr fromLexical(const NamePool::Ptr &np, const QString &strNumeric)
431  {
432  bool conversionOk = false;
433  TemporaryStorageType num;
434 
435  /* Depending on the type, we need to call different conversion
436  * functions on QString. */
437  switch(DerivedType)
438  {
439  case TypeUnsignedLong:
440  {
441  /* Qt decides to flag '-' as invalid, so remove it before. */
442  if(strNumeric.contains(QLatin1Char('-')))
443  {
444  num = QString(strNumeric).remove(QLatin1Char('-')).toULongLong(&conversionOk);
445 
446  if(num != 0)
447  conversionOk = false;
448  }
449  else
450  num = strNumeric.toULongLong(&conversionOk);
451 
452  break;
453  }
454  default:
455  {
456  num = strNumeric.toLongLong(&conversionOk);
457  break;
458  }
459  }
460 
461  if(conversionOk)
462  return fromValue(np, num);
463  else
465  }
466 
467  inline StorageType storedValue() const
468  {
469  return m_value;
470  }
471 
478  {
479  return m_value != 0;
480  }
481 
482  virtual QString stringValue() const
483  {
484  return QString::number(m_value);
485  }
486 
487  virtual ItemType::Ptr type() const
488  {
489  return itemType();
490  }
491 
492  virtual xsDouble toDouble() const
493  {
494  return static_cast<xsDouble>(m_value);
495  }
496 
497  virtual xsInteger toInteger() const
498  {
499  return m_value;
500  }
501 
502  virtual xsFloat toFloat() const
503  {
504  return static_cast<xsFloat>(m_value);
505  }
506 
507  virtual xsDecimal toDecimal() const
508  {
509  return static_cast<xsDecimal>(m_value);
510  }
511 
512  virtual Numeric::Ptr round() const
513  {
514  /* xs:integerS never have a mantissa. */
515  return Numeric::Ptr(static_cast<Numeric *>(const_cast<AtomicValue *>(Integer::fromValue(m_value).asAtomicValue())));
516  }
517 
519  {
520  return Numeric::Ptr(static_cast<Numeric *>(const_cast<AtomicValue *>(Integer::fromValue(m_value).asAtomicValue())));
521  }
522 
523  virtual Numeric::Ptr floor() const
524  {
525  return Numeric::Ptr(static_cast<Numeric *>(const_cast<AtomicValue *>(Integer::fromValue(m_value).asAtomicValue())));
526  }
527 
528  virtual Numeric::Ptr ceiling() const
529  {
530  return Numeric::Ptr(static_cast<Numeric *>(const_cast<AtomicValue *>(Integer::fromValue(m_value).asAtomicValue())));
531  }
532 
533  virtual Numeric::Ptr abs() const
534  {
535  /* We unconditionally create an Integer even if we're a positive
536  * value, because one part of this is the type change to
537  * xs:integer.
538  *
539  * We've manually inlined qAbs() and invoke xsInteger's
540  * constructor. The reason being that we other gets truncation down
541  * to StorageType. See for instance XQTS test case absint1args-1. */
542  return Numeric::Ptr(static_cast<Numeric *>(const_cast<AtomicValue *>(Integer::fromValue(largerOrEqual(m_value, 0) ? xsInteger(m_value) : -xsInteger(m_value)).asAtomicValue())));
543  }
544 
549  virtual bool isNaN() const
550  {
551  return false;
552  }
553 
558  virtual bool isInf() const
559  {
560  return false;
561  }
562 
563  virtual Item toNegated() const
564  {
565  return Integer::fromValue(-xsInteger(m_value));
566  }
567 
568  virtual bool isSigned() const
569  {
570  switch(DerivedType)
571  {
572  /* Fallthrough all these. */
573  case TypeByte:
574  case TypeInt:
575  case TypeLong:
576  case TypeNegativeInteger:
579  case TypePositiveInteger:
580  case TypeShort:
581  return true;
582  /* Fallthrough all these. */
583  case TypeUnsignedByte:
584  case TypeUnsignedInt:
585  case TypeUnsignedLong:
586  case TypeUnsignedShort:
587  return false;
588  }
589  return false;
590  }
591 
593  {
594  switch(DerivedType)
595  {
596  /* Fallthrough all these. */
597  case TypeByte:
598  case TypeInt:
599  case TypeLong:
600  case TypeNegativeInteger:
603  case TypePositiveInteger:
604  case TypeShort:
605  Q_ASSERT_X(false, Q_FUNC_INFO,
606  "It makes no sense to call this function, see Numeric::toUnsignedInteger().");
607  /* Fallthrough all these. */
608  case TypeUnsignedByte:
609  case TypeUnsignedInt:
610  case TypeUnsignedLong:
611  case TypeUnsignedShort:
612  return m_value;
613  }
614  return 0;
615  }
616 
617  };
618 }
619 
621 
623 
624 #endif
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
DerivedIntegerDetails< DerivedType >::TemporaryStorageType TemporaryStorageType
QExplicitlySharedDataPointer< AtomicValue > Ptr
Definition: qitem_p.h:127
qlonglong toLongLong(bool *ok=0, int base=10) const
Returns the string converted to a long long using base base, which is 10 by default and must be betwe...
Definition: qstring.cpp:5943
static const AtomicType::Ptr xsInt
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int qint32
Definition: qglobal.h:937
virtual Numeric::Ptr ceiling() const
static AtomicValue::Ptr fromValue(const NamePool::Ptr &np, const TemporaryStorageType num)
qulonglong toULongLong(bool *ok=0, int base=10) const
Returns the string converted to an unsigned long long using base base, which is 10 by default and mus...
Definition: qstring.cpp:5984
virtual qulonglong toUnsignedInteger() const
static const AtomicType::Ptr xsShort
static const AtomicType::Ptr xsNegativeInteger
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
static const AtomicType::Ptr xsByte
qint64 xsInteger
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
static AtomicValue::Ptr fromLexical(const NamePool::Ptr &np, const QString &strNumeric)
static AtomicValue::Ptr createError(const QString &description=QString(), const ReportContext::ErrorCode=ReportContext::FORG0001)
StorageType storedValue() const
bool evaluateEBV(const QExplicitlySharedDataPointer< DynamicContext > &) const
virtual xsInteger toInteger() const
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
long ASN1_INTEGER_get ASN1_INTEGER * a
unsigned char quint8
Definition: qglobal.h:934
virtual Numeric::Ptr round() const
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define A(arg)
DerivedIntegerDetails< DerivedType >::StorageType StorageType
virtual xsDecimal toDecimal() const
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual Numeric::Ptr floor() const
xsDouble xsDecimal
static Item fromValue(const xsInteger num)
Definition: qinteger.cpp:52
static const AtomicType::Ptr xsUnsignedByte
signed char qint8
Definition: qglobal.h:933
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool lessThan(const QChar *a, int l, const char *c)
Definition: qurl.cpp:3253
unsigned __int64 quint64
Definition: qglobal.h:943
virtual bool isSigned() const
Returns true if this value is signed. If false is returned, the value is unsigned.
virtual bool isNaN() const
short qint16
Definition: qglobal.h:935
static const AtomicType::Ptr xsUnsignedInt
The namespace for the internal API of QtXmlPatterns.
unsigned short quint16
Definition: qglobal.h:936
__int64 qint64
Definition: qglobal.h:942
static QString formatData(const QString &data)
DerivedInteger(const StorageType num)
static bool largerOrEqual(const A &a, const B &b)
virtual QString stringValue() const
static const AtomicType::Ptr xsNonNegativeInteger
#define Q_INT64_C(c)
Definition: qglobal.h:940
QExplicitlySharedDataPointer< ItemType > Ptr
Definition: qitemtype_p.h:88
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
Contains functions used for formatting arguments, such as keywords and paths, in translated strings...
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
Base class for all numeric values.
static const AtomicType::Ptr xsNonPositiveInteger
virtual Item toNegated() const
virtual xsFloat toFloat() const
virtual ItemType::Ptr type() const
virtual xsDouble toDouble() const
unsigned int quint32
Definition: qglobal.h:938
virtual Numeric::Ptr roundHalfToEven(const xsInteger) const
virtual Numeric::Ptr abs() const
virtual bool isInf() const
static const AtomicType::Ptr xsUnsignedShort
static AtomicValue::Ptr fromValueUnchecked(const TemporaryStorageType num)
xsDouble xsFloat
static const AtomicType::Ptr xsPositiveInteger
Represents instances of derived xs:integer types, such as xs:byte.
quint64 qulonglong
Definition: qglobal.h:952
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
static const AtomicType::Ptr xsLong
#define Q_UINT64_C(c)
Definition: qglobal.h:941
#define QT_END_HEADER
Definition: qglobal.h:137
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
static const AtomicType::Ptr xsUnsignedLong
static ItemType::Ptr itemType()
static bool lessThan(const A &a, const B &b)
QExplicitlySharedDataPointer< Numeric > Ptr
#define Q_FUNC_INFO
Definition: qglobal.h:1871