File: operatorcasttests.cpp

package info (click to toggle)
grantlee 0.1.4-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,432 kB
  • ctags: 1,799
  • sloc: cpp: 15,507; python: 108; sh: 44; ruby: 22; makefile: 4
file content (216 lines) | stat: -rw-r--r-- 4,235 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
  This file is part of the Grantlee template system.

  Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either version
  2 of the Licence, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <iostream>

class QString {
public:
  void operation1()
  {
    std::cout << "QString operation 1" << std::endl;
  }
  void operation2()
  {
    std::cout << "QString operation 2" << std::endl;
  }
};

class QVariant
{
public:
  QVariant() {}
  QVariant(const QString &) {
    std::cout << "QVariant::QVariant(QString)" << std::endl;
  }
};

class SafeString : public QString
{
public:
  operator QVariant() const {
    std::cout << "SafeString::operator QVariant()" << std::endl;
    return QVariant();
  }
};

class WrappingSafeString
{
public:
  WrappingSafeString() {}

  WrappingSafeString(const QString &) {
    std::cout << "WrappingSafeString::WrappingSafeString(QString)" << std::endl;
  }

  operator QVariant() const {
    std::cout << "WrappingSafeString::operator QVariant()" << std::endl;
    return QVariant();
  }

  operator QString() const {
    std::cout << "WrappingSafeString::operator QString()" << std::endl;
    return QString();
  }
};

class WrappingSubclassSafeString
{
public:
  WrappingSubclassSafeString() : m_isSafe(false), m_wrappedSubclass(this) {}

  WrappingSubclassSafeString(const QString &)  : m_isSafe(false), m_wrappedSubclass(this) {
    std::cout << "WrappingSubclassSafeString::WrappingSubclassSafeString(QString)" << std::endl;
  }

  bool isSafe() const { return m_isSafe; };
  void setSafe(bool safe) { m_isSafe = safe; }

  class Subclass : public QString
  {
    friend class WrappingSubclassSafeString;
    Subclass(WrappingSubclassSafeString *wsss) : m_wsss(wsss) {}
    WrappingSubclassSafeString *m_wsss;

  public:
    void operation2()
    {
      m_wsss->m_isSafe = false;
      std::cout << "overridden string operation 2 (wrapping)" << std::endl;
    }
  };

  Subclass m_wrappedSubclass;

  Subclass* operator->()
  {
    return &m_wrappedSubclass;
  }

  operator QVariant() const {
    std::cout << "WrappingSubclassSafeString::operator QVariant()" << std::endl;
    return QVariant();
  }

  operator QString() const {
    std::cout << "WrappingSubclassSafeString::operator QString()" << std::endl;
    return m_wrappedSubclass;
  }

private:
  bool m_isSafe;
};

class UsingSafeString : private QString
{
public:
  UsingSafeString() {}
  UsingSafeString(const QString &) {}

  void operation2() {
    std::cout << "overridden string operation 2 (using)" << std::endl;
  };

  using QString::operation1;

  operator QVariant()
  {
    return QVariant();
  }

  operator QString()
  {
    return QString();
  }
};

QVariant f1() {
  return QString();
}

QVariant f2() {
  return SafeString();
}

QVariant f3() {
  return WrappingSafeString();
}

QString f4() {
  return WrappingSafeString();
}

WrappingSafeString f5() {
  return QString();
}

QVariant f6() {
  return WrappingSubclassSafeString();
}

QString f7() {
  return WrappingSubclassSafeString();
}

WrappingSubclassSafeString f8() {
  return QString();
}

#ifdef BROKEN

QVariant f9() {
  return UsingSafeString();
}

QString f10() {
  return UsingSafeString();
}

UsingSafeString f11() {
  return QString();
}

#endif

int main(int argc, char ** argv)
{
  f1();
  f2();
  f3();
  f4();
  f5();
  f6();
  f7();
  f8();
#ifdef BROKEN
  f9();
  f10();
  f11();
#endif

  WrappingSubclassSafeString wsss;
  wsss.setSafe(true);
  wsss->operation1();
  std::cout << ( wsss.isSafe() ? "IsSafe" : "IsNotSafe" ) << std::endl;
  wsss->operation2();
  std::cout << ( wsss.isSafe() ? "IsSafe" : "IsNotSafe" ) << std::endl;

  return 0;
}