summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qmetatype.cpp
blob: 7977335c981219e07e6aeada6a3b8cb1fe4916f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
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
1245
1246
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
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
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
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
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
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
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
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
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
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
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
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
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
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qmetatype.h"
#include "qmetatype_p.h"
#include "qobjectdefs.h"
#include "qdatetime.h"
#include "qbytearray.h"
#include "qreadwritelock.h"
#include "qstring.h"
#include "qstringlist.h"
#include "qlist.h"
#include "qlocale.h"
#include "qdebug.h"
#if QT_CONFIG(easingcurve)
#include "qeasingcurve.h"
#endif
#include "quuid.h"
#include "qvariant.h"
#include "qdatastream.h"

#if QT_CONFIG(regularexpression)
#  include "qregularexpression.h"
#endif

#ifndef QT_BOOTSTRAPPED
#  include "qbitarray.h"
#  include "qurl.h"
#  include "qvariant.h"
#  include "qjsonvalue.h"
#  include "qjsonobject.h"
#  include "qjsonarray.h"
#  include "qjsondocument.h"
#  include "qcborvalue.h"
#  include "qcborarray.h"
#  include "qcbormap.h"
#  include "qbytearraylist.h"
#  include "qmetaobject.h"
#  include "qsequentialiterable.h"
#  include "qassociativeiterable.h"
#endif

#if QT_CONFIG(itemmodel)
#  include "qabstractitemmodel.h"
#endif

#ifndef QT_NO_GEOM_VARIANT
# include "qsize.h"
# include "qpoint.h"
# include "qrect.h"
# include "qline.h"
#endif

#include <bitset>
#include <new>
#include <cstring>

QT_BEGIN_NAMESPACE

#define NS(x) QT_PREPEND_NAMESPACE(x)


namespace {
struct DefinedTypesFilter {
    template<typename T>
    struct Acceptor {
        static const bool IsAccepted = QtMetaTypePrivate::TypeDefinition<T>::IsAvailable && QModulesPrivate::QTypeModuleInfo<T>::IsCore;
    };
};

struct QMetaTypeCustomRegistry
{
    QReadWriteLock lock;
    QList<QtPrivate::QMetaTypeInterface *> registry;
    QHash<QByteArray, QtPrivate::QMetaTypeInterface *> aliases;
    // index of first empty (unregistered) type in registry, if any.
    int firstEmpty = 0;

    int registerCustomType(QtPrivate::QMetaTypeInterface *ti)
    {
        {
            QWriteLocker l(&lock);
            if (ti->typeId)
                return ti->typeId;
            QByteArray name =
#ifndef QT_NO_QOBJECT
                    QMetaObject::normalizedType
#endif
                    (ti->name);
            if (auto ti2 = aliases.value(name)) {
                ti->typeId.storeRelaxed(ti2->typeId.loadRelaxed());
                return ti2->typeId;
            }
            aliases[name] = ti;
            int size = registry.size();
            while (firstEmpty < size && registry[firstEmpty])
                ++firstEmpty;
            if (firstEmpty < size) {
                registry[firstEmpty] = ti;
                ++firstEmpty;
            } else {
                registry.append(ti);
                firstEmpty = registry.size();
            }
            ti->typeId = firstEmpty + QMetaType::User;
        }
        if (ti->legacyRegisterOp)
            ti->legacyRegisterOp();
        return ti->typeId;
    };

    void unregisterDynamicType(int id)
    {
        if (!id)
            return;
        Q_ASSERT(id > QMetaType::User);
        QWriteLocker l(&lock);
        int idx = id - QMetaType::User - 1;
        auto &ti = registry[idx];

        // We must unregister all names.
        auto it = aliases.begin();
        while (it != aliases.end()) {
            if (it.value() == ti)
                it = aliases.erase(it);
            else
                ++it;
        }

        ti = nullptr;

        firstEmpty = std::min(firstEmpty, idx);
    }

    QtPrivate::QMetaTypeInterface *getCustomType(int id)
    {
        QReadLocker l(&lock);
        return registry.value(id - QMetaType::User - 1);
    }
};

Q_GLOBAL_STATIC(QMetaTypeCustomRegistry, customTypeRegistry)

} // namespace

/*!
    \macro Q_DECLARE_OPAQUE_POINTER(PointerType)
    \relates QMetaType
    \since 5.0

    This macro enables pointers to forward-declared types (\a PointerType)
    to be registered with QMetaType using either Q_DECLARE_METATYPE()
    or qRegisterMetaType().

    \sa Q_DECLARE_METATYPE(), qRegisterMetaType()
*/

/*!
    \macro Q_DECLARE_METATYPE(Type)
    \relates QMetaType

    This macro makes the type \a Type known to QMetaType as long as it
    provides a public default constructor, a public copy constructor and
    a public destructor.
    It is needed to use the type \a Type as a custom type in QVariant.

    This macro requires that \a Type is a fully defined type at the point where
    it is used. For pointer types, it also requires that the pointed to type is
    fully defined. Use in conjunction with Q_DECLARE_OPAQUE_POINTER() to
    register pointers to forward declared types.

    Ideally, this macro should be placed below the declaration of
    the class or struct. If that is not possible, it can be put in
    a private header file which has to be included every time that
    type is used in a QVariant.

    Adding a Q_DECLARE_METATYPE() makes the type known to all template
    based functions, including QVariant. Note that if you intend to
    use the type in \e queued signal and slot connections or in
    QObject's property system, you also have to call
    qRegisterMetaType() since the names are resolved at runtime.

    This example shows a typical use case of Q_DECLARE_METATYPE():

    \snippet code/src_corelib_kernel_qmetatype.cpp 0

    If \c MyStruct is in a namespace, the Q_DECLARE_METATYPE() macro
    has to be outside the namespace:

    \snippet code/src_corelib_kernel_qmetatype.cpp 1

    Since \c{MyStruct} is now known to QMetaType, it can be used in QVariant:

    \snippet code/src_corelib_kernel_qmetatype.cpp 2

    Some types are registered automatically and do not need this macro:

    \list
    \li Pointers to classes derived from QObject
    \li QList<T>, QQueue<T>, QStack<T> or QSet<T>
        where T is a registered meta type
    \li QHash<T1, T2>, QMap<T1, T2> or QPair<T1, T2> where T1 and T2 are
        registered meta types
    \li QPointer<T>, QSharedPointer<T>, QWeakPointer<T>, where T is a class that derives from QObject
    \li Enumerations registered with Q_ENUM or Q_FLAG
    \li Classes that have a Q_GADGET macro
    \endlist

    \note This method also registers the stream and debug operators for the type if they
    are visible at registration time. As this is done automatically in some places,
    it is strongly recommended to declare the stream operators for a type directly
    after the type itself.

    The stream operators should have the following signatures:

    \snippet code/src_corelib_kernel_qmetatype.cpp 6

    \sa qRegisterMetaType()
*/

/*!
    \macro Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(Container)
    \relates QMetaType

    This macro makes the container \a Container known to QMetaType as a sequential
    container. This makes it possible to put an instance of Container<T> into
    a QVariant, if T itself is known to QMetaType.

    Note that all of the Qt sequential containers already have built-in
    support, and it is not necessary to use this macro with them. The
    std::vector and std::list containers also have built-in support.

    This example shows a typical use of Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE():

    \snippet code/src_corelib_kernel_qmetatype.cpp 10
*/

/*!
    \macro Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(Container)
    \relates QMetaType

    This macro makes the container \a Container known to QMetaType as an associative
    container. This makes it possible to put an instance of Container<T, U> into
    a QVariant, if T and U are themselves known to QMetaType.

    Note that all of the Qt associative containers already have built-in
    support, and it is not necessary to use this macro with them. The
    std::map container also has built-in support.

    This example shows a typical use of Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE():

    \snippet code/src_corelib_kernel_qmetatype.cpp 11
*/

/*!
    \macro Q_DECLARE_SMART_POINTER_METATYPE(SmartPointer)
    \relates QMetaType

    This macro makes the smart pointer \a SmartPointer known to QMetaType as a
    smart pointer. This makes it possible to put an instance of SmartPointer<T> into
    a QVariant, if T is a type which inherits QObject.

    Note that the QWeakPointer, QSharedPointer and QPointer already have built-in
    support, and it is not necessary to use this macro with them.

    This example shows a typical use of Q_DECLARE_SMART_POINTER_METATYPE():

    \snippet code/src_corelib_kernel_qmetatype.cpp 13
*/

/*!
    \enum QMetaType::Type

    These are the built-in types supported by QMetaType:

    \value Void \c void
    \value Bool \c bool
    \value Int \c int
    \value UInt \c{unsigned int}
    \value Double \c double
    \value QChar QChar
    \value QString QString
    \value QByteArray QByteArray
    \value Nullptr \c{std::nullptr_t}

    \value VoidStar \c{void *}
    \value Long \c{long}
    \value LongLong LongLong
    \value Short \c{short}
    \value Char \c{char}
    \value ULong \c{unsigned long}
    \value ULongLong ULongLong
    \value UShort \c{unsigned short}
    \value SChar \c{signed char}
    \value UChar \c{unsigned char}
    \value Float \c float
    \value QObjectStar QObject *
    \value QVariant QVariant

    \value QCursor QCursor
    \value QDate QDate
    \value QSize QSize
    \value QTime QTime
    \value QVariantList QVariantList
    \value QPolygon QPolygon
    \value QPolygonF QPolygonF
    \value QColor QColor
    \value QColorSpace QColorSpace (introduced in Qt 5.15)
    \value QSizeF QSizeF
    \value QRectF QRectF
    \value QLine QLine
    \value QTextLength QTextLength
    \value QStringList QStringList
    \value QVariantMap QVariantMap
    \value QVariantHash QVariantHash
    \value QIcon QIcon
    \value QPen QPen
    \value QLineF QLineF
    \value QTextFormat QTextFormat
    \value QRect QRect
    \value QPoint QPoint
    \value QUrl QUrl
    \value QRegularExpression QRegularExpression
    \value QDateTime QDateTime
    \value QPointF QPointF
    \value QPalette QPalette
    \value QFont QFont
    \value QBrush QBrush
    \value QRegion QRegion
    \value QBitArray QBitArray
    \value QImage QImage
    \value QKeySequence QKeySequence
    \value QSizePolicy QSizePolicy
    \value QPixmap QPixmap
    \value QLocale QLocale
    \value QBitmap QBitmap
    \value QTransform QTransform
    \value QMatrix4x4 QMatrix4x4
    \value QVector2D QVector2D
    \value QVector3D QVector3D
    \value QVector4D QVector4D
    \value QQuaternion QQuaternion
    \value QEasingCurve QEasingCurve
    \value QJsonValue QJsonValue
    \value QJsonObject QJsonObject
    \value QJsonArray QJsonArray
    \value QJsonDocument QJsonDocument
    \value QCborValue QCborValue
    \value QCborArray QCborArray
    \value QCborMap QCborMap
    \value QCborSimpleType QCborSimpleType
    \value QModelIndex QModelIndex
    \value QPersistentModelIndex QPersistentModelIndex (introduced in Qt 5.5)
    \value QUuid QUuid
    \value QByteArrayList QByteArrayList

    \value User  Base value for user types
    \value UnknownType This is an invalid type id. It is returned from QMetaType for types that are not registered
    \omitvalue LastCoreType
    \omitvalue LastGuiType

    Additional types can be registered using Q_DECLARE_METATYPE().

    \sa type(), typeName()
*/

