• 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
object_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 OBJECT_BINDING_H
25 #define OBJECT_BINDING_H
26 
27 #include <kdemacros.h>
28 
29 #include "static_binding.h"
30 #include "variant_binding.h"
31 #include "pointer.h"
32 #include "kjseglobal.h"
33 
40 #define START_OBJECT_METHOD( METHODNAME, TYPE) \
41 KJS::JSValue *METHODNAME( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) \
42 { \
43  Q_UNUSED(exec);\
44  Q_UNUSED(self);\
45  Q_UNUSED(args);\
46  KJS::JSValue *result = KJS::jsNull(); \
47  KJSEmbed::ObjectBinding *imp = KJSEmbed::extractBindingImp<KJSEmbed::ObjectBinding>(exec, self ); \
48  if( imp ) \
49  { \
50  TYPE *object = imp->object<TYPE>(); \
51  if( object ) \
52  {
53 
57 #define END_OBJECT_METHOD \
58  } \
59  else \
60  KJS::throwError(exec, KJS::ReferenceError, QString("O: The internal object died."));\
61  } \
62  else \
63  KJS::throwError(exec, KJS::GeneralError, QString("Object cast failed."));\
64  return result; \
65 }
66 
67 #define START_STATIC_OBJECT_METHOD( METHODNAME ) \
68 KJS::JSValue *METHODNAME( KJS::ExecState *exec, KJS::JSObject *self, const KJS::List &args ) \
69 {\
70  Q_UNUSED(exec);\
71  Q_UNUSED(self);\
72  Q_UNUSED(args);\
73  KJS::JSValue *result = KJS::jsNull(); \
74 
75 #define END_STATIC_OBJECT_METHOD \
76  return result; \
77 }
78 
79 namespace KJSEmbed
80 {
81  class KJSEMBED_EXPORT ObjectFactory
82  {
83  public:
84  static const Method ObjectMethods[];
85  static const Method *methods(){ return ObjectMethods;}
86  };
87 
88  class KJSEMBED_EXPORT ObjectBinding : public ProxyBinding
89  {
90  public:
91  enum Ownership { CPPOwned, QObjOwned, JSOwned };
92  static const KJS::ClassInfo info;
93 
94  private:
95  const char *m_name;
96  mutable PointerBase *m_value;
97  Ownership m_owner;
98 
99  public:
100  template <typename T>
101  ObjectBinding( KJS::ExecState *exec, const char *typeName, T *ptr )
102  : ProxyBinding(exec),
103  m_name(typeName)
104  {
105  StaticBinding::publish( exec, this, ObjectFactory::methods() );
106 
107  m_owner = CPPOwned;
108  m_value = new Pointer<T>(ptr);
109  }
110 
111  virtual ~ObjectBinding();
112 
113  const char *typeName() const;
114 
118  template <typename T>
119  T *object() const
120  {
121  if( m_value )
122  return pointer_cast<T>(m_value);
123  else
124  return 0;
125  }
126 
127  void *voidStar() const
128  {
129  return m_value->voidStar();
130  }
131 
132  template <typename T>
133  void setObject( T *ptr )
134  {
135  if( m_owner == JSOwned )
136  {
137  //qDebug("object cleans up");
138  m_value->cleanup();
139  }
140  delete m_value;
141  m_value = new Pointer<T>(ptr);
142  }
143 
144  KJS::UString toString( KJS::ExecState *exec ) const;
145  KJS::UString className() const;
146  KJS::JSType type() const;
147 
148  Ownership ownership() const;
149  void setOwnership( Ownership owner );
150 
151  };
152 
157  template< typename T>
158  T * extractObject( KJS::ExecState *exec, KJS::JSValue *arg, T *defaultValue )
159  {
160  if( !arg )
161  return defaultValue;
162  else
163  {
164  T *returnValue = 0;
165  KJSEmbed::ObjectBinding *imp = KJSEmbed::extractBindingImp<KJSEmbed::ObjectBinding>(exec, arg );
166  if( imp )
167  {
168  // GCC 3.3 has problems calling template functions in another class from a template class.
169  // returnValue = imp->object<T>();
170 
171  returnValue = (T *)imp->voidStar();
172  }
173  if( returnValue )
174  return returnValue;
175  else
176  return defaultValue;
177  }
178  }
179 
184  template< typename T>
185  T * extractObject( KJS::ExecState *exec, const KJS::List &args, int idx, T *defaultValue = 0L)
186  {
187  if( args.size() > idx )
188  {
189  return extractObject<T>( exec, args[idx], defaultValue );
190  }
191  else
192  return defaultValue;
193  }
194 
199  template< typename T>
200  KJS::JSValue *createObject(KJS::ExecState *exec, const KJS::UString &className, const T *value, KJSEmbed::ObjectBinding::Ownership owner = KJSEmbed::ObjectBinding::JSOwned )
201  {
202  if ( 0 == value )
203  return KJS::jsNull();
204 
205  KJS::JSObject *parent = exec->dynamicInterpreter()->globalObject();
206  KJS::JSObject *returnValue = StaticConstructor::construct( exec, parent, className );
207  if( returnValue )
208  {
209  // If it is a value type setValue
210  KJSEmbed::ObjectBinding *imp = extractBindingImp<KJSEmbed::ObjectBinding>(exec, returnValue );
211  if( imp )
212  {
213  imp->setOwnership( KJSEmbed::ObjectBinding::JSOwned );
214  imp->setObject( value );
215  imp->setOwnership( owner );
216  }
217  else
218  {
219  throwError(exec, KJS::TypeError, i18n("%1 is not an Object type", className.ascii()));
220  return KJS::jsNull();
221  }
222  }
223  else
224  {
225  throwError(exec, KJS::GeneralError, "Could not construct value");
226  //throwError(exec, "Could not construct value" );
227  return KJS::jsNull();
228  }
229 
230  return returnValue;
231  }
232 
233  template< typename T >
234  T extractParameter( KJS::ExecState *exec, KJS::JSValue *arg, const T &defaultValue )
235  {
236  if( !arg )
237  return defaultValue;
238  else
239  {
240  switch (arg->type())
241  {
242  case KJS::NumberType:
243  return extractInt(exec, arg, defaultValue);
244  break;
245  case KJS::BooleanType:
246  return extractBool(exec, arg, 0);
247  break;
248  case KJS::UnspecifiedType:
249  case KJS::UndefinedType:
250  case KJS::NullType:
251  case KJS::GetterSetterType:
252  case KJS::StringType:
253  return defaultValue;
254  break;
255  }
256 
257  KJS::JSObject* object = arg->toObject(exec);
258  if(object->inherits(&VariantBinding::info))
259  {
260  return extractVariant<T>(exec, arg, defaultValue);
261  }
262  else if(object->inherits(&ObjectBinding::info))
263  {
264  return extractObject<T>(exec, arg, defaultValue);
265  }
266  else
267  return defaultValue;
268  }
269  }
270 }
271 #endif
272 
273 //kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle;
KJSEmbed::VariantBinding::info
static const KJS::ClassInfo info
Definition: variant_binding.h:123
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
setOwnership
END_METHOD_LUT setOwnership(CPPOwned)
KJSEmbed::ProxyBinding
Definition: binding_support.h:247
KJSEmbed::ObjectBinding::Ownership
Ownership
Definition: object_binding.h:91
Pointer
Definition: pointer.h:42
KJSEmbed::ObjectBinding::JSOwned
Definition: object_binding.h:91
KJSEmbed::ObjectBinding::setObject
void setObject(T *ptr)
Definition: object_binding.h:133
KJSEmbed::ObjectBinding::ObjectBinding
ObjectBinding(KJS::ExecState *exec, const char *typeName, T *ptr)
Definition: object_binding.h:101
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
KJSEmbed::extractBool
bool KJSEMBED_EXPORT extractBool(KJS::ExecState *exec, const KJS::List &args, int idx, bool defaultValue=false)
Extracts a bool from an argument list.
Definition: binding_support.cpp:149
KJSEmbed::ObjectBinding::QObjOwned
Definition: object_binding.h:91
KJSEmbed::extractInt
int KJSEMBED_EXPORT extractInt(KJS::ExecState *exec, const KJS::List &args, int idx, int defaultValue=0)
Extracts an integer from an argument list.
Definition: binding_support.cpp:72
KJSEmbed::ObjectBinding::voidStar
void * voidStar() const
Definition: object_binding.h:127
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::ObjectBinding::info
static const KJS::ClassInfo info
Definition: object_binding.h:92
KJSEmbed::Method
Method structure.
Definition: binding_support.h:294
KJSEmbed::ObjectBinding::object
T * object() const
Definition: object_binding.h:119
KJSEmbed::ObjectBinding
Definition: object_binding.h:88
DomElementNS::defaultValue
QString defaultValue
Definition: dom.cpp:506
KJSEmbed::ObjectFactory
Definition: object_binding.h:81
KJSEmbed::ObjectFactory::methods
static const Method * methods()
Definition: object_binding.h:85
KJSEmbed::ObjectBinding::setOwnership
void setOwnership(Ownership owner)
Definition: object_binding.cpp:70
NodeListNS::idx
END_VALUE_METHOD int idx
Definition: dom.cpp:691
pointer_cast
ValueType * pointer_cast(PointerBase *pointer)
Definition: pointer.h:133
AttrElementNS::owner
END_VALUE_METHOD QDomElement owner
Definition: dom.cpp:644
kjseglobal.h
List
Definition: variant_binding.cpp:130
KJSEmbed::extractObject
T * extractObject(KJS::ExecState *exec, KJS::JSValue *arg, T *defaultValue)
Extracts a pointer based type from an ObjectBinding object.
Definition: object_binding.h:158
static_binding.h
KJSEmbed::createObject
KJS::JSValue * createObject(KJS::ExecState *exec, const KJS::UString &className, const T *value, KJSEmbed::ObjectBinding::Ownership owner=KJSEmbed::ObjectBinding::JSOwned)
Can create any known KJSEmbed::ObjectBinding object and set the value.
Definition: object_binding.h:200
variant_binding.h
value
QVariant value
Definition: settings.cpp:35
KJSEMBED_EXPORT
#define KJSEMBED_EXPORT
Definition: kjseglobal.h:32
pointer.h
KJS::throwError
JSObject * throwError(ExecState *e, ErrorType t, const QString &m)
Definition: binding_support.h:241
KJSEmbed::extractParameter
T extractParameter(KJS::ExecState *exec, KJS::JSValue *arg, const T &defaultValue)
Definition: object_binding.h:234
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