Qt 4.8
qscriptstring.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtScript module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL-ONLY$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser
11 ** General Public License version 2.1 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.LGPL included in the
13 ** packaging of this file. Please review the following information to
14 ** ensure the GNU Lesser General Public License version 2.1 requirements
15 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** If you have questions regarding the use of this file, please contact
18 ** us via http://www.qt-project.org/.
19 **
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 
31 
66  : d_ptr(0)
67 {
68 }
69 
74  : d_ptr(other.d_ptr)
75 {
76  if (d_func() && (d_func()->type == QScriptStringPrivate::StackAllocated)) {
77  Q_ASSERT(d_func()->ref != 1);
78  d_ptr.detach();
79  d_func()->ref = 1;
80  d_func()->type = QScriptStringPrivate::HeapAllocated;
81  d_func()->engine->registerScriptString(d_func());
82  }
83 }
84 
89 {
91  if (d) {
92  switch (d->type) {
94  Q_ASSERT(d->ref == 1);
95  d->ref.ref(); // avoid deletion
96  break;
98  if (d->engine && (d->ref == 1)) {
99  // Make sure the identifier is removed from the correct engine.
100  QScript::APIShim shim(d->engine);
101  d->identifier = JSC::Identifier();
102  d->engine->unregisterScriptString(d);
103  }
104  break;
105  }
106  }
107 }
108 
113 {
114  if (d_func() && d_func()->engine && (d_func()->ref == 1) && (d_func()->type == QScriptStringPrivate::HeapAllocated)) {
115  // current d_ptr will be deleted at the assignment below, so unregister it first
116  d_func()->engine->unregisterScriptString(d_func());
117  }
118  d_ptr = other.d_ptr;
119  if (d_func() && (d_func()->type == QScriptStringPrivate::StackAllocated)) {
120  Q_ASSERT(d_func()->ref != 1);
121  d_ptr.detach();
122  d_func()->ref = 1;
123  d_func()->type = QScriptStringPrivate::HeapAllocated;
124  d_func()->engine->registerScriptString(d_func());
125  }
126  return *this;
127 }
128 
134 {
135  return QScriptStringPrivate::isValid(*this);
136 }
137 
142 bool QScriptString::operator==(const QScriptString &other) const
143 {
144  Q_D(const QScriptString);
145  if (!d || !other.d_func())
146  return d == other.d_func();
147  return d->identifier == other.d_func()->identifier;
148 }
149 
154 bool QScriptString::operator!=(const QScriptString &other) const
155 {
156  return !operator==(other);
157 }
158 
169 {
170  Q_D(const QScriptString);
171  if (!d) {
172  if (ok)
173  *ok = false;
174  return -1;
175  }
176  bool tmp;
177  bool *okok = ok ? ok : &tmp;
178  quint32 result = d->identifier.toArrayIndex(okok);
179  if (!*okok)
180  result = -1;
181  return result;
182 }
183 
191 {
192  Q_D(const QScriptString);
193  if (!d || !d->engine)
194  return QString();
195  return d->identifier.ustring();
196 }
197 
204 QScriptString::operator QString() const
205 {
206  return toString();
207 }
208 
210 {
212  if (!d)
213  return 0;
214  return qHash(d->identifier.ustring().rep());
215 }
216 
bool operator!=(const QScriptString &other) const
Returns true if this QScriptString is not equal to other; otherwise returns false.
double d
Definition: qnumeric_p.h:62
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QScriptString & operator=(const QScriptString &other)
Assigns the other value to this QScriptString.
~QScriptString()
Destroys this QScriptString.
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
The QScriptString class acts as a handle to "interned" strings in a QScriptEngine.
Definition: qscriptstring.h:38
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QScriptString()
Constructs an invalid QScriptString.
bool operator==(const QScriptString &other) const
Returns true if this QScriptString is equal to other; otherwise returns false.
unsigned int uint
Definition: qglobal.h:996
JSC::Identifier identifier
QExplicitlySharedDataPointer< QScriptStringPrivate > d_ptr
Definition: qscriptstring.h:58
static bool isValid(const QScriptString &q)
bool isValid() const
Returns true if this QScriptString is valid; otherwise returns false.
int key
unsigned int quint32
Definition: qglobal.h:938
void detach()
If the shared data object&#39;s reference count is greater than 1, this function creates a deep copy of t...
Definition: qshareddata.h:148
static QScriptStringPrivate * get(const QScriptString &q)
quint32 toArrayIndex(bool *ok=0) const
QString toString() const
Returns the string that this QScriptString represents, or a null string if this QScriptString is not ...
uint qHash(const QScriptString &key)