author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 12:15:23 +0300 | |
branch | RCL_3 |
changeset 12 | cc75c76972ee |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtScript module of the Qt Toolkit. |
|
8 |
** |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:LGPL-ONLY$ |
0 | 10 |
** GNU Lesser General Public License Usage |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
11 |
** This file may be used under the terms of the GNU Lesser |
0 | 12 |
** General Public License version 2.1 as published by the Free Software |
13 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
14 |
** packaging of this file. Please review the following information to |
|
15 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
16 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
17 |
** |
|
18 |
** If you have questions regarding the use of this file, please contact |
|
19 |
** Nokia at qt-info@nokia.com. |
|
20 |
** $QT_END_LICENSE$ |
|
21 |
** |
|
22 |
****************************************************************************/ |
|
23 |
||
24 |
#include "config.h" // compile on Windows |
|
25 |
#include "qscriptstring.h" |
|
26 |
#include "qscriptstring_p.h" |
|
27 |
#include "qscriptengine.h" |
|
28 |
#include "qscriptengine_p.h" |
|
29 |
||
30 |
QT_BEGIN_NAMESPACE |
|
31 |
||
32 |
/*! |
|
33 |
\since 4.4 |
|
34 |
\class QScriptString |
|
35 |
||
36 |
\brief The QScriptString class acts as a handle to "interned" strings in a QScriptEngine. |
|
37 |
||
38 |
\ingroup script |
|
39 |
||
40 |
||
41 |
QScriptString can be used to achieve faster (repeated) |
|
42 |
property getting/setting, and comparison of property names, of |
|
43 |
script objects. |
|
44 |
||
45 |
To get a QScriptString representation of a string, pass the string |
|
46 |
to QScriptEngine::toStringHandle(). The typical usage pattern is to |
|
47 |
register one or more pre-defined strings when setting up your script |
|
48 |
environment, then subsequently use the relevant QScriptString as |
|
49 |
argument to e.g. QScriptValue::property(). |
|
50 |
||
51 |
Call the toString() function to obtain the string that a |
|
52 |
QScriptString represents. |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
53 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
54 |
Call the toArrayIndex() function to convert a QScriptString to an |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
55 |
array index. This is useful when using QScriptClass to implement |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
56 |
array-like objects. |
0 | 57 |
*/ |
58 |
||
59 |
/*! |
|
60 |
Constructs an invalid QScriptString. |
|
61 |
*/ |
|
62 |
QScriptString::QScriptString() |
|
63 |
: d_ptr(0) |
|
64 |
{ |
|
65 |
} |
|
66 |
||
67 |
/*! |
|
68 |
Constructs a new QScriptString that is a copy of \a other. |
|
69 |
*/ |
|
70 |
QScriptString::QScriptString(const QScriptString &other) |
|
71 |
: d_ptr(other.d_ptr) |
|
72 |
{ |
|
73 |
if (d_func() && (d_func()->type == QScriptStringPrivate::StackAllocated)) { |
|
74 |
Q_ASSERT(d_func()->ref != 1); |
|
75 |
d_ptr.detach(); |
|
76 |
d_func()->ref = 1; |
|
77 |
d_func()->type = QScriptStringPrivate::HeapAllocated; |
|
78 |
d_func()->engine->registerScriptString(d_func()); |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
/*! |
|
83 |
Destroys this QScriptString. |
|
84 |
*/ |
|
85 |
QScriptString::~QScriptString() |
|
86 |
{ |
|
87 |
Q_D(QScriptString); |
|
88 |
if (d) { |
|
89 |
switch (d->type) { |
|
90 |
case QScriptStringPrivate::StackAllocated: |
|
91 |
Q_ASSERT(d->ref == 1); |
|
92 |
d->ref.ref(); // avoid deletion |
|
93 |
break; |
|
94 |
case QScriptStringPrivate::HeapAllocated: |
|
95 |
if (d->engine && (d->ref == 1)) |
|
96 |
d->engine->unregisterScriptString(d); |
|
97 |
break; |
|
98 |
} |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
/*! |
|
103 |
Assigns the \a other value to this QScriptString. |
|
104 |
*/ |
|
105 |
QScriptString &QScriptString::operator=(const QScriptString &other) |
|
106 |
{ |
|
107 |
if (d_func() && d_func()->engine && (d_func()->ref == 1) && (d_func()->type == QScriptStringPrivate::HeapAllocated)) { |
|
108 |
// current d_ptr will be deleted at the assignment below, so unregister it first |
|
109 |
d_func()->engine->unregisterScriptString(d_func()); |
|
110 |
} |
|
111 |
d_ptr = other.d_ptr; |
|
112 |
if (d_func() && (d_func()->type == QScriptStringPrivate::StackAllocated)) { |
|
113 |
Q_ASSERT(d_func()->ref != 1); |
|
114 |
d_ptr.detach(); |
|
115 |
d_func()->ref = 1; |
|
116 |
d_func()->type = QScriptStringPrivate::HeapAllocated; |
|
117 |
d_func()->engine->registerScriptString(d_func()); |
|
118 |
} |
|
119 |
return *this; |
|
120 |
} |
|
121 |
||
122 |
/*! |
|
123 |
Returns true if this QScriptString is valid; otherwise |
|
124 |
returns false. |
|
125 |
*/ |
|
126 |
bool QScriptString::isValid() const |
|
127 |
{ |
|
128 |
return QScriptStringPrivate::isValid(*this); |
|
129 |
} |
|
130 |
||
131 |
/*! |
|
132 |
Returns true if this QScriptString is equal to \a other; |
|
133 |
otherwise returns false. |
|
134 |
*/ |
|
135 |
bool QScriptString::operator==(const QScriptString &other) const |
|
136 |
{ |
|
137 |
Q_D(const QScriptString); |
|
138 |
if (!d || !other.d_func()) |
|
139 |
return d == other.d_func(); |
|
140 |
return d->identifier == other.d_func()->identifier; |
|
141 |
} |
|
142 |
||
143 |
/*! |
|
144 |
Returns true if this QScriptString is not equal to \a other; |
|
145 |
otherwise returns false. |
|
146 |
*/ |
|
147 |
bool QScriptString::operator!=(const QScriptString &other) const |
|
148 |
{ |
|
149 |
return !operator==(other); |
|
150 |
} |
|
151 |
||
152 |
/*! |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
153 |
\since 4.6 |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
154 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
Attempts to convert this QScriptString to a QtScript array index, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
and returns the result. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
If a conversion error occurs, *\a{ok} is set to false; otherwise |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
*\a{ok} is set to true. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
*/ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
quint32 QScriptString::toArrayIndex(bool *ok) const |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
162 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
163 |
Q_D(const QScriptString); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
164 |
if (!d) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
165 |
if (ok) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
*ok = false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
return -1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
169 |
bool tmp; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
170 |
bool *okok = ok ? ok : &tmp; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
171 |
quint32 result = d->identifier.toArrayIndex(okok); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
172 |
if (!*okok) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
173 |
result = -1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
174 |
return result; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
176 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
177 |
/*! |
0 | 178 |
Returns the string that this QScriptString represents, or a |
179 |
null string if this QScriptString is not valid. |
|
180 |
||
181 |
\sa isValid() |
|
182 |
*/ |
|
183 |
QString QScriptString::toString() const |
|
184 |
{ |
|
185 |
Q_D(const QScriptString); |
|
186 |
if (!d || !d->engine) |
|
187 |
return QString(); |
|
188 |
return d->identifier.ustring(); |
|
189 |
} |
|
190 |
||
191 |
/*! |
|
192 |
Returns the string that this QScriptString represents, or a |
|
193 |
null string if this QScriptString is not valid. |
|
194 |
||
195 |
\sa toString() |
|
196 |
*/ |
|
197 |
QScriptString::operator QString() const |
|
198 |
{ |
|
199 |
return toString(); |
|
200 |
} |
|
201 |
||
202 |
uint qHash(const QScriptString &key) |
|
203 |
{ |
|
204 |
QScriptStringPrivate *d = QScriptStringPrivate::get(key); |
|
205 |
if (!d) |
|
206 |
return 0; |
|
207 |
return qHash(d->identifier.ustring().rep()); |
|
208 |
} |
|
209 |
||
210 |
QT_END_NAMESPACE |