nvtx-sys 2.0.0

NVIDIA Tools Extension SDK
Documentation
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
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
/*
 * SPDX-FileCopyrightText: Copyright (c) 2020-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Licensed under the Apache License v2.0 with LLVM Exceptions.
 * See https://nvidia.github.io/NVTX/LICENSE.txt for license information.
 */

#if defined(NVTX_AS_SYSTEM_HEADER)
#if defined(__clang__)
#pragma clang system_header
#elif defined(__GNUC__) || defined(__NVCOMPILER)
#pragma GCC system_header
#elif defined(_MSC_VER)
#pragma system_header
#endif
#endif

/* Temporary helper #defines, #undef'ed at end of header */
#define NVTX3_CPP_VERSION_MAJOR 1
#define NVTX3_CPP_VERSION_MINOR 2

/*
 * NVTX C++ header versioning
 *
 * Goals
 * 1) Allow limited coexistence of different versions of NVTX headers in a
 * single translation unit (TU)
 * 2) Support linking objects built with different header versions of NVTX
 *
 * Namespaces
 *
 * Symbols live in nested major/minor namespaces, e.g.,
 * `nvtx3::v1::mv1::domain`. The minor namespace is always inlined into the
 * major namespace. This prevents ODR issues when linking code built against
 * different minor versions. Do not use minor-version names directly; headers
 * ship only the latest minor.
 *
 * Unversioned vs versioned names
 *
 * By default the major namespace is also inlined into `nvtx3`, so both
 * `nvtx3::v1::domain` and `nvtx3::domain` exist. Defining
 * `NVTX3_CPP_REQUIRE_EXPLICIT_VERSION` disables this inlining; unversioned
 * names are omitted. When set, you must use explicit major-version names
 * (e.g., `nvtx3::v1::scoped_range`).
 * With default inlining, including two different major headers in one TU is an
 * error. With `NVTX3_CPP_REQUIRE_EXPLICIT_VERSION` defined, it is technically
 * possible to mix major versions. However, this typically means all NVTX usage
 * in the TU must be versioned, unless exactly one major version is included
 * without the macro (providing unversioned symbols).
 *
 * Minor-version policy
 *
 * Minor versions are API-backward compatible. Include the highest minor
 * version first, or just don't mix them at all. Bump the minor version for any
 * implementation change to avoid ODR conflicts (`NVTX3_CPP_VERSION_MINOR` and
 * `NVTX3_CPP_INLINED_VERSION_MINOR`). Do not bump
 * `NVTX3_CPP_DEFINITIONS_V1_0`, this mechanism is no longer applied.
 *
 * Macros follow the same scheme, e.g., `NVTX3_V1_FUNC_RANGE`. Unversioned
 * aliases (e.g., `NVTX3_FUNC_RANGE`) exist unless
 * `NVTX3_CPP_REQUIRE_EXPLICIT_VERSION` is defined.
 *
 * The C++ version numbering described here is strictly about API compatibility
 * and is unrelated to NVTX release version numbers.
 *
 * Recommendations
 * - Prefer a single specific version of the NVTX headers per TU.
 * - Use the latest minor version.
 * - For maximal compatibility in complex builds or header-only code, use
 *   versioned symbols, e.g., `nvtx3::v1::domain`.
 * - Only define `NVTX3_CPP_REQUIRE_EXPLICIT_VERSION` when you need to mix
 *   multiple major versions in the same TU and thus suppress unversioned
 *   names.
 */
/* clang-format off */
#if !defined(NVTX3_CPP_REQUIRE_EXPLICIT_VERSION)
  /* Define macro used by all definitions in this header to indicate the
   * unversioned symbols should be defined in addition to the versioned ones.
   */
  #define NVTX3_INLINE_THIS_VERSION

  #if !defined(NVTX3_CPP_INLINED_VERSION_MAJOR)
    /* First occurrence of this header in the translation unit.  Define macros
     * indicating which version shall be used for unversioned symbols.
     */

    /**
     * @brief Semantic major version number for NVTX C++ wrappers of unversioned symbols
     *
     * Breaking changes may occur between major versions, and different major versions
     * cannot provide unversioned symbols in the same translation unit (.cpp file).
     *
     * Note: If NVTX3_CPP_REQUIRE_EXPLICIT_VERSION is defined, this macro is not defined.
     *
     * Not to be confused with the version number of the NVTX core library.
     */
    #define NVTX3_CPP_INLINED_VERSION_MAJOR 1  // NVTX3_CPP_VERSION_MAJOR

    /**
     * @brief Semantic minor version number for NVTX C++ wrappers of unversioned symbols
     *
     * No breaking changes occur between minor versions -- minor version changes within
     * a major version are purely additive.
     *
     * Note: If NVTX3_CPP_REQUIRE_EXPLICIT_VERSION is defined, this macro is not defined.
     *
     * Not to be confused with the version number of the NVTX core library.
     */
    #define NVTX3_CPP_INLINED_VERSION_MINOR 2  // NVTX3_CPP_VERSION_MINOR
  #elif NVTX3_CPP_INLINED_VERSION_MAJOR != NVTX3_CPP_VERSION_MAJOR
    /* Unsupported case -- cannot define unversioned symbols for different major versions
     * in the same translation unit.
     */
    #error \
      "Two different major versions of the NVTX C++ Wrappers are being included in a single .cpp file, with unversioned symbols enabled in both.  Only one major version can enable unversioned symbols in a .cpp file.  To disable unversioned symbols, #define NVTX3_CPP_REQUIRE_EXPLICIT_VERSION before #including nvtx3.hpp, and use the versioned symbols instead."
  #elif (NVTX3_CPP_INLINED_VERSION_MAJOR == NVTX3_CPP_VERSION_MAJOR) && \
    (NVTX3_CPP_INLINED_VERSION_MINOR < NVTX3_CPP_VERSION_MINOR)
    /* An older minor version of the same major version already defined unversioned
     * symbols.  The new features provided in this header will be inlined
     * redefine the minor version macro to this header's version.
     */
    #undef NVTX3_CPP_INLINED_VERSION_MINOR
    #define NVTX3_CPP_INLINED_VERSION_MINOR 2  // NVTX3_CPP_VERSION_MINOR
    // else, already have this version or newer, nothing to do
  #endif
#endif
/* clang-format on */

/**
 * @file nvtx3.hpp
 *
 * @brief Provides C++ constructs making the NVTX library safer and easier to
 * use with zero overhead.
 */

/**
 * \mainpage
 * \tableofcontents
 *
 * \section QUICK_START Quick Start
 *
 * To add NVTX ranges to your code, use the `nvtx3::scoped_range` RAII object. A
 * range begins when the object is created, and ends when the object is
 * destroyed.
 *
 * \code{.cpp}
 * #include "nvtx3/nvtx3.hpp"
 * void some_function() {
 *    // Begins a NVTX range with the message "some_function"
 *    // The range ends when some_function() returns and `r` is destroyed
 *    nvtx3::scoped_range r{"some_function"};
 *
 *    for(int i = 0; i < 5; ++i) {
 *       nvtx3::scoped_range loop{"loop range"};
 *       std::this_thread::sleep_for(std::chrono::seconds{1});
 *    }
 * } // Range ends when `r` is destroyed
 * \endcode
 *
 * The example code above generates the following timeline view in Nsight
 * Systems:
 *
 * \image html
 * https://raw.githubusercontent.com/NVIDIA/NVTX/release-v3/docs/images/example_range.png
 *
 * Alternatively, use the \ref MACROS like `NVTX3_FUNC_RANGE()` to add
 * ranges to your code that automatically use the name of the enclosing function
 * as the range's message.
 *
 * \code{.cpp}
 * #include "nvtx3/nvtx3.hpp"
 * void some_function() {
 *    // Creates a range with a message "some_function" that ends when the
 *    // enclosing function returns
 *    NVTX3_FUNC_RANGE();
 *    ...
 * }
 * \endcode
 *
 *
 * \section Overview
 *
 * The NVTX library provides a set of functions for users to annotate their code
 * to aid in performance profiling and optimization. These annotations provide
 * information to tools like Nsight Systems to improve visualization of
 * application timelines.
 *
 * \ref RANGES are one of the most commonly used NVTX constructs for annotating
 * a span of time. For example, imagine a user wanted to see every time a
 * function, `my_function`, is called and how long it takes to execute. This can
 * be accomplished with an NVTX range created on the entry to the function and
 * terminated on return from `my_function` using the push/pop C APIs:
 *
 * \code{.cpp}
 * void my_function(...) {
 *    nvtxRangePushA("my_function"); // Begins NVTX range
 *    // do work
 *    nvtxRangePop(); // Ends NVTX range
 * }
 * \endcode
 *
 * One of the challenges with using the NVTX C API is that it requires manually
 * terminating the end of the range with `nvtxRangePop`. This can be challenging
 * if `my_function()` has multiple returns or can throw exceptions as it
 * requires calling `nvtxRangePop()` before all possible return points.
 *
 * NVTX C++ solves this inconvenience through the "RAII" technique by providing
 * a `nvtx3::scoped_range` class that begins a range at construction and ends
 * the range on destruction. The above example then becomes:
 *
 * \code{.cpp}
 * void my_function(...) {
 *    nvtx3::scoped_range r{"my_function"}; // Begins NVTX range
 *    // do work
 * } // Range ends on exit from `my_function` when `r` is destroyed
 * \endcode
 *
 * The range object `r` is deterministically destroyed whenever `my_function`
 * returns---ending the NVTX range without manual intervention. For more
 * information, see \ref RANGES and `nvtx3::scoped_range_in`.
 *
 * Another inconvenience of the NVTX C APIs are the several constructs where the
 * user is expected to initialize an object at the beginning of an application
 * and reuse that object throughout the lifetime of the application. For example
 * see domains, categories, and registered messages.
 *
 * Example:
 * \code{.cpp}
 * nvtxDomainHandle_t D = nvtxDomainCreateA("my domain");
 * // Reuse `D` throughout the rest of the application
 * \endcode
 *
 * This can be problematic if the user application or library does not have an
 * explicit initialization function called before all other functions to
 * ensure that these long-lived objects are initialized before being used.
 *
 * NVTX C++ makes use of the "construct on first use" technique to alleviate
 * this inconvenience. In short, a function local static object is constructed
 * upon the first invocation of a function and returns a reference to that
 * object on all future invocations. See the documentation for `nvtx3::domain`,
 * `nvtx3::named_category`, `nvtx3::registered_string`, and
 * https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use for more
 * information.
 *
 * Using construct on first use, the above example becomes:
 * \code{.cpp}
 * struct my_domain{ static constexpr char const* name{"my domain"}; };
 *
 * // The first invocation of `domain::get` for the type `my_domain` will
 * // construct a `nvtx3::domain` object and return a reference to it. Future
 * // invocations simply return a reference.
 * nvtx3::domain const& D = nvtx3::domain::get<my_domain>();
 * \endcode
 * For more information about NVTX and how it can be used, see
 * https://docs.nvidia.com/cuda/profiler-users-guide/index.html#nvtx and
 * https://devblogs.nvidia.com/cuda-pro-tip-generate-custom-application-profile-timelines-nvtx/
 * for more information.
 *
 * \section RANGES Ranges
 *
 * Ranges are used to describe a span of time during the execution of an
 * application. Common examples are using ranges to annotate the time it takes
 * to execute a function or an iteration of a loop.
 *
 * NVTX C++ uses RAII to automate the generation of ranges that are tied to the
 * lifetime of objects. Similar to `std::lock_guard` in the C++ Standard
 * Template Library.
 *
 * \subsection scoped_range Scoped Range
 *
 * `nvtx3::scoped_range_in` is a class that begins a range upon construction
 * and ends the range at destruction. This is one of the most commonly used
 * constructs in NVTX C++ and is useful for annotating spans of time on a
 * particular thread. These ranges can be nested to arbitrary depths.
 *
 * `nvtx3::scoped_range` is an alias for a `nvtx3::scoped_range_in` in the
 * global NVTX domain. For more information about Domains, see \ref DOMAINS.
 *
 * Various attributes of a range can be configured constructing a
 * `nvtx3::scoped_range_in` with a `nvtx3::event_attributes` object. For
 * more information, see \ref ATTRIBUTES.
 *
 * Example:
 *
 * \code{.cpp}
 * void some_function() {
 *    // Creates a range for the duration of `some_function`
 *    nvtx3::scoped_range r{};
 *
 *    while(true) {
 *       // Creates a range for every loop iteration
 *       // `loop_range` is nested inside `r`
 *       nvtx3::scoped_range loop_range{};
 *    }
 * }
 * \endcode
 *
 * \subsection unique_range Unique Range
 *
 * `nvtx3::unique_range` is similar to `nvtx3::scoped_range`, with a few key differences:
 * - `unique_range` objects can be destroyed in any order whereas `scoped_range` objects must be
 *    destroyed in exact reverse creation order
 * - `unique_range` can start and end on different threads
 * - `unique_range` is movable
 * - `unique_range` objects can be constructed as heap objects
 *
 * There is extra overhead associated with `unique_range` constructs and therefore use of
 * `nvtx3::scoped_range_in` should be preferred.
 *
 * \subsection push_pop_range Push/Pop Range
 *
 * `nvtx3::push_range_in` and `nvtx3::pop_range_in` manually begin and end a
 * nested thread range. They wrap `nvtxDomainRangePushEx` and
 * `nvtxDomainRangePop` for cases where the push and pop cannot share one C++
 * object lifetime.
 *
 * Prefer `nvtx3::scoped_range_in` for normal scoped C++ code. Manual push/pop
 * ranges are intended for callback-driven code or other control flows where
 * range begin and end happen in separate scopes on the same thread.
 *
 * Example:
 *
 * \code{.cpp}
 * void begin_callback() {
 *    nvtx3::push_range_in<my_domain>("request");
 * }
 *
 * void end_callback() {
 *    nvtx3::pop_range_in<my_domain>();
 * }
 * \endcode
 *
 * \section MARKS Marks
 *
 * `nvtx3::mark` annotates an instantaneous point in time with a "marker".
 *
 * Unlike a "range" which has a beginning and an end, a marker is a single event
 * in an application, such as detecting a problem:
 *
 * \code{.cpp}
 * bool success = do_operation(...);
 * if (!success) {
 *    nvtx3::mark("operation failed!");
 * }
 * \endcode
 *
 * \section DOMAINS Domains
 *
 * Similar to C++ namespaces, domains allow for scoping NVTX events. By default,
 * all NVTX events belong to the "global" domain. Libraries and applications
 * should scope their events to use a custom domain to differentiate where the
 * events originate from.
 *
 * It is common for a library or application to have only a single domain and
 * for the name of that domain to be known at compile time. Therefore, Domains
 * in NVTX C++ are represented by _tag types_.
 *
 * For example, to define a custom domain, simply define a new concrete type
 * (a `class` or `struct`) with a `static` member called `name` that contains
 * the desired name of the domain.
 *
 * \code{.cpp}
 * struct my_domain{ static constexpr char const* name{"my domain"}; };
 * \endcode
 *
 * For any NVTX C++ construct that can be scoped to a domain, the type
 * `my_domain` can be passed as an explicit template argument to scope it to
 * the custom domain.
 *
 * The tag type `nvtx3::domain::global` represents the global NVTX domain.
 *
 * \code{.cpp}
 * // By default, `scoped_range_in` belongs to the global domain
 * nvtx3::scoped_range_in<> r0{};
 *
 * // Alias for a `scoped_range_in` in the global domain
 * nvtx3::scoped_range r1{};
 *
 * // `r` belongs to the custom domain
 * nvtx3::scoped_range_in<my_domain> r{};
 * \endcode
 *
 * When using a custom domain, it is recommended to define type aliases for NVTX
 * constructs in the custom domain.
 * \code{.cpp}
 * using my_scoped_range = nvtx3::scoped_range_in<my_domain>;
 * using my_registered_string = nvtx3::registered_string_in<my_domain>;
 * using my_named_category = nvtx3::named_category_in<my_domain>;
 * \endcode
 *
 * See `nvtx3::domain` for more information.
 *
 * \section ATTRIBUTES Event Attributes
 *
 * NVTX events can be customized with various attributes to provide additional
 * information (such as a custom message) or to control visualization of the
 * event (such as the color used). These attributes can be specified per-event
 * via arguments to a `nvtx3::event_attributes` object.
 *
 * NVTX events can be customized via five "attributes":
 * - \ref COLOR : color used to visualize the event in tools.
 * - \ref MESSAGES :  Custom message string.
 * - \ref PAYLOAD :  User-defined numerical value.
 * - \ref PAYLOAD_DATA : User-defined structured data (exclusive of payload).
 * - \ref CATEGORY : Intra-domain grouping.
 *
 * It is possible to construct a `nvtx3::event_attributes` from any number of
 * attribute objects (nvtx3::color, nvtx3::message, nvtx3::payload,
 * nvtx3::category, nvtx3::payload_data, or a container of nvtx3::payload_data)
 * in any order. If an attribute is not specified, a tool specific default value
 * is used. See `nvtx3::event_attributes` for more information.
 *
 * \code{.cpp}
 * // Set message, same as passing nvtx3::message{"message"}
 * nvtx3::event_attributes attr{"message"};
 *
 * // Set message and color
 * nvtx3::event_attributes attr{"message", nvtx3::rgb{127, 255, 0}};
 *
 * // Set message, color, payload, category
 * nvtx3::event_attributes attr{"message",
 *                              nvtx3::rgb{127, 255, 0},
 *                              nvtx3::payload{42},
 *                              nvtx3::category{1}};
 *
 * // Same as above -- can use any order of arguments
 * nvtx3::event_attributes attr{nvtx3::payload{42},
 *                              nvtx3::category{1},
 *                              "message",
 *                              nvtx3::rgb{127, 255, 0}};
 *
 * // Multiple arguments of the same type are allowed, but only the first is
 * // used -- in this example, payload is set to 42:
 * nvtx3::event_attributes attr{ nvtx3::payload{42}, nvtx3::payload{7} };
 * // payload and payload_data should not be used together.
 *
 * // Using the nvtx3 namespace in a local scope makes the syntax more succinct:
 * using namespace nvtx3;
 * event_attributes attr{"message", rgb{127, 255, 0}, payload{42}, category{1}};
 * \endcode
 *
 * \subsection MESSAGES message
 *
 * `nvtx3::message` sets the message string for an NVTX event.
 *
 * Example:
 * \code{.cpp}
 * // Create an `event_attributes` with the message "my message"
 * nvtx3::event_attributes attr{nvtx3::message{"my message"}};
 *
 * // strings and string literals implicitly assumed to be a `nvtx3::message`
 * nvtx3::event_attributes attr{"my message"};
 * \endcode
 *
 * \subsubsection REGISTERED_MESSAGE Registered Messages
 *
 * Associating a `nvtx3::message` with an event requires copying the contents of
 * the message every time the message is used, i.e., copying the entire message
 * string. This may cause non-trivial overhead in performance sensitive code.
 *
 * To eliminate this overhead, NVTX allows registering a message string,
 * yielding a "handle" that is inexpensive to copy that may be used in place of
 * a message string. When visualizing the events, tools such as Nsight Systems
 * will take care of mapping the message handle to its string.
 *
 * A message should be registered once and the handle reused throughout the rest
 * of the application. This can be done by either explicitly creating static
 * `nvtx3::registered_string` objects, or using the
 * `nvtx3::registered_string::get` construct on first use helper (recommended).
 *
 * Similar to \ref DOMAINS, `nvtx3::registered_string::get` requires defining a
 * custom tag type with a static `message` member whose value will be the
 * contents of the registered string.
 *
 * Example:
 * \code{.cpp}
 * // Explicitly constructed, static `registered_string` in my_domain:
 * static registered_string_in<my_domain> static_message{"my message"};
 *
 * // Or use construct on first use:
 * // Define a tag type with a `message` member string to register
 * struct my_message{ static constexpr char const* message{ "my message" }; };
 *
 * // Uses construct on first use to register the contents of
 * // `my_message::message`
 * auto& msg = nvtx3::registered_string_in<my_domain>::get<my_message>();
 * \endcode
 *
 * A `nvtx3::registered_string_in` can also be used inside of a struct for
 * `nvtx3::payload_data` using `TYPE_NVTX_REGISTERED_STRING_HANDLE`.
 *
 * \code{.cpp}
 *
 * \endcode
 *
 * \subsection COLOR color
 *
 * Associating a `nvtx3::color` with an event allows controlling how the event
 * is visualized in a tool such as Nsight Systems. This is a convenient way to
 * visually differentiate among different events.
 *
 * \code{.cpp}
 * // Define a color via rgb color values
 * nvtx3::color c{nvtx3::rgb{127, 255, 0}};
 * nvtx3::event_attributes attr{c};
 *
 * // rgb color values can be passed directly to an `event_attributes`
 * nvtx3::event_attributes attr1{nvtx3::rgb{127,255,0}};
 * \endcode
 *
 * \subsection CATEGORY category
 *
 * A `nvtx3::category` is simply an integer id that allows for fine-grain
 * grouping of NVTX events. For example, one might use separate categories for
 * IO, memory allocation, compute, etc.
 *
 * \code{.cpp}
 * nvtx3::event_attributes{nvtx3::category{1}};
 * \endcode
 *
 * \subsubsection NAMED_CATEGORIES Named Categories
 *
 * Associates a `name` string with a category `id` to help differentiate among
 * categories.
 *
 * For any given category id `Id`, a `named_category{Id, "name"}` should only
 * be constructed once and reused throughout an application. This can be done by
 * either explicitly creating static `nvtx3::named_category` objects, or using
 * the `nvtx3::named_category::get` construct on first use helper (recommended).
 *
 * Similar to \ref DOMAINS, `nvtx3::named_category::get` requires defining a
 * custom tag type with static `name` and `id` members.
 *
 * \code{.cpp}
 * // Explicitly constructed, static `named_category` in my_domain:
 * static nvtx3::named_category_in<my_domain> static_category{42, "my category"};
 *
 * // Or use construct on first use:
 * // Define a tag type with `name` and `id` members
 * struct my_category {
 *    static constexpr char const* name{"my category"}; // category name
 *    static constexpr uint32_t id{42}; // category id
 * };
 *
 * // Use construct on first use to name the category id `42`
 * // with name "my category":
 * auto& cat = named_category_in<my_domain>::get<my_category>();
 *
 * // Range `r` associated with category id `42`
 * nvtx3::event_attributes attr{cat};
 * \endcode
 *
 * \subsection PAYLOAD payload
 *
 * Allows associating a user-defined numerical value with an event.
 *
 * \code{.cpp}
 * // Constructs a payload from the `int32_t` value 42
 * nvtx3:: event_attributes attr{nvtx3::payload{42}};
 * \endcode
 *
 * \subsection PAYLOAD_DATA payload_data
 *
 * `nvtx3::payload_data` allows associating arbitrary structured data with an NVTX event.
 *
 * First, define a `struct` that is *standard-layout* and *trivially copyable*.
 * Then use `NVTX3_DEFINE_SCHEMA_GET` (`NVTX3_V1_DEFINE_SCHEMA_GET()`) to define the schema layout.
 * Finally you can use any such instance wrapped in `nvtx3::payload_data` in an
 * `nvtx3::event_attributes` object.
 * You can also pass containers (e.g., `std::vector`, `std::array`) of `payload_data` objects.
 * \note Constructing `nvtx3::payload_data` from temporaries or using tempoarary
 * `nvtx3::payload_data` objects is disabled to prevent dangling pointers.
 *
 * \section EXAMPLE Example
 *
 * Putting it all together:
 * \code{.cpp}
 * // Define a custom domain tag type
 * struct my_domain{ static constexpr char const* name{"my domain"}; };
 *
 * // Define a named category tag type
 * struct my_category{
 *    static constexpr char const* name{"my category"};
 *    static constexpr uint32_t id{42};
 * };
 *
 * // Define a registered string tag type
 * struct my_message{ static constexpr char const* message{"my message"}; };
 *
 * // For convenience, use aliases for domain scoped objects
 * using my_scoped_range = nvtx3::scoped_range_in<my_domain>;
 * using my_registered_string = nvtx3::registered_string_in<my_domain>;
 * using my_named_category = nvtx3::named_category_in<my_domain>;
 *
 * // Custom payload data
 * struct my_payload_data {
 *     uint32_t iteration;
 *     float    temperature;
 * };
 * NVTX3_DEFINE_SCHEMA_GET(
 *     my_domain,
 *     my_payload_data,
 *     "MyPayloadData",
 *     NVTX_PAYLOAD_ENTRIES((iteration, TYPE_UINT32, "Iteration Count"),
 *                          (temperature, TYPE_FLOAT, "Temperature"))
 * )
 *
 * // Default values for all attributes
 * nvtx3::event_attributes attr{};
 * my_scoped_range r0{attr};
 *
 * // Custom (unregistered) message, and unnamed category
 * nvtx3::event_attributes attr1{"message", nvtx3::category{2}};
 * my_scoped_range r1{attr1};
 *
 * // Alternatively, pass arguments of `event_attributes` constructor directly
 * // to `my_scoped_range`
 * my_scoped_range r2{"message", nvtx3::category{2}};
 *
 * // construct on first use a registered string
 * auto& msg = my_registered_string::get<my_message>();
 *
 * // construct on first use a named category
 * auto& cat = my_named_category::get<my_category>();
 *
 * // Use registered string and named category with a custom payload
 * my_scoped_range r3{msg, cat, nvtx3::payload{42}};
 *
 * // Use custom payload data
 * my_payload_data extras{42, 0.7f};
 * nvtx3::payload_data pd{extras};
 * my_scoped_range r4{pd};
 *
 * // Use a container of payload_data, can use different struct types
 * auto multiple = std::to_array({pd, pd2});
 * my_scoped_range r5{multiple};
 *
 * // Any number of arguments in any order
 * my_scoped_range r{nvtx3::rgb{127, 255, 0}, msg};
 *
 * \endcode
 * \section MACROS Convenience Macros
 *
 * Oftentimes users want to quickly and easily add NVTX ranges to their library
 * or application to aid in profiling and optimization.
 *
 * A convenient way to do this is to use the
 * \ref NVTX3_V1_FUNC_RANGE "NVTX3_FUNC_RANGE" and
 * \ref NVTX3_V1_FUNC_RANGE_IN "NVTX3_FUNC_RANGE_IN" macros. These macros take
 * care of constructing an
 * `nvtx3::scoped_range_in` with the name of the enclosing function as the
 * range's message.
 *
 * \code{.cpp}
 * void some_function() {
 *    // Automatically generates an NVTX range for the duration of the function
 *    // using "some_function" as the event's message.
 *    NVTX3_FUNC_RANGE();
 * }
 * \endcode
 *
 */

