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

Last change on this file since 636 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 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 d->engine->unregisterScriptString(d);
97 break;
98 }
99 }
100}
101
102/*!
103 Assigns the \a other value to this QScriptString.
104*/
105QScriptString &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*/
126bool 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*/
135bool 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*/
147bool QScriptString::operator!=(const QScriptString &other) const
148{
149 return !operator==(other);
150}
151
152/*!
153 \since 4.6
154
155 Attempts to convert this QScriptString to a QtScript array index,
156 and returns the result.
157
158 If a conversion error occurs, *\a{ok} is set to false; otherwise
159 *\a{ok} is set to true.
160*/
161quint32 QScriptString::toArrayIndex(bool *ok) const
162{
163 Q_D(const QScriptString);
164 if (!d) {
165 if (ok)
166 *ok = false;
167 return -1;
168 }
169 bool tmp;
170 bool *okok = ok ? ok : &tmp;
171 quint32 result = d->identifier.toArrayIndex(okok);
172 if (!*okok)
173 result = -1;
174 return result;
175}
176
177/*!
178 Returns the string that this QScriptString represents, or a
179 null string if this QScriptString is not valid.
180
181 \sa isValid()
182*/
183QString 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*/
197QScriptString::operator QString() const
198{
199 return toString();
200}
201
202uint 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
210QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.