/*!
    \enum QMetaType::TypeFlag

    The enum describes attributes of a type supported by QMetaType.

    \value NeedsConstruction This type has non-trivial constructors. If the flag is not set instances can be safely initialized with memset to 0.
    \value NeedsDestruction This type has a non-trivial destructor. If the flag is not set calls to the destructor are not necessary before discarding objects.
    \value MovableType An instance of a type having this attribute can be safely moved by memcpy.
    \omitvalue SharedPointerToQObject
    \value IsEnumeration This type is an enumeration
    \value If the type is an Enumeration, its underlying type is unsigned
    \value PointerToQObject This type is a pointer to a derived of QObject
    \omitvalue WeakPointerToQObject
    \omitvalue TrackingPointerToQObject
    \omitvalue IsGadget \omit This type is a Q_GADGET and it's corresponding QMetaObject can be accessed with QMetaType::metaObject Since 5.5. \endomit
    \omitvalue PointerToGadget
*/

/*!
    \class QMetaType
    \inmodule QtCore
    \brief The QMetaType class manages named types in the meta-object system.

    \ingroup objectmodel
    \threadsafe

    The class is used as a helper to marshall types in QVariant and
    in queued signals and slots connections. It associates a type
    name to a type so that it can be created and destructed
    dynamically at run-time. Declare new types with Q_DECLARE_METATYPE()
    to make them available to QVariant and other template-based functions.
    Call qRegisterMetaType() to make types available to non-template based
    functions, such as the queued signal and slot connections.

    Any class or struct that has a public default
    constructor, a public copy constructor, and a public destructor
    can be registered.

    The following code allocates and destructs an instance of
    \c{MyClass}:

    \snippet code/src_corelib_kernel_qmetatype.cpp 3

    If we want the stream operators \c operator<<() and \c
    operator>>() to work on QVariant objects that store custom types,
    the custom type must provide \c operator<<() and \c operator>>()
    operators.

    \sa Q_DECLARE_METATYPE(), QVariant::setValue(), QVariant::value(), QVariant::fromValue()
*/

/*!
    \fn bool QMetaType::isValid() const
    \since 5.0

    Returns \c true if this QMetaType object contains valid
    information about a type, false otherwise.
*/
bool QMetaType::isValid() const
{
    return d_ptr;
}

/*!
    \fn bool QMetaType::isRegistered() const
    \since 5.0

    Returns \c true if this QMetaType object contains valid
    information about a type, false otherwise.
*/
bool QMetaType::isRegistered() const
{
    return d_ptr;
}

/*!
    \fn int QMetaType::id() const
    \since 5.13

    Returns id type hold by this QMetatype instance.
*/
int QMetaType::id() const
{
    if (d_ptr) {
        if (d_ptr->typeId)
            return d_ptr->typeId;
        auto reg = customTypeRegistry();
        if (reg) {
            return reg->registerCustomType(d_ptr);
        }
    }
    return 0;
}

/*!
    \fn bool QMetaType::sizeOf() const
    \since 5.0

    Returns the size of the type in bytes (i.e. sizeof(T),
    where T is the actual type for which this QMetaType instance
    was constructed for).

    This function is typically used together with construct()
    to perform low-level management of the memory used by a type.

    \sa QMetaType::construct(), QMetaType::sizeOf(), QMetaType::alignOf()
*/
qsizetype QMetaType::sizeOf() const
{
    if (d_ptr)
        return d_ptr->size;
    return 0;
}

/*!
  \fn int QMetaType::alignOf() const
  \since 6.0

  Returns the alignment of the type in bytes (i.e. alignof(T),
  where T is the actual type for which this QMetaType instance
  was constructed for).

  This function is typically used together with construct()
  to perform low-level management of the memory used by a type.

  \sa QMetaType::construct(), QMetaType::sizeOf()

 */
qsizetype QMetaType::alignOf() const
{
    if (d_ptr)
        return d_ptr->alignment;
    return 0;
}

/*!
    \fn TypeFlags QMetaType::flags() const
    \since 5.0

    Returns flags of the type for which this QMetaType instance was constructed.

    \sa QMetaType::TypeFlags, QMetaType::flags()
*/
QMetaType::TypeFlags QMetaType::flags() const
{
    if (d_ptr)
        return TypeFlags(d_ptr->flags);
    return {};
}

/*!
    \fn const QMetaObject *QMetaType::metaObject() const
    \since 5.5

    return a QMetaObject relative to this type.

    If the type is a pointer type to a subclass of QObject, flags() contains
    QMetaType::PointerToQObject and this function returns the corresponding QMetaObject. This can
    be used to in combinaison with QMetaObject::construct to create QObject of this type.

    If the type is a Q_GADGET, flags() contains QMetaType::IsGadget, and this function returns its
    QMetaObject.  This can be used to retrieve QMetaMethod and QMetaProperty and use them on a
    pointer of this type. (given by QVariant::data for example)

    If the type is an enumeration, flags() contains QMetaType::IsEnumeration, and this function
    returns the QMetaObject of the enclosing object if the enum was registered as a Q_ENUM or
    \nullptr otherwise

    \sa QMetaType::flags()
*/
const QMetaObject *QMetaType::metaObject() const
{
    return d_ptr ? d_ptr->metaObject : nullptr;
}

/*!
    \fn void *QMetaType::create(const void *copy = 0) const
    \since 5.0

    Returns a copy of \a copy, assuming it is of the type that this
    QMetaType instance was created for. If \a copy is \nullptr, creates
    a default constructed instance.

    \sa QMetaType::destroy()
*/
void *QMetaType::create(const void *copy) const
{
    if (d_ptr) {
        void *where =
#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
            d_ptr->alignment > __STDCPP_DEFAULT_NEW_ALIGNMENT__ ?
                operator new(d_ptr->size, std::align_val_t(d_ptr->alignment)) :
#endif
                operator new(d_ptr->size);
        return construct(where, copy);
    }
    return nullptr;
}

/*!
    \fn void QMetaType::destroy(void *data) const
    \since 5.0

    Destroys the \a data, assuming it is of the type that this
    QMetaType instance was created for.

    \sa QMetaType::create()
*/
void QMetaType::destroy(void *data) const
{
    if (d_ptr && d_ptr->dtor) {
        d_ptr->dtor(d_ptr, data);
        if (d_ptr->alignment > __STDCPP_DEFAULT_NEW_ALIGNMENT__) {
            operator delete(data, std::align_val_t(d_ptr->alignment));
        } else {
            operator delete(data);
        }
    }
}

/*!
    \fn void *QMetaType::construct(void *where, const void *copy = 0) const
    \since 5.0

    Constructs a value of the type that this QMetaType instance
    was constructed for in the existing memory addressed by \a where,
    that is a copy of \a copy, and returns \a where. If \a copy is
    zero, the value is default constructed.

    This is a low-level function for explicitly managing the memory
    used to store the type. Consider calling create() if you don't
    need this level of control (that is, use "new" rather than
    "placement new").

    You must ensure that \a where points to a location where the new
    value can be stored and that \a where is suitably aligned.
    The type's size can be queried by calling sizeOf().

    The rule of thumb for alignment is that a type is aligned to its
    natural boundary, which is the smallest power of 2 that is bigger
    than the type, unless that alignment is larger than the maximum
    useful alignment for the platform. For practical purposes,
    alignment larger than 2 * sizeof(void*) is only necessary for
    special hardware instructions (e.g., aligned SSE loads and stores
    on x86).
*/
void *QMetaType::construct(void *where, const void *copy) const
{
    if (!where)
        return nullptr;
    if (d_ptr) {
        if (copy && d_ptr->copyCtr) {
            d_ptr->copyCtr(d_ptr, where, copy);
            return where;
        } else if (!copy && d_ptr->defaultCtr) {
            d_ptr->defaultCtr(d_ptr, where);
            return where;
        }
    }
    return nullptr;
}

/*!
    \fn void QMetaType::destruct(void *data) const
    \since 5.0

    Destructs the value, located at \a data, assuming that it is
    of the type for which this QMetaType instance was constructed for.

    Unlike destroy(), this function only invokes the type's
    destructor, it doesn't invoke the delete operator.
    \sa QMetaType::construct()
*/
void QMetaType::destruct(void *data) const
{
    if (!data)
        return;
    if (d_ptr && d_ptr->dtor) {
        d_ptr->dtor(d_ptr, data);
        return;
    }
}

/*!
    Compares the objects at \a lhs and \a rhs for ordering.

    Returns an empty optional if comparison is not supported or the values are unordered.
    Otherwise, returns -1, 0 or +1 according as \a lhs is less than, equal to or greater
    than \a rhs.

    Both objects must be of the type described by this metatype. If either \a lhs
    or \a rhs is \nullptr, the values are unordered. Comparison is only supported
    if the type's less than operator was visible to the metatype declaration.

    If the type's equality operator was also visible, values will only compare equal if the
    equality operator says they are. In the absence of an equality operator, when neither
    value is less than the other, values are considered equal; if equality is also available
    and two such values are not equal, they are considered unordered, just as NaN (not a
    number) values of a floating point type lie outside its ordering.

    \note If no less than operator was visible to the metatype declaration, values are
    unordered even if an equality operator visible to the declaration considers them equal:
    \c{compare() == 0} only agrees with equals() if the less than operator was visible.

    \since 6.0
    \sa equals(), isOrdered()
*/
std::optional<int> QMetaType::compare(const void *lhs, const void *rhs) const
{
    if (!lhs || !rhs)
        return std::optional<int>{};
    if (d_ptr->flags & QMetaType::IsPointer)
        return std::less<const void *>()(*reinterpret_cast<const void * const *>(lhs),
                                         *reinterpret_cast<const void * const *>(rhs));
    if (d_ptr && d_ptr->lessThan) {
        if (d_ptr->equals && d_ptr->equals(d_ptr, lhs, rhs))
            return 0;
        if (d_ptr->lessThan(d_ptr, lhs, rhs))
            return -1;
        if (d_ptr->lessThan(d_ptr, rhs, lhs))
            return 1;
        if (!d_ptr->equals)
            return 0;
    }
    return std::optional<int>{};
}

/*!
    Compares the objects at \a lhs and \a rhs for equality.

    Both objects must be of the type described by this metatype.  Can only compare the
    two objects if a less than or equality operator for the type was visible to the
    metatype declaration.  Otherwise, the metatype never considers values equal.  When
    an equality operator was visible to the metatype declaration, it is authoritative;
    otherwise, if less than is visible, when neither value is less than the other, the
    two are considered equal.  If values are unordered (see compare() for details) they
    are not equal.

    Returns true if the two objects compare equal, otherwise false.

    \since 6.0
    \sa isEqualityComparable(), compare()
*/
bool QMetaType::equals(const void *lhs, const void *rhs) const
{
    if (!lhs || !rhs)
        return false;
    if (d_ptr) {
        if (d_ptr->flags & QMetaType::IsPointer)
            return *reinterpret_cast<const void * const *>(lhs) == *reinterpret_cast<const void * const *>(rhs);

        if (d_ptr->equals)
            return d_ptr->equals(d_ptr, lhs, rhs);
        if (d_ptr->lessThan && !d_ptr->lessThan(d_ptr, lhs, rhs) && !d_ptr->lessThan(d_ptr, rhs, lhs))
            return true;
    }
    return false;
}

