Eneboo - Documentación para desarrolladores
|
00001 /**************************************************************************** 00002 ** $Id: qsobject.h 1.1.5 edited 2006-02-23T15:39:57$ 00003 ** 00004 ** Copyright (C) 2001-2006 Trolltech AS. All rights reserved. 00005 ** 00006 ** This file is part of the Qt Script for Applications framework (QSA). 00007 ** 00008 ** This file may be distributed and/or modified under the terms of the 00009 ** GNU General Public License version 2 as published by the Free Software 00010 ** Foundation and appearing in the file LICENSE.GPL included in the 00011 ** packaging of this file. 00012 ** 00013 ** Licensees holding a valid Qt Script for Applications license may use 00014 ** this file in accordance with the Qt Script for Applications License 00015 ** Agreement provided with the Software. 00016 ** 00017 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00018 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00019 ** 00020 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for 00021 ** information about QSA Commercial License Agreements. 00022 ** See http://www.trolltech.com/gpl/ for GPL licensing information. 00023 ** 00024 ** Contact info@trolltech.com if any conditions of this licensing are 00025 ** not clear to you. 00026 ** 00027 *****************************************************************************/ 00028 00029 #ifndef QSOBJECT_H 00030 #define QSOBJECT_H 00031 00032 #include "qsglobal.h" 00033 #include "../kernel/dlldefs.h" 00034 #include <qstring.h> 00035 #include <qvariant.h> 00036 00037 class QSList; 00038 class QSClass; 00039 class QSBooleanClass; 00040 class QSForInNode; 00041 class QSEngine; 00042 class QSEnv; 00043 00047 enum ErrorType { NoError = 0, 00048 GeneralError, 00049 EvalError, 00050 RangeError, 00051 ReferenceError, 00052 SyntaxError, 00053 TypeError, 00054 URIError, 00055 ThrowError 00056 }; 00057 00058 enum ValueType { 00059 TypeValue, 00060 TypeShared, 00061 TypeClass, 00062 TypeOther 00063 }; 00064 00065 // Reggie: This is a really ugly hack to get QSA link on windows 00066 double QUICKCORE_EXPORT NaN(); 00067 double QUICKCORE_EXPORT Inf(); 00068 #define NaN NaN() 00069 #define Inf Inf() 00070 00071 // forward declarations 00072 class Imp; 00073 class QSShared; 00074 struct QSProperty; 00075 typedef QMap<QString, QSProperty> QSPropertyMap; 00076 class PropList; 00077 class QSMember; 00078 00079 union QUICKCORE_EXPORT Value { 00080 int i; 00081 double d; 00082 bool b; 00083 QStringData *str; 00084 QSShared *sh; 00085 QSClass *cl; 00086 void *other; 00087 }; 00088 00094 class QSElementNode; 00095 class QSObject; 00096 00097 struct QSFakeQString { 00098 QStringData *d; 00099 }; 00100 inline QStringData *qsStringData(const QString &s) 00101 { 00102 return ((QSFakeQString *)&s)->d; 00103 } 00104 00105 class QUICKCORE_EXPORT QSObject 00106 { 00107 friend class QSStringClass; 00108 friend class QSEnv; 00109 public: 00113 QSObject(); 00114 QSObject(const QSClass *c); 00115 QSObject(const QSClass *c, QSShared *s); 00119 QSObject(const QSObject &); 00120 00121 /* 00122 * Assignment operator 00123 */ 00124 QSObject &operator=(const QSObject &); 00125 00126 bool operator==(const QSObject &a) const; 00127 00131 ~QSObject(); 00132 bool isValid() const { 00133 return !!clss; 00134 } 00135 void invalidate() { 00136 clss = 0; 00137 } 00141 bool isDefined() const; 00145 QString typeName() const; 00146 00147 ValueType valueType() const; 00148 00151 inline const QSClass *objectType() const; 00152 void setType(QSClass *c); 00153 00154 bool isA(const QSClass *c) const; 00160 bool isA(const char *s) const; 00161 00162 bool isObject() const; 00163 bool isNull() const; 00164 bool isUndefined() const; 00165 bool isNumber() const; 00166 bool isString() const; 00167 bool isBoolean() const; 00168 bool isFunction() const; 00169 bool isPrimitive() const; 00170 00171 bool equals(const QSObject &other) const; 00172 bool strictEquals(const QSObject &other) const; 00173 QSCompareResult compareTo(const QSObject &other) const; 00174 00179 QSEnv *env() const; 00185 // bool derivedFrom(const char *s) const; 00186 00192 QSObject toPrimitive(const QSClass *preferred = 0) const; // ECMA 9.1 00196 bool toBoolean() const; // ECMA 9.2 00200 double toNumber() const; // ECMA 9.3 00201 QVariant toVariant(QVariant::Type t) const; 00205 double round() const; 00209 int toInteger() const; // ECMA 9.4 00213 int toInt32() const; // ECMA 9.5 00217 unsigned int toUInt32() const; // ECMA 9.6 00221 unsigned short toUInt16() const; // ECMA 9.7 00225 QString toString() const; // ECMA 9.8 00226 00227 /* 00228 Gets a qualified name in this object. E.g. if name is 00229 a.b.c, c is returned based on this.a.b.c 00230 */ 00231 QSObject getQualified(const QString &name) const; 00232 00233 // Properties 00238 QSObject get(const QString &p) const; 00245 bool hasProperty(const QString &p) const; 00246 void put(const QString &p, const QSObject &v); 00254 bool deleteProperty(const QString &p); 00259 void put(const QString &p, double d); 00264 void put(const QString &p, int i); 00269 void put(const QString &p, unsigned int u); 00270 bool isExecutable() const; 00279 QSObject executeCall(QSObject *thisV, 00280 const QSList *args); 00281 00282 QSObject execute(const QSList &args); 00283 00284 QSObject invoke(const QSMember &mem, const QSList &args); 00285 00286 QSObject fetchValue(const QSMember &mem); 00287 void write(const QSMember &mem, const QSObject &val); 00288 00289 void mark(); 00290 00291 const QSClass *resolveMember(const QString &name, QSMember *mem, const QSClass *owner = 0, int *offset = 0) const; 00292 00293 #if QS_DEBUG_MEM == 1 00294 00297 static int count; 00298 #endif 00299 int iVal() const { 00300 return val.i; 00301 } 00302 void setVal(int v) { 00303 val.i = v; 00304 } 00305 00306 double dVal() const { 00307 return val.d; 00308 } 00309 void setVal(double v) { 00310 val.d = v; 00311 } 00312 00313 bool bVal() const { 00314 return val.b; 00315 } 00316 void setVal(bool v) { 00317 val.b = v; 00318 } 00319 00320 QString sVal() const; 00321 void setVal(const QString &v) { 00322 val.str = qsStringData(v); 00323 val.str->ref(); 00324 } 00325 00326 void *oVal() const { 00327 return val.other; 00328 } 00329 void setVal(void *v) { 00330 val.other = v; 00331 } 00332 00333 QSShared *shVal() const { 00334 return val.sh; 00335 } 00336 void setVal(QSShared *s); 00337 00338 QString debugString() const; 00339 protected: 00340 const QSClass *clss; 00341 00342 private: 00343 #if QS_DEBUG_MEM == 1 00344 void checkShared() const; 00345 #endif 00346 00347 Value val; 00348 }; // end of QSObject 00349 00350 inline QString QSObject::sVal() const 00351 { 00352 return *(QString *)(&val.str); 00353 } 00354 00355 inline void QSObject::setVal(QSShared *s) 00356 { 00357 val.sh = s; 00358 #if QS_DEBUG_MEM == 1 00359 if (s) 00360 checkShared(); 00361 #endif 00362 } 00363 00364 class QUICKCORE_EXPORT QSShared : public QShared 00365 { 00366 friend class QSEnv; 00367 public: 00368 QSShared() : next(0), prev(0) { } 00369 virtual ~QSShared() { } 00370 virtual void invalidate() { } 00371 bool isConnected() const { 00372 return next != 0 || prev != 0; 00373 } 00374 00375 private: 00376 QSShared *next; 00377 QSShared *prev; 00378 }; 00379 00380 class QUICKCORE_EXPORT QSWritable : public QSShared 00381 { 00382 public: 00383 QSWritable(); 00384 ~QSWritable(); 00385 00386 void setProperty(const QString &n, const QSProperty &p); 00387 QSProperty *reference(const QString &n) const; 00388 bool hasProperty(const QString &n) const; 00389 QSPropertyMap *properties() const { 00390 return props; 00391 } 00392 00393 void invalidate(); 00394 00395 private: 00396 QSPropertyMap *props; 00397 }; 00398 00399 struct QSProperty { 00400 QSProperty() { } 00401 QSProperty(const QSObject &o) : object(o) { } 00402 void invalidate() { 00403 object.invalidate(); 00404 } 00405 QSObject object; 00406 }; 00407 00408 class QSEngineImp; 00422 class QUICKCORE_EXPORT Global : public QSObject 00423 { 00424 friend class QSEngineImp; 00425 public: 00426 private: 00427 Global(QSEngine *e); 00428 void init(); 00429 void clear(); 00430 QSEngine *eng; 00431 }; 00432 00433 inline const QSClass *QSObject::objectType() const 00434 { 00435 Q_ASSERT(clss); 00436 return clss; 00437 } 00438 00439 #endif