Qt 4.8
Classes | Public Types | Public Functions | Protected Functions | Static Protected Functions | Protected Variables | List of all members
QPatternist::AbstractDuration Class Referenceabstract

Base class for classes implementing durations. More...

#include <qabstractduration_p.h>

Inheritance diagram for QPatternist::AbstractDuration:
QPatternist::AtomicValue QSharedData QPatternist::CppCastingHelper< AtomicValue > QPatternist::DayTimeDuration QPatternist::Duration QPatternist::YearMonthDuration

Classes

class  CaptureTable
 Acts as a mapping table for AbstractDuration::create() and describes where certain fields in a QRegExp pattern can be found for a particular W3C XML Schema duration type. More...
 

Public Types

typedef QExplicitlySharedDataPointer< AbstractDurationPtr
 
typedef qint64 Value
 The amount in milli seconds. More...
 
- Public Types inherited from QPatternist::AtomicValue
typedef QList< AtomicValue::PtrList
 
typedef QExplicitlySharedDataPointer< AtomicValuePtr
 

Public Functions

virtual DayCountProperty days () const =0
 
virtual Item fromValue (const Value val) const =0
 
virtual HourProperty hours () const =0
 
bool isPositive () const
 
virtual MinuteProperty minutes () const =0
 
virtual MonthProperty months () const =0
 
virtual MSecondProperty mseconds () const =0
 
bool operator== (const AbstractDuration &other) const
 
virtual SecondProperty seconds () const =0
 
virtual Value value () const =0
 
virtual YearProperty years () const =0
 
- Public Functions inherited from QPatternist::AtomicValue
virtual bool evaluateEBV (const QExplicitlySharedDataPointer< DynamicContext > &context) const
 
virtual bool hasError () const
 
virtual QString stringValue () const =0
 
virtual ItemType::Ptr type () const =0
 
virtual ~AtomicValue ()
 
- Public Functions inherited from QSharedData
 QSharedData ()
 Constructs a QSharedData object with a reference count of 0. More...
 
 QSharedData (const QSharedData &)
 Constructs a QSharedData object with reference count 0. More...
 
- Public Functions inherited from QPatternist::CppCastingHelper< AtomicValue >
const TCastTarget * as () const
 
TCastTarget * as ()
 

Protected Functions

 AbstractDuration (const bool isPos)
 
- Protected Functions inherited from QPatternist::AtomicValue
 AtomicValue ()
 
- Protected Functions inherited from QPatternist::CppCastingHelper< AtomicValue >
 CppCastingHelper ()
 

Static Protected Functions

static AtomicValue::Ptr create (const CaptureTable &captTable, const QString &lexical, bool *isPositive, YearProperty *years, MonthProperty *months, DayCountProperty *days, HourProperty *hours, MinuteProperty *minutes, SecondProperty *seconds, MSecondProperty *mseconds)
 
static QString serializeMSeconds (const MSecondProperty mseconds)
 

Protected Variables

const bool m_isPositive
 

Additional Inherited Members

- Static Public Functions inherited from QPatternist::AtomicValue
static ItemType::Ptr qtToXDMType (const QXmlItem &item)
 
static QVariant toQt (const AtomicValue *const value)
 
static QVariant toQt (const AtomicValue::Ptr &value)
 
static Item toXDM (const QVariant &value)
 
- Public Variables inherited from QSharedData
QAtomicInt ref
 

Detailed Description

Base class for classes implementing durations.

See also
XML Schema Part 2: Datatypes Second Edition, 3.2.6 duration
XQuery 1.0 and XPath 2.0 Data Model (XDM), 3.3.2 Dates and Times
Author
Frans Englich frans.nosp@m..eng.nosp@m.lich@.nosp@m.noki.nosp@m.a.com

Definition at line 76 of file qabstractduration_p.h.

Typedefs

◆ Ptr

Definition at line 79 of file qabstractduration_p.h.

◆ Value

The amount in milli seconds.

Definition at line 84 of file qabstractduration_p.h.

Constructors and Destructors

◆ AbstractDuration()

AbstractDuration::AbstractDuration ( const bool  isPos)
protected

Definition at line 54 of file qabstractduration.cpp.