/* Temporary helper #defines, removed with #undef at end of header */

/* Some compilers do not correctly support SFINAE, which is used in this API
 * to detect common usage errors and provide clearer error messages (by using
 * static_assert) than the compiler would produce otherwise.  These compilers
 * will generate errors while compiling this file such as:
 *
 *  error: 'name' is not a member of 'nvtx3::v1::domain::global'
 *
 * The following compiler versions are known to have this problem, and so are
 * set by default to disable the SFINAE-based checks:
 *
 * - All MSVC versions prior to VS2017 Update 7 (15.7)
 * - GCC 8.1-8.3 (the problem was fixed in GCC 8.4)
 *
 * If you find your compiler hits this problem, you can work around it by
 * defining NVTX3_USE_CHECKED_OVERLOADS_FOR_GET to 0 before including this
 * header, or you can add a check for your compiler version to this #if.
 * Also, please report the issue on the NVTX GitHub page.
 */
#if !defined(NVTX3_USE_CHECKED_OVERLOADS_FOR_GET)
#if defined(_MSC_VER) && _MSC_VER < 1914 \
  || defined(__GNUC__) && __GNUC__ == 8 && __GNUC_MINOR__ < 4
#define NVTX3_USE_CHECKED_OVERLOADS_FOR_GET 0
#else
#define NVTX3_USE_CHECKED_OVERLOADS_FOR_GET 1
#endif
#define NVTX3_USE_CHECKED_OVERLOADS_FOR_GET_DEFINED_HERE
#endif

/* Within this header, nvtx3::NVTX3_VERSION_NAMESPACE resolves to nvtx3::vX,
 * where "X" is the major version number. */
#define NVTX3_CONCAT(A, B) A##B
#define NVTX3_NAMESPACE_FOR(VERSION) NVTX3_CONCAT(v, VERSION)
#define NVTX3_VERSION_NAMESPACE NVTX3_NAMESPACE_FOR(NVTX3_CPP_VERSION_MAJOR)
/* We use a different prefix to avoid ambiguity with the version namespace */
#define NVTX3_MINOR_NAMESPACE_FOR(VERSION) NVTX3_CONCAT(mv, VERSION)
#define NVTX3_MINOR_VERSION_NAMESPACE NVTX3_MINOR_NAMESPACE_FOR(NVTX3_CPP_VERSION_MINOR)

/* Avoid duplicating #if defined(NVTX3_INLINE_THIS_VERSION) for namespaces
 * in each minor version by making a macro to use unconditionally, which
 * resolves to "inline" or nothing as appropriate. */
#if defined(NVTX3_INLINE_THIS_VERSION)
#define NVTX3_INLINE_IF_REQUESTED inline
#else
#define NVTX3_INLINE_IF_REQUESTED
#endif

/* Enables the use of constexpr when support for C++14 constexpr is present.
 *
 * Initialization of a class member that is a union to a specific union member
 * can only be done in the body of a constructor, not in a member initializer
 * list.  A constexpr constructor must have an empty body until C++14, so there
 * is no way to make an initializer of a member union constexpr in C++11.  This
 * macro allows making functions constexpr in C++14 or newer, but non-constexpr
 * in C++11 compilation.  It is used here on constructors that initialize their
 * member unions.
 */
#if __cpp_constexpr >= 201304L
#define NVTX3_CONSTEXPR_IF_CPP14 constexpr
#else
#define NVTX3_CONSTEXPR_IF_CPP14
#endif

/* Enables the use of constexpr when support for C++20 constexpr is present.
 */
#if __cplusplus >= 202002L
#define NVTX3_CONSTEXPR_IF_CPP20 constexpr
#else
#define NVTX3_CONSTEXPR_IF_CPP20
#endif

// Macro wrappers for C++ attributes
#if !defined(__has_cpp_attribute)
#define __has_cpp_attribute(x) 0
#endif
#if __has_cpp_attribute(maybe_unused)
#define NVTX3_MAYBE_UNUSED [[maybe_unused]]
#else
#define NVTX3_MAYBE_UNUSED
#endif
#if __has_cpp_attribute(nodiscard)
#define NVTX3_NO_DISCARD [[nodiscard]]
#else
#define NVTX3_NO_DISCARD
#endif

 /* Use a macro for static asserts, which defaults to static_assert, but that
  * testing tools can replace with a logging function.  For example:
  * #define NVTX3_STATIC_ASSERT(c, m) \
  *   do { if (!(c)) printf("static_assert would fail: %s\n", m); } while (0)
  */
#if !defined(NVTX3_STATIC_ASSERT)
#define NVTX3_STATIC_ASSERT(condition, message) static_assert(condition, message)
#define NVTX3_STATIC_ASSERT_DEFINED_HERE
#endif

/* The original idea here was to have separate implementation sections, enclosed
 * in guard macros for each minor version. However, this approach was very
 * limited in practice. Specifically, it is not possible to add constructors to
 * `event_attributes`, which was needed for version 1.1 and is API compatible.
 * So we stick with `NVTX3_CPP_DEFINITIONS_V1_0` as guard macro for now, even
 * with bumped minor versions.
 */
#ifndef NVTX3_CPP_DEFINITIONS_V1_0
#define NVTX3_CPP_DEFINITIONS_V1_0

#include "nvToolsExt.h"
#include "nvToolsExtCounters.h"
#include "nvToolsExtPayload.h"
#include "nvToolsExtPayloadHelper.h"
#include "nvToolsExtSemanticsCorrelation.h"
#include "nvToolsExtSemanticsCounters.h"
#include "nvToolsExtSemanticsScope.h"
#include "nvToolsExtSemanticsTime.h"

#include <memory>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <cstddef>
#include <cstdint>

