add more spacing
[personal-kdebase.git] / workspace / plasma / scriptengines / javascript / backportglobal.h
blob51d66f7ecb089d1b1f5454e4a082c8f9e15d0147
1 /****************************************************************************
2 **
3 ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
4 **
5 ** This file is part of the plugins of the Qt Toolkit.
6 **
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://trolltech.com/products/qt/licenses/licensing/opensource/
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
17 ** or contact the sales department at sales@trolltech.com.
19 ** In addition, as a special exception, Trolltech gives you certain
20 ** additional rights. These rights are described in the Trolltech GPL
21 ** Exception version 1.0, which can be found at
22 ** http://www.trolltech.com/products/qt/gplexception/ and in the file
23 ** GPL_EXCEPTION.txt in this package.
25 ** In addition, as a special exception, Trolltech, as the sole copyright
26 ** holder for Qt Designer, grants users of the Qt/Eclipse Integration
27 ** plug-in the right for the Qt/Eclipse Integration to link to
28 ** functionality provided by Qt Designer and its related libraries.
30 ** Trolltech reserves all rights not expressly granted herein.
31 **
32 ** Trolltech ASA (c) 2007
34 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
35 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
37 ****************************************************************************/
38 #ifndef QTSCRIPTEXTENSIONS_GLOBAL_H
39 #define QTSCRIPTEXTENSIONS_GLOBAL_H
41 #include <QtCore/QSharedData>
43 #define DECLARE_SELF(Class, __fn__) \
44 Class* self = qscriptvalue_cast<Class*>(ctx->thisObject()); \
45 if (!self) { \
46 return ctx->throwError(QScriptContext::TypeError, \
47 QString::fromLatin1("%0.prototype.%1: this object is not a %0") \
48 .arg(#Class).arg(#__fn__)); \
51 #define DECLARE_SELF2(Class, __fn__, __ret__) \
52 Class* self = qscriptvalue_cast<Class*>(thisObject()); \
53 if (!self) { \
54 context()->throwError(QScriptContext::TypeError, \
55 QString::fromLatin1("%0.prototype.%1: this object is not a %0") \
56 .arg(#Class).arg(#__fn__)); \
57 return __ret__; \
62 #define ADD_METHOD(__p__, __f__) \
63 __p__.setProperty(#__f__, __p__.engine()->newFunction(__f__))
65 #define ADD_GET_METHOD(__p__, __get__) \
66 ADD_METHOD(__p__, __get__)
68 #define ADD_GET_SET_METHODS(__p__, __get__, __set__) \
69 do { \
70 ADD_METHOD(__p__, __get__); \
71 ADD_METHOD(__p__, __set__); \
72 } while (0);
74 #define ADD_CTOR_FUNCTION(__c__, __f__) ADD_METHOD(__c__, __f__)
76 #define ADD_ENUM_VALUE(__c__, __ns__, __v__) \
77 __c__.setProperty(#__v__, QScriptValue(__c__.engine(), __ns__::__v__))
80 #define BEGIN_DECLARE_METHOD(Class, __mtd__) \
81 static QScriptValue __mtd__(QScriptContext *ctx, QScriptEngine *eng) \
82 { \
83 DECLARE_SELF(Class, __mtd__);
85 #define END_DECLARE_METHOD \
89 #define DECLARE_GET_METHOD(Class, __get__) \
90 BEGIN_DECLARE_METHOD(Class, __get__) { \
91 return qScriptValueFromValue(eng, self->__get__()); \
92 } END_DECLARE_METHOD
94 #define DECLARE_SET_METHOD(Class, T, __set__) \
95 BEGIN_DECLARE_METHOD(Class, __set__) { \
96 self->__set__(qscriptvalue_cast<T>(ctx->argument(0))); \
97 return eng->undefinedValue(); \
98 } END_DECLARE_METHOD
100 #define DECLARE_GET_SET_METHODS(Class, T, __get__, __set__) \
101 DECLARE_GET_METHOD(Class, /*T,*/ __get__) \
102 DECLARE_SET_METHOD(Class, T, __set__)
106 #define DECLARE_SIMPLE_GET_METHOD(Class, __get__) \
107 BEGIN_DECLARE_METHOD(Class, __get__) { \
108 return QScriptValue(eng, self->__get__()); \
109 } END_DECLARE_METHOD
111 #define DECLARE_SIMPLE_SET_METHOD(Class, ToType, __set__) \
112 BEGIN_DECLARE_METHOD(Class, __set__) { \
113 self->__set__(ctx->argument(0).ToType()); \
114 return eng->undefinedValue(); \
115 } END_DECLARE_METHOD
117 #define DECLARE_BOOLEAN_GET_METHOD(Class, __set__) \
118 DECLARE_SIMPLE_GET_METHOD(Class, __set__)
119 #define DECLARE_BOOLEAN_SET_METHOD(Class, __set__) \
120 DECLARE_SIMPLE_SET_METHOD(Class, toBoolean, __set__)
122 #define DECLARE_INT_GET_METHOD(Class, __set__) \
123 DECLARE_SIMPLE_GET_METHOD(Class, __set__)
124 #define DECLARE_INT_SET_METHOD(Class, __set__) \
125 DECLARE_SIMPLE_SET_METHOD(Class, toInt32, __set__)
127 #define DECLARE_NUMBER_GET_METHOD(Class, __set__) \
128 DECLARE_SIMPLE_GET_METHOD(Class, __set__)
129 #define DECLARE_NUMBER_SET_METHOD(Class, __set__) \
130 DECLARE_SIMPLE_SET_METHOD(Class, toNumber, __set__)
132 #define DECLARE_STRING_GET_METHOD(Class, __set__) \
133 DECLARE_SIMPLE_GET_METHOD(Class, __set__)
134 #define DECLARE_STRING_SET_METHOD(Class, __set__) \
135 DECLARE_SIMPLE_SET_METHOD(Class, toString, __set__)
137 #define DECLARE_QOBJECT_GET_METHOD(Class, __get__) \
138 BEGIN_DECLARE_METHOD(Class, __get__) { \
139 return eng->newQObject(self->__get__()); \
140 } END_DECLARE_METHOD
141 #define DECLARE_QOBJECT_SET_METHOD(Class, __set__) \
142 DECLARE_SIMPLE_SET_METHOD(Class, toQObject, __set__)
144 #define DECLARE_BOOLEAN_GET_SET_METHODS(Class, __get__, __set__) \
145 DECLARE_BOOLEAN_GET_METHOD(Class, __get__) \
146 DECLARE_BOOLEAN_SET_METHOD(Class, __set__)
148 #define DECLARE_NUMBER_GET_SET_METHODS(Class, __get__, __set__) \
149 DECLARE_NUMBER_GET_METHOD(Class, __get__) \
150 DECLARE_NUMBER_SET_METHOD(Class, __set__)
152 #define DECLARE_INT_GET_SET_METHODS(Class, __get__, __set__) \
153 DECLARE_INT_GET_METHOD(Class, __get__) \
154 DECLARE_INT_SET_METHOD(Class, __set__)
156 #define DECLARE_STRING_GET_SET_METHODS(Class, __get__, __set__) \
157 DECLARE_STRING_GET_METHOD(Class, __get__) \
158 DECLARE_STRING_SET_METHOD(Class, __set__)
160 #define DECLARE_QOBJECT_GET_SET_METHODS(Class, __get__, __set__) \
161 DECLARE_QOBJECT_GET_METHOD(Class, __get__) \
162 DECLARE_QOBJECT_SET_METHOD(Class, __set__)
165 #define DECLARE_VOID_METHOD(Class, __fun__) \
166 BEGIN_DECLARE_METHOD(Class, __fun__) { \
167 self->__fun__(); \
168 return eng->undefinedValue(); \
169 } END_DECLARE_METHOD
171 #define DECLARE_VOID_NUMBER_METHOD(Class, __fun__) \
172 BEGIN_DECLARE_METHOD(Class, __fun__) { \
173 self->__fun__(ctx->argument(0).toNumber()); \
174 return eng->undefinedValue(); \
175 } END_DECLARE_METHOD
177 #define DECLARE_VOID_NUMBER_NUMBER_METHOD(Class, __fun__) \
178 BEGIN_DECLARE_METHOD(Class, __fun__) { \
179 self->__fun__(ctx->argument(0).toNumber(), ctx->argument(1).toNumber()); \
180 return eng->undefinedValue(); \
181 } END_DECLARE_METHOD
183 #define DECLARE_VOID_QUAD_NUMBER_METHOD(Class, __fun__) \
184 BEGIN_DECLARE_METHOD(Class, __fun__) { \
185 self->__fun__(ctx->argument(0).toNumber(), ctx->argument(1).toNumber(), ctx->argument(2).toNumber(), ctx->argument(3).toNumber()); \
186 return eng->undefinedValue(); \
187 } END_DECLARE_METHOD
189 #define DECLARE_VOID_1ARG_METHOD(Class, ArgType, __fun__) \
190 BEGIN_DECLARE_METHOD(Class, __fun__) { \
191 self->__fun__(qscriptvalue_cast<ArgType>(ctx->argument(0))); \
192 return eng->undefinedValue(); \
193 } END_DECLARE_METHOD
195 #define DECLARE_BOOLEAN_1ARG_METHOD(Class, ArgType, __fun__) \
196 BEGIN_DECLARE_METHOD(Class, __fun__) { \
197 return QScriptValue(eng, self->__fun__(qscriptvalue_cast<ArgType>(ctx->argument(0)))); \
198 } END_DECLARE_METHOD
201 #define DECLARE_POINTER_METATYPE(T) \
202 Q_DECLARE_METATYPE(T*) \
203 Q_DECLARE_METATYPE(QScript::Pointer<T>::wrapped_pointer_type)
205 namespace QScript
208 enum {
209 UserOwnership = 1
212 template <typename T>
213 class Pointer : public QSharedData
215 public:
216 typedef T* pointer_type;
217 typedef QExplicitlySharedDataPointer<Pointer<T> > wrapped_pointer_type;
219 ~Pointer()
221 if (!(m_flags & UserOwnership))
222 delete m_value;
225 operator T*()
227 return m_value;
230 operator const T*() const
232 return m_value;
235 static wrapped_pointer_type create(T *value, uint flags = 0)
237 return wrapped_pointer_type(new Pointer(value, flags));
240 static QScriptValue toScriptValue(QScriptEngine *engine, T* const &source)
242 if (!source)
243 return engine->nullValue();
244 return engine->newVariant(qVariantFromValue(source));
247 static void fromScriptValue(const QScriptValue &value, T* &target)
249 if (value.isVariant()) {
250 QVariant var = value.toVariant();
251 if (qVariantCanConvert<T*>(var)) {
252 target = qvariant_cast<T*>(var);
253 } else if (qVariantCanConvert<wrapped_pointer_type>(var)) {
254 target = qvariant_cast<wrapped_pointer_type>(var)->operator T*();
255 } else {
256 // look in prototype chain
257 target = 0;
258 int type = qMetaTypeId<T*>();
259 int pointerType = qMetaTypeId<wrapped_pointer_type>();
260 QScriptValue proto = value.prototype();
261 while (proto.isObject() && proto.isVariant()) {
262 int protoType = proto.toVariant().userType();
263 if ((type == protoType) || (pointerType == protoType)) {
264 QByteArray name = QMetaType::typeName(var.userType());
265 if (name.startsWith("QScript::Pointer<")) {
266 target = (*reinterpret_cast<wrapped_pointer_type*>(var.data()))->operator T*();
267 break;
268 } else {
269 target = static_cast<T*>(var.data());
270 break;
273 proto = proto.prototype();
276 } else if (value.isQObject()) {
277 QObject *qobj = value.toQObject();
278 QByteArray typeName = QMetaType::typeName(qMetaTypeId<T*>());
279 target = reinterpret_cast<T*>(qobj->qt_metacast(typeName.left(typeName.size()-1)));
280 } else {
281 target = 0;
285 uint flags() const
286 { return m_flags; }
287 void setFlags(uint flags)
288 { m_flags = flags; }
289 void unsetFlags(uint flags)
290 { m_flags &= ~flags; }
292 protected:
293 Pointer(T* value, uint flags)
294 : m_flags(flags), m_value(value)
297 private:
298 uint m_flags;
299 T* m_value;
302 template <typename T>
303 int registerPointerMetaType(
304 QScriptEngine *eng,
305 const QScriptValue &prototype = QScriptValue(),
306 T * /* dummy */ = 0
309 QScriptValue (*mf)(QScriptEngine *, T* const &) = Pointer<T>::toScriptValue;
310 void (*df)(const QScriptValue &, T* &) = Pointer<T>::fromScriptValue;
311 const int id = qMetaTypeId<T*>();
312 qScriptRegisterMetaType_helper(
313 eng, id, reinterpret_cast<QScriptEngine::MarshalFunction>(mf),
314 reinterpret_cast<QScriptEngine::DemarshalFunction>(df),
315 prototype);
316 eng->setDefaultPrototype(qMetaTypeId<typename Pointer<T>::wrapped_pointer_type>(), prototype);
317 return id;
320 inline void maybeReleaseOwnership(const QScriptValue &value)
322 if (value.isVariant()) {
323 QVariant var = value.toVariant();
324 QByteArray name = QMetaType::typeName(var.userType());
325 if (name.startsWith("QScript::Pointer<"))
326 (*reinterpret_cast<Pointer<void*>::wrapped_pointer_type *>(var.data()))->setFlags(UserOwnership);
330 inline void maybeTakeOwnership(const QScriptValue &value)
332 if (value.isVariant()) {
333 QVariant var = value.toVariant();
334 QByteArray name = QMetaType::typeName(var.userType());
335 if (name.startsWith("QScript::Pointer<"))
336 (*reinterpret_cast<Pointer<void*>::wrapped_pointer_type *>(var.data()))->unsetFlags(UserOwnership);
340 template <class T>
341 inline QScriptValue wrapPointer(QScriptEngine *eng, T *ptr, uint flags = 0)
343 return eng->newVariant(qVariantFromValue(Pointer<T>::create(ptr, flags)));
346 } // namespace QScript
348 #ifdef QGRAPHICSITEM_H
350 namespace QScript {
352 template <class T>
353 inline QScriptValue wrapGVPointer(QScriptEngine *eng, T *item)
355 uint flags = item->parentItem() ? UserOwnership : 0;
356 return wrapPointer<T>(eng, item, flags);
359 } // namespace QScript
361 #endif // QGRAPHICSITEM_H
363 #endif // QTSCRIPTEXTENSIONS_GLOBAL_H