8#include <qscopedvaluerollback.h>
10#include <QtCore/qalloc.h>
11#include <QtCore/qloggingcategory.h>
12#include <QtCore/private/qthread_p.h>
13#include <QtCore/qmetaobject.h>
14#include <QtCore/qmutex.h>
20Q_STATIC_LOGGING_CATEGORY(lcQPropertyBinding,
"qt.qproperty.binding");
24void QPropertyBindingPrivatePtr::destroyAndFreeMemory()
26 QPropertyBindingPrivate::destroyAndFreeMemory(
static_cast<QPropertyBindingPrivate *>(d));
29void QPropertyBindingPrivatePtr::reset(QtPrivate::RefCounted *ptr)
noexcept
34 auto *old = std::exchange(d, ptr);
35 if (old && !old->deref())
36 QPropertyBindingPrivate::destroyAndFreeMemory(
static_cast<QPropertyBindingPrivate *>(d));
43 if (
auto *b = binding()) {
44 observer->prev = &b->firstObserver.ptr;
45 observer->next = b->firstObserver.ptr;
47 observer->next->prev = &observer->next;
48 b->firstObserver.ptr = observer;
50 auto &d = ptr->d_ref();
51 Q_ASSERT(!(d & QPropertyBindingData::BindingBit));
52 auto firstObserver =
reinterpret_cast<QPropertyObserver*>(d);
53 observer->prev =
reinterpret_cast<QPropertyObserver**>(&d);
54 observer->next = firstObserver;
56 observer->next->prev = &observer->next;
57 d =
reinterpret_cast<quintptr>(observer);
62
63
64
65
66
67
68
69
84
85
86
87
88
89
90
91
92 void addProperty(
const QPropertyBindingData *bindingData, QUntypedPropertyData *propertyData) {
93 if (bindingData->isNotificationDelayed())
96 while (data->used == size) {
102 auto *delayed = data->delayedProperties + data->used;
103 *delayed = QPropertyProxyBindingData { bindingData->d_ptr, bindingData, propertyData };
106 quintptr bindingBit = bindingData->d_ptr & QPropertyBindingData::BindingBit;
107 bindingData->d_ptr =
reinterpret_cast<quintptr>(delayed) | QPropertyBindingData::DelayedNotificationBit | bindingBit;
108 Q_ASSERT(bindingData->d_ptr > 3);
110 if (
auto observer =
reinterpret_cast<QPropertyObserver *>(delayed->d_ptr))
111 observer->prev =
reinterpret_cast<QPropertyObserver **>(&delayed->d_ptr);
116
117
118
119
120
121
122
123
124
125
126
127 void evaluateBindings(PendingBindingObserverList &bindingObservers, qsizetype index, QBindingStatus *status) {
128 auto *delayed = delayedProperties + index;
129 auto *bindingData = delayed->originalBindingData;
133 bindingData->d_ptr = delayed->d_ptr;
134 Q_ASSERT(!(bindingData->d_ptr & QPropertyBindingData::DelayedNotificationBit));
135 if (!bindingData->hasBinding()) {
136 if (
auto observer =
reinterpret_cast<QPropertyObserver *>(bindingData->d_ptr))
137 observer->prev =
reinterpret_cast<QPropertyObserver **>(&bindingData->d_ptr);
143 observer.evaluateBindings(bindingObservers, status);
147
148
149
150
151
152
153
154
156 auto *delayed = delayedProperties + index;
157 if (delayed->d_ptr & QPropertyBindingData::BindingBit)
159 if (!delayed->originalBindingData)
161 delayed->originalBindingData =
nullptr;
163 QPropertyObserverPointer observer {
reinterpret_cast<QPropertyObserver *>(delayed->d_ptr & ~QPropertyBindingData::DelayedNotificationBit) };
167 observer.notify(delayed->propertyData);
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
195Q_CONSTINIT
thread_local QBindingStatus *tl_status =
nullptr;
199 auto status =
new QBindingStatus {};
201
202
203
206 QThread *currentThread = threadData->thread;
208 QThreadPrivate *threadPriv =
static_cast<QThreadPrivate *>(QObjectPrivate::get(currentThread));
209 QMutexLocker lock(&threadPriv->mutex);
210 threadData->m_statusOrPendingObjects.setStatusAndClearList(status);
214 threadData->m_statusOrPendingObjects.setStatusAndClearList(status);
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
248 if (!groupUpdateData)
250 ++groupUpdateData
->ref;
254
255
256
257
258
259
260
261
262
263
264
267 auto status = &bindingStatus();
269 auto *data = groupUpdateData;
273 groupUpdateData =
nullptr;
275 PendingBindingObserverList bindingObservers;
279 for (qsizetype i = 0; i < data->used; ++i)
280 data->evaluateBindings(bindingObservers, i, status);
284 for (
const auto &bindingPtr: bindingObservers) {
285 auto *binding =
static_cast<QPropertyBindingPrivate *>(bindingPtr.get());
286 binding->notifyNonRecursive();
291 for (qsizetype i = 0; i < data->used; ++i)
293 delete std::exchange(data, data->next);
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
320
321
322
323
326
327
328
329
334static_assert(std::is_trivially_destructible_v<QPropertyBindingSourceLocation>);
337QPropertyBindingPrivate::~QPropertyBindingPrivate()
340 firstObserver.unlink();
342 vtable->destroy(
reinterpret_cast<std::byte *>(
this)
343 + QPropertyBindingPrivate::getSizeEnsuringAlignment());
346void QPropertyBindingPrivate::clearDependencyObservers() {
347 for (size_t i = 0; i < qMin(dependencyObserverCount, inlineDependencyObservers.size()); ++i) {
348 QPropertyObserverPointer p{&inlineDependencyObservers[i]};
352 heapObservers->clear();
353 dependencyObserverCount = 0;
356QPropertyObserverPointer QPropertyBindingPrivate::allocateDependencyObserver_slow()
358 ++dependencyObserverCount;
360 heapObservers.reset(
new std::vector<QPropertyObserver>());
361 return {&heapObservers->emplace_back()};
364void QPropertyBindingPrivate::unlinkAndDeref()
366 clearDependencyObservers();
367 propertyDataPtr =
nullptr;
369 destroyAndFreeMemory(
this);
372bool QPropertyBindingPrivate::evaluateRecursive(PendingBindingObserverList &bindingObservers, QBindingStatus *status)
375 status = &bindingStatus();
376 return evaluateRecursive_inline(bindingObservers, status);
379void QPropertyBindingPrivate::notifyNonRecursive(
const PendingBindingObserverList &bindingObservers)
381 notifyNonRecursive();
382 for (
auto &&bindingPtr: bindingObservers) {
383 auto *binding =
static_cast<QPropertyBindingPrivate *>(bindingPtr.get());
384 binding->notifyNonRecursive();
388QPropertyBindingPrivate::NotificationState QPropertyBindingPrivate::notifyNonRecursive()
392 pendingNotify =
false;
396 firstObserver.noSelfDependencies(
this);
397 firstObserver.notify(propertyDataPtr);
399 if (hasStaticObserver)
400 staticObserverCallback(propertyDataPtr);
406
407
408
409
410
411
412
413
416
417
418
419
420QUntypedPropertyBinding::QUntypedPropertyBinding() =
default;
423
424
425
426
427
430
431
432
433
434
435
436QUntypedPropertyBinding::QUntypedPropertyBinding(QMetaType metaType,
const BindingFunctionVTable *vtable,
void *function,
437 const QPropertyBindingSourceLocation &location)
439 std::byte *mem =
new std::byte[QPropertyBindingPrivate::getSizeEnsuringAlignment() + vtable->size]();
440 d =
new(mem) QPropertyBindingPrivate(metaType, vtable, std::move(location));
441 vtable->moveConstruct(mem + QPropertyBindingPrivate::getSizeEnsuringAlignment(), function);
445
446
447
448
449
450QUntypedPropertyBinding::QUntypedPropertyBinding(QUntypedPropertyBinding &&other)
451 : d(std::move(other.d))
456
457
458QUntypedPropertyBinding::QUntypedPropertyBinding(
const QUntypedPropertyBinding &other)
463
464
465QUntypedPropertyBinding &QUntypedPropertyBinding::operator=(
const QUntypedPropertyBinding &other)
472
473
474
475
476
477QUntypedPropertyBinding &QUntypedPropertyBinding::operator=(QUntypedPropertyBinding &&other)
479 d = std::move(other.d);
484
485
486QUntypedPropertyBinding::QUntypedPropertyBinding(QPropertyBindingPrivate *priv)
492
493
494QUntypedPropertyBinding::~QUntypedPropertyBinding()
499
500
501
502
503
504bool QUntypedPropertyBinding::isNull()
const
510
511
512
513
514QPropertyBindingError QUntypedPropertyBinding::error()
const
517 return QPropertyBindingError();
518 return static_cast<QPropertyBindingPrivate *>(d.get())->bindingError();
522
523
524
525QMetaType QUntypedPropertyBinding::valueMetaType()
const
529 return static_cast<QPropertyBindingPrivate *>(d.get())->valueMetaType();
532QPropertyBindingData::~QPropertyBindingData()
534 QPropertyBindingDataPointer d{
this};
535 if (isNotificationDelayed())
536 proxyData()->originalBindingData =
nullptr;
537 for (
auto observer = d.firstObserver(); observer;) {
538 auto next = observer.nextObserver();
542 if (
auto binding = d.binding())
543 binding->unlinkAndDeref();
546QUntypedPropertyBinding QPropertyBindingData::setBinding(
const QUntypedPropertyBinding &binding,
547 QUntypedPropertyData *propertyDataPtr,
548 QPropertyObserverCallback staticObserverCallback,
549 QtPrivate::QPropertyBindingWrapper guardCallback)
551 QPropertyBindingPrivatePtr oldBinding;
552 QPropertyBindingPrivatePtr newBinding = binding.d;
554 QPropertyBindingDataPointer d{
this};
555 QPropertyObserverPointer observer;
557 auto &data = d_ref();
558 if (
auto *existingBinding = d.binding()) {
559 if (existingBinding == newBinding.data())
560 return QUntypedPropertyBinding(
static_cast<QPropertyBindingPrivate *>(oldBinding.data()));
561 if (existingBinding->isUpdating()) {
562 existingBinding->setError({QPropertyBindingError::BindingLoop, QStringLiteral(
"Binding set during binding evaluation!")});
563 return QUntypedPropertyBinding(
static_cast<QPropertyBindingPrivate *>(oldBinding.data()));
565 oldBinding = QPropertyBindingPrivatePtr(existingBinding);
566 observer =
static_cast<QPropertyBindingPrivate *>(oldBinding.data())->takeObservers();
567 static_cast<QPropertyBindingPrivate *>(oldBinding.data())->unlinkAndDeref();
570 observer = d.firstObserver();
574 newBinding.data()->addRef();
575 data =
reinterpret_cast<quintptr>(newBinding.data());
577 auto newBindingRaw =
static_cast<QPropertyBindingPrivate *>(newBinding.data());
578 newBindingRaw->setProperty(propertyDataPtr);
580 newBindingRaw->prependObserver(observer);
581 newBindingRaw->setStaticObserver(staticObserverCallback, guardCallback);
583 PendingBindingObserverList bindingObservers;
584 newBindingRaw->evaluateRecursive(bindingObservers);
585 newBindingRaw->notifyNonRecursive(bindingObservers);
586 }
else if (observer) {
587 d.setObservers(observer.ptr);
593 static_cast<QPropertyBindingPrivate *>(oldBinding.data())->detachFromProperty();
595 return QUntypedPropertyBinding(
static_cast<QPropertyBindingPrivate *>(oldBinding.data()));
598QPropertyBindingData::QPropertyBindingData(QPropertyBindingData &&other) : d_ptr(std::exchange(other.d_ptr, 0))
600 QPropertyBindingDataPointer::fixupAfterMove(
this);
607 QBindingStatus *s = status;
610 currentState = &s->currentlyEvaluatingBinding;
613 binding->clearDependencyObservers();
625 currentlyEvaluatingBindingList = &bindingStatus().currentlyEvaluatingBinding;
630QPropertyBindingPrivate *QPropertyBindingPrivate::currentlyEvaluatingBinding()
632 auto currentState = bindingStatus().currentlyEvaluatingBinding ;
633 return currentState ? currentState->binding :
nullptr;
637void QPropertyBindingData::evaluateIfDirty(
const QUntypedPropertyData *)
const
641void QPropertyBindingData::removeBinding_helper()
643 QPropertyBindingDataPointer d{
this};
645 auto *existingBinding = d.binding();
646 Q_ASSERT(existingBinding);
647 if (existingBinding->isSticky()) {
651 auto observer = existingBinding->takeObservers();
654 d.setObservers(observer.ptr);
655 existingBinding->unlinkAndDeref();
658void QPropertyBindingData::registerWithCurrentlyEvaluatingBinding()
const
660 auto currentState = bindingStatus().currentlyEvaluatingBinding;
663 registerWithCurrentlyEvaluatingBinding_helper(currentState);
667void QPropertyBindingData::registerWithCurrentlyEvaluatingBinding_helper(BindingEvaluationState *currentState)
const
669 QPropertyBindingDataPointer d{
this};
671 if (currentState->alreadyCaptureProperties.contains(
this))
674 currentState->alreadyCaptureProperties.push_back(
this);
676 QPropertyObserverPointer dependencyObserver = currentState->binding->allocateDependencyObserver();
677 Q_ASSERT(QPropertyObserver::ObserverNotifiesBinding == 0);
678 dependencyObserver.setBindingToNotify_unsafe(currentState->binding);
679 d.addObserver(dependencyObserver.ptr);
682void QPropertyBindingData::notifyObservers(QUntypedPropertyData *propertyDataPtr)
const
684 notifyObservers(propertyDataPtr,
nullptr);
687void QPropertyBindingData::notifyObservers(QUntypedPropertyData *propertyDataPtr, QBindingStorage *storage)
const
689 if (isNotificationDelayed())
691 QPropertyBindingDataPointer d{
this};
693 PendingBindingObserverList bindingObservers;
694 if (QPropertyObserverPointer observer = d.firstObserver()) {
695 if (notifyObserver_helper(propertyDataPtr, storage, observer, bindingObservers) == Evaluated) {
697
698
699
700
701
702
704 d = QPropertyBindingDataPointer {storage->bindingData(propertyDataPtr)};
705 if (QPropertyObserverPointer observer = d.firstObserver())
706 observer.notify(propertyDataPtr);
707 for (
auto &&bindingPtr: bindingObservers) {
708 auto *binding =
static_cast<QPropertyBindingPrivate *>(bindingPtr.get());
709 binding->notifyNonRecursive();
715QPropertyBindingData::NotificationResult QPropertyBindingData::notifyObserver_helper
717 QUntypedPropertyData *propertyDataPtr, QBindingStorage *storage,
718 QPropertyObserverPointer observer,
719 PendingBindingObserverList &bindingObservers)
const
721#ifdef QT_HAS_FAST_CURRENT_THREAD_ID
722 QBindingStatus *status = storage ? storage->bindingStatus :
nullptr;
723 if (!status || status->threadId != QThread::currentThreadId())
724 status = &bindingStatus();
727 QBindingStatus *status = &bindingStatus();
729 if (QPropertyDelayedNotifications *delay = status->groupUpdateData) {
730 delay->addProperty(
this, propertyDataPtr);
734 observer.evaluateBindings(bindingObservers, status);
739QPropertyObserver::QPropertyObserver(ChangeHandler changeHandler)
741 QPropertyObserverPointer d{
this};
742 d.setChangeHandler(changeHandler);
745#if QT_DEPRECATED_SINCE(6
, 6
)
746QPropertyObserver::QPropertyObserver(QUntypedPropertyData *data)
748 QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
750 next.setTag(ObserverIsAlias);
756
757void QPropertyObserver::setSource(
const QPropertyBindingData &property)
759 QPropertyObserverPointer d{
this};
760 QPropertyBindingDataPointer propPrivate{&property};
761 d.observeProperty(propPrivate);
764QPropertyObserver::~QPropertyObserver()
766 QPropertyObserverPointer d{
this};
770QPropertyObserver::QPropertyObserver(QPropertyObserver &&other)
noexcept
772 binding = std::exchange(other.binding, {});
773 next = std::exchange(other.next, {});
774 prev = std::exchange(other.prev, {});
778 prev.setPointer(
this);
781QPropertyObserver &QPropertyObserver::operator=(QPropertyObserver &&other)
noexcept
786 QPropertyObserverPointer d{
this};
790 binding = std::exchange(other.binding, {});
791 next = std::exchange(other.next, {});
792 prev = std::exchange(other.prev, {});
796 prev.setPointer(
this);
802
803
804
805
809
810
811
812
813
814
818 Q_ASSERT(ptr->next.tag() != QPropertyObserver::ObserverIsPlaceholder);
819 ptr->changeHandler = changeHandler;
820 ptr->next.setTag(QPropertyObserver::ObserverNotifiesChangeHandler);
824
825
826
829 Q_ASSERT(ptr->next.tag() == QPropertyObserver::ObserverNotifiesBinding);
830 ptr->binding = binding;
834
835
836
837
838
841
842
843
844
849 auto observer =
const_cast<QPropertyObserver*>(ptr);
852 if (QPropertyObserver::ObserverTag(observer->next.tag()) == QPropertyObserver::ObserverNotifiesBinding)
853 if (observer->binding == binding) {
854 qCritical(
"Property depends on itself!");
858 observer = observer->next.data();
867 auto observer =
const_cast<QPropertyObserver*>(ptr);
870 QPropertyObserver *next = observer->next.data();
872 if (QPropertyObserver::ObserverTag(observer->next.tag()) == QPropertyObserver::ObserverNotifiesBinding) {
873 auto bindingToEvaluate = observer->binding;
874 QPropertyObserverNodeProtector protector(observer);
876 QPropertyBindingPrivatePtr currentBinding(observer->binding);
877 const bool evalStatus = bindingToEvaluate->evaluateRecursive_inline(bindingObservers, status);
879 bindingObservers.push_back(std::move(currentBinding));
880 next = protector.next();
891 property.addObserver(
ptr);
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
937
938
939
940
941QPropertyBindingError::QPropertyBindingError()
946
947
948
949QPropertyBindingError::QPropertyBindingError(Type type,
const QString &description)
951 if (type != NoError) {
952 d =
new QPropertyBindingErrorPrivate;
954 d->description = description;
959
960
961QPropertyBindingError::QPropertyBindingError(
const QPropertyBindingError &other)
967
968
969QPropertyBindingError &QPropertyBindingError::operator=(
const QPropertyBindingError &other)
976
977
978
979QPropertyBindingError::QPropertyBindingError(QPropertyBindingError &&other)
980 : d(std::move(other.d))
985
986
987
988QPropertyBindingError &QPropertyBindingError::operator=(QPropertyBindingError &&other)
990 d = std::move(other.d);
995
996
997QPropertyBindingError::~QPropertyBindingError()
1002
1003
1004
1005
1006QPropertyBindingError::Type QPropertyBindingError::type()
const
1009 return QPropertyBindingError::NoError;
1014
1015
1016
1017QString QPropertyBindingError::description()
const
1021 return d->description;
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1047
1048
1049
1050
1051
1052
1053
1056
1057
1058
1059
1060
1063
1064
1065
1066
1067
1068
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1087
1088
1089
1090
1091
1094
1095
1096
1097
1098
1099
1100
1101
1104
1105
1106
1107
1108
1111
1112
1113
1114
1115
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1130
1131
1132
1133
1136
1137
1138
1139
1140
1143
1144
1145
1146
1147
1148
1149
1150
1153
1154
1155
1156
1157
1158
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1173
1174
1175
1176
1177
1178
1179
1182
1183
1184
1185
1186
1187
1188
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1203
1204
1205
1206
1209
1210
1211
1212
1213
1214
1215
1216
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1274
1275
1276
1277
1278
1281
1282
1283
1284
1285
1288
1289
1290
1291
1292
1293
1294
1295
1298
1299
1300
1301
1302
1303
1304
1305
1309
1310
1311
1312
1313
1314
1315
1316
1319
1320
1321
1322
1323
1326
1327
1328
1329
1330
1331
1332
1335
1336
1337
1338
1339
1340
1341
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1376
1377
1378
1379
1382
1383
1384
1385
1388
1389
1390
1391
1394
1395
1396
1397
1398
1401
1402
1403
1404
1405
1406
1407
1410
1411
1412
1413
1414
1415
1416
1419
1420
1421
1422
1425
1426
1427
1428
1429
1432
1433
1434
1435
1436
1437
1440
1441
1442
1443
1444
1447
1448
1449
1450
1451
1452
1453
1454
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1485
1486
1487
1488
1489
1490
1493
1494
1495
1496
1497
1498
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1555
1556
1557
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1635
1636
1637
1638
1639
1640
1641
1642
1643
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1733
1734
1735
1736
1737
1738
1739
1740
1741
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1797
1798
1799
1800
1801
1802
1803
1804
1805
1809
1810
1811
1814
1815
1816
1817
1820
1821
1822
1823
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1850
1851
1852
1853
1854
1855
1856
1857
1860
1861
1862
1863
1866
1867
1868
1869
1870
1873
1874
1875
1876
1877
1878
1879
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1940
1941
1942
1943
1947
1948
1949
1950
1951
1952
1955
1956
1957
1958
1959
1960
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2014
2015
2016
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2097
2098
2099
2100
2103
2104
2105
2106
2109
2110
2111
2112
2113
2116
2117
2118
2119
2120
2123
2124
2125
2126
2127
2130
2131
2132
2133
2134
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2181
2182
2183
2184
2185
2188
2189
2190
2191
2192
2193
2196
2197
2198
2199
2200
2201
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2255
2256
2257
2258
2259
2260
2279 static_assert(
alignof(
Pair) ==
alignof(
void *));
2280 static_assert(
alignof(
size_t) ==
alignof(
void *));
2287 return reinterpret_cast<
Pair *>(dd + 1);
2291 Q_ASSERT(!
d || newSize >
d->size);
2293 void *nd = calloc(1, allocSize);
2295 newData->size = newSize;
2300 newData->used =
d->used;
2302 for (size_t i = 0; i <
d->size; ++i, ++p) {
2305 Q_ASSERT(newData->size && (newData->size & (newData->size - 1)) == 0);
2306 size_t index = qHash(p->data) & (newData->size - 1);
2307 while (pp[index].data) {
2309 if (index == newData->size)
2312 new (pp + index)
Pair{p->data, QPropertyBindingData(
std::move(p->bindingData))};
2317 QtPrivate::sizedFree(d, oldAllocSize);
2326 Q_ASSERT(
d->size && (
d->size & (
d->size - 1)) == 0);
2327 size_t index = qHash(data) & (
d->size - 1);
2329 while (p[index].data) {
2330 if (p[index].data == data)
2331 return &p[index].bindingData;
2333 if (index ==
d->size)
2345 else if (d->used*2 >= d->size)
2346 reallocate(d->size*2);
2347 Q_ASSERT(
d->size && (
d->size & (
d->size - 1)) == 0);
2348 size_t index = qHash(data) & (
d->size - 1);
2350 while (p[index].data) {
2351 if (p[index].data == data)
2352 return &p[index].bindingData;
2354 if (index ==
d->size)
2360 new (p + index)
Pair{data, QPropertyBindingData()};
2361 return &p[index].bindingData;
2369 for (size_t i = 0; i <
d->size; ++i) {
2375 QtPrivate::sizedFree(d, allocSize);
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2391QBindingStorage::QBindingStorage()
2393 bindingStatus = &QT_PREPEND_NAMESPACE(bindingStatus)();
2394 Q_ASSERT(bindingStatus);
2397QBindingStorage::~QBindingStorage()
2399 QBindingStoragePrivate(d).destroy();
2402void QBindingStorage::reinitAfterThreadMove()
2404 bindingStatus = &QT_PREPEND_NAMESPACE(bindingStatus)();
2405 Q_ASSERT(bindingStatus);
2408void QBindingStorage::clear()
2410 QBindingStoragePrivate(d).destroy();
2412 bindingStatus =
nullptr;
2415void QBindingStorage::registerDependency_helper(
const QUntypedPropertyData *data)
const
2417 Q_ASSERT(bindingStatus);
2420 QtPrivate::BindingEvaluationState *currentBinding;
2421#ifdef QT_HAS_FAST_CURRENT_THREAD_ID
2422 const bool threadMatches = (QThread::currentThreadId() == bindingStatus->threadId);
2423 if (Q_LIKELY(threadMatches))
2424 currentBinding = bindingStatus->currentlyEvaluatingBinding;
2426 currentBinding = QT_PREPEND_NAMESPACE(bindingStatus)().currentlyEvaluatingBinding;
2428 currentBinding = QT_PREPEND_NAMESPACE(bindingStatus)().currentlyEvaluatingBinding;
2430 QUntypedPropertyData *dd =
const_cast<QUntypedPropertyData *>(data);
2431 if (!currentBinding)
2433 auto storage = QBindingStoragePrivate(d).get(dd,
true);
2436 storage->registerWithCurrentlyEvaluatingBinding(currentBinding);
2440QPropertyBindingData *QBindingStorage::bindingData_helper(
const QUntypedPropertyData *data)
const
2442 return QBindingStoragePrivate(d).get(data);
2445const QBindingStatus *QBindingStorage::status(QtPrivate::QBindingStatusAccessToken)
const
2447 return bindingStatus;
2450QPropertyBindingData *QBindingStorage::bindingData_helper(QUntypedPropertyData *data,
bool create)
2452 return QBindingStoragePrivate(d).get(data, create);
2459void initBindingStatusThreadId()
2461 bindingStatus().threadId = QThread::currentThreadId();
2466 auto ret = bindingStatus().currentlyEvaluatingBinding;
2477
2478
2479
2480
2481
2482
2483
2486 return bindingStatus().currentlyEvaluatingBinding !=
nullptr;
2492 if (
const auto current = bindingStatus().currentCompatProperty)
2493 return current->property == property;
2503 qCWarning(lcQPropertyBinding).noquote() << prefix.toString()
2504 <<
"The QBindable does not allow interaction with the binding.";
2507 qCWarning(lcQPropertyBinding).noquote() << prefix.toString()
2508 <<
"The QBindable is read-only.";
2512 qCWarning(lcQPropertyBinding).noquote() << prefix.toString()
2513 <<
"The QBindable is invalid.";
2520 qCWarning(lcQPropertyBinding) <<
"setBinding: Could not set binding as the property expects it to be of type"
2522 <<
"but got" << expected.name() <<
"instead.";
2527 qCWarning(lcQPropertyBinding)
2528 <<
"Property has a custom getter and also a change signal with arguments."
2529 <<
"This requires the getter to be called to retrieve the argument,"
2530 <<
"possibly creating spurious binding dependencies";
2536
2537
2538
2551void getter(
const QUntypedPropertyData *d,
void *value)
2554 adaptor->bindingData().registerWithCurrentlyEvaluatingBinding();
2555 auto mt = adaptor->metaProperty().metaType();
2557 mt.construct(value, adaptor->metaProperty().read(adaptor->object()).data());
2560void setter(QUntypedPropertyData *d,
const void *value)
2563 adaptor->bindingData().removeBinding();
2564 adaptor->metaProperty().write(adaptor->object(),
2565 QVariant(adaptor->metaProperty().metaType(), value));
2571 return QUntypedPropertyBinding(adaptor->bindingData().binding());
2575 QtPrivate::QPropertyBindingFunction binding, QUntypedPropertyData *temp,
2579 type.destruct(value);
2580 type.construct(value, adaptor->metaProperty().read(adaptor->object()).data());
2581 if (binding.vtable->call(type, temp, binding.functor)) {
2582 adaptor->metaProperty().write(adaptor->object(), QVariant(type, value));
2589 QPropertyBindingWrapper wrapper)
2592 return adaptor->bindingData().setBinding(binding, d,
nullptr, wrapper);
2595void setObserver(
const QUntypedPropertyData *d, QPropertyObserver *observer)
2602 : QSlotObjectBase(&impl), obj(o), metaProperty_(p)
2606#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
2620 if (!self->bindingData_.hasBinding())
2621 self->bindingData_.notifyObservers(self);
2641 if (!metaProperty.isValid()) {
2642 qCWarning(lcQPropertyBinding) <<
"QUntypedBindable: Property is not valid";
2646 if (metaProperty.isBindable()) {
2647 *
this = metaProperty.bindable(obj);
2651 if (!metaProperty.hasNotifySignal()) {
2652 qCWarning(lcQPropertyBinding)
2653 <<
"QUntypedBindable: Property" << metaProperty.name() <<
"has no notify signal";
2657 auto metatype =
iface->metaType();
2658 if (metaProperty.metaType() != metatype) {
2659 qCWarning(lcQPropertyBinding) <<
"QUntypedBindable: Property" << metaProperty.name()
2660 <<
"of type" << metaProperty.metaType().name()
2661 <<
"does not match requested type" << metatype.name();
2666 if (obj->metaObject()->property(metaProperty.propertyIndex()).name() != metaProperty.name()) {
2667 qCWarning(lcQPropertyBinding) <<
"QUntypedBindable: Property" << metaProperty.name()
2668 <<
"does not belong to this object";
2673 auto adaptor = QObjectPrivate::get(obj)->getPropertyAdaptorSlotObject(metaProperty);
2678 auto c = QObjectPrivate::connect(obj, metaProperty.notifySignalIndex(), obj, adaptor,
2679 Qt::DirectConnection);
2690 [=]() -> QMetaProperty {
2693 auto propertyIndex = obj->metaObject()->indexOfProperty(property);
2694 if (propertyIndex < 0) {
2695 qCWarning(lcQPropertyBinding)
2696 <<
"QUntypedBindable: No property named" << property;
2699 return obj->metaObject()->property(propertyIndex);
const QtPrivate::QBindableInterface * iface
friend class QT_PREPEND_NAMESPACE(QUntypedBindable)
Combined button and popup list for selecting options.
void printMetaTypeMismatch(QMetaType actual, QMetaType expected)
void printUnsuitableBindableWarning(QAnyStringView prefix, BindableWarnings::Reason reason)
Q_CORE_EXPORT void printSignalArgumentsWithCustomGetter()
void setter(QUntypedPropertyData *d, const void *value)
void getter(const QUntypedPropertyData *d, void *value)
bool bindingWrapper(QMetaType type, QUntypedPropertyData *d, QtPrivate::QPropertyBindingFunction binding, QUntypedPropertyData *temp, void *value)
QUntypedPropertyBinding getBinding(const QUntypedPropertyData *d)
QUntypedPropertyBinding setBinding(QUntypedPropertyData *d, const QUntypedPropertyBinding &binding, QPropertyBindingWrapper wrapper)
void setObserver(const QUntypedPropertyData *d, QPropertyObserver *observer)
Q_CORE_EXPORT bool isAnyBindingEvaluating()
void setBindingStatus(QBindingStatus *status, QBindingStatusAccessToken)
Q_CORE_EXPORT void restoreBindingStatus(BindingEvaluationState *status)
QBindingStatus * getBindingStatus(QtPrivate::QBindingStatusAccessToken)
Q_CORE_EXPORT bool isPropertyInBindingWrapper(const QUntypedPropertyData *property)
Q_CORE_EXPORT void beginPropertyUpdateGroup()
Q_CORE_EXPORT void endPropertyUpdateGroup()
static QBindingStatus & bindingStatus()
static Q_NEVER_INLINE void initBindingStatus()
QPropertyBindingData bindingData
QUntypedPropertyData * data
static Pair * pairs(QBindingStorageData *dd)
QBindingStoragePrivate(QBindingStorageData *&_d)
void reallocate(size_t newSize)
QPropertyBindingData * get(QUntypedPropertyData *data, bool create)
QPropertyBindingData * get(const QUntypedPropertyData *data)
Q_ALWAYS_INLINE void addObserver(QPropertyObserver *observer)
QPropertyObserverPointer firstObserver() const
QPropertyProxyBindingData delayedProperties[size]
void notify(qsizetype index)
static constexpr auto PageSize
void addProperty(const QPropertyBindingData *bindingData, QUntypedPropertyData *propertyData)
static constexpr qsizetype size
void evaluateBindings(PendingBindingObserverList &bindingObservers, qsizetype index, QBindingStatus *status)
QPropertyDelayedNotifications * next
void observeProperty(QPropertyBindingDataPointer property)
void setChangeHandler(QPropertyObserver::ChangeHandler changeHandler)
BindingEvaluationState(QPropertyBindingPrivate *binding, QBindingStatus *status)
BindingEvaluationState * previousState
BindingEvaluationState ** currentState
QtPrivate::BindingEvaluationState ** currentlyEvaluatingBindingList
CompatPropertySafePoint * previousState
CompatPropertySafePoint ** currentState
QtPrivate::BindingEvaluationState * bindingState