namespace nvtx3 {

NVTX3_INLINE_IF_REQUESTED namespace NVTX3_VERSION_NAMESPACE
{
inline namespace NVTX3_MINOR_VERSION_NAMESPACE
{

namespace detail {

template <typename Unused>
struct always_false : std::false_type {};

template <typename T, typename = void>
struct has_name : std::false_type {};
template <typename T>
struct has_name<T, decltype((void)T::name, void())> : std::true_type {};

template <typename T, typename = void>
struct has_id : std::false_type {};
template <typename T>
struct has_id<T, decltype((void)T::id, void())> : std::true_type {};

template <typename T, typename = void>
struct has_message : std::false_type {};
template <typename T>
struct has_message<T, decltype((void)T::message, void())> : std::true_type {};

template <typename T, typename = void>
struct is_c_string : std::false_type {};
template <typename T>
struct is_c_string<T, typename std::enable_if<
  std::is_convertible<T, char const*   >::value ||
  std::is_convertible<T, wchar_t const*>::value
>::type> : std::true_type {};

template <typename T>
using is_uint32 = std::is_same<typename std::decay<T>::type, uint32_t>;

template <typename... Args>
static inline void silence_unused(Args const&...) noexcept {}

// Type trait to check for a .data() member returning T* or const T*
// Some day, we can just use std::span
template <typename T, typename TD>
struct has_data_member
{
private:
    template <typename U>
    static auto test_data(int) -> decltype(std::declval<const U>().data());
    template <typename>
    static auto test_data(...) -> void;
    using return_type = decltype(test_data<T>(0));

public:
    static constexpr bool value =
        std::is_same<return_type, TD*>::value || std::is_same<return_type, TD const*>::value;
};

// Type trait to check for a .size() member returning something convertible to size_t
template <typename T>
struct has_size_member
{
private:
    template <typename U>
    static auto test_size(int) -> decltype(std::declval<const U>().size());
    template <typename>
    static auto test_size(...) -> void;
    using return_type = decltype(test_size<T>(0));

public:
    static constexpr bool value = !std::is_same<return_type, void>::value &&
                                    std::is_convertible<return_type, size_t>::value;
};

/**
    * To ensure that we can "safely" use one type as another, for pointers, in
    * arrays or in structs. This is used for our wrappers around C-types so that
    * we can pass them back to C.
    *
    * This is by no means perfect and foolproof.
    *
    * @tparam W The wrapper type.
    * @tparam U The type it is wrapping.
    */
template <typename W, typename U, typename = void>
struct is_safe_wrapper_of : std::false_type
{
};

template <typename W, typename U>
struct is_safe_wrapper_of<
    W,
    U,
    typename std::enable_if<
        std::is_standard_layout<W>::value && std::is_standard_layout<U>::value &&
        std::is_trivially_copyable<W>::value && std::is_trivially_copyable<U>::value &&
        sizeof(W) == sizeof(U)>::type> : std::true_type
{
};
/**
 * @brief Base class for semantic types, providing header access.
 *
 * The pointer returned by get() aliases internal storage of the builder and is
 * invalidated when the builder is destroyed. Semantic chains constructed from
 * another C++ semantic builder own a snapshot of the referenced chain, so
 * inline temporary chains are safe to use.
 *
 * @tparam DataType The underlying C struct type (e.g., nvtxSemanticsCounter_t).
 */
template <typename DataType>
class semantic_base {
public:
  /**
   * @brief Get pointer to the underlying C semantics header.
   *
   * The returned pointer aliases internal storage of this object. It is valid
   * only for the lifetime of *this and must not be used after this object is
   * destroyed.
   *
   * @return Pointer to the semantics header.
   */
  nvtxSemanticsHeader_t const* get() const & noexcept
  {
    return reinterpret_cast<nvtxSemanticsHeader_t const*>(&data_);
  }

protected:
  explicit semantic_base(DataType const& data) noexcept : data_{data} {}

  void set_next(nvtxSemanticsHeader_t const* next) noexcept
  {
    // Raw C-header chaining is non-owning; callers must keep next alive.
    next_owner_.reset();
    data_.header.next = next;
  }

  template <typename OtherDataType>
  void own_next(semantic_base<OtherDataType> const& next)
  {
    // C++ wrapper chaining snapshots next and points the C header at the owned copy.
    auto owned_next = std::make_shared<semantic_base<OtherDataType>>(next);
    data_.header.next = owned_next->get();
    next_owner_ = owned_next;
  }

  DataType data_;

private:
  // Type-erased owner for the semantic object addressed by data_.header.next.
  // Stored here to keep the pointer alive
  std::shared_ptr<void const> next_owner_;
};

/**
 * @brief Maps a C++ limit type to the matching nvtxSemanticsCounter_t storage.
 *
 * The primary template is intentionally undefined so that an unsupported T
 * produces a clear "no type named 'limit_type_value' in
 * counter_limit_traits<T>" diagnostic at the call site.
 */
template <typename T>
struct counter_limit_traits;

template <>
struct counter_limit_traits<int64_t>
{
  static constexpr int64_t limit_type_value = NVTX_COUNTER_LIMIT_I64;
  static NVTX3_CONSTEXPR_IF_CPP20 void store(nvtxCounterLimit_t& slot, int64_t v) noexcept
  {
    slot.i64 = v;
  }
};

template <>
struct counter_limit_traits<uint64_t>
{
  static constexpr int64_t limit_type_value = NVTX_COUNTER_LIMIT_U64;
  static NVTX3_CONSTEXPR_IF_CPP20 void store(nvtxCounterLimit_t& slot, uint64_t v) noexcept
  {
    slot.u64 = v;
  }
};

template <>
struct counter_limit_traits<double>
{
  static constexpr int64_t limit_type_value = NVTX_COUNTER_LIMIT_F64;
  static NVTX3_CONSTEXPR_IF_CPP20 void store(nvtxCounterLimit_t& slot, double v) noexcept
  {
    slot.f64 = v;
  }
};

} // namespace detail

/**
 * @brief `domain`s allow for grouping NVTX events into a single scope to
 * differentiate them from events in other `domain`s.
 *
 * By default, all NVTX constructs are placed in the "global" NVTX domain.
 *
 * A custom `domain` may be used in order to differentiate a library's or
 * application's NVTX events from other events.
 *
 * `domain`s are expected to be long-lived and unique to a library or
 * application. As such, it is assumed a domain's name is known at compile
 * time. Therefore, all NVTX constructs that can be associated with a domain
 * require the domain to be specified via a *type* `D` passed as an
 * explicit template parameter.
 *
 * The type `domain::global` may be used to indicate that the global NVTX
 * domain should be used.
 *
 * None of the C++ NVTX constructs require the user to manually construct a
 * `domain` object. Instead, if a custom domain is desired, the user is
 * expected to define a type `D` that contains a member
 * `D::name` which resolves to either a `char const*` or `wchar_t
 * const*`. The value of `D::name` is used to name and uniquely
 * identify the custom domain.
 *
 * Upon the first use of an NVTX construct associated with the type
 * `D`, the "construct on first use" pattern is used to construct a
 * function local static `domain` object. All future NVTX constructs
 * associated with `D` will use a reference to the previously
 * constructed `domain` object. See `domain::get`.
 *
 * Example:
 * \code{.cpp}
 * // The type `my_domain` defines a `name` member used to name and identify
 * // the `domain` object identified by `my_domain`.
 * struct my_domain{ static constexpr char const* name{"my_domain"}; };
 *
 * // The NVTX range `r` will be grouped with all other NVTX constructs
 * // associated with  `my_domain`.
 * nvtx3::scoped_range_in<my_domain> r{};
 *
 * // An alias can be created for a `scoped_range_in` in the custom domain
 * using my_scoped_range = nvtx3::scoped_range_in<my_domain>;
 * my_scoped_range my_range{};
 *
 * // `domain::global` indicates that the global NVTX domain is used
 * nvtx3::scoped_range_in<domain::global> r2{};
 *
 * // For convenience, `nvtx3::scoped_range` is an alias for a range in the
 * // global domain
 * nvtx3::scoped_range r3{};
 * \endcode
 */
class domain {
 public:
  domain(domain const&) = delete;
  domain& operator=(domain const&) = delete;
  domain(domain&&) = delete;
  domain& operator=(domain&&) = delete;

  /**
   * @brief Tag type for the "global" NVTX domain.
   *
   * This type may be passed as a template argument to any function/class
   * expecting a type to identify a domain to indicate that the global domain
   * should be used.
   *
   * All NVTX events in the global domain across all libraries and
   * applications will be grouped together.
   *
   */
  struct global {
  };

#if NVTX3_USE_CHECKED_OVERLOADS_FOR_GET
  /**
   * @brief Returns reference to an instance of a function local static
   * `domain` object.
   *
   * Uses the "construct on first use" idiom to safely ensure the `domain`
   * object is initialized exactly once upon first invocation of
   * `domain::get<D>()`. All following invocations will return a
   * reference to the previously constructed `domain` object. See
   * https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
   *
   * None of the constructs in this header require the user to directly invoke
   * `domain::get`. It is automatically invoked when constructing objects like
   * a `scoped_range_in` or `category`. Advanced users may wish to use
   * `domain::get` for the convenience of the "construct on first use" idiom
   * when using domains with their own use of the NVTX C API.
   *
   * This function is thread-safe as of C++11. If two or more threads call
   * `domain::get<D>` concurrently, exactly one of them is guaranteed
   * to construct the `domain` object and the other(s) will receive a
   * reference to the object after it is fully constructed.
   *
   * The domain's name is specified via the type `D` pass as an
   * explicit template parameter. `D` is required to contain a
   * member `D::name` that resolves to either a `char const*` or
   * `wchar_t const*`. The value of `D::name` is used to name and
   * uniquely identify the `domain`.
   *
   * Example:
   * \code{.cpp}
   * // The type `my_domain` defines a `name` member used to name and identify
   * // the `domain` object identified by `my_domain`.
   * struct my_domain{ static constexpr char const* name{"my domain"}; };
   *
   * auto& D1 = domain::get<my_domain>(); // First invocation constructs a
   *                                      // `domain` with the name "my domain"
   *
   * auto& D2 = domain::get<my_domain>(); // Quickly returns reference to
   *                                      // previously constructed `domain`.
   * \endcode
   *
   * @tparam D Type that contains a `D::name` member used to
   * name the `domain` object.
   * @return Reference to the `domain` corresponding to the type `D`.
   */
  template <typename D = global,
    typename std::enable_if<
      detail::is_c_string<decltype(D::name)>::value
    , int>::type = 0>
  NVTX3_NO_DISCARD static domain const& get() noexcept
  {
    static domain const d(D::name);
    return d;
  }

  /**
   * @brief Overload of `domain::get` to provide a clear compile error when
   * `D` has a `name` member that is not directly convertible to either
   * `char const*` or `wchar_t const*`.
   */
  template <typename D = global,
    typename std::enable_if<
      !detail::is_c_string<decltype(D::name)>::value
    , int>::type = 0>
  NVTX3_NO_DISCARD static domain const& get() noexcept
  {
    NVTX3_STATIC_ASSERT(detail::always_false<D>::value,
      "Type used to identify an NVTX domain must contain a static constexpr member "
      "called 'name' of type const char* or const wchar_t* -- 'name' member is not "
      "convertible to either of those types");
    static domain const unused;
    return unused;  // Function must compile for static_assert to be triggered
  }

  /**
   * @brief Overload of `domain::get` to provide a clear compile error when
   * `D` does not have a `name` member.
   */
  template <typename D = global,
    typename std::enable_if<
      !detail::has_name<D>::value
    , int>::type = 0>
  NVTX3_NO_DISCARD static domain const& get() noexcept
  {
    NVTX3_STATIC_ASSERT(detail::always_false<D>::value,
      "Type used to identify an NVTX domain must contain a static constexpr member "
      "called 'name' of type const char* or const wchar_t* -- 'name' member is missing");
    static domain const unused;
    return unused;  // Function must compile for static_assert to be triggered
  }
#else
  template <typename D = global>
  NVTX3_NO_DISCARD static domain const& get() noexcept
  {
    static domain const d(D::name);
    return d;
  }
#endif

  /**
   * @brief Conversion operator to `nvtxDomainHandle_t`.
   *
   * Allows transparently passing a domain object into an API expecting a
   * native `nvtxDomainHandle_t` object.
   */
  operator nvtxDomainHandle_t() const noexcept { return _domain; }

 private:
  /**
   * @brief Construct a new domain with the specified `name`.
   *
   * This constructor is private as it is intended that `domain` objects only
   * be created through the `domain::get` function.
   *
   * @param name A unique name identifying the domain
   */
  explicit domain(char const* name) noexcept : _domain{nvtxDomainCreateA(name)} {}

  /**
   * @brief Construct a new domain with the specified `name`.
   *
   * This constructor is private as it is intended that `domain` objects only
   * be created through the `domain::get` function.
   *
   * @param name A unique name identifying the domain
   */
  explicit domain(wchar_t const* name) noexcept : _domain{nvtxDomainCreateW(name)} {}

  /**
   * @brief Construct a new domain with the specified `name`.
   *
   * This constructor is private as it is intended that `domain` objects only
   * be created through the `domain::get` function.
   *
   * @param name A unique name identifying the domain
   */
  explicit domain(std::string const& name) noexcept : domain{name.c_str()} {}

  /**
   * @brief Construct a new domain with the specified `name`.
   *
   * This constructor is private as it is intended that `domain` objects only
   * be created through the `domain::get` function.
   *
   * @param name A unique name identifying the domain
   */
  explicit domain(std::wstring const& name) noexcept : domain{name.c_str()} {}

  /**
   * @brief Default constructor creates a `domain` representing the
   * "global" NVTX domain.
   *
   * All events not associated with a custom `domain` are grouped in the
   * "global" NVTX domain.
   *
   */
  constexpr domain() noexcept {}

  /**
   * @brief Intentionally avoid calling nvtxDomainDestroy on the `domain` object.
   *
   * No currently-available tools attempt to free domain resources when the
   * nvtxDomainDestroy function is called, due to the thread-safety and
   * efficiency challenges of freeing thread-local storage for other threads.
   * Since libraries may be disallowed from introducing static destructors,
   * and destroying the domain is likely to have no effect, the destructor
   * for `domain` intentionally chooses to not destroy the domain.
   *
   * In a situation where domain destruction is necessary, either manually
   * call nvtxDomainDestroy on the domain's handle, or make a class that
   * derives from `domain` and calls nvtxDomainDestroy in its destructor.
   */
  ~domain() = default;

 private:
  nvtxDomainHandle_t const _domain{};  ///< The `domain`s NVTX handle
};

/**
 * @brief Returns reference to the `domain` object that represents the global
 * NVTX domain.
 *
 * This specialization for `domain::global` returns a default constructed,
 * `domain` object for use when the "global" domain is desired.
 *
 * All NVTX events in the global domain across all libraries and applications
 * will be grouped together.
 *
 * @return Reference to the `domain` corresponding to the global NVTX domain.
 *
 */
template <>
NVTX3_NO_DISCARD inline domain const& domain::get<domain::global>() noexcept
{
  static domain const d{};
  return d;
}

/**
 * @brief Indicates the values of the red, green, and blue color channels for
 * an RGB color to use as an event attribute (assumes no transparency).
 *
 */
struct rgb {
  /// Type used for component values
  using component_type = uint8_t;

  /**
   * @brief Construct a rgb with red, green, and blue channels
   * specified by `red_`, `green_`, and `blue_`, respectively.
   *
   * Valid values are in the range `[0,255]`.
   *
   * @param red_ Value of the red channel
   * @param green_ Value of the green channel
   * @param blue_ Value of the blue channel
   */
  constexpr rgb(
    component_type red_,
    component_type green_,
    component_type blue_) noexcept
    : red{red_}, green{green_}, blue{blue_}
  {
  }

  component_type red{};    ///< Red channel value
  component_type green{};  ///< Green channel value
  component_type blue{};   ///< Blue channel value
};

/**
 * @brief Indicates the value of the alpha, red, green, and blue color
 * channels for an ARGB color to use as an event attribute.
 *
 */
struct argb final : rgb {
  /**
   * @brief Construct an argb with alpha, red, green, and blue channels
   * specified by `alpha_`, `red_`, `green_`, and `blue_`, respectively.
   *
   * Valid values are in the range `[0,255]`.
   *
   * @param alpha_  Value of the alpha channel (opacity)
   * @param red_  Value of the red channel
   * @param green_  Value of the green channel
   * @param blue_  Value of the blue channel
   *
   */
  constexpr argb(
    component_type alpha_,
    component_type red_,
    component_type green_,
    component_type blue_) noexcept
    : rgb{red_, green_, blue_}, alpha{alpha_}
  {
  }

  component_type alpha{};  ///< Alpha channel value
};

/**
 * @brief Represents a custom color that can be associated with an NVTX event
 * via its `event_attributes`.
 *
 * Specifying colors for NVTX events is a convenient way to visually
 * differentiate among different events in a visualization tool such as Nsight
 * Systems.
 *
 */
class color {
 public:
  /// Type used for the color's value
  using value_type = uint32_t;

  /**
   * @brief Constructs a `color` using the value provided by `hex_code`.
   *
   * `hex_code` is expected to be a 4 byte argb hex code.
   *
   * The most significant byte indicates the value of the alpha channel
   * (opacity) (0-255)
   *
   * The next byte indicates the value of the red channel (0-255)
   *
   * The next byte indicates the value of the green channel (0-255)
   *
   * The least significant byte indicates the value of the blue channel
   * (0-255)
   *
   * @param hex_code The hex code used to construct the `color`
   */
  constexpr explicit color(value_type hex_code) noexcept : _value{hex_code} {}

  /**
   * @brief Construct a `color` using the alpha, red, green, blue components
   * in `argb`.
   *
   * @param argb_ The alpha, red, green, blue components of the desired `color`
   */
  constexpr color(argb argb_) noexcept
    : color{from_bytes_msb_to_lsb(argb_.alpha, argb_.red, argb_.green, argb_.blue)}
  {
  }

  /**
   * @brief Construct a `color` using the red, green, blue components in
   * `rgb`.
   *
   * Uses maximum value for the alpha channel (opacity) of the `color`.
   *
   * @param rgb_ The red, green, blue components of the desired `color`
   */
  constexpr color(rgb rgb_) noexcept
    : color{from_bytes_msb_to_lsb(0xFF, rgb_.red, rgb_.green, rgb_.blue)}
  {
  }

  /**
   * @brief Returns the `color`s argb hex code
   *
   */
  constexpr value_type get_value() const noexcept { return _value; }

  /**
   * @brief Return the NVTX color type of the color.
   *
   */
  constexpr nvtxColorType_t get_type() const noexcept { return _type; }

  color() = delete;
  ~color() = default;
  color(color const&) = default;
  color& operator=(color const&) = default;
  color(color&&) = default;
  color& operator=(color&&) = default;

 private:
  /**
   * @brief Constructs an unsigned, 4B integer from the component bytes in
   * most to least significant byte order.
   *
   */
  constexpr static value_type from_bytes_msb_to_lsb(
    uint8_t byte3,
    uint8_t byte2,
    uint8_t byte1,
    uint8_t byte0) noexcept
  {
    return uint32_t{byte3} << 24 | uint32_t{byte2} << 16 | uint32_t{byte1} << 8 | uint32_t{byte0};
  }

  value_type _value{};                     ///< color's argb color code
  nvtxColorType_t _type{NVTX_COLOR_ARGB};  ///< NVTX color type code
};

/**
 * @brief Object for intra-domain grouping of NVTX events.
 *
 * A `category` is simply an integer id that allows for fine-grain grouping of
 * NVTX events. For example, one might use separate categories for IO, memory
 * allocation, compute, etc.
 *
 * Example:
 * \code{.cpp}
 * nvtx3::category cat1{1};
 *
 * // Range `r1` belongs to the category identified by the value `1`.
 * nvtx3::scoped_range r1{cat1};
 *
 * // Range `r2` belongs to the same category as `r1`
 * nvtx3::scoped_range r2{nvtx3::category{1}};
 * \endcode
 *
 * To associate a name string with a category id, see `named_category`.
 *
 */
class category {
 public:
  /// Type used for `category`s integer id.
  using id_type = uint32_t;

  /**
   * @brief Construct a `category` with the specified `id`.
   *
   * The `category` will be unnamed and identified only by its `id` value.
   *
   * All `category`s in a domain sharing the same `id` are equivalent.
   *
   * @param[in] id The `category`'s identifying value
   */
  constexpr explicit category(id_type id) noexcept : id_{id} {}

  /**
   * @brief Returns the id of the category.
   *
   */
  constexpr id_type get_id() const noexcept { return id_; }

  category() = delete;
  ~category() = default;
  category(category const&) = default;
  category& operator=(category const&) = default;
  category(category&&) = default;
  category& operator=(category&&) = default;

 private:
  id_type id_{};  ///< category's unique identifier
};

/**
 * @brief A `category` with an associated name string.
 *
 * Associates a `name` string with a category `id` to help differentiate among
 * categories.
 *
 * For any given category id `Id`, a `named_category(Id, "name")` should only
 * be constructed once and reused throughout an application. This can be done
 * by either explicitly creating static `named_category` objects, or using the
 * `named_category::get` construct on first use helper (recommended).
 *
 * Creating two or more `named_category` objects with the same value for `id`
 * in the same domain results in undefined behavior.
 *
 * Similarly, behavior is undefined when a `named_category` and `category`
 * share the same value of `id`.
 *
 * Example:
 * \code{.cpp}
 * // Explicitly constructed, static `named_category` in global domain:
 * static nvtx3::named_category static_category{42, "my category"};
 *
 * // Range `r` associated with category id `42`
 * nvtx3::scoped_range r{static_category};
 *
 * // OR use construct on first use:
 *
 * // Define a type with `name` and `id` members
 * struct my_category {
 *    static constexpr char const* name{"my category"}; // category name
 *    static constexpr uint32_t id{42}; // category id
 * };
 *
 * // Use construct on first use to name the category id `42`
 * // with name "my category"
 * auto& cat = named_category_in<my_domain>::get<my_category>();
 *
 * // Range `r` associated with category id `42`
 * nvtx3::scoped_range r{cat};
 * \endcode
 *
 * `named_category_in<D>`'s association of a name to a category id is local to
 * the domain specified by the type `D`. An id may have a different name in
 * another domain.
 *
 * @tparam D Type containing `name` member used to identify the `domain` to
 * which the `named_category_in` belongs. Else, `domain::global` to indicate
 * that the global NVTX domain should be used.
 */
template <typename D = domain::global>
class named_category_in final : public category {
 public:
#if NVTX3_USE_CHECKED_OVERLOADS_FOR_GET
  /**
   * @brief Returns a global instance of a `named_category_in` as a
   * function-local static.
   *
   * Creates a `named_category_in<D>` with name and id specified by the contents
   * of a type `C`. `C::name` determines the name and `C::id` determines the
   * category id.
   *
   * This function is useful for constructing a named `category` exactly once
   * and reusing the same instance throughout an application.
   *
   * Example:
   * \code{.cpp}
   * // Define a type with `name` and `id` members
   * struct my_category {
   *    static constexpr char const* name{"my category"}; // category name
   *    static constexpr uint32_t id{42}; // category id
   * };
   *
   * // Use construct on first use to name the category id `42`
   * // with name "my category"
   * auto& cat = named_category_in<my_domain>::get<my_category>();
   *
   * // Range `r` associated with category id `42`
   * nvtx3::scoped_range r{cat};
   * \endcode
   *
   * Uses the "construct on first use" idiom to safely ensure the `category`
   * object is initialized exactly once. See
   * https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
   *
   * @tparam C Type containing a member `C::name` that resolves to either a
   * `char const*` or `wchar_t const*` and `C::id`.
   */
  template <typename C,
    typename std::enable_if<
      detail::is_c_string<decltype(C::name)>::value &&
      detail::is_uint32<decltype(C::id)>::value
    , int>::type = 0>
  static named_category_in const& get() noexcept
  {
    static named_category_in const cat(C::id, C::name);
    return cat;
  }

  /**
   * @brief Overload of `named_category_in::get` to provide a clear compile error
   * when `C` has the required `name` and `id` members, but they are not the
   * required types.  `name` must be directly convertible to `char const*` or
   * `wchar_t const*`, and `id` must be `uint32_t`.
   */
  template <typename C,
    typename std::enable_if<
      !detail::is_c_string<decltype(C::name)>::value ||
      !detail::is_uint32<decltype(C::id)>::value
    , int>::type = 0>
  NVTX3_NO_DISCARD static named_category_in const& get() noexcept
  {
    NVTX3_STATIC_ASSERT(detail::is_c_string<decltype(C::name)>::value,
      "Type used to name an NVTX category must contain a static constexpr member "
      "called 'name' of type const char* or const wchar_t* -- 'name' member is not "
      "convertible to either of those types");
    NVTX3_STATIC_ASSERT(detail::is_uint32<decltype(C::id)>::value,
      "Type used to name an NVTX category must contain a static constexpr member "
      "called 'id' of type uint32_t -- 'id' member is the wrong type");
    static named_category_in const unused;
    return unused;  // Function must compile for static_assert to be triggered
  }

  /**
   * @brief Overload of `named_category_in::get` to provide a clear compile error
   * when `C` does not have the required `name` and `id` members.
   */
  template <typename C,
    typename std::enable_if<
      !detail::has_name<C>::value ||
      !detail::has_id<C>::value
    , int>::type = 0>
  NVTX3_NO_DISCARD static named_category_in const& get() noexcept
  {
    NVTX3_STATIC_ASSERT(detail::has_name<C>::value,
      "Type used to name an NVTX category must contain a static constexpr member "
      "called 'name' of type const char* or const wchar_t* -- 'name' member is missing");
    NVTX3_STATIC_ASSERT(detail::has_id<C>::value,
      "Type used to name an NVTX category must contain a static constexpr member "
      "called 'id' of type uint32_t -- 'id' member is missing");
    static named_category_in const unused;
    return unused;  // Function must compile for static_assert to be triggered
  }
#else
  template <typename C>
  NVTX3_NO_DISCARD static named_category_in const& get() noexcept
  {
    static named_category_in const cat(C::id, C::name);
    return cat;
  }
#endif

 private:
  // Default constructor is only used internally for static_assert(false) cases.
  named_category_in() noexcept : category{0} {}

 public:
  /**
   * @brief Construct a `named_category_in` with the specified `id` and `name`.
   *
   * The name `name` will be registered with `id`.
   *
   * Every unique value of `id` should only be named once.
   *
   * @param[in] id The category id to name
   * @param[in] name The name to associated with `id`
   */
  named_category_in(id_type id, char const* name) noexcept : category{id}
  {
#ifndef NVTX_DISABLE
    nvtxDomainNameCategoryA(domain::get<D>(), get_id(), name);
#else
    (void)id;
    (void)name;
#endif
  }

  /**
   * @brief Construct a `named_category_in` with the specified `id` and `name`.
   *
   * The name `name` will be registered with `id`.
   *
   * Every unique value of `id` should only be named once.
   *
   * @param[in] id The category id to name
   * @param[in] name The name to associated with `id`
   */
  named_category_in(id_type id, std::string const& name) noexcept
    : named_category_in{id, name.c_str()} {}

  /**
   * @brief Construct a `named_category_in` with the specified `id` and `name`.
   *
   * The name `name` will be registered with `id`.
   *
   * Every unique value of `id` should only be named once.
   *
   * @param[in] id The category id to name
   * @param[in] name The name to associated with `id`
   */
  named_category_in(id_type id, wchar_t const* name) noexcept : category{id}
  {
#ifndef NVTX_DISABLE
    nvtxDomainNameCategoryW(domain::get<D>(), get_id(), name);
#else
    (void)id;
    (void)name;
#endif
  }

  /**
   * @brief Construct a `named_category_in` with the specified `id` and `name`.
   *
   * The name `name` will be registered with `id`.
   *
   * Every unique value of `id` should only be named once.
   *
   * @param[in] id The category id to name
   * @param[in] name The name to associated with `id`
   */
  named_category_in(id_type id, std::wstring const& name) noexcept
    : named_category_in{id, name.c_str()} {}
};

/**
 * @brief Alias for a `named_category_in` in the global NVTX domain.
 *
 */
using named_category = named_category_in<domain::global>;

/**
 * @brief A message registered with NVTX.
 *
 * Normally, associating a `message` with an NVTX event requires copying the
 * contents of the message string. This may cause non-trivial overhead in
 * highly performance sensitive regions of code.
 *
 * message registration is an optimization to lower the overhead of
 * associating a message with an NVTX event. Registering a message yields a
 * handle that is inexpensive to copy that may be used in place of a message
 * string.
 *
 * A particular message should only be registered once and the handle
 * reused throughout the rest of the application. This can be done by either
 * explicitly creating static `registered_string_in` objects, or using the
 * `registered_string_in::get` construct on first use helper (recommended).
 *
 * Example:
 * \code{.cpp}
 * // Explicitly constructed, static `registered_string` in my_domain:
 * static registered_string_in<my_domain> static_message{"message"};
 *
 * // "message" is associated with the range `r`
 * nvtx3::scoped_range r{static_message};
 *
 * // Or use construct on first use:
 *
 * // Define a type with a `message` member that defines the contents of the
 * // registered string
 * struct my_message{ static constexpr char const* message{ "my message" }; };
 *
 * // Uses construct on first use to register the contents of
 * // `my_message::message`
 * auto& msg = registered_string_in<my_domain>::get<my_message>();
 *
 * // "my message" is associated with the range `r`
 * nvtx3::scoped_range r{msg};
 * \endcode
 *
 * `registered_string_in`s are local to a particular domain specified via
 * the type `D`.
 *
 * @tparam D Type containing `name` member used to identify the `domain` to
 * which the `registered_string_in` belongs. Else, `domain::global` to indicate
 * that the global NVTX domain should be used.
 */
template <typename D = domain::global>
class registered_string_in {
 public:
#if NVTX3_USE_CHECKED_OVERLOADS_FOR_GET
  /**
   * @brief Returns a global instance of a `registered_string_in` as a function
   * local static.
   *
   * Provides a convenient way to register a message with NVTX without having
   * to explicitly register the message.
   *
   * Upon first invocation, constructs a `registered_string_in` whose contents
   * are specified by `message::message`.
   *
   * All future invocations will return a reference to the object constructed
   * in the first invocation.
   *
   * Example:
   * \code{.cpp}
   * // Define a type with a `message` member that defines the contents of the
   * // registered string
   * struct my_message{ static constexpr char const* message{ "my message" };
   * };
   *
   * // Uses construct on first use to register the contents of
   * // `my_message::message`
   * auto& msg = registered_string_in<my_domain>::get<my_message>();
   *
   * // "my message" is associated with the range `r`
   * nvtx3::scoped_range r{msg};
   * \endcode
   *
   * @tparam M Type required to contain a member `M::message` that
   * resolves to either a `char const*` or `wchar_t const*` used as the
   * registered string's contents.
   * @return Reference to a `registered_string_in` associated with the type `M`.
   */
  template <typename M,
    typename std::enable_if<
      detail::is_c_string<decltype(M::message)>::value
    , int>::type = 0>
  NVTX3_NO_DISCARD static registered_string_in const& get() noexcept
  {
    static registered_string_in const regstr(M::message);
    return regstr;
  }

  /**
   * @brief Overload of `registered_string_in::get` to provide a clear compile error
   * when `M` has a `message` member that is not directly convertible to either
   * `char const*` or `wchar_t const*`.
   */
  template <typename M,
    typename std::enable_if<
      !detail::is_c_string<decltype(M::message)>::value
    , int>::type = 0>
  NVTX3_NO_DISCARD static registered_string_in const& get() noexcept
  {
    NVTX3_STATIC_ASSERT(detail::always_false<M>::value,
      "Type used to register an NVTX string must contain a static constexpr member "
      "called 'message' of type const char* or const wchar_t* -- 'message' member is "
      "not convertible to either of those types");
    static registered_string_in const unused;
    return unused;  // Function must compile for static_assert to be triggered
  }

  /**
   * @brief Overload of `registered_string_in::get` to provide a clear compile error when
   * `M` does not have a `message` member.
   */
  template <typename M,
    typename std::enable_if<
      !detail::has_message<M>::value
    , int>::type = 0>
  NVTX3_NO_DISCARD static registered_string_in const& get() noexcept
  {
    NVTX3_STATIC_ASSERT(detail::always_false<M>::value,
      "Type used to register an NVTX string must contain a static constexpr member "
      "called 'message' of type const char* or const wchar_t* -- 'message' member "
      "is missing");
    static registered_string_in const unused;
    return unused;  // Function must compile for static_assert to be triggered
  }
#else
  template <typename M>
  NVTX3_NO_DISCARD static registered_string_in const& get() noexcept
  {
    static registered_string_in const regstr(M::message);
    return regstr;
  }
#endif

  /**
   * @brief Constructs a `registered_string_in` from the specified `msg` string.
   *
   * Registers `msg` with NVTX and associates a handle with the registered
   * message.
   *
   * A particular message should should only be registered once and the handle
   * reused throughout the rest of the application.
   *
   * @param msg The contents of the message
   */
  explicit registered_string_in(char const* msg) noexcept
    : handle_{nvtxDomainRegisterStringA(domain::get<D>(), msg)}
  {
  }

  /**
   * @brief Constructs a `registered_string_in` from the specified `msg` string.
   *
   * Registers `msg` with NVTX and associates a handle with the registered
   * message.
   *
   * A particular message should should only be registered once and the handle
   * reused throughout the rest of the application.
   *
   * @param msg The contents of the message
   */
  explicit registered_string_in(std::string const& msg) noexcept
    : registered_string_in{msg.c_str()} {}

  /**
   * @brief Constructs a `registered_string_in` from the specified `msg` string.
   *
   * Registers `msg` with NVTX and associates a handle with the registered
   * message.
   *
   * A particular message should should only be registered once and the handle
   * reused throughout the rest of the application.
   *
   * @param msg The contents of the message
   */
  explicit registered_string_in(wchar_t const* msg) noexcept
    : handle_{nvtxDomainRegisterStringW(domain::get<D>(), msg)}
  {
  }

  /**
   * @brief Constructs a `registered_string_in` from the specified `msg` string.
   *
   * Registers `msg` with NVTX and associates a handle with the registered
   * message.
   *
   * A particular message should only be registered once and the handle
   * reused throughout the rest of the application.
   *
   * @param msg The contents of the message
   */
  explicit registered_string_in(std::wstring const& msg) noexcept
    : registered_string_in{msg.c_str()} {}

  /**
   * @brief Returns the registered string's handle
   *
   */
  nvtxStringHandle_t get_handle() const noexcept
  {
    // We want to ensure that we can use the registered_string in place of a handle
    // in a payload data struct.
    NVTX3_STATIC_ASSERT(
      (detail::is_safe_wrapper_of<registered_string_in<D>, nvtxStringHandle_t>::value),
      "Internal error! registered_string_in is potentially unsafe.");
    return handle_;
  }

private:
  // Default constructor is only used internally for static_assert(false) cases.
  registered_string_in() noexcept {}
public:
  ~registered_string_in() = default;
  registered_string_in(registered_string_in const&) = default;
  registered_string_in& operator=(registered_string_in const&) = default;
  registered_string_in(registered_string_in&&) = default;
  registered_string_in& operator=(registered_string_in&&) = default;

 private:
  nvtxStringHandle_t handle_{};  ///< The handle returned from
                                 ///< registering the message with NVTX
};

/**
 * @brief Alias for a `registered_string_in` in the global NVTX domain.
 *
 */
using registered_string = registered_string_in<domain::global>;

/**
 * @brief Allows associating a message string with an NVTX event via
 * its `EventAttribute`s.
 *
 * Associating a `message` with an NVTX event through its `event_attributes`
 * allows for naming events to easily differentiate them from other events.
 *
 * Every time an NVTX event is created with an associated `message`, the
 * contents of the message string must be copied.  This may cause non-trivial
 * overhead in highly performance sensitive sections of code. Use of a
 * `nvtx3::registered_string` is recommended in these situations.
 *
 * Example:
 * \code{.cpp}
 * // Creates an `event_attributes` with message "message 0"
 * nvtx3::event_attributes attr0{nvtx3::message{"message 0"}};
 *
 * // `range0` contains message "message 0"
 * nvtx3::scoped_range range0{attr0};
 *
 * // `std::string` and string literals are implicitly assumed to be
 * // the contents of an `nvtx3::message`
 * // Creates an `event_attributes` with message "message 1"
 * nvtx3::event_attributes attr1{"message 1"};
 *
 * // `range1` contains message "message 1"
 * nvtx3::scoped_range range1{attr1};
 *
 * // `range2` contains message "message 2"
 * nvtx3::scoped_range range2{nvtx3::message{"message 2"}};
 *
 * // `std::string` and string literals are implicitly assumed to be
 * // the contents of an `nvtx3::message`
 * // `range3` contains message "message 3"
 * nvtx3::scoped_range range3{"message 3"};
 * \endcode
 */
class message {
 public:
  using value_type = nvtxMessageValue_t;

  /**
   * @brief Construct a `message` whose contents are specified by `msg`.
   *
   * @param msg The contents of the message
   */
  NVTX3_CONSTEXPR_IF_CPP14 message(char const* msg) noexcept : type_{NVTX_MESSAGE_TYPE_ASCII}
  {
    value_.ascii = msg;
  }

  /**
   * @brief Construct a `message` whose contents are specified by `msg`.
   *
   * @param msg The contents of the message
   */
  message(std::string const& msg) noexcept : message{msg.c_str()} {}

  /**
   * @brief Disallow construction for `std::string` r-value
   *
   * `message` is a non-owning type and therefore cannot take ownership of an
   * r-value. Therefore, constructing from an r-value is disallowed to prevent
   * a dangling pointer.
   *
   */
#ifndef NVTX3_ALLOW_RVALUE_CONSTRUCTORS
  message(std::string&&) = delete;
#endif

  /**
   * @brief Construct a `message` whose contents are specified by `msg`.
   *
   * @param msg The contents of the message
   */
  NVTX3_CONSTEXPR_IF_CPP14 message(wchar_t const* msg) noexcept : type_{NVTX_MESSAGE_TYPE_UNICODE}
  {
    value_.unicode = msg;
  }

  /**
   * @brief Construct a `message` whose contents are specified by `msg`.
   *
   * @param msg The contents of the message
   */
  message(std::wstring const& msg) noexcept : message{msg.c_str()} {}

  /**
   * @brief Disallow construction for `std::wstring` r-value
   *
   * `message` is a non-owning type and therefore cannot take ownership of an
   * r-value. Therefore, constructing from an r-value is disallowed to prevent
   * a dangling pointer.
   *
   */
#ifndef NVTX3_ALLOW_RVALUE_CONSTRUCTORS
  message(std::wstring&&) = delete;
#endif

  /**
   * @brief Construct a `message` from a `registered_string_in`.
   *
   * @tparam D Type containing `name` member used to identify the `domain`
   * to which the `registered_string_in` belongs. Else, `domain::global` to
   * indicate that the global NVTX domain should be used.
   * @param msg The message that has already been registered with NVTX.
   */
  template <typename D>
  NVTX3_CONSTEXPR_IF_CPP14 message(registered_string_in<D> const& msg) noexcept
    : type_{NVTX_MESSAGE_TYPE_REGISTERED}
  {
    value_.registered = msg.get_handle();
  }

  /**
   * @brief Construct a `message` from NVTX C API type and value.
   *
   * @param type nvtxMessageType_t enum value indicating type of the payload
   * @param value nvtxMessageValue_t union containing message
   */
  constexpr message(
    nvtxMessageType_t const& type,
    nvtxMessageValue_t const& value) noexcept
    : type_{type}, value_(value)
  {
  }

  /**
   * @brief Construct a `message` from NVTX C API registered string handle.
   *
   * @param handle nvtxStringHandle_t value of registered string handle
   */
  NVTX3_CONSTEXPR_IF_CPP14 message(nvtxStringHandle_t handle) noexcept
    : type_{NVTX_MESSAGE_TYPE_REGISTERED}
  {
    value_.registered = handle;
  }

  /**
   * @brief Return the union holding the value of the message.
   *
   */
  constexpr value_type get_value() const noexcept { return value_; }

  /**
   * @brief Return the type information about the value the union holds.
   *
   */
  constexpr nvtxMessageType_t get_type() const noexcept { return type_; }

 private:
  nvtxMessageType_t type_{};    ///< message type
  nvtxMessageValue_t value_{};  ///< message contents
};

/**
 * @brief A numerical value that can be associated with an NVTX event via
 * its `event_attributes`.
 *
 * Example:
 * \code{.cpp}
 * // Constructs a payload from the int32_t value 42
 * nvtx3:: event_attributes attr{nvtx3::payload{42}};
 *
 * // `range0` will have an int32_t payload of 42
 * nvtx3::scoped_range range0{attr};
 *
 * // range1 has double payload of 3.14
 * nvtx3::scoped_range range1{nvtx3::payload{3.14}};
 * \endcode
 */
class payload {
 public:
  using value_type = typename nvtxEventAttributes_v2::payload_t;

  /**
   * @brief Construct a `payload` from a signed, 8 byte integer.
   *
   * @param value Value to use as contents of the payload
   */
  NVTX3_CONSTEXPR_IF_CPP14 explicit payload(int64_t value) noexcept
    : type_{NVTX_PAYLOAD_TYPE_INT64}, value_{}
  {
    value_.llValue = value;
  }

  /**
   * @brief Construct a `payload` from a signed, 4 byte integer.
   *
   * @param value Value to use as contents of the payload
   */
  NVTX3_CONSTEXPR_IF_CPP14 explicit payload(int32_t value) noexcept
    : type_{NVTX_PAYLOAD_TYPE_INT32}, value_{}
  {
    value_.iValue = value;
  }

  /**
   * @brief Construct a `payload` from an unsigned, 8 byte integer.
   *
   * @param value Value to use as contents of the payload
   */
  NVTX3_CONSTEXPR_IF_CPP14 explicit payload(uint64_t value) noexcept
    : type_{NVTX_PAYLOAD_TYPE_UNSIGNED_INT64}, value_{}
  {
    value_.ullValue = value;
  }

  /**
   * @brief Construct a `payload` from an unsigned, 4 byte integer.
   *
   * @param value Value to use as contents of the payload
   */
  NVTX3_CONSTEXPR_IF_CPP14 explicit payload(uint32_t value) noexcept
    : type_{NVTX_PAYLOAD_TYPE_UNSIGNED_INT32}, value_{}
  {
    value_.uiValue = value;
  }

  /**
   * @brief Construct a `payload` from a single-precision floating point
   * value.
   *
   * @param value Value to use as contents of the payload
   */
  NVTX3_CONSTEXPR_IF_CPP14 explicit payload(float value) noexcept
    : type_{NVTX_PAYLOAD_TYPE_FLOAT}, value_{}
  {
    value_.fValue = value;
  }

  /**
   * @brief Construct a `payload` from a double-precision floating point
   * value.
   *
   * @param value Value to use as contents of the payload
   */
  NVTX3_CONSTEXPR_IF_CPP14 explicit payload(double value) noexcept
    : type_{NVTX_PAYLOAD_TYPE_DOUBLE}, value_{}
  {
    value_.dValue = value;
  }

  /**
   * @brief Construct a `payload` from NVTX C API type and value.
   *
   * @param type nvtxPayloadType_t enum value indicating type of the payload
   * @param value nvtxEventAttributes_t::payload_t union containing payload
   */
  constexpr payload(
    nvtxPayloadType_t const& type,
    value_type const& value) noexcept
    : type_{type}, value_(value)
  {
  }

  /**
   * @brief Return the union holding the value of the payload
   *
   */
  constexpr value_type get_value() const noexcept { return value_; }

  /**
   * @brief Return the information about the type the union holds.
   *
   */
  constexpr nvtxPayloadType_t get_type() const noexcept { return type_; }

 private:
  nvtxPayloadType_t type_;  ///< Type of the payload value
  value_type value_;        ///< Union holding the payload value
};

/**
 * @brief Represents the registered schema for a payload struct.
 *
 * This class encapsulates the schema ID obtained by registering a payload struct
 * using `nvtxPayloadSchemaRegister`. The primary mechanism for obtaining an
 * instance is via the static template function `get<T>()`, which must be
 * specialized for each payload struct type `T` using the
 * `NVTX3_DEFINE_SCHEMA_GET` (`NVTX3_V1_DEFINE_SCHEMA_GET()`) macro.
 *
 * The schema ID is used internally when constructing `payload_data` objects.
 */
class schema
{
private:
  /**
   * @brief Private default constructor.
   *
   * Used only internally for static_assert(false) cases.
   */
  schema()
      : _schema_id{0}
  {}

  public:
  schema(schema const&) = delete;
  schema& operator=(schema const&) = delete;
  schema(schema&&) = delete;
  schema& operator=(schema&&) = delete;

  /**
   * @brief Constructs a schema object directly from a schema ID.
   *
   * This constructor is primarily for internal use or advanced scenarios where
   * the schema ID is obtained manually.
   *
   * @param id The NVTX schema ID.
   */
  explicit schema(uint64_t id)
      : _schema_id{id}
  {}

  /**
   * @brief Gets the schema instance for a specific payload struct type.
   *
   * This function relies on template specialization. Users must provide a
   * specialization for each payload struct type `T` using the
   * `NVTX3_DEFINE_SCHEMA_GET` (`NVTX3_V1_DEFINE_SCHEMA_GET()`) macro.
   *
   * @tparam T The payload struct type for which to get the schema.
   * @return A constant reference to the schema object for type `T`.
   */
  template <typename T>
  NVTX3_NO_DISCARD static schema const& get() noexcept
  {
    NVTX3_STATIC_ASSERT(
        detail::always_false<T>::value,
        "payload_data schema deduction requires a template specialization. Use the macro "
        "NVTX3_DEFINE_SCHEMA_GET to generate this specialization.");
    static schema unused;
    return unused;
  }

  /**
   * @brief Return the underlying C handle of the schema.
   */
  uint64_t get_handle() const noexcept
  {
    return _schema_id;
  }

private:
  uint64_t const _schema_id;
};

/**
 * @brief Wrapper around the NVTX C API `nvtxPayloadData_t` struct.
 *
 * This class facilitates associating a structured payload with an NVTX event.
 * It combines a pointer to the payload data instance with its registered schema
 * ID and size.
 */
class payload_data
{
public:
  /**
   * @brief Constructs `payload_data` from an existing NVTX C API struct.
   *
   * Provides interoperability with code using the NVTX C API directly.
   *
   * @param pd An existing `nvtxPayloadData_t` struct.
   */
  explicit payload_data(nvtxPayloadData_t const& pd)
      : data_(pd)
  {}

  /**
   * @brief Constructs `payload_data` for a specific payload struct instance.
   *
   * This template constructor automatically retrieves the necessary information
   * from the given struct type `T` and the reference to the instance.
   * The type `T` must be standard layout and trivially copyable as well as
   * have a `schema::get<T>()` specialization via `NVTX3_DEFINE_SCHEMA_GET` (`NVTX3_V1_DEFINE_SCHEMA_GET()`).
   *
   * Make sure the provided referenace is valid for the lifetime of the created
   * `payload_data` object.
   *
   * @param t A constant reference to the payload struct instance.
   */
  // We cannot simply delete the rvalue constructor here because with a template
  // parameter that would be a forwarding reference. Thus, we have this one
  // ctor with a forwarding reference and use a static assert for the rvalue check.
  // Disable this for the C-style nvtxPayloadData_t to prefer above ctor for non-const.
  template <
      typename R,
      typename T = typename std::remove_cv<typename std::remove_reference<R>::type>::type,
      typename = typename std::enable_if<!std::is_same<T, nvtxPayloadData_t>::value>::type>
  explicit payload_data(R&& t)
      : data_{schema::get<T>().get_handle(), sizeof(T), &t}
  {
#ifndef NVTX3_ALLOW_RVALUE_CONSTRUCTORS
    NVTX3_STATIC_ASSERT(
        std::is_lvalue_reference<R>::value,
        "payload_data requires an lvalue reference to the underlying data. Constructing "
        "from an rvalue is potentially unsafe and therefore forbidden.");
#endif
    NVTX3_STATIC_ASSERT(
        std::is_standard_layout<T>::value && std::is_trivially_copyable<T>::value,
        "structs used for NVTX3 payload schema must be standard layout and trivially copyable");
  }

  /**
   * @brief Constructs `payload_data` for a payload instance with a given schema.
   *
   * Use this constructor if you have a dynamic schema for your struct rather than
   * a static one defined by `NVTX3_DEFINE_SCHEMA_GET` (`NVTX3_V1_DEFINE_SCHEMA_GET()`).
   *
   * Make sure the provided referenace is valid for the lifetime of the created
   * `payload_data` object.
   *
   * @param t A constant reference to the payload struct instance.
   * @param s The schema to use for the payload data.
   */
  template <typename T>
  explicit payload_data(T const& t, schema s)
      : data_{s.get_handle(), sizeof(T), &t}
  {
    NVTX3_STATIC_ASSERT(
        std::is_standard_layout<T>::value && std::is_trivially_copyable<T>::value,
        "structs used for NVTX3 payload schema must be standard layout and trivially copyable");
  }

  /**
   * @return A pointer to the `nvtxPayloadData_t` struct as ullValue to use
   * with NVTX C API functions.
   */
  uint64_t as_ull_value() const noexcept
  {
    NVTX3_STATIC_ASSERT(
        (detail::is_safe_wrapper_of<payload_data, nvtxPayloadData_t>::value),
        "Internal error! payload_data is potentially unsafe.");
    return NVTX_POINTER_AS_PAYLOAD_ULLVALUE(&data_);
  }

private:
  nvtxPayloadData_t data_;
};

/**
 * @brief Describes the attributes of a NVTX event.
 *
 * NVTX events can be customized via four "attributes":
 *
 * - color:    color used to visualize the event in tools such as Nsight
 *             Systems. See `color`.
 * - message:  Custom message string. See `message`.
 * - payload:  User-defined numerical value. See `payload`.
 * - category: Intra-domain grouping. See `category`.
 *
 * These component attributes are specified via an `event_attributes` object.
 * See `nvtx3::color`, `nvtx3::message`, `nvtx3::payload`, and
 * `nvtx3::category` for how these individual attributes are constructed.
 *
 * While it is possible to specify all four attributes, it is common to want
 * to only specify a subset of attributes and use default values for the
 * others. For convenience, `event_attributes` can be constructed from any
 * number of attribute components in any order.
 *
 * Example:
 * \code{.cpp}
 * // Set message, same as using nvtx3::message{"message"}
 * event_attributes attr{"message"};
 *
 * // Set message and color
 * event_attributes attr{"message", nvtx3::rgb{127, 255, 0}};
 *
 * // Set message, color, payload, category
 * event_attributes attr{"message",
 *                       nvtx3::rgb{127, 255, 0},
 *                       nvtx3::payload{42},
 *                       nvtx3::category{1}};
 *
 * // Same as above -- can use any order of arguments
 * event_attributes attr{nvtx3::payload{42},
 *                       nvtx3::category{1},
 *                       "message",
 *                       nvtx3::rgb{127, 255, 0}};
 *
 * // Multiple arguments of the same type are allowed, but only the first is
 * // used -- in this example, payload is set to 42:
 * event_attributes attr{ nvtx3::payload{42}, nvtx3::payload{7} };
 *
 * // Range `r` will be customized according the attributes in `attr`
 * nvtx3::scoped_range r{attr};
 *
 * // For convenience, `event_attributes` constructor arguments may be passed
 * // to the `scoped_range_in` constructor -- they are forwarded to the
 * // `event_attributes` constructor
 * nvtx3::scoped_range r{nvtx3::payload{42}, nvtx3::category{1}, "message"};
 *
 * // Using the nvtx3 namespace in a local scope makes the syntax more succinct:
 * using namespace nvtx3;
 * scoped_range r{payload{42}, category{1}, "message"};
 * \endcode
 *
 */
class event_attributes {
 public:
  using value_type = nvtxEventAttributes_t;

  /**
   * @brief Default constructor creates an `event_attributes` with no
   * category, color, payload, nor message.
   */
  constexpr event_attributes() noexcept
    : attributes_{
        NVTX_VERSION,                   // version
        sizeof(nvtxEventAttributes_t),  // size
        0,                              // category
        NVTX_COLOR_UNKNOWN,             // color type
        0,                              // color value
        NVTX_PAYLOAD_UNKNOWN,           // payload type
        0,                              // reserved 4B
        {0},                            // payload value (union)
        NVTX_MESSAGE_UNKNOWN,           // message type
        {nullptr}                       // message value (union)
      }
  {
  }

  /**
   * @brief Variadic constructor where the first argument is a `category`.
   *
   * Sets the value of the `EventAttribute`s category based on `c` and
   * forwards the remaining variadic parameter pack to the next constructor.
   *
   */
  template <typename... Args>
  NVTX3_CONSTEXPR_IF_CPP14 explicit event_attributes(category const& c, Args const&... args) noexcept
    : event_attributes(args...)
  {
    attributes_.category = c.get_id();
  }

  /**
   * @brief Variadic constructor where the first argument is a `color`.
   *
   * Sets the value of the `EventAttribute`s color based on `c` and forwards
   * the remaining variadic parameter pack to the next constructor.
   *
   */
  template <typename... Args>
  NVTX3_CONSTEXPR_IF_CPP14 explicit event_attributes(color const& c, Args const&... args) noexcept
    : event_attributes(args...)
  {
    attributes_.color     = c.get_value();
    attributes_.colorType = c.get_type();
  }

  /**
   * @brief Variadic constructor where the first argument is a `payload`.
   *
   * Sets the value of the `EventAttribute`s payload based on `p` and forwards
   * the remaining variadic parameter pack to the next constructor.
   *
   */
  template <typename... Args>
  NVTX3_CONSTEXPR_IF_CPP14 explicit event_attributes(payload const& p, Args const&... args) noexcept
    : event_attributes(args...)
  {
    attributes_.payload     = p.get_value();
    attributes_.payloadType = p.get_type();
  }

  /**
   * @brief Variadic constructor where the first argument is a single `payload_data`.
   *
   * Sets the value of the `EventAttribute`s payload data based on `pd` and forwards
   * the remaining variadic parameter pack to the next constructor.
   *
   */
  template <typename... Args>
  NVTX3_CONSTEXPR_IF_CPP14 explicit event_attributes(payload_data const& pd, Args const&... args) noexcept
      : event_attributes(args...)
  {
    attributes_.payloadType = NVTX_PAYLOAD_TYPE_EXT;
    attributes_.reserved0 = 1;
    attributes_.payload.ullValue = pd.as_ull_value();
  }

  /**
   * @brief Deleted constructor for `payload_data` rvalue references.
   *
   * `event_attributes` is non-owning and therefore cannot take ownership of an r-value
   * `payload_data`. Therefore this constructor is deleted by default to prevent dangling
   * pointers.
   */
#ifndef NVTX3_ALLOW_RVALUE_CONSTRUCTORS
  template <typename... Args>
  NVTX3_CONSTEXPR_IF_CPP14 explicit event_attributes(payload_data&& pd, Args const&... args) = delete;
#endif

  /**
   * @brief Variadic constructor template for containers of `payload_data`.
   *
   * Associates multiple structured payloads contained in `pdc` with the event attributes.
   * This constructor is enabled for types that have both a `data()` member function
   * returning `payload_data*` or `const payload_data*` and a `size()` member function.
   * E.g., `std::vector` or `std::array` can be used.
   * Forwards the remaining variadic parameter pack to the next constructor.
   */
  template <
      typename T,
      typename... Args,
      typename = typename std::enable_if<
          detail::has_data_member<T, payload_data>::value && detail::has_size_member<T>::value>::type>
  NVTX3_CONSTEXPR_IF_CPP20 explicit event_attributes(T&& pdc, Args const&... args) noexcept
      : event_attributes(args...)
  {
    NVTX3_STATIC_ASSERT(
        std::is_lvalue_reference<T>::value,
        "event_attributes requires an lvalue reference to the underlying data. "
        "Constructing from an rvalue is potentially unsafe and therefore forbidden.");
    attributes_.payloadType = NVTX_PAYLOAD_TYPE_EXT;
    attributes_.reserved0 = static_cast<int32_t>(pdc.size()); // Cast size to int32_t
    attributes_.payload.ullValue = pdc.data()->as_ull_value();
  }

  /**
   * @brief Variadic constructor where the first argument is a `message`.
   *
   * Sets the value of the `EventAttribute`s message based on `m` and forwards
   * the remaining variadic parameter pack to the next constructor.
   *
   */
  template <typename... Args>
  NVTX3_CONSTEXPR_IF_CPP14 explicit event_attributes(message const& m, Args const&... args) noexcept
    : event_attributes(args...)
  {
    attributes_.message     = m.get_value();
    attributes_.messageType = m.get_type();
  }

  ~event_attributes() = default;
  event_attributes(event_attributes const&) = default;
  event_attributes& operator=(event_attributes const&) = default;
  event_attributes(event_attributes&&) = default;
  event_attributes& operator=(event_attributes&&) = default;

  /**
   * @brief Get raw pointer to underlying NVTX attributes object.
   *
   */
  constexpr value_type const* get() const noexcept { return &attributes_; }

 private:
  value_type attributes_{};  ///< The NVTX attributes structure
};

/**
 * @brief A RAII object for creating a NVTX range local to a thread within a
 * domain.
 *
 * When constructed, begins a nested NVTX range on the calling thread in the
 * specified domain. Upon destruction, ends the NVTX range.
 *
 * Behavior is undefined if a `scoped_range_in` object is
 * created/destroyed on different threads.
 *
 * `scoped_range_in` is neither movable nor copyable.
 *
 * `scoped_range_in`s may be nested within other ranges.
 *
 * The domain of the range is specified by the template type parameter `D`.
 * By default, the `domain::global` is used, which scopes the range to the
 * global NVTX domain. The convenience alias `scoped_range` is provided for
 * ranges scoped to the global domain.
 *
 * A custom domain can be defined by creating a type, `D`, with a static
 * member `D::name` whose value is used to name the domain associated with
 * `D`. `D::name` must resolve to either `char const*` or `wchar_t const*`
 *
 * Example:
 * \code{.cpp}
 * // Define a type `my_domain` with a member `name` used to name the domain
 * // associated with the type `my_domain`.
 * struct my_domain{
 *    static constexpr char const* name{"my domain"};
 * };
 * \endcode
 *
 * Usage:
 * \code{.cpp}
 * nvtx3::scoped_range_in<my_domain> r1{"range 1"}; // Range in my domain
 *
 * // Three equivalent ways to make a range in the global domain:
 * nvtx3::scoped_range_in<nvtx3::domain::global> r2{"range 2"};
 * nvtx3::scoped_range_in<> r3{"range 3"};
 * nvtx3::scoped_range r4{"range 4"};
 *
 * // Create an alias to succinctly make ranges in my domain:
 * using my_scoped_range = nvtx3::scoped_range_in<my_domain>;
 *
 * my_scoped_range r3{"range 3"};
 * \endcode
 */
template <class D = domain::global>
class NVTX3_MAYBE_UNUSED scoped_range_in {
 public:
  /**
   * @brief Construct a `scoped_range_in` with the specified
   * `event_attributes`
   *
   * Example:
   * \code{cpp}
   * nvtx3::event_attributes attr{"msg", nvtx3::rgb{127,255,0}};
   * nvtx3::scoped_range range{attr}; // Creates a range with message contents
   *                                  // "msg" and green color
   * \endcode
   *
   * @param[in] attr `event_attributes` that describes the desired attributes
   * of the range.
   */
  explicit scoped_range_in(event_attributes const& attr) noexcept
  {
#ifndef NVTX_DISABLE
    nvtxDomainRangePushEx(domain::get<D>(), attr.get());
#else
    (void)attr;
#endif
  }

  /**
   * @brief Constructs a `scoped_range_in` from the constructor arguments
   * of an `event_attributes`.
   *
   * Forwards the arguments `args...` to construct an
   * `event_attributes` object. The `event_attributes` object is then
   * associated with the `scoped_range_in`.
   *
   * For more detail, see `event_attributes` documentation.
   *
   * Example:
   * \code{cpp}
   * // Creates a range with message "message" and green color
   * nvtx3::scoped_range r{"message", nvtx3::rgb{127,255,0}};
   * \endcode
   *
   * @param[in] args Arguments to used to construct an `event_attributes` associated with this
   * range.
   *
   */
  template <typename... Args>
  explicit scoped_range_in(Args const&... args) noexcept
    : scoped_range_in{event_attributes{args...}}
  {
  }

  /**
   * @brief Default constructor creates a `scoped_range_in` with no
   * message, color, payload, nor category.
   *
   */
  scoped_range_in() noexcept : scoped_range_in{event_attributes{}} {}

  /**
   * @brief Delete `operator new` to disallow heap allocated objects.
   *
   * `scoped_range_in` must follow RAII semantics to guarantee proper push/pop semantics.
   *
   */
  void* operator new(std::size_t) = delete;

  scoped_range_in(scoped_range_in const&) = delete;
  scoped_range_in& operator=(scoped_range_in const&) = delete;
  scoped_range_in(scoped_range_in&&) = delete;
  scoped_range_in& operator=(scoped_range_in&&) = delete;

  /**
   * @brief Destroy the scoped_range_in, ending the NVTX range event.
   */
  ~scoped_range_in() noexcept
  {
#ifndef NVTX_DISABLE
    nvtxDomainRangePop(domain::get<D>());
#endif
  }
};

/**
 * @brief Alias for a `scoped_range_in` in the global NVTX domain.
 *
 */
using scoped_range = scoped_range_in<domain::global>;

/**
 * @brief Manually begin a nested thread range in a domain.
 *
 * This function begins a nested range on the calling thread in the specified
 * domain. End the range with a matching call to `pop_range_in<D>()` on the same
 * thread.
 *
 * Prefer `nvtx3::scoped_range_in` when a range can be tied to a C++ object
 * lifetime. Use this manual push/pop API for callback-driven code or other
 * control flows where the range begin and end happen in separate C++ scopes.
 *
 * @warning Manual push/pop ranges are not RAII protected. The caller is
 * responsible for ensuring each pushed range has exactly one matching pop,
 * ranges are properly nested, and the matching pop is called on the same
 * thread.
 *
 * Example:
 * \code{.cpp}
 * nvtx3::event_attributes attr{"msg", nvtx3::rgb{127,255,0}};
 * nvtx3::push_range_in<my_domain>(attr);
 * ...
 * nvtx3::pop_range_in<my_domain>();
 * \endcode
 *
 * @tparam D Type containing `name` member used to identify the `domain`
 * to which the range belongs. Else, `domain::global` to indicate that the
 * global NVTX domain should be used.
 * @param[in] attr `event_attributes` that describes the desired attributes
 * of the range.
 * @return The 0-based level of the range being started, or a negative value
 * if an error occurs.
 */
template <typename D = domain::global>
inline int push_range_in(event_attributes const& attr) noexcept
{
#ifndef NVTX_DISABLE
  return nvtxDomainRangePushEx(domain::get<D>(), attr.get());
#else
  (void)attr;
  return 0;
#endif
}

/**
 * @brief Manually begin a nested thread range in a domain.
 *
 * This overload uses `args...` to construct an `event_attributes` object to
 * associate with the range. For more detail, see `event_attributes`.
 *
 * Prefer `nvtx3::scoped_range_in` when a range can be tied to a C++ object
 * lifetime. Use this manual push/pop API for callback-driven code or other
 * control flows where the range begin and end happen in separate C++ scopes.
 *
 * @warning Manual push/pop ranges are not RAII protected. The caller is
 * responsible for ensuring each pushed range has exactly one matching pop,
 * ranges are properly nested, and the matching pop is called on the same
 * thread.
 *
 * Example:
 * \code{.cpp}
 * nvtx3::push_range_in<my_domain>("msg", nvtx3::rgb{127,255,0});
 * ...
 * nvtx3::pop_range_in<my_domain>();
 * \endcode
 *
 * @tparam D Type containing `name` member used to identify the `domain`
 * to which the range belongs. Else, `domain::global` to indicate that the
 * global NVTX domain should be used.
 * @param[in] args Variadic parameter pack of the arguments for an `event_attributes`.
 * @return The 0-based level of the range being started, or a negative value
 * if an error occurs.
 */
template <typename D = domain::global, typename... Args>
inline int push_range_in(Args const&... args) noexcept
{
#ifndef NVTX_DISABLE
  return push_range_in<D>(event_attributes{args...});
#else
  detail::silence_unused(args...);
  return 0;
#endif
}

/**
 * @brief Manually begin a nested thread range in the global domain.
 *
 * This function is equivalent to `nvtx3::push_range_in<>()` and
 * `nvtx3::push_range_in<nvtx3::domain::global>()`.
 *
 * Prefer `nvtx3::scoped_range` when a range can be tied to a C++ object
 * lifetime. Use this manual push/pop API for callback-driven code or other
 * control flows where the range begin and end happen in separate C++ scopes.
 *
 * @warning Manual push/pop ranges are not RAII protected. The caller is
 * responsible for ensuring each pushed range has exactly one matching pop,
 * ranges are properly nested, and the matching pop is called on the same
 * thread.
 *
 * @param[in] attr `event_attributes` that describes the desired attributes
 * of the range.
 * @return The 0-based level of the range being started, or a negative value
 * if an error occurs.
 */
inline int push_range(event_attributes const& attr) noexcept
{
#ifndef NVTX_DISABLE
  return push_range_in<domain::global>(attr);
#else
  (void)attr;
  return 0;
#endif
}

/**
 * @brief Manually begin a nested thread range in the global domain.
 *
 * This function is equivalent to `nvtx3::push_range_in<>()` and
 * `nvtx3::push_range_in<nvtx3::domain::global>()`.
 *
 * This overload uses `args...` to construct an `event_attributes` object to
 * associate with the range. For more detail, see `event_attributes`.
 *
 * Prefer `nvtx3::scoped_range` when a range can be tied to a C++ object
 * lifetime. Use this manual push/pop API for callback-driven code or other
 * control flows where the range begin and end happen in separate C++ scopes.
 *
 * @warning Manual push/pop ranges are not RAII protected. The caller is
 * responsible for ensuring each pushed range has exactly one matching pop,
 * ranges are properly nested, and the matching pop is called on the same
 * thread.
 *
 * @param[in] args Variadic parameter pack of the arguments for an `event_attributes`.
 * @return The 0-based level of the range being started, or a negative value
 * if an error occurs.
 */
template <typename... Args>
inline int push_range(Args const&... args) noexcept
{
#ifndef NVTX_DISABLE
  return push_range_in<domain::global>(args...);
#else
  detail::silence_unused(args...);
  return 0;
#endif
}

/**
 * @brief Manually end the current nested thread range in a domain.
 *
 * Ends the innermost range previously started by `push_range_in<D>()` on the
 * same thread.
 *
 * Prefer `nvtx3::scoped_range_in` when a range can be tied to a C++ object
 * lifetime. Use this manual push/pop API for callback-driven code or other
 * control flows where the range begin and end happen in separate C++ scopes.
 *
 * @warning Manual push/pop ranges are not RAII protected. The caller is
 * responsible for ensuring each pushed range has exactly one matching pop,
 * ranges are properly nested, and the matching pop is called on the same
 * thread.
 *
 * @tparam D Type containing `name` member used to identify the `domain`
 * to which the range belongs. Else, `domain::global` to indicate that the
 * global NVTX domain should be used.
 * @return The level of the range being ended, or a negative value if an error
 * occurs.
 */
template <typename D = domain::global>
inline int pop_range_in() noexcept
{
#ifndef NVTX_DISABLE
  return nvtxDomainRangePop(domain::get<D>());
#else
  return 0;
#endif
}

/**
 * @brief Manually end the current nested thread range in the global domain.
 *
 * This function is equivalent to `nvtx3::pop_range_in<>()` and
 * `nvtx3::pop_range_in<nvtx3::domain::global>()`.
 *
 * Prefer `nvtx3::scoped_range` when a range can be tied to a C++ object
 * lifetime. Use this manual push/pop API for callback-driven code or other
 * control flows where the range begin and end happen in separate C++ scopes.
 *
 * @warning Manual push/pop ranges are not RAII protected. The caller is
 * responsible for ensuring each pushed range has exactly one matching pop,
 * ranges are properly nested, and the matching pop is called on the same
 * thread.
 *
 * @return The level of the range being ended, or a negative value if an error
 * occurs.
 */
inline int pop_range() noexcept
{
#ifndef NVTX_DISABLE
  return pop_range_in<domain::global>();
#else
  return 0;
#endif
}

namespace detail {

/// @cond internal
template <typename D = domain::global>
class NVTX3_MAYBE_UNUSED optional_scoped_range_in
{
public:
  optional_scoped_range_in() = default;

  void begin(event_attributes const& attr) noexcept
  {
#ifndef NVTX_DISABLE
    // This class is not meant to be part of the public NVTX C++ API and should
    // only be used in the `NVTX3_FUNC_RANGE_IF` and `NVTX3_FUNC_RANGE_IF_IN`
    // macros. However, to prevent developers from misusing this class, make
    // sure to not start multiple ranges.
    if (initialized) { return; }

    nvtxDomainRangePushEx(domain::get<D>(), attr.get());
    initialized = true;
#else
    (void)attr;
#endif
  }

  ~optional_scoped_range_in() noexcept
  {
#ifndef NVTX_DISABLE
    if (initialized) { nvtxDomainRangePop(domain::get<D>()); }
#endif
  }

  void* operator new(std::size_t) = delete;
  optional_scoped_range_in(optional_scoped_range_in const&) = delete;
  optional_scoped_range_in& operator=(optional_scoped_range_in const&) = delete;
  optional_scoped_range_in(optional_scoped_range_in&&) = delete;
  optional_scoped_range_in& operator=(optional_scoped_range_in&&) = delete;

private:
#ifndef NVTX_DISABLE
  bool initialized = false;
#endif
};
/// @endcond

} // namespace detail

/**
 * @brief Handle used for correlating explicit range start and end events.
 *
 * A handle is "null" if it does not correspond to any range.
 *
 */
struct range_handle {
  /// Type used for the handle's value
  using value_type = nvtxRangeId_t;


  /**
   * @brief Construct a `range_handle` from the given id.
   *
   */
  constexpr explicit range_handle(value_type id) noexcept : _range_id{id} {}

  /**
   * @brief Constructs a null range handle.
   *
   * A null range_handle corresponds to no range. Calling `end_range` on a
   * null handle is undefined behavior when a tool is active.
   *
   */
  constexpr range_handle() noexcept = default;

  /**
   * @brief Checks whether this handle is null
   *
   * Provides contextual conversion to `bool`.
   *
   * \code{cpp}
   * range_handle handle{};
   * if (handle) {...}
   * \endcode
   *
   */
  constexpr explicit operator bool() const noexcept { return get_value() != null_range_id; }

  /**
   * @brief Implicit conversion from `nullptr` constructs a null handle.
   *
   * Satisfies the "NullablePointer" requirement to make `range_handle` comparable with `nullptr`.
   *
   */
  constexpr range_handle(std::nullptr_t) noexcept {}

  /**
   * @brief Returns the `range_handle`'s value
   *
   * @return value_type The handle's value
   */
  constexpr value_type get_value() const noexcept { return _range_id; }

 private:
  /// Sentinel value for a null handle that corresponds to no range
  static constexpr value_type null_range_id = nvtxRangeId_t{0};

  value_type _range_id{null_range_id};  ///< The underlying NVTX range id
};

/**
 * @brief Compares two range_handles for equality
 *
 * @param lhs The first range_handle to compare
 * @param rhs The second range_handle to compare
 */
inline constexpr bool operator==(range_handle lhs, range_handle rhs) noexcept
{
  return lhs.get_value() == rhs.get_value();
}

/**
 * @brief Compares two range_handles for inequality
 *
 * @param lhs The first range_handle to compare
 * @param rhs The second range_handle to compare
 */
inline constexpr bool operator!=(range_handle lhs, range_handle rhs) noexcept { return !(lhs == rhs); }

/**
 * @brief Manually begin an NVTX range.
 *
 * Explicitly begins an NVTX range and returns a unique handle. To end the
 * range, pass the handle to `end_range_in<D>()`.
 *
 * `nvtx3::start_range(...)` is equivalent to `nvtx3::start_range_in<>(...)` and
 * `nvtx3::start_range_in<nvtx3::domain::global>(...)`.
 *
 * `start_range_in/end_range_in` are the most explicit and lowest level APIs
 * provided for creating ranges.  Use of `nvtx3::unique_range_in` should be
 * preferred unless one is unable to tie the range to the lifetime of an object.
 *
 * Example:
 * \code{.cpp}
 * nvtx3::event_attributes attr{"msg", nvtx3::rgb{127,255,0}};
 * // Manually begin a range
 * nvtx3::range_handle h = nvtx3::start_range_in<my_domain>(attr);
 * ...
 * nvtx3::end_range_in<my_domain>(h); // End the range
 * \endcode
 *
 * @tparam D Type containing `name` member used to identify the `domain`
 * to which the range belongs. Else, `domain::global` to indicate that the
 * global NVTX domain should be used.
 * @param[in] attr `event_attributes` that describes the desired attributes
 * of the range.
 * @return Unique handle to be passed to `end_range_in` to end the range.
 */
template <typename D = domain::global>
NVTX3_NO_DISCARD inline range_handle start_range_in(event_attributes const& attr) noexcept
{
#ifndef NVTX_DISABLE
  return range_handle{nvtxDomainRangeStartEx(domain::get<D>(), attr.get())};
#else
  (void)attr;
  return {};
#endif
}

/**
 * @brief Manually begin an NVTX range.
 *
 * Explicitly begins an NVTX range and returns a unique handle. To end the
 * range, pass the handle to `end_range_in<D>()`.
 *
 * `nvtx3::start_range(...)` is equivalent to `nvtx3::start_range_in<>(...)` and
 * `nvtx3::start_range_in<nvtx3::domain::global>(...)`.
 *
 * `start_range_in/end_range_in` are the most explicit and lowest level APIs
 * provided for creating ranges.  Use of `nvtx3::unique_range_in` should be
 * preferred unless one is unable to tie the range to the lifetime of an object.
 *
 * This overload uses `args...` to construct an  `event_attributes` to
 * associate with the range.  For more detail, see `event_attributes`.
 *
 * Example:
 * \code{cpp}
 * // Manually begin a range
 * nvtx3::range_handle h = nvtx3::start_range_in<D>("msg", nvtx3::rgb{127,255,0});
 * ...
 * nvtx3::end_range_in<D>(h); // Ends the range
 * \endcode
 *
 * @tparam D Type containing `name` member used to identify the `domain`
 * to which the range belongs. Else, `domain::global` to indicate that the
 * global NVTX domain should be used.
 * @param[in] args Variadic parameter pack of the arguments for an `event_attributes`.
 * @return Unique handle to be passed to `end_range` to end the range.
 */
template <typename D = domain::global, typename... Args>
NVTX3_NO_DISCARD inline range_handle start_range_in(Args const&... args) noexcept
{
#ifndef NVTX_DISABLE
  return start_range_in<D>(event_attributes{args...});
#else
  detail::silence_unused(args...);
  return {};
#endif
}

/**
 * @brief Manually begin an NVTX range in the global domain.
 *
 * Explicitly begins an NVTX range and returns a unique handle. To end the
 * range, pass the handle to `end_range()`.
 *
 * `nvtx3::start_range(...)` is equivalent to `nvtx3::start_range_in<>(...)` and
 * `nvtx3::start_range_in<nvtx3::domain::global>(...)`.
 *
 * `start_range/end_range` are the most explicit and lowest level APIs
 * provided for creating ranges.  Use of `nvtx3::unique_range` should be
 * preferred unless one is unable to tie the range to the lifetime of an object.
 *
 * Example:
 * \code{.cpp}
 * nvtx3::event_attributes attr{"msg", nvtx3::rgb{127,255,0}};
 * // Manually begin a range
 * nvtx3::range_handle h = nvtx3::start_range(attr);
 * ...
 * nvtx3::end_range(h); // End the range
 * \endcode
 *
 * @param[in] attr `event_attributes` that describes the desired attributes
 * of the range.
 * @return Unique handle to be passed to `end_range_in` to end the range.
 */
NVTX3_NO_DISCARD inline range_handle start_range(event_attributes const& attr) noexcept
{
#ifndef NVTX_DISABLE
  return start_range_in<domain::global>(attr);
#else
  (void)attr;
  return {};
#endif
}

/**
 * @brief Manually begin an NVTX range in the global domain.
 *
 * Explicitly begins an NVTX range and returns a unique handle. To end the
 * range, pass the handle to `end_range_in<D>()`.
 *
 * `nvtx3::start_range(...)` is equivalent to `nvtx3::start_range_in<>(...)` and
 * `nvtx3::start_range_in<nvtx3::domain::global>(...)`.
 *
 * `start_range_in/end_range_in` are the most explicit and lowest level APIs
 * provided for creating ranges.  Use of `nvtx3::unique_range_in` should be
 * preferred unless one is unable to tie the range to the lifetime of an object.
 *
 * This overload uses `args...` to construct an  `event_attributes` to
 * associate with the range.  For more detail, see `event_attributes`.
 *
 * Example:
 * \code{cpp}
 * // Manually begin a range
 * nvtx3::range_handle h = nvtx3::start_range("msg", nvtx3::rgb{127,255,0});
 * ...
 * nvtx3::end_range(h); // Ends the range
 * \endcode
 *
 * @param[in] args Variadic parameter pack of the arguments for an `event_attributes`.
 * @return Unique handle to be passed to `end_range` to end the range.
 */
template <typename... Args>
NVTX3_NO_DISCARD inline range_handle start_range(Args const&... args) noexcept
{
#ifndef NVTX_DISABLE
  return start_range_in<domain::global>(args...);
#else
  detail::silence_unused(args...);
  return {};
#endif
}

/**
 * @brief Manually end the range associated with the handle `r` in domain `D`.
 *
 * Explicitly ends the NVTX range indicated by the handle `r` returned from a
 * prior call to `start_range_in<D>`. The range may end on a different thread
 * from where it began.
 *
 * @tparam D Type containing `name` member used to identify the `domain` to
 * which the range belongs. Else, `domain::global` to indicate that the global
 * NVTX domain should be used.
 * @param r Handle to a range started by a prior call to `start_range_in`.
 *
 * @warning The domain type specified as template parameter to this function
 * must be the same that was specified on the associated `start_range_in` call.
 */
template <typename D = domain::global>
inline void end_range_in(range_handle r) noexcept
{
#ifndef NVTX_DISABLE
  nvtxDomainRangeEnd(domain::get<D>(), r.get_value());
#else
  (void)r;
#endif
}

/**
 * @brief Manually end the range associated with the handle `r` in the global
 * domain.
 *
 * Explicitly ends the NVTX range indicated by the handle `r` returned from a
 * prior call to `start_range`. The range may end on a different thread from
 * where it began.
 *
 * @param r Handle to a range started by a prior call to `start_range`.
 *
 * @warning The domain type specified as template parameter to this function
 * must be the same that was specified on the associated `start_range` call.
 */
inline void end_range(range_handle r) noexcept
{
#ifndef NVTX_DISABLE
  end_range_in<domain::global>(r);
#else
  (void)r;
#endif
}

/**
 * @brief A RAII object for creating a NVTX range within a domain that can
 * be created and destroyed on different threads.
 *
 * When constructed, begins a NVTX range in the specified domain. Upon
 * destruction, ends the NVTX range.
 *
 * Similar to `nvtx3::scoped_range_in`, with a few key differences:
 * - `unique_range` objects can be destroyed in an order whereas `scoped_range` objects must be
 *    destroyed in exact reverse creation order
 * - `unique_range` can start and end on different threads
 * - `unique_range` is movable
 * - `unique_range` objects can be constructed as heap objects
 *
 * There is extra overhead associated with `unique_range` constructs and therefore use of
 * `nvtx3::scoped_range_in` should be preferred.
 *
 * @tparam D Type containing `name` member used to identify the `domain`
 * to which the `unique_range_in` belongs. Else, `domain::global` to
 * indicate that the global NVTX domain should be used.
 */
template <typename D = domain::global>
class NVTX3_MAYBE_UNUSED unique_range_in {
 public:
  /**
   * @brief Construct a new unique_range_in object with the specified event attributes
   *
   * Example:
   * \code{cpp}
   * nvtx3::event_attributes attr{"msg", nvtx3::rgb{127,255,0}};
   * nvtx3::unique_range_in<my_domain> range{attr}; // Creates a range with message contents
   *                                            // "msg" and green color
   * \endcode
   *
   * @param[in] attr `event_attributes` that describes the desired attributes
   * of the range.
   */
  explicit unique_range_in(event_attributes const& attr) noexcept
    : handle_{start_range_in<D>(attr)}
  {
  }

  /**
   * @brief Constructs a `unique_range_in` from the constructor arguments
   * of an `event_attributes`.
   *
   * Forwards the arguments `args...` to construct an
   * `event_attributes` object. The `event_attributes` object is then
   * associated with the `unique_range_in`.
   *
   * For more detail, see `event_attributes` documentation.
   *
   * Example:
   * \code{.cpp}
   * // Creates a range with message "message" and green color
   * nvtx3::unique_range_in<> r{"message", nvtx3::rgb{127,255,0}};
   * \endcode
   *
   * @param[in] args Variadic parameter pack of arguments to construct an `event_attributes`
   * associated with this range.
   */
  template <typename... Args>
  explicit unique_range_in(Args const&... args) noexcept
    : unique_range_in{event_attributes{args...}}
  {
  }

  /**
   * @brief Default constructor creates a `unique_range_in` with no
   * message, color, payload, nor category.
   *
   */
  constexpr unique_range_in() noexcept : unique_range_in{event_attributes{}} {}

  /**
   * @brief Destroy the `unique_range_in` ending the range.
   *
   */
  ~unique_range_in() noexcept = default;

  /**
   * @brief Move constructor allows taking ownership of the NVTX range from
   * another `unique_range_in`.
   *
   * @param other The range to take ownership of
   */
  unique_range_in(unique_range_in&& other) noexcept = default;

  /**
   * @brief Move assignment operator allows taking ownership of an NVTX range
   * from another `unique_range_in`.
   *
   * @param other The range to take ownership of
   */
  unique_range_in& operator=(unique_range_in&& other) noexcept = default;

  /// Copy construction is not allowed to prevent multiple objects from owning
  /// the same range handle
  unique_range_in(unique_range_in const&) = delete;

  /// Copy assignment is not allowed to prevent multiple objects from owning the
  /// same range handle
  unique_range_in& operator=(unique_range_in const&) = delete;

 private:

  struct end_range_handle {
    using pointer = range_handle;  /// Override the pointer type of the unique_ptr
    void operator()(range_handle h) const noexcept { end_range_in<D>(h); }
  };

  /// Range handle used to correlate the start/end of the range
  std::unique_ptr<range_handle, end_range_handle> handle_;
};

/**
 * @brief Alias for a `unique_range_in` in the global NVTX domain.
 *
 */
using unique_range = unique_range_in<domain::global>;

/**
 * @brief Annotates an instantaneous point in time with a "marker", using the
 * attributes specified by `attr`.
 *
 * Unlike a "range" which has a beginning and an end, a marker is a single event
 * in an application, such as detecting a problem:
 *
 * \code{.cpp}
 * bool success = do_operation(...);
 * if (!success) {
 *    nvtx3::event_attributes attr{"operation failed!", nvtx3::rgb{255,0,0}};
 *    nvtx3::mark_in<my_domain>(attr);
 * }
 * \endcode
 *
 * Note that nvtx3::mark_in<D> is a function, not a class like scoped_range_in<D>.
 *
 * @tparam D Type containing `name` member used to identify the `domain`
 * to which the `unique_range_in` belongs. Else, `domain::global` to
 * indicate that the global NVTX domain should be used.
 * @param[in] attr `event_attributes` that describes the desired attributes
 * of the mark.
 */
template <typename D = domain::global>
inline void mark_in(event_attributes const& attr) noexcept
{
#ifndef NVTX_DISABLE
  nvtxDomainMarkEx(domain::get<D>(), attr.get());
#else
  (void)(attr);
#endif
}

/**
 * @brief Annotates an instantaneous point in time with a "marker", using the
 * arguments to construct an `event_attributes`.
 *
 * Unlike a "range" which has a beginning and an end, a marker is a single event
 * in an application, such as detecting a problem:
 *
 * \code{.cpp}
 * bool success = do_operation(...);
 * if (!success) {
 *    nvtx3::mark_in<my_domain>("operation failed!", nvtx3::rgb{255,0,0});
 * }
 * \endcode
 *
 * Note that nvtx3::mark_in<D> is a function, not a class like scoped_range_in<D>.
 *
 * Forwards the arguments `args...` to construct an `event_attributes` object.
 * The attributes are then associated with the marker. For more detail, see
 * the `event_attributes` documentation.
 *
 * @tparam D Type containing `name` member used to identify the `domain`
 * to which the `unique_range_in` belongs. Else `domain::global` to
 * indicate that the global NVTX domain should be used.
 * @param[in] args Variadic parameter pack of arguments to construct an `event_attributes`
 * associated with this range.
 *
 */
template <typename D = domain::global, typename... Args>
inline void mark_in(Args const&... args) noexcept
{
#ifndef NVTX_DISABLE
  mark_in<D>(event_attributes{args...});
#else
  detail::silence_unused(args...);
#endif
}

/**
 * @brief Annotates an instantaneous point in time with a "marker", using the
 * attributes specified by `attr`, in the global domain.
 *
 * Unlike a "range" which has a beginning and an end, a marker is a single event
 * in an application, such as detecting a problem:
 *
 * \code{.cpp}
 * bool success = do_operation(...);
 * if (!success) {
 *    nvtx3::event_attributes attr{"operation failed!", nvtx3::rgb{255,0,0}};
 *    nvtx3::mark(attr);
 * }
 * \endcode
 *
 * Note that nvtx3::mark is a function, not a class like scoped_range.
 *
 * @param[in] attr `event_attributes` that describes the desired attributes
 * of the mark.
 */
inline void mark(event_attributes const& attr) noexcept
{
#ifndef NVTX_DISABLE
  mark_in<domain::global>(attr);
#else
  (void)attr;
#endif
}

/**
 * @brief Annotates an instantaneous point in time with a "marker", using the
 * arguments to construct an `event_attributes`, in the global domain.
 *
 * Unlike a "range" which has a beginning and an end, a marker is a single event
 * in an application, such as detecting a problem:
 *
 * \code{.cpp}
 * bool success = do_operation(...);
 * if (!success) {
 *    nvtx3::mark("operation failed!", nvtx3::rgb{255,0,0});
 * }
 * \endcode
 *
 * Note that nvtx3::mark is a function, not a class like scoped_range.
 *
 * Forwards the arguments `args...` to construct an `event_attributes` object.
 * The attributes are then associated with the marker. For more detail, see
 * the `event_attributes` documentation.
 *
 * @param[in] args Variadic parameter pack of arguments to construct an
 * `event_attributes` associated with this range.
 *
 */
template <typename... Args>
inline void mark(Args const&... args) noexcept
{
#ifndef NVTX_DISABLE
  mark_in<domain::global>(args...);
#else
  detail::silence_unused(args...);
#endif
}

/* ===== Counter Semantics API ============================================== */

/**
 * @brief Type-safe wrapper for NVTX scope identifiers.
 *
 * Scopes define the context in which a counter is meaningful, such as
 * process-wide or thread-local.
 *
 * Predefined scopes are available as static member functions. Custom scopes
 * can be created using explicit construction from a raw scope ID (C interop)
 * or in future versions via `nvtxScopeRegister`.
 *
 * Example:
 * \code{.cpp}
 * // Use predefined scopes
 * nvtx3::counter<int64_t> c1{"counter", nvtx3::scope::current_sw_process()};
 *
 * // C interop: wrap a raw scope ID
 * uint64_t raw_id = some_c_api_returning_scope();
 * nvtx3::scope custom{raw_id};
 * nvtx3::counter<int64_t> c2{"counter", custom};
 * \endcode
 */
class scope {
public:
  using value_type = uint64_t;

  /**
   * @brief Explicit constructor from a raw scope ID.
   *
   * Use this for C API interoperability when you have a scope ID
   * from `nvtxScopeRegister` or similar.
   *
   * @param id The raw scope ID.
   */
  constexpr explicit scope(value_type id) noexcept : id_{id} {}

  /**
   * @brief Get the underlying scope ID.
   * @return The raw scope ID for C API interop.
   */
  constexpr value_type get() const noexcept { return id_; }

  /** @brief No scope specified. */
  static constexpr scope none() noexcept { return scope{NVTX_SCOPE_NONE}; }
  /** @brief The root in a hierarchy. */
  static constexpr scope root() noexcept { return scope{NVTX_SCOPE_ROOT}; }

  /* Hardware events */
  /** @brief Node/machine name */
  static constexpr scope current_hw_machine() noexcept { return scope{NVTX_SCOPE_CURRENT_HW_MACHINE}; }
  static constexpr scope current_hw_socket() noexcept { return scope{NVTX_SCOPE_CURRENT_HW_SOCKET}; }
  /** @brief Physical CPU core */
  static constexpr scope current_hw_cpu_physical() noexcept { return scope{NVTX_SCOPE_CURRENT_HW_CPU_PHYSICAL}; }
  /** @brief Logical CPU core */
  static constexpr scope current_hw_cpu_logical() noexcept { return scope{NVTX_SCOPE_CURRENT_HW_CPU_LOGICAL}; }
  /** @brief Innermost HW execution context */
  static constexpr scope current_hw_innermost() noexcept { return scope{NVTX_SCOPE_CURRENT_HW_INNERMOST}; }

  /* Virtualized hardware, virtual machines */
  static constexpr scope current_hypervisor() noexcept { return scope{NVTX_SCOPE_CURRENT_HYPERVISOR}; }
  static constexpr scope current_vm() noexcept { return scope{NVTX_SCOPE_CURRENT_VM}; }
  static constexpr scope current_kernel() noexcept { return scope{NVTX_SCOPE_CURRENT_KERNEL}; }
  static constexpr scope current_container() noexcept { return scope{NVTX_SCOPE_CURRENT_CONTAINER}; }
  static constexpr scope current_os() noexcept { return scope{NVTX_SCOPE_CURRENT_OS}; }

  /* Software scopes */
  /** @brief Process scope */
  static constexpr scope current_sw_process() noexcept { return scope{NVTX_SCOPE_CURRENT_SW_PROCESS}; }
  /** @brief Thread scope */
  static constexpr scope current_sw_thread() noexcept { return scope{NVTX_SCOPE_CURRENT_SW_THREAD}; }
  /** @brief Innermost SW execution context */
  static constexpr scope current_sw_innermost() noexcept { return scope{NVTX_SCOPE_CURRENT_SW_INNERMOST}; }

private:
  value_type id_;
};

namespace detail {

// scope_semantic::scope is a builder method whose name hides the scope type
// during member lookup. This version-local alias avoids spelling the fully
// qualified type name, ::nvtx3::NVTX3_VERSION_NAMESPACE::scope, and depending
// on the optional nvtx3::scope name, which is absent when
// NVTX3_CPP_REQUIRE_EXPLICIT_VERSION is defined.
using scope_type = scope;

} // namespace detail

/**
 * @brief Reasons for sampling a counter without a value.
 *
 * Used with `counter_in::sample_no_value()` to indicate why a sample
 * has no associated value.
 */
enum class no_value_reason : uint8_t {
  zero        = NVTX_COUNTER_SAMPLE_ZERO,
  unchanged   = NVTX_COUNTER_SAMPLE_UNCHANGED,
  unavailable = NVTX_COUNTER_SAMPLE_UNAVAILABLE
};

/**
 * @brief Builder class for configuring counter semantics.
 *
 * Counter semantics provide additional metadata about counter values,
 * such as units, limits, interpolation behavior, and value types. This
 * information helps tools to properly display and interpret counter data.
 *
 * Settings are always accumulated as flags, never reset.
 * Do not mix overlapping settings.
 *
 * Setters return `*this` to allow method chaining.
 *
 * Example:
 * \code{.cpp}
 * nvtx3::counter_semantic sem;
 * sem.unit("bytes")
 *    .limits<int64_t>(0, 1024LL * 1024 * 1024)
 *    .interpolation_since_last();
 *
 * nvtx3::counter<int64_t> memory{"heap_size",
 *                                "Process heap size",
 *                                nvtx3::scope::current_sw_process(),
 *                                sem};
 * \endcode
 */
class counter_semantic : public detail::semantic_base<nvtxSemanticsCounter_t> {
public:
  /**
   * @brief Construct an unchained counter semantic.
   */
  counter_semantic() noexcept
    : detail::semantic_base<nvtxSemanticsCounter_t>{
        {{sizeof(nvtxSemanticsCounter_t), NVTX_SEMANTIC_ID_COUNTERS_V1,
          NVTX_COUNTER_SEMANTIC_VERSION, nullptr},
         NVTX_COUNTER_FLAGS_NONE,
         nullptr,
         1,
         1,
         NVTX_COUNTER_LIMIT_UNDEFINED,
         {0},
         {0}}}
  {
  }

  // Raw C-header chaining is non-owning. Prefer the semantic-wrapper overload.
  /**
   * @brief Construct a counter semantic chained to another semantic header.
   *
   * @param next Non-owning pointer to the next semantic in the chain (nullptr if none).
   */
  explicit counter_semantic(nvtxSemanticsHeader_t const* next)
    : counter_semantic{}
  {
    set_next(next);
  }

  /**
   * @brief Construct a counter semantic that chains to another semantic builder.
   *
   * Convenience overload of the header-pointer constructor that accepts a
   * sibling semantic builder object directly. The referenced builder is copied
   * into this object.
   *
   * @tparam OtherDataType Underlying C struct type of the other semantic.
   * @param next The semantic builder to chain after this one.
   */
  template <typename OtherDataType>
  explicit counter_semantic(detail::semantic_base<OtherDataType> const& next)
    : counter_semantic{}
  {
    own_next(next);
  }

  /**
   * @brief Set the unit string for the counter (e.g., "bytes", "ms", "%").
   * @param unit_name Unit string (must remain valid for the lifetime of this object).
   * @return Reference to this object for chaining.
   */
  counter_semantic& unit(const char* unit_name) noexcept
  {
    data_.unit = unit_name;
    return *this;
  }

  /**
   * @brief Set the unit string for the counter (e.g., "bytes", "ms", "%").
   * @param unit_name Unit string (must remain valid for the lifetime of this object).
   * @return Reference to this object for chaining.
   */
  counter_semantic& unit(std::string const& unit_name) noexcept
  {
    return unit(unit_name.c_str());
  }

  /**
   * @brief Disallow setting the borrowed unit string from a temporary string.
   */
  counter_semantic& unit(std::string&& unit_name) = delete;

  /**
   * @brief Set the unit scale as a fraction (numerator/denominator).
   *
   * @param numerator Scale numerator (should be 1 if not used).
   * @param denominator Scale denominator (should be 1 if not used).
   * @return Reference to this object for chaining.
   */
  counter_semantic& unit_scale(uint64_t numerator, uint64_t denominator = 1) noexcept
  {
    data_.unitScaleNumerator = numerator;
    data_.unitScaleDenominator = denominator;
    return *this;
  }

  /**
   * @brief Enable normalization of counter values.
   *
   * Convert the fixed point value to a normalized floating point.
   * Use the sign/unsign from the underlying type this flag is applied to.
   * Unsigned [0f : 1f] or signed [-1f : 1f]
   *
   * @return Reference to this object for chaining.
   */
  counter_semantic& normalize() noexcept
  {
    data_.flags |= NVTX_COUNTER_FLAG_NORMALIZE;
    return *this;
  }

  /**
   * @brief Set value type to absolute.
   *
   * @throws std::logic_error if any valuetype_* setter has already been
   *         called on this object.
   * @return Reference to this object for chaining.
   */
  counter_semantic& valuetype_absolute()
  {
    set_enum_group_flag(NVTX_COUNTER_FLAG_VALUETYPES,
                        NVTX_COUNTER_FLAG_VALUETYPE_ABSOLUTE, "valuetype");
    return *this;
  }

  /**
   * @brief Set value type to delta from previous sample.
   *
   * @throws std::logic_error if any valuetype_* setter has already been
   *         called on this object.
   * @return Reference to this object for chaining.
   */
  counter_semantic& valuetype_delta()
  {
    set_enum_group_flag(NVTX_COUNTER_FLAG_VALUETYPES,
                        NVTX_COUNTER_FLAG_VALUETYPE_DELTA, "valuetype");
    return *this;
  }

  /**
   * @brief Set value type to delta since start.
   *
   * @throws std::logic_error if any valuetype_* setter has already been
   *         called on this object.
   * @return Reference to this object for chaining.
   */
  counter_semantic& valuetype_delta_since_start()
  {
    set_enum_group_flag(NVTX_COUNTER_FLAG_VALUETYPES,
                        NVTX_COUNTER_FLAG_VALUETYPE_DELTA_SINCE_START, "valuetype");
    return *this;
  }

  /**
   * @brief Set interpolation to point (no interpolation between samples).
   *
   * @throws std::logic_error if any interpolation_* setter has already
   *         been called on this object.
   * @return Reference to this object for chaining.
   */
  counter_semantic& interpolation_point()
  {
    set_enum_group_flag(NVTX_COUNTER_FLAG_INTERPOLATIONS,
                        NVTX_COUNTER_FLAG_INTERPOLATION_POINT, "interpolation");
    return *this;
  }

  /**
   * @brief Set interpolation to piecewise constant from last sample.
   *
   * @throws std::logic_error if any interpolation_* setter has already
   *         been called on this object.
   * @return Reference to this object for chaining.
   */
  counter_semantic& interpolation_since_last()
  {
    set_enum_group_flag(NVTX_COUNTER_FLAG_INTERPOLATIONS,
                        NVTX_COUNTER_FLAG_INTERPOLATION_SINCE_LAST, "interpolation");
    return *this;
  }

  /**
   * @brief Set interpolation to piecewise constant until next sample.
   *
   * @throws std::logic_error if any interpolation_* setter has already
   *         been called on this object.
   * @return Reference to this object for chaining.
   */
  counter_semantic& interpolation_until_next()
  {
    set_enum_group_flag(NVTX_COUNTER_FLAG_INTERPOLATIONS,
                        NVTX_COUNTER_FLAG_INTERPOLATION_UNTIL_NEXT, "interpolation");
    return *this;
  }

  /**
   * @brief Set interpolation to linear between samples.
   *
   * @throws std::logic_error if any interpolation_* setter has already
   *         been called on this object.
   * @return Reference to this object for chaining.
   */
  counter_semantic& interpolation_linear()
  {
    set_enum_group_flag(NVTX_COUNTER_FLAG_INTERPOLATIONS,
                        NVTX_COUNTER_FLAG_INTERPOLATION_LINEAR, "interpolation");
    return *this;
  }

  /**
   * @brief Set minimum and maximum limits, typed.
   *
   * @tparam T One of `int64_t`, `uint64_t`, or `double`; selects the union
   *           member in the underlying nvtxSemanticsCounter_v1 struct and
   *           the corresponding NVTX_COUNTER_LIMIT_* tag stored in
   *           `limitType`.
   * @param min_val Minimum limit.
   * @param max_val Maximum limit.
   * @return Reference to this object for chaining.
   *
   * @note If limit_min<T1>() and limit_max<T2>() are called with different
   *       T the last call wins on `limitType`, and the earlier write lives
   *       in the wrong union member. Prefer this overload of limits() over
   *       paired limit_min/limit_max when you want both bounds.
   */
  template <typename T>
  counter_semantic& limits(T min_val, T max_val) noexcept
  {
    using traits = detail::counter_limit_traits<T>;
    data_.limitType = traits::limit_type_value;
    data_.flags |= NVTX_COUNTER_FLAG_LIMITS;
    traits::store(data_.min, min_val);
    traits::store(data_.max, max_val);
    return *this;
  }

  /**
   * @brief Set only the minimum limit, typed.
   * @tparam T One of `int64_t`, `uint64_t`, or `double`.
   * @param min_val Minimum limit.
   * @return Reference to this object for chaining.
   */
  template <typename T>
  counter_semantic& limit_min(T min_val) noexcept
  {
    using traits = detail::counter_limit_traits<T>;
    data_.limitType = traits::limit_type_value;
    data_.flags |= NVTX_COUNTER_FLAG_LIMIT_MIN;
    traits::store(data_.min, min_val);
    return *this;
  }

  /**
   * @brief Set only the maximum limit, typed.
   * @tparam T One of `int64_t`, `uint64_t`, or `double`.
   * @param max_val Maximum limit.
   * @return Reference to this object for chaining.
   */
  template <typename T>
  counter_semantic& limit_max(T max_val) noexcept
  {
    using traits = detail::counter_limit_traits<T>;
    data_.limitType = traits::limit_type_value;
    data_.flags |= NVTX_COUNTER_FLAG_LIMIT_MAX;
    traits::store(data_.max, max_val);
    return *this;
  }

private:
  void set_enum_group_flag(uint64_t group_mask, uint64_t value, char const* group_name)
  {
    if ((data_.flags & group_mask) != 0) {
      throw std::logic_error(std::string("counter_semantic: conflicting ") + group_name
                             + " setter; the " + group_name + " can be set only once");
    }
    data_.flags |= value;
  }
};

/**
 * @brief Builder for the scope semantic applied to a payload entry.
 *
 * Specifies the NVTX scope that applies to a particular value within a
 * payload (for example, the scope of a counter or a timestamp). The
 * scope must be known at schema registration time.
 */
class scope_semantic : public detail::semantic_base<nvtxSemanticsScope_t> {
public:
  /**
   * @brief Construct an unchained scope semantic.
   */
  scope_semantic() noexcept
    : detail::semantic_base<nvtxSemanticsScope_t>{
        {{sizeof(nvtxSemanticsScope_t), NVTX_SEMANTIC_ID_SCOPE_V1,
          NVTX_SCOPE_SEMANTIC_VERSION, nullptr},
         NVTX_SCOPE_NONE}}
  {
  }

  // Raw C-header chaining is non-owning. Prefer the semantic-wrapper overload.
  /**
   * @brief Construct a scope semantic chained to another semantic header.
   *
   * @param next Non-owning pointer to the next semantic in the chain, or nullptr.
   */
  explicit scope_semantic(nvtxSemanticsHeader_t const* next)
    : scope_semantic{}
  {
    set_next(next);
  }

  /**
   * @brief Construct a scope semantic that chains to another semantic builder.
   *
   * Convenience overload of the header-pointer constructor that accepts a
   * sibling semantic builder object directly. The referenced builder is copied
   * into this object.
   *
   * @tparam OtherDataType Underlying C struct type of the other semantic.
   * @param next The semantic builder to chain after this one.
   */
  template <typename OtherDataType>
  explicit scope_semantic(detail::semantic_base<OtherDataType> const& next)
    : scope_semantic{}
  {
    own_next(next);
  }

  /**
   * @brief Set the scope by raw NVTX scope identifier.
   * @param scope_id One of the NVTX_SCOPE_* values or a tool-defined scope.
   * @return Reference to this object for chaining.
   */
  scope_semantic& scope(uint64_t scope_id) noexcept
  {
    data_.scopeId = scope_id;
    return *this;
  }

  /**
   * @brief Set the scope by \c nvtx3::scope identifier.
   * @param s Scope identifier (wraps an NVTX_SCOPE_* value).
   * @return Reference to this object for chaining.
   */
  scope_semantic& scope(detail::scope_type s) noexcept
  {
    data_.scopeId = s.get();
    return *this;
  }
};

/**
 * @brief A domain-scoped handle to a user-registered NVTX scope.
 *
 * Calls \c nvtxScopeRegister at construction to create a scope identified
 * by a path, relative to an optional parent scope. The resulting scope
 * ID can be used anywhere an NVTX scope is expected and, in particular,
 * can be handed to \c scope_semantic::scope() through the implicit
 * conversion to \c nvtx3::scope.
 *
 * @tparam D NVTX domain (defaults to \c domain::global).
 */
template <typename D = domain::global>
class scope_in {
public:
  /**
   * @brief Register a scope in the given domain.
   *
   * @param path Path delimited by '/' characters, relative to \p parent.
   *             See \c nvtxScopeAttr_t for the full syntax. `nullptr` and
   *             `""` are treated equivalently.
   * @param parent Parent scope. Defaults to \c scope::none() (which the
   *               tool treats as root).
   * @param static_id Optional static scope ID, which must be in
   *                  [\c NVTX_SCOPE_ID_STATIC_START, \c NVTX_SCOPE_ID_DYNAMIC_START).
   *                  Defaults to \c NVTX_SCOPE_NONE, which lets the tool
   *                  assign a dynamic ID.
   */
  explicit scope_in(char const* path,
                    detail::scope_type parent = detail::scope_type::none(),
                    uint64_t static_id = NVTX_SCOPE_NONE) noexcept
  {
#ifndef NVTX_DISABLE
    nvtxScopeAttr_t attr{};
    attr.structSize = sizeof(nvtxScopeAttr_t);
    attr.path = path;
    attr.parentScope = parent.get();
    attr.scopeId = static_id;
    id_ = nvtxScopeRegister(domain::get<D>(), &attr);
#else
    (void)path;
    (void)parent;
    (void)static_id;
    id_ = NVTX_SCOPE_NONE;
#endif
  }

  /**
   * @brief Register a scope in the given domain from the specified path string.
   *
   * @param path Path delimited by '/' characters, relative to \p parent.
   *             See \c nvtxScopeAttr_t for the full syntax. `""` is treated
   *             equivalently to `nullptr`.
   * @param parent Parent scope. Defaults to \c scope::none() (which the
   *               tool treats as root).
   * @param static_id Optional static scope ID, which must be in
   *                  [\c NVTX_SCOPE_ID_STATIC_START, \c NVTX_SCOPE_ID_DYNAMIC_START).
   *                  Defaults to \c NVTX_SCOPE_NONE, which lets the tool
   *                  assign a dynamic ID.
   */
  explicit scope_in(std::string const& path,
                    detail::scope_type parent = detail::scope_type::none(),
                    uint64_t static_id = NVTX_SCOPE_NONE) noexcept
    : scope_in{path.c_str(), parent, static_id} {}

  /** @brief The registered scope ID (\c NVTX_SCOPE_NONE on failure). */
  uint64_t id() const noexcept { return id_; }

  /** @brief Convert to an \c nvtx3::scope for use with scope_semantic. */
  operator detail::scope_type() const noexcept { return detail::scope_type{id_}; }

private:
  uint64_t id_;
};

/**
 * @brief Builder for the time semantic applied to a payload entry.
 *
 * Specifies the time domain of a timestamp payload entry. The domain
 * is either a user-registered identifier or one of the predefined
 * \c NVTX_TIMESTAMP_TYPE_* constants.
 */
class time_semantic : public detail::semantic_base<nvtxSemanticsTime_t> {
public:
  /**
   * @brief Construct an unchained time semantic.
   */
  time_semantic() noexcept
    : detail::semantic_base<nvtxSemanticsTime_t>{
        {{sizeof(nvtxSemanticsTime_t), NVTX_SEMANTIC_ID_TIME_V1,
          NVTX_TIME_SEMANTIC_VERSION, nullptr},
         0}}
  {
  }

  // Raw C-header chaining is non-owning. Prefer the semantic-wrapper overload.
  /**
   * @brief Construct a time semantic chained to another semantic header.
   *
   * @param next Non-owning pointer to the next semantic in the chain, or nullptr.
   */
  explicit time_semantic(nvtxSemanticsHeader_t const* next)
    : time_semantic{}
  {
    set_next(next);
  }

  /**
   * @brief Construct a time semantic that chains to another semantic builder.
   *
   * Convenience overload of the header-pointer constructor that accepts a
   * sibling semantic builder object directly. The referenced builder is copied
   * into this object.
   *
   * @tparam OtherDataType Underlying C struct type of the other semantic.
   * @param next The semantic builder to chain after this one.
   */
  template <typename OtherDataType>
  explicit time_semantic(detail::semantic_base<OtherDataType> const& next)
    : time_semantic{}
  {
    own_next(next);
  }

  /**
   * @brief Set the time domain by identifier.
   * @param domain_id A tool-registered time domain ID or a predefined
   *                  \c NVTX_TIMESTAMP_TYPE_* value.
   * @return Reference to this object for chaining.
   */
  time_semantic& time_domain(uint64_t domain_id) noexcept
  {
    data_.timeDomainId = domain_id;
    return *this;
  }
};

/**
 * @brief Read a timestamp from the NVTX handler (wraps \c nvtxTimestampGet).
 *
 * If no tool is attached, the returned value is tool-defined (often a
 * CPU TSC read). The timestamp is only meaningful alongside a registered
 * timer source or time domain that tells the tool how to interpret it.
 *
 * @return The current NVTX timestamp.
 */
inline int64_t timestamp() noexcept
{
#ifndef NVTX_DISABLE
  return nvtxTimestampGet();
#else
  return 0;
#endif
}

/**
 * @brief A domain-scoped handle to a user-registered NVTX time domain.
 *
 * Wraps \c nvtxTimeDomainRegister and the follow-up APIs that describe
 * timer sources and report time synchronization between domains. Once
 * constructed, the cached time domain ID can be passed to
 * \c time_semantic::time_domain() or used directly with the C API.
 *
 * @tparam D NVTX domain (defaults to \c domain::global).
 */
template <typename D = domain::global>
class time_domain_in {
public:
  /**
   * @brief Register a time domain in the given NVTX domain.
   *
   * @param timestamp_type_id A predefined \c NVTX_TIMESTAMP_TYPE_* value or 0
   *                          for tool-defined.
   * @param s Scope in which the timestamps apply.
   * @param timer_flags \c NVTX_TIMER_FLAG_* bits describing timer properties.
   * @param timer_resolution Ticks per second (0 means unknown).
   * @param timer_start One of \c NVTX_TIMER_START_*.
   * @param static_id Optional static time domain ID, which must be in
   *                  [\c NVTX_TIME_DOMAIN_ID_STATIC_START,
   *                  \c NVTX_TIME_DOMAIN_ID_DYNAMIC_START). Defaults to 0,
   *                  which lets the tool assign the ID.
   */
  explicit time_domain_in(uint64_t timestamp_type_id,
                          detail::scope_type s = detail::scope_type::none(),
                          uint64_t timer_flags = NVTX_TIMER_FLAG_NONE,
                          int64_t timer_resolution = 0,
                          uint64_t timer_start = NVTX_TIMER_START_UNKNOWN,
                          uint64_t static_id = 0) noexcept
  {
#ifndef NVTX_DISABLE
    nvtxTimeDomainAttr_t attr{};
    attr.scopeId = s.get();
    attr.timestampTypeId = timestamp_type_id;
    attr.timeDomainId = static_id;
    attr.timerFlags = timer_flags;
    attr.timerResolution = timer_resolution;
    attr.timerStart = timer_start;
    id_ = nvtxTimeDomainRegister(domain::get<D>(), &attr);
#else
    (void)timestamp_type_id;
    (void)s;
    (void)timer_flags;
    (void)timer_resolution;
    (void)timer_start;
    (void)static_id;
    id_ = 0;
#endif
  }

  /** @brief The registered time domain ID. */
  uint64_t id() const noexcept { return id_; }

  /**
   * @brief Describe the timer source for this time domain.
   *
   * Wraps \c nvtxTimerSource.
   *
   * @param flags Provider flags (e.g. whether the function is safe to call
   *              after process teardown).
   * @param provider Pointer to a function that returns a timestamp.
   */
  void set_timer_source(uint64_t flags, int64_t (*provider)()) noexcept
  {
#ifndef NVTX_DISABLE
    nvtxTimerSource(domain::get<D>(), id_, flags, provider);
#else
    (void)flags;
    (void)provider;
#endif
  }

  /**
   * @brief Describe the timer source with a user data pointer.
   *
   * Wraps \c nvtxTimerSourceWithData.
   *
   * @param flags Provider flags.
   * @param provider Pointer to a function that returns a timestamp given
   *                 \p data.
   * @param data Opaque pointer passed through to \p provider.
   */
  void set_timer_source(uint64_t flags,
                        int64_t (*provider)(void*),
                        void* data) noexcept
  {
#ifndef NVTX_DISABLE
    nvtxTimerSourceWithData(domain::get<D>(), id_, flags, provider, data);
#else
    (void)flags;
    (void)provider;
    (void)data;
#endif
  }

  /**
   * @brief Report one synchronization point to another time domain.
   *
   * Wraps \c nvtxTimeSyncPoint.
   *
   * @param other_id Target time domain ID (a registered ID or a predefined
   *                 \c NVTX_TIMESTAMP_TYPE_* when unambiguous).
   * @param ts_self Timestamp in this time domain.
   * @param ts_other Timestamp in the other time domain.
   */
  void sync_point(uint64_t other_id, int64_t ts_self, int64_t ts_other) noexcept
  {
#ifndef NVTX_DISABLE
    nvtxTimeSyncPoint(domain::get<D>(), id_, other_id, ts_self, ts_other);
#else
    (void)other_id;
    (void)ts_self;
    (void)ts_other;
#endif
  }

  /**
   * @brief Report a table of synchronization points to another time domain.
   *
   * Wraps \c nvtxTimeSyncPointTable.
   *
   * @param other_id Target time domain ID.
   * @param points Array of \c nvtxSyncPoint_t pairs.
   * @param count Number of entries in \p points.
   */
  void sync_point_table(uint64_t other_id,
                        nvtxSyncPoint_t const* points,
                        size_t count) noexcept
  {
#ifndef NVTX_DISABLE
    nvtxTimeSyncPointTable(domain::get<D>(), id_, other_id, points, count);
#else
    (void)other_id;
    (void)points;
    (void)count;
#endif
  }

  /**
   * @brief Report a conversion factor from this domain to another.
   *
   * Wraps \c nvtxTimestampConversionFactor.
   *
   * @param other_id Target time domain ID.
   * @param slope Conversion factor (other-ticks / this-ticks).
   * @param ts_self Reference timestamp in this time domain.
   * @param ts_other Reference timestamp in the other time domain.
   */
  void conversion_factor(uint64_t other_id,
                         double slope,
                         int64_t ts_self,
                         int64_t ts_other) noexcept
  {
#ifndef NVTX_DISABLE
    nvtxTimestampConversionFactor(domain::get<D>(), id_, other_id, slope, ts_self, ts_other);
#else
    (void)other_id;
    (void)slope;
    (void)ts_self;
    (void)ts_other;
#endif
  }

private:
  uint64_t id_;
};

/**
 * @brief Builder for the correlation semantic applied to a payload entry.
 *
 * Marks an entry as the identifier of a correlation domain and specifies
 * how the event relates to other events in that domain. The UUID must be
 * globally unique across NVTX domains; the optional display name is
 * copied by the tool at schema registration time.
 */
class correlation_semantic : public detail::semantic_base<nvtxSemanticsCorrelation_t> {
public:
  /**
   * @brief Construct an unchained correlation semantic.
   */
  correlation_semantic() noexcept
    : detail::semantic_base<nvtxSemanticsCorrelation_t>{
        {{sizeof(nvtxSemanticsCorrelation_t), NVTX_SEMANTIC_ID_CORRELATION_V1,
          NVTX_CORRELATION_SEMANTIC_VERSION, nullptr},
         {0},
         nullptr,
         NVTX_CORRELATION_ROLE_NONE}}
  {
  }

  // Raw C-header chaining is non-owning. Prefer the semantic-wrapper overload.
  /**
   * @brief Construct a correlation semantic chained to another semantic header.
   *
   * @param next Non-owning pointer to the next semantic in the chain, or nullptr.
   */
  explicit correlation_semantic(nvtxSemanticsHeader_t const* next)
    : correlation_semantic{}
  {
    set_next(next);
  }

  /**
   * @brief Construct a correlation semantic that chains to another semantic builder.
   *
   * Convenience overload of the header-pointer constructor that accepts a
   * sibling semantic builder object directly. The referenced builder is copied
   * into this object.
   *
   * @tparam OtherDataType Underlying C struct type of the other semantic.
   * @param next The semantic builder to chain after this one.
   */
  template <typename OtherDataType>
  explicit correlation_semantic(detail::semantic_base<OtherDataType> const& next)
    : correlation_semantic{}
  {
    own_next(next);
  }

  /**
   * @brief Set the correlation domain UUID.
   * @param uuid A 16-byte array that must be globally unique across NVTX domains.
   * @return Reference to this object for chaining.
   */
  correlation_semantic& domain_uuid(const unsigned char uuid[16]) noexcept
  {
    for (int i = 0; i < 16; ++i) {
      data_.correlationDomainUuid[i] = uuid[i];
    }
    return *this;
  }

  /**
   * @brief Set the optional display name for the correlation domain.
   * @param name String copied by the tool at schema registration; must remain
   *             valid until then.
   * @return Reference to this object for chaining.
   */
  correlation_semantic& display_name(const char* name) noexcept
  {
    data_.displayName = name;
    return *this;
  }

  /**
   * @brief Set the optional display name for the correlation domain.
   * @param name String copied by the tool at schema registration; must remain
   *             valid until then.
   * @return Reference to this object for chaining.
   */
  correlation_semantic& display_name(std::string const& name) noexcept
  {
    return display_name(name.c_str());
  }

  /**
   * @brief Disallow setting the borrowed display name from a temporary string.
   */
  correlation_semantic& display_name(std::string&& name) = delete;

  /**
   * @brief Set the role this entry plays in the correlation.
   * @param role_id One of the \c NVTX_CORRELATION_ROLE_* values.
   * @return Reference to this object for chaining.
   */
  correlation_semantic& role(uint64_t role_id) noexcept
  {
    data_.role = role_id;
    return *this;
  }
};

namespace detail {

/**
 * @brief Type trait to get the NVTX schema ID for a given C++ type.
 *
 * Primary template uses schema::get<T>() for user-defined struct types.
 * Specializations exist for primitive types.
 */
template <typename T, typename = void>
struct counter_schema_id {
  static uint64_t get() noexcept
  {
    return schema::get<T>().get_handle();
  }
};

// Specializations for primitive types
template <>
struct counter_schema_id<int64_t> {
  static constexpr uint64_t get() noexcept { return NVTX_PAYLOAD_ENTRY_TYPE_INT64; }
};

template <>
struct counter_schema_id<uint64_t> {
  static constexpr uint64_t get() noexcept { return NVTX_PAYLOAD_ENTRY_TYPE_UINT64; }
};

template <>
struct counter_schema_id<int32_t> {
  static constexpr uint64_t get() noexcept { return NVTX_PAYLOAD_ENTRY_TYPE_INT32; }
};

template <>
struct counter_schema_id<uint32_t> {
  static constexpr uint64_t get() noexcept { return NVTX_PAYLOAD_ENTRY_TYPE_UINT32; }
};

template <>
struct counter_schema_id<double> {
  static constexpr uint64_t get() noexcept { return NVTX_PAYLOAD_ENTRY_TYPE_FLOAT64; }
};

template <>
struct counter_schema_id<float> {
  static constexpr uint64_t get() noexcept { return NVTX_PAYLOAD_ENTRY_TYPE_FLOAT; }
};

} // namespace detail

/**
 * @brief A type-safe NVTX counter in a specific domain.
 *
 * `counter_in` provides a type-safe interface for NVTX counters. The counter
 * value type is specified as a template parameter, ensuring that only values
 * of the correct type can be sampled.
 *
 * For primitive types (int64_t, double, etc.), the schema is automatically
 * determined. For user-defined struct types, a schema must be registered
 * using `NVTX3_DEFINE_SCHEMA_GET`.
 *
 * @tparam T The value type of the counter.
 * @tparam D The domain type (defaults to global domain).
 *
 * Example:
 * \code{.cpp}
 * // Simple int64_t counter
 * nvtx3::counter<int64_t> iterations{"iteration_count"};
 * iterations.sample(42);
 *
 * // Counter with semantics
 * nvtx3::counter_semantic sem;
 * sem.unit("bytes").limits(int64_t{0}, int64_t{1024 * 1024});
 * nvtx3::counter<int64_t> memory{"heap_size",
 *                                "Process heap size",
 *                                nvtx3::scope::current_sw_process(),
 *                                sem};
 * memory.sample(512 * 1024);
 *
 * // Struct counter (requires NVTX3_DEFINE_SCHEMA_GET)
 * nvtx3::counter<gpu_metrics> gpu{"gpu_0"};
 * gpu.sample({72.5f, 250, 0.85});
 * \endcode
 */
template <typename T, typename D = domain::global>
class counter_in {
public:
  using value_type = T;

  /**
   * @brief Construct a counter with a name, description, and scope.
   *
   * @param name The counter name.
   * @param description The counter description.
   * @param s The scope for this counter.
   */
  counter_in(const char* name, const char* description = nullptr, scope s = scope::none() ) noexcept
    : counter_in{name, description, s, nullptr}
  {
  }

  /**
   * @brief Construct a counter with a name and scope.
   *
   * @param name The counter name.
   * @param s The scope for this counter.
   */
  counter_in(std::string const& name, scope s = scope::none()) noexcept
    : counter_in{name.c_str(), nullptr, s, nullptr}
  {
  }

  /**
   * @brief Construct a counter with a name, description, and scope.
   *
   * @param name The counter name.
   * @param description The counter description.
   * @param s The scope for this counter.
   */
  counter_in(std::string const& name,
             std::string const& description,
             scope s = scope::none()) noexcept
    : counter_in{name.c_str(), description.c_str(), s, nullptr}
  {
  }

  /**
   * @brief Construct a counter with name, description, scope, and semantics.
   *
   * @param name The counter name.
   * @param description The counter description.
   * @param s The scope for this counter.
   * @param semantic The counter semantics.
   */
  counter_in(const char* name, const char* description, scope s, const counter_semantic& semantic) noexcept
    : counter_in{name, description, s, &semantic}
  {
  }

  /**
   * @brief Construct a counter with name, description, scope, and semantics.
   *
   * @param name The counter name.
   * @param description The counter description.
   * @param s The scope for this counter.
   * @param semantic The counter semantics.
   */
  counter_in(std::string const& name,
             std::string const& description,
             scope s,
             const counter_semantic& semantic) noexcept
    : counter_in{name.c_str(), description.c_str(), s, &semantic}
  {
  }

  /**
   * @brief Sample the counter with a value.
   *
   * The value type must match the counter's template parameter T.
   * For primitive types (int64_t, double), optimized sampling functions
   * are used. For struct types, the value is passed by reference.
   *
   * @param value The counter value to sample.
   */
  void sample(T const& value) noexcept
  {
#ifndef NVTX_DISABLE
    sample_impl(value, std::is_same<T, int64_t>{}, std::is_same<T, double>{});
#else
    (void)value;
#endif
  }

  /**
   * @brief Sample the counter without a value.
   *
   * Used when a sample cannot be taken but should still be recorded.
   *
   * @param reason The reason for the missing value.
   */
  void sample_no_value(no_value_reason reason) noexcept
  {
#ifndef NVTX_DISABLE
    nvtxCounterSampleNoValue(domain::get<D>(), id_, static_cast<uint8_t>(reason));
#else
    (void)reason;
#endif
  }

  /**
   * @brief Submit a batch of counter samples.
   *
   * Use this overload when the counter sample layout includes timestamp entries.
   * Otherwise, provide timestamps with one of the timestamp overloads.
   *
   * @param values Pointer to a contiguous array of counter samples.
   * @param count Number of samples in \p values.
   * @param flags \c NVTX_BATCH_FLAG_* and \c NVTX_COUNTER_BATCH_FLAG_* bits.
   */
  void submit_batch(T const* values,
                    size_t count,
                    uint64_t flags = NVTX_BATCH_FLAG_TIME_SORTED) noexcept
  {
    submit_batch(values, count, nullptr, 0, flags);
  }

  /**
   * @brief Submit a batch of counter samples with external timestamps.
   *
   * @param values Pointer to a contiguous array of counter samples.
   * @param count Number of samples in \p values.
   * @param timestamps Pointer to timestamps or timestamp/interval pairs.
   * @param timestamp_count Number of timestamp values in \p timestamps.
   * @param flags \c NVTX_BATCH_FLAG_* and \c NVTX_COUNTER_BATCH_FLAG_* bits.
   */
  void submit_batch(T const* values,
                    size_t count,
                    int64_t const* timestamps,
                    size_t timestamp_count,
                    uint64_t flags = NVTX_BATCH_FLAG_TIME_SORTED) noexcept
  {
#ifndef NVTX_DISABLE
    nvtxCounterBatch_t batch{};
    batch.counterId = id_;
    batch.counters = values;
    batch.countersSize = count * sizeof(T);
    batch.flags = flags;
    batch.timestamps = timestamps;
    batch.timestampsSize = timestamp_count * sizeof(int64_t);

    nvtxCounterBatchSubmit(domain::get<D>(), &batch);
#else
    (void)values;
    (void)count;
    (void)timestamps;
    (void)timestamp_count;
    (void)flags;
#endif
  }

  /**
   * @brief Submit a batch from a contiguous container.
   *
   * The container must expose \c data() returning \c T* or \c T const*, and
   * \c size() convertible to \c size_t.
   */
  template <
      typename CounterContainer,
      typename = typename std::enable_if<
          detail::has_data_member<CounterContainer, T>::value
          && detail::has_size_member<CounterContainer>::value>::type>
  void submit_batch(CounterContainer const& values,
                    uint64_t flags = NVTX_BATCH_FLAG_TIME_SORTED) noexcept
  {
    submit_batch(values.data(), values.size(), flags);
  }

  /**
   * @brief Submit a batch from contiguous sample and timestamp containers.
   *
   * The sample container must expose \c T elements. The timestamp container must
   * expose \c int64_t elements.
   */
  template <
      typename CounterContainer,
      typename TimestampContainer,
      typename = typename std::enable_if<
          detail::has_data_member<CounterContainer, T>::value
          && detail::has_size_member<CounterContainer>::value
          && detail::has_data_member<TimestampContainer, int64_t>::value
          && detail::has_size_member<TimestampContainer>::value>::type>
  void submit_batch(CounterContainer const& values,
                    TimestampContainer const& timestamps,
                    uint64_t flags = NVTX_BATCH_FLAG_TIME_SORTED) noexcept
  {
    submit_batch(values.data(), values.size(), timestamps.data(), timestamps.size(), flags);
  }

  /**
   * @brief Get the underlying counter ID.
   *
   * Useful for interoperability with the NVTX C API.
   *
   * @return The counter ID.
   */
  uint64_t id() const noexcept { return id_; }

private:
  counter_in(const char* name, const char* description, scope s, counter_semantic const* semantic) noexcept
  {
#ifndef NVTX_DISABLE
    nvtxCounterAttr_t attr{};
    attr.structSize = sizeof(nvtxCounterAttr_t);
    attr.schemaId = detail::counter_schema_id<T>::get();
    attr.name = name;
    attr.description = description;
    attr.scopeId = s.get();
    attr.semantics = semantic ? semantic->get() : nullptr;

    id_ = nvtxCounterRegister(domain::get<D>(), &attr);
#else
    (void)name;
    (void)description;
    (void)s;
    (void)semantic;
#endif
  }

  // Overloads for optimized primitive sampling (int64_t)
  void sample_impl(int64_t value, std::true_type /*is_int64*/, std::false_type) noexcept
  {
    nvtxCounterSampleInt64(domain::get<D>(), id_, value);
  }

  // Overloads for optimized primitive sampling (double)
  void sample_impl(double value, std::false_type, std::true_type /*is_double*/) noexcept
  {
    nvtxCounterSampleFloat64(domain::get<D>(), id_, value);
  }

  // Generic fallback for struct types
  void sample_impl(T const& value, std::false_type, std::false_type) noexcept
  {
    nvtxCounterSample(domain::get<D>(), id_, &value, sizeof(T));
  }

  uint64_t id_{0};
};

/**
 * @brief Alias for a counter in the global NVTX domain.
 *
 * Example:
 * \code{.cpp}
 * nvtx3::counter<int64_t> my_counter{"name"};
 * \endcode
 */
template <typename T>
using counter = counter_in<T, domain::global>;

}  // namespace NVTX3_MINOR_VERSION_NAMESPACE
}  // namespace NVTX3_VERSION_NAMESPACE
} // namespace nvtx3

