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

Last change on this file since 729 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 8.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia 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 "qatomiccasterlocator_p.h"
56#include "qatomiccaster_p.h"
57#include "qatomicstring_p.h"
58#include "qatomictype_p.h"
59#include "qbuiltintypes_p.h"
60#include "qcommonsequencetypes_p.h"
61#include "qpatternistlocale_p.h"
62#include "qqnamevalue_p.h"
63#include "qschematypefactory_p.h"
64#include "qstaticcontext_p.h"
65#include "qvalidationerror_p.h"
66
67QT_BEGIN_HEADER
68
69QT_BEGIN_NAMESPACE
70
71namespace QPatternist
72{
73 /**
74 * @short Provides casting functionality for classes, such as CastAs or NumberFN, which
75 * needs to perform casting.
76 *
77 * Classes which need to perform casting can simply from this class and gain
78 * access to casting functinality wrapped in a convenient way. At the center of this
79 * class is the cast() function, which is used at runtime to perform the actual cast.
80 *
81 * The actual circumstances where casting is used, such as in the 'castable as'
82 * expression or the <tt>fn:number()</tt> function, often have other things to handle as well,
83 * error handling and cardinality checks for example. This class handles only casting
84 * and leaves the other case-specific details to the sub-class such that this class only
85 * do one thing well.
86 *
87 * This template class takes two parameters:
88 * - TSubClass This should be the class inheriting from CastingPlatform.
89 * - issueError if true, errors are issued via ReportContext, otherwise
90 * ValidationError instances are returned appropriately.
91 *
92 * The class inheriting CastingPlatform must implement the following function:
93 * @code
94 * ItemType::Ptr targetType() const
95 * @endcode
96 *
97 * that returns the type that should be cast to. The type must be an AtomicType.
98 * Typically, it is appropriate to declare this function @c inline.
99 *
100 * A sub-class calls prepareCasting() at compile time(such that CastingPlatform can attempt
101 * to lookup the proper AtomicCaster) and then it simply uses the cast() function at runtime. The
102 * function targetType() must be implemented such that CastingPlatform knows
103 * what type it shall cast to.
104 *
105 * @see ValueFactory
106 * @author Frans Englich <[email protected]>
107 * @ingroup Patternist_expressions
108 */
109 template<typename TSubClass, const bool issueError>
110 class CastingPlatform
111 {
112 protected:
113 /**
114 * @note issueCastError() depends on the default value.
115 */
116 inline CastingPlatform(const ReportContext::ErrorCode code = ReportContext::FORG0001) : m_errorCode(code)
117 {
118 }
119
120 /**
121 * Attempts to cast @p sourceValue to targetType(), and returns
122 * the created value. Remember that prepareCasting() should have been
123 * called at compile time, otherwise this function will be slow.
124 *
125 * Error reporting is done in two ways. If a cast fails because
126 * of an error in lexical representation a ValidationError is returned.
127 * If the cause of failure is that the casting combination is invalid(such as
128 * when attempting to cast @c xs:date to @c xs:integer), a ValidationError