/*!
    Returns \c true if a less than or equality operator for the type described by
    this metatype was visible to the metatype declaration, otherwise \c false.

    \sa equals(), isOrdered()
*/
bool QMetaType::isEqualityComparable() const
{
    return d_ptr && (d_ptr->flags & QMetaType::IsPointer || d_ptr->equals != nullptr || d_ptr->lessThan != nullptr);
}

/*!
    Returns \c true if a less than operator for the type described by this metatype
    was visible to the metatype declaration, otherwise \c false.

    \sa compare(), isEqualityComparable()
*/
bool QMetaType::isOrdered() const
{
    return d_ptr && (d_ptr->flags & QMetaType::IsPointer || d_ptr->lessThan != nullptr);
}


/*!
   \internal
*/
void QMetaType::unregisterMetaType(QMetaType type)
{
    if (type.d_ptr && type.d_ptr->typeId.loadRelaxed() >= QMetaType::User) {
        if (auto reg = customTypeRegistry())
            reg->unregisterDynamicType(type.d_ptr->typeId.loadRelaxed());
        type.d_ptr->typeId.storeRelease(0);
    }
}

/*!
    \fn QMetaType::~QMetaType()

    Destructs this object.
*/


/*!
    \fn template<typename T> QMetaType QMetaType::fromType()
    \since 5.15

    Returns the QMetaType corresponding to the type in the template parameter.
*/

/*! \fn bool operator==(QMetaType a, QMetaType b)
    \since 5.15
    \relates QMetaType
    \overload

    Returns \c true if the QMetaType \a a represents the same type
    as the QMetaType \a b, otherwise returns \c false.
*/

/*! \fn bool operator!=(QMetaType a, QMetaType b)
    \since 5.15
    \relates QMetaType
    \overload

    Returns \c true if the QMetaType \a a represents a different type
    than the QMetaType \a b, otherwise returns \c false.
*/

#define QT_ADD_STATIC_METATYPE(MetaTypeName, MetaTypeId, RealName) \
    { #RealName, sizeof(#RealName) - 1, MetaTypeId },

#define QT_ADD_STATIC_METATYPE_ALIASES_ITER(MetaTypeName, MetaTypeId, AliasingName, RealNameStr) \
    { RealNameStr, sizeof(RealNameStr) - 1, QMetaType::MetaTypeName },



static const struct { const char * typeName; int typeNameLength; int type; } types[] = {
    QT_FOR_EACH_STATIC_TYPE(QT_ADD_STATIC_METATYPE)
    QT_FOR_EACH_STATIC_ALIAS_TYPE(QT_ADD_STATIC_METATYPE_ALIASES_ITER)
    QT_ADD_STATIC_METATYPE(_, QMetaTypeId2<qreal>::MetaType, qreal)
    {nullptr, 0, QMetaType::UnknownType}
};

static const struct : QMetaTypeModuleHelper
{
    template<typename T, typename LiteralWrapper =
             std::conditional_t<std::is_same_v<T, QString>, QLatin1String, const char *>>
    static inline bool convertToBool(const T &source)
    {
        T str = source.toLower();
        return !(str.isEmpty() || str == LiteralWrapper("0") || str == LiteralWrapper("false"));
    }

    QtPrivate::QMetaTypeInterface *interfaceForType(int type) const override {
        switch (type) {
            QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(QT_METATYPE_CONVERT_ID_TO_TYPE)
            QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(QT_METATYPE_CONVERT_ID_TO_TYPE)
            QT_FOR_EACH_STATIC_CORE_CLASS(QT_METATYPE_CONVERT_ID_TO_TYPE)
            QT_FOR_EACH_STATIC_CORE_POINTER(QT_METATYPE_CONVERT_ID_TO_TYPE)
            QT_FOR_EACH_STATIC_CORE_TEMPLATE(QT_METATYPE_CONVERT_ID_TO_TYPE)
        default:
            return nullptr;
        }
    }

    bool convert(const void *from, int fromTypeId, void *to, int toTypeId) const override
    {
        Q_ASSERT(fromTypeId != toTypeId);

        bool onlyCheck = (from == nullptr && to == nullptr);

        using Char = char;
        using SChar = signed char;
        using UChar = unsigned char;
        using Short = short;
        using UShort = unsigned short;
        using Int = int;
        using UInt = unsigned int;
        using Long = long;
        using LongLong = qlonglong;
        using ULong = unsigned long;
        using ULongLong = qulonglong;
        using Float = float;
        using Double = double;
        using Bool = bool;
        using Nullptr = std::nullptr_t;

#define QMETATYPE_CONVERTER_ASSIGN_DOUBLE(To, From) \
    QMETATYPE_CONVERTER(To, From, result = double(source); return true;)
#define QMETATYPE_CONVERTER_ASSIGN_NUMBER(To, From) \
    QMETATYPE_CONVERTER(To, From, result = To::number(source); return true;)
#ifndef QT_BOOTSTRAPPED
#define CONVERT_CBOR_AND_JSON(To) \
    QMETATYPE_CONVERTER(To, QCborValue, \
        if constexpr(std::is_same_v<To, Bool>) { \
            if (!source.isBool()) \
                return false; \
            result = source.toBool(); \
        } else { \
            if (!source.isInteger() && !source.isDouble()) \
                return false; \
            if constexpr(std::is_integral_v<To>) \
                result = source.toInteger(); \
            else \
                result = source.toDouble(); \
        } \
        return true; \
    ); \
    QMETATYPE_CONVERTER(To, QJsonValue, \
        if constexpr(std::is_same_v<To, Bool>) { \
            if (!source.isBool()) \
                return false; \
            result = source.toBool(); \
        } else { \
            if (!source.isDouble()) \
                return false; \
            if constexpr(std::is_integral_v<To>) \
                result = source.toInteger(); \
            else \
                result = source.toDouble(); \
        } \
        return true; \
    )
#else
#define CONVERT_CBOR_AND_JSON(To)
#endif

#define INTEGRAL_CONVERTER(To) \
    QMETATYPE_CONVERTER_ASSIGN(To, Bool); \
    QMETATYPE_CONVERTER_ASSIGN(To, Char); \
    QMETATYPE_CONVERTER_ASSIGN(To, UChar); \
    QMETATYPE_CONVERTER_ASSIGN(To, SChar); \
    QMETATYPE_CONVERTER_ASSIGN(To, Short); \
    QMETATYPE_CONVERTER_ASSIGN(To, UShort); \
    QMETATYPE_CONVERTER_ASSIGN(To, Int); \
    QMETATYPE_CONVERTER_ASSIGN(To, UInt); \
    QMETATYPE_CONVERTER_ASSIGN(To, Long); \
    QMETATYPE_CONVERTER_ASSIGN(To, ULong); \
    QMETATYPE_CONVERTER_ASSIGN(To, LongLong); \
    QMETATYPE_CONVERTER_ASSIGN(To, ULongLong); \
    QMETATYPE_CONVERTER(To, Float, result = qRound64(source); return true;); \
    QMETATYPE_CONVERTER(To, Double, result = qRound64(source); return true;); \
    QMETATYPE_CONVERTER(To, QChar, result = source.unicode(); return true;); \
    QMETATYPE_CONVERTER(To, QString, \
        bool ok = false; \
        if constexpr(std::is_same_v<To, bool>) \
            result = (ok = true, convertToBool(source)); \
        else if constexpr(std::is_signed_v<To>) \
            result = To(source.toLongLong(&ok)); \
        else \
            result = To(source.toULongLong(&ok)); \
        return ok; \
    ); \
    QMETATYPE_CONVERTER(To, QByteArray, \
        bool ok = false; \
        if constexpr(std::is_same_v<To, bool>) \
            result = (ok = true, convertToBool(source)); \
        else if constexpr(std::is_signed_v<To>) \
            result = To(source.toLongLong(&ok)); \
        else \
            result = To(source.toULongLong(&ok)); \
        return ok; \
    ); \
    CONVERT_CBOR_AND_JSON(To)

#define FLOAT_CONVERTER(To) \
    QMETATYPE_CONVERTER_ASSIGN(To, Bool); \
    QMETATYPE_CONVERTER_ASSIGN(To, Char); \
    QMETATYPE_CONVERTER_ASSIGN(To, UChar); \
    QMETATYPE_CONVERTER_ASSIGN(To, SChar); \
    QMETATYPE_CONVERTER_ASSIGN(To, Short); \
    QMETATYPE_CONVERTER_ASSIGN(To, UShort); \
    QMETATYPE_CONVERTER_ASSIGN(To, Int); \
    QMETATYPE_CONVERTER_ASSIGN(To, UInt); \
    QMETATYPE_CONVERTER_ASSIGN(To, Long); \
    QMETATYPE_CONVERTER_ASSIGN(To, ULong); \
    QMETATYPE_CONVERTER_ASSIGN(To, LongLong); \
    QMETATYPE_CONVERTER_ASSIGN(To, ULongLong); \
    QMETATYPE_CONVERTER_ASSIGN(To, Float); \
    QMETATYPE_CONVERTER_ASSIGN(To, Double); \
    QMETATYPE_CONVERTER(To, QString, \
        bool ok = false; \
        result = source.toDouble(&ok); \
        return ok; \
    ); \
    QMETATYPE_CONVERTER(To, QByteArray, \
        bool ok = false; \
        result = source.toDouble(&ok); \
        return ok; \
    ); \
    CONVERT_CBOR_AND_JSON(To)

        switch (makePair(toTypeId, fromTypeId)) {

        // integral conversions
        INTEGRAL_CONVERTER(Bool);
        INTEGRAL_CONVERTER(Char);
        INTEGRAL_CONVERTER(UChar);
        INTEGRAL_CONVERTER(SChar);
        INTEGRAL_CONVERTER(Short);
        INTEGRAL_CONVERTER(UShort);
        INTEGRAL_CONVERTER(Int);
        INTEGRAL_CONVERTER(UInt);
        INTEGRAL_CONVERTER(Long);
        INTEGRAL_CONVERTER(ULong);
        INTEGRAL_CONVERTER(LongLong);
        INTEGRAL_CONVERTER(ULongLong);
        FLOAT_CONVERTER(Float);
        FLOAT_CONVERTER(Double);

#ifndef QT_BOOTSTRAPPED
        QMETATYPE_CONVERTER_ASSIGN(QUrl, QString);
        QMETATYPE_CONVERTER(QUrl, QCborValue,
            if (source.isUrl()) {
                result = source.toUrl();
                return true;
             }
            return false;
        );
#endif
#if QT_CONFIG(itemmodel)
        QMETATYPE_CONVERTER_ASSIGN(QModelIndex, QPersistentModelIndex);
        QMETATYPE_CONVERTER_ASSIGN(QPersistentModelIndex, QModelIndex);
#endif // QT_CONFIG(itemmodel)

        // QChar methods
#define QMETATYPE_CONVERTER_ASSIGN_QCHAR(From) \
        QMETATYPE_CONVERTER(QChar, From, result = QChar::fromUcs2(source); return true;)
        QMETATYPE_CONVERTER_ASSIGN_QCHAR(Char);
        QMETATYPE_CONVERTER_ASSIGN_QCHAR(SChar);
        QMETATYPE_CONVERTER_ASSIGN_QCHAR(Short);
        QMETATYPE_CONVERTER_ASSIGN_QCHAR(Long);
        QMETATYPE_CONVERTER_ASSIGN_QCHAR(Int);
        QMETATYPE_CONVERTER_ASSIGN_QCHAR(LongLong);
        QMETATYPE_CONVERTER_ASSIGN_QCHAR(Float);
        QMETATYPE_CONVERTER_ASSIGN_QCHAR(UChar);
        QMETATYPE_CONVERTER_ASSIGN_QCHAR(UShort);
        QMETATYPE_CONVERTER_ASSIGN_QCHAR(ULong);
        QMETATYPE_CONVERTER_ASSIGN_QCHAR(UInt);
        QMETATYPE_CONVERTER_ASSIGN_QCHAR(ULongLong);

        // conversions to QString
        QMETATYPE_CONVERTER_ASSIGN(QString, QChar);
        QMETATYPE_CONVERTER(QString, Bool,
            result = source ? QStringLiteral("true") : QStringLiteral("false");
            return true;
        );
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QString, Short);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QString, Long);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QString, Int);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QString, LongLong);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QString, UShort);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QString, ULong);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QString, UInt);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QString, ULongLong);
        QMETATYPE_CONVERTER(QString, Float,
            result = QString::number(source, 'g', QLocale::FloatingPointShortest);
            return true;
        );
        QMETATYPE_CONVERTER(QString, Double,
            result = QString::number(source, 'g', QLocale::FloatingPointShortest);
            return true;
        );
        QMETATYPE_CONVERTER(QString, Char,
            result = QString::fromLatin1(&source, 1);
            return true;
        );
        QMETATYPE_CONVERTER(QString, SChar,
            char s = source;
            result = QString::fromLatin1(&s, 1);
            return true;
        );
        QMETATYPE_CONVERTER(QString, UChar,
            char s = source;
            result = QString::fromLatin1(&s, 1);
            return true;
        );