#ifndef NVTX_DISABLE
/**
 * @brief Convenience macro for generating a range in the specified `domain`
 * from the lifetime of a function
 *
 * This macro is useful for generating an NVTX range in `domain` from
 * the entry point of a function to its exit. It is intended to be the first
 * line of the function.
 *
 * Constructs a static `registered_string_in` using the name of the immediately
 * enclosing function returned by `__func__` and constructs a
 * `nvtx3::scoped_range` using the registered function name as the range's
 * message.
 *
 * Example:
 * \code{.cpp}
 * struct my_domain{static constexpr char const* name{"my_domain"};};
 *
 * void foo(...) {
 *    NVTX3_FUNC_RANGE_IN(my_domain); // Range begins on entry to foo()
 *    // do stuff
 *    ...
 * } // Range ends on return from foo()
 * \endcode
 *
 * @param[in] D Type containing `name` member used to identify the
 * `domain` to which the `registered_string_in` belongs. Else,
 * `domain::global` to  indicate that the global NVTX domain should be used.
 */
#define NVTX3_V1_FUNC_RANGE_IN(D)                                                  \
  static ::nvtx3::v1::registered_string_in<D> const nvtx3_func_name__{__func__};   \
  static ::nvtx3::v1::event_attributes const nvtx3_func_attr__{nvtx3_func_name__}; \
  ::nvtx3::v1::scoped_range_in<D> const nvtx3_range__{nvtx3_func_attr__}

