Qt 4.8
Public Functions | Protected Functions | List of all members
QPatternist::CppCastingHelper< TSubClass > Class Template Reference

Provides convenience methods for performing static casts between C++ classes. More...

#include <qcppcastinghelper_p.h>

Public Functions

template<typename TCastTarget >
const TCastTarget * as () const
 
template<typename TCastTarget >
TCastTarget * as ()
 

Protected Functions

 CppCastingHelper ()
 

Detailed Description

template<typename TSubClass>
class QPatternist::CppCastingHelper< TSubClass >

Provides convenience methods for performing static casts between C++ classes.

In Patternist, it is very common to do up-casts from Expression or Item, which typically involves writing messy code. Such an old-way cast looks like this:

static_cast<const MyClass *>(myInstance.data())->myClassMember()

CppCastingHelper provides the convenience method as() for this, which is functionally equivalent to the above code, but simpler:

myInstance->as<MyClass>()->myClassMember()

The as() function performs a static cast.

By using CppCastingHelper, this is achieved:

CppCastingHelper is a template class where the TSubClass parameter must be the class inheriting CppCastingHelper. See Item or Expression for demonstration.

Author
Frans Englich frans.nosp@m..eng.nosp@m.lich@.nosp@m.noki.nosp@m.a.com

Definition at line 97 of file qcppcastinghelper_p.h.

Constructors and Destructors

◆ CppCastingHelper()

template<typename TSubClass>
QPatternist::CppCastingHelper< TSubClass >::CppCastingHelper ( )
inlineprotected

This constructor is protected because this class must be sub-classed.

Definition at line 153 of file qcppcastinghelper_p.h.

153 {}

Functions

◆ as() [1/2]

template<typename TSubClass>
template<typename TCastTarget >
const TCastTarget* QPatternist::CppCastingHelper< TSubClass >::as ( ) const
inline

Casts this instance to:

const TCastTarget *

and returns the result.

When compiled in debug mode, this function perform a dynamic_cast, in order to check the correctness of the cast.

Definition at line 114 of file qcppcastinghelper_p.h.

Referenced by QPatternist::CastAs::castToQName(), QPatternist::XsdTypeChecker::checkConstrainingFacets(), QPatternist::XsdSchemaChecker::checkConstrainingFacets(), QPatternist::XsdTypeChecker::checkConstrainingFacetsBinary(), QPatternist::XsdTypeChecker::checkConstrainingFacetsBoolean(), QPatternist::XsdTypeChecker::checkConstrainingFacetsDateTime(), QPatternist::XsdTypeChecker::checkConstrainingFacetsDecimal(), QPatternist::XsdTypeChecker::checkConstrainingFacetsDouble(), QPatternist::XsdTypeChecker::checkConstrainingFacetsDuration(), QPatternist::XsdTypeChecker::checkConstrainingFacetsList(), QPatternist::XsdTypeChecker::checkConstrainingFacetsNotation(), QPatternist::XsdTypeChecker::checkConstrainingFacetsQName(), QPatternist::XsdTypeChecker::checkConstrainingFacetsSignedInteger(), QPatternist::XsdTypeChecker::checkConstrainingFacetsString(), QPatternist::XsdTypeChecker::checkConstrainingFacetsUnion(), QPatternist::XsdTypeChecker::checkConstrainingFacetsUnsignedInteger(), QPatternist::SubsequenceFN::compress(), QPatternist::GenericPredicate::create(), QPatternist::DeepEqualFN::evaluateEBV(), QPatternist::ComparisonIdentifier::matches(), QPatternist::IntegerIdentifier::matches(), QPatternist::XsdSchemaParser::parseComplexContent(), QPatternist::XsdSchemaParser::parseDefaultOpenContent(), QPatternist::XsdSchemaParser::parseFractionDigitsFacet(), QPatternist::XsdSchemaParser::parseGlobalComplexType(), QPatternist::XsdSchemaParser::parseGlobalElement(), QPatternist::XsdSchemaParser::parseLengthFacet(), QPatternist::XsdSchemaParser::parseLocalComplexType(), QPatternist::XsdSchemaParser::parseLocalElement(), QPatternist::XsdSchemaParser::parseMaxExclusiveFacet(), QPatternist::XsdSchemaParser::parseMaxInclusiveFacet(), QPatternist::XsdSchemaParser::parseMaxLengthFacet(), QPatternist::XsdSchemaParser::parseMinExclusiveFacet(), QPatternist::XsdSchemaParser::parseMinInclusiveFacet(), QPatternist::XsdSchemaParser::parseMinLengthFacet(), QPatternist::XsdSchemaParser::parseMinMaxConstraint(), QPatternist::XsdSchemaParser::parseTotalDigitsFacet(), QPatternist::XsdSchemaParser::parseWhiteSpaceFacet(), QPatternist::XsdSchemaResolver::resolveEnumerationFacetValues(), QPatternist::XSLT20CoreFunctions::retrieveExpression(), QPatternist::XPath10CoreFunctions::retrieveExpression(), QPatternist::XPath20CoreFunctions::retrieveExpression(), QPatternist::RangeExpression::staticType(), QPatternist::SubsequenceFN::staticType(), QPatternist::AtomicValue::toQt(), QPatternist::ElementConstructor::typeCheck(), QPatternist::XsdValidatingInstanceReader::validateElement(), and QPatternist::yyparse().

115  {
116 #if defined(Patternist_DEBUG) && !defined(Q_CC_XLC)
117 /* At least on aix-xlc-64, the compiler cries when it sees dynamic_cast. */
118  Q_ASSERT_X(dynamic_cast<const TCastTarget *>(static_cast<const TSubClass *>(this)),
119  Q_FUNC_INFO,
120  "The cast is invalid. This class does not inherit the cast target.");
121 #endif
122  return static_cast<const TCastTarget *>(static_cast<const TSubClass *>(this));
123  }
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ as() [2/2]

template<typename TSubClass>
template<typename TCastTarget >
TCastTarget* QPatternist::CppCastingHelper< TSubClass >::as ( )
inline

Casts this instance to:

TCastTarget *

and returns the result.

When compiled in debug mode, a dynamic_cast is attempted, in order to check the correctness of the cast.

Definition at line 138 of file qcppcastinghelper_p.h.

139  {
140 #if defined(Patternist_DEBUG) && !defined(Q_CC_XLC)
141 /* At least on aix-xlc-64, the compiler cries when it sees dynamic_cast. */
142  Q_ASSERT_X(dynamic_cast<TCastTarget *>(static_cast<TSubClass *>(this)),
143  Q_FUNC_INFO,
144  "The cast is invalid. This class does not inherit the cast target.");
145 #endif
146  return static_cast<TCastTarget *>(static_cast<TSubClass *>(this));
147  }
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
#define Q_FUNC_INFO
Definition: qglobal.h:1871

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