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_CppCastingHelper_H
51#define Patternist_CppCastingHelper_H
52
53#include <QtCore/QtGlobal>
54
55QT_BEGIN_NAMESPACE
56
57namespace QPatternist
58{
59 /**
60 * @short Provides convenience methods for performing static casts between C++ classes.
61 *
62 * In Patternist, it is very common to do up-casts from Expression or Item, which typically
63 * involves writing messy code. Such an old-way cast looks like this:
64 *
65 * @code
66 * static_cast<const MyClass *>(myInstance.data())->myClassMember()
67 * @endcode
68 *
69 * CppCastingHelper provides the convenience method as() for this, which is functionally
70 * equivalent to the above code, but simpler:
71 *
72 * @code
73 * myInstance->as<MyClass>()->myClassMember()
74 * @endcode
75 *
76 * The as() function performs a static cast.
77 *
78 * By using CppCastingHelper, this is achieved:
79 *
80 * - Const correctness is automatically taken care of
81 * - Less code to write
82 * - When compiling in debug mode, the as() functions uses a @c dynamic_cast to verify that the
83 * static casts are properly done, such that sensible error messages are given when the casts
84 * are invalid. It also traps invalid casts which nevertheless happen to work on a particular
85 * platform/compiler/hardware architecture.
86 *
87 * CppCastingHelper is a template class where the TSubClass parameter must be the class
88 * inheriting CppCastingHelper. See Item or Expression for demonstration.
89 *
90 * @author Frans Englich <frans.englich@nokia.com>
91 */
92 template<typename TSubClass>
93 class CppCastingHelper
94 {
95 public:
96
97 /**
98 * Casts this instance to:
99 *
100 * @code
101 * const TCastTarget *
102 * @endcode
103 *
104 * and returns the result.
105 *
106 * When compiled in debug mode, this function perform a @c dynamic_cast, in order to
107 * check the correctness of the cast.
108 */
109 template<typename TCastTarget>
110 inline const TCastTarget *as() const
111 {
112#if defined(Patternist_DEBUG) && !defined(Q_CC_XLC)
113/* At least on aix-xlc-64, the compiler cries when it sees dynamic_cast. */
114 Q_ASSERT_X(dynamic_cast<const TCastTarget *>(static_cast<const TSubClass *>(this)),
115 Q_FUNC_INFO,
116 "The cast is invalid. This class does not inherit the cast target.");
117#endif
118 return static_cast<const TCastTarget *>(static_cast<const TSubClass *>(this));
119 }
120
121 /**
122 * Casts this instance to:
123 *
124 * @code
125 * TCastTarget *
126 * @endcode
127 *
128 * and returns the result.
129 *
130 * When compiled in debug mode, a @c dynamic_cast is attempted, in order to
131 * check the correctness of the cast.
132 */
133 template<typename TCastTarget>
134 inline TCastTarget *as()
135 {
136#if defined(Patternist_DEBUG) && !defined(Q_CC_XLC)
137/* At least on aix-xlc-64, the compiler cries when it sees dynamic_cast. */
138 Q_ASSERT_X(dynamic_cast<TCastTarget *>(static_cast<TSubClass *>(this)),
139 Q_FUNC_INFO,
140 "The cast is invalid. This class does not inherit the cast target.");
141#endif
142 return static_cast<TCastTarget *>(static_cast<TSubClass *>(this));
143 }
144
145 protected:
146 /**
147 * This constructor is protected because this class must be sub-classed.
148 */
149 inline CppCastingHelper() {}
150 };
151}
152
153QT_END_NAMESPACE
154
155#endif
156

source code of qtxmlpatterns/src/xmlpatterns/utils/qcppcastinghelper_p.h