#if QT_CONFIG(datestring)
        QMETATYPE_CONVERTER(QString, QDate, result = source.toString(Qt::ISODate); return true;);
        QMETATYPE_CONVERTER(QString, QTime, result = source.toString(Qt::ISODateWithMs); return true;);
        QMETATYPE_CONVERTER(QString, QDateTime, result = source.toString(Qt::ISODateWithMs); return true;);
#endif
        QMETATYPE_CONVERTER(QString, QByteArray, result = QString::fromUtf8(source); return true;);
        QMETATYPE_CONVERTER(QString, QStringList,
            return (source.count() == 1) ? (result = source.at(0), true) : false;
        );
#ifndef QT_BOOTSTRAPPED
        QMETATYPE_CONVERTER(QString, QUrl, result = source.toString(); return true;);
        QMETATYPE_CONVERTER(QString, QJsonValue,
            if (source.isString() || source.isNull()) {
                result = source.toString();
                return true;
            }
            return false;
        );
#endif
        QMETATYPE_CONVERTER(QString, Nullptr, Q_UNUSED(source); result = QString(); return true;);

        // QByteArray
        QMETATYPE_CONVERTER(QByteArray, QString, result = source.toUtf8(); return true;);
        QMETATYPE_CONVERTER(QByteArray, Bool,
            result = source ? "true" : "false";
            return true;
        );
        QMETATYPE_CONVERTER(QByteArray, Char, result = QByteArray(source, 1); return true;);
        QMETATYPE_CONVERTER(QByteArray, SChar, result = QByteArray(source, 1); return true;);
        QMETATYPE_CONVERTER(QByteArray, UChar, result = QByteArray(source, 1); return true;);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QByteArray, Short);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QByteArray, Long);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QByteArray, Int);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QByteArray, LongLong);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QByteArray, UShort);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QByteArray, ULong);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QByteArray, UInt);
        QMETATYPE_CONVERTER_ASSIGN_NUMBER(QByteArray, ULongLong);
        QMETATYPE_CONVERTER(QByteArray, Float,
            result = QByteArray::number(source, 'g', QLocale::FloatingPointShortest);
            return true;
        );
        QMETATYPE_CONVERTER(QByteArray, Double,
            result = QByteArray::number(source, 'g', QLocale::FloatingPointShortest);
            return true;
        );
        QMETATYPE_CONVERTER(QByteArray, Nullptr, Q_UNUSED(source); result = QByteArray(); return true;);

        QMETATYPE_CONVERTER(QString, QUuid, result = source.toString(); return true;);
        QMETATYPE_CONVERTER(QUuid, QString, result = QUuid(source); return true;);
        QMETATYPE_CONVERTER(QByteArray, QUuid, result = source.toByteArray(); return true;);
        QMETATYPE_CONVERTER(QUuid, QByteArray, result = QUuid(source); return true;);

#ifndef QT_NO_GEOM_VARIANT
        QMETATYPE_CONVERTER(QSize, QSizeF, result = source.toSize(); return true;);
        QMETATYPE_CONVERTER_ASSIGN(QSizeF, QSize);
        QMETATYPE_CONVERTER(QLine, QLineF, result = source.toLine(); return true;);
        QMETATYPE_CONVERTER_ASSIGN(QLineF, QLine);
        QMETATYPE_CONVERTER(QRect, QRectF, result = source.toRect(); return true;);
        QMETATYPE_CONVERTER_ASSIGN(QRectF, QRect);
        QMETATYPE_CONVERTER(QPoint, QPointF, result = source.toPoint(); return true;);
        QMETATYPE_CONVERTER_ASSIGN(QPointF, QPoint);
 #endif

        QMETATYPE_CONVERTER(QByteArrayList, QVariantList,
            result.reserve(source.size());
            for (auto v: source)
                result.append(v.toByteArray());
            return true;
        );
        QMETATYPE_CONVERTER(QVariantList, QByteArrayList,
            result.reserve(source.size());
            for (auto v: source)
                result.append(QVariant(v));
            return true;
        );

        QMETATYPE_CONVERTER(QStringList, QVariantList,
            result.reserve(source.size());
            for (auto v: source)
                result.append(v.toString());
            return true;
        );
        QMETATYPE_CONVERTER(QVariantList, QStringList,
            result.reserve(source.size());
            for (auto v: source)
                result.append(QVariant(v));
            return true;
        );
        QMETATYPE_CONVERTER(QStringList, QString, result = QStringList() << source; return true;);

        QMETATYPE_CONVERTER(QVariantHash, QVariantMap,
            for (auto it = source.begin(); it != source.end(); ++it)
                result.insert(it.key(), it.value());
            return true;
        );
        QMETATYPE_CONVERTER(QVariantMap, QVariantHash,
            for (auto it = source.begin(); it != source.end(); ++it)
                result.insert(it.key(), it.value());
            return true;
        );

#ifndef QT_BOOTSTRAPPED
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, QString);
        QMETATYPE_CONVERTER(QString, QCborValue,
            if (source.isContainer() || source.isTag())
                 return false;
            result = source.toVariant().toString();
            return true;
        );
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, QByteArray);
        QMETATYPE_CONVERTER(QByteArray, QCborValue,
            if (source.isByteArray()) {
                result = source.toByteArray();
                return true;
            }
            return false;
        );
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, QUuid);
        QMETATYPE_CONVERTER(QUuid, QCborValue,
            if (!source.isUuid())
                return false;
            result = source.toUuid();
            return true;
        );
        QMETATYPE_CONVERTER(QCborValue, QVariantList, result = QCborArray::fromVariantList(source); return true;);
        QMETATYPE_CONVERTER(QVariantList, QCborValue,
            if (!source.isArray())
                return false;
            result = source.toArray().toVariantList();
            return true;
        );
        QMETATYPE_CONVERTER(QCborValue, QVariantMap, result = QCborMap::fromVariantMap(source); return true;);
        QMETATYPE_CONVERTER(QVariantMap, QCborValue,
            if (!source.isMap())
                return false;
                result = source.toMap().toVariantMap();
            return true;
        );
        QMETATYPE_CONVERTER(QCborValue, QVariantHash, result = QCborMap::fromVariantHash(source); return true;);
        QMETATYPE_CONVERTER(QVariantHash, QCborValue,
            if (!source.isMap())
                return false;
            result = source.toMap().toVariantHash();
            return true;
        );
#if QT_CONFIG(regularexpression)
        QMETATYPE_CONVERTER(QCborValue, QRegularExpression, result = QCborValue(source); return true;);
        QMETATYPE_CONVERTER(QRegularExpression, QCborValue,
            if (!source.isRegularExpression())
                return false;
            result = source.toRegularExpression();
            return true;
        );
