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

kjsembed

  • 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) \
40KJS::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>();
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
88namespace 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::ProxyBinding
Definition: binding_support.h:248
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::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::ValueBinding
Value binding implementation.
Definition: value_binding.h:104
KJSEmbed::ValueBinding::~ValueBinding
virtual ~ValueBinding()
Definition: value_binding.cpp:61
KJSEmbed::ValueBinding::info
static const KJS::ClassInfo info
Definition: value_binding.h:152
KJSEmbed::ValueBinding::ValueBinding
ValueBinding(KJS::ExecState *exec, const char *typeName, T val)
Definition: value_binding.h:107
KJSEmbed::ValueBinding::value
T value() const
Returns the stored value.
Definition: value_binding.h:124
KJSEmbed::ValueBinding::setValue
void setValue(const T &val)
Set the internal value.
Definition: value_binding.h:137
KJSEmbed::ValueBinding::castValue
static T castValue(ValueBinding *imp)
Definition: value_binding.h:144
KJSEmbed::ValueBinding::className
KJS::UString className() const
Definition: value_binding.h:118
KJSEmbed::ValueBinding::toString
KJS::UString toString(KJS::ExecState *exec) const
Definition: value_binding.cpp:66
KJSEmbed::ValueFactory
The Bindings for the KJSEmbed::ValueBinding.
Definition: value_binding.h:94
KJSEmbed::ValueFactory::methods
static const Method * methods()
Definition: value_binding.cpp:48
KJSEmbed::ValueFactory::ValueMethods
static const Method ValueMethods[]
Definition: value_binding.h:96
KJSEmbed
Definition: application.h:33
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::toQString
QString toQString(const KJS::UString &u)
Definition: kjseglobal.h:58
KJSEmbed::createValue
KJS::JSValue * createValue(KJS::ExecState *exec, const KJS::UString &className, const T &value)
Definition: value_binding.h:195
KJSEmbed::toUString
KJS::UString toUString(const QString &qs)
Definition: kjseglobal.h:66
KJS::throwError
JSObject * throwError(ExecState *e, ErrorType t, const QString &m)
Definition: binding_support.h:241
pointer.h
parent
QObject * parent
Definition: qaction_binding.cpp:48
className
END_QOBJECT_METHOD QByteArray className
Definition: qobject_binding.cpp:832
value
QVariant value
Definition: settings.cpp:35
static_binding.h
KJSEmbed::Method
Method structure.
Definition: binding_support.h:295
PointerBase
Definition: pointer.h:32
PointerBase::voidStar
virtual void * voidStar()=0
Value
Definition: pointer.h:74
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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