54  : m_isPositive(isPos)
55 {
56 }

Functions

◆ create()

AtomicValue::Ptr AbstractDuration::create ( const CaptureTable captTable,
const QString lexical,
bool *  isPositive,
YearProperty years,
MonthProperty months,
DayCountProperty days,
HourProperty hours,
MinuteProperty minutes,
SecondProperty seconds,
MSecondProperty mseconds 
)
staticprotected

Definition at line 61 of file qabstractduration.cpp.

Referenced by QPatternist::Duration::fromLexical(), QPatternist::YearMonthDuration::fromLexical(), and QPatternist::DayTimeDuration::fromLexical().

71 {
72  /* We don't directly write into the arguments(eg @p years) but uses these
73  * because the arguments are intended for normalized values, and therefore
74  * can cause overflows. */
75  MonthCountProperty monthCount = 0;
76  MinuteCountProperty minCount = 0;
77  HourCountProperty hourCount = 0;
78  SecondCountProperty secCount = 0;
79 
81  QRegExp myExp(captTable.regExp); /* Copy, in order to stay thread safe. */
82 
83  if(!myExp.exactMatch(lexical))
84  {
85  error(QString());
86  }
87 
88  const QStringList capts(myExp.capturedTexts());
89 
90 
91  if(days)
92  {
93  if(getCapt(tDelimiter).isEmpty())
94  {
95  if((years && getCapt(year).isEmpty() && getCapt(month).isEmpty() && getCapt(day).isEmpty())
96  ||
97  (!years && getCapt(day).isEmpty()))
98  {
99  error(QtXmlPatterns::tr("At least one component must be present."));
100  }
101  }
102  else if(getCapt(hour).isEmpty() &&
103  getCapt(minutes).isEmpty() &&
104  getCapt(seconds).isEmpty() &&
106  {
107  error(QtXmlPatterns::tr("At least one time component must appear "
108  "after the %1-delimiter.")
109  .arg(formatKeyword("T")));
110  }
111  }
112  else if(getCapt(year).isEmpty() && getCapt(month).isEmpty()) /* This checks xs:yearMonthDuration. */
113  {
114  error(QtXmlPatterns::tr("At least one component must be present."));
115  }
116 
117  /* If we got no '-', we are positive. */
118  *isPositive = capts.at(1).isEmpty();
119 
120  if(days)
121  {
122  Q_ASSERT(hours);
123  Q_ASSERT(minutes);
124  Q_ASSERT(seconds);
126 
127  *days = getCapt(day).toInt();
128  hourCount = getCapt(hour).toInt();
129  minCount = getCapt(minutes).toInt();
130  secCount = getCapt(seconds).toInt();
131 
132  const QString msecondsStr(getCapt(mseconds));
133  if(!msecondsStr.isEmpty())
134  *mseconds = msecondsStr.leftJustified(3, QLatin1Char('0')).toInt();
135  else
136  *mseconds = msecondsStr.toInt();
137 
138  if(secCount > 59)
139  {
140  minCount += secCount / 60;
141  *seconds = secCount % 60;
142  }
143  else
144  *seconds = secCount;
145 
146  if(minCount > 59)
147  {
148  hourCount += minCount / 60;
149  *minutes = minCount % 60;
150  }
151  else
152  *minutes = minCount;
153 
154  if(hourCount > 23)
155  {
156  *days += hourCount / 24;
157  *hours = hourCount % 24;
158  }
159  else
160  *hours = hourCount;
161  }
162 
163  if(!years)
164  return AtomicValue::Ptr();
165 
166  /* We're supposed to handle years/months. */
167  Q_ASSERT(months);
168 
169  *years = getCapt(year).toInt();
170  monthCount = getCapt(month).toInt();
171 
172  if(monthCount > 11)
173  {
174  *years += monthCount / 12;
175  *months = monthCount % 12;
176  }
177  else
178  *months = monthCount;
179 
180  return AtomicValue::Ptr();
181 }
QExplicitlySharedDataPointer< AtomicValue > Ptr
Definition: qitem_p.h:127
virtual SecondProperty seconds() const =0
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
QString formatKeyword(const QString &keyword)
qint32 MinuteCountProperty
virtual MinuteProperty minutes() const =0
#define error(msg)
qint32 MonthCountProperty
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define getCapt(sym)
virtual MonthProperty months() const =0
static bool isEmpty(const char *str)
virtual DayCountProperty days() const =0
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
virtual HourProperty hours() const =0
virtual YearProperty years() const =0
qint32 HourCountProperty
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
qint32 SecondCountProperty
virtual MSecondProperty mseconds() const =0

◆ days()

virtual DayCountProperty QPatternist::AbstractDuration::days ( ) const
pure virtual

◆ fromValue()

virtual Item QPatternist::AbstractDuration::fromValue ( const Value  val) const
pure virtual

A polymorphic factory function that returns instances of the sub-class with the value val.

Implemented in QPatternist::DayTimeDuration, QPatternist::Duration, and QPatternist::YearMonthDuration.

◆ hours()

virtual HourProperty QPatternist::AbstractDuration::hours ( ) const
pure virtual

◆ isPositive()

bool AbstractDuration::isPositive ( ) const

◆ minutes()

virtual MinuteProperty QPatternist::AbstractDuration::minutes ( ) const
pure virtual

◆ months()

virtual MonthProperty QPatternist::AbstractDuration::months ( ) const
pure virtual

◆ mseconds()

virtual MSecondProperty QPatternist::AbstractDuration::mseconds ( ) const
pure virtual

◆ operator==()

bool AbstractDuration::operator== ( const AbstractDuration other) const

Determines whether this Duration is equal to other.

Note
Do not re-implement this function. It uses getters such as years() and mseconds() for determining its truth value.

Definition at line 185 of file qabstractduration.cpp.

186 {
187  if(years() == other.years()
188  && months() == other.months()
189  && days() == other.days()
190  && hours() == other.hours()
191  && minutes() == other.minutes()
192  && seconds() == other.seconds()
193  && mseconds() == other.mseconds())
194  {
195  if(isPositive() == other.isPositive())
196  return true;
197  else if(years() == 0
198  && months() == 0
199  && days() == 0
200  && hours() == 0
201  && minutes() == 0
202  && seconds () == 0
203  && mseconds() == 0)
204  {
205  return true; /* Signedness doesn't matter if all are zero. */
206  }
207  }
208 
209  return false;
210 }
virtual SecondProperty seconds() const =0
virtual MinuteProperty minutes() const =0
virtual MonthProperty months() const =0
virtual DayCountProperty days() const =0
virtual HourProperty hours() const =0
virtual YearProperty years() const =0
virtual MSecondProperty mseconds() const =0

◆ seconds()

virtual SecondProperty QPatternist::AbstractDuration::seconds ( ) const
pure virtual

◆ serializeMSeconds()

QString AbstractDuration::serializeMSeconds ( const MSecondProperty  mseconds)
staticprotected

Definition at line 212 of file qabstractduration.cpp.

Referenced by QPatternist::Duration::stringValue(), and QPatternist::DayTimeDuration::stringValue().

213 {
214  QString retval;
215  retval.append(QLatin1Char('.'));
216  int div = 100;
217  MSecondProperty msecs = mseconds;
218 
219  while(msecs > 0)
220  {
221  int d = msecs / div;
222  retval.append(QLatin1Char(d + '0'));
223  msecs = msecs % div;
224  div = div / 10;
225  }
226 
227  return retval;
228 }
double d
Definition: qnumeric_p.h:62
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString & append(QChar c)
Definition: qstring.cpp:1777
qint16 MSecondProperty
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
virtual MSecondProperty mseconds() const =0

◆ value()

virtual Value QPatternist::AbstractDuration::value ( ) const
pure virtual
Returns
the value of this AbstractDuration. For example, in the case of xs:yearMonthDuration, that is YearMonthDuration, years times twelve plus the months is returned.

Implemented in QPatternist::DayTimeDuration, QPatternist::Duration, and QPatternist::YearMonthDuration.

◆ years()

virtual YearProperty QPatternist::AbstractDuration::years ( ) const
pure virtual

Properties

◆ m_isPositive

const bool QPatternist::AbstractDuration::m_isPositive
protected

The documentation for this class was generated from the following files: