Qt 4.8
Protected Functions | Private Functions | Properties | List of all members
QPatternist::ComparisonPlatform< TSubClass, issueError, comparisonType, errorCode > Class Template Reference

Provides comparison functionality for classes that compare Items, such as ValueComparison or MaxFN. More...

#include <qcomparisonplatform_p.h>

Protected Functions

const AtomicComparator::Ptrcomparator () const
 
bool compare (const Item &i1, const Item &i2, const AtomicComparator::Ptr &comp, const AtomicComparator::Operator op) const
 Compares i1 and i2 with operator op, using comparator comp. All input arguments must be valid, and not null. More...
 
 ComparisonPlatform ()
 
AtomicComparator::ComparisonResult detailedFlexibleCompare (const Item &it1, const Item &it2, const DynamicContext::Ptr &context) const
 like flexibleCompare(), but returns the result as an AtomicComparator::Operator instead of bool. More...
 
AtomicComparator::Ptr fetchComparator (const ItemType::Ptr &type1, const ItemType::Ptr &type2, const ReportContext::Ptr &context) const
 
bool flexibleCompare (const Item &it1, const Item &it2, const DynamicContext::Ptr &context) const
 Compares it1 against it2, using comparator() and operatorID(). More...
 
void prepareComparison (const AtomicComparator::Ptr &comparator)
 
void useCaseInsensitiveComparator ()
 

Private Functions

AtomicComparator::Operator operatorID () const
 

Properties

AtomicComparator::Ptr m_comparator
 

Detailed Description

template<typename TSubClass, bool issueError, AtomicComparator::ComparisonType comparisonType = AtomicComparator::AsValueComparison, ReportContext::ErrorCode errorCode = ReportContext::XPTY0004>
class QPatternist::ComparisonPlatform< TSubClass, issueError, comparisonType, errorCode >

Provides comparison functionality for classes that compare Items, such as ValueComparison or MaxFN.

Classes which need comparison functionalities should inherit from this class.

The parameter of this template class is the class inheriting from ComparisonPlatform.

The class inheriting ComparisonPlatform must implement the following function:

Author
Vincent Ricard magic.nosp@m.@mag.nosp@m.icnin.nosp@m.ja.o.nosp@m.rg

Definition at line 89 of file qcomparisonplatform_p.h.

Constructors and Destructors

◆ ComparisonPlatform()

template<typename TSubClass, bool issueError, AtomicComparator::ComparisonType comparisonType = AtomicComparator::AsValueComparison, ReportContext::ErrorCode errorCode = ReportContext::XPTY0004>
QPatternist::ComparisonPlatform< TSubClass, issueError, comparisonType, errorCode >::ComparisonPlatform ( )
inlineprotected

Default constructor. Does nothing. It is implemented in order make template instantiation easier.

Definition at line 101 of file qcomparisonplatform_p.h.

102  {
103  }

Functions

◆ comparator()

template<typename TSubClass, bool issueError, AtomicComparator::ComparisonType comparisonType = AtomicComparator::AsValueComparison, ReportContext::ErrorCode errorCode = ReportContext::XPTY0004>
const AtomicComparator::Ptr& QPatternist::ComparisonPlatform< TSubClass, issueError, comparisonType, errorCode >::comparator ( ) const
inlineprotected
Returns
the AtomicComparator that has been allocated at compile time, with prepareComparison(). If no AtomicComparator has been allocated for some reason, this function returns null.

Definition at line 166 of file qcomparisonplatform_p.h.

167  {
168  return m_comparator;
169  }

◆ compare()

template<typename TSubClass , bool issueError, AtomicComparator::ComparisonType comparisonType, ReportContext::ErrorCode errorCode>
bool QPatternist::ComparisonPlatform< TSubClass, issueError, comparisonType, errorCode >::compare ( const Item i1,
const Item i2,
const AtomicComparator::Ptr comp,
const AtomicComparator::Operator  op 
) const
protected

Compares i1 and i2 with operator op, using comparator comp. All input arguments must be valid, and not null.

This is a fast, raw function which has the requirement that the caller knows what to compare and with what.

Definition at line 95 of file qcomparisonplatform.cpp.

Referenced by QPatternist::ComparisonPlatform< IndexOfIterator, false >::ComparisonPlatform().

99 {
100  Q_ASSERT(oand1);
101  Q_ASSERT(oand2);
102  Q_ASSERT(comp);
103 
104  switch(op)
105  {
107  return comp->equals(oand1, oand2);
109  return !comp->equals(oand1, oand2);
112  /* Fallthrough. */
114  return comp->compare(oand1, op, oand2) == AtomicComparator::LessThan;
116  return comp->compare(oand1, op, oand2) == AtomicComparator::GreaterThan;
118  {
119  const AtomicComparator::ComparisonResult ret = comp->compare(oand1, op, oand2);
120  return ret == AtomicComparator::LessThan || ret == AtomicComparator::Equal;
121  }
123  {
124  const AtomicComparator::ComparisonResult ret = comp->compare(oand1, op, oand2);
126  }
127  }
128 
129  /* GCC unbarfer, this line should never be reached. */
130  Q_ASSERT(false);
131  return false;
132 }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823

◆ detailedFlexibleCompare()

template<typename TSubClass , bool issueError, AtomicComparator::ComparisonType comparisonType, ReportContext::ErrorCode errorCode>
AtomicComparator::ComparisonResult QPatternist::ComparisonPlatform< TSubClass, issueError, comparisonType, errorCode >::detailedFlexibleCompare ( const Item it1,
const Item it2,
const DynamicContext::Ptr context 
) const
protected

like flexibleCompare(), but returns the result as an AtomicComparator::Operator instead of bool.

This is useful when it is significant how a less than comparison fails; whether the two values are equal or greater than.

Definition at line 72 of file qcomparisonplatform.cpp.

Referenced by QPatternist::ComparisonPlatform< IndexOfIterator, false >::ComparisonPlatform(), and qLess< Item::List >::operator()().

75 {
77 
78  if(m_comparator)
79  comp = m_comparator;
80  else
81  {
82  comp = fetchComparator(it1.type(),
83  it2.type(),
84  context);
85  }
86 
88  Q_FUNC_INFO, "Only OperatorLessThan is currently supported for this function.");
89  return comp->compare(it1, operatorID(), it2);
90 }
AtomicComparator::Ptr fetchComparator(const ItemType::Ptr &type1, const ItemType::Ptr &type2, const ReportContext::Ptr &context) const
AtomicComparator::Operator operatorID() const
QExplicitlySharedDataPointer< AtomicComparator > Ptr
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ fetchComparator()

template<typename TSubClass , bool issueError, AtomicComparator::ComparisonType comparisonType, ReportContext::ErrorCode errorCode>
AtomicComparator::Ptr QPatternist::ComparisonPlatform< TSubClass, issueError, comparisonType, errorCode >::fetchComparator ( const ItemType::Ptr type1,
const ItemType::Ptr type2,
const ReportContext::Ptr context 
) const
protected

Utility function for fetching the appropriate AtomicComparator for two atomic values of type type1 and type2, for the operator op.

This function is used throughout the implementation, ranging from the ValueComparison itself, to for example the aggregate functions.

Parameters
contextthe ordinary ReportContext, used for issuing errors.
type1the type of the first operand value in a comparison for which the returned AtomicComparator is intended for
type2the type of the second operand value in a comparison for which the returned AtomicComparator is intended for. Whether type1 and type2 corresponds to what is the first second operand type does not have significance, the order can be arbitrary

Definition at line 137 of file qcomparisonplatform.cpp.

Referenced by QPatternist::ComparisonPlatform< IndexOfIterator, false >::ComparisonPlatform().

140 {
141  Q_ASSERT(t1);
142  Q_ASSERT(t2);
143 
144  if(*BuiltinTypes::xsAnyAtomicType == *t1 ||
146  *BuiltinTypes::item == *t1 ||
147  *BuiltinTypes::item == *t2 ||
148  *BuiltinTypes::numeric == *t1 ||
149  *BuiltinTypes::numeric == *t2 ||
150  *CommonSequenceTypes::Empty == *t1 ||
152  {
153  /* The static type of(at least) one of the operands could not
154  * be narrowed further, so we do the operator
155  * lookup at runtime.
156  */
157  return AtomicComparator::Ptr();
158  }
159 
160  const AtomicComparatorLocator::Ptr locator
161  (static_cast<const AtomicType *>(t1.data())->comparatorLocator());
162 
163  if(!locator)
164  {
165  if(issueError)
166  {
167  context->error(QtXmlPatterns::tr("No comparisons can be done involving the type %1.")
168  .arg(formatType(context->namePool(), t1)),
169  errorCode, static_cast<const TSubClass *>(this)->actualReflection());
170  }
171  return AtomicComparator::Ptr();
172  }
173 
174  const AtomicComparator::Ptr comp(static_cast<const AtomicType *>(t2.data())->accept(locator, operatorID(),
175  static_cast<const TSubClass *>(this)->actualReflection()));
176 
177  if(comp)
178  return comp;
179  else if(issueError)
180  {
181  context->error(QtXmlPatterns::tr("Operator %1 is not available between atomic values of type %2 and %3.")
183  comparisonType)),
184  formatType(context->namePool(), t1),
185  formatType(context->namePool(), t2)),
186  errorCode, static_cast<const TSubClass *>(this)->actualReflection());
187  }
188 
189  return AtomicComparator::Ptr();
190 }
QString formatKeyword(const QString &keyword)
QString formatType(const NamePool::Ptr &np, const T &type)
Formats ItemType and SequenceType.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static const ItemType::Ptr item
AtomicComparator::Operator operatorID() const
QExplicitlySharedDataPointer< AtomicComparator > Ptr
static const EmptySequenceType::Ptr Empty
QExplicitlySharedDataPointer< AtomicComparatorLocator > Ptr
static const AtomicType::Ptr numeric
static QString displayName(const AtomicComparator::Operator op, const ComparisonType type)
static const AtomicType::Ptr xsAnyAtomicType

