source: trunk/src/xmlpatterns/expr/qcastingplatform_p.h@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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_CastingPlatform_H
53#define Patternist_CastingPlatform_H
54
55#include "qatomiccaster_p.h"
56#include "qqnamevalue_p.h"
57#include "qatomicstring_p.h"
58#include "qvalidationerror_p.h"
59#include "qatomiccasterlocator_p.h"
60#include "qatomictype_p.h"
61#include "qbuiltintypes_p.h"
62#include "qcommonsequencetypes_p.h"
63#include "qschematypefactory_p.h"
64#include "qpatternistlocale_p.h"
65
66QT_BEGIN_HEADER
67
68QT_BEGIN_NAMESPACE
69
70namespace QPatternist
71{
72 /**
73 * @short Provides casting functionality for classes, such as CastAs or NumberFN, which
74 * needs to perform casting.
75 *
76 * Classes which need to perform casting can simply from this class and gain
77 * access to casting functinality wrapped in a convenient way. At the center of this
78 * class is the cast() function, which is used at runtime to perform the actual cast.
79 *
80 * The actual circumstances where casting is used, such as in the 'castable as'
81 * expression or the <tt>fn:number()</tt> function, often have other things to handle as well,
82 * error handling and cardinality checks for example. This class handles only casting
83 * and leaves the other case-specific details to the sub-class such that this class only
84 * do one thing well.
85 *
86 * This template class takes two parameters:
87 * - TSubClass This should be the class inheriting from CastingPlatform.
88 * - issueError if true, errors are issued via ReportContext, otherwise
89 * ValidationError instances are returned appropriately.
90 *
91 * The class inheriting CastingPlatform must implement the following function:
92 * @code
93 * ItemType::Ptr targetType() const
94 * @endcode
95 *
96 * that returns the type that should be cast to. The type must be an AtomicType.
97 * Typically, it is appropriate to declare this function @c inline.
98 *
99 * A sub-class calls prepareCasting() at compile time(such that CastingPlatform can attempt
100 * to lookup the proper AtomicCaster) and then it simply uses the cast() function at runtime. The
101 * function targetType() must be implemented such that CastingPlatform knows
102 * what type it shall cast to.
103 *
104 * @author Frans Englich <[email protected]>
105 * @ingroup Patternist_expressions
106 */
107 template<typename TSubClass, const bool issueError>
108 class CastingPlatform
109 {
110 protected:
111 /**
112 * @note issueCastError() depends on the default value.
113 */
114 inline CastingPlatform(const ReportContext::ErrorCode code = ReportContext::FORG0001) : m_errorCode(code)
115 {
116 }
117
118 /**
119 * Attempts to cast @p sourceValue to targetType(), and returns
120 * the created value. Remember that prepareCasting() should have been
121 * called at compile time, otherwise this function will be slow.
122 *
123 * Error reporting is done in two ways. If a cast fails because
124 * of an error in lexical representation a ValidationError is returned.
125 * If the cause of failure is that the casting combination is invalid(such as
126 * when attempting to cast @c xs:date to @c xs:integer), a ValidationError
127 * is returned if @c false was passed in the template instantiation,
128 * an error is issued via @p context.
129 *
130 * @param sourceValue the value to cast. Must be non @c null.
131 * @param context the usual ReportContext, used for error reporting.
132 * @returns the new value which was the result of the cast. If the
133 * cast failed, an ValidationError is returned.
134 */
135 Item cast(const Item &sourceValue,
136 const ReportContext::Ptr &context) const;
137
138 /**
139 * This function should be called at compiled time, it attempts to determine
140 * what AtomicCaster that should be used when casting from @p sourceType to
141 * targetType(). If that is not possible, because the @p sourceType is
142 * @c xs:anyAtomicType for instance, the AtomicCaster lookup will done at
143 * runtime on a case-per-case basis.
144 *
145 * @returns @c true if the requested casting combination is valid or might be valid.
146 * If it is guranteed to be invalid, @c false is returned.
147 */
148 bool prepareCasting(const ReportContext::Ptr &context,
149 const ItemType::Ptr &sourceType);
150
151 /**
152 * Checks that the targetType() is a valid target type for <tt>castable as</tt>
153 * and <tt>cast as</tt>. For example, that it is not abstract. If the type is
154 * invalid, an error is raised via the @p context. Note that it is assumed the type
155 * is atomic.
156 */
157 void checkTargetType(const ReportContext::Ptr &context) const;
158
159 private:
160 inline Item castWithCaster(const Item &sourceValue,
161 const AtomicCaster::Ptr &caster,
162 const ReportContext::Ptr &context) const;
163
164 /**
165 * Locates the caster for casting values of type @p sourceType to targetType(), if
166 * possible.
167 *
168 * @p castImpossible is not initialized. Initialize it to @c false.
169 */
170 AtomicCaster::Ptr locateCaster(const ItemType::Ptr &sourceType,
171 const ReportContext::Ptr &context,
172 bool &castImpossible) const;
173
174 inline ItemType::Ptr targetType() const
175 {
176 Q_ASSERT(static_cast<const TSubClass *>(this)->targetType());
177 return static_cast<const TSubClass *>(this)->targetType();
178 }
179
180 void issueCastError(const Item &validationError,
181 const Item &sourceValue,
182 const ReportContext::Ptr &context) const;
183
184 Q_DISABLE_COPY(CastingPlatform)
185 AtomicCaster::Ptr m_caster;
186 const ReportContext::ErrorCode m_errorCode;
187 };
188
189#include "qcastingplatform.cpp"
190
191}
192
193QT_END_NAMESPACE
194
195QT_END_HEADER
196
197#endif
Note: See TracBrowser for help on using the repository browser.