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>
12#include <private/qthread_p.h>
16QQmlData::QQmlData(Ownership ownership)
17 : ownMemory(ownership == OwnsMemory)
18 , indestructible(
true)
19 , explicitIndestructibleSet(
false)
20 , hasTaintedV4Object(
false)
21 , isQueuedForDeletion(
false)
22 , rootObjectInCreation(
false)
23 , hasInterceptorMetaObject(
false)
24 , hasVMEMetaObject(
false)
25 , hasConstWrapper(
false)
27 , bindingBitsArraySize(InlineBindingArraySize)
29 memset(bindingBitsValue, 0,
sizeof(bindingBitsValue));
33QQmlData::~QQmlData() =
default;
35void QQmlData::destroyed(QAbstractDeclarativeData *d, QObject *o)
37 QQmlData *ddata =
static_cast<QQmlData *>(d);
50 QMetaMethod method = target->metaObject()->method(methodIndex);
51 Q_ASSERT(method.methodType() == QMetaMethod::Signal);
52 int signalIndex = QMetaObjectPrivate::signalIndex(method);
53 QQmlData *ddata = QQmlData::get(target,
false);
55 if (ep) QQmlNotifier::emitNotify(ep, a);
63void QQmlData::signalEmitted(QAbstractDeclarativeData *, QObject *object,
int index,
void **a)
65 QQmlData *ddata = QQmlData::get(object,
false);
77 if (!ddata->notifyList.loadRelaxed())
80 auto objectThreadData = QObjectPrivate::get(object)->threadData.loadRelaxed();
81 if (QThread::currentThreadId() != objectThreadData->threadId.loadRelaxed()) {
82 if (!objectThreadData->thread.loadAcquire())
85 QMetaMethod m = QMetaObjectPrivate::signal(object->metaObject(), index);
86 const QList<QByteArray> parameterTypes = m.parameterTypes();
88 QVarLengthArray<
const QtPrivate::QMetaTypeInterface *, 16> argTypes;
89 argTypes.reserve(1 + parameterTypes.size());
90 argTypes.emplace_back(
nullptr);
91 for (
const QByteArray &typeName: parameterTypes) {
93 if (typeName.endsWith(
'*'))
94 type = QMetaType(QMetaType::VoidStar);
96 type = QMetaType::fromName(typeName);
98 if (!type.isValid()) {
99 qWarning(
"QObject::connect: Cannot queue arguments of type '%s'\n"
100 "(Make sure '%s' is registered using qRegisterMetaType().)",
101 typeName.constData(), typeName.constData());
105 argTypes.emplace_back(type.iface());
108 auto ev = std::make_unique<QQueuedMetaCallEvent>(m.methodIndex(), 0,
nullptr, object, index,
109 argTypes.size(), argTypes.data(), a);
111 QQmlThreadNotifierProxyObject *mpo =
new QQmlThreadNotifierProxyObject;
112 mpo->target = object;
113 mpo->moveToThread(objectThreadData->thread.loadAcquire());
114 QCoreApplication::postEvent(mpo, ev.release());
117 QQmlNotifierEndpoint *ep = ddata->notify(index);
118 if (ep) QQmlNotifier::emitNotify(ep, a);
122int QQmlData::receivers(QAbstractDeclarativeData *d,
const QObject *,
int index)
124 QQmlData *ddata =
static_cast<QQmlData *>(d);
125 return ddata->endpointCount(index);
128bool QQmlData::isSignalConnected(QAbstractDeclarativeData *d,
const QObject *,
int index)
130 QQmlData *ddata =
static_cast<QQmlData *>(d);
131 return ddata->signalHasEndpoint(index);
134int QQmlData::endpointCount(
int index)
137 QQmlNotifierEndpoint *ep = notify(index);
148void QQmlData::markAsDeleted(QObject *o)
150 QVarLengthArray<QObject *> workStack;
151 workStack.push_back(o);
152 while (!workStack.isEmpty()) {
153 auto currentObject = workStack.last();
154 workStack.pop_back();
155 QQmlData::setQueuedForDeletion(currentObject);
156 auto currentObjectPriv = QObjectPrivate::get(currentObject);
157 for (QObject *child: std::as_const(currentObjectPriv->children))
158 workStack.push_back(child);
162void QQmlData::setQueuedForDeletion(QObject *object)
165 if (QQmlData *ddata = QQmlData::get(object)) {
166 if (ddata->ownContext) {
167 Q_ASSERT(ddata->ownContext.data() == ddata->context);
168 ddata->ownContext->deepClearContextObject(object);
169 ddata->ownContext.reset();
170 ddata->context =
nullptr;
172 ddata->isQueuedForDeletion =
true;
179 ddata->disconnectNotifiers(DeleteNotifyList::No);
184void QQmlData::flushPendingBinding(
int coreIndex)
186 clearPendingBindingBit(coreIndex);
189 QQmlAbstractBinding *b = bindings;
190 while (b && (b->targetPropertyIndex().coreIndex() != coreIndex ||
191 b->targetPropertyIndex().hasValueTypeIndex()))
192 b = b->nextBinding();
194 if (b && b->targetPropertyIndex().coreIndex() == coreIndex &&
195 !b->targetPropertyIndex().hasValueTypeIndex())
196 b->setEnabled(
true, QQmlPropertyData::BypassInterceptor |
197 QQmlPropertyData::DontRemoveBinding);
200QQmlData::DeferredData::DeferredData() =
default;
201QQmlData::DeferredData::~DeferredData() =
default;
213void QQmlData::NotifyList::layout(QQmlNotifierEndpoint *endpoint)
217 endpoint->prev =
nullptr;
219 while (endpoint->next) {
220 Q_ASSERT(
reinterpret_cast<QQmlNotifierEndpoint *>(endpoint->next->prev) == endpoint);
221 endpoint = endpoint->next;
225 QQmlNotifierEndpoint *ep = (QQmlNotifierEndpoint *) endpoint->prev;
227 int index = endpoint->sourceSignal;
228 index = qMin(index, 0xFFFF - 1);
230 endpoint->next = notifies[index];
231 if (endpoint->next) endpoint->next->prev = &endpoint->next;
232 endpoint->prev = ¬ifies[index];
233 notifies[index] = endpoint;
239void QQmlData::NotifyList::layout()
241 Q_ASSERT(maximumTodoIndex >= notifiesSize);
244 QQmlNotifierEndpoint **old = notifies;
245 const int reallocSize = (maximumTodoIndex + 1) *
sizeof(QQmlNotifierEndpoint*);
246 notifies = (QQmlNotifierEndpoint**)realloc(notifies, reallocSize);
247 const int memsetSize = (maximumTodoIndex - notifiesSize + 1) *
248 sizeof(QQmlNotifierEndpoint*);
249 memset(notifies + notifiesSize, 0, memsetSize);
251 if (notifies != old) {
252 for (
int ii = 0; ii < notifiesSize; ++ii)
254 notifies[ii]->prev = ¬ifies[ii];
257 notifiesSize = maximumTodoIndex + 1;
262 maximumTodoIndex = 0;
266void QQmlData::deferData(
267 int objectIndex,
const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
268 const QQmlRefPointer<QQmlContextData> &context,
const QString &inlineComponentName)
270 QQmlData::DeferredData *deferData =
new QQmlData::DeferredData;
271 deferData->deferredIdx = objectIndex;
272 deferData->compilationUnit = compilationUnit;
273 deferData->context = context;
274 deferData->inlineComponentName = inlineComponentName;
276 const QV4::CompiledData::Object *compiledObject = compilationUnit->objectAt(objectIndex);
277 const QV4::CompiledData::BindingPropertyData *propertyData
278 = compilationUnit->bindingPropertyDataPerObjectAt(objectIndex);
280 const QV4::CompiledData::Binding *binding = compiledObject->bindingTable();
281 for (quint32 i = 0; i < compiledObject->nBindings; ++i, ++binding) {
282 const QQmlPropertyData *property = propertyData->at(i);
283 if (binding->hasFlag(QV4::CompiledData::Binding::IsDeferredBinding))
284 deferData->bindings.insert(property ? property->coreIndex() : -1, binding);
287 deferredData.append(deferData);
290void QQmlData::releaseDeferredData()
292 auto it = deferredData.begin();
293 while (it != deferredData.end()) {
294 DeferredData *deferData = *it;
295 if (deferData->bindings.isEmpty()) {
297 it = deferredData.erase(it);
304void QQmlData::addNotify(
int index, QQmlNotifierEndpoint *endpoint)
308 NotifyList *list = notifyList.loadRelaxed();
311 list =
new NotifyList;
317 notifyList.storeRelaxed(list);
320 Q_ASSERT(!endpoint->isConnected());
322 index = qMin(index, 0xFFFF - 1);
327 list->connectionMask.storeRelaxed(
328 list->connectionMask.loadRelaxed() | (1ULL << quint64(index % 64)));
330 if (index < list->notifiesSize) {
331 endpoint->next = list->notifies[index];
332 if (endpoint->next) endpoint->next->prev = &endpoint->next;
333 endpoint->prev = &list->notifies[index];
334 list->notifies[index] = endpoint;
336 list->maximumTodoIndex = qMax(
int(list->maximumTodoIndex), index);
338 endpoint->next = list->todo;
339 if (endpoint->next) endpoint->next->prev = &endpoint->next;
340 endpoint->prev = &list->todo;
341 list->todo = endpoint;
345void QQmlData::disconnectNotifiers(QQmlData::DeleteNotifyList doDelete)
348 if (NotifyList *list = notifyList.loadRelaxed()) {
349 while (QQmlNotifierEndpoint *todo = list->todo)
351 for (
int ii = 0; ii < list->notifiesSize; ++ii) {
352 while (QQmlNotifierEndpoint *ep = list->notifies[ii])
355 free(list->notifies);
357 if (doDelete == DeleteNotifyList::Yes) {
361 notifyList.storeRelaxed(
nullptr);
367 list->connectionMask.storeRelaxed(0);
368 list->maximumTodoIndex = 0;
369 list->notifiesSize = 0;
370 list->notifies =
nullptr;
376QHash<QQmlAttachedPropertiesFunc, QObject *> *QQmlData::attachedProperties()
const
378 if (!extendedData) extendedData =
new QQmlDataExtended;
379 return &extendedData->attachedProperties;
382void QQmlData::removeFromContext()
384 if (nextContextObject)
385 nextContextObject->prevContextObject = prevContextObject;
386 if (prevContextObject)
387 *prevContextObject = nextContextObject;
388 else if (outerContext && outerContext->ownedObjects() ==
this)
389 outerContext->setOwnedObjects(nextContextObject);
391 nextContextObject =
nullptr;
392 prevContextObject =
nullptr;
393 outerContext =
nullptr;
396void QQmlData::clearBindings()
398 if (QQmlAbstractBinding *binding = std::exchange(bindings,
nullptr)) {
399 for (QQmlAbstractBinding *next = binding; next; next = next->nextBinding())
400 next->setAddedToObject(
false);
401 if (!binding->ref.deref())
406bool QQmlData::clearSignalHandlers()
408 for (QQmlBoundSignal *signalHandler = std::exchange(signalHandlers,
nullptr); signalHandler;) {
409 if (signalHandler->isNotifying()) {
410 signalHandlers = signalHandler;
414 QQmlBoundSignal *next = signalHandler->m_nextSignal;
415 signalHandler->m_prevSignal =
nullptr;
416 signalHandler->m_nextSignal =
nullptr;
417 delete signalHandler;
418 signalHandler = next;
424void QQmlData::destroyed(QObject *object)
429 compilationUnit.reset();
430 qDeleteAll(deferredData);
431 deferredData.clear();
433 if (!clearSignalHandlers()) {
438 QString locationString;
439 QQmlBoundSignalExpression *expr = signalHandlers->expression();
441 QQmlSourceLocation location = expr->sourceLocation();
442 if (location.sourceFile.isEmpty())
443 location.sourceFile = QStringLiteral(
"<Unknown File>");
444 locationString.append(location.sourceFile);
445 locationString.append(QStringLiteral(
":%0: ").arg(location.line));
446 QString source = expr->expression();
447 if (source.size() > 100) {
449 source.append(QLatin1String(
" ..."));
451 locationString.append(source);
453 locationString = QStringLiteral(
"<Unknown Location>");
455 qFatal(
"Object %p destroyed while one of its QML signal handlers is in progress.\n"
456 "Most likely the object was deleted synchronously (use QObject::deleteLater() "
457 "instead), or the application is running a nested event loop.\n"
458 "This behavior is NOT supported!\n"
459 "%s", object, qPrintable(locationString));
462 if (bindingBitsArraySize > InlineBindingArraySize)
466 propertyCache.reset();
471 auto *guard = guards;
472 guard->setObject(
nullptr);
473 if (guard->objectDestroyed)
474 guard->objectDestroyed(guard);
477 disconnectNotifiers(DeleteNotifyList::Yes);
491QQmlData::BindingBitsType *QQmlData::growBits(QObject *obj,
int bit)
493 BindingBitsType *bits = (bindingBitsArraySize == InlineBindingArraySize) ? bindingBitsValue : bindingBits;
494 int props = QQmlMetaObject(obj).propertyCount();
495 Q_ASSERT(bit < 2 * props);
498 uint arraySize = (2 *
static_cast<uint>(props) + BitsPerType - 1) / BitsPerType;
499 Q_ASSERT(arraySize > 1);
500 Q_ASSERT(arraySize <= 0xffff);
502 BindingBitsType *newBits =
static_cast<BindingBitsType *>(malloc(arraySize*
sizeof(BindingBitsType)));
503 memcpy(newBits, bits, bindingBitsArraySize *
sizeof(BindingBitsType));
504 memset(newBits + bindingBitsArraySize, 0,
sizeof(BindingBitsType) * (arraySize - bindingBitsArraySize));
506 if (bindingBitsArraySize > InlineBindingArraySize)
508 bindingBits = newBits;
510 bindingBitsArraySize = arraySize;
514QQmlData *QQmlData::createQQmlData(QObjectPrivate *priv)
517 Q_ASSERT(!priv->isDeletingChildren);
518 priv->declarativeData =
new QQmlData(OwnsMemory);
519 return static_cast<QQmlData *>(priv->declarativeData);
522QQmlPropertyCache::ConstPtr QQmlData::createPropertyCache(QObject *object)
524 QQmlData *ddata = QQmlData::get(object,
true);
525 ddata->propertyCache = QQmlMetaType::propertyCache(object, QTypeRevision {});
526 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.