kjsembed
binding_support.h
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org> 00003 Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com> 00004 Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org> 00005 Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #ifndef BINDING_SUPPORT_H 00024 #define BINDING_SUPPORT_H 00025 00026 #include <QtCore/QDate> 00027 #include <QtCore/QStringList> 00028 00029 #include <kjsembed/kjseglobal.h> 00030 #include <kjsembed/pointer.h> 00031 #include <kjs/object.h> 00032 #include <kdemacros.h> 00033 00034 #define KJS_BINDING( NAME ) \ 00035 class NAME \ 00036 { \ 00037 public: \ 00038 static const KJSEmbed::Method p_methods[]; \ 00039 static const KJSEmbed::Method p_statics[]; \ 00040 static const KJSEmbed::Enumerator p_enums[]; \ 00041 static const KJSEmbed::Constructor p_constructor; \ 00042 static KJS::JSObject *ctorMethod( KJS::ExecState *exec, const KJS::List &args );\ 00043 static const KJSEmbed::Enumerator *enums() { return p_enums;} \ 00044 static const KJSEmbed::Method *methods() { return p_methods;} \ 00045 static const KJSEmbed::Method *statics() { return p_statics;} \ 00046 static const KJSEmbed::Constructor *constructor() { return &p_constructor;} \ 00047 static const KJS::JSObject *construct(KJS::ExecState *exec, const KJS::List &args)\ 00048 { return (*p_constructor.construct)(exec,args); } \ 00049 }; 00050 00051 #define KJSO_BINDING( NAME, TYPE, BASENAME ) \ 00052 class KJSEMBED_EXPORT NAME : public BASENAME \ 00053 { \ 00054 public: \ 00055 NAME(KJS::ExecState *exec, TYPE * obj); \ 00056 static const KJSEmbed::Method p_methods[]; \ 00057 static const KJSEmbed::Method p_statics[]; \ 00058 static const KJSEmbed::Enumerator p_enums[]; \ 00059 static const KJSEmbed::Constructor p_constructor; \ 00060 static KJS::JSObject *bindMethod( KJS::ExecState *exec, PointerBase& ptrObj );\ 00061 static KJS::JSObject *ctorMethod( KJS::ExecState *exec, const KJS::List &args );\ 00062 static const KJSEmbed::Enumerator *enums() { return p_enums;} \ 00063 static const KJSEmbed::Method *methods() { return p_methods;} \ 00064 static const KJSEmbed::Method *statics() { return p_statics;} \ 00065 static const KJSEmbed::Constructor *constructor() { return &p_constructor;} \ 00066 }; 00067 00068 00069 #define KJSO_BINDING_NOEXP( NAME, TYPE, BASENAME ) \ 00070 class NAME : public BASENAME \ 00071 { \ 00072 public: \ 00073 NAME(KJS::ExecState *exec, TYPE * obj); \ 00074 static const KJSEmbed::Method p_methods[]; \ 00075 static const KJSEmbed::Method p_statics[]; \ 00076 static const KJSEmbed::Enumerator p_enums[]; \ 00077 static const KJSEmbed::Constructor p_constructor; \ 00078 static KJS::JSObject *bindMethod( KJS::ExecState *exec, PointerBase& ptrObj );\ 00079 static KJS::JSObject *ctorMethod( KJS::ExecState *exec, const KJS::List &args );\ 00080 static const KJSEmbed::Enumerator *enums() { return p_enums;} \ 00081 static const KJSEmbed::Method *methods() { return p_methods;} \ 00082 static const KJSEmbed::Method *statics() { return p_statics;} \ 00083 static const KJSEmbed::Constructor *constructor() { return &p_constructor;} \ 00084 }; 00085 00086 #define KJSO_VALUE_BINDING( NAME, TYPE, BASENAME ) \ 00087 class KJSEMBED_EXPORT NAME : public BASENAME \ 00088 { \ 00089 public: \ 00090 NAME(KJS::ExecState *exec, const TYPE & val ); \ 00091 NAME(KJS::ExecState *exec, const char *typeName ); \ 00092 static const KJSEmbed::Method p_methods[]; \ 00093 static const KJSEmbed::Method p_statics[]; \ 00094 static const KJSEmbed::Enumerator p_enums[]; \ 00095 static const KJSEmbed::Constructor p_constructor; \ 00096 static KJS::JSObject *bindMethod( KJS::ExecState *exec, PointerBase& ptrObj );\ 00097 static KJS::JSObject *ctorMethod( KJS::ExecState *exec, const KJS::List &args );\ 00098 static const KJSEmbed::Enumerator *enums() { return p_enums;} \ 00099 static const KJSEmbed::Method *methods() { return p_methods;} \ 00100 static const KJSEmbed::Method *statics() { return p_statics;} \ 00101 static const KJSEmbed::Constructor *constructor() { return &p_constructor;} \ 00102 }; 00103 00104 #define KJSO_START_BINDING_CTOR( NAME, TYPE, BASENAME ) \ 00105 NAME::NAME(KJS::ExecState *exec, TYPE * obj) \ 00106 : BASENAME( exec, obj ) \ 00107 { \ 00108 StaticBinding::publish( exec, this, NAME::methods() ); 00109 00110 #define KJSO_END_BINDING_CTOR \ 00111 } 00112 00113 #define KJSO_SIMPLE_BINDING_CTOR( NAME, TYPE, BASENAME ) \ 00114 NAME::NAME(KJS::ExecState *exec, TYPE * obj) \ 00115 : BASENAME( exec, obj ) \ 00116 { \ 00117 StaticBinding::publish( exec, this, NAME::methods() ); \ 00118 } 00119 00120 #define KJSV_SIMPLE_BINDING_CTOR( NAME, JSNAME, TYPE, BASENAME ) \ 00121 NAME::NAME(KJS::ExecState *exec, const TYPE & value) \ 00122 : BASENAME( exec, #JSNAME , value ) \ 00123 { \ 00124 StaticBinding::publish( exec, this, NAME::methods() ); \ 00125 } 00126 00127 #define START_METHOD_LUT( TYPE ) \ 00128 const Method TYPE::p_methods[] = \ 00129 { 00130 00131 #define START_STATIC_METHOD_LUT( TYPE ) \ 00132 const Method TYPE::p_statics[] = \ 00133 { 00134 00135 #define END_METHOD_LUT \ 00136 ,{0, 0, 0, 0 }\ 00137 }; 00138 00139 #define START_ENUM_LUT( TYPE) \ 00140 const Enumerator TYPE::p_enums[] =\ 00141 { 00142 00143 #define END_ENUM_LUT \ 00144 ,{0, 0 }\ 00145 }; 00146 00147 #define NO_ENUMS( TYPE ) \ 00148 const Enumerator TYPE::p_enums[] = {{0, 0 }}; 00149 00150 #define NO_METHODS( TYPE )\ 00151 const Method TYPE::p_methods[] = { {0, 0, 0, 0 } }; 00152 00153 #define NO_STATICS( TYPE )\ 00154 const Method TYPE::p_statics[] = { {0, 0, 0, 0 } }; 00155 00156 00157 #define START_CTOR( TYPE, JSNAME, ARGS )\ 00158 const Constructor TYPE::p_constructor = \ 00159 { \ 00160 #JSNAME, ARGS, KJS::DontDelete|KJS::ReadOnly, 0, &TYPE::ctorMethod, p_statics, p_enums, p_methods };\ 00161 KJS::JSObject *TYPE::ctorMethod( KJS::ExecState *exec, const KJS::List &args )\ 00162 {\ 00163 Q_UNUSED(exec);\ 00164 Q_UNUSED(args); 00165 00166 #define END_CTOR \ 00167 } 00168 00169 #define KJSO_START_CTOR( TYPE, JSNAME, ARGS )\ 00170 const Constructor TYPE::p_constructor = \ 00171 { \ 00172 #JSNAME, ARGS, KJS::DontDelete|KJS::ReadOnly, &TYPE::bindMethod, &TYPE::ctorMethod, p_statics, p_enums, p_methods };\ 00173 KJS::JSObject *TYPE::ctorMethod( KJS::ExecState *exec, const KJS::List &args )\ 00174 {\ 00175 Q_UNUSED(exec);\ 00176 Q_UNUSED(args); 00177 00178 #define KJSO_END_CTOR \ 00179 } 00180 00181 00182 #define KJSO_START_BIND( NAME, TYPE )\ 00183 KJS::JSObject *NAME::bindMethod( KJS::ExecState *exec, PointerBase& ptrObj )\ 00184 {\ 00185 Q_UNUSED(exec);\ 00186 Q_UNUSED(ptrObj); \ 00187 00188 #define KJSO_END_BIND \ 00189 } 00190 00191 #define KJSO_QOBJECT_START_BIND( NAME, TYPE )\ 00192 KJS::JSObject *NAME::bindMethod( KJS::ExecState *exec, PointerBase& ptrObj )\ 00193 {\ 00194 Q_UNUSED(exec);\ 00195 QObject* qobj = pointer_cast<QObject>(&ptrObj); \ 00196 if (! qobj ) \ 00197 return 0; \ 00198 TYPE* object = qobject_cast<TYPE*>(qobj); \ 00199 if (! object ) \ 00200 return 0; \ 00201 00202 #define KJSO_QOBJECT_END_BIND \ 00203 } 00204 00205 #define KJSO_QOBJECT_BIND( NAME, TYPE )\ 00206 KJS::JSObject *NAME::bindMethod( KJS::ExecState *exec, PointerBase& ptrObj )\ 00207 {\ 00208 Q_UNUSED(exec);\ 00209 QObject* qobj = pointer_cast<QObject>(&ptrObj); \ 00210 if (! qobj ) \ 00211 return 0; \ 00212 TYPE* object = qobject_cast<TYPE*>(qobj); \ 00213 if (! object ) \ 00214 return 0; \ 00215 return new NAME(exec, object); \ 00216 } 00217 00218 #define KJSO_VALUE_START_BIND( NAME, TYPE )\ 00219 KJS::JSObject *NAME::bindMethod( KJS::ExecState *exec, PointerBase& ptrObj )\ 00220 {\ 00221 Q_UNUSED(exec);\ 00222 TYPE* object = pointer_cast<TYPE>(&ptrObj); \ 00223 if (! object ) \ 00224 return 0; \ 00225 00226 #define KJSO_VALUE_END_BIND \ 00227 } 00228 00229 #define KJSO_VALUE_BIND( NAME, TYPE )\ 00230 KJS::JSObject *NAME::bindMethod( KJS::ExecState *exec, PointerBase& ptrObj )\ 00231 {\ 00232 Q_UNUSED(exec);\ 00233 TYPE* object = pointer_cast<TYPE>(&ptrObj); \ 00234 if (! object ) \ 00235 return 0; \ 00236 return new NAME(exec, *object); \ 00237 } 00238 00239 namespace KJS 00240 { 00241 inline JSObject* throwError(ExecState* e, ErrorType t, const QString& m) 00242 { return throwError(e, t, KJSEmbed::toUString(m)); } 00243 } 00244 00245 namespace KJSEmbed 00246 { 00247 class KJSEMBED_EXPORT ProxyBinding : public KJS::JSObject 00248 { 00249 public: 00250 ProxyBinding( KJS::ExecState *exec ); 00251 virtual ~ProxyBinding() {} 00252 00253 bool implementsCall() const { return true; } 00254 bool implementsConstruct() const { return true; } 00255 }; 00256 00267 template <typename T> 00268 T * extractBindingImp( KJS::ExecState *exec, KJS::JSValue *val) 00269 { 00270 return dynamic_cast<T*>(val->toObject(exec)); 00271 } 00272 00276 template <typename T> 00277 T * extractBindingImp( KJS::JSObject *obj ) 00278 { 00279 return dynamic_cast<T*>( obj ); 00280 } 00281 00289 typedef KJS::JSValue *(*callMethod)(KJS::ExecState*, KJS::JSObject *, const KJS::List&); 00290 00294 struct KJSEMBED_EXPORT Method 00295 { 00299 const char *name; 00303 const int argc; 00307 const int flags; 00311 const callMethod call; 00312 }; 00313 00317 struct KJSEMBED_EXPORT Enumerator 00318 { 00322 const char *name; 00326 const unsigned int value; 00327 }; 00328 00335 typedef KJS::JSObject *(*callBind)(KJS::ExecState*, PointerBase&); 00336 00343 typedef KJS::JSObject *(*callConstructor)(KJS::ExecState*, const KJS::List&); 00344 00345 struct KJSEMBED_EXPORT Constructor 00346 { 00351 const char *name; 00355 const int argc; 00359 const int flags; 00363 const callBind bind; 00367 const callConstructor construct; 00371 const Method *staticMethods; 00375 const Enumerator *enumerators; 00379 const Method *methods; 00380 }; 00381 00386 QString KJSEMBED_EXPORT extractQString( KJS::ExecState *exec, const KJS::List &args, int idx, const QString defaultValue = QString() ); 00387 00391 QString KJSEMBED_EXPORT extractQString( KJS::ExecState *exec, KJS::JSValue *value, const QString defaultValue = QString() ); 00392 00396 KJS::JSValue* createQString( KJS::ExecState *exec, const QString &value ); 00397 00402 QByteArray KJSEMBED_EXPORT extractQByteArray( KJS::ExecState *exec, const KJS::List &args, int idx, const QByteArray &defaultValue = QByteArray() ); 00403 00407 QByteArray KJSEMBED_EXPORT extractQByteArray( KJS::ExecState *exec, KJS::JSValue *value, const QByteArray &defaultValue = QByteArray() ); 00408 00412 KJS::JSValue* createQByteArray( KJS::ExecState *exec, const QByteArray &value ); 00413 00414 00415 template<typename T> 00416 inline T KJSEMBED_EXPORT extractString(KJS::ExecState *exec, KJS::JSValue *value, T defaultValue = T()) 00417 { 00418 if (!value || !value->isString()) 00419 return defaultValue; 00420 00421 return (T)(value->toString(exec).ascii()); 00422 } 00423 00424 template<typename T> 00425 inline T KJSEMBED_EXPORT extractString(KJS::ExecState *exec, const KJS::List &args, int idx, T defaultValue = T()) 00426 { 00427 if (args.size() >= idx) 00428 return extractString<T>(exec, args[idx], defaultValue); 00429 else 00430 return defaultValue; 00431 } 00432 00433 00437 template<typename T> 00438 inline T KJSEMBED_EXPORT extractNumber(KJS::ExecState *exec, KJS::JSValue *value, T defaultValue = T(0)) 00439 { 00440 if (!value || !value->isNumber()) 00441 return defaultValue; 00442 00443 return static_cast<T>(value->toNumber(exec)); 00444 } 00445 00450 template<typename T> 00451 inline T KJSEMBED_EXPORT extractNumber(KJS::ExecState *exec, const KJS::List &args, int idx, T defaultValue = T(0)) 00452 { 00453 if( args.size() >= idx ) 00454 { 00455 return extractNumber<T>( exec, args[idx], defaultValue ); 00456 } 00457 else 00458 return defaultValue; 00459 } 00460 00464 template<typename T> 00465 inline T KJSEMBED_EXPORT extractInteger(KJS::ExecState *exec, KJS::JSValue *value, T defaultValue) 00466 { 00467 if (!value || !value->isNumber()) 00468 return defaultValue; 00469 00470 // deal with MSVC annoyances 00471 #if COMPILER(MSVC) || __GNUC__ == 3 00472 return static_cast<T>(static_cast<int>(value->toInteger(exec))); 00473 #else 00474 return static_cast<T>(value->toInteger(exec)); 00475 #endif 00476 } 00477 00478 // extractInteger specialization 00479 template<> 00480 inline qint32 KJSEMBED_EXPORT extractInteger<qint32>(KJS::ExecState *exec, KJS::JSValue *value, qint32 defaultValue) 00481 { 00482 if (!value || !value->isNumber()) 00483 return defaultValue; 00484 00485 return static_cast<qint32>(value->toInt32(exec)); 00486 } 00487 00488 // extractInteger specialization 00489 template<> 00490 inline quint32 KJSEMBED_EXPORT extractInteger<quint32>(KJS::ExecState *exec, KJS::JSValue *value, quint32 defaultValue) 00491 { 00492 if (!value || !value->isNumber()) 00493 return defaultValue; 00494 00495 return static_cast<quint32>(value->toUInt32(exec)); 00496 } 00497 00498 // extractInteger specialization 00499 template<> 00500 inline quint16 KJSEMBED_EXPORT extractInteger<quint16>(KJS::ExecState *exec, KJS::JSValue *value, quint16 defaultValue) 00501 { 00502 if (!value || !value->isNumber()) 00503 return defaultValue; 00504 00505 return static_cast<quint16>(value->toUInt16(exec)); 00506 } 00507 00512 template<typename T> 00513 inline T KJSEMBED_EXPORT extractInteger(KJS::ExecState *exec, const KJS::List &args, int idx, T defaultValue = T(0)) 00514 { 00515 if( args.size() >= idx ) 00516 { 00517 return extractInteger<T>( exec, args[idx], defaultValue ); 00518 } 00519 else 00520 return defaultValue; 00521 } 00522 00527 int KJSEMBED_EXPORT extractInt( KJS::ExecState *exec, const KJS::List &args, int idx, int defaultValue = 0 ); 00531 int KJSEMBED_EXPORT extractInt( KJS::ExecState *exec, KJS::JSValue *value, int defaultValue = 0 ); 00535 KJS::JSValue* createInt( KJS::ExecState *exec, int value ); 00536 00541 double KJSEMBED_EXPORT extractDouble( KJS::ExecState *exec, const KJS::List &args, int idx, double defaultValue = 0 ); 00545 double KJSEMBED_EXPORT extractDouble( KJS::ExecState *exec, KJS::JSValue *value, double defaultValue = 0 ); 00549 KJS::JSValue* createDouble( KJS::ExecState *exec, double value ); 00550 00555 float KJSEMBED_EXPORT extractFloat( KJS::ExecState *exec, const KJS::List &args, int idx, float defaultValue = 0 ); 00559 float KJSEMBED_EXPORT extractFloat( KJS::ExecState *exec, KJS::JSValue *value, float defaultValue = 0 ); 00563 KJS::JSValue* createFloat( KJS::ExecState *exec, float value ); 00564 00569 bool KJSEMBED_EXPORT extractBool( KJS::ExecState *exec, const KJS::List &args, int idx, bool defaultValue = false ); 00573 bool extractBool( KJS::ExecState *exec, KJS::JSValue *value, bool defaultValue = false ); 00577 KJS::JSValue* createBool( KJS::ExecState *exec, bool value ); 00578 00583 QDateTime KJSEMBED_EXPORT extractQDateTime( KJS::ExecState *exec, const KJS::List &args, int idx, const QDateTime &defaultValue = QDateTime() ); 00587 QDateTime KJSEMBED_EXPORT extractQDateTime( KJS::ExecState *exec, KJS::JSValue *value, const QDateTime &defaultValue = QDateTime() ); 00591 KJS::JSValue* createQDateTime( KJS::ExecState *exec, const QDateTime &value ); 00592 00597 QDate KJSEMBED_EXPORT extractQDate( KJS::ExecState *exec, const KJS::List &args, int idx, const QDate &defaultValue = QDate() ); 00601 QDate KJSEMBED_EXPORT extractQDate( KJS::ExecState *exec, KJS::JSValue *value, const QDate &defaultValue = QDate() ); 00605 KJS::JSValue* createQDate( KJS::ExecState *exec, const QDate &value ); 00606 00611 QTime KJSEMBED_EXPORT extractQTime( KJS::ExecState *exec, const KJS::List &args, int idx, const QTime &defaultValue = QTime() ); 00615 QTime KJSEMBED_EXPORT extractQTime( KJS::ExecState *exec, KJS::JSValue *value, const QTime &defaultValue = QTime() ); 00619 KJS::JSValue* createQTime( KJS::ExecState *exec, const QTime &value ); 00620 00625 QStringList KJSEMBED_EXPORT extractQStringList( KJS::ExecState *exec, const KJS::List &args, int idx, const QStringList &defaultValue = QStringList() ); 00629 QStringList KJSEMBED_EXPORT extractQStringList( KJS::ExecState *exec, KJS::JSValue *value, const QStringList &defaultValue = QStringList() ); 00633 KJS::JSValue* createQStringList( KJS::ExecState *exec, const QStringList &value ); 00634 00635 } 00636 00637 #endif 00638 //kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle;
KDE 4.6 API Reference