source: trunk/src/script/api/qscriptstring.cpp@ 1023

Last change on this file since 1023 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtScript module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL-ONLY$
10** GNU Lesser General Public License Usage
11** This file may be used under the terms of the GNU Lesser
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 [email protected].
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
30QT_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.
53
54 Call the toArrayIndex() function to convert a QScriptString to an
55 array index. This is useful when using QScriptClass to implement
56 array-like objects.
57*/
58
59/*!
60 Constructs an invalid QScriptString.
61*/
62QScriptString::QScriptString()
63 : d_ptr(0)
64{
65}
66
67/*!
68 Constructs a new QScriptString that is a copy of \a other.
69*/
70QScriptString::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*/
85QScriptString::~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 // Make sure the identifier is removed from the correct engine.
97 QScript::APIShim shim(d->engine);
98 d->identifier = JSC::Identifier();
99 d->engine->unregisterScriptString(d);
100 }
101 break;
102 }
103 }
104}
105
106/*!
107 Assigns the \a other value to this QScriptString.
108*/
109QScriptString &QScriptString::operator=(const QScriptString &other)
110{
111 if (d_func() && d_func()->engine && (d_func()->ref == 1) && (d_func()->type == QScriptStringPrivate::HeapAllocated)) {
112 // current d_ptr will be deleted at the assignment below, so unregister it first
113 d_func()->engine->unregisterScriptString(d_func());
114 }
115 d_ptr = other.d_ptr;
116 if (d_func() && (d_func()->type == QScriptStringPrivate::StackAllocated)) {
117 Q_ASSERT(d_func()->ref != 1);
118 d_ptr.detach();
119 d_func()->ref = 1;
120 d_func()->type = QScriptStringPrivate::HeapAllocated;
121 d_func()->engine->registerScriptString(d_func());
122 }
123 return *this;
124}
125
126/*!
127 Returns true if this QScriptString is valid; otherwise
128 returns false.
129*/
130bool QScriptString::isValid() const
131{
132 return QScriptStringPrivate::isValid(*this);
133}
134
135/*!
136 Returns true if this QScriptString is equal to \a other;
137 otherwise returns false.
138*/
139bool QScriptString::operator==(const QScriptString &other) const
140{
141 Q_D(const QScriptString);
142 if (!d || !other.d_func())
143 return d == other.d_func();
144 return d->identifier == other.d_func()->identifier;
145}
146
147/*!
148 Returns true if this QScriptString is not equal to \a other;
149 otherwise returns false.
150*/
151bool QScriptString::operator!=(const QScriptString &other) const
152{
153 return !operator==(other);
154}
155
156/*!
157 \since 4.6
158
159 Attempts to convert this QScriptString to a QtScript array index,
160 and returns the result.
161
162 If a conversion error occurs, *\a{ok} is set to false; otherwise
163 *\a{ok} is set to true.
164*/
165quint32 QScriptString::toArrayIndex(bool *ok) const
166{
167 Q_D(const QScriptString);
168 if (!d) {
169 if (ok)
170 *ok = false;
171 return -1;
172 }
173 bool tmp;
174 bool *okok = ok ? ok : &tmp;
175 quint32 result = d->identifier.toArrayIndex(okok);
176 if (!*okok)
177 result = -1;
178 return result;
179}
180
181/*!
182 Returns the string that this QScriptString represents, or a
183 null string if this QScriptString is not valid.
184
185 \sa isValid()
186*/
187QString QScriptString::toString() const
188{
189 Q_D(const QScriptString);
190 if (!d || !d->engine)
191 return QString();
192 return d->identifier.ustring();
193}
194
195/*!
196 Returns the string that this QScriptString represents, or a
197 null string if this QScriptString is not valid.
198
199 \sa toString()
200*/
201QScriptString::operator QString() const
202{
203 return toString();
204}
205
206uint qHash(const QScriptString &key)
207{
208 QScriptStringPrivate *d = QScriptStringPrivate::get(key);
209 if (!d)
210 return 0;
211 return qHash(d->identifier.ustring().rep());
212}
213
214QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.