0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
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 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "config.h" // compile on Windows
|
|
43 |
#include "qscriptstring.h"
|
|
44 |
#include "qscriptstring_p.h"
|
|
45 |
#include "qscriptengine.h"
|
|
46 |
#include "qscriptengine_p.h"
|
|
47 |
|
|
48 |
QT_BEGIN_NAMESPACE
|
|
49 |
|
|
50 |
/*!
|
|
51 |
\since 4.4
|
|
52 |
\class QScriptString
|
|
53 |
|
|
54 |
\brief The QScriptString class acts as a handle to "interned" strings in a QScriptEngine.
|
|
55 |
|
|
56 |
\ingroup script
|
|
57 |
|
|
58 |
|
|
59 |
QScriptString can be used to achieve faster (repeated)
|
|
60 |
property getting/setting, and comparison of property names, of
|
|
61 |
script objects.
|
|
62 |
|
|
63 |
To get a QScriptString representation of a string, pass the string
|
|
64 |
to QScriptEngine::toStringHandle(). The typical usage pattern is to
|
|
65 |
register one or more pre-defined strings when setting up your script
|
|
66 |
environment, then subsequently use the relevant QScriptString as
|
|
67 |
argument to e.g. QScriptValue::property().
|
|
68 |
|
|
69 |
Call the toString() function to obtain the string that a
|
|
70 |
QScriptString represents.
|
|
71 |
*/
|
|
72 |
|
|
73 |
/*!
|
|
74 |
Constructs an invalid QScriptString.
|
|
75 |
*/
|
|
76 |
QScriptString::QScriptString()
|
|
77 |
: d_ptr(0)
|
|
78 |
{
|
|
79 |
}
|
|
80 |
|
|
81 |
/*!
|
|
82 |
Constructs a new QScriptString that is a copy of \a other.
|
|
83 |
*/
|
|
84 |
QScriptString::QScriptString(const QScriptString &other)
|
|
85 |
: d_ptr(other.d_ptr)
|
|
86 |
{
|
|
87 |
if (d_func() && (d_func()->type == QScriptStringPrivate::StackAllocated)) {
|
|
88 |
Q_ASSERT(d_func()->ref != 1);
|
|
89 |
d_ptr.detach();
|
|
90 |
d_func()->ref = 1;
|
|
91 |
d_func()->type = QScriptStringPrivate::HeapAllocated;
|
|
92 |
d_func()->engine->registerScriptString(d_func());
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
/*!
|
|
97 |
Destroys this QScriptString.
|
|
98 |
*/
|
|
99 |
QScriptString::~QScriptString()
|
|
100 |
{
|
|
101 |
Q_D(QScriptString);
|
|
102 |
if (d) {
|
|
103 |
switch (d->type) {
|
|
104 |
case QScriptStringPrivate::StackAllocated:
|
|
105 |
Q_ASSERT(d->ref == 1);
|
|
106 |
d->ref.ref(); // avoid deletion
|
|
107 |
break;
|
|
108 |
case QScriptStringPrivate::HeapAllocated:
|
|
109 |
if (d->engine && (d->ref == 1))
|
|
110 |
d->engine->unregisterScriptString(d);
|
|
111 |
break;
|
|
112 |
}
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
/*!
|
|
117 |
Assigns the \a other value to this QScriptString.
|
|
118 |
*/
|
|
119 |
QScriptString &QScriptString::operator=(const QScriptString &other)
|
|
120 |
{
|
|
121 |
if (d_func() && d_func()->engine && (d_func()->ref == 1) && (d_func()->type == QScriptStringPrivate::HeapAllocated)) {
|
|
122 |
// current d_ptr will be deleted at the assignment below, so unregister it first
|
|
123 |
d_func()->engine->unregisterScriptString(d_func());
|
|
124 |
}
|
|
125 |
d_ptr = other.d_ptr;
|
|
126 |
if (d_func() && (d_func()->type == QScriptStringPrivate::StackAllocated)) {
|
|
127 |
Q_ASSERT(d_func()->ref != 1);
|
|
128 |
d_ptr.detach();
|
|
129 |
d_func()->ref = 1;
|
|
130 |
d_func()->type = QScriptStringPrivate::HeapAllocated;
|
|
131 |
d_func()->engine->registerScriptString(d_func());
|
|
132 |
}
|
|
133 |
return *this;
|
|
134 |
}
|
|
135 |
|
|
136 |
/*!
|
|
137 |
Returns true if this QScriptString is valid; otherwise
|
|
138 |
returns false.
|
|
139 |
*/
|
|
140 |
bool QScriptString::isValid() const
|
|
141 |
{
|
|
142 |
return QScriptStringPrivate::isValid(*this);
|
|
143 |
}
|
|
144 |
|
|
145 |
/*!
|
|
146 |
Returns true if this QScriptString is equal to \a other;
|
|
147 |
otherwise returns false.
|
|
148 |
*/
|
|
149 |
bool QScriptString::operator==(const QScriptString &other) const
|
|
150 |
{
|
|
151 |
Q_D(const QScriptString);
|
|
152 |
if (!d || !other.d_func())
|
|
153 |
return d == other.d_func();
|
|
154 |
return d->identifier == other.d_func()->identifier;
|
|
155 |
}
|
|
156 |
|
|
157 |
/*!
|
|
158 |
Returns true if this QScriptString is not equal to \a other;
|
|
159 |
otherwise returns false.
|
|
160 |
*/
|
|
161 |
bool QScriptString::operator!=(const QScriptString &other) const
|
|
162 |
{
|
|
163 |
return !operator==(other);
|
|
164 |
}
|
|
165 |
|
|
166 |
/*!
|
|
167 |
Returns the string that this QScriptString represents, or a
|
|
168 |
null string if this QScriptString is not valid.
|
|
169 |
|
|
170 |
\sa isValid()
|
|
171 |
*/
|
|
172 |
QString QScriptString::toString() const
|
|
173 |
{
|
|
174 |
Q_D(const QScriptString);
|
|
175 |
if (!d || !d->engine)
|
|
176 |
return QString();
|
|
177 |
return d->identifier.ustring();
|
|
178 |
}
|
|
179 |
|
|
180 |
/*!
|
|
181 |
Returns the string that this QScriptString represents, or a
|
|
182 |
null string if this QScriptString is not valid.
|
|
183 |
|
|
184 |
\sa toString()
|
|
185 |
*/
|
|
186 |
QScriptString::operator QString() const
|
|
187 |
{
|
|
188 |
return toString();
|
|
189 |
}
|
|
190 |
|
|
191 |
uint qHash(const QScriptString &key)
|
|
192 |
{
|
|
193 |
QScriptStringPrivate *d = QScriptStringPrivate::get(key);
|
|
194 |
if (!d)
|
|
195 |
return 0;
|
|
196 |
return qHash(d->identifier.ustring().rep());
|
|
197 |
}
|
|
198 |
|
|
199 |
QT_END_NAMESPACE
|