• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

kjsembed

  • sources
  • kde-4.12
  • kdelibs
  • kjsembed
  • kjsembed
variant_binding.h
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
3  Copyright (C) 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
4  Copyright (C) 2005, 2006 Richard J. Moore <rich@kde.org>
5  Copyright (C) 2005, 2006 Erik L. Bunce <kde@bunce.us>
6  Copyright (C) 2007, 2008 Sebastian Sauer <mail@dipe.org>
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to
20  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  Boston, MA 02110-1301, USA.
22 */
23 
24 
25 #ifndef VARIANT_BINDING_H
26 #define VARIANT_BINDING_H
27 
28 #include <QtCore/QVariant>
29 
30 #include <kdemacros.h>
31 #include <kjs/object.h>
32 #include <kjs/interpreter.h>
33 
34 #include "static_binding.h"
35 
42 #define START_VARIANT_METHOD( METHODNAME, TYPE) \
43 KJS::JSValue *METHODNAME( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) \
44 { \
45  Q_UNUSED(exec);\
46  Q_UNUSED(self);\
47  Q_UNUSED(args);\
48  KJS::JSValue *result = KJS::jsNull(); \
49  KJSEmbed::VariantBinding *imp = KJSEmbed::extractBindingImp<KJSEmbed::VariantBinding>(exec, self ); \
50  if( imp ) \
51  { \
52  TYPE value = imp->value<TYPE>();
53 
56 #define END_VARIANT_METHOD \
57  imp->setValue(qVariantFromValue(value)); \
58  } \
59  else \
60  {\
61  KJS::throwError(exec, KJS::GeneralError, "We have a problem baby");\
62  }\
63  return result; \
64 }
65 
66 #define KJSO_VARIANT_SIMPLE_BINDING_CTOR( NAME, JSNAME, TYPE, BASENAME ) \
67  NAME::NAME(KJS::ExecState *exec, const char* typeName ) \
68  : BASENAME( exec, typeName ) \
69  { \
70  StaticBinding::publish( exec, this, NAME::methods() ); \
71  } \
72  NAME::NAME(KJS::ExecState *exec, const TYPE & value) \
73  : BASENAME( exec, QVariant::fromValue(value)) \
74  { \
75  StaticBinding::publish( exec, this, NAME::methods() ); \
76  }
77 
78 namespace KJSEmbed
79 {
88  class KJSEMBED_EXPORT VariantBinding : public ProxyBinding
89  {
90  public:
94  VariantBinding( KJS::ExecState *exec, const QVariant &value );
95  virtual ~VariantBinding() {}
96 
97  void *pointer();
98 
99  KJS::UString toString(KJS::ExecState *) const;
100  KJS::UString className() const;
101 
105  QVariant variant() const;
106 
111  template<typename T>
112  T value() const { return qVariantValue<T>(m_value); }
116  void setValue( const QVariant &val );
117 
121  QGenericArgument arg(const char *type) const;
122 
123  static const KJS::ClassInfo info;
124 
125  private:
126  virtual const KJS::ClassInfo* classInfo() const { return &info; }
127  QVariant m_value;
128 
129  };
130 
134  QVariant KJSEMBED_EXPORT extractVariant( KJS::ExecState *exec, KJS::JSValue *value );
135 
141  template< typename T>
142  T extractVariant( KJS::ExecState *exec, KJS::JSValue *arg, const T &defaultValue )
143  {
144  if( !arg )
145  return defaultValue;
146  else
147  {
148  QVariant variant = extractVariant( exec, arg );
149  if( !variant.isNull() )
150  {
151  if( qVariantCanConvert<T>(variant) )
152  return qVariantValue<T>(variant);
153  else
154  {
155  throwError(exec, KJS::TypeError, "Cast failed" );
156  return defaultValue;
157  }
158  }
159  else
160  return defaultValue;
161  }
162  }
163 
168  template< typename T>
169  T extractVariant( KJS::ExecState *exec, const KJS::List &args, int idx, const T &defaultValue = T())
170  {
171  if( args.size() >= idx )
172  {
173  return extractVariant<T>( exec, args[idx], defaultValue );
174  }
175  else
176  return defaultValue;
177  }
178 
184  template< typename T>
185  KJS::JSValue* createVariant(KJS::ExecState *exec, const KJS::UString &className, const T &value)
186  {
187  KJS::JSObject *parent;
188  parent = exec->dynamicInterpreter()->globalObject();
189  KJS::JSObject *returnValue = StaticConstructor::construct( exec, parent, className );
190  if( returnValue )
191  {
192  // If it is a value type setValue
193  KJSEmbed::VariantBinding *imp = extractBindingImp<KJSEmbed::VariantBinding>(exec, returnValue );
194  if( imp )
195  imp->setValue( qVariantFromValue( value ) );
196  else
197  {
198  throwError(exec, KJS::TypeError, toUString(QString("Created failed to cast to %1 failed").arg(toQString(className)) ));
199  return KJS::jsNull();
200  }
201  }
202  else
203  {
204  throwError(exec, KJS::TypeError, toUString(QString("Could not construct a %1").arg(toQString(className) )));
205  return KJS::jsNull();
206  }
207  return returnValue;
208  }
209 
216  QMap<QString, QVariant> KJSEMBED_EXPORT convertArrayToMap( KJS::ExecState *exec, KJS::JSValue *value );
217 
224  QList<QVariant> KJSEMBED_EXPORT convertArrayToList( KJS::ExecState *exec, KJS::JSValue *value );
225 
229  QStringList KJSEMBED_EXPORT convertArrayToStringList( KJS::ExecState *exec, KJS::JSValue *value );
230 
234  QVariant KJSEMBED_EXPORT convertToVariant( KJS::ExecState *exec, KJS::JSValue *value );
235 
241  KJSEMBED_EXPORT KJS::JSValue* convertToValue( KJS::ExecState *exec, const QVariant &value );
242 
246  struct Method;
247  class KJSEMBED_EXPORT VariantFactory
248  {
249  public:
250  static const Method VariantMethods[];
251  static const Method *methods(){ return VariantMethods;}
252  };
253 
254 }
255 #endif
256 
257 //kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle;
KJSEmbed::convertToVariant
QVariant KJSEMBED_EXPORT convertToVariant(KJS::ExecState *exec, KJS::JSValue *value)
Convert a KJS::JSValue into a QVariant object.
Definition: variant_binding.cpp:253
KJSEmbed::VariantBinding::info
static const KJS::ClassInfo info
Definition: variant_binding.h:123
KJSEmbed::VariantBinding::value
T value() const
Extract the actual value from the wrapper.
Definition: variant_binding.h:112
KJSEmbed::convertArrayToStringList
QStringList KJSEMBED_EXPORT convertArrayToStringList(KJS::ExecState *exec, KJS::JSValue *value)
Convert a KJS::JSValue inot a QStringList.
Definition: variant_binding.cpp:199
setValue
object setValue(key, value)
KJSEmbed::VariantBinding
QVariant based binding.
Definition: variant_binding.h:88
KJSEmbed::convertArrayToMap
QMap< QString, QVariant > KJSEMBED_EXPORT convertArrayToMap(KJS::ExecState *exec, KJS::JSValue *value)
Convert a KJS::JSValue that contains an associative array into a QMap.
Definition: variant_binding.cpp:153
KJSEmbed::ProxyBinding
Definition: binding_support.h:247
KJSEmbed::createVariant
KJS::JSValue * createVariant(KJS::ExecState *exec, const KJS::UString &className, const T &value)
Can create any known KJSEmbed::VariantBinding object and set the value.
Definition: variant_binding.h:185
KJSEmbed::StaticConstructor::construct
KJS::JSObject * construct(KJS::ExecState *exec, const KJS::List &args)
Calls the callback that will in turn create a new instance of this object with the arguments passed i...
Definition: static_binding.cpp:79
DomElementNS::val
QString val
Definition: dom.cpp:529
className
END_QOBJECT_METHOD QByteArray className
Definition: qobject_binding.cpp:832
parent
QObject * parent
Definition: qaction_binding.cpp:48
KJSEmbed::Method
Method structure.
Definition: binding_support.h:294
KJSEmbed::VariantFactory
Definition: variant_binding.h:247
DomElementNS::defaultValue
QString defaultValue
Definition: dom.cpp:506
KJSEmbed::convertArrayToList
QList< QVariant > KJSEMBED_EXPORT convertArrayToList(KJS::ExecState *exec, KJS::JSValue *value)
Convert a KJS::JSValue into a QList.
Definition: variant_binding.cpp:169
KJSEmbed::VariantBinding::setValue
void setValue(const QVariant &val)
Set the internal value of the QVariant.
Definition: variant_binding.cpp:73
NodeListNS::idx
END_VALUE_METHOD int idx
Definition: dom.cpp:691
KJSEmbed::VariantFactory::methods
static const Method * methods()
Definition: variant_binding.h:251
KJSEmbed::VariantBinding::~VariantBinding
virtual ~VariantBinding()
Definition: variant_binding.h:95
List
Definition: variant_binding.cpp:130
static_binding.h
KJSEmbed::toUString
KJS::UString toUString(const QString &qs)
Definition: kjseglobal.h:66
KJSEmbed::convertToValue
KJSEMBED_EXPORT KJS::JSValue * convertToValue(KJS::ExecState *exec, const QVariant &value)
Convert a QVariant to a KJS::JSValue.
Definition: variant_binding.cpp:298
value
QVariant value
Definition: settings.cpp:35
KJSEMBED_EXPORT
#define KJSEMBED_EXPORT
Definition: kjseglobal.h:32
KJS::throwError
JSObject * throwError(ExecState *e, ErrorType t, const QString &m)
Definition: binding_support.h:241
KJSEmbed::extractVariant
QVariant KJSEMBED_EXPORT extractVariant(KJS::ExecState *exec, KJS::JSValue *value)
Extracts a QVariant from a KJS::JSValue if the conversion fails a QVariant::Null is returned...
Definition: variant_binding.cpp:407
KJSEmbed::toQString
QString toQString(const KJS::UString &u)
Definition: kjseglobal.h:58
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kjsembed

Skip menu "kjsembed"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal