1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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 https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qqml.h"
41
42#include <QtQml/qqmlprivate.h>
43
44#include <private/qqmlengine_p.h>
45#include <private/qqmlmetatype_p.h>
46#include <private/qqmlmetatypedata_p.h>
47#include <private/qqmltype_p_p.h>
48#include <private/qqmltypemodule_p_p.h>
49
50#include <QtCore/qmutex.h>
51
52QT_BEGIN_NAMESPACE
53
54void qmlClearTypeRegistrations() // Declared in qqml.h
55{
56 QQmlMetaType::clearTypeRegistrations();
57 QQmlEnginePrivate::baseModulesUninitialized = true; //So the engine re-registers its types
58 qmlClearEnginePlugins();
59}
60
61//From qqml.h
62bool qmlProtectModule(const char *uri, int majVersion)
63{
64 return QQmlMetaType::protectModule(uri, majVersion);
65}
66
67//From qqml.h
68void qmlRegisterModule(const char *uri, int versionMajor, int versionMinor)
69{
70 QQmlMetaType::registerModule(uri, versionMajor, versionMinor);
71}
72
73//From qqml.h
74int qmlTypeId(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
75{
76 return QQmlMetaType::typeId(uri, versionMajor, versionMinor, qmlName);
77}
78
79// From qqmlprivate.h
80QObject *QQmlPrivate::RegisterSingletonFunctor::operator()(QQmlEngine *qeng, QJSEngine *)
81{
82 if (qeng->thread() != m_object->thread()) {
83 QQmlError error;
84 error.setDescription(QLatin1String("Registered object must live in the same thread as the engine it was registered with"));
85 QQmlEnginePrivate::get(qeng)->warning(qeng, error);
86 return nullptr;
87 }
88 if (alreadyCalled) {
89 QQmlError error;
90 error.setDescription(QLatin1String("Singleton registered by registerSingletonInstance must only be accessed from one engine"));
91 QQmlEnginePrivate::get(qeng)->warning(qeng, error);
92 return nullptr;
93 }
94 alreadyCalled = true;
95 qeng->setObjectOwnership(m_object, QQmlEngine::CppOwnership);
96 return m_object;
97};
98
99/*
100This method is "over generalized" to allow us to (potentially) register more types of things in
101the future without adding exported symbols.
102*/
103int QQmlPrivate::qmlregister(RegistrationType type, void *data)
104{
105 if (type == AutoParentRegistration) {
106 return QQmlMetaType::registerAutoParentFunction(
107 *reinterpret_cast<RegisterAutoParent *>(data));
108 } else if (type == QmlUnitCacheHookRegistration) {
109 return QQmlMetaType::registerUnitCacheHook(
110 *reinterpret_cast<RegisterQmlUnitCacheHook *>(data));
111 }
112
113 QQmlType dtype;
114 if (type == TypeRegistration)
115 dtype = QQmlMetaType::registerType(*reinterpret_cast<RegisterType *>(data));
116 else if (type == InterfaceRegistration)
117 dtype = QQmlMetaType::registerInterface(*reinterpret_cast<RegisterInterface *>(data));
118 else if (type == SingletonRegistration)
119 dtype = QQmlMetaType::registerSingletonType(*reinterpret_cast<RegisterSingletonType *>(data));
120 else if (type == CompositeRegistration)
121 dtype = QQmlMetaType::registerCompositeType(*reinterpret_cast<RegisterCompositeType *>(data));
122 else if (type == CompositeSingletonRegistration)
123 dtype = QQmlMetaType::registerCompositeSingletonType(*reinterpret_cast<RegisterCompositeSingletonType *>(data));
124 else
125 return -1;
126
127 if (!dtype.isValid())
128 return -1;
129
130 QQmlMetaType::registerUndeletableType(dtype);
131 return dtype.index();
132}
133
134void QQmlPrivate::qmlunregister(RegistrationType type, quintptr data)
135{
136 switch (type) {
137 case AutoParentRegistration:
138 QQmlMetaType::unregisterAutoParentFunction(reinterpret_cast<AutoParentFunction>(data));
139 break;
140 case QmlUnitCacheHookRegistration:
141 QQmlMetaType::removeCachedUnitLookupFunction(
142 reinterpret_cast<QmlUnitCacheLookupFunction>(data));
143 break;
144 case TypeRegistration:
145 case InterfaceRegistration:
146 case SingletonRegistration:
147 case CompositeRegistration:
148 case CompositeSingletonRegistration:
149 QQmlMetaType::unregisterType(data);
150 break;
151 }
152}
153
154QT_END_NAMESPACE
155