• 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
value_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 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 
24 #ifndef VALUE_BINDING_H
25 #define VALUE_BINDING_H
26 
27 #include <kjs/object.h>
28 #include <kjs/interpreter.h>
29 
30 #include "static_binding.h"
31 #include "pointer.h"
32 
39 #define START_VALUE_METHOD( METHODNAME, TYPE) \
40 KJS::JSValue *METHODNAME( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) \
41 { \
42  Q_UNUSED(exec);\
43  Q_UNUSED(self);\
44  Q_UNUSED(args);\
45  KJS::JSValue *result = KJS::jsNull(); \
46  KJSEmbed::ValueBinding *imp = KJSEmbed::extractBindingImp<KJSEmbed::ValueBinding>(exec, self ); \
47  if( imp ) \
48  { \
49  TYPE value = imp->value<TYPE>();
50 
53 #define END_VALUE_METHOD \
54  imp->setValue(value); \
55  } \
56  else { \
57  KJS::throwError(exec, KJS::GeneralError, "Problem in ValueBinding here");\
58  }\
59  return result; \
60 }
61 
62 #define KJSO_VALUE_SIMPLE_BINDING_CTOR( NAME, JSNAME, TYPE, BASENAME ) \
63  NAME::NAME(KJS::ExecState *exec, const char* typeName ) \
64  : BASENAME( exec, typeName ) \
65  { \
66  StaticBinding::publish( exec, this, NAME::methods() ); \
67  } \
68  NAME::NAME(KJS::ExecState *exec, const TYPE & value) \
69  : BASENAME( exec, #JSNAME , value ) \
70  { \
71  StaticBinding::publish( exec, this, NAME::methods() ); \
72  }
73 
74 #define KJSO_VALUE_DERIVED_BINDING_CTOR( NAME, JSNAME, TYPE, BASENAME ) \
75  NAME::NAME(KJS::ExecState *exec, const char* typeName ) \
76  : BASENAME( exec, typeName ) \
77  { \
78  StaticBinding::publish( exec, this, NAME::methods() ); \
79  } \
80  NAME::NAME(KJS::ExecState *exec, const TYPE & value) \
81  : BASENAME( exec, #JSNAME ) \
82  { \
83  setValue(value); \
84  StaticBinding::publish( exec, this, NAME::methods() ); \
85  }
86 
87 
88 namespace KJSEmbed
89 {
93  class ValueFactory
94  {
95  public:
96  static const Method ValueMethods[];
97  static const Method *methods();
98  };
99 
103  class ValueBinding : public ProxyBinding
104  {
105  public:
106  template <typename T>
107  ValueBinding( KJS::ExecState *exec, const char *typeName, T val )
108  : ProxyBinding( exec ),
109  m_name(typeName)
110  {
111  m_value = new Value<T>(val);
112  StaticBinding::publish( exec, this, ValueFactory::methods() );
113  }
114  ValueBinding( KJS::ExecState *exec, const char *typeName);
115  virtual ~ValueBinding();
116 
117  KJS::UString toString(KJS::ExecState *exec) const;
118  KJS::UString className() const { return m_name; }
119 
123  template< typename T>
124  T value() const
125  {
126  const T *ptr = reinterpret_cast<const T*>(m_value->voidStar());
127  if( ptr )
128  return *ptr;
129  else
130  return T();
131  }
132 
136  template< typename T>
137  void setValue( const T &val )
138  {
139  delete m_value;
140  m_value = new Value<T>(val);
141  }
142 
143  template< typename T>
144  static T castValue( ValueBinding *imp)
145  {
146  const T *ptr = reinterpret_cast<const T*>( imp->m_value->voidStar() );
147  if( ptr )
148  return *ptr;
149  else
150  return T();
151  }
152  static const KJS::ClassInfo info;
153 
154  private:
155  virtual const KJS::ClassInfo* classInfo() const { return &info; }
156 
157  PointerBase *m_value;
158  const char *m_name;
159 
160  };
161 
166  template< typename T>
167  T extractValue( KJS::ExecState *exec, KJS::JSValue *arg, const T &defaultValue )
168  {
169  if( arg )
170  {
171  KJSEmbed::ValueBinding *imp =
172  KJSEmbed::extractBindingImp<KJSEmbed::ValueBinding>(exec, arg );
173  if( imp )
174  return ValueBinding::castValue<T>( imp );
175  }
176  return defaultValue;
177  }
178 
183  template< typename T>
184  T extractValue( KJS::ExecState *exec, const KJS::List &args, int idx, const T &defaultValue = T())
185  {
186  if( args.size() > idx )
187  {
188  return extractValue<T>( exec, args[idx], defaultValue );
189  }
190  else
191  return defaultValue;
192  }
193 
194  template< typename T>
195  KJS::JSValue *createValue(KJS::ExecState *exec, const KJS::UString &className, const T &value)
196  {
197  KJS::JSObject *parent = exec->dynamicInterpreter()->globalObject();
198  KJS::JSObject *returnValue = StaticConstructor::construct( exec, parent, className );
199  if( returnValue )
200  {
201  // If it is a value type setValue
202  KJSEmbed::ValueBinding *imp =
203  extractBindingImp<KJSEmbed::ValueBinding>(exec, returnValue );
204  if( imp )
205  imp->setValue( value );
206  else
207  {
208  KJS::throwError(exec, KJS::TypeError, toUString(QString("Created failed to cast to %1 failed").arg(toQString(className))));
209  return KJS::jsNull();
210  }
211  }
212  else
213  {
214  KJS::throwError(exec, KJS::TypeError, toUString(QString("Could not construct a %1").arg(toQString(className) )));
215  return KJS::jsNull();
216  }
217  return returnValue;
218  }
219 }
220 
221 #endif
222 
223 //kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle;
KJSEmbed::ValueFactory::ValueMethods
static const Method ValueMethods[]
Definition: value_binding.h:96
KJSEmbed::ValueBinding::toString
KJS::UString toString(KJS::ExecState *exec) const
Definition: value_binding.cpp:66
KJSEmbed::StaticBinding::publish
static void publish(KJS::ExecState *exec, KJS::JSObject *object, const Method *methods)
Publishes an array of Methods to an object.
Definition: static_binding.cpp:60
KJSEmbed::ProxyBinding
Definition: binding_support.h:247
KJSEmbed::ValueBinding::ValueBinding
ValueBinding(KJS::ExecState *exec, const char *typeName, T val)
Definition: value_binding.h:107
KJSEmbed::ValueBinding::castValue
static T castValue(ValueBinding *imp)
Definition: value_binding.h:144
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
KJSEmbed::ValueBinding::~ValueBinding
virtual ~ValueBinding()
Definition: value_binding.cpp:61
KJSEmbed::ValueBinding
Value binding implementation.
Definition: value_binding.h:103
className
END_QOBJECT_METHOD QByteArray className
Definition: qobject_binding.cpp:832
PointerBase
Definition: pointer.h:31
parent
QObject * parent
Definition: qaction_binding.cpp:48
KJSEmbed::Method
Method structure.
Definition: binding_support.h:294
KJSEmbed::ValueBinding::value
T value() const
Returns the stored value.
Definition: value_binding.h:124
DomElementNS::defaultValue
QString defaultValue
Definition: dom.cpp:506
KJSEmbed::ValueBinding::className
KJS::UString className() const
Definition: value_binding.h:118
NodeListNS::idx
END_VALUE_METHOD int idx
Definition: dom.cpp:691
Value
Definition: pointer.h:73
KJSEmbed::ValueFactory::methods
static const Method * methods()
Definition: value_binding.cpp:48
KJSEmbed::ValueBinding::info
static const KJS::ClassInfo info
Definition: value_binding.h:152
List
Definition: variant_binding.cpp:130
KJSEmbed::ValueBinding::setValue
void setValue(const T &val)
Set the internal value.
Definition: value_binding.h:137
static_binding.h
KJSEmbed::extractValue
T extractValue(KJS::ExecState *exec, KJS::JSValue *arg, const T &defaultValue)
Extracts a pointer based type from an ObjectBinding object.
Definition: value_binding.h:167
KJSEmbed::toUString
KJS::UString toUString(const QString &qs)
Definition: kjseglobal.h:66
PointerBase::voidStar
virtual void * voidStar()=0
KJSEmbed::createValue
KJS::JSValue * createValue(KJS::ExecState *exec, const KJS::UString &className, const T &value)
Definition: value_binding.h:195
value
QVariant value
Definition: settings.cpp:35
pointer.h
KJS::throwError
JSObject * throwError(ExecState *e, ErrorType t, const QString &m)
Definition: binding_support.h:241
KJSEmbed::ValueFactory
The Bindings for the KJSEmbed::ValueBinding.
Definition: value_binding.h:93
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