#endif

        QMETATYPE_CONVERTER(QCborValue, Nullptr,
            Q_UNUSED(source);
            result = QCborValue(QCborValue::Null);
            return true;
        );
        QMETATYPE_CONVERTER(Nullptr, QCborValue,
            result = nullptr;
            return source.isNull();
        );
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, Bool);
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, Int);
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, UInt);
        QMETATYPE_CONVERTER(QCborValue, ULong, result = qlonglong(source); return true;);
        QMETATYPE_CONVERTER(QCborValue, Long, result = qlonglong(source); return true;);
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, LongLong);
        QMETATYPE_CONVERTER(QCborValue, ULongLong, result = qlonglong(source); return true;);
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, UShort);
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, UChar);
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, Char);
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, SChar);
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, Short);
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, Double);
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, Float);
        QMETATYPE_CONVERTER(QCborValue, QStringList,
            result = QCborArray::fromStringList(source);
            return true;
        );
        QMETATYPE_CONVERTER(QCborValue, QDate,
            result = QCborValue(source.startOfDay());
            return true;
        );
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, QUrl);
        QMETATYPE_CONVERTER(QCborValue, QJsonValue,
            result = QCborValue::fromJsonValue(source);
            return true;
        );
        QMETATYPE_CONVERTER(QCborValue, QJsonObject,
            result = QCborMap::fromJsonObject(source);
            return true;
        );
        QMETATYPE_CONVERTER(QCborValue, QJsonArray,
            result = QCborArray::fromJsonArray(source);
            return true;
        );
        QMETATYPE_CONVERTER(QCborValue, QJsonDocument,
            QJsonDocument doc = source;
            if (doc.isArray())
                result = QCborArray::fromJsonArray(doc.array());
            else
                result = QCborMap::fromJsonObject(doc.object());
            return true;
        );
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, QCborMap);
        QMETATYPE_CONVERTER_ASSIGN(QCborValue, QCborArray);

        QMETATYPE_CONVERTER_ASSIGN(QCborValue, QDateTime);
        QMETATYPE_CONVERTER(QDateTime, QCborValue,
            if (source.isDateTime()) {
                result = source.toDateTime();
                return true;
            }
            return false;
        );

        QMETATYPE_CONVERTER_ASSIGN(QCborValue, QCborSimpleType);
        QMETATYPE_CONVERTER(QCborSimpleType, QCborValue,
            if (source.isSimpleType()) {
                 result = source.toSimpleType();
                 return true;
             }
             return false;
        );

        QMETATYPE_CONVERTER(QCborArray, QVariantList, result = QCborArray::fromVariantList(source); return true;);
        QMETATYPE_CONVERTER(QVariantList, QCborArray, result = source.toVariantList(); return true;);
        QMETATYPE_CONVERTER(QCborArray, QStringList, result = QCborArray::fromStringList(source); return true;);
        QMETATYPE_CONVERTER(QCborMap, QVariantMap, result = QCborMap::fromVariantMap(source); return true;);
        QMETATYPE_CONVERTER(QVariantMap, QCborMap, result = source.toVariantMap(); return true;);
        QMETATYPE_CONVERTER(QCborMap, QVariantHash, result = QCborMap::fromVariantHash(source); return true;);
        QMETATYPE_CONVERTER(QVariantHash, QCborMap, result = source.toVariantHash(); return true;);

        QMETATYPE_CONVERTER(QCborArray, QCborValue,
            if (!source.isArray())
                return false;
            result = source.toArray();
            return true;
        );
        QMETATYPE_CONVERTER(QCborArray, QJsonDocument,
            if (!source.isArray())
                return false;
            result = QCborArray::fromJsonArray(source.array());
            return true;
        );
        QMETATYPE_CONVERTER(QCborArray, QJsonValue,
            if (!source.isArray())
                return false;
            result = QCborArray::fromJsonArray(source.toArray());
            return true;
        );
        QMETATYPE_CONVERTER(QCborArray, QJsonArray,
            result = QCborArray::fromJsonArray(source);
            return true;
        );
        QMETATYPE_CONVERTER(QCborMap, QCborValue,
            if (!source.isMap())
                return false;
            result = source.toMap();
            return true;
        );
        QMETATYPE_CONVERTER(QCborMap, QJsonDocument,
            if (source.isArray())
                return false;
            result = QCborMap::fromJsonObject(source.object());
            return true;
        );
        QMETATYPE_CONVERTER(QCborMap, QJsonValue,
            if (!source.isObject())
                return false;
            result = QCborMap::fromJsonObject(source.toObject());
            return true;
        );
        QMETATYPE_CONVERTER(QCborMap, QJsonObject,
            result = QCborMap::fromJsonObject(source);
            return true;
        );


        QMETATYPE_CONVERTER(QVariantList, QJsonValue,
            if (!source.isArray())
                return false;
            result = source.toArray().toVariantList();
            return true;
        );
        QMETATYPE_CONVERTER(QVariantList, QJsonArray, result = source.toVariantList(); return true;);
        QMETATYPE_CONVERTER(QVariantMap, QJsonValue,
            if (!source.isObject())
                return false;
            result = source.toObject().toVariantMap();
            return true;
        );
        QMETATYPE_CONVERTER(QVariantMap, QJsonObject, result = source.toVariantMap(); return true;);
        QMETATYPE_CONVERTER(QVariantHash, QJsonValue,
            if (!source.isObject())
                return false;
            result = source.toObject().toVariantHash();
            return true;
        );
        QMETATYPE_CONVERTER(QVariantHash, QJsonObject, result = source.toVariantHash(); return true;);


        QMETATYPE_CONVERTER(QJsonArray, QStringList, result = QJsonArray::fromStringList(source); return true;);
        QMETATYPE_CONVERTER(QJsonArray, QVariantList, result = QJsonArray::fromVariantList(source); return true;);
        QMETATYPE_CONVERTER(QJsonArray, QJsonValue,
            if (!source.isArray())
                return false;
            result = source.toArray();
            return true;
        );
        QMETATYPE_CONVERTER(QJsonArray, QJsonDocument,
            if (!source.isArray())
                return false;
            result = source.array();
            return true;
        );
        QMETATYPE_CONVERTER(QJsonArray, QCborValue,
            if (!source.isArray())
                return false;
            result = source.toArray().toJsonArray();
            return true;
        );
        QMETATYPE_CONVERTER(QJsonArray, QCborArray, result = source.toJsonArray(); return true;);
        QMETATYPE_CONVERTER(QJsonObject, QVariantMap, result = QJsonObject::fromVariantMap(source); return true;);
        QMETATYPE_CONVERTER(QJsonObject, QVariantHash, result = QJsonObject::fromVariantHash(source); return true;);
        QMETATYPE_CONVERTER(QJsonObject, QJsonValue,
            if (!source.isObject())
                return false;
            result = source.toObject();
            return true;
        );
        QMETATYPE_CONVERTER(QJsonObject, QJsonDocument,
            if (source.isArray())
                return false;
            result = source.object();
            return true;
        );
        QMETATYPE_CONVERTER(QJsonObject, QCborValue,
            if (!source.isMap())
                return false;
            result = source.toMap().toJsonObject();
            return true;
        );
        QMETATYPE_CONVERTER(QJsonObject, QCborMap, result = source.toJsonObject(); return true; );

        QMETATYPE_CONVERTER(QJsonValue, Nullptr,
            Q_UNUSED(source);
            result = QJsonValue(QJsonValue::Null);
            return true;
        );
        QMETATYPE_CONVERTER(Nullptr, QJsonValue,
            result = nullptr;
            return source.isNull();
        );
        QMETATYPE_CONVERTER(QJsonValue, Bool,
            result = QJsonValue(source);
            return true;);
        QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, Int);
        QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, UInt);
        QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, Double);
        QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, Float);
        QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, ULong);
        QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, Long);
        QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, LongLong);
        QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, ULongLong);
        QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, UShort);
        QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, UChar);
        QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, Char);
        QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, SChar);
        QMETATYPE_CONVERTER_ASSIGN_DOUBLE(QJsonValue, Short);
        QMETATYPE_CONVERTER_ASSIGN(QJsonValue, QString);
        QMETATYPE_CONVERTER(QJsonValue, QStringList,
            result = QJsonValue(QJsonArray::fromStringList(source));
            return true;
        );
        QMETATYPE_CONVERTER(QJsonValue, QVariantList,
            result = QJsonValue(QJsonArray::fromVariantList(source));
            return true;
        );
        QMETATYPE_CONVERTER(QJsonValue, QVariantMap,
            result = QJsonValue(QJsonObject::fromVariantMap(source));
            return true;
        );
        QMETATYPE_CONVERTER(QJsonValue, QVariantHash,
            result = QJsonValue(QJsonObject::fromVariantHash(source));
            return true;
        );
        QMETATYPE_CONVERTER(QJsonValue, QJsonObject,
            result = source;
            return true;
        );
        QMETATYPE_CONVERTER(QJsonValue, QJsonArray,
            result = source;
            return true;
        );
        QMETATYPE_CONVERTER(QJsonValue, QJsonDocument,
            QJsonDocument doc = source;
            result = doc.isArray() ? QJsonValue(doc.array()) : QJsonValue(doc.object());
            return true;
        );
        QMETATYPE_CONVERTER(QJsonValue, QCborValue,
            result = source.toJsonValue();
            return true;
        );
        QMETATYPE_CONVERTER(QJsonValue, QCborMap,
            result = source.toJsonObject();
            return true;
        );
        QMETATYPE_CONVERTER(QJsonValue, QCborArray,
            result = source.toJsonArray();
            return true;
        );

#endif

        QMETATYPE_CONVERTER(QDate, QDateTime, result = source.date(); return true;);
        QMETATYPE_CONVERTER(QTime, QDateTime, result = source.time(); return true;);
        QMETATYPE_CONVERTER(QDateTime, QDate, result = source.startOfDay(); return true;);
#if QT_CONFIG(datestring)
        QMETATYPE_CONVERTER(QDate, QString,
            result = QDate::fromString(source, Qt::ISODate);
            return result.isValid();
        );
        QMETATYPE_CONVERTER(QTime, QString,
            result = QTime::fromString(source, Qt::ISODate);
            return result.isValid();
        );
        QMETATYPE_CONVERTER(QDateTime, QString,
            result = QDateTime::fromString(source, Qt::ISODate);
            return result.isValid();
        );
#endif

        }
        return false;
    }
} metatypeHelper;

static const QMetaTypeModuleHelper *qMetaTypeCoreHelper = &metatypeHelper;
Q_CORE_EXPORT const QMetaTypeModuleHelper *qMetaTypeGuiHelper = nullptr;
Q_CORE_EXPORT const QMetaTypeModuleHelper *qMetaTypeWidgetsHelper = nullptr;

static const QMetaTypeModuleHelper *qModuleHelperForType(int type)
{
    if (type <= QMetaType::LastCoreType)
        return qMetaTypeCoreHelper;
    if (type >= QMetaType::FirstGuiType && type <= QMetaType::LastGuiType)
        return qMetaTypeGuiHelper;
    else if (type >= QMetaType::FirstWidgetsType && type <= QMetaType::LastWidgetsType)
        return qMetaTypeWidgetsHelper;
    return nullptr;
}

template<typename T, typename Key>
class QMetaTypeFunctionRegistry
{
public:
    ~QMetaTypeFunctionRegistry()
    {
        const QWriteLocker locker(&lock);
        map.clear();
    }

    bool contains(Key k) const
    {
        const QReadLocker locker(&lock);
        return map.contains(k);
    }

    bool insertIfNotContains(Key k, const T &f)
    {
        const QWriteLocker locker(&lock);
        if (map.contains(k))
            return false;
        map.insert(k, f);
        return true;
    }

    const T *function(Key k) const
    {
        const QReadLocker locker(&lock);
        auto it = map.find(k);
        return it == map.end() ? nullptr : std::addressof(*it);
    }

    void remove(int from, int to)
    {
        const Key k(from, to);
        const QWriteLocker locker(&lock);
        map.remove(k);
    }
private:
    mutable QReadWriteLock lock;
    QHash<Key, T> map;
};

typedef QMetaTypeFunctionRegistry<QMetaType::ConverterFunction,QPair<int,int> >
QMetaTypeConverterRegistry;

Q_GLOBAL_STATIC(QMetaTypeConverterRegistry, customTypesConversionRegistry)

/*!
    \fn bool QMetaType::registerConverter()
    \since 5.2
    Registers the possibility of an implicit conversion from type From to type To in the meta
    type system. Returns \c true if the registration succeeded, otherwise false.
*/

/*!
    \fn  template<typename MemberFunction, int> bool QMetaType::registerConverter(MemberFunction function)
    \since 5.2
    \overload
    Registers a method \a function like To From::function() const as converter from type From
    to type To in the meta type system. Returns \c true if the registration succeeded, otherwise false.
*/

/*!
    \fn template<typename MemberFunctionOk, char> bool QMetaType::registerConverter(MemberFunctionOk function)
    \since 5.2
    \overload
    Registers a method \a function like To From::function(bool *ok) const as converter from type From
    to type To in the meta type system. Returns \c true if the registration succeeded, otherwise false.
*/

/*!
    \fn template<typename UnaryFunction> bool QMetaType::registerConverter(UnaryFunction function)
    \since 5.2
    \overload
    Registers a unary function object \a function as converter from type From
    to type To in the meta type system. Returns \c true if the registration succeeded, otherwise false.
*/

/*!
    Registers function \a f as converter function from type id \a from to \a to.
    If there's already a conversion registered, this does nothing but deleting \a f.
    Returns \c true if the registration succeeded, otherwise false.
    \since 5.2
    \internal
*/
bool QMetaType::registerConverterFunction(const ConverterFunction &f, int from, int to)
{
    if (!customTypesConversionRegistry()->insertIfNotContains(qMakePair(from, to), f)) {
        qWarning("Type conversion already registered from type %s to type %s",
                 QMetaType(from).name(), QMetaType(to).name());
        return false;
    }
    return true;
}

/*!
    \internal

    Invoked automatically when a converter function object is destroyed.
 */
void QMetaType::unregisterConverterFunction(int from, int to)
{
    if (customTypesConversionRegistry.isDestroyed())
        return;
    customTypesConversionRegistry()->remove(from, to);
}

#ifndef QT_NO_DEBUG_STREAM

/*!
    Streams the object at \a rhs of type \a typeId to the debug stream \a dbg. Returns \c true
    on success, otherwise false.
    \since 5.2
*/
bool QMetaType::debugStream(QDebug& dbg, const void *rhs)
{
    if (d_ptr && d_ptr->flags & QMetaType::IsPointer) {
        dbg << *reinterpret_cast<const void * const *>(rhs);
        return true;
    }
    if (d_ptr && d_ptr->debugStream) {
        d_ptr->debugStream(d_ptr, dbg, rhs);
        return true;
    }
    return false;
}

