Cutelee
6.1.0
templates
tests
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
23
class
QString
24
{
25
public
:
26
void
operation1() { std::cout <<
"QString operation 1"
<< std::endl; }
27
void
operation2() { std::cout <<
"QString operation 2"
<< std::endl; }
28
};
29
30
class
QVariant
31
{
32
public
:
33
QVariant
() {}
34
QVariant
(
const
QString
&)
35
{
36
std::cout <<
"QVariant::QVariant(QString)"
<< std::endl;
37
}
38
};
39
40
class
SafeString
:
public
QString
41
{
42
public
:
43
operator
QVariant
()
const
44
{
45
std::cout <<
"SafeString::operator QVariant()"
<< std::endl;
46
return
QVariant
();
47
}
48
};
49
50
class
WrappingSafeString
51
{
52
public
:
53
WrappingSafeString
() {}
54
55
WrappingSafeString
(
const
QString
&)
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
73
class
WrappingSubclassSafeString
74
{
75
public
:
76
WrappingSubclassSafeString
() : m_isSafe(
false
), m_wrappedSubclass(
this
) {}
77
78
WrappingSubclassSafeString
(
const
QString
&)
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) {}
93
WrappingSubclassSafeString
*m_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
119
private
:
120
bool
m_isSafe;
121
};
122
123
class
UsingSafeString
:
private
QString
124
{
125
public
:
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
141
QVariant
f1() {
return
QString
(); }
142
143
QVariant
f2() {
return
SafeString
(); }
144
145
QVariant
f3() {
return
WrappingSafeString
(); }
146
147
QString
f4() {
return
WrappingSafeString
(); }
148
149
WrappingSafeString
f5() {
return
QString
(); }
150
151
QVariant
f6() {
return
WrappingSubclassSafeString
(); }
152
153
QString
f7() {
return
WrappingSubclassSafeString
(); }
154
155
WrappingSubclassSafeString
f8() {
return
QString
(); }
156
157
#ifdef BROKEN
158
159
QVariant
f9() {
return
UsingSafeString
(); }
160
161
QString
f10() {
return
UsingSafeString
(); }
162
163
UsingSafeString
f11() {
return
QString
(); }
164
165
#endif
166
167
int
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
183
WrappingSubclassSafeString
wsss;
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
}
QString
Definition
operatorcasttests.cpp:24
QVariant
Definition
operatorcasttests.cpp:31
SafeString
Definition
operatorcasttests.cpp:41
UsingSafeString
Definition
operatorcasttests.cpp:124
WrappingSafeString
Definition
operatorcasttests.cpp:51
WrappingSubclassSafeString::Subclass
Definition
operatorcasttests.cpp:90
WrappingSubclassSafeString
Definition
operatorcasttests.cpp:74
Generated by
1.9.8