1 /****************************************************************************
3 ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
5 ** This file is part of the plugins of the Qt Toolkit.
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.
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()); \
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()); \
54 context()->throwError(QScriptContext::TypeError, \
55 QString::fromLatin1("%0.prototype.%1: this object is not a %0") \
56 .arg(#Class).arg(#__fn__)); \
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__) \
70 ADD_METHOD(__p__, __get__); \
71 ADD_METHOD(__p__, __set__); \
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) \
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__()); \
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(); \
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__()); \
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(); \
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__()); \
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__) { \
168 return eng->undefinedValue(); \
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(); \
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(); \
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(); \
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(); \
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)))); \
201 #define DECLARE_POINTER_METATYPE(T) \
202 Q_DECLARE_METATYPE(T*) \
203 Q_DECLARE_METATYPE(QScript::Pointer<T>::wrapped_pointer_type)
212 template <typename T
>
213 class Pointer
: public QSharedData
216 typedef T
* pointer_type
;
217 typedef QExplicitlySharedDataPointer
<Pointer
<T
> > wrapped_pointer_type
;
221 if (!(m_flags
& UserOwnership
))
230 operator const T
*() const
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
)
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
*();
256 // look in prototype chain
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
*();
269 target
= static_cast<T
*>(var
.data());
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)));
287 void setFlags(uint flags
)
289 void unsetFlags(uint flags
)
290 { m_flags
&= ~flags
; }
293 Pointer(T
* value
, uint flags
)
294 : m_flags(flags
), m_value(value
)
302 template <typename T
>
303 int registerPointerMetaType(
305 const QScriptValue
&prototype
= QScriptValue(),
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
),
316 eng
->setDefaultPrototype(qMetaTypeId
<typename Pointer
<T
>::wrapped_pointer_type
>(), prototype
);
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
);
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
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