/*!
    \fn bool QMetaType::debugStream(QDebug& dbg, const void *rhs, int typeId)
    \overload
    \obsolete
*/

/*!
    \fn bool QMetaType::hasRegisteredDebugStreamOperator()
    \obsolete
    \since 5.2

    Returns \c true, if the meta type system has a registered debug stream operator for type T.
 */

/*!
    \fn bool QMetaType::hasRegisteredDebugStreamOperator(int typeId)
    \obsolete

    Returns \c true, if the meta type system has a registered debug stream operator for type
    id \a typeId.
    \since 5.2
*/

/*!
    \fn bool QMetaType::hasRegisteredDebugStreamOperator(int typeId)
    \since 6.0

    Returns \c true, if the meta type system has a registered debug stream operator for this
    meta type.
*/
bool QMetaType::hasRegisteredDebugStreamOperator() const

{
    return d_ptr && d_ptr->debugStream != nullptr;
}
#endif

#ifndef QT_NO_QOBJECT
/*!
  \internal
  returns a QMetaEnum for a given meta tape type id if possible
*/
static QMetaEnum metaEnumFromType(QMetaType t)
{
    if (t.flags() & QMetaType::IsEnumeration) {
        if (const QMetaObject *metaObject = t.metaObject()) {
            const QByteArray enumName = t.name();
            const char *lastColon = std::strrchr(enumName, ':');
            return metaObject->enumerator(metaObject->indexOfEnumerator(
                    lastColon ? lastColon + 1 : enumName.constData()));
        }
    }
    return QMetaEnum();
}
#endif

static bool convertFromEnum(QMetaType fromType, const void *from, QMetaType toType, void *to)
{
    qlonglong ll;
    if (fromType.flags() & QMetaType::IsUnsignedEnumeration) {
        qulonglong ull;
        switch (fromType.sizeOf()) {
        case 1:
            ull = *static_cast<const unsigned char *>(from);
            break;
        case 2:
            ull = *static_cast<const unsigned short *>(from);
            break;
        case 4:
            ull = *static_cast<const unsigned int *>(from);
            break;
        case 8:
            ull = *static_cast<const quint64 *>(from);
            break;
        default:
            Q_UNREACHABLE();
        }
        if (toType.id() == QMetaType::ULongLong) {
            *static_cast<qulonglong *>(to) = ull;
            return true;
        }
        if (toType.id() != QMetaType::QString && toType.id() != QMetaType::QByteArray)
            return QMetaType::convert(QMetaType::fromType<qulonglong>(), &ull, toType, to);
        ll = qlonglong(ull);
    } else {
        switch (fromType.sizeOf()) {
        case 1:
            ll = *static_cast<const signed char *>(from);
            break;
        case 2:
            ll = *static_cast<const short *>(from);
            break;
        case 4:
            ll = *static_cast<const int *>(from);
            break;
        case 8:
            ll = *static_cast<const qint64 *>(from);
            break;
        default:
            Q_UNREACHABLE();
        }
        if (toType.id() == QMetaType::LongLong) {
            *static_cast<qlonglong *>(to) = ll;
            return true;
        }
        if (toType.id() != QMetaType::QString && toType.id() != QMetaType::QByteArray)
            return QMetaType::convert(QMetaType::fromType<qlonglong>(), &ll, toType, to);
    }
    Q_ASSERT(toType.id() == QMetaType::QString || toType.id() == QMetaType::QByteArray);
#ifndef QT_NO_QOBJECT
    QMetaEnum en = metaEnumFromType(fromType);
    if (en.isValid()) {
        const char *key = en.valueToKey(ll);
        if (toType.id() == QMetaType::QString)
            *static_cast<QString *>(to) = QString::fromUtf8(key);
        else
            *static_cast<QByteArray *>(to) = key;
        return true;
    }
#endif
    return false;
}

static bool convertToEnum(QMetaType fromType, const void *from, QMetaType toType, void *to)
{
    int fromTypeId = fromType.id();
    qlonglong value;
    bool ok = false;
#ifndef QT_NO_QOBJECT
    if (fromTypeId == QMetaType::QString || fromTypeId == QMetaType::QByteArray) {
        QMetaEnum en = metaEnumFromType(toType);
        if (!en.isValid())
            return false;
        QByteArray keys = (fromTypeId == QMetaType::QString)
                ? static_cast<const QString *>(from)->toUtf8()
                : *static_cast<const QByteArray *>(from);
        value = en.keysToValue(keys.constData(), &ok);
    }
#endif
    if (!ok) {
        if (fromTypeId == QMetaType::LongLong) {
            value = *static_cast<const qlonglong *>(from);
            ok = true;
        } else {
            ok = QMetaType::convert(fromType, from, QMetaType::fromType<qlonglong>(), &value);
        }
    }

    if (!ok)
        return false;

    switch (toType.sizeOf()) {
    case 1:
        *static_cast<signed char *>(to) = value;
        return true;
    case 2:
        *static_cast<qint16 *>(to) = value;
        return true;
    case 4:
        *static_cast<qint32 *>(to) = value;
        return true;
    case 8:
        *static_cast<qint64 *>(to) = value;
        return true;
    default:
        Q_UNREACHABLE();
        return false;
    }
}

#ifndef QT_BOOTSTRAPPED
static bool convertIterableToVariantList(QMetaType fromType, const void *from, void *to)
{
    const QMetaType::ConverterFunction * const f =
        customTypesConversionRegistry()->function(
                qMakePair(fromType.id(), qMetaTypeId<QIterable<QMetaSequence>>()));
    if (!f)
        return false;

    QSequentialIterable list;
    (*f)(from, &list);
    QVariantList &l = *static_cast<QVariantList *>(to);
    l.clear();
    l.reserve(list.size());
    auto end = list.end();
    for (auto it = list.begin(); it != end; ++it)
        l << *it;
    return true;
}

static bool convertIterableToVariantMap(QMetaType fromType, const void *from, void *to)
{
    const QMetaType::ConverterFunction * const f =
        customTypesConversionRegistry()->function(
                qMakePair(fromType.id(), qMetaTypeId<QIterable<QMetaAssociation>>()));
    if (!f)
        return false;

    QAssociativeIterable map;
    (*f)(from, &map);
    QVariantMap &h = *static_cast<QVariantMap *>(to);
    h.clear();
    auto end = map.end();
    for (auto it = map.begin(); it != end; ++it)
        h.insert(it.key().toString(), it.value());
    return true;
}

static bool convertIterableToVariantHash(QMetaType fromType, const void *from, void *to)
{
    const QMetaType::ConverterFunction * const f =
        customTypesConversionRegistry()->function(
                qMakePair(fromType.id(), qMetaTypeId<QIterable<QMetaAssociation>>()));
    if (!f)
        return false;

    QAssociativeIterable map;
    (*f)(from, &map);
    QVariantHash &h = *static_cast<QVariantHash *>(to);
    h.clear();
    h.reserve(map.size());
    auto end = map.end();
    for (auto it = map.begin(); it != end; ++it)
        h.insert(it.key().toString(), it.value());
    return true;
}
#endif

static bool convertIterableToVariantPair(QMetaType fromType, const void *from, void *to)
{
    const QMetaType::ConverterFunction * const f =
        customTypesConversionRegistry()->function(qMakePair(fromType.id(),
                                                            qMetaTypeId<QtMetaTypePrivate::QPairVariantInterfaceImpl>()));
    if (!f)
        return false;

    QtMetaTypePrivate::QPairVariantInterfaceImpl pi;
    (*f)(from, &pi);

    QVariant v1(pi._metaType_first);
    void *dataPtr;
    if (pi._metaType_first == QMetaType::fromType<QVariant>())
        dataPtr = &v1;
    else
        dataPtr = v1.data();
    pi.first(dataPtr);

    QVariant v2(pi._metaType_second);
    if (pi._metaType_second == QMetaType::fromType<QVariant>())
        dataPtr = &v2;
    else
        dataPtr = v2.data();
    pi.second(dataPtr);

    *static_cast<QVariantPair *>(to) = QVariantPair(v1, v2);
    return true;
}

#ifndef QT_BOOTSTRAPPED
static bool convertToSequentialIterable(QMetaType fromType, const void *from, void *to)
{
    using namespace QtMetaTypePrivate;
    int fromTypeId = fromType.id();

    QSequentialIterable &i = *static_cast<QSequentialIterable *>(to);
    if (fromTypeId == QMetaType::QVariantList) {
        i = QSequentialIterable(reinterpret_cast<const QVariantList *>(from));
        return true;
    }
    if (fromTypeId == QMetaType::QStringList) {
        i = QSequentialIterable(reinterpret_cast<const QStringList *>(from));
        return true;
    }
    else if (fromTypeId == QMetaType::QByteArrayList) {
        i = QSequentialIterable(reinterpret_cast<const QByteArrayList *>(from));
        return true;
    }

    QSequentialIterable impl;
    if (QMetaType::convert(
                fromType, from, QMetaType::fromType<QIterable<QMetaSequence>>(), &impl)) {
        i = std::move(impl);
        return true;
    }

    return false;
}

static bool convertToAssociativeIterable(QMetaType fromType, const void *from, void *to)
{
    using namespace QtMetaTypePrivate;

    QAssociativeIterable &i = *static_cast<QAssociativeIterable *>(to);
    if (fromType.id() == QMetaType::QVariantMap) {
        i = QAssociativeIterable(reinterpret_cast<const QVariantMap *>(from));
        return true;
    }
    if (fromType.id() == QMetaType::QVariantHash) {
        i = QAssociativeIterable(reinterpret_cast<const QVariantHash *>(from));
        return true;
    }

    QAssociativeIterable impl;
    if (QMetaType::convert(
                fromType, from, QMetaType::fromType<QIterable<QMetaAssociation>>(), &impl)) {
        i = std::move(impl);
        return true;
    }

    return false;
}

static bool canConvertMetaObject(QMetaType fromType, QMetaType toType)
{
    const QMetaObject *f = fromType.metaObject();
    const QMetaObject *t = toType.metaObject();
    if (f && t) {
        return f->inherits(t) || (t->inherits(f));
    }
    return false;
}
#endif

/*!
    \fn bool QMetaType::convert(const void *from, int fromTypeId, void *to, int toTypeId)
    \obsolete

    Converts the object at \a from from \a fromTypeId to the preallocated space at \a to
    typed \a toTypeId. Returns \c true, if the conversion succeeded, otherwise false.
    \since 5.2
*/

