Qt 4.8
Public Functions | Properties | Friends | List of all members
QScriptString Class Reference

The QScriptString class acts as a handle to "interned" strings in a QScriptEngine. More...

#include <qscriptstring.h>

Public Functions

bool isValid () const
 Returns true if this QScriptString is valid; otherwise returns false. More...
 
 operator QString () const
 Returns the string that this QScriptString represents, or a null string if this QScriptString is not valid. More...
 
bool operator!= (const QScriptString &other) const
 Returns true if this QScriptString is not equal to other; otherwise returns false. More...
 
QScriptStringoperator= (const QScriptString &other)
 Assigns the other value to this QScriptString. More...
 
bool operator== (const QScriptString &other) const
 Returns true if this QScriptString is equal to other; otherwise returns false. More...
 
 QScriptString ()
 Constructs an invalid QScriptString. More...
 
 QScriptString (const QScriptString &other)
 Constructs a new QScriptString that is a copy of other. More...
 
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 valid. More...
 
 ~QScriptString ()
 Destroys this QScriptString. More...
 

Properties

QExplicitlySharedDataPointer< QScriptStringPrivated_ptr
 

Friends

class QScriptValue
 

Detailed Description

The QScriptString class acts as a handle to "interned" strings in a QScriptEngine.

Since
4.4

QScriptString can be used to achieve faster (repeated) property getting/setting, and comparison of property names, of script objects.

To get a QScriptString representation of a string, pass the string to QScriptEngine::toStringHandle(). The typical usage pattern is to register one or more pre-defined strings when setting up your script environment, then subsequently use the relevant QScriptString as argument to e.g. QScriptValue::property().

Call the toString() function to obtain the string that a QScriptString represents.

Call the toArrayIndex() function to convert a QScriptString to an array index. This is useful when using QScriptClass to implement array-like objects.

Definition at line 38 of file qscriptstring.h.

Constructors and Destructors

◆ QScriptString() [1/2]

QScriptString::QScriptString ( )

Constructs an invalid QScriptString.

Definition at line 65 of file qscriptstring.cpp.

66  : d_ptr(0)
67 {
68 }
QExplicitlySharedDataPointer< QScriptStringPrivate > d_ptr
Definition: qscriptstring.h:58

◆ QScriptString() [2/2]

QScriptString::QScriptString ( const QScriptString other)

Constructs a new QScriptString that is a copy of other.

Definition at line 73 of file qscriptstring.cpp.

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 }
int type
Definition: qmetatype.cpp:239
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QExplicitlySharedDataPointer< QScriptStringPrivate > d_ptr
Definition: qscriptstring.h:58
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

◆ ~QScriptString()

QScriptString::~QScriptString ( )

Destroys this QScriptString.

Definition at line 88 of file qscriptstring.cpp.

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 }
double d
Definition: qnumeric_p.h:62
#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

Functions

◆ isValid()

bool QScriptString::isValid ( ) const

Returns true if this QScriptString is valid; otherwise returns false.

Definition at line 133 of file qscriptstring.cpp.

134 {
135  return QScriptStringPrivate::isValid(*this);
136 }
static bool isValid(const QScriptString &q)

◆ operator QString()

QScriptString::operator QString ( ) const

Returns the string that this QScriptString represents, or a null string if this QScriptString is not valid.

See also
toString()

Definition at line 204 of file qscriptstring.cpp.

205 {
206  return toString();
207 }
QString toString() const
Returns the string that this QScriptString represents, or a null string if this QScriptString is not ...

◆ operator!=()

bool QScriptString::operator!= ( const QScriptString other) const

Returns true if this QScriptString is not equal to other; otherwise returns false.

Definition at line 154 of file qscriptstring.cpp.

155 {
156  return !operator==(other);
157 }
bool operator==(const QScriptString &other) const
Returns true if this QScriptString is equal to other; otherwise returns false.

◆ operator=()

QScriptString & QScriptString::operator= ( const QScriptString other)

Assigns the other value to this QScriptString.

Definition at line 112 of file qscriptstring.cpp.

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 }
int type
Definition: qmetatype.cpp:239
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QExplicitlySharedDataPointer< QScriptStringPrivate > d_ptr
Definition: qscriptstring.h:58
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

◆ operator==()

bool QScriptString::operator== ( const QScriptString other) const

Returns true if this QScriptString is equal to other; otherwise returns false.

Definition at line 142 of file qscriptstring.cpp.

Referenced by operator!=().

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 }
double d
Definition: qnumeric_p.h:62
#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

◆ toArrayIndex()

quint32 QScriptString::toArrayIndex ( bool *  ok = 0) const
Since
4.6

Attempts to convert this QScriptString to a QtScript array index, and returns the result.

If a conversion error occurs, *{ok} is set to false; otherwise {ok} is set to true.

Definition at line 168 of file qscriptstring.cpp.

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 }
double d
Definition: qnumeric_p.h:62
#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
unsigned int quint32
Definition: qglobal.h:938

◆ toString()

QString QScriptString::toString ( ) const

Returns the string that this QScriptString represents, or a null string if this QScriptString is not valid.

See also
isValid()

Definition at line 190 of file qscriptstring.cpp.

Referenced by QScript::ClassObjectDelegate::getOwnPropertyNames(), operator QString(), NamedNodeMapClass::queryProperty(), NodeListClass::queryProperty(), QDeclarativeGlobalScriptClass::setProperty(), and QScriptValue::setProperty().

191 {
192  Q_D(const QScriptString);
193  if (!d || !d->engine)
194  return QString();
195  return d->identifier.ustring();
196 }
double d
Definition: qnumeric_p.h:62
The QString class provides a Unicode character string.
Definition: qstring.h:83
#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

Friends and Related Functions

◆ QScriptValue

friend class QScriptValue
friend

Definition at line 59 of file qscriptstring.h.

Properties

◆ d_ptr

QExplicitlySharedDataPointer<QScriptStringPrivate> QScriptString::d_ptr
private

The documentation for this class was generated from the following files: