Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmltcobjectcreationhelper_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#ifndef QQMLTCOBJECTCREATIONHELPER_P_H
6#define QQMLTCOBJECTCREATIONHELPER_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtQml/qqml.h>
20#include <QtCore/private/qglobal_p.h>
21#include <QtCore/qversionnumber.h>
22#include <private/qtqmlglobal_p.h>
23#include <private/qqmltype_p.h>
24
25#include <array>
26
27QT_BEGIN_NAMESPACE
28
29/*!
30 \internal
31
32 (Kind of) type-erased object creation utility that can be used throughout
33 the generated C++ code. By nature it shows relative data to the current QML
34 document and allows to get and set object pointers.
35 */
36class QQmltcObjectCreationHelper
37{
38 QObject **m_data = nullptr; // QObject* array
39 const qsizetype m_size = 0; // size of m_data array, exists for bounds checking
40 const qsizetype m_offset = 0; // global offset into m_data array
41
42 qsizetype offset() const { return m_offset; }
43
44public:
45 /*!
46 Constructs initial "view" from basic data. Supposed to only be called
47 once from QQmltcObjectCreationBase.
48 */
49 QQmltcObjectCreationHelper(QObject **data, qsizetype size) : m_data(data), m_size(size)
50 {
51 Q_UNUSED(m_size);
52 }
53
54 /*!
55 Constructs new "view" from \a base view, adding \a localOffset to the
56 offset of that base.
57 */
58 QQmltcObjectCreationHelper(const QQmltcObjectCreationHelper *base, qsizetype localOffset)
59 : m_data(base->m_data), m_size(base->m_size), m_offset(base->m_offset + localOffset)
60 {
61 }
62
63 template<typename T>
64 T *get(qsizetype i) const
65 {
66 Q_ASSERT(m_data);
67 Q_ASSERT(i >= 0 && i + offset() < m_size);
68 Q_ASSERT(qobject_cast<T *>(m_data[i + offset()]) != nullptr);
69 // Note: perform cheap cast as we know *exactly* the real type of the
70 // object
71 return static_cast<T *>(m_data[i + offset()]);
72 }
73
74 void set(qsizetype i, QObject *object)
75 {
76 Q_ASSERT(m_data);
77 Q_ASSERT(i >= 0 && i + offset() < m_size);
78 Q_ASSERT(m_data[i + offset()] == nullptr); // prevent accidental resets
79 m_data[i + offset()] = object;
80 }
81
82 template<typename T>
83 static constexpr uint typeCount() noexcept
84 {
85 return T::q_qmltc_typeCount();
86 }
87};
88
89/*!
90 \internal
91
92 Base helper for qmltc-generated types that linearly stores pointers to all
93 the to-be-created objects for fast access during object creation.
94 */
95template<typename QmltcGeneratedType>
97{
98 // Note: +1 for the document root itself
99 std::array<QObject *, QmltcGeneratedType::q_qmltc_typeCount() + 1> m_objects = {};
100
101public:
102 QQmltcObjectCreationHelper view()
103 {
104 return QQmltcObjectCreationHelper(m_objects.data(), m_objects.size());
105 }
106};
107
109{
112 const QMetaObject *metaObject = nullptr;
113
114 template<typename QmltcGeneratedType>
115 QmltcTypeData(QmltcGeneratedType *)
116 : allocationSize(sizeof(QmltcGeneratedType)),
118 {
119 }
120};
121
122Q_QML_EXPORT void qmltcCreateDynamicMetaObject(QObject *object, const QmltcTypeData &data);
123
124QT_END_NAMESPACE
125
126#endif // QQMLTCOBJECTCREATIONHELPER_P_H
QQmltcObjectCreationHelper view()
Combined button and popup list for selecting options.
QT_BEGIN_NAMESPACE void qmltcCreateDynamicMetaObject(QObject *object, const QmltcTypeData &data)
QQmlType::RegistrationType regType
const QMetaObject * metaObject
QmltcTypeData(QmltcGeneratedType *)