/**
 * @brief Convenience macro for generating a range in the specified `domain`
 * from the lifetime of a function if the given boolean expression evaluates
 * to true.
 *
 * Similar to `NVTX3_V1_FUNC_RANGE_IN(D)`, the only difference being that
 * `NVTX3_V1_FUNC_RANGE_IF_IN(D, C)` only generates a range if the given boolean
 * expression evaluates to true.
 *
 * @param[in] D Type containing `name` member used to identify the
 * `domain` to which the `registered_string_in` belongs. Else,
 * `domain::global` to indicate that the global NVTX domain should be used.
 *
 * @param[in] C Boolean expression used to determine if a range should be
 * generated.
 */
#define NVTX3_V1_FUNC_RANGE_IF_IN(D, C) \
  ::nvtx3::v1::detail::optional_scoped_range_in<D> optional_nvtx3_range__;           \
  if (C) {                                                                           \
    static ::nvtx3::v1::registered_string_in<D> const nvtx3_func_name__{__func__};   \
    static ::nvtx3::v1::event_attributes const nvtx3_func_attr__{nvtx3_func_name__}; \
    optional_nvtx3_range__.begin(nvtx3_func_attr__);                                 \
  } (void)0
#else /* NVTX_DISABLE */
#define NVTX3_V1_FUNC_RANGE_IN(D) (void)0
#define NVTX3_V1_FUNC_RANGE_IF_IN(D, C) (void)(C)
#endif /* NVTX_DISABLE */

