Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / v8 / qjsengine.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtScript module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL-ONLY$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser
11 ** General Public License version 2.1 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.LGPL included in the
13 ** packaging of this file.  Please review the following information to
14 ** ensure the GNU Lesser General Public License version 2.1 requirements
15 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** If you have questions regarding the use of this file, please contact
18 ** us via http://www.qt-project.org/.
19 **
20 ** $QT_END_LICENSE$
21 **
22 ****************************************************************************/
23
24 #ifndef QJSENGINE_H
25 #define QJSENGINE_H
26
27 #include <QtCore/qmetatype.h>
28
29 #include <QtCore/qvariant.h>
30 #include <QtCore/qsharedpointer.h>
31 #include <QtCore/qobject.h>
32 #include <QtDeclarative/qjsvalue.h>
33
34 QT_BEGIN_HEADER
35
36 QT_BEGIN_NAMESPACE
37
38
39 class QDateTime;
40 class QV8Engine;
41
42 class QRegExp;
43
44 template <typename T>
45 inline T qjsvalue_cast(const QJSValue &);
46
47 class QJSEnginePrivate;
48 class Q_DECLARATIVE_EXPORT QJSEngine
49     : public QObject
50 {
51     Q_OBJECT
52 public:
53 #ifdef QT_DEPRECATED
54     enum ContextOwnership {
55         AdoptCurrentContext,
56         CreateNewContext
57     };
58     QT_DEPRECATED explicit QJSEngine(ContextOwnership ownership);
59 #endif
60
61     QJSEngine();
62     explicit QJSEngine(QObject *parent);
63     virtual ~QJSEngine();
64
65     QJSValue globalObject() const;
66
67     QJSValue evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1);
68
69     QJSValue newObject();
70     QJSValue newArray(uint length = 0);
71
72     QJSValue newQObject(QObject *object);
73
74     template <typename T>
75     inline QJSValue toScriptValue(const T &value)
76     {
77         return create(qMetaTypeId<T>(), &value);
78     }
79     template <typename T>
80     inline T fromScriptValue(const QJSValue &value)
81     {
82         return qjsvalue_cast<T>(value);
83     }
84
85     void collectGarbage();
86
87     QV8Engine *handle() const { return d; }
88
89 #ifdef QT_DEPRECATED
90     QT_DEPRECATED bool hasUncaughtException() const;
91     QT_DEPRECATED QJSValue uncaughtException() const;
92     QT_DEPRECATED void clearExceptions();
93
94     QT_DEPRECATED QJSValue nullValue();
95     QT_DEPRECATED QJSValue undefinedValue();
96
97     QT_DEPRECATED QJSValue newVariant(const QVariant &value);
98
99     QT_DEPRECATED QJSValue newRegExp(const QRegExp &regexp);
100
101     QT_DEPRECATED QJSValue newRegExp(const QString &pattern, const QString &flags);
102     QT_DEPRECATED QJSValue newDate(double value);
103     QT_DEPRECATED QJSValue newDate(const QDateTime &value);
104
105     QT_DEPRECATED QJSValue toObject(const QJSValue &value);
106 #endif
107
108 Q_SIGNALS:
109     void signalHandlerException(const QJSValue &exception);
110
111 private:
112     QJSValue create(int type, const void *ptr);
113
114     static bool convertV2(const QJSValue &value, int type, void *ptr);
115
116     friend inline bool qjsvalue_cast_helper(const QJSValue &, int, void *);
117
118 protected:
119     QJSEngine(QJSEnginePrivate &dd, QObject *parent = 0);
120
121 private:
122     QV8Engine *d;
123     Q_DISABLE_COPY(QJSEngine)
124     Q_DECLARE_PRIVATE(QJSEngine)
125     friend class QV8Engine;
126 };
127
128 inline bool qjsvalue_cast_helper(const QJSValue &value, int type, void *ptr)
129 {
130     return QJSEngine::convertV2(value, type, ptr);
131 }
132
133 template<typename T>
134 T qjsvalue_cast(const QJSValue &value)
135 {
136     T t;
137     const int id = qMetaTypeId<T>();
138
139     if (qjsvalue_cast_helper(value, id, &t))
140         return t;
141     else if (value.isVariant())
142         return qvariant_cast<T>(value.toVariant());
143
144     return T();
145 }
146
147 template <>
148 inline QVariant qjsvalue_cast<QVariant>(const QJSValue &value)
149 {
150     return value.toVariant();
151 }
152
153 QT_END_NAMESPACE
154
155 QT_END_HEADER
156
157 #endif // QJSENGINE_H