/*!
    Converts the object at \a from from \a fromType to the preallocated space at \a to
    typed \a toType. Returns \c true, if the conversion succeeded, otherwise false.
    \since 5.2
*/
bool QMetaType::convert(QMetaType fromType, const void *from, QMetaType toType, void *to)
{
    if (!fromType.isValid() || !toType.isValid())
        return false;

    if (fromType == toType) {
        // just make a copy
        fromType.destruct(to);
        fromType.construct(to, from);
        return true;
    }

    int fromTypeId = fromType.id();
    int toTypeId = toType.id();

    if (auto moduleHelper = qModuleHelperForType(qMax(fromTypeId, toTypeId))) {
        if (moduleHelper->convert(from, fromTypeId, to, toTypeId))
            return true;
    }
    const QMetaType::ConverterFunction * const f =
        customTypesConversionRegistry()->function(qMakePair(fromTypeId, toTypeId));
    if (f)
        return (*f)(from, to);

    if (fromType.flags() & QMetaType::IsEnumeration)
        return convertFromEnum(fromType, from, toType, to);
    if (toType.flags() & QMetaType::IsEnumeration)
        return convertToEnum(fromType, from, toType, to);
    if (toTypeId == Nullptr) {
        *static_cast<std::nullptr_t *>(to) = nullptr;
        if (fromType.flags() & QMetaType::IsPointer) {
            if (*static_cast<const void * const *>(from) == nullptr)
                return true;
        }
    }

    if (toTypeId == QVariantPair && convertIterableToVariantPair(fromType, from, to))
        return true;

#ifndef QT_BOOTSTRAPPED
    // handle iterables
    if (toTypeId == QVariantList && convertIterableToVariantList(fromType, from, to))
        return true;

    if (toTypeId == QVariantMap && convertIterableToVariantMap(fromType, from, to))
        return true;

    if (toTypeId == QVariantHash && convertIterableToVariantHash(fromType, from, to))
        return true;

    if (toTypeId == qMetaTypeId<QSequentialIterable>())
        return convertToSequentialIterable(fromType, from, to);

    if (toTypeId == qMetaTypeId<QAssociativeIterable>())
        return convertToAssociativeIterable(fromType, from, to);

    // handle QObject conversion
    if ((fromType.flags() & QMetaType::PointerToQObject) && (toType.flags() & QMetaType::PointerToQObject)) {
        QObject *fromObject = *static_cast<QObject * const *>(from);
        // use dynamic metatype of from if possible
        if (fromObject && fromObject->metaObject()->inherits(toType.metaObject()))  {
            *static_cast<QObject **>(to) = toType.metaObject()->cast(fromObject);
            return true;
        } else if (!fromObject && fromType.metaObject()) {
            // if fromObject is null, use static fromType to check if conversion works
            *static_cast<void **>(to) = nullptr;
            return fromType.metaObject()->inherits(toType.metaObject());
        } else {
            return false;
        }
    }
#endif

    return false;
}

/*!
    Returns \c true if QMetaType::convert can convert from \a fromType to
    \a toType.

    The following conversions are supported by Qt:

    \table
    \header \li Type \li Automatically Cast To
    \row \li \l QMetaType::Bool \li \l QMetaType::QChar, \l QMetaType::Double,
        \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QString,
        \l QMetaType::UInt, \l QMetaType::ULongLong
    \row \li \l QMetaType::QByteArray \li \l QMetaType::Double,
        \l QMetaType::Int, \l QMetaType::LongLong, \l QMetaType::QString,
        \l QMetaType::UInt, \l QMetaType::ULongLong, \l QMetaType::QUuid
    \row \li \l QMetaType::QChar \li \l QMetaType::Bool, \l QMetaType::Int,
        \l QMetaType::UInt, \l QMetaType::LongLong, \l QMetaType::ULongLong
    \row \li \l QMetaType::QColor \li \l QMetaType::QString
    \row \li \l QMetaType::QDate \li \l QMetaType::QDateTime,
        \l QMetaType::QString
    \row \li \l QMetaType::QDateTime \li \l QMetaType::QDate,
        \l QMetaType::QString, \l QMetaType::QTime
    \row \li \l QMetaType::Double \li \l QMetaType::Bool, \l QMetaType::Int,
        \l QMetaType::LongLong, \l QMetaType::QString, \l QMetaType::UInt,
        \l QMetaType::ULongLong
    \row \li \l QMetaType::QFont \li \l QMetaType::QString
    \row \li \l QMetaType::Int \li \l QMetaType::Bool, \l QMetaType::QChar,
        \l QMetaType::Double, \l QMetaType::LongLong, \l QMetaType::QString,
        \l QMetaType::UInt, \l QMetaType::ULongLong
    \row \li \l QMetaType::QKeySequence \li \l QMetaType::Int,
        \l QMetaType::QString
    \row \li \l QMetaType::QVariantList \li \l QMetaType::QStringList (if the
        list's items can be converted to QStrings)
    \row \li \l QMetaType::LongLong \li \l QMetaType::Bool,
        \l QMetaType::QByteArray, \l QMetaType::QChar, \l QMetaType::Double,
        \l QMetaType::Int, \l QMetaType::QString, \l QMetaType::UInt,
        \l QMetaType::ULongLong
    \row \li \l QMetaType::QPoint \li QMetaType::QPointF
    \row \li \l QMetaType::QRect \li QMetaType::QRectF
    \row \li \l QMetaType::QString \li \l QMetaType::Bool,
        \l QMetaType::QByteArray, \l QMetaType::QChar, \l QMetaType::QColor,
        \l QMetaType::QDate, \l QMetaType::QDateTime, \l QMetaType::Double,
        \l QMetaType::QFont, \l QMetaType::Int, \l QMetaType::QKeySequence,
        \l QMetaType::LongLong, \l QMetaType::QStringList, \l QMetaType::QTime,
        \l QMetaType::UInt, \l QMetaType::ULongLong, \l QMetaType::QUuid
    \row \li \l QMetaType::QStringList \li \l QMetaType::QVariantList,
        \l QMetaType::QString (if the list contains exactly one item)
    \row \li \l QMetaType::QTime \li \l QMetaType::QString
    \row \li \l QMetaType::UInt \li \l QMetaType::Bool, \l QMetaType::QChar,
        \l QMetaType::Double, \l QMetaType::Int, \l QMetaType::LongLong,
        \l QMetaType::QString, \l QMetaType::ULongLong
    \row \li \l QMetaType::ULongLong \li \l QMetaType::Bool,
        \l QMetaType::QChar, \l QMetaType::Double, \l QMetaType::Int,
        \l QMetaType::LongLong, \l QMetaType::QString, \l QMetaType::UInt
    \row \li \l QMetaType::QUuid \li \l QMetaType::QByteArray, \l QMetaType::QString
    \endtable

    Casting between primitive type (int, float, bool etc.) is supported.

    Converting between pointers of types derived from QObject will also return true for this
    function if a qobject_cast from the type described by \a fromType to the type described
    by \a toType would succeed.

    A cast from a sequential container will also return true for this
    function if the \a toType is QVariantList.

    Similarly, a cast from an associative container will also return true for this
    function the \a toType is QVariantHash or QVariantMap.

    \sa convert(), QSequentialIterable, Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(), QAssociativeIterable,
        Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE()
*/
bool QMetaType::canConvert(QMetaType fromType, QMetaType toType)
{
    int fromTypeId = fromType.id();
    int toTypeId = toType.id();

    if (fromTypeId == UnknownType || toTypeId == UnknownType)
        return false;

    if (fromTypeId == toTypeId)
        return true;

    if (auto moduleHelper = qModuleHelperForType(qMax(fromTypeId, toTypeId))) {
        if (moduleHelper->convert(nullptr, fromTypeId, nullptr, toTypeId))
            return true;
    }
    const ConverterFunction * const f =
        customTypesConversionRegistry()->function(qMakePair(fromTypeId, toTypeId));
    if (f)
        return true;

#ifndef QT_BOOTSTRAPPED
    if (toTypeId == QVariantList && hasRegisteredConverterFunction(
                    fromTypeId, qMetaTypeId<QIterable<QMetaSequence>>()))
        return true;

    if ((toTypeId == QVariantHash || toTypeId == QVariantMap) && hasRegisteredConverterFunction(
                    fromTypeId, qMetaTypeId<QIterable<QMetaAssociation>>()))
        return true;
#endif

    if (toTypeId == QVariantPair && hasRegisteredConverterFunction(
                    fromTypeId, qMetaTypeId<QtMetaTypePrivate::QPairVariantInterfaceImpl>()))
        return true;

    if (fromType.flags() & IsEnumeration) {
        if (toTypeId == QString || toTypeId == QByteArray)
            return true;
        return canConvert(QMetaType(LongLong), toType);
    }
    if (toType.flags() & IsEnumeration) {
        if (fromTypeId == QString || fromTypeId == QByteArray)
            return true;
        return canConvert(fromType, QMetaType(LongLong));
    }
    if (toTypeId == Nullptr && fromType.flags() & IsPointer)
        return true;
#ifndef QT_BOOTSTRAPPED
    if (canConvertMetaObject(fromType, toType))
        return true;
#endif

    return false;
}

/*!
    bool QMetaType::compare(const void *lhs, const void *rhs, int typeId, int* result)
    \deprecated Use the non-static compare method instead

    Compares the objects at \a lhs and \a rhs. Both objects need to be of type \a typeId.
    \a result is set to less than, equal to or greater than zero, if \a lhs is less than, equal to
    or greater than \a rhs. Returns \c true, if the comparison succeeded, otherwise \c false.
*/

/*!
    \fn bool QMetaType::hasRegisteredConverterFunction()
    Returns \c true, if the meta type system has a registered conversion from type From to type To.
    \since 5.2
    \overload
    */

/*!
    Returns \c true, if the meta type system has a registered conversion from meta type id \a fromTypeId
    to \a toTypeId
    \since 5.2
*/
bool QMetaType::hasRegisteredConverterFunction(int fromTypeId, int toTypeId)
{
    return customTypesConversionRegistry()->contains(qMakePair(fromTypeId, toTypeId));
}

/*!
    \fn const char *QMetaType::typeName(int typeId)
    \deprecated

    Returns the type name associated with the given \a typeId, or a null
    pointer if no matching type was found. The returned pointer must not be
    deleted.

    \sa type(), isRegistered(), Type, name()
*/

/*!
    \since 5.15

    Returns the type name associated with this QMetaType, or a null
    pointer if no matching type was found. The returned pointer must not be
    deleted.

    \sa typeName()
*/
const char *QMetaType::name() const
{
    return d_ptr ? d_ptr->name : nullptr;
}

/*
    Similar to QMetaType::type(), but only looks in the static set of types.
*/
static inline int qMetaTypeStaticType(const char *typeName, int length)
{
    int i = 0;
    while (types[i].typeName && ((length != types[i].typeNameLength)
                                 || memcmp(typeName, types[i].typeName, length))) {
        ++i;
    }
    return types[i].type;
}

/*
    Similar to QMetaType::type(), but only looks in the custom set of
    types, and doesn't lock the mutex.

*/
static int qMetaTypeCustomType_unlocked(const char *typeName, int length)
{
    if (auto reg = customTypeRegistry()) {
#if QT_CONFIG(thread)
        Q_ASSERT(!reg->lock.tryLockForWrite());
#endif
        if (auto ti = reg->aliases.value(QByteArray(typeName, length), nullptr)) {
            return ti->typeId;
        }
    }
    return QMetaType::UnknownType;
}

/*!
    \internal

    Registers a user type for marshalling, as an alias of another type (typedef).
    Note that normalizedTypeName is not checked for conformance with Qt's normalized format,
    so it must already conform.
*/
void QMetaType::registerNormalizedTypedef(const NS(QByteArray) & normalizedTypeName,
                                          QMetaType metaType)
{
    if (!metaType.isValid())
        return;
    if (auto reg = customTypeRegistry()) {
        QWriteLocker lock(&reg->lock);
        auto &al = reg->aliases[normalizedTypeName];
        if (al)
            return;
        al = metaType.d_ptr;
    }
}

/*!
    Returns \c true if the datatype with ID \a type is registered;
    otherwise returns \c false.

    \sa type(), typeName(), Type
*/
bool QMetaType::isRegistered(int type)
{
    return QMetaType(type).isRegistered();
}

