kjsembed
variant_binding.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 Copyright (C) 2007, 2008 Sebastian Sauer <mail@dipe.org> 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Library General Public 00010 License as published by the Free Software Foundation; either 00011 version 2 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Library General Public License for more details. 00017 00018 You should have received a copy of the GNU Library General Public License 00019 along with this library; see the file COPYING.LIB. If not, write to 00020 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00021 Boston, MA 02110-1301, USA. 00022 */ 00023 00024 00025 #ifndef VARIANT_BINDING_H 00026 #define VARIANT_BINDING_H 00027 00028 #include <QtCore/QVariant> 00029 00030 #include <kdemacros.h> 00031 #include <kjs/object.h> 00032 #include <kjs/interpreter.h> 00033 00034 #include "static_binding.h" 00035 00042 #define START_VARIANT_METHOD( METHODNAME, TYPE) \ 00043 KJS::JSValue *METHODNAME( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) \ 00044 { \ 00045 Q_UNUSED(exec);\ 00046 Q_UNUSED(self);\ 00047 Q_UNUSED(args);\ 00048 KJS::JSValue *result = KJS::jsNull(); \ 00049 KJSEmbed::VariantBinding *imp = KJSEmbed::extractBindingImp<KJSEmbed::VariantBinding>(exec, self ); \ 00050 if( imp ) \ 00051 { \ 00052 TYPE value = imp->value<TYPE>(); 00053 00056 #define END_VARIANT_METHOD \ 00057 imp->setValue(qVariantFromValue(value)); \ 00058 } \ 00059 else \ 00060 {\ 00061 KJS::throwError(exec, KJS::GeneralError, "We have a problem baby");\ 00062 }\ 00063 return result; \ 00064 } 00065 00066 #define KJSO_VARIANT_SIMPLE_BINDING_CTOR( NAME, JSNAME, TYPE, BASENAME ) \ 00067 NAME::NAME(KJS::ExecState *exec, const char* typeName ) \ 00068 : BASENAME( exec, typeName ) \ 00069 { \ 00070 StaticBinding::publish( exec, this, NAME::methods() ); \ 00071 } \ 00072 NAME::NAME(KJS::ExecState *exec, const TYPE & value) \ 00073 : BASENAME( exec, QVariant::fromValue(value)) \ 00074 { \ 00075 StaticBinding::publish( exec, this, NAME::methods() ); \ 00076 } 00077 00078 namespace KJSEmbed 00079 { 00088 class KJSEMBED_EXPORT VariantBinding : public ProxyBinding 00089 { 00090 public: 00094 VariantBinding( KJS::ExecState *exec, const QVariant &value ); 00095 virtual ~VariantBinding() {} 00096 00097 void *pointer(); 00098 00099 KJS::UString toString(KJS::ExecState *) const; 00100 KJS::UString className() const; 00101 00105 QVariant variant() const; 00106 00111 template<typename T> 00112 T value() const { return qVariantValue<T>(m_value); } 00116 void setValue( const QVariant &val ); 00117 00121 QGenericArgument arg(const char *type) const; 00122 00123 static const KJS::ClassInfo info; 00124 00125 private: 00126 virtual const KJS::ClassInfo* classInfo() const { return &info; } 00127 QVariant m_value; 00128 00129 }; 00130 00134 QVariant KJSEMBED_EXPORT extractVariant( KJS::ExecState *exec, KJS::JSValue *value ); 00135 00141 template< typename T> 00142 T extractVariant( KJS::ExecState *exec, KJS::JSValue *arg, const T &defaultValue ) 00143 { 00144 if( !arg ) 00145 return defaultValue; 00146 else 00147 { 00148 QVariant variant = extractVariant( exec, arg ); 00149 if( !variant.isNull() ) 00150 { 00151 if( qVariantCanConvert<T>(variant) ) 00152 return qVariantValue<T>(variant); 00153 else 00154 { 00155 throwError(exec, KJS::TypeError, "Cast failed" ); 00156 return defaultValue; 00157 } 00158 } 00159 else 00160 return defaultValue; 00161 } 00162 } 00163 00168 template< typename T> 00169 T extractVariant( KJS::ExecState *exec, const KJS::List &args, int idx, const T &defaultValue = T()) 00170 { 00171 if( args.size() >= idx ) 00172 { 00173 return extractVariant<T>( exec, args[idx], defaultValue ); 00174 } 00175 else 00176 return defaultValue; 00177 } 00178 00184 template< typename T> 00185 KJS::JSValue* createVariant(KJS::ExecState *exec, const KJS::UString &className, const T &value) 00186 { 00187 KJS::JSObject *parent; 00188 parent = exec->dynamicInterpreter()->globalObject(); 00189 KJS::JSObject *returnValue = StaticConstructor::construct( exec, parent, className ); 00190 if( returnValue ) 00191 { 00192 // If it is a value type setValue 00193 KJSEmbed::VariantBinding *imp = extractBindingImp<KJSEmbed::VariantBinding>(exec, returnValue ); 00194 if( imp ) 00195 imp->setValue( qVariantFromValue( value ) ); 00196 else 00197 { 00198 throwError(exec, KJS::TypeError, toUString(QString("Created failed to cast to %1 failed").arg(toQString(className)) )); 00199 return KJS::jsNull(); 00200 } 00201 } 00202 else 00203 { 00204 throwError(exec, KJS::TypeError, toUString(QString("Could not construct a %1").arg(toQString(className) ))); 00205 return KJS::jsNull(); 00206 } 00207 return returnValue; 00208 } 00209 00216 QMap<QString, QVariant> KJSEMBED_EXPORT convertArrayToMap( KJS::ExecState *exec, KJS::JSValue *value ); 00217 00224 QList<QVariant> KJSEMBED_EXPORT convertArrayToList( KJS::ExecState *exec, KJS::JSValue *value ); 00225 00229 QStringList KJSEMBED_EXPORT convertArrayToStringList( KJS::ExecState *exec, KJS::JSValue *value ); 00230 00234 QVariant KJSEMBED_EXPORT convertToVariant( KJS::ExecState *exec, KJS::JSValue *value ); 00235 00241 KJSEMBED_EXPORT KJS::JSValue* convertToValue( KJS::ExecState *exec, const QVariant &value ); 00242 00246 struct Method; 00247 class KJSEMBED_EXPORT VariantFactory 00248 { 00249 public: 00250 static const Method VariantMethods[]; 00251 static const Method *methods(){ return VariantMethods;} 00252 }; 00253 00254 } 00255 #endif 00256 00257 //kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle;
KDE 4.6 API Reference