Cutelee 6.1.0
operatorcasttests.cpp
1/*
2 This file is part of the Cutelee template system.
3
4 Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either version
9 2.1 of the Licence, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library. If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include <iostream>
22
24{
25public:
26 void operation1() { std::cout << "QString operation 1" << std::endl; }
27 void operation2() { std::cout << "QString operation 2" << std::endl; }
28};
29
31{
32public:
33 QVariant() {}
34 QVariant(const QString &)
35 {
36 std::cout << "QVariant::QVariant(QString)" << std::endl;
37 }
38};
39
40class SafeString : public QString
41{
42public:
43 operator QVariant() const
44 {
45 std::cout << "SafeString::operator QVariant()" << std::endl;
46 return QVariant();
47 }
48};
49
51{
52public:
54
56 {
57 std::cout << "WrappingSafeString::WrappingSafeString(QString)" << std::endl;
58 }
59
60 operator QVariant() const
61 {
62 std::cout << "WrappingSafeString::operator QVariant()" << std::endl;
63 return QVariant();
64 }
65
66 operator QString() const
67 {
68 std::cout << "WrappingSafeString::operator QString()" << std::endl;
69 return QString();
70 }
71};
72
74{
75public:
76 WrappingSubclassSafeString() : m_isSafe(false), m_wrappedSubclass(this) {}
77
79 : m_isSafe(false), m_wrappedSubclass(this)
80 {
81 std::cout
82 << "WrappingSubclassSafeString::WrappingSubclassSafeString(QString)"
83 << std::endl;
84 }
85
86 bool isSafe() const { return m_isSafe; };
87 void setSafe(bool safe) { m_isSafe = safe; }
88
89 class Subclass : public QString
90 {
91 friend class WrappingSubclassSafeString;
92 Subclass(WrappingSubclassSafeString *wsss) : m_wsss(wsss) {}
94
95 public:
96 void operation2()
97 {
98 m_wsss->m_isSafe = false;
99 std::cout << "overridden string operation 2 (wrapping)" << std::endl;
100 }
101 };
102
103 Subclass m_wrappedSubclass;
104
105 Subclass *operator->() { return &m_wrappedSubclass; }
106
107 operator QVariant() const
108 {
109 std::cout << "WrappingSubclassSafeString::operator QVariant()" << std::endl;
110 return QVariant();
111 }
112
113 operator QString() const
114 {
115 std::cout << "WrappingSubclassSafeString::operator QString()" << std::endl;
116 return m_wrappedSubclass;
117 }
118
119private:
120 bool m_isSafe;
121};
122
124{
125public:
126 UsingSafeString() {}
127 UsingSafeString(const QString &) {}
128
129 void operation2()
130 {
131 std::cout << "overridden string operation 2 (using)" << std::endl;
132 };
133
134 using QString::operation1;
135
136 operator QVariant() { return QVariant(); }
137
138 operator QString() { return QString(); }
139};
140
141QVariant f1() { return QString(); }
142
143QVariant f2() { return SafeString(); }
144
145QVariant f3() { return WrappingSafeString(); }
146
147QString f4() { return WrappingSafeString(); }
148
149WrappingSafeString f5() { return QString(); }
150
151QVariant f6() { return WrappingSubclassSafeString(); }
152
153QString f7() { return WrappingSubclassSafeString(); }
154
155WrappingSubclassSafeString f8() { return QString(); }
156
157#ifdef BROKEN
158
159QVariant f9() { return UsingSafeString(); }
160
161QString f10() { return UsingSafeString(); }
162
163UsingSafeString f11() { return QString(); }
164
165#endif
166
167int main(int argc, char **argv)
168{
169 f1();
170 f2();
171 f3();
172 f4();
173 f5();
174 f6();
175 f7();
176 f8();
177#ifdef BROKEN
178 f9();
179 f10();
180 f11();
181#endif
182
184 wsss.setSafe(true);
185 wsss->operation1();
186 std::cout << (wsss.isSafe() ? "IsSafe" : "IsNotSafe") << std::endl;
187 wsss->operation2();
188 std::cout << (wsss.isSafe() ? "IsSafe" : "IsNotSafe") << std::endl;
189
190 return 0;
191}