template <bool tryNormalizedType>
static inline int qMetaTypeTypeImpl(const char *typeName, int length)
{
    if (!length)
        return QMetaType::UnknownType;
    int type = qMetaTypeStaticType(typeName, length);
    if (type == QMetaType::UnknownType) {
        QReadLocker locker(&customTypeRegistry()->lock);
        type = qMetaTypeCustomType_unlocked(typeName, length);
#ifndef QT_NO_QOBJECT
        if ((type == QMetaType::UnknownType) && tryNormalizedType) {
            const NS(QByteArray) normalizedTypeName = QMetaObject::normalizedType(typeName);
            type = qMetaTypeStaticType(normalizedTypeName.constData(),
                                       normalizedTypeName.size());
            if (type == QMetaType::UnknownType) {
                type = qMetaTypeCustomType_unlocked(normalizedTypeName.constData(),
                                                    normalizedTypeName.size());
            }
        }
#endif
    }
    return type;
}

/*!
    \fn int QMetaType::type(const char *typeName)
    \deprecated

    Returns a handle to the type called \a typeName, or QMetaType::UnknownType if there is
    no such type.

    \sa isRegistered(), typeName(), Type
*/

/*!
    \a internal

    Similar to QMetaType::type(); the only difference is that this function
    doesn't attempt to normalize the type name (i.e., the lookup will fail
    for type names in non-normalized form).
*/
Q_CORE_EXPORT int qMetaTypeTypeInternal(const char *typeName)
{
    return qMetaTypeTypeImpl</*tryNormalizedType=*/false>(typeName, int(qstrlen(typeName)));
}

/*!
    \fn int QMetaType::type(const QByteArray &typeName)

    \since 5.5
    \overload
    \deprecated

    Returns a handle to the type called \a typeName, or 0 if there is
    no such type.

    \sa isRegistered(), typeName()
*/

#ifndef QT_NO_DATASTREAM
/*!
    Writes the object pointed to by \a data with the ID \a type to
    the given \a stream. Returns \c true if the object is saved
    successfully; otherwise returns \c false.

    The type must have been registered with Q_DECLARE_METATYPE()
    beforehand.

    Normally, you should not need to call this function directly.
    Instead, use QVariant's \c operator<<(), which relies on save()
    to stream custom types.

    \sa load()
*/
bool QMetaType::save(QDataStream &stream, const void *data) const
{
    if (!data || !isValid())
        return false;

    // keep compatibility for long/ulong
    if (id() == QMetaType::Long) {
        stream << qlonglong(*(long *)data);
        return true;
    } else if (id() == QMetaType::ULong) {
        stream << qlonglong(*(unsigned long *)data);
        return true;
    }

    if (!d_ptr->dataStreamOut)
        return false;

    d_ptr->dataStreamOut(d_ptr, stream, data);
    return true;
}

/*!
   \fn bool QMetaType::save(QDataStream &stream, int type, const void *data)
   \overload
   \obsolete
*/

/*!
    Reads the object of the specified \a type from the given \a
    stream into \a data. Returns \c true if the object is loaded
    successfully; otherwise returns \c false.

    The type must have been registered with Q_DECLARE_METATYPE()
    beforehand.

    Normally, you should not need to call this function directly.
    Instead, use QVariant's \c operator>>(), which relies on load()
    to stream custom types.

    \sa save()
*/
bool QMetaType::load(QDataStream &stream, void *data) const
{
    if (!data || !isValid())
        return false;

    // keep compatibility for long/ulong
    if (id() == QMetaType::Long) {
        qlonglong ll;
        stream >> ll;
        *(long *)data = long(ll);
        return true;
    } else if (id() == QMetaType::ULong) {
        qulonglong ull;
        stream >> ull;
        *(unsigned long *)data = (unsigned long)(ull);
        return true;
    }
    if (!d_ptr->dataStreamIn)
        return false;

    d_ptr->dataStreamIn(d_ptr, stream, data);
    return true;
}

/*!
   \fn bool QMetaType::load(QDataStream &stream, int type, void *data)
   \overload
   \obsolete
*/
#endif // QT_NO_DATASTREAM

/*!
    Returns a QMetaType matching \a typeName. The returned object is
    not valid if the typeName is not known to QMetaType
 */
QMetaType QMetaType::fromName(QByteArrayView typeName)
{
    return QMetaType(qMetaTypeTypeImpl</*tryNormalizedType=*/true>(typeName.data(), typeName.size()));
}

/*!
    \fn void *QMetaType::create(int type, const void *copy)
    \deprecated

    Returns a copy of \a copy, assuming it is of type \a type. If \a
    copy is zero, creates a default constructed instance.

    \sa destroy(), isRegistered(), Type
*/

/*!
    \fn void QMetaType::destroy(int type, void *data)
    \deprecated
    Destroys the \a data, assuming it is of the \a type given.

    \sa create(), isRegistered(), Type
*/

/*!
    \fn void *QMetaType::construct(int type, void *where, const void *copy)
    \since 5.0
    \deprecated

    Constructs a value of the given \a type in the existing memory
    addressed by \a where, that is a copy of \a copy, and returns
    \a where. If \a copy is zero, the value is default constructed.

    This is a low-level function for explicitly managing the memory
    used to store the type. Consider calling create() if you don't
    need this level of control (that is, use "new" rather than
    "placement new").

    You must ensure that \a where points to a location that can store
    a value of type \a type, and that \a where is suitably aligned.
    The type's size can be queried by calling sizeOf().

    The rule of thumb for alignment is that a type is aligned to its
    natural boundary, which is the smallest power of 2 that is bigger
    than the type, unless that alignment is larger than the maximum
    useful alignment for the platform. For practical purposes,
    alignment larger than 2 * sizeof(void*) is only necessary for
    special hardware instructions (e.g., aligned SSE loads and stores
    on x86).

    \sa destruct(), sizeOf()
*/


/*!
    \fn void QMetaType::destruct(int type, void *where)
    \since 5.0
    \deprecated

    Destructs the value of the given \a type, located at \a where.

    Unlike destroy(), this function only invokes the type's
    destructor, it doesn't invoke the delete operator.

    \sa construct()
*/

/*!
    \fn int QMetaType::sizeOf(int type)
    \since 5.0
    \deprecated

    Returns the size of the given \a type in bytes (i.e. sizeof(T),
    where T is the actual type identified by the \a type argument).

    This function is typically used together with construct()
    to perform low-level management of the memory used by a type.

    \sa construct(), QMetaType::alignOf()
*/

/*!
    \fn QMetaType::TypeFlags QMetaType::typeFlags(int type)
    \since 5.0
    \deprecated

    Returns flags of the given \a type.

    \sa QMetaType::TypeFlags
*/

/*!
    \fn const QMetaObject *QMetaType::metaObjectForType(int type)
    \since 5.0
    \deprecated

    returns QMetaType::metaObject for \a type

    \sa metaObject()
*/

/*!
    \fn int qRegisterMetaType(const char *typeName)
    \relates QMetaType
    \threadsafe

    Registers the type name \a typeName for the type \c{T}. Returns
    the internal ID used by QMetaType. Any class or struct that has a
    public default constructor, a public copy constructor and a public
    destructor can be registered.

    This function requires that \c{T} is a fully defined type at the point
    where the function is called. For pointer types, it also requires that the
    pointed to type is fully defined. Use Q_DECLARE_OPAQUE_POINTER() to be able
    to register pointers to forward declared types.

    After a type has been registered, you can create and destroy
    objects of that type dynamically at run-time.

    This example registers the class \c{MyClass}:

    \snippet code/src_corelib_kernel_qmetatype.cpp 4

    This function is useful to register typedefs so they can be used
    by QMetaProperty, or in QueuedConnections

    \snippet code/src_corelib_kernel_qmetatype.cpp 9

    \warning This function is useful only for registering an alias (typedef)
    for every other use case Q_DECLARE_METATYPE and qMetaTypeId() should be used instead.

    \sa {QMetaType::}{isRegistered()}, Q_DECLARE_METATYPE()
*/

/*!
    \fn int qRegisterMetaType()
    \relates QMetaType
    \threadsafe
    \since 4.2

    Call this function to register the type \c T. \c T must be declared with
    Q_DECLARE_METATYPE(). Returns the meta type Id.

    Example:

    \snippet code/src_corelib_kernel_qmetatype.cpp 7

    This function requires that \c{T} is a fully defined type at the point
    where the function is called. For pointer types, it also requires that the
    pointed to type is fully defined. Use Q_DECLARE_OPAQUE_POINTER() to be able
    to register pointers to forward declared types.

    After a type has been registered, you can create and destroy
    objects of that type dynamically at run-time.

    To use the type \c T in QVariant, using Q_DECLARE_METATYPE() is
    sufficient. To use the type \c T in queued signal and slot connections,
    \c{qRegisterMetaType<T>()} must be called before the first connection
    is established.

    Also, to use type \c T with the QObject::property() API,
    \c{qRegisterMetaType<T>()} must be called before it is used, typically
    in the constructor of the class that uses \c T, or in the \c{main()}
    function.

    \sa Q_DECLARE_METATYPE()
 */

/*!
    \fn int qMetaTypeId()
    \relates QMetaType
    \threadsafe
    \since 4.1

    Returns the meta type id of type \c T at compile time. If the
    type was not declared with Q_DECLARE_METATYPE(), compilation will
    fail.

    Typical usage:

    \snippet code/src_corelib_kernel_qmetatype.cpp 8

    QMetaType::type() returns the same ID as qMetaTypeId(), but does
    a lookup at runtime based on the name of the type.
    QMetaType::type() is a bit slower, but compilation succeeds if a
    type is not registered.

    \sa Q_DECLARE_METATYPE(), QMetaType::type()
*/

static QtPrivate::QMetaTypeInterface *interfaceForType(int typeId)
{
    QtPrivate::QMetaTypeInterface *iface = nullptr;
    if (typeId >= QMetaType::User) {
        if (auto reg = customTypeRegistry())
            iface = reg->getCustomType(typeId);
    } else {
        if (auto moduleHelper = qModuleHelperForType(typeId))
            iface = moduleHelper->interfaceForType(typeId);
    }

    if (!iface && typeId != QMetaType::UnknownType)
        qWarning("Trying to construct an instance of an invalid type, type id: %i", typeId);

    return iface;
}

/*!
     \fn QMetaType::QMetaType(int typeId)
     \since 5.0

     Constructs a QMetaType object that contains all information about type \a typeId.
*/
QMetaType::QMetaType(int typeId) : QMetaType(interfaceForType(typeId)) {}

namespace QtPrivate {
#ifndef QT_BOOTSTRAPPED
// Explicit instantiation definition
#define QT_METATYPE_DECLARE_TEMPLATE_ITER(TypeName, Id, Name) template class QMetaTypeForType<Name>;
QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(QT_METATYPE_DECLARE_TEMPLATE_ITER)
QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(QT_METATYPE_DECLARE_TEMPLATE_ITER)
QT_FOR_EACH_STATIC_CORE_CLASS(QT_METATYPE_DECLARE_TEMPLATE_ITER)
QT_FOR_EACH_STATIC_CORE_POINTER(QT_METATYPE_DECLARE_TEMPLATE_ITER)
QT_FOR_EACH_STATIC_CORE_TEMPLATE(QT_METATYPE_DECLARE_TEMPLATE_ITER)
#undef QT_METATYPE_DECLARE_TEMPLATE_ITER
#endif
}

QT_END_NAMESPACE