/**
 * @brief Convenience macro for generating a range in the global domain from the
 * lifetime of a function.
 *
 * This macro is useful for generating an NVTX range in the global domain from
 * the entry point of a function to its exit. It is intended to be the first
 * line of the function.
 *
 * Constructs a static `registered_string_in` using the name of the immediately
 * enclosing function returned by `__func__` and constructs a
 * `nvtx3::scoped_range` using the registered function name as the range's
 * message.
 *
 * Example:
 * \code{.cpp}
 * void foo(...) {
 *    NVTX3_FUNC_RANGE(); // Range begins on entry to foo()
 *    // do stuff
 *    ...
 * } // Range ends on return from foo()
 * \endcode
 */
#define NVTX3_V1_FUNC_RANGE() NVTX3_V1_FUNC_RANGE_IN(::nvtx3::v1::domain::global)

/**
 * @brief Convenience macro for generating a range in the global domain from the
 * lifetime of a function if the given boolean expression evaluates to true.
 *
 * Similar to `NVTX3_V1_FUNC_RANGE()`, the only difference being that
 * `NVTX3_V1_FUNC_RANGE_IF(C)` only generates a range if the given boolean
 * expression evaluates to true.
 *
 * @param[in] C Boolean expression used to determine if a range should be
 * generated.
 */
