Eneboo - Documentación para desarrolladores
|
00001 /**************************************************************************** 00002 ** $Id: qsclass.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 QSCLASS_H 00030 #define QSCLASS_H 00031 00032 #include "qsvalues.h" 00033 #include "qstypes.h" 00034 #include "qsenv.h" 00035 #include "qsmember.h" 00036 #include "qsobjectlist.h" 00037 00038 #if defined(Q_TEMPLATEDLL) && (defined(Q_CC_MSVC_NET) || defined(Q_CC_MSVC)) 00039 Q_TEMPLATE_EXTERN template class QUICKCORE_EXPORT QValueList<int>; 00040 Q_TEMPLATE_EXTERN template class QUICKCORE_EXPORT QValueList<bool>; 00041 Q_TEMPLATE_EXTERN template class QUICKCORE_EXPORT QMap<QString, QString>; 00042 #endif 00043 00044 class QSClassClass; 00045 class QSClassDefNode; 00046 class QSInstanceData; 00047 class QSFunctionBodyNode; 00048 class QSNode; 00049 class QSNodeList; 00050 00053 class QUICKCORE_EXPORT QSClass 00054 { 00055 friend class QSObject; 00056 public: 00057 QSClass(QSEnv *e, int a = AttributeNone); 00058 QSClass(QSClass *b, int a = AttributeNone); 00059 virtual ~QSClass(); 00060 virtual void clear(); 00061 00062 QSEnv *env() const { 00063 return en; 00064 } 00065 QSClass *base() const { 00066 return bclass; 00067 } 00068 void setBase(QSClass *base) { 00069 bclass = base; 00070 } 00071 virtual QString name() const = 0; 00072 virtual QString identifier() const { 00073 return name(); 00074 } 00075 QSClassClass *asClass() const; 00076 bool inherits(const QSClass *c) const; 00077 00078 QSClass *enclosingClass() const { 00079 return encClass; 00080 } 00081 void setEnclosingClass(QSClass *cl) { 00082 encClass = cl; 00083 } 00084 00085 bool isFinal() const { 00086 return attrs & AttributeFinal; 00087 } 00088 bool isExecutable() const { 00089 return attrs & AttributeExecutable; 00090 } 00091 bool isAbstract() const { 00092 return attrs & AttributeAbstract; 00093 } 00094 00095 virtual QSCompareResult compare(const QSObject &a, const QSObject &b) const; 00096 00097 virtual void ref(QSObject *o) const; 00098 virtual void deref(QSObject *o) const; 00099 virtual void mark(QSObject *o) const; 00100 00101 virtual bool toBoolean(const QSObject *obj) const; 00102 virtual double toNumber(const QSObject *obj) const; 00103 virtual QString toString(const QSObject *obj) const; 00104 virtual QSObject toPrimitive(const QSObject *obj, 00105 const QSClass *preferred = 0) const; 00106 virtual QVariant toVariant(const QSObject *obj, QVariant::Type pref) const; 00107 00108 virtual QString debugString(const QSObject *obj) const; 00109 00110 bool hasProperty(const QSObject *obj, const QString &prop) const; 00111 QSObject get(const QSObject *obj, const QString &prop) const; 00112 void put(QSObject *obj, const QString &prop, const QSObject &v) const; 00113 00114 virtual bool deleteProperty(QSObject *obj, const QSMember &mem) const; 00115 virtual bool member(const QSObject *o, const QString &n, 00116 QSMember *m) const; 00117 virtual QSObject fetchValue(const QSObject *objPtr, 00118 const QSMember &mem) const; 00119 virtual void write(QSObject *objPtr, const QSMember &mem, 00120 const QSObject &val) const; 00121 void write(QSObject *objPtr, int index, const QSObject &val) const; 00122 00123 void addFunctionMember(const QString &n, QSFunctionBodyNode *f, 00124 int attributes); 00125 int addVariableMember(const QString &n, int attributes); 00126 void addStaticVariableMember(const QString &n, const QSObject &v, 00127 int attr); 00128 void addMember(const QString &n, const QSMember &m); 00129 virtual void addMember(const QString &n, const QSMember &m, 00130 const QSObject &stVal); 00131 virtual bool deleteMember(const QString &n); 00132 void replaceMember(const QString &n, QSMember *m); 00133 virtual void replaceMember(const QString &n, QSMember *m, 00134 const QSObject &stVal); 00135 void setNumVariables(int count) { 00136 numVars = count; 00137 } 00138 int numVariables() const { 00139 return numVars; 00140 } 00141 int numStaticVariables() const { 00142 return numStaticVars; 00143 } 00144 00145 virtual QSObject construct(const QSList &args) const; 00146 virtual QSObject execute(const QSObject *o, QSObject *thisValue, const QSList &args) const; 00147 virtual QSObject cast(const QSList &args) const; 00148 00149 virtual QSMemberMap *definedMembers() const { 00150 return mmap; 00151 } 00152 virtual QSMemberMap members(const QSObject *obj) const; 00153 00154 void setStaticMember(int idx, const QSObject &val); 00155 QSObject staticMember(int idx) const; 00156 00157 int attributes() const { 00158 return attrs; 00159 } 00160 00161 void throwError(ErrorType e, const QString &msg) const; 00162 00163 QSObject createString(const QString &s) const; 00164 QSObject createNumber(double d) const; 00165 QSObject createBoolean(bool b) const; 00166 QSObject createUndefined() const; 00167 QSObject createNull() const; 00168 00169 virtual ValueType valueType() const { 00170 return TypeOther; 00171 } 00172 00173 virtual void finalize(); 00174 00175 protected: 00176 virtual QSEqualsResult isEqual(const QSObject &a, const QSObject &b) const; 00177 virtual QSEqualsResult isStrictEqual(const QSObject &a, 00178 const QSObject &b) const; 00179 virtual QSObject invoke(QSObject *objPtr, const QSMember &mem) const; 00180 00181 private: 00182 void init(); 00183 void removeStaticVar(const QSMember &old); 00184 void fillMemberVarIndex(QSMember *member); 00185 00186 private: 00187 QSEnv *en; 00188 QSClass *bclass; 00189 QSClass *encClass; 00190 int attrs; 00191 QSMemberMap *mmap; 00192 QSObjectList staticMembers; 00193 QValueList<int> replacedVars; 00194 int numVars; 00195 int numStaticVars; 00196 }; 00197 00201 class QUICKCORE_EXPORT QSObjectClass : public QSClass 00202 { 00203 public: 00204 QSObjectClass(QSEnv *e); 00205 QSObjectClass(QSClass *c) : QSClass(c) { } 00206 void init(); 00207 QString name() const { 00208 return QString::fromLatin1("Object"); 00209 } 00210 00211 bool toBoolean(const QSObject *) const { 00212 return TRUE; // #### 00213 } 00214 double toNumber(const QSObject *) const { 00215 return NaN; // #### 00216 } 00217 QString toString(const QSObject *obj) const; 00218 00219 QSObject construct() const; 00220 QSObject construct(const QSList &args) const; 00221 virtual QSObject cast(const QSList &args) const; 00222 00223 static QSObject toStringScript(QSEnv *env); 00224 static QSObject valueOf(QSEnv *env); 00225 }; 00226 00227 class QUICKCORE_EXPORT QSUndefinedClass : public QSClass 00228 { 00229 public: 00230 QSUndefinedClass(QSClass *b) : QSClass(b) { } 00231 QString name() const { 00232 return QString::fromLatin1("Undefined"); 00233 } 00234 00235 bool toBoolean(const QSObject *obj) const; 00236 double toNumber(const QSObject *obj) const; 00237 QString toString(const QSObject *) const; 00238 QSObject toPrimitive(const QSObject *obj, 00239 const QSClass *preferred = 0) const; 00240 00241 protected: 00242 virtual QSEqualsResult isEqual(const QSObject &a, const QSObject &b) const; 00243 }; 00244 00245 class QUICKCORE_EXPORT QSNullClass : public QSClass 00246 { 00247 public: 00248 QSNullClass(QSClass *b) : QSClass(b) { } 00249 QString name() const { 00250 return QString::fromLatin1("Null"); 00251 } 00252 00253 bool toBoolean(const QSObject *obj) const; 00254 double toNumber(const QSObject *obj) const; 00255 QString toString(const QSObject *) const; 00256 QSObject toPrimitive(const QSObject *obj, 00257 const QSClass *preferred = 0) const; 00258 00259 protected: 00260 virtual QSEqualsResult isEqual(const QSObject &a, const QSObject &b) const; 00261 }; 00262 00263 class QUICKCORE_EXPORT QSBooleanClass : public QSClass 00264 { 00265 public: 00266 QSBooleanClass(QSClass *b); 00267 void init(); 00268 QString name() const { 00269 return QString::fromLatin1("Boolean"); 00270 } 00271 00272 bool toBoolean(const QSObject *obj) const; 00273 double toNumber(const QSObject *obj) const; 00274 QString toString(const QSObject *) const; 00275 QSObject toPrimitive(const QSObject *obj, 00276 const QSClass *preferred = 0) const; 00277 QVariant toVariant(const QSObject *obj, QVariant::Type) const; 00278 00279 QSObject construct(bool b) const; 00280 QSObject construct(const QSList &args) const; 00281 virtual QSObject cast(const QSList &args) const; 00282 00283 virtual ValueType valueType() const { 00284 return TypeValue; 00285 } 00286 00287 protected: 00288 virtual QSEqualsResult isEqual(const QSObject &a, const QSObject &b) const; 00289 }; 00290 00291 class QUICKCORE_EXPORT QSNumberClass : public QSClass 00292 { 00293 friend class QSBooleanClass; 00294 public: 00295 QSNumberClass(QSClass *b); 00296 void init(); 00297 QString name() const { 00298 return QString::fromLatin1("Number"); 00299 } 00300 00301 bool toBoolean(const QSObject *obj) const; 00302 double toNumber(const QSObject *obj) const; 00303 QString toString(const QSObject *) const; 00304 QSObject toPrimitive(const QSObject *obj, 00305 const QSClass *preferred = 0) const; 00306 QVariant toVariant(const QSObject *obj, QVariant::Type) const; 00307 00308 QSObject construct(const QSList &args) const; 00309 virtual QSObject cast(const QSList &args) const; 00310 00311 static QSObject toStringScript(QSEnv *); 00312 static QSObject valueOf(QSEnv *); 00313 00314 virtual ValueType valueType() const { 00315 return TypeValue; 00316 } 00317 00318 protected: 00319 virtual QSEqualsResult isEqual(const QSObject &a, const QSObject &b) const; 00320 }; 00321 00322 class QUICKCORE_EXPORT QSStringClass : public QSClass 00323 { 00324 public: 00325 QSStringClass(QSClass *b); 00326 void init(); 00327 QString name() const { 00328 return QString::fromLatin1("String"); 00329 } 00330 virtual void ref(QSObject *o) const; 00331 virtual void deref(QSObject *o) const; 00332 00333 virtual QSObject fetchValue(const QSObject *objPtr, 00334 const QSMember &mem) const; 00335 00336 bool toBoolean(const QSObject *obj) const; 00337 double toNumber(const QSObject *obj) const; 00338 QString toString(const QSObject *) const; 00339 QSObject toPrimitive(const QSObject *obj, 00340 const QSClass *preferred = 0) const; 00341 QVariant toVariant(const QSObject *obj, QVariant::Type) const; 00342 00343 QSObject construct(const QSList &args) const; 00344 virtual QSObject cast(const QSList &args) const; 00345 00346 virtual ValueType valueType() const { 00347 return TypeValue; 00348 } 00349 00350 static QSObject fromCharCode(QSEnv *); 00351 00352 static QSObject toStringScript(QSEnv *); 00353 static QSObject charAt(QSEnv *); 00354 static QSObject charCodeAt(QSEnv *); 00355 static QSObject indexOf(QSEnv *); 00356 static QSObject lastIndexOf(QSEnv *); 00357 static QSObject match(QSEnv *); 00358 static QSObject replace(QSEnv *); 00359 static QSObject split(QSEnv *); 00360 static QSObject substr(QSEnv *); 00361 static QSObject toLowerCase(QSEnv *); 00362 static QSObject toUpperCase(QSEnv *); 00363 // QString API 00364 static QSObject isEmpty(QSEnv *); 00365 static QSObject left(QSEnv *); 00366 static QSObject mid(QSEnv *); 00367 static QSObject right(QSEnv *); 00368 static QSObject find(QSEnv *); 00369 static QSObject findRev(QSEnv *); 00370 static QSObject startsWith(QSEnv *); 00371 static QSObject endsWith(QSEnv *); 00372 00373 static QSObject argInt(QSEnv *); 00374 static QSObject argDec(QSEnv *); 00375 static QSObject argStr(QSEnv *); 00376 static QSObject arg(QSEnv *); 00377 00378 protected: 00379 virtual QSEqualsResult isEqual(const QSObject &a, const QSObject &b) const; 00380 }; 00381 00382 class QUICKCORE_EXPORT QSCharacterClass : public QSClass 00383 { 00384 public: 00385 QSCharacterClass(QSClass *b) : QSClass(b) { } 00386 QString name() const { 00387 return QString::fromLatin1("Character"); 00388 } 00389 00390 bool toBoolean(const QSObject *obj) const; 00391 double toNumber(const QSObject *obj) const; 00392 QString toString(const QSObject *) const; 00393 00394 virtual ValueType valueType() const { 00395 return TypeValue; 00396 } 00397 }; 00398 00399 class QSSharedClass : public QSClass 00400 { 00401 public: 00402 QSSharedClass(QSEnv *e, int attr = AttributeNone) 00403 : QSClass(e, attr) { } 00404 QSSharedClass(QSClass *b, int attr = AttributeNone) 00405 : QSClass(b, attr) { } 00406 00407 virtual void ref(QSObject *o) const; 00408 virtual void deref(QSObject *o) const; 00409 00410 virtual ValueType valueType() const { 00411 return TypeShared; 00412 } 00413 }; 00414 00415 class QSClassClass : public QSSharedClass 00416 { 00417 public: 00418 QSClassClass(QSClass *b, int a, const QString &n); 00419 ~QSClassClass(); 00420 QString name() const { 00421 return QString::fromLatin1("Class"); 00422 } 00423 QString identifier() const { 00424 return cname; 00425 } 00426 00427 bool toBoolean(const QSObject *obj) const; 00428 double toNumber(const QSObject *obj) const; 00429 QString toString(const QSObject *) const; 00430 00431 QSObject construct(const QSList &args) const; 00432 00433 virtual QSEqualsResult isEqual(const QSObject &a, const QSObject &b) const; 00434 00435 static QSInstanceData *data(QSObject *obj); 00436 static const QSInstanceData *data(const QSObject *obj); 00437 00438 bool hasDefaultConstructor() const { 00439 return defaultCtor; 00440 } 00441 void setHasDefaultConstructor(bool d) { 00442 defaultCtor = d; 00443 } 00444 00445 void addMemberInitializer(QSNode *node); 00446 void addStaticInitializer(QSNode *node); 00447 void setClassBodyNode(QSFunctionBodyNode *node); 00448 QSFunctionBodyNode *classBodyNode() const { 00449 return bodyNode; 00450 } 00451 00452 void setClassDefNode(QSClassDefNode *node) { 00453 clDefNode = node; 00454 } 00455 QSClassDefNode *classDefNode() const { 00456 return clDefNode; 00457 } 00458 00459 void executeBlock(QSEnv *env); 00460 00461 private: 00462 int initVariables(QSInstanceData *data) const; 00463 00464 private: 00465 QString cname; 00466 uint defaultCtor: 1; 00467 QSNodeList *memberInit; 00468 QSNodeList *staticInit; 00469 QSFunctionBodyNode *bodyNode; 00470 QSClassDefNode *clDefNode; 00471 }; 00472 00473 class QUICKCORE_EXPORT QSWritableClass : public QSSharedClass 00474 { 00475 public: 00476 QSWritableClass(QSClass *b, int a = AttributeNone) : QSSharedClass(b, a) { } 00477 void mark(QSObject *o) const; 00478 virtual bool member(const QSObject *o, const QString &n, 00479 QSMember *m) const; 00480 virtual QSObject fetchValue(const QSObject *objPtr, 00481 const QSMember &mem) const; 00482 virtual void write(QSObject *objPtr, const QSMember &mem, 00483 const QSObject &val) const; 00484 00485 virtual bool deleteProperty(QSObject *obj, const QSMember &mem) const; 00486 00487 virtual QSMemberMap members(const QSObject *obj) const; 00488 00489 virtual QSEqualsResult isEqual(const QSObject &a, const QSObject &b) const; 00490 00491 QSObject createWritable() const; 00492 00493 QSPropertyMap *properties(const QSObject *obj) const; 00494 protected: 00495 static QSWritable *data(QSObject *obj); 00496 static const QSWritable *data(const QSObject *obj); 00497 virtual QSObject invoke(QSObject *objPtr, const QSMember &mem) const; 00498 }; 00499 00500 class QSFuncDeclNode; 00501 00502 class QUICKCORE_EXPORT QSFunctionScopeClass : public QSWritableClass 00503 { 00504 public: 00505 QSFunctionScopeClass(QSClass *b, QSFuncDeclNode *func); 00506 00507 QSFunctionScopeClass(QSClass *b) 00508 : QSWritableClass(b), numArgs(0), body_node(0) { } 00509 00510 virtual QString name() const { 00511 return QString::fromLatin1("FunctionScope"); 00512 } 00513 virtual QString identifier() const; 00514 virtual QSObject construct(const QSList &) const; 00515 virtual void clear(); 00516 void setNumArguments(int num) { 00517 numArgs = num; 00518 } 00519 int numArguments() const { 00520 return numArgs; 00521 } 00522 00523 void setFunctionBodyNode(QSFunctionBodyNode *bn) { 00524 body_node = bn; 00525 } 00526 QSFunctionBodyNode *functionBodyNode() const { 00527 return body_node; 00528 } 00529 00530 private: 00531 QString ident; 00532 int numArgs; 00533 QSFunctionBodyNode *body_node; 00534 }; 00535 00536 class QUICKCORE_EXPORT QSEvalScopeClass: public QSWritableClass 00537 { 00538 public: 00539 QSEvalScopeClass(QSClass *b) : QSWritableClass(b) { } 00540 virtual QString name() const { 00541 return QString::fromLatin1("EvalScope"); 00542 } 00543 virtual QSObject construct(const QSList &) const; 00544 }; 00545 00546 class QUICKCORE_EXPORT QSBlockScopeClass: public QSSharedClass 00547 { 00548 public: 00549 QSBlockScopeClass(QSEnv *e) : QSSharedClass(e) { } 00550 void activateScope() const; 00551 void deactivateScope() const; 00552 virtual QString name() const { 00553 return QString::fromLatin1("BlockScope"); 00554 } 00555 virtual QSMemberMap members(const QSObject *obj) const; 00556 }; 00557 00558 // doesn't add much value. just a concrete class for anonymous 00559 // objects. 00560 class QUICKCORE_EXPORT QSDynamicClass : public QSWritableClass 00561 { 00562 public: 00563 QSDynamicClass(QSClass *b) : QSWritableClass(b) { } 00564 QString name() const { 00565 return QString::fromLatin1("Object"); 00566 } 00567 }; 00568 00569 class QUICKCORE_EXPORT QSTypeClass : public QSSharedClass 00570 { 00571 public: 00572 QSTypeClass(QSClass *b) : QSSharedClass(b, AttributeExecutable) { } 00573 QString name() const { 00574 return QString::fromLatin1("Type"); 00575 } 00576 00577 virtual bool member(const QSObject *o, const QString &n, 00578 QSMember *m) const; 00579 virtual QSMemberMap members(const QSObject *obj) const; 00580 QSMemberMap allMembers(const QSObject *obj) const; 00581 00582 virtual QSObject fetchValue(const QSObject *o, 00583 const QSMember &mem) const; 00584 virtual void write(QSObject *objPtr, const QSMember &mem, 00585 const QSObject &val) const; 00586 00587 virtual QSEqualsResult isEqual(const QSObject &a, const QSObject &b) const; 00588 virtual ValueType valueType() const { 00589 return TypeClass; 00590 } 00591 00592 static QSClass *classValue(const QSObject *obj); 00593 00594 QSObject createType(QSClass *cl) const; 00595 QSShared *createTypeShared(QSClass *cl) const; 00596 00597 protected: 00598 virtual QSObject invoke(QSObject *objPtr, const QSMember &mem) const; 00599 00600 }; 00601 00602 class QUICKCORE_EXPORT QSGlobalClass : public QSWritableClass 00603 { 00604 public: 00605 QSGlobalClass(QSClass *b); 00606 QString name() const { 00607 return QString::fromLatin1("Global"); // invisible to the user 00608 } 00609 }; 00610 00611 class QUICKCORE_EXPORT QSDebugClass : public QSClass 00612 { 00613 public: 00614 QSDebugClass(QSClass *base); 00615 00616 QString name() const { 00617 return QString::fromLatin1("Debug"); 00618 } 00619 00620 static void dumpObject(QSEnv *env); 00621 static void dumpScope(QSEnv *env); 00622 static void dumpType(QSEnv *env); 00623 }; 00624 00625 class QUICKCORE_EXPORT QSSystemClass : public QSClass 00626 { 00627 public: 00628 QSSystemClass(QSClass *base); 00629 00630 QString name() const { 00631 return QString::fromLatin1("System"); 00632 } 00633 00634 static void print(QSEnv *env); 00635 static void println(QSEnv *env); 00636 static QSObject getenv(QSEnv *env); 00637 static void setenv(QSEnv *env); 00638 }; 00639 00640 class QUICKCORE_EXPORT QSAbstractBaseClass : public QSClass 00641 { 00642 public: 00643 QSAbstractBaseClass(QSClass *base, const QString &id) 00644 : QSClass(base), ident(id) { }; 00645 00646 QString identifier() const { 00647 return ident; 00648 } 00649 QString name() const { 00650 return QString::fromLatin1("AbstractBase"); 00651 } 00652 00653 void replace(QSClassClass *classDef); 00654 00655 private: 00656 QString ident; 00657 }; 00658 00659 class QSInstanceData : public QSWritable 00660 { 00661 public: 00662 QSInstanceData(int count, const QSObject &def); 00663 ~QSInstanceData() { 00664 delete [] vals; 00665 } 00666 00667 int size() const { 00668 return sz; 00669 } 00670 void resize(int count, const QSObject &def); 00671 void ensureSize(int count, const QSObject &def); 00672 00673 QSObject *value(int index) const { 00674 Q_ASSERT(index >= 0 && index < sz); 00675 return &vals[index]; 00676 } 00677 00678 void setValue(int index, const QSObject &value) { 00679 Q_ASSERT(index >= 0 && index < sz); 00680 vals[index] = value; 00681 } 00682 00683 virtual void invalidate(); 00684 00685 private: 00686 // disabled 00687 QSInstanceData(const QSInstanceData &); 00688 QSInstanceData &operator=(const QSInstanceData &); 00689 00690 int sz; 00691 QSObject *vals; 00692 }; 00693 00694 inline void QSClass::replaceMember(const QString &n, QSMember *m) 00695 { 00696 replaceMember(n, m, createUndefined()); 00697 } 00698 00699 inline void QSClass::addMember(const QString &n, const QSMember &m) 00700 { 00701 addMember(n, m, createUndefined()); 00702 } 00703 00704 00705 00706 #endif