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
|
// © 2024 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include "unicode/utypes.h"
#ifndef MESSAGEFORMAT_DATA_MODEL_H
#define MESSAGEFORMAT_DATA_MODEL_H
#if U_SHOW_CPLUSPLUS_API
#if !UCONFIG_NO_NORMALIZATION
#if !UCONFIG_NO_FORMATTING
#if !UCONFIG_NO_MF2
#include "unicode/localpointer.h"
#include "unicode/messageformat2_data_model_names.h"
#ifndef U_HIDE_DEPRECATED_API
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <optional>
#include <variant>
#include <vector>
U_NAMESPACE_BEGIN
class UVector;
// Helpers
// Note: this _must_ be declared `inline` or else gcc will generate code
// for its instantiations, which needs to be avoided because it returns
// a std::vector
template<typename T>
static inline std::vector<T> toStdVector(const T* arr, int32_t len) {
std::vector<T> result;
for (int32_t i = 0; i < len; i++) {
result.push_back(arr[i]);
}
return result;
}
#if defined(U_REAL_MSVC)
#pragma warning(push)
// Ignore warning 4251 as these templates are instantiated later in this file,
// after the classes used to instantiate them have been defined.
#pragma warning(disable: 4251)
#endif
namespace message2 {
class Checker;
class MessageFormatter;
class Parser;
class Serializer;
namespace data_model {
class Binding;
class Literal;
class Operator;
class MFDataModel;
/**
* The `Literal` class corresponds to the `literal` nonterminal in the MessageFormat 2 grammar,
* https://github.com/unicode-org/message-format-wg/blob/main/spec/message.abnf and the
* `Literal` interface defined in
* // https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#expressions
*
* `Literal` is immutable, copyable and movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API Literal : public UObject {
public:
/**
* Returns the quoted representation of this literal (enclosed in '|' characters)
*
* @return A string representation of the literal enclosed in quote characters.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
UnicodeString quoted() const;
/**
* Returns the parsed string contents of this literal.
*
* @return A string representation of this literal.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
const UnicodeString& unquoted() const;
/**
* Determines if this literal appeared as a quoted literal in the message.
*
* @return true if and only if this literal appeared as a quoted literal in the
* message.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
UBool isQuoted() const { return thisIsQuoted; }
/**
* Literal constructor.
*
* @param q True if and only if this literal was parsed with the `quoted` nonterminal
* (appeared enclosed in '|' characters in the message text).
* @param s The string contents of this literal; escape sequences are assumed to have
* been interpreted already.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Literal(UBool q, const UnicodeString& s) : thisIsQuoted(q), contents(s) {}
/**
* Copy constructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Literal(const Literal& other) : thisIsQuoted(other.thisIsQuoted), contents(other.contents) {}
/**
* Non-member swap function.
* @param l1 will get l2's contents
* @param l2 will get l1's contents
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
friend inline void swap(Literal& l1, Literal& l2) noexcept {
using std::swap;
swap(l1.thisIsQuoted, l2.thisIsQuoted);
swap(l1.contents, l2.contents);
}
/**
* Assignment operator.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Literal& operator=(Literal) noexcept;
/**
* Default constructor.
* Puts the Literal into a valid but undefined state.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Literal() = default;
/**
* Less than operator. Compares `this.stringContents()` with
* `other.stringContents()`. This method is used in representing
* the mapping from key lists to patterns in a message with variants,
* and is not expected to be useful otherwise.
*
* @param other The Literal to compare to this one.
* @return true if the parsed string corresponding to this `Literal`
* is less than the parsed string corresponding to the other `Literal`
* (according to `UnicodeString`'s less-than operator).
* Returns false otherwise.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
bool operator<(const Literal& other) const;
/**
* Equality operator. Compares `this.stringContents()` with
* `other.stringContents()`. This method is used in representing
* the mapping from key lists to patterns in a message with variants,
* and is not expected to be useful otherwise.
*
* @param other The Literal to compare to this one.
* @return true if the parsed string corresponding to this `Literal`
* equals the parsed string corresponding to the other `Literal`
* (according to `UnicodeString`'s equality operator).
* Returns false otherwise.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
bool operator==(const Literal& other) const;
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
virtual ~Literal();
private:
/* const */ bool thisIsQuoted = false;
/* const */ UnicodeString contents;
};
/**
* The `Operand` class corresponds to the `operand` nonterminal in the MessageFormat 2 grammar,
* https://github.com/unicode-org/message-format-wg/blob/main/spec/message.abnf .
* It represents a `Literal | VariableRef` -- see the `operand?` field of the `FunctionRef`
* interface defined at:
* https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#expressions
* with the difference that it can also represent a null operand (the absent operand in an
* `annotation` with no operand).
*
* `Operand` is immutable and is copyable and movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API_CLASS Operand : public UObject {
public:
/**
* Determines if this operand represents a variable.
*
* @return True if and only if the operand is a variable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API UBool isVariable() const;
/**
* Determines if this operand represents a literal.
*
* @return True if and only if the operand is a literal.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API UBool isLiteral() const;
/**
* Determines if this operand is the null operand.
*
* @return True if and only if the operand is the null operand.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API virtual UBool isNull() const;
/**
* Returns a reference to this operand's variable name.
* Precondition: isVariable()
*
* @return A reference to the name of the variable
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API const UnicodeString& asVariable() const;
/**
* Returns a reference to this operand's literal contents.
* Precondition: isLiteral()
*
* @return A reference to the literal
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API const Literal& asLiteral() const;
/**
* Default constructor.
* Creates a null Operand.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API Operand() : contents(std::nullopt) {}
/**
* Variable operand constructor.
*
* @param v The variable name; an operand corresponding
* to a reference to `v` is returned.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API explicit Operand(const UnicodeString& v) : contents(VariableName(v)) {}
/**
* Literal operand constructor.
*
* @param l The literal to use for this operand; an operand
* corresponding to `l` is returned.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API explicit Operand(const Literal& l) : contents(l) {}
/**
* Non-member swap function.
* @param o1 will get o2's contents
* @param o2 will get o1's contents
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API friend inline void swap(Operand& o1, Operand& o2) noexcept {
using std::swap;
(void) o1;
(void) o2;
swap(o1.contents, o2.contents);
}
/**
* Assignment operator.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API virtual Operand& operator=(Operand) noexcept;
/**
* Copy constructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API Operand(const Operand&);
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API virtual ~Operand();
private:
std::optional<std::variant<VariableName, Literal>> contents;
}; // class Operand
/**
* The `Key` class corresponds to the `key` nonterminal in the MessageFormat 2 grammar,
* https://github.com/unicode-org/message-format-wg/blob/main/spec/message.abnf .
* It also corresponds to
* the `Literal | CatchallKey` that is the
* element type of the `keys` array in the `Variant` interface
* defined in https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#messages
*
* A key is either a literal or the wildcard symbol (represented in messages as '*')
*
* `Key` is immutable, copyable and movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API_CLASS Key : public UObject {
public:
/**
* Determines if this is a wildcard key
*
* @return True if and only if this is the wildcard key
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API UBool isWildcard() const { return !contents.has_value(); }
/**
* Returns the contents of this key as a literal.
* Precondition: !isWildcard()
*
* @return The literal contents of the key
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API const Literal& asLiteral() const;
/**
* Copy constructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API Key(const Key& other) : contents(other.contents) {}
/**
* Wildcard constructor; constructs a Key representing the
* catchall or wildcard key, '*'.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API Key() : contents(std::nullopt) {}
/**
* Literal key constructor.
*
* @param lit A Literal to use for this key. The result matches the
* literal `lit`.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API explicit Key(const Literal& lit) : contents(lit) {}
/**
* Non-member swap function.
* @param k1 will get k2's contents
* @param k2 will get k1's contents
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API friend inline void swap(Key& k1, Key& k2) noexcept {
using std::swap;
swap(k1.contents, k2.contents);
}
/**
* Assignment operator
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API Key& operator=(Key) noexcept;
/**
* Less than operator. Compares the literal of `this` with the literal of `other`.
* This method is used in representing the mapping from key lists to patterns
* in a message with variants, and is not expected to be useful otherwise.
*
* @param other The Key to compare to this one.
* @return true if the two `Key`s are not wildcards and if `this.asLiteral()`
* < `other.asLiteral()`.
* Returns false otherwise.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API bool operator<(const Key& other) const;
/**
* Equality operator. Compares the literal of `this` with the literal of `other`.
* This method is used in representing the mapping from key lists to patterns
* in a message with variants, and is not expected to be useful otherwise.
*
* @param other The Key to compare to this one.
* @return true if either both `Key`s are wildcards, or `this.asLiteral()`
* == `other.asLiteral()`.
* Returns false otherwise.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API bool operator==(const Key& other) const;
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API virtual ~Key();
private:
/* const */ std::optional<Literal> contents;
}; // class Key
/**
* The `SelectorKeys` class represents the key list for a single variant.
* It corresponds to the `keys` array in the `Variant` interface
* defined in https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#messages
*
* `SelectorKeys` is immutable, copyable and movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API_CLASS SelectorKeys : public UObject {
public:
/**
* Returns the underlying list of keys.
*
* @return The list of keys for this variant.
* Returns an empty list if allocating this `SelectorKeys`
* object previously failed.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API std::vector<Key> getKeys() const {
return toStdVector<Key>(keys.getAlias(), len);
}
/**
* The mutable `SelectorKeys::Builder` class allows the key list to be constructed
* one key at a time.
*
* Builder is not copyable or movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API Builder : public UMemory {
private:
friend class SelectorKeys;
UVector* keys; // This is a raw pointer and not a LocalPointer<UVector> to avoid undefined behavior warnings,
// since UVector is forward-declared
// The vector owns its elements
public:
/**
* Adds a single key to the list.
*
* @param key The key to be added. Passed by move
* @param status Input/output error code
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& add(Key&& key, UErrorCode& status) noexcept;
/**
* Constructs a new immutable `SelectorKeys` using the list of keys
* set with previous `add()` calls.
*
* The builder object (`this`) can still be used after calling `build()`.
*
* @param status Input/output error code
* @return The new SelectorKeys object
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
SelectorKeys build(UErrorCode& status) const;
/**
* Default constructor.
* Returns a Builder with an empty list of keys.
*
* @param status Input/output error code
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder(UErrorCode& status);
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
virtual ~Builder();
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(Builder&&) = delete;
}; // class SelectorKeys::Builder
/**
* Less than operator. Compares the two key lists lexicographically.
* This method makes it possible for a `SelectorKeys` to be used as a map
* key, which allows variants to be represented as a map. It is not expected
* to be useful otherwise.
*
* @param other The SelectorKeys to compare to this one.
* @return true if `this` is less than `other`, comparing the two key lists
* lexicographically.
* Returns false otherwise.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API bool operator<(const SelectorKeys& other) const;
/**
* Default constructor.
* Puts the SelectorKeys into a valid but undefined state.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API SelectorKeys() : len(0) {}
/**
* Non-member swap function.
* @param s1 will get s2's contents
* @param s2 will get s1's contents
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API friend inline void swap(SelectorKeys& s1, SelectorKeys& s2) noexcept {
using std::swap;
swap(s1.len, s2.len);
swap(s1.keys, s2.keys);
}
/**
* Copy constructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API SelectorKeys(const SelectorKeys& other);
/**
* Assignment operator.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API SelectorKeys& operator=(SelectorKeys other) noexcept;
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API virtual ~SelectorKeys();
private:
friend class Builder;
friend class message2::Checker;
friend class message2::MessageFormatter;
friend class message2::Serializer;
/* const */ LocalArray<Key> keys;
/* const */ int32_t len;
const Key* getKeysInternal() const;
SelectorKeys(const UVector& ks, UErrorCode& status);
}; // class SelectorKeys
class Operator;
/**
* An `Option` pairs an option name with an Operand.
*
* `Option` is immutable, copyable and movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API Option : public UObject {
public:
/**
* Accesses the right-hand side of the option.
*
* @return A reference to the operand.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
const Operand& getValue() const { return rand; }
/**
* Accesses the left-hand side of the option.
*
* @return A reference to the option name.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
const UnicodeString& getName() const { return name; }
/**
* Constructor. Returns an `Option` representing the
* named option "name=rand".
*
* @param n The name of the option.
* @param r The value of the option.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Option(const UnicodeString& n, Operand&& r) : name(n), rand(std::move(r)) {}
/**
* Default constructor.
* Returns an Option in a valid but undefined state.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Option() {}
/**
* Non-member swap function.
* @param o1 will get o2's contents
* @param o2 will get o1's contents
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
friend inline void swap(Option& o1, Option& o2) noexcept {
using std::swap;
swap(o1.name, o2.name);
swap(o1.rand, o2.rand);
}
/**
* Copy constructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Option(const Option& other);
/**
* Assignment operator
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Option& operator=(Option other) noexcept;
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
virtual ~Option();
private:
/* const */ UnicodeString name;
/* const */ Operand rand;
}; // class Option
// Internal only
#ifndef U_IN_DOXYGEN
// Options
// This is a wrapper class around a vector of options that provides lookup operations
class U_I18N_API_CLASS OptionMap : public UObject {
public:
U_I18N_API int32_t size() const;
// Needs to take an error code b/c an earlier copy might have failed
U_I18N_API const Option& getOption(int32_t, UErrorCode&) const;
U_I18N_API friend inline void swap(OptionMap& m1, OptionMap& m2) noexcept {
using std::swap;
swap(m1.bogus, m2.bogus);
swap(m1.options, m2.options);
swap(m1.len, m2.len);
}
U_I18N_API OptionMap() : len(0) {}
U_I18N_API OptionMap(const OptionMap&);
U_I18N_API OptionMap& operator=(OptionMap);
U_I18N_API std::vector<Option> getOptions() const {
return toStdVector<Option>(options.getAlias(), len);
}
U_I18N_API OptionMap(const UVector&, UErrorCode&);
U_I18N_API OptionMap(Option*, int32_t);
U_I18N_API virtual ~OptionMap();
class U_I18N_API Builder : public UObject {
private:
UVector* options;
bool checkDuplicates = true;
public:
Builder& add(Option&& opt, UErrorCode&);
Builder(UErrorCode&);
static Builder attributes(UErrorCode&);
// As this class is private, build() is destructive
OptionMap build(UErrorCode&);
friend inline void swap(Builder& m1, Builder& m2) noexcept {
using std::swap;
swap(m1.options, m2.options);
swap(m1.checkDuplicates, m2.checkDuplicates);
}
Builder(Builder&&);
Builder(const Builder&) = delete;
Builder& operator=(Builder) noexcept;
virtual ~Builder();
}; // class OptionMap::Builder
private:
friend class message2::Serializer;
bool bogus = false;
LocalArray<Option> options;
int32_t len;
}; // class OptionMap
#endif
/**
* The `Operator` class corresponds to the `FunctionRef` type in the
* `Expression` interface defined in
* https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#patterns
*
* It represents the annotation that an expression can have: a function name paired
* with a map from option names to operands (possibly empty).
*
* `Operator` is immutable, copyable and movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API Operator : public UObject {
public:
/**
* Accesses the function name.
*
* @return The function name of this operator.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
const FunctionName& getFunctionName() const;
/**
* Accesses function options.
*
* @return A vector of function options for this operator.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
std::vector<Option> getOptions() const {
return options.getOptions();
}
/**
* The mutable `Operator::Builder` class allows the operator to be constructed
* incrementally.
*
* Builder is not copyable or movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API Builder : public UMemory {
private:
friend class Operator;
FunctionName functionName;
OptionMap::Builder options;
public:
/**
* Sets this operator to be a function annotation and sets its name
* to `func`.
*
* @param func The function name.
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& setFunctionName(FunctionName&& func);
/**
* Sets this operator to be a function annotation and adds a
* single option.
*
* @param key The name of the option.
* @param value The value (right-hand side) of the option.
* @param status Input/output error code.
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& addOption(const UnicodeString &key, Operand&& value, UErrorCode& status) noexcept;
/**
* Constructs a new immutable `Operator` using the
* function name and options that were previously set.
*
* The builder object (`this`) can still be used after calling `build()`.
*
* The `build()` method is non-const for internal implementation reasons,
* but is observably const.
*
* @param status Input/output error code.
* @return The new Operator
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Operator build(UErrorCode& status);
/**
* Default constructor.
* Returns a Builder with no function name or options set.
*
* @param status Input/output error code.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder(UErrorCode& status);
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
virtual ~Builder();
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(Builder&&) = delete;
}; // class Operator::Builder
/**
* Copy constructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Operator(const Operator& other) noexcept;
/**
* Non-member swap function.
* @param o1 will get o2's contents
* @param o2 will get o1's contents
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
friend inline void swap(Operator& o1, Operator& o2) noexcept {
using std::swap;
swap(o1.name, o2.name);
swap(o1.options, o2.options);
}
/**
* Assignment operator.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Operator& operator=(Operator) noexcept;
/**
* Default constructor.
* Puts the Operator into a valid but undefined state.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Operator() {}
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
virtual ~Operator();
private:
friend class Binding;
friend class Builder;
friend class message2::Checker;
friend class message2::MessageFormatter;
friend class message2::Serializer;
// Function call constructor
Operator(const FunctionName& f, const UVector& options, UErrorCode&);
const OptionMap& getOptionsInternal() const;
Operator(const FunctionName&, const OptionMap&);
/* const */ FunctionName name;
/* const */ OptionMap options;
}; // class Operator
// Internal only
typedef enum UMarkupType {
UMARKUP_OPEN = 0,
UMARKUP_CLOSE,
UMARKUP_STANDALONE,
UMARKUP_COUNT
} UMarkupType;
/**
* The `Markup` class corresponds to the `markup` nonterminal in the MessageFormat 2
* grammar and the `markup` interface defined in
* https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model/message.json
*
* `Markup` is immutable, copyable and movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API Markup : public UObject {
public:
/**
* Checks if this markup is an opening tag.
*
* @return True if and only if this represents an opening tag.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
UBool isOpen() const { return (type == UMARKUP_OPEN); }
/**
* Checks if this markup is an closing tag.
*
* @return True if and only if this represents an closing tag.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
UBool isClose() const { return (type == UMARKUP_CLOSE); }
/**
* Checks if this markup is an standalone tag.
*
* @return True if and only if this represents a standalone tag.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
UBool isStandalone() const { return (type == UMARKUP_STANDALONE); }
/**
* Gets the name of this markup
*
* @return A reference to the string identifying the markup
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
const UnicodeString& getName() const { return name; }
/**
* Gets the options of this markup
*
* @return A reference to the string identifying the markup
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
std::vector<Option> getOptions() const { return options.getOptions(); }
/**
* Gets the attributes of this markup
*
* @return A vector of attributes
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
std::vector<Option> getAttributes() const { return attributes.getOptions(); }
/**
* Default constructor.
* Puts the Markup into a valid but undefined state.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Markup() {}
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
virtual ~Markup();
/**
* The mutable `Markup::Builder` class allows the markup to be constructed
* incrementally.
*
* Builder is not copyable or movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API Builder : public UMemory {
private:
friend class Markup;
UnicodeString name;
OptionMap::Builder options;
OptionMap::Builder attributes;
UMarkupType type = UMARKUP_COUNT;
public:
/**
* Sets the name of this markup.
*
* @param n A string representing the name.
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& setName(const UnicodeString& n) { name = n; return *this; }
/**
* Sets this to be an opening markup.
*
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& setOpen() { type = UMARKUP_OPEN; return *this; }
/**
* Sets this to be an closing markup.
*
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& setClose() { type = UMARKUP_CLOSE; return *this; }
/**
* Sets this to be a standalone markup.
*
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& setStandalone() { type = UMARKUP_STANDALONE; return *this; }
/**
* Adds a single option.
*
* @param key The name of the option.
* @param value The value (right-hand side) of the option.
* @param status Input/output error code.
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& addOption(const UnicodeString &key, Operand&& value, UErrorCode& status);
/**
* Adds a single attribute.
*
* @param key The name of the attribute.
* @param value The value (right-hand side) of the attribute.
* @param status Input/output error code.
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& addAttribute(const UnicodeString &key, Operand&& value, UErrorCode& status);
/**
* Constructs a new immutable `Markup` using the name and type
* and (optionally) options and attributes that were previously set.
* If `setName()` and at least one of `setOpen()`, `setClose()`, and `setStandalone()`
* were not previously called,
* then `status` is set to U_INVALID_STATE_ERROR.
*
* The builder object (`this`) can still be used after calling `build()`.
* The `build()` method is non-const for internal implementation reasons,
* but is observably const.
*
* @param status Input/output error code.
* @return The new Markup.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Markup build(UErrorCode& status);
/**
* Default constructor.
* Returns a Builder with no name, type, options, or attributes set.
*
* @param status Input/output error code.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder(UErrorCode& status);
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
virtual ~Builder();
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(Builder&&) = delete;
}; // class Markup::Builder
private:
friend class Builder;
friend class message2::Serializer;
UMarkupType type;
UnicodeString name;
OptionMap options;
OptionMap attributes;
const OptionMap& getOptionsInternal() const { return options; }
const OptionMap& getAttributesInternal() const { return attributes; }
Markup(UMarkupType, UnicodeString, OptionMap&&, OptionMap&&);
}; // class Markup
/**
* The `Expression` class corresponds to the `expression` nonterminal in the MessageFormat 2
* grammar and the `Expression` interface defined in
* https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#patterns
*
* It represents either an operand with no annotation; an annotation with no operand;
* or an operand annotated with an annotation.
*
* `Expression` is immutable, copyable and movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API_CLASS Expression : public UObject {
public:
/**
* Checks if this expression is an annotation
* with no operand.
*
* @return True if and only if the expression has
* an annotation and has no operand.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API UBool isStandaloneAnnotation() const;
/**
* Checks if this expression has a function
* annotation (with or without an operand).
*
* @return True if and only if the expression has an annotation
* that is a function.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API UBool isFunctionCall() const;
/**
* Accesses the function
* annotating this expression.
* If !(isFunctionCall()), sets
* `status` to U_INVALID_STATE_ERROR.
*
* @param status Input/output error code.
* @return A non-owned pointer to the operator of this expression,
* which does not outlive the expression.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API const Operator* getOperator(UErrorCode& status) const;
/**
* Accesses the operand of this expression.
*
* @return A reference to the operand of this expression,
* which may be the null operand.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API const Operand& getOperand() const;
/**
* Gets the attributes of this expression
*
* @return A vector of attributes
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API std::vector<Option> getAttributes() const { return attributes.getOptions(); }
/**
* The mutable `Expression::Builder` class allows the operator to be constructed
* incrementally.
*
* Builder is not copyable or movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API Builder : public UMemory {
private:
friend class Expression;
bool hasOperand = false;
bool hasOperator = false;
Operand rand;
Operator rator;
OptionMap::Builder attributes;
public:
/**
* Sets the operand of this expression.
*
* @param rAnd The operand to set. Passed by move.
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& setOperand(Operand&& rAnd);
/**
* Sets the operator of this expression.
*
* @param rAtor The operator to set. Passed by move.
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& setOperator(Operator&& rAtor);
/**
* Adds a single attribute.
*
* @param key The name of the attribute.
* @param value The value (right-hand side) of the attribute.
* @param status Input/output error code.
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& addAttribute(const UnicodeString &key, Operand&& value, UErrorCode& status);
/**
* Constructs a new immutable `Expression` using the operand and operator that
* were previously set. If neither `setOperand()` nor `setOperator()` was
* previously called, or if `setOperand()` was called with the null operand
* and `setOperator()` was never called, then `status` is set to
* U_INVALID_STATE_ERROR.
*
* The builder object (`this`) can still be used after calling `build()`.
* The `build()` method is non-const for internal implementation reasons,
* but is observably const.
* @param status Input/output error code.
* @return The new Expression.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Expression build(UErrorCode& status);
/**
* Default constructor.
* Returns a Builder with no operator or operand set.
*
* @param status Input/output error code.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder(UErrorCode& status);
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
virtual ~Builder();
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(Builder&&) = delete;
}; // class Expression::Builder
/**
* Non-member swap function.
* @param e1 will get e2's contents
* @param e2 will get e1's contents
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API friend inline void swap(Expression& e1, Expression& e2) noexcept {
using std::swap;
swap(e1.rator, e2.rator);
swap(e1.rand, e2.rand);
swap(e1.attributes, e2.attributes);
}
/**
* Copy constructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API Expression(const Expression& other);
/**
* Assignment operator.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API Expression& operator=(Expression) noexcept;
/**
* Default constructor.
* Puts the Expression into a valid but undefined state.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API Expression();
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API virtual ~Expression();
private:
friend class message2::Serializer;
/*
Internally, an expression is represented as the application of an optional operator to an operand.
The operand is always present; for function calls with no operand, it's represented
as an operand for which `isNull()` is true.
Operator | Operand
--------------------------------
{ |42| :fun opt=value } => (FunctionName=fun, | Literal(quoted=true, contents="42")
options={opt: value})
{ abcd } => null | Literal(quoted=false, contents="abcd")
{ : fun opt=value } => (FunctionName=fun,
options={opt: value}) | NullOperand()
*/
Expression(const Operator &rAtor, const Operand &rAnd, const OptionMap& attrs) : rator(rAtor), rand(rAnd), attributes(attrs) {}
Expression(const Operand &rAnd, const OptionMap& attrs) : rator(std::nullopt), rand(Operand(rAnd)), attributes(attrs) {}
Expression(const Operator &rAtor, const OptionMap& attrs) : rator(rAtor), rand(), attributes(attrs) {}
/* const */ std::optional<Operator> rator;
/* const */ Operand rand;
/* const */ OptionMap attributes;
const OptionMap& getAttributesInternal() const { return attributes; }
}; // class Expression
class Pattern;
// Despite the comments, `PatternPart` is internal-only
/**
* A `PatternPart` is a single element (text or expression) in a `Pattern`.
* It corresponds to the `body` field of the `Pattern` interface
* defined in https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#patterns
*
* `PatternPart` is immutable, copyable and movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API_CLASS PatternPart : public UObject {
public:
/**
* Checks if the part is a text part.
*
* @return True if and only if this is a text part.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API UBool isText() const { return std::holds_alternative<UnicodeString>(piece); }
/**
* Checks if the part is a markup part.
*
* @return True if and only if this is a markup part.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API UBool isMarkup() const { return std::holds_alternative<Markup>(piece); }
/**
* Checks if the part is an expression part.
*
* @return True if and only if this is an expression part.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API UBool isExpression() const { return std::holds_alternative<Expression>(piece); }
/**
* Accesses the expression of the part.
* Precondition: isExpression()
*
* @return A reference to the part's underlying expression.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API const Expression& contents() const;
/**
* Accesses the expression of the part.
* Precondition: isMarkup()
*
* @return A reference to the part's underlying expression.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API const Markup& asMarkup() const;
/**
* Accesses the text contents of the part.
* Precondition: isText()
*
* @return A reference to a string representing the part's text..
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API const UnicodeString& asText() const;
/**
* Non-member swap function.
* @param p1 will get p2's contents
* @param p2 will get p1's contents
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API friend inline void swap(PatternPart& p1, PatternPart& p2) noexcept {
using std::swap;
swap(p1.piece, p2.piece);
}
/**
* Copy constructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API PatternPart(const PatternPart& other);
/**
* Assignment operator.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API PatternPart& operator=(PatternPart) noexcept;
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API virtual ~PatternPart();
/**
* Text part constructor. Returns a text pattern part
* with text `t`.
*
* @param t A text string.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API explicit PatternPart(const UnicodeString& t) : piece(t) {}
/**
* Expression part constructor. Returns an Expression pattern
* part with expression `e`.
*
* @param e An Expression.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API explicit PatternPart(Expression&& e) : piece(e) {}
/**
* Markup part constructor. Returns a Markup pattern
* part with markup `m`
*
* @param m A Markup.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API explicit PatternPart(Markup&& m) : piece(m) {}
/**
* Default constructor.
* Puts the PatternPart into a valid but undefined state.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API PatternPart() = default;
private:
friend class Pattern;
std::variant<UnicodeString, Expression, Markup> piece;
}; // class PatternPart
/**
* A `Pattern` is a sequence of formattable parts.
* It corresponds to the `Pattern` interface
* defined in https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#patterns
*
* `Pattern` is immutable, copyable and movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API_CLASS Pattern : public UObject {
private:
friend class PatternPart;
public:
#ifndef U_IN_DOXYGEN
struct U_I18N_API Iterator;
#endif
/**
* Returns the parts of this pattern
*
* @return A forward iterator of variants. Each element is either a string (text part)
* or an expression part.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API Iterator begin() const {
return Iterator(this, 0);
}
/**
* Returns a special value to mark the end of iteration
*
* @return A forward iterator of variants. This should only be used for comparisons
* against an iterator returned by incrementing begin().
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API Iterator end() const {
return Iterator(this, len);
}
/**
* The mutable `Pattern::Builder` class allows the pattern to be
* constructed one part at a time.
*
* Builder is not copyable or movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API Builder : public UMemory {
private:
friend class Pattern;
UVector* parts; // Not a LocalPointer for the same reason as in `SelectorKeys::Builder`
public:
/**
* Adds a single expression part to the pattern.
*
* @param part The part to be added (passed by move)
* @param status Input/output error code.
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& add(Expression&& part, UErrorCode& status) noexcept;
/**
* Adds a single markup part to the pattern.
*
* @param part The part to be added (passed by move)
* @param status Input/output error code.
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& add(Markup&& part, UErrorCode& status) noexcept;
/**
* Adds a single text part to the pattern. Copies `part`.
*
* @param part The part to be added (passed by move)
* @param status Input/output error code.
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& add(UnicodeString&& part, UErrorCode& status) noexcept;
/**
* Constructs a new immutable `Pattern` using the list of parts
* set with previous `add()` calls.
*
* The builder object (`this`) can still be used after calling `build()`.
*
* @param status Input/output error code.
* @return The pattern object
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Pattern build(UErrorCode& status) const noexcept;
/**
* Default constructor.
* Returns a Builder with an empty sequence of PatternParts.
*
* @param status Input/output error code
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder(UErrorCode& status);
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
virtual ~Builder();
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(Builder&&) = delete;
}; // class Pattern::Builder
/**
* Default constructor.
* Puts the Pattern into a valid but undefined state.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API Pattern() : parts(LocalArray<PatternPart>()) {}
/**
* Non-member swap function.
* @param p1 will get p2's contents
* @param p2 will get p1's contents
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API friend inline void swap(Pattern& p1, Pattern& p2) noexcept {
using std::swap;
swap(p1.bogus, p2.bogus);
swap(p1.len, p2.len);
swap(p1.parts, p2.parts);
}
/**
* Copy constructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API Pattern(const Pattern& other);
/**
* Assignment operator
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API Pattern& operator=(Pattern) noexcept;
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API virtual ~Pattern();
/**
* The `Pattern::Iterator` class provides an iterator over the formattable
* parts of a pattern.
*
* `Pattern::Iterator` is mutable and is not copyable or movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
struct U_I18N_API Iterator {
private:
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = std::variant<UnicodeString, Expression, Markup>;
using pointer = value_type*;
using reference = const value_type&;
friend class Pattern;
Iterator(const Pattern* p, int32_t i) : pos(i), pat(p) {}
friend bool operator== (const Iterator& a, const Iterator& b) { return (a.pat == b.pat && a.pos == b.pos); }
int32_t pos;
const Pattern* pat;
public:
/**
* Dereference operator (gets the element at the current iterator position)
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
reference operator*() const {
const PatternPart& part = pat->parts[pos];
return patternContents(part);
}
/**
* Increment operator (advances to the next iterator position)
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Iterator operator++() { pos++; return *this; }
/**
* Inequality comparison operator (used for comparing an iterator to the result of end())
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
friend bool operator!= (const Iterator& a, const Iterator& b) { return !(a == b); }
}; // struct Iterator
private:
friend class Builder;
friend class message2::MessageFormatter;
friend class message2::Serializer;
// Set to true if a copy constructor fails;
// needed in order to distinguish an uninitialized
// Pattern from a 0-length pattern
bool bogus = false;
// Possibly-empty array of parts
int32_t len = 0;
LocalArray<PatternPart> parts;
Pattern(const UVector& parts, UErrorCode& status);
// Helper
static void initParts(Pattern&, const Pattern&);
/**
* Returns the size.
*
* @return The number of parts in the pattern.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
int32_t numParts() const;
/**
* Returns the `i`th part in the pattern.
* Precondition: i < numParts()
*
* @param i Index of the part being accessed.
* @return A reference to the part at index `i`.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
const PatternPart& getPart(int32_t i) const;
// Gets around not being able to declare Pattern::Iterator as a friend
// in PatternPart
static const std::variant<UnicodeString, Expression, Markup>&
patternContents(const PatternPart& p) { return p.piece; }
}; // class Pattern
/**
* A `Variant` pairs a list of keys with a pattern
* It corresponds to the `Variant` interface
* defined in https://github.com/unicode-org/message-format-wg/tree/main/spec/data-model
*
* `Variant` is immutable, copyable and movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API Variant : public UObject {
public:
/**
* Accesses the pattern of the variant.
*
* @return A reference to the pattern.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
const Pattern& getPattern() const { return p; }
/**
* Accesses the keys of the variant.
*
* @return A reference to the keys.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
const SelectorKeys& getKeys() const { return k; }
/**
* Constructor. Returns a variant that formats to `pattern`
* when `keys` match the selector expressions in the enclosing
* `match` construct.
*
* @param keys A reference to a `SelectorKeys`.
* @param pattern A pattern (passed by move)
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Variant(const SelectorKeys& keys, Pattern&& pattern) : k(keys), p(std::move(pattern)) {}
/**
* Non-member swap function.
* @param v1 will get v2's contents
* @param v2 will get v1's contents
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
friend inline void swap(Variant& v1, Variant& v2) noexcept {
using std::swap;
swap(v1.k, v2.k);
swap(v1.p, v2.p);
}
/**
* Assignment operator
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Variant& operator=(Variant other) noexcept;
/**
* Default constructor.
* Returns a Variant in a valid but undefined state.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Variant() = default;
/**
* Copy constructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Variant(const Variant&);
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
virtual ~Variant();
private:
/* const */ SelectorKeys k;
/* const */ Pattern p;
}; // class Variant
/**
* A `Binding` pairs a variable name with an expression.
* It corresponds to the `Declaration` interface
* defined in https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#messages
*
* `Binding` is immutable and copyable. It is not movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API Binding : public UObject {
public:
/**
* Accesses the right-hand side of a binding.
*
* @return A reference to the expression.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
const Expression& getValue() const;
/**
* Accesses the left-hand side of the binding.
*
* @return A reference to the variable name.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
const VariableName& getVariable() const { return var; }
/**
* Constructor for input binding.
*
* @param variableName The variable name (left-hand side) of the binding. Passed by move.
* @param rhs The right-hand side of the input binding. Passed by move.
* `rhs` must have an operand that is a variable reference to `variableName`.
* If `rhs` has an operator, it must be a function call.
* If either of these properties is violated, `errorCode` is set to
* U_INVALID_STATE_ERROR.
* @param errorCode Input/output error code
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
static Binding input(UnicodeString&& variableName, Expression&& rhs, UErrorCode& errorCode);
/**
* Returns true if and only if this binding represents a local declaration.
* Otherwise, it's an input declaration.
*
* @return True if this binding represents a variable and expression;
* false if it represents a variable plus an annotation.
* @internal ICU 78 technology preview
* @deprecated This API is for technology preview only.
*/
UBool isLocal() const { return local; }
/**
* Constructor.
*
* @param v A variable name.
* @param e An expression.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Binding(const VariableName& v, Expression&& e) : var(v), expr(std::move(e)), local(true), annotation(nullptr) {}
/**
* Non-member swap function.
* @param b1 will get b2's contents
* @param b2 will get b1's contents
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
friend inline void swap(Binding& b1, Binding& b2) noexcept {
using std::swap;
swap(b1.var, b2.var);
swap(b1.expr, b2.expr);
swap(b1.local, b2.local);
b1.updateAnnotation();
b2.updateAnnotation();
}
/**
* Copy constructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Binding(const Binding& other);
/**
* Copy assignment operator
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Binding& operator=(Binding) noexcept;
/**
* Default constructor.
* Puts the Binding into a valid but undefined state.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Binding() : local(true) {}
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
virtual ~Binding();
private:
friend class message2::Checker;
friend class message2::MessageFormatter;
friend class message2::Parser;
friend class message2::Serializer;
/* const */ VariableName var;
/* const */ Expression expr;
/* const */ bool local;
// The following field is always nullptr for a local
// declaration, and possibly nullptr for an .input declaration
// If non-null, the referent is a member of `expr` so
// its lifetime is the same as the lifetime of the enclosing Binding
// (as long as there's no mutation)
const Operator* annotation = nullptr;
const OptionMap& getOptionsInternal() const;
bool hasAnnotation() const { return !local && (annotation != nullptr); }
void updateAnnotation();
}; // class Binding
// Internal only
#ifndef U_IN_DOXYGEN
class U_I18N_API_CLASS Matcher : public UObject {
public:
U_I18N_API Matcher& operator=(Matcher);
U_I18N_API Matcher(const Matcher&);
/**
* Non-member swap function.
* @param m1 will get m2's contents
* @param m2 will get m1's contents
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API friend inline void swap(Matcher& m1, Matcher& m2) noexcept {
using std::swap;
if (m1.bogus) {
m2.bogus = true;
return;
}
if (m2.bogus) {
m1.bogus = true;
return;
}
swap(m1.selectors, m2.selectors);
swap(m1.numSelectors, m2.numSelectors);
swap(m1.variants, m2.variants);
swap(m1.numVariants, m2.numVariants);
}
U_I18N_API virtual ~Matcher();
private:
friend class MFDataModel;
Matcher(VariableName* ss, int32_t ns, Variant* vs, int32_t nv);
Matcher() {}
// A Matcher may have numSelectors=0 and numVariants=0
// (this is a data model error, but it's representable).
// So we have to keep a separate flag to track failed copies.
bool bogus = false;
// The variables that are being matched on.
LocalArray<VariableName> selectors;
// The number of selectors
int32_t numSelectors = 0;
// The list of `when` clauses (case arms).
LocalArray<Variant> variants;
// The number of variants
int32_t numVariants = 0;
}; // class Matcher
#endif
// -----------------------------------------------------------------------
// Public MFDataModel class
/**
*
* The `MFDataModel` class describes a parsed representation of the text of a message.
* This representation is public as higher-level APIs for messages will need to know its public
* interface: for example, to re-instantiate a parsed message with different values for imported
variables.
*
* The MFDataModel API implements <a target="github"
href="https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md">the
* specification of the abstract syntax (data model representation)</a> for MessageFormat.
*
* `MFDataModel` is immutable, copyable and movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API_CLASS MFDataModel : public UMemory {
/*
Classes that represent nodes in the data model are nested inside the
`MFDataModel` class.
Classes such as `Expression`, `Pattern` and `VariantMap` are immutable and
are constructed using the builder pattern.
Most classes representing nodes have copy constructors. This is because builders
contain immutable data that must be copied when calling `build()`, since the builder
could go out of scope before the immutable result of the builder does. Copying is
also necessary to prevent unexpected mutation if intermediate builders are saved
and mutated again after calling `build()`.
The copy constructors perform a deep copy, for example by copying the entire
list of options for an `Operator` (and copying the entire underlying vector.)
Some internal fields should be `const`, but are declared as non-`const` to make
the copy constructor simpler to implement. (These are noted throughout.) In
other words, those fields are `const` except during the execution of a copy
constructor.
On the other hand, intermediate `Builder` methods that return a `Builder&`
mutate the state of the builder, so in code like:
Expression::Builder& exprBuilder = Expression::builder()-> setOperand(foo);
Expression::Builder& exprBuilder2 = exprBuilder.setOperator(bar);
the call to `setOperator()` would mutate `exprBuilder`, since `exprBuilder`
and `exprBuilder2` are references to the same object.
An alternate choice would be to make `build()` destructive, so that copying would
be unnecessary. Or, both copying and moving variants of `build()` could be
provided. Copying variants of the intermediate `Builder` methods could be
provided as well, if this proved useful.
*/
public:
/**
* Accesses the local variable declarations for this data model.
*
* @return A vector of bindings for local variables.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API std::vector<Binding> getLocalVariables() const {
std::vector<Binding> result;
if (!bogus) {
return toStdVector<Binding>(bindings.getAlias(), bindingsLen);
}
return {};
}
/**
* Accesses the selectors. Returns an empty vector if this is a pattern message.
*
* @return A vector of selectors.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API std::vector<VariableName> getSelectors() const {
if (std::holds_alternative<Pattern>(body)) {
return {};
}
const Matcher* match = std::get_if<Matcher>(&body);
// match must be non-null, given the previous check
return toStdVector<VariableName>(match->selectors.getAlias(), match->numSelectors);
}
/**
* Accesses the variants. Returns an empty vector if this is a pattern message.
*
* @return A vector of variants.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API std::vector<Variant> getVariants() const {
// Return empty vector if no variants
if (std::holds_alternative<Pattern>(body)) {
return {};
}
const Matcher* match = std::get_if<Matcher>(&body);
// match must be non-null, given the previous check
return toStdVector<Variant>(match->variants.getAlias(), match->numVariants);
return {};
}
/**
* Accesses the pattern (in a message without selectors).
* Returns a reference to an empty pattern if the message has selectors.
*
* @return A reference to the pattern.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API const Pattern& getPattern() const;
/**
* The mutable `MFDataModel::Builder` class allows the data model to be
* constructed incrementally.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API Builder;
/**
* Default constructor.
* Puts the MFDataModel into a valid but undefined state.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API MFDataModel();
/**
* Non-member swap function.
* @param m1 will get m2's contents
* @param m2 will get m1's contents
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API friend inline void swap(MFDataModel& m1, MFDataModel& m2) noexcept {
using std::swap;
if (m1.bogus) {
m2.bogus = true;
return;
}
if (m2.bogus) {
m1.bogus = true;
return;
}
swap(m1.body, m2.body);
swap(m1.bindings, m2.bindings);
swap(m1.bindingsLen, m2.bindingsLen);
}
/**
* Assignment operator
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API MFDataModel& operator=(MFDataModel) noexcept;
/**
* Copy constructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API MFDataModel(const MFDataModel& other);
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
U_I18N_API virtual ~MFDataModel();
/**
* The mutable `MFDataModel::Builder` class allows the data model to be
* constructed incrementally. Builder is not copyable or movable.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
class U_I18N_API Builder : public UMemory {
private:
friend class MFDataModel;
void checkDuplicate(const VariableName&, UErrorCode&) const;
void buildSelectorsMessage(UErrorCode&);
bool hasPattern = true;
bool hasSelectors = false;
Pattern pattern;
// The following members are not LocalPointers for the same reason as in SelectorKeys::Builder
UVector* selectors = nullptr;
UVector* variants = nullptr;
UVector* bindings = nullptr;
public:
/**
* Adds a binding, There must not already be a binding
* with the same name.
*
* @param b The binding. Passed by move.
* @param status Input/output error code. Set to U_DUPLICATE_DECLARATION_ERROR
* if `addBinding()` was previously called with a binding
* with the same variable name as `b`.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& addBinding(Binding&& b, UErrorCode& status);
/**
* Adds a selector variable.
* If a pattern was previously set, clears the pattern.
*
* @param selector Variable to add as a selector. Passed by move.
* @param errorCode Input/output error code
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& addSelector(VariableName&& selector, UErrorCode& errorCode);
/**
* Adds a single variant.
* If a pattern was previously set using `setPattern()`, clears the pattern.
*
* @param keys Keys for the variant. Passed by move.
* @param pattern Pattern for the variant. Passed by move.
* @param errorCode Input/output error code
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& addVariant(SelectorKeys&& keys, Pattern&& pattern, UErrorCode& errorCode) noexcept;
/**
* Sets the body of the message as a pattern.
* If selectors and/or variants were previously set, clears them.
*
* @param pattern Pattern to represent the body of the message.
* Passed by move.
* @return A reference to the builder.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder& setPattern(Pattern&& pattern);
/**
* Constructs a new immutable data model.
* If `setPattern()` has not been called and if `addSelector()` and
* `addVariant()` were not each called at least once,
* `status` is set to `U_INVALID_STATE_ERROR`.
* If `addSelector()` was called and `addVariant()` was never called,
* or vice versa, then `status` is set to U_INVALID_STATE_ERROR.
* Otherwise, either a Pattern or Selectors message is constructed
* based on the pattern that was previously set, or selectors and variants
* that were previously set.
*
* The builder object (`this`) can still be used after calling `build()`.
*
* @param status Input/output error code.
* @return The new MFDataModel
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
MFDataModel build(UErrorCode& status) const noexcept;
/**
* Default constructor.
* Returns a Builder with no pattern or selectors set.
* Either `setPattern()` or both `addSelector()` and
* `addVariant()` must be called before calling `build()`
* on the resulting builder.
*
* @param status Input/output error code.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
Builder(UErrorCode& status);
/**
* Destructor.
*
* @internal ICU 75 technology preview
* @deprecated This API is for technology preview only.
*/
virtual ~Builder();
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(Builder&&) = delete;
}; // class Builder
private:
friend class message2::Checker;
friend class message2::MessageFormatter;
friend class message2::Serializer;
Pattern empty; // Provided so that `getPattern()` can return a result
// if called on a selectors message
bool hasPattern() const { return std::holds_alternative<Pattern>(body); }
bool bogus = false; // Set if a copy constructor fails
// A message body is either a matcher (selector list and variant list),
// or a single pattern
std::variant<Matcher, Pattern> body;
// Bindings for local variables
/* const */ LocalArray<Binding> bindings;
int32_t bindingsLen = 0;
const Binding* getLocalVariablesInternal() const;
const VariableName* getSelectorsInternal() const;
const Variant* getVariantsInternal() const;
int32_t numSelectors() const {
const Matcher* matcher = std::get_if<Matcher>(&body);
return (matcher == nullptr ? 0 : matcher->numSelectors);
}
int32_t numVariants() const {
const Matcher* matcher = std::get_if<Matcher>(&body);
return (matcher == nullptr ? 0 : matcher->numVariants);
}
// Helper
void initBindings(const Binding*);
MFDataModel(const Builder& builder, UErrorCode&) noexcept;
}; // class MFDataModel
} // namespace data_model
} // namespace message2
U_NAMESPACE_END
#endif // U_HIDE_DEPRECATED_API
#endif /* #if !UCONFIG_NO_MF2 */
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif /* #if !UCONFIG_NO_NORMALIZATION */
#endif /* U_SHOW_CPLUSPLUS_API */
#endif // MESSAGEFORMAT_DATA_MODEL_H
// eof
|