7#include <qscopedvaluerollback.h>
9#include <QtCore/qalloc.h>
10#include <QtCore/qloggingcategory.h>
11#include <QtCore/private/qthread_p.h>
12#include <QtCore/qmetaobject.h>
13#include <QtCore/qmutex.h>
19Q_STATIC_LOGGING_CATEGORY(lcQPropertyBinding,
"qt.qproperty.binding");
23void QPropertyBindingPrivatePtr::destroyAndFreeMemory()
25 QPropertyBindingPrivate::destroyAndFreeMemory(
static_cast<QPropertyBindingPrivate *>(d));
28void QPropertyBindingPrivatePtr::reset(QtPrivate::RefCounted *ptr)
noexcept
33 auto *old = std::exchange(d, ptr);
34 if (old && !old->deref())
35 QPropertyBindingPrivate::destroyAndFreeMemory(
static_cast<QPropertyBindingPrivate *>(d));
42 if (
auto *b = binding()) {
43 observer->prev = &b->firstObserver.ptr;
44 observer->next = b->firstObserver.ptr;
46 observer->next->prev = &observer->next;
47 b->firstObserver.ptr = observer;
49 auto &d = ptr->d_ref();
50 Q_ASSERT(!(d & QPropertyBindingData::BindingBit));
51 auto firstObserver =
reinterpret_cast<QPropertyObserver*>(d);
52 observer->prev =
reinterpret_cast<QPropertyObserver**>(&d);
53 observer->next = firstObserver;
55 observer->next->prev = &observer->next;
56 d =
reinterpret_cast<quintptr>(observer);
61
62
63
64
65
66
67
68
83
84
85
86
87
88
89
90
91 void addProperty(
const QPropertyBindingData *bindingData, QUntypedPropertyData *propertyData) {
92 if (bindingData->isNotificationDelayed())
95 while (data->used == size) {
101 auto *delayed = data->delayedProperties + data->used;
102 *delayed = QPropertyProxyBindingData { bindingData->d_ptr, bindingData, propertyData };
105 quintptr bindingBit = bindingData->d_ptr & QPropertyBindingData::BindingBit;
106 bindingData->d_ptr =
reinterpret_cast<quintptr>(delayed) | QPropertyBindingData::DelayedNotificationBit | bindingBit;
107 Q_ASSERT(bindingData->d_ptr > 3);
109 if (
auto observer =
reinterpret_cast<QPropertyObserver *>(delayed->d_ptr))
110 observer->prev =
reinterpret_cast<QPropertyObserver **>(&delayed->d_ptr);
115
116
117
118
119
120
121
122
123
124
125
126 void evaluateBindings(PendingBindingObserverList &bindingObservers, qsizetype index, QBindingStatus *status) {
127 auto *delayed = delayedProperties + index;
128 auto *bindingData = delayed->originalBindingData;
132 bindingData->d_ptr = delayed->d_ptr;
133 Q_ASSERT(!(bindingData->d_ptr & QPropertyBindingData::DelayedNotificationBit));
134 if (!bindingData->hasBinding()) {
135 if (
auto observer =
reinterpret_cast<QPropertyObserver *>(bindingData->d_ptr))
136 observer->prev =
reinterpret_cast<QPropertyObserver **>(&bindingData->d_ptr);
142 observer.evaluateBindings(bindingObservers, status);
146
147
148
149
150
151
152
153
155 auto *delayed = delayedProperties + index;
156 if (delayed->d_ptr & QPropertyBindingData::BindingBit)
158 if (!delayed->originalBindingData)
160 delayed->originalBindingData =
nullptr;
162 QPropertyObserverPointer observer {
reinterpret_cast<QPropertyObserver *>(delayed->d_ptr & ~QPropertyBindingData::DelayedNotificationBit) };
166 observer.notify(delayed->propertyData);
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
194Q_CONSTINIT
thread_local QBindingStatus *tl_status =
nullptr;
198 auto status =
new QBindingStatus {};
200
201
202
205 QThread *currentThread = threadData->thread;
207 QThreadPrivate *threadPriv =
static_cast<QThreadPrivate *>(QObjectPrivate::get(currentThread));
208 QMutexLocker lock(&threadPriv->mutex);
209 threadData->m_statusOrPendingObjects.setStatusAndClearList(status);
213 threadData->m_statusOrPendingObjects.setStatusAndClearList(status);
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
247 if (!groupUpdateData)
249 ++groupUpdateData
->ref;
253
254
255
256
257
258
259
260
261
262
263
266 auto status = &bindingStatus();
268 auto *data = groupUpdateData;
272 groupUpdateData =
nullptr;
274 PendingBindingObserverList bindingObservers;
278 for (qsizetype i = 0; i < data->used; ++i)
279 data->evaluateBindings(bindingObservers, i, status);
283 for (
const auto &bindingPtr: bindingObservers) {
284 auto *binding =
static_cast<QPropertyBindingPrivate *>(bindingPtr.get());
285 binding->notifyNonRecursive();
290 for (qsizetype i = 0; i < data->used; ++i)
292 delete std::exchange(data, data->next);
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
319
320
321
322
325
326
327
328
333static_assert(std::is_trivially_destructible_v<QPropertyBindingSourceLocation>);
336QPropertyBindingPrivate::~QPropertyBindingPrivate()
339 firstObserver.unlink();
341 vtable->destroy(
reinterpret_cast<std::byte *>(
this)
342 + QPropertyBindingPrivate::getSizeEnsuringAlignment());
345void QPropertyBindingPrivate::clearDependencyObservers() {
346 for (size_t i = 0; i < qMin(dependencyObserverCount, inlineDependencyObservers.size()); ++i) {
347 QPropertyObserverPointer p{&inlineDependencyObservers[i]};
351 heapObservers->clear();
352 dependencyObserverCount = 0;
355QPropertyObserverPointer QPropertyBindingPrivate::allocateDependencyObserver_slow()
357 ++dependencyObserverCount;
359 heapObservers.reset(
new std::vector<QPropertyObserver>());
360 return {&heapObservers->emplace_back()};
363void QPropertyBindingPrivate::unlinkAndDeref()
365 clearDependencyObservers();
366 propertyDataPtr =
nullptr;
368 destroyAndFreeMemory(
this);
371bool QPropertyBindingPrivate::evaluateRecursive(PendingBindingObserverList &bindingObservers, QBindingStatus *status)
374 status = &bindingStatus();
375 return evaluateRecursive_inline(bindingObservers, status);
378void QPropertyBindingPrivate::notifyNonRecursive(
const PendingBindingObserverList &bindingObservers)
380 notifyNonRecursive();
381 for (
auto &&bindingPtr: bindingObservers) {
382 auto *binding =
static_cast<QPropertyBindingPrivate *>(bindingPtr.get());
383 binding->notifyNonRecursive();
387QPropertyBindingPrivate::NotificationState QPropertyBindingPrivate::notifyNonRecursive()
391 pendingNotify =
false;
395 firstObserver.noSelfDependencies(
this);
396 firstObserver.notify(propertyDataPtr);
398 if (hasStaticObserver)
399 staticObserverCallback(propertyDataPtr);
405
406
407
408
409QUntypedPropertyBinding::QUntypedPropertyBinding() =
default;
412
413
414
415
416
419
420
421
422
423
424
425QUntypedPropertyBinding::QUntypedPropertyBinding(QMetaType metaType,
const BindingFunctionVTable *vtable,
void *function,
426 const QPropertyBindingSourceLocation &location)
428 std::byte *mem =
new std::byte[QPropertyBindingPrivate::getSizeEnsuringAlignment() + vtable->size]();
429 d =
new(mem) QPropertyBindingPrivate(metaType, vtable, std::move(location));
430 vtable->moveConstruct(mem + QPropertyBindingPrivate::getSizeEnsuringAlignment(), function);
434
435
436
437
438
439QUntypedPropertyBinding::QUntypedPropertyBinding(QUntypedPropertyBinding &&other)
440 : d(std::move(other.d))
445
446
447QUntypedPropertyBinding::QUntypedPropertyBinding(
const QUntypedPropertyBinding &other)
453
454
455QUntypedPropertyBinding &QUntypedPropertyBinding::operator=(
const QUntypedPropertyBinding &other)
462
463
464
465
466
467QUntypedPropertyBinding &QUntypedPropertyBinding::operator=(QUntypedPropertyBinding &&other)
469 d = std::move(other.d);
474
475
476QUntypedPropertyBinding::QUntypedPropertyBinding(QPropertyBindingPrivate *priv)
482
483
484QUntypedPropertyBinding::~QUntypedPropertyBinding()
489
490
491
492
493
494bool QUntypedPropertyBinding::isNull()
const
500
501
502
503
504QPropertyBindingError QUntypedPropertyBinding::error()
const
507 return QPropertyBindingError();
508 return static_cast<QPropertyBindingPrivate *>(d.get())->bindingError();
512
513
514
515QMetaType QUntypedPropertyBinding::valueMetaType()
const
519 return static_cast<QPropertyBindingPrivate *>(d.get())->valueMetaType();
522QPropertyBindingData::~QPropertyBindingData()
524 QPropertyBindingDataPointer d{
this};
525 if (isNotificationDelayed())
526 proxyData()->originalBindingData =
nullptr;
527 for (
auto observer = d.firstObserver(); observer;) {
528 auto next = observer.nextObserver();
532 if (
auto binding = d.binding())
533 binding->unlinkAndDeref();
536QUntypedPropertyBinding QPropertyBindingData::setBinding(
const QUntypedPropertyBinding &binding,
537 QUntypedPropertyData *propertyDataPtr,
538 QPropertyObserverCallback staticObserverCallback,
539 QtPrivate::QPropertyBindingWrapper guardCallback)
541 QPropertyBindingPrivatePtr oldBinding;
542 QPropertyBindingPrivatePtr newBinding = binding.d;
544 QPropertyBindingDataPointer d{
this};
545 QPropertyObserverPointer observer;
547 auto &data = d_ref();
548 if (
auto *existingBinding = d.binding()) {
549 if (existingBinding == newBinding.data())
550 return QUntypedPropertyBinding(
static_cast<QPropertyBindingPrivate *>(oldBinding.data()));
551 if (existingBinding->isUpdating()) {
552 existingBinding->setError({QPropertyBindingError::BindingLoop, QStringLiteral(
"Binding set during binding evaluation!")});
553 return QUntypedPropertyBinding(
static_cast<QPropertyBindingPrivate *>(oldBinding.data()));
555 oldBinding = QPropertyBindingPrivatePtr(existingBinding);
556 observer =
static_cast<QPropertyBindingPrivate *>(oldBinding.data())->takeObservers();
557 static_cast<QPropertyBindingPrivate *>(oldBinding.data())->unlinkAndDeref();
560 observer = d.firstObserver();
564 newBinding.data()->addRef();
565 data =
reinterpret_cast<quintptr>(newBinding.data());
567 auto newBindingRaw =
static_cast<QPropertyBindingPrivate *>(newBinding.data());
568 newBindingRaw->setProperty(propertyDataPtr);
570 newBindingRaw->prependObserver(observer);
571 newBindingRaw->setStaticObserver(staticObserverCallback, guardCallback);
573 PendingBindingObserverList bindingObservers;
574 newBindingRaw->evaluateRecursive(bindingObservers);
575 newBindingRaw->notifyNonRecursive(bindingObservers);
576 }
else if (observer) {
577 d.setObservers(observer.ptr);
583 static_cast<QPropertyBindingPrivate *>(oldBinding.data())->detachFromProperty();
585 return QUntypedPropertyBinding(
static_cast<QPropertyBindingPrivate *>(oldBinding.data()));
588QPropertyBindingData::QPropertyBindingData(QPropertyBindingData &&other) : d_ptr(std::exchange(other.d_ptr, 0))
590 QPropertyBindingDataPointer::fixupAfterMove(
this);
597 QBindingStatus *s = status;
600 currentState = &s->currentlyEvaluatingBinding;
603 binding->clearDependencyObservers();
615 currentlyEvaluatingBindingList = &bindingStatus().currentlyEvaluatingBinding;
620QPropertyBindingPrivate *QPropertyBindingPrivate::currentlyEvaluatingBinding()
622 auto currentState = bindingStatus().currentlyEvaluatingBinding ;
623 return currentState ? currentState->binding :
nullptr;
627void QPropertyBindingData::evaluateIfDirty(
const QUntypedPropertyData *)
const
631void QPropertyBindingData::removeBinding_helper()
633 QPropertyBindingDataPointer d{
this};
635 auto *existingBinding = d.binding();
636 Q_ASSERT(existingBinding);
637 if (existingBinding->isSticky()) {
641 auto observer = existingBinding->takeObservers();
644 d.setObservers(observer.ptr);
645 existingBinding->unlinkAndDeref();
648void QPropertyBindingData::registerWithCurrentlyEvaluatingBinding()
const
650 auto currentState = bindingStatus().currentlyEvaluatingBinding;
653 registerWithCurrentlyEvaluatingBinding_helper(currentState);
657void QPropertyBindingData::registerWithCurrentlyEvaluatingBinding_helper(BindingEvaluationState *currentState)
const
659 QPropertyBindingDataPointer d{
this};
661 if (currentState->alreadyCaptureProperties.contains(
this))
664 currentState->alreadyCaptureProperties.push_back(
this);
666 QPropertyObserverPointer dependencyObserver = currentState->binding->allocateDependencyObserver();
667 Q_ASSERT(QPropertyObserver::ObserverNotifiesBinding == 0);
668 dependencyObserver.setBindingToNotify_unsafe(currentState->binding);
669 d.addObserver(dependencyObserver.ptr);
672void QPropertyBindingData::notifyObservers(QUntypedPropertyData *propertyDataPtr)
const
674 notifyObservers(propertyDataPtr,
nullptr);
677void QPropertyBindingData::notifyObservers(QUntypedPropertyData *propertyDataPtr, QBindingStorage *storage)
const
679 if (isNotificationDelayed())
681 QPropertyBindingDataPointer d{
this};
683 PendingBindingObserverList bindingObservers;
684 if (QPropertyObserverPointer observer = d.firstObserver()) {
685 if (notifyObserver_helper(propertyDataPtr, storage, observer, bindingObservers) == Evaluated) {
687
688
689
690
691
692
694 d = QPropertyBindingDataPointer {storage->bindingData(propertyDataPtr)};
695 if (QPropertyObserverPointer observer = d.firstObserver())
696 observer.notify(propertyDataPtr);
697 for (
auto &&bindingPtr: bindingObservers) {
698 auto *binding =
static_cast<QPropertyBindingPrivate *>(bindingPtr.get());
699 binding->notifyNonRecursive();
705QPropertyBindingData::NotificationResult QPropertyBindingData::notifyObserver_helper
707 QUntypedPropertyData *propertyDataPtr, QBindingStorage *storage,
708 QPropertyObserverPointer observer,
709 PendingBindingObserverList &bindingObservers)
const
711#ifdef QT_HAS_FAST_CURRENT_THREAD_ID
712 QBindingStatus *status = storage ? storage->bindingStatus :
nullptr;
713 if (!status || status->threadId != QThread::currentThreadId())
714 status = &bindingStatus();
717 QBindingStatus *status = &bindingStatus();
719 if (QPropertyDelayedNotifications *delay = status->groupUpdateData) {
720 delay->addProperty(
this, propertyDataPtr);
724 observer.evaluateBindings(bindingObservers, status);
729QPropertyObserver::QPropertyObserver(ChangeHandler changeHandler)
731 QPropertyObserverPointer d{
this};
732 d.setChangeHandler(changeHandler);
735#if QT_DEPRECATED_SINCE(6
, 6
)
736QPropertyObserver::QPropertyObserver(QUntypedPropertyData *data)
738 QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED
740 next.setTag(ObserverIsAlias);
746
747void QPropertyObserver::setSource(
const QPropertyBindingData &property)
749 QPropertyObserverPointer d{
this};
750 QPropertyBindingDataPointer propPrivate{&property};
751 d.observeProperty(propPrivate);
754QPropertyObserver::~QPropertyObserver()
756 QPropertyObserverPointer d{
this};
760QPropertyObserver::QPropertyObserver(QPropertyObserver &&other)
noexcept
762 binding = std::exchange(other.binding, {});
763 next = std::exchange(other.next, {});
764 prev = std::exchange(other.prev, {});
768 prev.setPointer(
this);
771QPropertyObserver &QPropertyObserver::operator=(QPropertyObserver &&other)
noexcept
776 QPropertyObserverPointer d{
this};
780 binding = std::exchange(other.binding, {});
781 next = std::exchange(other.next, {});
782 prev = std::exchange(other.prev, {});
786 prev.setPointer(
this);
792
793
794
795
799
800
801
802
803
804
808 Q_ASSERT(ptr->next.tag() != QPropertyObserver::ObserverIsPlaceholder);
809 ptr->changeHandler = changeHandler;
810 ptr->next.setTag(QPropertyObserver::ObserverNotifiesChangeHandler);
814
815
816
819 Q_ASSERT(ptr->next.tag() == QPropertyObserver::ObserverNotifiesBinding);
820 ptr->binding = binding;
824
825
826
827
828
831
832
833
834
839 auto observer =
const_cast<QPropertyObserver*>(ptr);
842 if (QPropertyObserver::ObserverTag(observer->next.tag()) == QPropertyObserver::ObserverNotifiesBinding)
843 if (observer->binding == binding) {
844 qCritical(
"Property depends on itself!");
848 observer = observer->next.data();
857 auto observer =
const_cast<QPropertyObserver*>(ptr);
860 QPropertyObserver *next = observer->next.data();
862 if (QPropertyObserver::ObserverTag(observer->next.tag()) == QPropertyObserver::ObserverNotifiesBinding) {
863 auto bindingToEvaluate = observer->binding;
864 QPropertyObserverNodeProtector protector(observer);
866 QPropertyBindingPrivatePtr currentBinding(observer->binding);
867 const bool evalStatus = bindingToEvaluate->evaluateRecursive_inline(bindingObservers, status);
869 bindingObservers.push_back(std::move(currentBinding));
870 next = protector.next();
881 property.addObserver(
ptr);
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
927
928
929
930
931QPropertyBindingError::QPropertyBindingError()
936
937
938
939QPropertyBindingError::QPropertyBindingError(Type type,
const QString &description)
941 if (type != NoError) {
942 d =
new QPropertyBindingErrorPrivate;
944 d->description = description;
949
950
951QPropertyBindingError::QPropertyBindingError(
const QPropertyBindingError &other)
957
958
959QPropertyBindingError &QPropertyBindingError::operator=(
const QPropertyBindingError &other)
966
967
968
969QPropertyBindingError::QPropertyBindingError(QPropertyBindingError &&other)
970 : d(std::move(other.d))
975
976
977
978QPropertyBindingError &QPropertyBindingError::operator=(QPropertyBindingError &&other)
980 d = std::move(other.d);
985
986
987QPropertyBindingError::~QPropertyBindingError()
992
993
994
995
996QPropertyBindingError::Type QPropertyBindingError::type()
const
999 return QPropertyBindingError::NoError;
1004
1005
1006
1007QString QPropertyBindingError::description()
const
1011 return d->description;
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1034
1035
1036
1037
1038
1039
1040
1043
1044
1045
1046
1047
1050
1051
1052
1053
1054
1055
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1074
1075
1076
1077
1078
1081
1082
1083
1084
1085
1086
1087
1088
1091
1092
1093
1094
1095
1098
1099
1100
1101
1102
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1117
1118
1119
1120
1123
1124
1125
1126
1127
1130
1131
1132
1133
1134
1135
1136
1137
1140
1141
1142
1143
1144
1145
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1160
1161
1162
1163
1164
1165
1166
1169
1170
1171
1172
1173
1174
1175
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1190
1191
1192
1193
1196
1197
1198
1199
1200
1201
1202
1203
1204
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1259
1260
1261
1262
1263
1266
1267
1268
1269
1270
1273
1274
1275
1276
1277
1278
1279
1280
1283
1284
1285
1286
1287
1288
1289
1290
1294
1295
1296
1297
1298
1299
1300
1301
1304
1305
1306
1307
1308
1311
1312
1313
1314
1315
1316
1317
1320
1321
1322
1323
1324
1325
1326
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1358
1359
1360
1361
1364
1365
1366
1367
1370
1371
1372
1373
1376
1377
1378
1379
1380
1383
1384
1385
1386
1387
1388
1389
1392
1393
1394
1395
1396
1397
1398
1401
1402
1403
1404
1407
1408
1409
1410
1411
1414
1415
1416
1417
1418
1419
1422
1423
1424
1425
1426
1429
1430
1431
1432
1433
1434
1435
1436
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1467
1468
1469
1470
1471
1472
1475
1476
1477
1478
1479
1480
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1498
1499
1500
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
1532
1533
1534
1537
1538
1539
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
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
1611
1612
1613
1614
1615
1616
1617
1618
1619
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
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
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1709
1710
1711
1712
1713
1714
1715
1716
1717
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1767
1768
1769
1770
1771
1772
1773
1774
1775
1779
1780
1781
1784
1785
1786
1787
1790
1791
1792
1793
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1820
1821
1822
1823
1824
1825
1826
1827
1830
1831
1832
1833
1836
1837
1838
1839
1840
1843
1844
1845
1846
1847
1848
1849
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1910
1911
1912
1913
1917
1918
1919
1920
1921
1922
1925
1926
1927
1928
1929
1930
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1984
1985
1986
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2063
2064
2065
2066
2069
2070
2071
2072
2075
2076
2077
2078
2079
2082
2083
2084
2085
2086
2089
2090
2091
2092
2093
2096
2097
2098
2099
2100
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2147
2148
2149
2150
2151
2154
2155
2156
2157
2158
2159
2162
2163
2164
2165
2166
2167
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2221
2222
2223
2224
2225
2226
2245 static_assert(
alignof(
Pair) ==
alignof(
void *));
2246 static_assert(
alignof(
size_t) ==
alignof(
void *));
2253 return reinterpret_cast<
Pair *>(dd + 1);
2257 Q_ASSERT(!
d || newSize >
d->size);
2259 void *nd = calloc(1, allocSize);
2261 newData->size = newSize;
2266 newData->used =
d->used;
2268 for (size_t i = 0; i <
d->size; ++i, ++p) {
2271 Q_ASSERT(newData->size && (newData->size & (newData->size - 1)) == 0);
2272 size_t index = qHash(p->data) & (newData->size - 1);
2273 while (pp[index].data) {
2275 if (index == newData->size)
2278 new (pp + index)
Pair{p->data, QPropertyBindingData(
std::move(p->bindingData))};
2283 QtPrivate::sizedFree(d, oldAllocSize);
2292 Q_ASSERT(
d->size && (
d->size & (
d->size - 1)) == 0);
2293 size_t index = qHash(data) & (
d->size - 1);
2295 while (p[index].data) {
2296 if (p[index].data == data)
2297 return &p[index].bindingData;
2299 if (index ==
d->size)
2311 else if (d->used*2 >= d->size)
2312 reallocate(d->size*2);
2313 Q_ASSERT(
d->size && (
d->size & (
d->size - 1)) == 0);
2314 size_t index = qHash(data) & (
d->size - 1);
2316 while (p[index].data) {
2317 if (p[index].data == data)
2318 return &p[index].bindingData;
2320 if (index ==
d->size)
2326 new (p + index)
Pair{data, QPropertyBindingData()};
2327 return &p[index].bindingData;
2335 for (size_t i = 0; i <
d->size; ++i) {
2341 QtPrivate::sizedFree(d, allocSize);
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2357QBindingStorage::QBindingStorage()
2359 bindingStatus = &QT_PREPEND_NAMESPACE(bindingStatus)();
2360 Q_ASSERT(bindingStatus);
2363QBindingStorage::~QBindingStorage()
2365 QBindingStoragePrivate(d).destroy();
2368void QBindingStorage::reinitAfterThreadMove()
2370 bindingStatus = &QT_PREPEND_NAMESPACE(bindingStatus)();
2371 Q_ASSERT(bindingStatus);
2374void QBindingStorage::clear()
2376 QBindingStoragePrivate(d).destroy();
2378 bindingStatus =
nullptr;
2381void QBindingStorage::registerDependency_helper(
const QUntypedPropertyData *data)
const
2383 Q_ASSERT(bindingStatus);
2386 QtPrivate::BindingEvaluationState *currentBinding;
2387#ifdef QT_HAS_FAST_CURRENT_THREAD_ID
2388 const bool threadMatches = (QThread::currentThreadId() == bindingStatus->threadId);
2389 if (Q_LIKELY(threadMatches))
2390 currentBinding = bindingStatus->currentlyEvaluatingBinding;
2392 currentBinding = QT_PREPEND_NAMESPACE(bindingStatus)().currentlyEvaluatingBinding;
2394 currentBinding = QT_PREPEND_NAMESPACE(bindingStatus)().currentlyEvaluatingBinding;
2396 QUntypedPropertyData *dd =
const_cast<QUntypedPropertyData *>(data);
2397 if (!currentBinding)
2399 auto storage = QBindingStoragePrivate(d).get(dd,
true);
2402 storage->registerWithCurrentlyEvaluatingBinding(currentBinding);
2406QPropertyBindingData *QBindingStorage::bindingData_helper(
const QUntypedPropertyData *data)
const
2408 return QBindingStoragePrivate(d).get(data);
2411const QBindingStatus *QBindingStorage::status(QtPrivate::QBindingStatusAccessToken)
const
2413 return bindingStatus;
2416QPropertyBindingData *QBindingStorage::bindingData_helper(QUntypedPropertyData *data,
bool create)
2418 return QBindingStoragePrivate(d).get(data, create);
2425void initBindingStatusThreadId()
2427 bindingStatus().threadId = QThread::currentThreadId();
2432 auto ret = bindingStatus().currentlyEvaluatingBinding;
2443
2444
2445
2446
2447
2448
2449
2452 return bindingStatus().currentlyEvaluatingBinding !=
nullptr;
2458 if (
const auto current = bindingStatus().currentCompatProperty)
2459 return current->property == property;
2468 case QtPrivate::BindableWarnings::NonBindableInterface:
2469 qCWarning(lcQPropertyBinding).noquote() << prefix.toString()
2470 <<
"The QBindable does not allow interaction with the binding.";
2472 case QtPrivate::BindableWarnings::ReadOnlyInterface:
2473 qCWarning(lcQPropertyBinding).noquote() << prefix.toString()
2474 <<
"The QBindable is read-only.";
2477 case QtPrivate::BindableWarnings::InvalidInterface:
2478 qCWarning(lcQPropertyBinding).noquote() << prefix.toString()
2479 <<
"The QBindable is invalid.";
2486 qCWarning(lcQPropertyBinding) <<
"setBinding: Could not set binding as the property expects it to be of type"
2488 <<
"but got" << expected.name() <<
"instead.";
2494
2495
2496
2509void getter(
const QUntypedPropertyData *d,
void *value)
2512 adaptor->bindingData().registerWithCurrentlyEvaluatingBinding();
2513 auto mt = adaptor->metaProperty().metaType();
2515 mt.construct(value, adaptor->metaProperty().read(adaptor->object()).data());
2518void setter(QUntypedPropertyData *d,
const void *value)
2521 adaptor->bindingData().removeBinding();
2522 adaptor->metaProperty().write(adaptor->object(),
2523 QVariant(adaptor->metaProperty().metaType(), value));
2529 return QUntypedPropertyBinding(adaptor->bindingData().binding());
2533 QtPrivate::QPropertyBindingFunction binding, QUntypedPropertyData *temp,
2537 type.destruct(value);
2538 type.construct(value, adaptor->metaProperty().read(adaptor->object()).data());
2539 if (binding.vtable->call(type, temp, binding.functor)) {
2540 adaptor->metaProperty().write(adaptor->object(), QVariant(type, value));
2547 QPropertyBindingWrapper wrapper)
2550 return adaptor->bindingData().setBinding(binding, d,
nullptr, wrapper);
2553void setObserver(
const QUntypedPropertyData *d, QPropertyObserver *observer)
2560 : QSlotObjectBase(&impl), obj(o), metaProperty_(p)
2564#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
2578 if (!self->bindingData_.hasBinding())
2579 self->bindingData_.notifyObservers(self);
2599 if (!metaProperty.isValid()) {
2600 qCWarning(lcQPropertyBinding) <<
"QUntypedBindable: Property is not valid";
2604 if (metaProperty.isBindable()) {
2605 *
this = metaProperty.bindable(obj);
2609 if (!metaProperty.hasNotifySignal()) {
2610 qCWarning(lcQPropertyBinding)
2611 <<
"QUntypedBindable: Property" << metaProperty.name() <<
"has no notify signal";
2615 auto metatype =
iface->metaType();
2616 if (metaProperty.metaType() != metatype) {
2617 qCWarning(lcQPropertyBinding) <<
"QUntypedBindable: Property" << metaProperty.name()
2618 <<
"of type" << metaProperty.metaType().name()
2619 <<
"does not match requested type" << metatype.name();
2624 if (obj->metaObject()->property(metaProperty.propertyIndex()).name() != metaProperty.name()) {
2625 qCWarning(lcQPropertyBinding) <<
"QUntypedBindable: Property" << metaProperty.name()
2626 <<
"does not belong to this object";
2631 auto adaptor = QObjectPrivate::get(obj)->getPropertyAdaptorSlotObject(metaProperty);
2636 auto c = QObjectPrivate::connect(obj, metaProperty.notifySignalIndex(), obj, adaptor,
2637 Qt::DirectConnection);
2648 [=]() -> QMetaProperty {
2651 auto propertyIndex = obj->metaObject()->indexOfProperty(property);
2652 if (propertyIndex < 0) {
2653 qCWarning(lcQPropertyBinding)
2654 <<
"QUntypedBindable: No property named" << property;
2657 return obj->metaObject()->property(propertyIndex);
const QtPrivate::QBindableInterface * iface
friend class QT_PREPEND_NAMESPACE(QUntypedBindable)
void printMetaTypeMismatch(QMetaType actual, QMetaType expected)
void printUnsuitableBindableWarning(QAnyStringView prefix, BindableWarnings::Reason reason)
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