#define NVTX3_V1_FUNC_RANGE_IF(C) NVTX3_V1_FUNC_RANGE_IF_IN(::nvtx3::v1::domain::global, C)

// We need another helper macro because the other one will get undefined
#if __has_cpp_attribute(nodiscard)
#define NVTX3_V1_NO_DISCARD [[nodiscard]]
#else
#define NVTX3_V1_NO_DISCARD
#endif
/**
 * @brief Convenience macro for generating a sspecialization of nvtx3::schema::get
 * for an existing `struct`.
 *
 * Use this macro after your struct to enable the struct to be used as a payload.
 *
 * \note This macro must not be used inside a namespace.
 *
 * Example:
 * \code{.cpp}
 * struct SensorData
 * {
 *     int32_t sensorId;
 *     uint8_t channelId;
 * };
 *
 * NVTX3_DEFINE_SCHEMA_GET(
 *     my_domain,
 *     SensorData,
 *     "SensorEvent",
 *     NVTX_PAYLOAD_ENTRIES(
 *         (sensorId, TYPE_INT32, "SensorID"),
 *         (channelId, TYPE_UINT8, "ChannelID")
 *     )
 * )
 * \endcode
 *
 * @param[in] dom The NVTX domain.
 * @param[in] struct_id The name of the struct.
 * @param[in] schema_name Name of the payload schema.
 * @param[in] entries Payload schema entries using NVTX_PAYLOAD_ENTRIES macro.
 *
 * @note On MSVC, this macro requires the conforming preprocessor:
 *       \c /Zc:preprocessor (VS 2019+) or \c /experimental:preprocessor
 *       (VS 2017 v15.5+).  Not supported on older MSVC versions.
 */
