1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40//
41// W A R N I N G
42// -------------
43//
44// This file is not part of the Qt API. It exists purely as an
45// implementation detail. This header file may change from version to
46// version without notice, or even be removed.
47//
48// We mean it.
49
50#ifndef Patternist_CastingPlatform_H
51#define Patternist_CastingPlatform_H
52
53#include <private/qatomiccasterlocator_p.h>
54#include <private/qatomiccaster_p.h>
55#include <private/qatomicstring_p.h>
56#include <private/qatomictype_p.h>
57#include <private/qbuiltintypes_p.h>
58#include <private/qcommonsequencetypes_p.h>
59#include <private/qpatternistlocale_p.h>
60#include <private/qqnamevalue_p.h>
61#include <private/qschematypefactory_p.h>
62#include <private/qstaticcontext_p.h>
63#include <private/qvalidationerror_p.h>
64
65QT_BEGIN_NAMESPACE
66
67namespace QPatternist
68{
69 /**
70 * @short Provides casting functionality for classes, such as CastAs or NumberFN, which
71 * needs to perform casting.
72 *
73 * Classes which need to perform casting can simply from this class and gain
74 * access to casting functinality wrapped in a convenient way. At the center of this
75 * class is the cast() function, which is used at runtime to perform the actual cast.
76 *
77 * The actual circumstances where casting is used, such as in the 'castable as'
78 * expression or the <tt>fn:number()</tt> function, often have other things to handle as well,
79 * error handling and cardinality checks for example. This class handles only casting
80 * and leaves the other case-specific details to the sub-class such that this class only
81 * do one thing well.
82 *
83 * This template class takes two parameters:
84 * - TSubClass This should be the class inheriting from CastingPlatform.
85 * - issueError if true, errors are issued via ReportContext, otherwise
86 * ValidationError instances are returned appropriately.
87 *
88 * The class inheriting CastingPlatform must implement the following function:
89 * @code
90 * ItemType::Ptr targetType() const
91 * @endcode
92 *
93 * that returns the type that should be cast to. The type must be an AtomicType.
94 * Typically, it is appropriate to declare this function @c inline.
95 *
96 * A sub-class calls prepareCasting() at compile time(such that CastingPlatform can attempt
97 * to lookup the proper AtomicCaster) and then it simply uses the cast() function at runtime. The
98 * function targetType() must be implemented such that CastingPlatform knows
99 * what type it shall cast to.
100 *
101 * @see ValueFactory
102 * @author Frans Englich <frans.englich@nokia.com>
103 * @ingroup Patternist_expressions
104 */
105 template<typename TSubClass, const bool issueError>
106 class CastingPlatform
107 {
108 protected:
109 /**
110 * @note issueCastError() depends on the default value.
111 */
112 inline CastingPlatform(const ReportContext::ErrorCode code = ReportContext::FORG0001) : m_errorCode(code)
113 {
114 }
115
116 /**
117 * Attempts to cast @p sourceValue to targetType(), and returns
118 * the created value. Remember that prepareCasting() should have been
119 * called at compile time, otherwise this function will be slow.
120 *
121 * Error reporting is done in two ways. If a cast fails because
122 * of an error in lexical representation a ValidationError is returned.
123 * If the cause of failure is that the casting combination is invalid(such as
124 * when attempting to cast @c xs:date to @c xs:integer), a ValidationError
125 * is returned if @c false was passed in the template instantiation,
126 * an error is issued via @p context.
127 *
128 * @param sourceValue the value to cast. Must be non @c null.
129 * @param context the usual ReportContext, used for error reporting.
130 * @returns the new value which was the result of the cast. If the
131 * cast failed, an ValidationError is returned.
132 */
133 Item cast(const Item &sourceValue,
134 const ReportContext::Ptr &context) const;
135
136 /**
137 * This function should be called at compiled time, it attempts to determine
138 * what AtomicCaster that should be used when casting from @p sourceType to
139 * targetType(). If that is not possible, because the @p sourceType is
140 * @c xs:anyAtomicType for instance, the AtomicCaster lookup will done at
141 * runtime on a case-per-case basis.
142 *
143 * @returns @c true if the requested casting combination is valid or might be valid.
144 * If it is guranteed to be invalid, @c false is returned.
145 */
146 bool prepareCasting(const ReportContext::Ptr &context,
147 const ItemType::Ptr &sourceType);
148
149 /**
150 * Checks that the targetType() is a valid target type for <tt>castable as</tt>
151 * and <tt>cast as</tt>. For example, that it is not abstract. If the type is
152 * invalid, an error is raised via the @p context. Note that it is assumed the type
153 * is atomic.
154 */
155 void checkTargetType(const ReportContext::Ptr &context) const;
156
157 private:
158 inline Item castWithCaster(const Item &sourceValue,
159 const AtomicCaster::Ptr &caster,
160 const ReportContext::Ptr &context) const;
161
162 /**
163 * Locates the caster for casting values of type @p sourceType to targetType(), if
164 * possible.
165 *
166 * @p castImpossible is not initialized. Initialize it to @c false.
167 */
168 static AtomicCaster::Ptr locateCaster(const ItemType::Ptr &sourceType,
169 const ReportContext::Ptr &context,
170 bool &castImpossible,
171 const SourceLocationReflection *const location,
172 const ItemType::Ptr &targetType);
173 private:
174 inline Item castWithCaster(const Item &sourceValue,
175 const AtomicCaster::Ptr &caster,
176 const DynamicContext::Ptr &context) const;
177
178
179 inline ItemType::Ptr targetType() const
180 {
181 Q_ASSERT(static_cast<const TSubClass *>(this)->targetType());
182 return static_cast<const TSubClass *>(this)->targetType();
183 }
184
185 void issueCastError(const Item &validationError,
186 const Item &sourceValue,
187 const ReportContext::Ptr &context) const;
188
189 Q_DISABLE_COPY(CastingPlatform)
190 AtomicCaster::Ptr m_caster;
191 const ReportContext::ErrorCode m_errorCode;
192 };
193
194#include "qcastingplatform_tpl_p.h"
195
196}
197
198QT_END_NAMESPACE
199
200#endif
201

source code of qtxmlpatterns/src/xmlpatterns/expr/qcastingplatform_p.h