kjsembed
variant_binding.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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