#define NVTX3_V1_DEFINE_SCHEMA_GET(dom, struct_id, schema_name, entries)                               \
    template <>                                                                                        \
    NVTX3_V1_NO_DISCARD inline nvtx3::v1::schema const& nvtx3::v1::schema::get<struct_id>() noexcept   \
    {                                                                                                  \
        static_assert(                                                                                 \
            std::is_standard_layout<struct_id>::value,                                                 \
            "structs used for NVTX3 payload schema must be standard layout");                          \
        static_assert(                                                                                 \
            std::is_trivially_copyable<struct_id>::value,                                              \
            "structs used for NVTX3 payload schema must be trivially copyable");                       \
        using nvtx_struct_id = struct_id; /* avoids issues with namespaced struct_id */                \
        _NVTX_DEFINE_SCHEMA_FOR_STRUCT(nvtx_struct_id, schema_name, static const, entries)              \
        static const schema s{                                                                         \
            nvtxPayloadSchemaRegister(nvtx3::v1::domain::get<dom>(), &nvtx_struct_id##Attr)};          \
        return s;                                                                                      \
    }

/**
 * @brief Macro for inline semantic definitions in schema entries.
 *
 * Use this macro within NVTX_PAYLOAD_ENTRIES to attach per-field semantics
 * or a chain thereof. The macro creates a static local to give the
 * semantic object stable storage for the lifetime of the program, and
 * returns the pointer to its header that the schema entry's \c semantics
 * field expects.
 *
 * The type of the semantic is deduced from the expression, so any builder
 * in the `nvtx3::v1` semantic family works. If the semantic is chained to
 * another semantic, the chain is copied into the static semantic object.
 *
 * Example:
 * \code{.cpp}
 * NVTX3_DEFINE_SCHEMA_GET(
 *     my_domain,
 *     sensor_data,
 *     "SensorData",
 *     NVTX_PAYLOAD_ENTRIES(
 *         (temperature, TYPE_FLOAT, "Temperature", nullptr, 0, UNUSED,
 *             NVTX3_SEMANTIC(nvtx3::counter_semantic{}.unit("C").limits(-40.0, 85.0))),
 *         (pressure, TYPE_FLOAT, "Pressure", nullptr, 0, UNUSED,
 *             NVTX3_SEMANTIC(nvtx3::counter_semantic{}.unit("hPa")))))
 * \endcode
 *
 * @param expr Any semantic builder expression exposing a \c get() method
 *             returning an \c nvtxSemanticsHeader_t const*.
 */
#define NVTX3_V1_SEMANTIC(expr)                                    \
    ([]() -> nvtxSemanticsHeader_t const* {                        \
        static auto const s_ = (expr);                             \
        return s_.get();                                           \
    }())

/* When inlining this version, versioned macros must have unversioned aliases.
 * For each NVTX3_Vx_ #define, make an NVTX3_ alias of it here.*/
#if defined(NVTX3_INLINE_THIS_VERSION)
/* clang format off */
#define NVTX3_FUNC_RANGE        NVTX3_V1_FUNC_RANGE
#define NVTX3_FUNC_RANGE_IF     NVTX3_V1_FUNC_RANGE_IF
#define NVTX3_FUNC_RANGE_IN     NVTX3_V1_FUNC_RANGE_IN
#define NVTX3_FUNC_RANGE_IF_IN  NVTX3_V1_FUNC_RANGE_IF_IN
#define NVTX3_DEFINE_SCHEMA_GET NVTX3_V1_DEFINE_SCHEMA_GET
#define NVTX3_SEMANTIC          NVTX3_V1_SEMANTIC
/* clang format on */
#endif

#endif  // NVTX3_CPP_DEFINITIONS_V1_0

/* Undefine all temporarily-defined unversioned macros, which would conflict with
 * subsequent includes of different versions of this header. */
#undef NVTX3_CPP_VERSION_MAJOR
#undef NVTX3_CPP_VERSION_MINOR
#undef NVTX3_CONCAT
#undef NVTX3_NAMESPACE_FOR
#undef NVTX3_VERSION_NAMESPACE
#undef NVTX3_MINOR_NAMESPACE_FOR
#undef NVTX3_MINOR_VERSION_NAMESPACE
#undef NVTX3_INLINE_IF_REQUESTED
#undef NVTX3_CONSTEXPR_IF_CPP14
#undef NVTX3_CONSTEXPR_IF_CPP20
#undef NVTX3_MAYBE_UNUSED
#undef NVTX3_NO_DISCARD

#if defined(NVTX3_INLINE_THIS_VERSION)
#undef NVTX3_INLINE_THIS_VERSION
#endif

#if defined(NVTX3_USE_CHECKED_OVERLOADS_FOR_GET_DEFINED_HERE)
#undef NVTX3_USE_CHECKED_OVERLOADS_FOR_GET_DEFINED_HERE
#undef NVTX3_USE_CHECKED_OVERLOADS_FOR_GET
#endif

#if defined(NVTX3_STATIC_ASSERT_DEFINED_HERE)
#undef NVTX3_STATIC_ASSERT_DEFINED_HERE
#undef NVTX3_STATIC_ASSERT
#endif