7#include <private/qmetaobject_p.h>
8#include <private/qqmlabstractbinding_p.h>
9#include <private/qqmlboundsignal_p.h>
10#include <private/qqmlcontextdata_p.h>
11#include <private/qqmlnotifier_p.h>
13#include <QtCore/qtyperevision.h>
14#include <private/qthread_p.h>
18QQmlData::QQmlData(Ownership ownership)
19 : ownMemory(ownership == OwnsMemory)
20 , indestructible(
true)
21 , explicitIndestructibleSet(
false)
22 , hasTaintedV4Object(
false)
23 , isQueuedForDeletion(
false)
24 , rootObjectInCreation(
false)
25 , hasInterceptorMetaObject(
false)
26 , hasVMEMetaObject(
false)
27 , hasConstWrapper(
false)
29 , bindingBitsArraySize(InlineBindingArraySize)
31 memset(bindingBitsValue, 0,
sizeof(bindingBitsValue));
35QQmlData::~QQmlData() =
default;
37void QQmlData::destroyed(QAbstractDeclarativeData *d, QObject *o)
39 QQmlData *ddata =
static_cast<QQmlData *>(d);
52 QMetaMethod method = target->metaObject()->method(methodIndex);
53 Q_ASSERT(method.methodType() == QMetaMethod::Signal);
54 int signalIndex = QMetaObjectPrivate::signalIndex(method);
55 QQmlData *ddata = QQmlData::get(target,
false);
57 if (ep) QQmlNotifier::emitNotify(ep, a);
65void QQmlData::signalEmitted(QAbstractDeclarativeData *, QObject *object,
int index,
void **a)
67 QQmlData *ddata = QQmlData::get(object,
false);
79 if (!ddata->notifyList.loadRelaxed())
82 auto objectThreadData = QObjectPrivate::get(object)->threadData.loadRelaxed();
83 if (QThread::currentThreadId() != objectThreadData->threadId.loadRelaxed()) {
84 if (!objectThreadData->thread.loadAcquire())
87 QMetaMethod m = QMetaObjectPrivate::signal(object->metaObject(), index);
88 const QList<QByteArray> parameterTypes = m.parameterTypes();
90 QVarLengthArray<
const QtPrivate::QMetaTypeInterface *, 16> argTypes;
91 argTypes.reserve(1 + parameterTypes.size());
92 argTypes.emplace_back(
nullptr);
93 for (
const QByteArray &typeName: parameterTypes) {
95 if (typeName.endsWith(
'*'))
96 type = QMetaType(QMetaType::VoidStar);
98 type = QMetaType::fromName(typeName);
100 if (!type.isValid()) {
101 qWarning(
"QObject::connect: Cannot queue arguments of type '%s'\n"
102 "(Make sure '%s' is registered using qRegisterMetaType().)",
103 typeName.constData(), typeName.constData());
107 argTypes.emplace_back(type.iface());
110 auto ev = std::make_unique<QQueuedMetaCallEvent>(m.methodIndex(), 0,
nullptr, object, index,
111 argTypes.size(), argTypes.data(), a);
113 QQmlThreadNotifierProxyObject *mpo =
new QQmlThreadNotifierProxyObject;
114 mpo->target = object;
115 mpo->moveToThread(objectThreadData->thread.loadAcquire());
116 QCoreApplication::postEvent(mpo, ev.release());
119 QQmlNotifierEndpoint *ep = ddata->notify(index);
120 if (ep) QQmlNotifier::emitNotify(ep, a);
124int QQmlData::receivers(QAbstractDeclarativeData *d,
const QObject *,
int index)
126 QQmlData *ddata =
static_cast<QQmlData *>(d);
127 return ddata->endpointCount(index);
130bool QQmlData::isSignalConnected(QAbstractDeclarativeData *d,
const QObject *,
int index)
132 QQmlData *ddata =
static_cast<QQmlData *>(d);
133 return ddata->signalHasEndpoint(index);
136int QQmlData::endpointCount(
int index)
139 QQmlNotifierEndpoint *ep = notify(index);
150void QQmlData::markAsDeleted(QObject *o)
152 QVarLengthArray<QObject *> workStack;
153 workStack.push_back(o);
154 while (!workStack.isEmpty()) {
155 auto currentObject = workStack.last();
156 workStack.pop_back();
157 QQmlData::setQueuedForDeletion(currentObject);
158 auto currentObjectPriv = QObjectPrivate::get(currentObject);
159 for (QObject *child: std::as_const(currentObjectPriv->children))
160 workStack.push_back(child);
164void QQmlData::setQueuedForDeletion(QObject *object)
167 if (QQmlData *ddata = QQmlData::get(object)) {
168 if (ddata->ownContext) {
169 Q_ASSERT(ddata->ownContext.data() == ddata->context);
170 ddata->ownContext->deepClearContextObject(object);
171 ddata->ownContext.reset();
172 ddata->context =
nullptr;
174 ddata->isQueuedForDeletion =
true;
181 ddata->disconnectNotifiers(DeleteNotifyList::No);
186void QQmlData::flushPendingBinding(
int coreIndex)
188 clearPendingBindingBit(coreIndex);
191 QQmlAbstractBinding *b = bindings;
192 while (b && (b->targetPropertyIndex().coreIndex() != coreIndex ||
193 b->targetPropertyIndex().hasValueTypeIndex()))
194 b = b->nextBinding();
196 if (b && b->targetPropertyIndex().coreIndex() == coreIndex &&
197 !b->targetPropertyIndex().hasValueTypeIndex())
198 b->setEnabled(
true, QQmlPropertyData::BypassInterceptor |
199 QQmlPropertyData::DontRemoveBinding);
202QQmlData::DeferredData::DeferredData() =
default;
203QQmlData::DeferredData::~DeferredData() =
default;
215void QQmlData::NotifyList::layout(QQmlNotifierEndpoint *endpoint)
219 endpoint->prev =
nullptr;
221 while (endpoint->next) {
222 Q_ASSERT(
reinterpret_cast<QQmlNotifierEndpoint *>(endpoint->next->prev) == endpoint);
223 endpoint = endpoint->next;
227 QQmlNotifierEndpoint *ep = (QQmlNotifierEndpoint *) endpoint->prev;
229 int index = endpoint->sourceSignal;
230 index = qMin(index, 0xFFFF - 1);
232 endpoint->next = notifies[index];
233 if (endpoint->next) endpoint->next->prev = &endpoint->next;
234 endpoint->prev = ¬ifies[index];
235 notifies[index] = endpoint;
241void QQmlData::NotifyList::layout()
243 Q_ASSERT(maximumTodoIndex >= notifiesSize);
246 QQmlNotifierEndpoint **old = notifies;
247 const int reallocSize = (maximumTodoIndex + 1) *
sizeof(QQmlNotifierEndpoint*);
248 notifies = (QQmlNotifierEndpoint**)realloc(notifies, reallocSize);
249 const int memsetSize = (maximumTodoIndex - notifiesSize + 1) *
250 sizeof(QQmlNotifierEndpoint*);
251 memset(notifies + notifiesSize, 0, memsetSize);
253 if (notifies != old) {
254 for (
int ii = 0; ii < notifiesSize; ++ii)
256 notifies[ii]->prev = ¬ifies[ii];
259 notifiesSize = maximumTodoIndex + 1;
264 maximumTodoIndex = 0;
268void QQmlData::deferData(
269 int objectIndex,
const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
270 const QQmlRefPointer<QQmlContextData> &context,
const QString &inlineComponentName)
272 QQmlData::DeferredData *deferData =
new QQmlData::DeferredData;
273 deferData->deferredIdx = objectIndex;
274 deferData->compilationUnit = compilationUnit;
275 deferData->context = context;
276 deferData->inlineComponentName = inlineComponentName;
278 const QV4::CompiledData::Object *compiledObject = compilationUnit->objectAt(objectIndex);
279 const QV4::CompiledData::BindingPropertyData *propertyData
280 = compilationUnit->bindingPropertyDataPerObjectAt(objectIndex);
282 const QV4::CompiledData::Binding *binding = compiledObject->bindingTable();
283 for (quint32 i = 0; i < compiledObject->nBindings; ++i, ++binding) {
284 const QQmlPropertyData *property = propertyData->at(i);
285 if (binding->hasFlag(QV4::CompiledData::Binding::IsDeferredBinding))
286 deferData->bindings.insert(property ? property->coreIndex() : -1, binding);
289 deferredData.append(deferData);
292void QQmlData::releaseDeferredData()
294 auto it = deferredData.begin();
295 while (it != deferredData.end()) {
296 DeferredData *deferData = *it;
297 if (deferData->bindings.isEmpty()) {
299 it = deferredData.erase(it);
306void QQmlData::addNotify(
int index, QQmlNotifierEndpoint *endpoint)
310 NotifyList *list = notifyList.loadRelaxed();
313 list =
new NotifyList;
319 notifyList.storeRelaxed(list);
322 Q_ASSERT(!endpoint->isConnected());
324 index = qMin(index, 0xFFFF - 1);
329 list->connectionMask.storeRelaxed(
330 list->connectionMask.loadRelaxed() | (1ULL << quint64(index % 64)));
332 if (index < list->notifiesSize) {
333 endpoint->next = list->notifies[index];
334 if (endpoint->next) endpoint->next->prev = &endpoint->next;
335 endpoint->prev = &list->notifies[index];
336 list->notifies[index] = endpoint;
338 list->maximumTodoIndex = qMax(
int(list->maximumTodoIndex), index);
340 endpoint->next = list->todo;
341 if (endpoint->next) endpoint->next->prev = &endpoint->next;
342 endpoint->prev = &list->todo;
343 list->todo = endpoint;
347void QQmlData::disconnectNotifiers(QQmlData::DeleteNotifyList doDelete)
350 if (NotifyList *list = notifyList.loadRelaxed()) {
351 while (QQmlNotifierEndpoint *todo = list->todo)
353 for (
int ii = 0; ii < list->notifiesSize; ++ii) {
354 while (QQmlNotifierEndpoint *ep = list->notifies[ii])
357 free(list->notifies);
359 if (doDelete == DeleteNotifyList::Yes) {
363 notifyList.storeRelaxed(
nullptr);
369 list->connectionMask.storeRelaxed(0);
370 list->maximumTodoIndex = 0;
371 list->notifiesSize = 0;
372 list->notifies =
nullptr;
378QHash<QQmlAttachedPropertiesFunc, QObject *> *QQmlData::attachedProperties()
const
380 if (!extendedData) extendedData =
new QQmlDataExtended;
381 return &extendedData->attachedProperties;
384void QQmlData::removeFromContext()
386 if (nextContextObject)
387 nextContextObject->prevContextObject = prevContextObject;
388 if (prevContextObject)
389 *prevContextObject = nextContextObject;
390 else if (outerContext && outerContext->ownedObjects() ==
this)
391 outerContext->setOwnedObjects(nextContextObject);
393 nextContextObject =
nullptr;
394 prevContextObject =
nullptr;
395 outerContext =
nullptr;
398void QQmlData::clearBindings()
400 if (QQmlAbstractBinding *binding = std::exchange(bindings,
nullptr)) {
401 for (QQmlAbstractBinding *next = binding; next; next = next->nextBinding())
402 next->setAddedToObject(
false);
403 if (!binding->ref.deref())
408bool QQmlData::clearSignalHandlers()
410 for (QQmlBoundSignal *signalHandler = std::exchange(signalHandlers,
nullptr); signalHandler;) {
411 if (signalHandler->isNotifying()) {
412 signalHandlers = signalHandler;
416 QQmlBoundSignal *next = signalHandler->m_nextSignal;
417 signalHandler->m_prevSignal =
nullptr;
418 signalHandler->m_nextSignal =
nullptr;
419 delete signalHandler;
420 signalHandler = next;
426void QQmlData::destroyed(QObject *object)
431 compilationUnit.reset();
432 qDeleteAll(deferredData);
433 deferredData.clear();
435 if (!clearSignalHandlers()) {
440 QString locationString;
441 QQmlBoundSignalExpression *expr = signalHandlers->expression();
443 QQmlSourceLocation location = expr->sourceLocation();
444 if (location.sourceFile.isEmpty())
445 location.sourceFile = QStringLiteral(
"<Unknown File>");
446 locationString.append(location.sourceFile);
447 locationString.append(QStringLiteral(
":%0: ").arg(location.line));
448 QString source = expr->expression();
449 if (source.size() > 100) {
451 source.append(QLatin1String(
" ..."));
453 locationString.append(source);
455 locationString = QStringLiteral(
"<Unknown Location>");
457 qFatal(
"Object %p destroyed while one of its QML signal handlers is in progress.\n"
458 "Most likely the object was deleted synchronously (use QObject::deleteLater() "
459 "instead), or the application is running a nested event loop.\n"
460 "This behavior is NOT supported!\n"
461 "%s", object, qPrintable(locationString));
464 if (bindingBitsArraySize > InlineBindingArraySize)
468 propertyCache.reset();
473 auto *guard = guards;
474 guard->setObject(
nullptr);
475 if (guard->objectDestroyed)
476 guard->objectDestroyed(guard);
479 disconnectNotifiers(DeleteNotifyList::Yes);
493QQmlData::BindingBitsType *QQmlData::growBits(QObject *obj,
int bit)
495 BindingBitsType *bits = (bindingBitsArraySize == InlineBindingArraySize) ? bindingBitsValue : bindingBits;
496 int props = QQmlMetaObject(obj).propertyCount();
497 Q_ASSERT(bit < 2 * props);
500 uint arraySize = (2 *
static_cast<uint>(props) + BitsPerType - 1) / BitsPerType;
501 Q_ASSERT(arraySize > 1);
502 Q_ASSERT(arraySize <= 0xffff);
504 BindingBitsType *newBits =
static_cast<BindingBitsType *>(malloc(arraySize*
sizeof(BindingBitsType)));
505 memcpy(newBits, bits, bindingBitsArraySize *
sizeof(BindingBitsType));
506 memset(newBits + bindingBitsArraySize, 0,
sizeof(BindingBitsType) * (arraySize - bindingBitsArraySize));
508 if (bindingBitsArraySize > InlineBindingArraySize)
510 bindingBits = newBits;
512 bindingBitsArraySize = arraySize;
516QQmlData *QQmlData::createQQmlData(QObjectPrivate *priv)
519 Q_ASSERT(!priv->isDeletingChildren);
520 priv->declarativeData =
new QQmlData(OwnsMemory);
521 return static_cast<QQmlData *>(priv->declarativeData);
524QQmlPropertyCache::ConstPtr QQmlData::createPropertyCache(QObject *object)
526 QQmlData *ddata = QQmlData::get(object,
true);
527 ddata->propertyCache = QQmlMetaType::propertyCache(object, QTypeRevision {});
528 return ddata->propertyCache;
QHash< QQmlAttachedPropertiesFunc, QObject * > attachedProperties
~QQmlDataExtended()=default
QPointer< QObject > target
int qt_metacall(QMetaObject::Call, int methodIndex, void **a) override
Combined button and popup list for selecting options.