Qt 4.8
qcardinality_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_Cardinality_H
53 #define Patternist_Cardinality_H
54 
55 #include <QtCore/QtGlobal>
56 
58 
60 
61 class QString;
62 
63 namespace QPatternist
64 {
81  {
82  public:
86  typedef qint32 Count;
87 
93  {
98 
103  };
104 
109  inline Cardinality(const Cardinality &other) : m_min(other.m_min),
110  m_max(other.m_max)
111  {
112  }
113 
120  inline Cardinality() : m_min(-1), m_max(0)
121  {
122  }
123 
128  static inline Cardinality empty()
129  {
130  return Cardinality(0, 0);
131  }
132 
137  static inline Cardinality exactlyOne()
138  {
139  return Cardinality(1, 1);
140  }
141 
146  static inline Cardinality zeroOrOne()
147  {
148  return Cardinality(0, 1);
149  }
150 
155  static inline Cardinality zeroOrMore()
156  {
157  return Cardinality(0, -1);
158  }
159 
163  static inline Cardinality oneOrMore()
164  {
165  return Cardinality(1, -1);
166  }
167 
173  static inline Cardinality twoOrMore()
174  {
175  return Cardinality(2, -1);
176  }
177 
186  static inline Cardinality fromCount(const Count count)
187  {
188  Q_ASSERT_X(count > -1, Q_FUNC_INFO,
189  "A count smaller than 0 makes no sense.");
190  return Cardinality(count, count);
191  }
192 
204  static inline Cardinality fromRange(const Count minimum, const Count maximum)
205  {
206  Q_ASSERT_X(minimum > -1, Q_FUNC_INFO,
207  "minimum should never be less than 0.");
208  Q_ASSERT_X(minimum <= maximum || maximum == -1, Q_FUNC_INFO,
209  "minimum cannot be larger than maximum.");
210 
211  return Cardinality(minimum, maximum);
212  }
213 
214  static inline Cardinality fromExact(const Count count)
215  {
216  Q_ASSERT(count >= 0);
217  return Cardinality(count, count);
218  }
219 
224  inline Count minimum() const
225  {
226  Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality are invalid.");
227  return m_min;
228  }
229 
234  inline Count maximum() const
235  {
236  Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality are invalid.");
237  return m_max;
238  }
239 
244  inline bool allowsMany() const
245  {
246  Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality are invalid.");
247  return m_max == -1 || m_max > 1;
248  }
249 
254  inline bool allowsEmpty() const
255  {
256  Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality are invalid.");
257  return m_min == 0;
258  }
259 
267  inline Cardinality toWithoutMany() const
268  {
269  return m_min == 0 ? Cardinality(0, 1)
270  : Cardinality(1, 1);
271  }
272 
279  inline bool isMatch(const Cardinality &other) const
280  {
281  Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO, "One of the cardinalities are invalid.");
282  if(other.m_min < m_min)
283  return false;
284  else
285  { /* Ok, we now know the minimum will always be ok. */
286  if(m_max == -1)
287  return true; /* We allow infinite, so anything can match. */
288  else if(other.m_max == -1)
289  return false; /* other allows infinity, while we don't. */
290  else
291  return m_max >= other.m_max;
292  }
293  }
294 
300  inline bool canMatch(const Cardinality &other) const
301  {
302  Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO, "One of the cardinalities are invalid.");
303  if(m_max == -1)
304  return m_min <= other.m_min || other.m_max >= m_min || other.m_max == -1;
305  else
306  {
307  if(m_max == other.m_min)
308  return true;
309  else if(m_max > other.m_min)
310  return other.m_max >= m_min || other.m_max == -1;
311  else /* m_max < other.m_min */
312  return false;
313  }
314  }
315 
320  inline bool isEmpty() const
321  {
322  Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality is invalid.");
323  return m_min == 0 && m_max == 0;
324  }
325 
330  inline bool isZeroOrOne() const
331  {
332  Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality is invalid.");
333  return m_min == 0 && m_max == 1;
334  }
335 
340  inline bool isExactlyOne() const
341  {
342  Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality is invalid.");
343  return m_min == 1 && m_max == 1;
344  }
345 
350  inline bool isOneOrMore() const
351  {
352  Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality is invalid.");
353  return m_min > 0 && (m_max == -1 || m_max >= 1);
354  }
355 
360  inline bool isExact() const
361  {
362  Q_ASSERT_X(m_min != -1, Q_FUNC_INFO, "The cardinality is invalid.");
363  return m_min == m_max;
364  }
365 
382  QString displayName(const CustomizeDisplayName explanation) const;
383 
389  inline Cardinality operator|(const Cardinality &other) const
390  {
391  Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO, "One of the cardinalities are invalid.");
392  if(m_max == -1 || other.m_max == -1)
393  return Cardinality(qMin(m_min, other.m_min), -1);
394  else
395  return Cardinality(qMin(m_min, other.m_min), qMax(m_max, other.m_max));
396  }
397 
401  inline Cardinality &operator|=(const Cardinality &other)
402  {
403  Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO, "One of the cardinalities are invalid.");
404  m_min = qMin(m_min, other.m_min);
405 
406  if(m_max == -1)
407  return *this;
408  else if(other.m_max == -1)
409  m_max = -1;
410  else
411  m_max = qMax(m_max, other.m_max);
412 
413  return *this;
414  }
415 
424  inline Cardinality operator&(const Cardinality &other) const
425  {
426  Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO, "One of the cardinalities are invalid.");
427 
428  if(m_max < other.m_min) /* No intersection. */
429  return empty();
430 
431  const Count min = qMax(m_min, other.m_min);
432 
433  if(m_max == -1)
434  return Cardinality(min, other.m_max);
435  else if(other.m_max == -1)
436  return Cardinality(min, m_max);
437  else
438  return Cardinality(min, qMin(m_max, other.m_max));
439  }
440 
448  inline Cardinality operator+(const Cardinality &other) const
449  {
450  Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO, "One of the cardinalities are invalid.");
451  if(m_max == -1 || other.m_max == -1)
452  return Cardinality(m_min + other.m_min, -1);
453  else
454  return Cardinality(m_min + other.m_min, m_max + other.m_max);
455  }
456 
460  inline Cardinality &operator+=(const Cardinality &other)
461  {
462  Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO,
463  "One of the cardinalities are invalid.");
464  m_min += other.m_min;
465 
466  if(m_max == -1)
467  return *this;
468  if(other.m_max == -1)
469  m_max = -1;
470  else
471  m_max += other.m_max;
472 
473  return *this;
474  }
475 
482  inline Cardinality operator*(const Cardinality &other) const
483  {
484  Q_ASSERT_X(m_min != -1 && other.m_min != -1, Q_FUNC_INFO,
485  "One of the cardinalities are invalid.");
486  if(m_max == -1 || other.m_max == -1)
487  return Cardinality(m_min * other.m_min, -1);
488  else
489  return Cardinality(m_min * other.m_min, m_max * other.m_max);
490  }
491 
496  inline Cardinality &operator=(const Cardinality &other)
497  {
498  Q_ASSERT_X(this != &other, Q_FUNC_INFO, "Assigning to oneself makes no sense.");
499  m_min = other.m_min;
500  m_max = other.m_max;
501  return *this;
502  }
503 
512  inline bool operator==(const Cardinality &other) const
513  {
514  return m_min == other.m_min &&
515  m_max == other.m_max;
516  }
517 
521  inline bool operator!=(const Cardinality &other) const
522  {
523  return m_min != other.m_min ||
524  m_max != other.m_max;
525  }
526 
527  private:
528  inline Cardinality(const Count min, const Count max) : m_min(min),
529  m_max(max)
530  {
531  }
532 
533  Count m_min;
534  Count m_max;
535  };
536 }
537 
539 
541 
543 
544 #endif
static Cardinality zeroOrMore()
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int qint32
Definition: qglobal.h:937
bool operator!=(const Cardinality &other) const
Cardinality & operator=(const Cardinality &other)
QString displayName(const CustomizeDisplayName explanation) const
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static Cardinality fromExact(const Count count)
Cardinality operator*(const Cardinality &other) const
static Cardinality twoOrMore()
bool operator==(const Cardinality &other) const
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
Cardinality & operator+=(const Cardinality &other)
static Cardinality empty()
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static Cardinality fromCount(const Count count)
Cardinality operator &(const Cardinality &other) const
static Cardinality oneOrMore()
The namespace for the internal API of QtXmlPatterns.
Cardinality operator+(const Cardinality &other) const
Cardinality operator|(const Cardinality &other) const
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
static Cardinality zeroOrOne()
bool canMatch(const Cardinality &other) const
Cardinality toWithoutMany() const
Cardinality(const Cardinality &other)
Cardinality & operator|=(const Cardinality &other)
static Cardinality exactlyOne()
Cardinality(const Count min, const Count max)
Represents a cardinality, a possible , often represented by occurrence indicators.
bool isMatch(const Cardinality &other) const
Q_DECLARE_TYPEINFO(QPatternist::Cardinality, Q_MOVABLE_TYPE)
static Cardinality fromRange(const Count minimum, const Count maximum)
#define QT_END_HEADER
Definition: qglobal.h:137
#define Q_FUNC_INFO
Definition: qglobal.h:1871