6#include <private/qmetaobject_p.h>
7#include <private/qqmlabstractbinding_p.h>
8#include <private/qqmlboundsignal_p.h>
9#include <private/qqmlcontextdata_p.h>
10#include <private/qqmlnotifier_p.h>
11#include <private/qthread_p.h>
15QQmlData::QQmlData(Ownership ownership)
16 : ownMemory(ownership == OwnsMemory)
17 , indestructible(
true)
18 , explicitIndestructibleSet(
false)
19 , hasTaintedV4Object(
false)
20 , isQueuedForDeletion(
false)
21 , rootObjectInCreation(
false)
22 , hasInterceptorMetaObject(
false)
23 , hasVMEMetaObject(
false)
24 , hasConstWrapper(
false)
26 , bindingBitsArraySize(InlineBindingArraySize)
28 memset(bindingBitsValue, 0,
sizeof(bindingBitsValue));
32QQmlData::~QQmlData() =
default;
34void QQmlData::destroyed(QAbstractDeclarativeData *d, QObject *o)
36 QQmlData *ddata =
static_cast<QQmlData *>(d);
49 QMetaMethod method = target->metaObject()->method(methodIndex);
50 Q_ASSERT(method.methodType() == QMetaMethod::Signal);
51 int signalIndex = QMetaObjectPrivate::signalIndex(method);
52 QQmlData *ddata = QQmlData::get(target,
false);
54 if (ep) QQmlNotifier::emitNotify(ep, a);
62void QQmlData::signalEmitted(QAbstractDeclarativeData *, QObject *object,
int index,
void **a)
64 QQmlData *ddata = QQmlData::get(object,
false);
76 if (!ddata->notifyList.loadRelaxed())
79 auto objectThreadData = QObjectPrivate::get(object)->threadData.loadRelaxed();
80 if (QThread::currentThreadId() != objectThreadData->threadId.loadRelaxed()) {
81 if (!objectThreadData->thread.loadAcquire())
84 QMetaMethod m = QMetaObjectPrivate::signal(object->metaObject(), index);
85 const QList<QByteArray> parameterTypes = m.parameterTypes();
87 QVarLengthArray<
const QtPrivate::QMetaTypeInterface *, 16> argTypes;
88 argTypes.reserve(1 + parameterTypes.size());
89 argTypes.emplace_back(
nullptr);
90 for (
const QByteArray &typeName: parameterTypes) {
92 if (typeName.endsWith(
'*'))
93 type = QMetaType(QMetaType::VoidStar);
95 type = QMetaType::fromName(typeName);
97 if (!type.isValid()) {
98 qWarning(
"QObject::connect: Cannot queue arguments of type '%s'\n"
99 "(Make sure '%s' is registered using qRegisterMetaType().)",
100 typeName.constData(), typeName.constData());
104 argTypes.emplace_back(type.iface());
107 auto ev = std::make_unique<QQueuedMetaCallEvent>(m.methodIndex(), 0,
nullptr, object, index,
108 argTypes.size(), argTypes.data(), a);
110 QQmlThreadNotifierProxyObject *mpo =
new QQmlThreadNotifierProxyObject;
111 mpo->target = object;
112 mpo->moveToThread(objectThreadData->thread.loadAcquire());
113 QCoreApplication::postEvent(mpo, ev.release());
116 QQmlNotifierEndpoint *ep = ddata->notify(index);
117 if (ep) QQmlNotifier::emitNotify(ep, a);
121int QQmlData::receivers(QAbstractDeclarativeData *d,
const QObject *,
int index)
123 QQmlData *ddata =
static_cast<QQmlData *>(d);
124 return ddata->endpointCount(index);
127bool QQmlData::isSignalConnected(QAbstractDeclarativeData *d,
const QObject *,
int index)
129 QQmlData *ddata =
static_cast<QQmlData *>(d);
130 return ddata->signalHasEndpoint(index);
133int QQmlData::endpointCount(
int index)
136 QQmlNotifierEndpoint *ep = notify(index);
147void QQmlData::markAsDeleted(QObject *o)
149 QVarLengthArray<QObject *> workStack;
150 workStack.push_back(o);
151 while (!workStack.isEmpty()) {
152 auto currentObject = workStack.last();
153 workStack.pop_back();
154 QQmlData::setQueuedForDeletion(currentObject);
155 auto currentObjectPriv = QObjectPrivate::get(currentObject);
156 for (QObject *child: std::as_const(currentObjectPriv->children))
157 workStack.push_back(child);
161void QQmlData::setQueuedForDeletion(QObject *object)
164 if (QQmlData *ddata = QQmlData::get(object)) {
165 if (ddata->ownContext) {
166 Q_ASSERT(ddata->ownContext.data() == ddata->context);
167 ddata->ownContext->deepClearContextObject(object);
168 ddata->ownContext.reset();
169 ddata->context =
nullptr;
171 ddata->isQueuedForDeletion =
true;
178 ddata->disconnectNotifiers(DeleteNotifyList::No);
183void QQmlData::flushPendingBinding(
int coreIndex)
185 clearPendingBindingBit(coreIndex);
188 QQmlAbstractBinding *b = bindings;
189 while (b && (b->targetPropertyIndex().coreIndex() != coreIndex ||
190 b->targetPropertyIndex().hasValueTypeIndex()))
191 b = b->nextBinding();
193 if (b && b->targetPropertyIndex().coreIndex() == coreIndex &&
194 !b->targetPropertyIndex().hasValueTypeIndex())
195 b->setEnabled(
true, QQmlPropertyData::BypassInterceptor |
196 QQmlPropertyData::DontRemoveBinding);
199QQmlData::DeferredData::DeferredData() =
default;
200QQmlData::DeferredData::~DeferredData() =
default;
212void QQmlData::NotifyList::layout(QQmlNotifierEndpoint *endpoint)
216 endpoint->prev =
nullptr;
218 while (endpoint->next) {
219 Q_ASSERT(
reinterpret_cast<QQmlNotifierEndpoint *>(endpoint->next->prev) == endpoint);
220 endpoint = endpoint->next;
224 QQmlNotifierEndpoint *ep = (QQmlNotifierEndpoint *) endpoint->prev;
226 int index = endpoint->sourceSignal;
227 index = qMin(index, 0xFFFF - 1);
229 endpoint->next = notifies[index];
230 if (endpoint->next) endpoint->next->prev = &endpoint->next;
231 endpoint->prev = ¬ifies[index];
232 notifies[index] = endpoint;
238void QQmlData::NotifyList::layout()
240 Q_ASSERT(maximumTodoIndex >= notifiesSize);
243 QQmlNotifierEndpoint **old = notifies;
244 const int reallocSize = (maximumTodoIndex + 1) *
sizeof(QQmlNotifierEndpoint*);
245 notifies = (QQmlNotifierEndpoint**)realloc(notifies, reallocSize);
246 const int memsetSize = (maximumTodoIndex - notifiesSize + 1) *
247 sizeof(QQmlNotifierEndpoint*);
248 memset(notifies + notifiesSize, 0, memsetSize);
250 if (notifies != old) {
251 for (
int ii = 0; ii < notifiesSize; ++ii)
253 notifies[ii]->prev = ¬ifies[ii];
256 notifiesSize = maximumTodoIndex + 1;
261 maximumTodoIndex = 0;
265void QQmlData::deferData(
266 int objectIndex,
const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
267 const QQmlRefPointer<QQmlContextData> &context,
const QString &inlineComponentName)
269 QQmlData::DeferredData *deferData =
new QQmlData::DeferredData;
270 deferData->deferredIdx = objectIndex;
271 deferData->compilationUnit = compilationUnit;
272 deferData->context = context;
273 deferData->inlineComponentName = inlineComponentName;
275 const QV4::CompiledData::Object *compiledObject = compilationUnit->objectAt(objectIndex);
276 const QV4::CompiledData::BindingPropertyData *propertyData
277 = compilationUnit->bindingPropertyDataPerObjectAt(objectIndex);
279 const QV4::CompiledData::Binding *binding = compiledObject->bindingTable();
280 for (quint32 i = 0; i < compiledObject->nBindings; ++i, ++binding) {
281 const QQmlPropertyData *property = propertyData->at(i);
282 if (binding->hasFlag(QV4::CompiledData::Binding::IsDeferredBinding))
283 deferData->bindings.insert(property ? property->coreIndex() : -1, binding);
286 deferredData.append(deferData);
289void QQmlData::releaseDeferredData()
291 auto it = deferredData.begin();
292 while (it != deferredData.end()) {
293 DeferredData *deferData = *it;
294 if (deferData->bindings.isEmpty()) {
296 it = deferredData.erase(it);
303void QQmlData::addNotify(
int index, QQmlNotifierEndpoint *endpoint)
307 NotifyList *list = notifyList.loadRelaxed();
310 list =
new NotifyList;
316 notifyList.storeRelaxed(list);
319 Q_ASSERT(!endpoint->isConnected());
321 index = qMin(index, 0xFFFF - 1);
326 list->connectionMask.storeRelaxed(
327 list->connectionMask.loadRelaxed() | (1ULL << quint64(index % 64)));
329 if (index < list->notifiesSize) {
330 endpoint->next = list->notifies[index];
331 if (endpoint->next) endpoint->next->prev = &endpoint->next;
332 endpoint->prev = &list->notifies[index];
333 list->notifies[index] = endpoint;
335 list->maximumTodoIndex = qMax(
int(list->maximumTodoIndex), index);
337 endpoint->next = list->todo;
338 if (endpoint->next) endpoint->next->prev = &endpoint->next;
339 endpoint->prev = &list->todo;
340 list->todo = endpoint;
344void QQmlData::disconnectNotifiers(QQmlData::DeleteNotifyList doDelete)
347 if (NotifyList *list = notifyList.loadRelaxed()) {
348 while (QQmlNotifierEndpoint *todo = list->todo)
350 for (
int ii = 0; ii < list->notifiesSize; ++ii) {
351 while (QQmlNotifierEndpoint *ep = list->notifies[ii])
354 free(list->notifies);
356 if (doDelete == DeleteNotifyList::Yes) {
360 notifyList.storeRelaxed(
nullptr);
366 list->connectionMask.storeRelaxed(0);
367 list->maximumTodoIndex = 0;
368 list->notifiesSize = 0;
369 list->notifies =
nullptr;
375QHash<QQmlAttachedPropertiesFunc, QObject *> *QQmlData::attachedProperties()
const
377 if (!extendedData) extendedData =
new QQmlDataExtended;
378 return &extendedData->attachedProperties;
381void QQmlData::removeFromContext()
383 if (nextContextObject)
384 nextContextObject->prevContextObject = prevContextObject;
385 if (prevContextObject)
386 *prevContextObject = nextContextObject;
387 else if (outerContext && outerContext->ownedObjects() ==
this)
388 outerContext->setOwnedObjects(nextContextObject);
390 nextContextObject =
nullptr;
391 prevContextObject =
nullptr;
392 outerContext =
nullptr;
395void QQmlData::clearBindings()
397 if (QQmlAbstractBinding *binding = std::exchange(bindings,
nullptr)) {
398 for (QQmlAbstractBinding *next = binding; next; next = next->nextBinding())
399 next->setAddedToObject(
false);
400 if (!binding->ref.deref())
405bool QQmlData::clearSignalHandlers()
407 for (QQmlBoundSignal *signalHandler = std::exchange(signalHandlers,
nullptr); signalHandler;) {
408 if (signalHandler->isNotifying()) {
409 signalHandlers = signalHandler;
413 QQmlBoundSignal *next = signalHandler->m_nextSignal;
414 signalHandler->m_prevSignal =
nullptr;
415 signalHandler->m_nextSignal =
nullptr;
416 delete signalHandler;
417 signalHandler = next;
423void QQmlData::destroyed(QObject *object)
428 compilationUnit.reset();
429 qDeleteAll(deferredData);
430 deferredData.clear();
432 if (!clearSignalHandlers()) {
437 QString locationString;
438 QQmlBoundSignalExpression *expr = signalHandlers->expression();
440 QQmlSourceLocation location = expr->sourceLocation();
441 if (location.sourceFile.isEmpty())
442 location.sourceFile = QStringLiteral(
"<Unknown File>");
443 locationString.append(location.sourceFile);
444 locationString.append(QStringLiteral(
":%0: ").arg(location.line));
445 QString source = expr->expression();
446 if (source.size() > 100) {
448 source.append(QLatin1String(
" ..."));
450 locationString.append(source);
452 locationString = QStringLiteral(
"<Unknown Location>");
454 qFatal(
"Object %p destroyed while one of its QML signal handlers is in progress.\n"
455 "Most likely the object was deleted synchronously (use QObject::deleteLater() "
456 "instead), or the application is running a nested event loop.\n"
457 "This behavior is NOT supported!\n"
458 "%s", object, qPrintable(locationString));
461 if (bindingBitsArraySize > InlineBindingArraySize)
465 propertyCache.reset();
470 auto *guard = guards;
471 guard->setObject(
nullptr);
472 if (guard->objectDestroyed)
473 guard->objectDestroyed(guard);
476 disconnectNotifiers(DeleteNotifyList::Yes);
490QQmlData::BindingBitsType *QQmlData::growBits(QObject *obj,
int bit)
492 BindingBitsType *bits = (bindingBitsArraySize == InlineBindingArraySize) ? bindingBitsValue : bindingBits;
493 int props = QQmlMetaObject(obj).propertyCount();
494 Q_ASSERT(bit < 2 * props);
497 uint arraySize = (2 *
static_cast<uint>(props) + BitsPerType - 1) / BitsPerType;
498 Q_ASSERT(arraySize > 1);
499 Q_ASSERT(arraySize <= 0xffff);
501 BindingBitsType *newBits =
static_cast<BindingBitsType *>(malloc(arraySize*
sizeof(BindingBitsType)));
502 memcpy(newBits, bits, bindingBitsArraySize *
sizeof(BindingBitsType));
503 memset(newBits + bindingBitsArraySize, 0,
sizeof(BindingBitsType) * (arraySize - bindingBitsArraySize));
505 if (bindingBitsArraySize > InlineBindingArraySize)
507 bindingBits = newBits;
509 bindingBitsArraySize = arraySize;
513QQmlData *QQmlData::createQQmlData(QObjectPrivate *priv)
516 Q_ASSERT(!priv->isDeletingChildren);
517 priv->declarativeData =
new QQmlData(OwnsMemory);
518 return static_cast<QQmlData *>(priv->declarativeData);
521QQmlPropertyCache::ConstPtr QQmlData::createPropertyCache(QObject *object)
523 QQmlData *ddata = QQmlData::get(object,
true);
524 ddata->propertyCache = QQmlMetaType::propertyCache(object, QTypeRevision {});
525 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.