1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL21$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
32 ****************************************************************************/
33 #ifndef QMLJS_MANAGED_H
34 #define QMLJS_MANAGED_H
36 #include "qv4global_p.h"
37 #include "qv4value_p.h"
38 #include <private/qv4heap_p.h>
44 #define Q_MANAGED_CHECK \
45 template <typename _T> inline void qt_check_for_QMANAGED_macro(const _T *_q_argument) const \
46 { int i = qYouForgotTheQ_MANAGED_Macro(this, _q_argument); i = i + 1; }
49 inline int qYouForgotTheQ_MANAGED_Macro(T, T) { return 0; }
51 template <typename T1, typename T2>
52 inline void qYouForgotTheQ_MANAGED_Macro(T1, T2) {}
54 #ifdef Q_COMPILER_STATIC_ASSERT
55 #define V4_MANAGED_SIZE_TEST void __dataTest() { Q_STATIC_ASSERT(sizeof(*this) == sizeof(Managed)); }
57 #define V4_MANAGED_SIZE_TEST
60 #define V4_NEEDS_DESTROY static void destroy(QV4::Heap::Base *b) { static_cast<Data *>(b)->~Data(); }
63 #define V4_MANAGED(DataClass, superClass) \
66 typedef QV4::Heap::DataClass Data; \
67 typedef superClass SuperClass; \
68 static const QV4::VTable static_vtbl; \
69 static inline const QV4::VTable *staticVTable() { return &static_vtbl; } \
70 V4_MANAGED_SIZE_TEST \
71 QV4::Heap::DataClass *d() const { return static_cast<QV4::Heap::DataClass *>(m()); }
73 #define Q_MANAGED_TYPE(type) \
75 enum { MyType = Type_##type };
77 #define Q_VTABLE_FUNCTION(classname, func) \
78 (classname::func == QV4::Managed::func ? 0 : classname::func)
81 #define DEFINE_MANAGED_VTABLE_INT(classname, parentVTable) \
84 classname::IsExecutionContext, \
85 classname::IsString, \
86 classname::IsObject, \
87 classname::IsFunctionObject, \
88 classname::IsErrorObject, \
89 classname::IsArrayData, \
93 Q_VTABLE_FUNCTION(classname, destroy), \
98 #define DEFINE_MANAGED_VTABLE(classname) \
99 const QV4::VTable classname::static_vtbl = DEFINE_MANAGED_VTABLE_INT(classname, 0)
101 struct Q_QML_PRIVATE_EXPORT Managed : Value
103 V4_MANAGED(Base, Managed)
105 IsExecutionContext = false,
108 IsFunctionObject = false,
109 IsErrorObject = false,
113 void *operator new(size_t);
114 Managed() Q_DECL_EQ_DELETE;
115 Q_DISABLE_COPY(Managed)
131 Type_ArgumentsObject,
135 Type_ExecutionContext,
136 Type_ForeachIteratorObject,
141 Q_MANAGED_TYPE(Invalid)
143 bool isListType() const { return d()->vtable()->type == Type_QmlSequence; }
145 bool isArrayObject() const { return d()->vtable()->type == Type_ArrayObject; }
146 bool isStringObject() const { return d()->vtable()->type == Type_StringObject; }
148 QString className() const;
150 bool isEqualTo(const Managed *other) const
151 { return d()->vtable()->isEqualTo(const_cast<Managed *>(this), const_cast<Managed *>(other)); }
153 static bool isEqualTo(Managed *m, Managed *other);
155 bool inUse() const { return d()->inUse(); }
156 bool markBit() const { return d()->isMarked(); }
158 static void destroy(Heap::Base *) {}
160 friend class MemoryManager;
161 friend struct Identifiers;
162 friend struct ObjectIterator;
167 inline const Managed *Value::as() const {
174 inline const Object *Value::as() const {
175 return isManaged() && m() && m()->vtable()->isObject ? objectValue() : 0;