~ubuntu-branches/ubuntu/precise/grantlee/precise

« back to all changes in this revision

Viewing changes to tests/operatorcasttests.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2010-06-11 23:41:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100611234145-oas7rhdrbwy8j55c
Tags: upstream-0.1.1
ImportĀ upstreamĀ versionĀ 0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of the Grantlee 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 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
  Library 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
public:
 
25
  void operation1()
 
26
  {
 
27
    std::cout << "QString operation 1" << std::endl;
 
28
  }
 
29
  void operation2()
 
30
  {
 
31
    std::cout << "QString operation 2" << std::endl;
 
32
  }
 
33
};
 
34
 
 
35
class QVariant
 
36
{
 
37
public:
 
38
  QVariant() {}
 
39
  QVariant(const QString &) {
 
40
    std::cout << "QVariant::QVariant(QString)" << std::endl;
 
41
  }
 
42
};
 
43
 
 
44
class SafeString : public QString
 
45
{
 
46
public:
 
47
  operator QVariant() const {
 
48
    std::cout << "SafeString::operator QVariant()" << std::endl;
 
49
    return QVariant();
 
50
  }
 
51
};
 
52
 
 
53
class WrappingSafeString
 
54
{
 
55
public:
 
56
  WrappingSafeString() {}
 
57
 
 
58
  WrappingSafeString(const QString &) {
 
59
    std::cout << "WrappingSafeString::WrappingSafeString(QString)" << std::endl;
 
60
  }
 
61
 
 
62
  operator QVariant() const {
 
63
    std::cout << "WrappingSafeString::operator QVariant()" << std::endl;
 
64
    return QVariant();
 
65
  }
 
66
 
 
67
  operator QString() const {
 
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 &)  : m_isSafe(false), m_wrappedSubclass(this) {
 
79
    std::cout << "WrappingSubclassSafeString::WrappingSubclassSafeString(QString)" << std::endl;
 
80
  }
 
81
 
 
82
  bool isSafe() const { return m_isSafe; };
 
83
  void setSafe(bool safe) { m_isSafe = safe; }
 
84
 
 
85
  class Subclass : public QString
 
86
  {
 
87
    friend class WrappingSubclassSafeString;
 
88
    Subclass(WrappingSubclassSafeString *wsss) : m_wsss(wsss) {}
 
89
    WrappingSubclassSafeString *m_wsss;
 
90
 
 
91
  public:
 
92
    void operation2()
 
93
    {
 
94
      m_wsss->m_isSafe = false;
 
95
      std::cout << "overridden string operation 2 (wrapping)" << std::endl;
 
96
    }
 
97
  };
 
98
 
 
99
  Subclass m_wrappedSubclass;
 
100
 
 
101
  Subclass* operator->()
 
102
  {
 
103
    return &m_wrappedSubclass;
 
104
  }
 
105
 
 
106
  operator QVariant() const {
 
107
    std::cout << "WrappingSubclassSafeString::operator QVariant()" << std::endl;
 
108
    return QVariant();
 
109
  }
 
110
 
 
111
  operator QString() const {
 
112
    std::cout << "WrappingSubclassSafeString::operator QString()" << std::endl;
 
113
    return m_wrappedSubclass;
 
114
  }
 
115
 
 
116
private:
 
117
  bool m_isSafe;
 
118
};
 
119
 
 
120
class UsingSafeString : private QString
 
121
{
 
122
public:
 
123
  UsingSafeString() {}
 
124
  UsingSafeString(const QString &) {}
 
125
 
 
126
  void operation2() {
 
127
    std::cout << "overridden string operation 2 (using)" << std::endl;
 
128
  };
 
129
 
 
130
  using QString::operation1;
 
131
 
 
132
  operator QVariant()
 
133
  {
 
134
    return QVariant();
 
135
  }
 
136
 
 
137
  operator QString()
 
138
  {
 
139
    return QString();
 
140
  }
 
141
};
 
142
 
 
143
QVariant f1() {
 
144
  return QString();
 
145
}
 
146
 
 
147
QVariant f2() {
 
148
  return SafeString();
 
149
}
 
150
 
 
151
QVariant f3() {
 
152
  return WrappingSafeString();
 
153
}
 
154
 
 
155
QString f4() {
 
156
  return WrappingSafeString();
 
157
}
 
158
 
 
159
WrappingSafeString f5() {
 
160
  return QString();
 
161
}
 
162
 
 
163
QVariant f6() {
 
164
  return WrappingSubclassSafeString();
 
165
}
 
166
 
 
167
QString f7() {
 
168
  return WrappingSubclassSafeString();
 
169
}
 
170
 
 
171
WrappingSubclassSafeString f8() {
 
172
  return QString();
 
173
}
 
174
 
 
175
#ifdef BROKEN
 
176
 
 
177
QVariant f9() {
 
178
  return UsingSafeString();
 
179
}
 
180
 
 
181
QString f10() {
 
182
  return UsingSafeString();
 
183
}
 
184
 
 
185
UsingSafeString f11() {
 
186
  return QString();
 
187
}
 
188
 
 
189
#endif
 
190
 
 
191
int main(int argc, char ** argv)
 
192
{
 
193
  f1();
 
194
  f2();
 
195
  f3();
 
196
  f4();
 
197
  f5();
 
198
  f6();
 
199
  f7();
 
200
  f8();
 
201
#ifdef BROKEN
 
202
  f9();
 
203
  f10();
 
204
  f11();
 
205
#endif
 
206
 
 
207
  WrappingSubclassSafeString wsss;
 
208
  wsss.setSafe(true);
 
209
  wsss->operation1();
 
210
  std::cout << ( wsss.isSafe() ? "IsSafe" : "IsNotSafe" ) << std::endl;
 
211
  wsss->operation2();
 
212
  std::cout << ( wsss.isSafe() ? "IsSafe" : "IsNotSafe" ) << std::endl;
 
213
 
 
214
  return 0;
 
215
}
 
216