◆ flexibleCompare()

template<typename TSubClass , bool issueError, AtomicComparator::ComparisonType comparisonType, ReportContext::ErrorCode errorCode>
bool QPatternist::ComparisonPlatform< TSubClass, issueError, comparisonType, errorCode >::flexibleCompare ( const Item it1,
const Item it2,
const DynamicContext::Ptr context 
) const
protected

Compares it1 against it2, using comparator() and operatorID().

If the comparator wasn't looked up at compile time, it will be attempted before comparing. If this fails, errors are reported via context.

Definition at line 51 of file qcomparisonplatform.cpp.

Referenced by QPatternist::ComparisonPlatform< IndexOfIterator, false >::ComparisonPlatform().

54 {
55  if(m_comparator)
56  /* The comparator was located at compile time. */
57  return compare(it1, it2, m_comparator, operatorID());
58  else
59  {
60  const AtomicComparator::Ptr cp(fetchComparator(it1.type(),
61  it2.type(),
62  context));
63 
64  return cp ? compare(it1, it2, cp, operatorID()) : false;
65  }
66 }
bool compare(const Item &i1, const Item &i2, const AtomicComparator::Ptr &comp, const AtomicComparator::Operator op) const
Compares i1 and i2 with operator op, using comparator comp. All input arguments must be valid...
AtomicComparator::Ptr fetchComparator(const ItemType::Ptr &type1, const ItemType::Ptr &type2, const ReportContext::Ptr &context) const
AtomicComparator::Operator operatorID() const
QExplicitlySharedDataPointer< AtomicComparator > Ptr

◆ operatorID()

template<typename TSubClass, bool issueError, AtomicComparator::ComparisonType comparisonType = AtomicComparator::AsValueComparison, ReportContext::ErrorCode errorCode = ReportContext::XPTY0004>
AtomicComparator::Operator QPatternist::ComparisonPlatform< TSubClass, issueError, comparisonType, errorCode >::operatorID ( ) const
inlineprivate
Returns
the operator that is used.

Definition at line 186 of file qcomparisonplatform_p.h.

Referenced by QPatternist::ComparisonPlatform< IndexOfIterator, false >::operatorID().

187  {
188  Q_ASSERT(static_cast<const TSubClass *>(this)->operatorID());
189  return static_cast<const TSubClass *>(this)->operatorID();
190  }
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
AtomicComparator::Operator operatorID() const

◆ prepareComparison()

template<typename TSubClass , bool issueError, AtomicComparator::ComparisonType comparisonType, ReportContext::ErrorCode errorCode>
void QPatternist::ComparisonPlatform< TSubClass, issueError, comparisonType, errorCode >::prepareComparison ( const AtomicComparator::Ptr comparator)
protected

Makes ComparisonPlatform use the AtomicComparator comparator.

Definition at line 195 of file qcomparisonplatform.cpp.

196 {
197  m_comparator = c;
198 }
unsigned char c[8]
Definition: qnumeric_p.h:62

◆ useCaseInsensitiveComparator()

template<typename TSubClass, bool issueError, AtomicComparator::ComparisonType comparisonType = AtomicComparator::AsValueComparison, ReportContext::ErrorCode errorCode = ReportContext::XPTY0004>
void QPatternist::ComparisonPlatform< TSubClass, issueError, comparisonType, errorCode >::useCaseInsensitiveComparator ( )
inlineprotected

Calling this function makes ComparisonPlatform use a comparator that compares strings case insensitively.

See also
ValueComparison::isCaseInsensitiveCompare()

Definition at line 177 of file qcomparisonplatform_p.h.

178  {
179  m_comparator = AtomicComparator::Ptr(new CaseInsensitiveStringComparator());
180  }
QExplicitlySharedDataPointer< AtomicComparator > Ptr

Properties

◆ m_comparator

template<typename TSubClass, bool issueError, AtomicComparator::ComparisonType comparisonType = AtomicComparator::AsValueComparison, ReportContext::ErrorCode errorCode = ReportContext::XPTY0004>
AtomicComparator::Ptr QPatternist::ComparisonPlatform< TSubClass, issueError, comparisonType, errorCode >::m_comparator
private

The comparator that is used for comparing atomic values. The AtomicComparator that is used, depends on the static type of the operands. m_comparator can be null if it wasn't possible to determine what comparator to use at compile time.

Definition at line 197 of file qcomparisonplatform_p.h.

Referenced by QPatternist::ComparisonPlatform< IndexOfIterator, false >::comparator(), and QPatternist::ComparisonPlatform< IndexOfIterator, false >::useCaseInsensitiveComparator().


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