/usr/include/trilinos/Teuchos_as.hpp is in libtrilinos-teuchos-dev 12.12.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | // @HEADER
// ***********************************************************************
//
// Teuchos: Common Tools Package
// Copyright (2004) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ***********************************************************************
// @HEADER
#ifndef TEUCHOS_AS_HPP
#define TEUCHOS_AS_HPP
/// \file Teuchos_as.hpp
/// \brief Definition of Teuchos::as, for conversions between types.
///
/// This header file declares Teuchos::as, which is a template
/// function that converts between two different types. For example,
/// the following code converts from \c double to \c int:
/// \code
/// double d = 3.14;
/// int i = Teuchos::as<double> (d);
/// assert (i == 3);
/// \endcode
/// In a debug build of Teuchos, this code would check for overflow
/// when converting from double (64 bits) to int (32 bits, on most
/// platforms these days), and throw an exception if overflow occurs.
/// In a release build, this code would not check for overflow. Users
/// who want to check for overflow may use the Teuchos::asSafe
/// template function. This works the same way as Teuchos::as, except
/// that it always checks for overflow.
///
/// This file includes definitions of a small number of useful
/// conversions. If you want to define your own conversions, you
/// should specialize the Teuchos::ValueTypeConversionTraits template
/// class for the "to" and "from" types in your conversion. This
/// automatically defines Teuchos::as and Teuchos::asSafe for your
/// conversion.
#include "Teuchos_ConfigDefs.hpp"
#include "Teuchos_Assert.hpp"
#include <limits>
#include <cstdlib>
#include <cerrno>
#include <climits>
#ifdef HAVE_TEUCHOS_COMPLEX
#include <complex>
#endif // HAVE_TEUCHOS_COMPLEX
#ifdef HAVE_TEUCHOS_QD
#include <qd/qd_real.h>
#include <qd/dd_real.h>
#endif // HAVE_TEUCHOS_QD
namespace Teuchos {
/** \class ValueTypeConversionTraits
* \brief Default traits class for all conversions between value types.
* \ingroup teuchos_language_support_grp
*
* \note Users should never call this class directly. Please use the
* as() or asSafe() template functions.
*
* \tparam TypeTo The type to which to convert; the output type.
* \tparam TypeFrom The type from which to convert; the input type.
*
* The type conversion functions as() and asSafe() defined in this
* file use this traits class to convert between types. The default
* implementation of this class simply does an implicit type
* conversion. Syntactically, it expects either that TypeTo have a
* constructor which takes a single TypeFrom argument, like this:
* \code
* class TypeTo {
* public:
* TypeTo (const TypeFrom& x);
* }
* \endcode
* or that TypeFrom has an "operator TypeTo()" method, like this:
* \code
* class TypeFrom {
* public:
* operator TypeTo ();
* }
* \endcode
* Any conversions which are built into C++ and are safe to do will
* not need a traits class specialization, and should not generate any
* compiler warnings. This includes the conversions <tt>float</tt> to
* <tt>double</tt>, <tt>short</tt> to <tt>int</tt>, <tt>int</tt> to
* <tt>long</tt>, or an enum value to <tt>int</tt>.
*
* Any conversion which is not syntactically legal according to the
* above rules <i>must</i> have a specialization. There are a number
* of examples in the header file below, including conversions between
* <tt>qd_real</tt> and various built-in types for which
* <tt>qd_real</tt> does not provide a native conversion operator.
* Other examples include <tt>std::string</tt> to <tt>int</tt> or
* <tt>double</tt>.
*
* Any conversion that <i>is</i> syntactically legal, but could cause
* compiler warnings and/or result in incorrect behavior at run time
* should be given a specialization that does not rely on an implicit
* conversion. This includes the following:
*
* - <tt>type</tt> to and from <tt>unsigned type</tt>, where
* <tt>type</tt> is a built-in integer type
* - <tt>double</tt> to <tt>int</tt>, or from any floating-point
* type to any integer type where overflow is possible
*
* If the user (through as() or asSafe()) requests a conversion for
* which no specialization of this class exists, then the default
* implementation below will be instantiated. If the conversion is
* not syntactically correct, then the compiler will report a compiler
* error. If the conversion is syntactically correct but unsafe, the
* compiler <i>may</i> report a warning. In either case, you can fix
* the error or warning by specializing this class for your
* combination of types. There are a number of examples of
* specializations in this header file, some of which include bounds
* checking for overflow (for safeConvert()).
*
* The IEEE 754 standard defines the result of conversions from a
* larger to a smaller built-in floating-point type, including
* <tt>double</tt> to <tt>float</tt>, <tt>long double</tt> to
* <tt>float</tt>, and \c <tt>long double</tt> to <tt>double</tt>.
* Such conversions might overflow (result in a value too large in
* magnitude to fit in the target type) or underflow (result in a
* value too small to fit in a normalized value of the target type).
* We <i>never</i> check for overflow or underflow for these
* conversions. Their behavior depends on the current rounding mode
* and whether your hardware and compiler correctly implement
* denormalized values. Typically, overflow results in an Inf of the
* same sign as the input, and underflow results in either a
* denormalized value or zero. If you want to do bounds checking, you
* should set the appropriate trap so that overflow or underflow will
* raise the SIGFPE signal. Please refer to the IEEE 754 standard for
* details. Note that safeConvert() conversions from e.g.,
* std::string to built-in floating-point types still should check for
* overflow.
*
* \note We cannot promise that converting from T1 to T2 and back
* again will result in the same T1 value with which we started.
* For example, converting from a long long to a double may result
* in truncation, since long long has 63 bits of significand and
* double has 53.
*/
template<class TypeTo, class TypeFrom>
class ValueTypeConversionTraits {
public:
//! Convert t from a TypeFrom object to a TypeTo object.
static TypeTo convert (const TypeFrom t) {
// This default implementation is just an implicit conversion and
// may generate compiler warnings on dangerous conversions.
return t;
}
//! Convert t from a TypeFrom object to a TypeTo object, with checks for validity.
static TypeTo safeConvert (const TypeFrom t) {
// This default implementation is just an implicit conversion and
// may generate compiler warnings on dangerous conversions. No
// runtime checking (e.g., for overflow) can be done by default;
// only specializations can define meaningful and portable
// run-time checks of conversions.
return t;
}
};
/** \brief Convert from one value type to another.
* \ingroup teuchos_language_support_grp
*
* \tparam TypeTo The type to which to convert; the output type.
* \tparam TypeFrom The type from which to convert; the input type.
*
* \section Teuchos_as_User User documentation
*
* This template function lets you convert from one value type to
* another, possibly with checks for overflow (where appropriate) in a
* debug build of Teuchos. For example, to convert between int and
* double:
* \code
* double d = 3.14;
* int i = Teuchos::as<double> (d);
* assert (i == 3);
* \endcode
* In a debug build of Teuchos, this will check for overflow, since
* there are some double-precision floating-point values that do not
* fit in a 32-bit integer. In a release build, this will not check
* for overflow. You are responsible for knowing the difference. If
* you <i>always</i> want to check for overflow (e.g., to validate
* user input), use the asSafe() function. Note that conversion from
* a floating-point number to an integer, or from a higher-precision
* floating-point number to a lower-precision floating-point number,
* may truncate or round (as it does in the above example). We do
* not check for truncation or rounding.
*
* "Debug build of Teuchos" means more than just building with debug
* compiler flags. It means debug checking was turned on when
* Trilinos was built. If you are building Trilinos yourself, you may
* turn on debug checking by setting the
* <tt>Trilinos_ENABLE_DEBUG</tt> CMake configure option to \c ON
* (rather than \c OFF, which is the default). Note that enabling
* debug checking affects other operations in Teuchos besides this
* conversion, and may have a significant run-time cost, especially
* for RCP and ArrayRCP.
*
* The IEEE 754 standard defines the result of conversions from a
* larger to a smaller built-in floating-point type, including
* <tt>double</tt> to <tt>float</tt>, <tt>long double</tt> to
* <tt>float</tt>, and \c <tt>long double</tt> to <tt>double</tt>.
* Such conversions might overflow (result in a value too large in
* magnitude to fit in the target type) or underflow (result in a
* value too small to fit in a normalized value of the target type).
* We <i>never</i> check for overflow or underflow for these
* conversions. Their behavior depends on the current rounding mode
* and whether your hardware and compiler correctly implement
* denormalized values. Typically, overflow results in an Inf of the
* same sign as the input, and underflow results in either a
* denormalized value or zero. If you want to do bounds checking, you
* should set the appropriate trap so that overflow or underflow will
* raise the SIGFPE signal. Please refer to the IEEE 754 standard for
* details. Note that debug-mode as() conversions from e.g.,
* std::string to built-in floating-point types still check for
* overflow.
*
* \note We cannot promise that converting from a type T1 to another
* type T2 and back again will result in the same T1 value with
* which we started. For example, converting from a long long to a
* double may result in truncation, since long long has 63 bits of
* significand and double has 53.
*
* \section Teuchos_as_Dev Developer documentation
*
* This function uses the traits class ValueTypeConversionTraits to
* perform checking and conversion. If debug checking is turned on,
* this function uses the traits class' safeConvert() method to
* perform possibly checked conversion. Otherwise, it uses the traits
* class' convert() method for unchecked conversion.
*
* If you want to specialize this function's behavior, you should
* specialize ValueTypeConversionTraits for your combination of input
* and output types (TypeFrom resp. TypeTo). Be sure to define the
* specialization in the Teuchos namespace. We provide
* specializations of ValueTypeConversionTraits in this file for a
* variety of types. You must define both safeConvert() and convert()
* in the specialization, since as() will call safeConvert() in a
* debug build and convert() in a release build.
*
* \note The implementations below do not consider truncation of
* floating-point values to be unsafe conversion. For example,
* converting from a long long (63 bits of significand) to a double
* (53 bits of significand) may result in truncation, but we do not
* consider this unsafe. "Unsafe" mainly refers to overflow or lack
* of representation.
*/
template<class TypeTo, class TypeFrom>
inline TypeTo as( const TypeFrom& t );
/** \brief Convert from one value type to another, with validity checks if appropriate.
* \ingroup teuchos_language_support_grp
*
* \tparam TypeTo The type to which to convert; the output type.
* \tparam TypeFrom The type from which to convert; the input type.
*
* \section Teuchos_asSafe_User User documentation
*
* This template function lets you convert from one value type to
* another. For example, to convert between int and double:
* \code
* double d = 3.14;
* int i = Teuchos::asSafe<double> (d);
* assert (i == 3);
* \endcode
* This function always checks for validity of the conversion before
* attempting it. Examples of invalid conversions are those which
* would overflow, for example from double to int, if the
* double-precision floating-point input is bigger than the largest
* int or smaller than the smallest int. If you only which to check
* in a debug build, use the as() function instead. Note that
* conversion from a floating-point number to an integer, or from a
* higher-precision floating-point number to a lower-precision
* floating-point number, may truncate or round (as it does in the
* above example).
*
* The IEEE 754 standard defines the result of conversions from a
* larger to a smaller built-in floating-point type, including
* <tt>double</tt> to <tt>float</tt>, <tt>long double</tt> to
* <tt>float</tt>, and \c <tt>long double</tt> to <tt>double</tt>.
* Such conversions might overflow (result in a value too large in
* magnitude to fit in the target type) or underflow (result in a
* value too small to fit in a normalized value of the target type).
* We <i>never</i> check for overflow or underflow for these
* conversions. Their behavior depends on the current rounding mode
* and whether your hardware and compiler correctly implement
* denormalized values. Typically, overflow results in an Inf of the
* same sign as the input, and underflow results in either a
* denormalized value or zero. If you want to do bounds checking, you
* should set the appropriate trap so that overflow or underflow will
* raise the SIGFPE signal. Please refer to the IEEE 754 standard for
* details. Note that asSafe() conversions from e.g., std::string to
* built-in floating-point types still check for overflow.
*
* \section Teuchos_asSafe_Dev Developer documentation
*
* This function uses the traits class ValueTypeConversionTraits to
* perform the actual checking and conversion. It always uses the
* traits class' safeConvert() method to perform a possibly checked
* conversion.
*
* If you want to specialize this function's behavior, you should
* specialize ValueTypeConversionTraits for your combination of input
* and output types (TypeFrom resp. TypeTo). Be sure to define the
* specialization in the Teuchos namespace. We provide
* specializations of ValueTypeConversionTraits for a variety of
* types.
*
* \note The implementations below do not consider truncation of
* floating-point values to be unsafe conversion. For example,
* converting from a long long (63 bits of significand) to a double
* (53 bits of significand) may result in truncation, but we do not
* consider this unsafe. "Unsafe" mainly refers to overflow or lack
* of representation.
*/
template<class TypeTo, class TypeFrom>
inline TypeTo asSafe( const TypeFrom& t );
/// \class asFunc
/// \brief Function object wrapper for as().
/// \ingroup teuchos_language_support_grp
///
/// \tparam TypeTo Type to which to convert; the output type.
///
/// Sometimes it is useful to pass around the as() type conversion
/// function as a first-class object, for example as a function
/// argument of generic algorithms such as std::transform(). In this
/// case, you may use this class, which invokes as() in its operator()
/// method. The operator() method is templated on the input type
/// TypeFrom.
template <class TypeTo>
class asFunc {
public:
asFunc() {}
template <class TypeFrom>
inline TypeTo operator()(const TypeFrom &t) {
return as<TypeTo>(t);
}
};
namespace { // anonymous
/// Helper function for converting the given \c std::string to an
/// integer of type <tt>IntType</tt>.
///
/// \tparam IntType A built-in integer type, like \c int or \c long.
/// It may be signed or unsigned.
///
/// \param t [in] The string to convert.
///
/// \param rawConvert [in] A function with the same arguments as
/// strtol, strtoul, strtoll, or strtoull, which returns
/// <tt>IntType<tt>. It must return the same type as
/// <tt>IntType</tt>. Note that all of these but strtol require
/// C99 support. It's up to you to pick the right function for
/// <tt>IntType</tt>.
///
/// \param intTypeName [in] Human-readable string which is the name
/// of <tt>IntType</tt>.
template<class IntType>
IntType
intToString (const std::string& t,
IntType (*rawConvert) (const char*, char**, int),
const char* intTypeName)
{
// We call the "raw" conversion function instead of using
// std::istringstream, because we want more detailed information
// in case of failure to convert. I have no idea what
// operator>>(std::istream&, unsigned long long&) does if it
// encounters an integer too long to fit in IntType, for example.
//
// mfh 13 Nov 2012: It's fair to assume that if you have "long
// long", then your implementation of the C standard library
// includes strtoul(). Ditto for "unsigned long long" and
// strtoull(). If this is not the case, we could include a
// configure-time test for these functions(), with a fall-back to
// an std::istringstream operator>> implementation.
char* endptr = NULL;
// Keep the pointer, because std::string doesn't necessarily
// guarantee that this is the same across calls to c_str(), does
// it? Or perhaps it does...
const char* t_ptr = t.c_str ();
// We preset errno to 0, to distinguish success or failure after
// calling strtoull. Most implementations of the C standard
// library written with threads in mind have errno be a macro that
// expands to thread-local storage. Thanks to the Linux
// documentation for strtol ("man 3 strtol", Red Hat Enterprise
// Linux 5) for advice with the following checks.
errno = 0;
const IntType val = rawConvert (t_ptr, &endptr, 10);
const IntType minVal = std::numeric_limits<IntType>::min ();
const IntType maxVal = std::numeric_limits<IntType>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
errno == ERANGE && (val == minVal || val == maxVal),
std::range_error,
"Teuchos::ValueTypeConversionTraits<" << intTypeName << ", std::string>::convert: "
"The integer value in the given string \"" << t << "\" overflows " << intTypeName << ".");
TEUCHOS_TEST_FOR_EXCEPTION(
errno != 0 && val == 0,
std::invalid_argument,
"Teuchos::ValueTypeConversionTraits<" << intTypeName << ", std::string>::convert: "
"The conversion function was unable to convert the given string \"" << t << "\" to " << intTypeName << ".");
TEUCHOS_TEST_FOR_EXCEPTION(
endptr == t_ptr, // See above discussion of c_str().
std::invalid_argument,
"Teuchos::ValueTypeConversionTraits<" << intTypeName << ", std::string>::convert: "
"The conversion function was unable to read any integer digits from the given string "
"\"" << t << "\".");
return val;
}
/// Convert the given InputRealType real-valued floating-point
/// number to OutputRealType, also a real-valued floating-point
/// number with a possibly different precision. Optionally do
/// bounds checking and throw std::range_error on overflow.
///
/// This function is useful for ValueTypeConversionTraits<float,
/// std::string>::safeConvert(), and in general whenever we need to
/// use an intermediate floating-point type to convert from a string
/// to an final floating-point type. This is because asSafe() does
/// not do bounds checking when converting between floating-point
/// types.
template<class OutputRealType, class InputRealType>
OutputRealType
realToReal (const InputRealType& x, const bool doBoundsChecking)
{
using Teuchos::TypeNameTraits;
if (doBoundsChecking) {
// For floating-point types T, std::numeric_limits<T>::min()
// returns the smallest positive value. IEEE 754 types have a
// sign bit, so the largest-magnitude negative value is the
// negative of the largest-magnitude positive value.
const OutputRealType minVal = -std::numeric_limits<OutputRealType>::max ();
const OutputRealType maxVal = std::numeric_limits<OutputRealType>::max ();
// NaN is neither less than nor greater than anything. We just
// let it pass through, per the rules for propagation of silent
// NaN. (Signaling NaN will signal, but that's OK.)
TEUCHOS_TEST_FOR_EXCEPTION(
x < minVal || x > maxVal,
std::range_error,
"realToReal<" << TypeNameTraits<OutputRealType>::name () << ", "
<< TypeNameTraits<InputRealType>::name () << ">: "
"Input value x = " << x << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to the output type.");
}
// Call as() and not static_cast, because there might not
// necessarily be a conversion defined between the two types,
// other than via as(). Definitely don't call asSafe(), because
// that could cause infinite recursion.
return as<OutputRealType> (x);
}
/// \brief Convert the given string to a RealType floating-point number.
///
/// Helper function for converting the given \c std::string to a
/// real-valued floating-point number of type RealType.
///
/// \tparam RealType The real-valued floating-point type of the
/// return value. We always assume that RealType is default
/// constructible and that <tt>operator>>(std::istream&,
/// RealType&)</tt> is defined. We also assume that <tt>t !=
/// 0</tt> is a well-formed Boolean expression for t of type
/// RealType.
///
/// \param t [in] The string to convert.
///
/// \param rawConvert [in] If not NULL, this must be one of the
/// following C standard library functions: strtod, strtof, or
/// strtold. (strtof and strtold require C99 support.) In that
/// case, we use this function to read the value from the input
/// string. If NULL, we use <tt>operator>>(std::istream&,
/// RealType&)</tt> to read the value from the string (via
/// std::istringstream).
///
/// \param realTypeName [in] Human-readable string which is the name
/// of RealType.
template<class RealType>
RealType
stringToReal (const std::string& t,
RealType (*rawConvert) (const char*, char**),
const char* realTypeName)
{
if (rawConvert == NULL) {
std::istringstream in (t);
RealType out;
in >> out;
return out;
}
else {
char* endptr = NULL;
// Keep the pointer, because std::string doesn't necessarily
// guarantee that this is the same across calls to c_str(), does
// it? Or perhaps it does...
const char* t_ptr = t.c_str ();
// We preset errno to 0, to distinguish success or failure after
// calling strtoull. Most implementations of the C standard
// library written with threads in mind have errno be a macro that
// expands to thread-local storage. Thanks to the Linux
// documentation for strtod ("man 3 strtod", Red Hat Enterprise
// Linux 5) for advice with the following checks.
errno = 0;
const RealType val = rawConvert (t_ptr, &endptr);
TEUCHOS_TEST_FOR_EXCEPTION(
errno == ERANGE && (val != 0),
std::range_error,
"Teuchos::ValueTypeConversionTraits<" << realTypeName
<< ", std::string>::convert: "
"The value in the given string \"" << t << "\" overflows "
<< realTypeName << ".");
//
// mfh 20 Nov 2012: Should we treat underflow as an error?
//
TEUCHOS_TEST_FOR_EXCEPTION(
errno == ERANGE && val == 0,
std::invalid_argument,
"Teuchos::ValueTypeConversionTraits<" << realTypeName
<< ", std::string>::convert: "
"The value in the given string \"" << t << "\" underflows "
<< realTypeName << ".");
TEUCHOS_TEST_FOR_EXCEPTION(
endptr == t_ptr, // See above discussion of c_str().
std::invalid_argument,
"Teuchos::ValueTypeConversionTraits<" << realTypeName
<< ", std::string>::convert: "
"The conversion function was unable to read any floating-point data "
"from the given string \"" << t << "\".");
return val;
}
}
} // namespace (anonymous)
//
// Standard specializations of ValueTypeConversionTraits
//
//
// * Partial specialization for conversion from std::string to any type T.
// There are full specializations for specific types T below.
//
/// \brief Convert an \c std::string to a type \c OutType.
///
/// This partial specialization assumes that \c OutType is default
/// constructible, and that <tt>operator>>(std::istream&,
/// OutType&)</tt> has been defined. It does no bounds checking or
/// other input validation.
///
/// If you would like to add input validation for a specific output
/// type \c OutType, please implement a full specialization. We
/// include many different full specializations in this file, so
/// please check this file first to see if we have already done the
/// work for you.
template<class OutType>
class ValueTypeConversionTraits<OutType, std::string> {
public:
static OutType safeConvert (const std::string& t) {
return convert (t);
}
static OutType convert (const std::string& t) {
std::istringstream in (t);
OutType out;
in >> out;
return out;
}
};
//
// * Specializations for conversions from std::string to build-in
// real-valued floating-point types.
//
//! Convert an \c std::string to a \c double.
template<>
class ValueTypeConversionTraits<double, std::string> {
public:
static double convert (const std::string& t) {
return stringToReal<double> (t, &strtod, "double");
}
static double safeConvert (const std::string& t) {
return stringToReal<double> (t, &strtod, "double");
}
};
//! Convert an \c std::string to a \c float.
template<>
class ValueTypeConversionTraits<float, std::string> {
public:
static float convert (const std::string& t) {
#ifdef _ISOC99_SOURCE
return stringToReal<float> (t, &strtof, "float");
#else
// strtof is new in C99. If you don't have it, just use strtod
// and convert the resulting double to float.
const double d = stringToReal<double> (t, &strtod, "double");
return realToReal<float, double> (d, false);
#endif // _ISOC99_SOURCE
}
static float safeConvert (const std::string& t) {
#ifdef _ISOC99_SOURCE
return stringToReal<float> (t, &strtof, "float");
#else
// strtof is new in C99. If you don't have it, just use strtod
// and convert the resulting double to float.
const double d = stringToReal<double> (t, &strtod, "double");
return realToReal<float, double> (d, true);
#endif // _ISOC99_SOURCE
}
};
//! Convert an \c std::string to a <tt>long double</tt>.
template<>
class ValueTypeConversionTraits<long double, std::string> {
public:
static long double convert (const std::string& t) {
#ifdef _ISOC99_SOURCE
return stringToReal<long double> (t, &strtold, "long double");
#else
// strtof is new in C99. If you don't have it, just use
// operator>>(std::istream&, long double&).
return stringToReal<long double> (t, NULL, "long double");
#endif // _ISOC99_SOURCE
}
static long double safeConvert (const std::string& t) {
return convert (t);
}
};
//
// * Specializations for conversions from std::string to build-in integer types.
//
#ifdef HAVE_TEUCHOS_LONG_LONG_INT
/// \brief Convert an \c std::string to a <tt>long long</tt>.
///
/// We assume the string stores a base-10 integer, if it stores an integer at all.
template<>
class ValueTypeConversionTraits<long long, std::string> {
public:
/// \brief Convert the given \c std::string to a <tt>long long</tt>, with checks.
///
/// If the string overflows <tt>long long</tt>, this throws
/// <tt>std::range_error</tt>. If it does not contain an integer,
/// this throws <tt>std::invalid_argument</tt>.
static long long safeConvert (const std::string& t) {
#if defined(_MSC_VER)
// Windows does not implement strtoull, so we resort to a
// fallback. Thanks to Ross Bartlett for pointing out _strtoi64.
// See the MSDN reference [last accessed 21 Mar 2013]:
//
// http://msdn.microsoft.com/en-us/library/h80404d3%28v=vs.80%29.aspx
return intToString<long long> (t, &_strtoi64, "long long");
#else
return intToString<long long> (t, &strtoll, "long long");
#endif // defined(_MSC_VER)
}
//! Convert the given \c std::string to a <tt>long long</tt>.
static long long convert (const std::string& t) {
return safeConvert (t);
}
};
/// \brief Convert an \c std::string to an <tt>unsigned long long</tt>.
///
/// We assume the string stores a base-10 integer, if it stores an integer at all.
template<>
class ValueTypeConversionTraits<unsigned long long, std::string> {
public:
/// \brief Convert the given \c std::string to an <tt>unsigned long long</tt>, with checks.
///
/// If the string overflows <tt>unsigned long long</tt>, this throws
/// <tt>std::range_error</tt>. If it does not contain an integer,
/// this throws <tt>std::invalid_argument</tt>.
static unsigned long long safeConvert (const std::string& t) {
#if defined(_MSC_VER)
// Windows does not implement strtoull, so we resort to a
// fallback. The fallback does not know how to check for under-
// or overflow. Alas, Windows does not seem to have an equivalent
// of _strtoi64 for unsigned long long.
const char intTypeName[] = "unsigned long long";
std::istringstream istr (t);
unsigned long long i = 0;
istr >> i;
TEUCHOS_TEST_FOR_EXCEPTION(
! istr, std::invalid_argument,
"Teuchos::ValueTypeConversionTraits<" << intTypeName << ", std::string>::"
"convert: Unable to convert the given string \"" << t << "\" to " <<
intTypeName << ". Windows lacks strtoull(), so we had to resort to a "
"fall-back conversion. The fall-back method does not know how to test "
"for overflow.");
return i;
#else
return intToString<unsigned long long> (t, &strtoull, "unsigned long long");
#endif // defined(_MSC_VER)
}
//! Convert the given \c std::string to an <tt>unsigned long long</tt>.
static unsigned long long convert (const std::string& t) {
return safeConvert (t);
}
};
#endif // HAVE_TEUCHOS_LONG_LONG_INT
/// \brief Convert an \c std::string to a \c long.
///
/// We assume the string stores a base-10 integer, if it stores an integer at all.
template<>
class ValueTypeConversionTraits<long, std::string> {
public:
/// \brief Convert the given \c std::string to a \c long, with checks.
///
/// If the string overflows <tt>long</tt>, this throws
/// <tt>std::range_error</tt>. If it does not contain an integer,
/// this throws <tt>std::invalid_argument</tt>.
static long safeConvert (const std::string& t) {
return intToString<long> (t, &strtol, "long");
}
//! Convert the given \c std::string to a \c long.
static long convert (const std::string& t) {
return safeConvert (t);
}
};
/// \brief Convert an \c std::string to an <tt>unsigned long</tt>.
///
/// We assume the string stores a base-10 integer, if it stores an integer at all.
template<>
class ValueTypeConversionTraits<unsigned long, std::string> {
public:
/// \brief Convert the given std::string to an <tt>unsigned long</tt>, with checks.
///
/// If the string overflows <tt>unsigned long</tt>, this throws
/// <tt>std::range_error</tt>. If it does not contain an integer,
/// this throws <tt>std::invalid_argument</tt>.
static unsigned long safeConvert (const std::string& t) {
return intToString<unsigned long> (t, &strtoul, "unsigned long");
}
//! Convert the given \c std::string to an <tt>unsigned long</tt>.
static unsigned long convert (const std::string& t) {
return safeConvert (t);
}
};
// Windows size_t is not unsigned long(32 bit), but unsigned long long(64 bit)
#ifdef HAVE_TEUCHOS___INT64
/// \brief Convert an \c std::string to an <tt>unsigned __int64</tt>.
///
/// We assume the string stores a base-10 integer, if it stores an integer at all.
template<>
class ValueTypeConversionTraits<unsigned __int64, std::string> {
public:
/// \brief Convert the given std::string to an <tt>unsigned long long</tt>, with checks.
///
/// If the string overflows <tt>unsigned __int64</tt>, this throws
/// <tt>std::range_error</tt>. If it does not contain an integer,
/// this throws <tt>std::invalid_argument</tt>.
static unsigned __int64 safeConvert(const std::string& t) {
unsigned __int64 output;
std::istringstream stream(t);
stream >> output;
return output;
}
//! Convert the given \c std::string to an <tt>unsigned __int64</tt>.
static unsigned __int64 convert(const std::string& t) {
unsigned __int64 output;
std::istringstream stream(t);
stream >> output;
return output;
}
};
#endif // HAVE_TEUCHOS___INT64
/// \brief Convert an \c std::string to an \c int.
///
/// We assume the string stores a base-10 integer, if it stores an integer at all.
template<>
class ValueTypeConversionTraits<int, std::string> {
private:
//! Convert the given \c std::string to an intermediate \c long, with checks.
static long safeConvertToLong (const std::string& t) {
long val = 0;
try {
val = ValueTypeConversionTraits<long, std::string>::safeConvert (t);
} catch (std::range_error&) {
TEUCHOS_TEST_FOR_EXCEPTION(
true,
std::range_error,
"Teuchos::ValueTypeConversionTraits<int, std::string>::convert: "
"The given std::string \"" << t << "\" is too big to fit into long, so there is no way it could fit into int.");
} catch (std::invalid_argument& e) {
TEUCHOS_TEST_FOR_EXCEPTION(
true,
std::invalid_argument,
"Teuchos::ValueTypeConversionTraits<int, std::string>::convert: "
"Intermediate conversion from std::string to long failed, with the following error message: "
<< e.what ());
}
return val;
}
public:
/// \brief Convert the given \c std::string to an <tt>int</tt>, with checks.
///
/// If the string overflows <tt>int</tt>, this throws
/// <tt>std::range_error</tt>. If it does not contain an integer,
/// this throws <tt>std::invalid_argument</tt>.
static int safeConvert (const std::string& t) {
return asSafe<int> (safeConvertToLong (t));
}
//! Convert the given \c std::string to an \c int.
static int convert (const std::string& t) {
return as<int> (safeConvertToLong (t));
}
};
/// \brief Convert an \c std::string to an <tt>unsigned int</tt>.
///
/// We assume the string stores a base-10 integer, if it stores an integer at all.
template<>
class ValueTypeConversionTraits<unsigned int, std::string> {
private:
//! Convert the given \c std::string to an intermediate <tt>unsigned long</tt>, with checks.
static unsigned long safeConvertToUnsignedLong (const std::string& t) {
unsigned long val = 0;
try {
val = as<unsigned long> (t);
} catch (std::range_error&) {
TEUCHOS_TEST_FOR_EXCEPTION(
true,
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned int, std::string>::convert: "
"The given std::string \"" << t << "\" is too big to fit into unsigned long, so there is no way it could fit into unsigned int.");
} catch (std::invalid_argument& e) {
TEUCHOS_TEST_FOR_EXCEPTION(
true,
std::invalid_argument,
"Teuchos::ValueTypeConversionTraits<unsigned int, std::string>::convert: "
"Intermediate conversion from std::string to unsigned long failed, with the following error message: "
<< e.what ());
}
return val;
}
public:
/// \brief Convert the given \c std::string to an <tt>unsigned int</tt>, with checks.
///
/// If the string overflows <tt>unsigned int</tt>, this throws
/// <tt>std::range_error</tt>. If it does not contain an integer,
/// this throws <tt>std::invalid_argument</tt>.
static unsigned int safeConvert (const std::string& t) {
return asSafe<unsigned int> (safeConvertToUnsignedLong (t));
}
//! Convert the given \c std::string to an <tt>unsigned int</tt>.
static unsigned int convert (const std::string& t) {
return as<unsigned int> (safeConvertToUnsignedLong (t));
}
};
/// \brief Convert an \c std::string to a \c short.
///
/// We assume the string stores a base-10 integer, if it stores an integer at all.
template<>
class ValueTypeConversionTraits<short, std::string> {
private:
//! Convert the given \c std::string to an intermediate \c long, with checks.
static long safeConvertToLong (const std::string& t) {
long val = 0;
try {
val = ValueTypeConversionTraits<long, std::string>::safeConvert (t);
} catch (std::range_error&) {
TEUCHOS_TEST_FOR_EXCEPTION(
true,
std::range_error,
"Teuchos::ValueTypeConversionTraits<short, std::string>::convert: "
"The given std::string \"" << t << "\" is too big to fit into long, so there is no way it could fit into short.");
} catch (std::invalid_argument& e) {
TEUCHOS_TEST_FOR_EXCEPTION(
true,
std::invalid_argument,
"Teuchos::ValueTypeConversionTraits<short, std::string>::convert: "
"Intermediate conversion from std::string to long failed, with the following error message: "
<< e.what ());
}
return val;
}
public:
/// \brief Convert the given \c std::string to a <tt>short</tt>, with checks.
///
/// If the string overflows <tt>short</tt>, this throws
/// <tt>std::range_error</tt>. If it does not contain an integer,
/// this throws <tt>std::invalid_argument</tt>.
static short safeConvert (const std::string& t) {
return asSafe<short> (safeConvertToLong (t));
}
//! Convert the given \c std::string to a \c short.
static short convert (const std::string& t) {
return as<short> (safeConvertToLong (t));
}
};
/// \brief Convert an \c std::string to an <tt>unsigned short</tt>.
///
/// We assume the string stores a base-10 integer, if it stores an integer at all.
template<>
class ValueTypeConversionTraits<unsigned short, std::string> {
private:
//! Convert the given \c std::string to an intermediate <tt>unsigned long</tt>, with checks.
static unsigned long safeConvertToUnsignedLong (const std::string& t) {
unsigned long val = 0;
try {
val = as<unsigned long> (t);
} catch (std::range_error&) {
TEUCHOS_TEST_FOR_EXCEPTION(
true,
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned short, std::string>::convert: "
"The given std::string \"" << t << "\" is too big to fit into unsigned long, so there is no way it could fit into unsigned short.");
} catch (std::invalid_argument& e) {
TEUCHOS_TEST_FOR_EXCEPTION(
true,
std::invalid_argument,
"Teuchos::ValueTypeConversionTraits<unsigned short, std::string>::convert: "
"Intermediate conversion from std::string to unsigned long failed, with the following error message: "
<< e.what ());
}
return val;
}
public:
/// \brief Convert the given \c std::string to an <tt>unsigned short</tt>, with checks.
///
/// If the string overflows <tt>unsigned short</tt>, this throws
/// <tt>std::range_error</tt>. If it does not contain an integer,
/// this throws <tt>std::invalid_argument</tt>.
static unsigned short safeConvert (const std::string& t) {
return asSafe<unsigned short> (safeConvertToUnsignedLong (t));
}
//! Convert the given \c std::string to an <tt>unsigned short</tt>.
static unsigned short convert (const std::string& t) {
return as<unsigned short> (safeConvertToUnsignedLong (t));
}
};
//
// * Specializations for conversions between built-in real-valued
// floating-point types (like float and double).
//
//! Convert from \c double to \c float.
template<>
class ValueTypeConversionTraits<float, double> {
public:
static float safeConvert (const double t) {
// mfh 25 Nov 2012: Disabling bounds checking, in favor of IEEE
// 754 overflow semantics. Users who want bounds checking should
// set the appropriate trap.
#if 0
// For floating-point types T, std::numeric_limits<T>::min()
// returns the smallest positive value. IEEE 754 types have a
// sign bit, so the largest-magnitude negative value is the
// negative of the largest-magnitude positive value.
const float minVal = -std::numeric_limits<float>::max ();
const float maxVal = std::numeric_limits<float>::max ();
// NaN is neither less than nor greater than anything. We just
// let it pass through, per the rules for propagation of silent
// NaN. (Signaling NaN will signal, but that's OK.)
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<float, double>::safeConvert: "
"Input double t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to float.");
#endif // 0
return static_cast<float> (t);
}
static float convert (const double t) {
return static_cast<float> (t);
}
};
//! Convert from <tt>long double</tt> to \c float.
template<>
class ValueTypeConversionTraits<float, long double> {
public:
static float safeConvert (const long double t) {
// mfh 25 Nov 2012: Disabling bounds checking, in favor of IEEE
// 754 overflow semantics. Users who want bounds checking should
// set the appropriate trap.
#if 0
// For floating-point types T, std::numeric_limits<T>::min()
// returns the smallest positive value. IEEE 754 types have a
// sign bit, so the largest-magnitude negative value is the
// negative of the largest-magnitude positive value.
const float minVal = -std::numeric_limits<float>::max ();
const float maxVal = std::numeric_limits<float>::max ();
// NaN is neither less than nor greater than anything. We just
// let it pass through, per the rules for propagation of silent
// NaN. (Signaling NaN will signal, but that's OK.)
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<float, long double>::safeConvert: "
"Input long double t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to float.");
#endif // 0
return static_cast<float> (t);
}
static float convert (const long double t) {
return static_cast<float> (t);
}
};
//! Convert from <tt>long double</tt> to \c double.
template<>
class ValueTypeConversionTraits<double, long double> {
public:
static double safeConvert (const long double t) {
// mfh 25 Nov 2012: Disabling bounds checking, in favor of IEEE
// 754 overflow semantics. Users who want bounds checking should
// set the appropriate trap.
#if 0
// For floating-point types T, std::numeric_limits<T>::min()
// returns the smallest positive value. IEEE 754 types have a
// sign bit, so the largest-magnitude negative value is the
// negative of the largest-magnitude positive value.
const double minVal = -std::numeric_limits<double>::max ();
const double maxVal = std::numeric_limits<double>::max ();
// NaN is neither less than nor greater than anything. We just
// let it pass through, per the rules for propagation of silent
// NaN. (Signaling NaN will signal, but that's OK.)
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<double, long double>::safeConvert: "
"Input long double t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to double.");
#endif // 0
return static_cast<double> (t);
}
static double convert (const long double t) {
return static_cast<double> (t);
}
};
//
// * Specializations for conversions from built-in real-valued
// floating-point types (float and double) to build-in integer
// types.
//
//! Convert from \c double to \c short.
template<>
class ValueTypeConversionTraits<short, double> {
public:
/// \brief Convert the given \c double to a \c short.
///
/// \warning Double-precision floating-point values may overflow
/// <tt>short</tt>. You should use safeConvert() if you aren't sure
/// that the given value fits in an <tt>short</tt>.
static short convert (const double t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<short> (t);
}
//! Convert the given \c double to a \c short, checking for overflow first.
static short safeConvert (const double t) {
const short minVal = std::numeric_limits<short>::min ();
const short maxVal = std::numeric_limits<short>::max ();
// Cases:
// 1. sizeof(short) < sizeof(double) == 8
// 2. sizeof(short) == sizeof(double) == 8
// 3. sizeof(short) > sizeof(double) == 8
//
// Overflow when converting from double to short is possible only
// for Case 1. Loss of accuracy (rounding) is possible for Cases
// 2 and 3, but safeConvert() only cares about overflow, not
// rounding. In Case 3, casting minVal or maxVal to double in
// this case could result in overflow. Thus, we only do the test
// for Case 1.
//
// All three cases are legal according to both C++03 and C99.
// However, I (mfh 15 Nov 2012) have never encountered Cases 2 and
// 3.
if (sizeof (short) < sizeof (double)) {
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<short, double>::safeConvert: "
"Input double t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to short.");
}
return static_cast<short> (t);
}
};
//! Convert from \c double to <tt>unsigned short</tt>.
template<>
class ValueTypeConversionTraits<unsigned short, double> {
public:
//! Convert the given \c double to an <tt>unsigned short</tt>.
static unsigned short convert (const double t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<unsigned short> (t);
}
//! Convert the given \c double to an <tt>unsigned short</tt>, checking for overflow first.
static unsigned short safeConvert (const double t) {
const unsigned short minVal = 0;
const unsigned short maxVal = std::numeric_limits<unsigned short>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned short, double>::safeConvert: "
"Input double t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to unsigned short.");
return static_cast<unsigned short> (t);
}
};
//! Convert from \c double to \c int.
template<>
class ValueTypeConversionTraits<int, double> {
public:
/// \brief Convert the given \c double to an \c int.
///
/// \warning Double-precision floating-point values may overflow
/// <tt>int</tt>. You should use safeConvert() if you aren't sure
/// that the given value fits in an <tt>int</tt>.
static int convert (const double t) {
// Implicit conversion from double to int causes compiler
// warnings, but static_cast does not.
return static_cast<int> (t);
}
//! Convert the given \c double to an \c int, checking for overflow first.
static int safeConvert (const double t) {
const int minVal = std::numeric_limits<int>::min ();
const int maxVal = std::numeric_limits<int>::max ();
// Cases:
// 1. sizeof(int) < sizeof(double) == 8
// 2. sizeof(int) == sizeof(double) == 8
// 3. sizeof(int) > sizeof(double) == 8
//
// Overflow when converting from double to int is possible only
// for Case 1. Loss of accuracy (rounding) is possible for Cases
// 2 and 3, but safeConvert() only cares about overflow, not
// rounding. Case 3 is quite rare, but casting minVal or maxVal
// to double in this case could result in overflow. Thus, we only
// do the cast for Case 1.
if (sizeof (int) < sizeof (double)) {
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<int, double>::safeConvert: "
"Input double t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to int.");
}
return static_cast<int> (t);
}
};
//! Convert from \c double to <tt>unsigned int</tt>.
template<>
class ValueTypeConversionTraits<unsigned int, double> {
public:
//! Convert the given \c double to an <tt>unsigned int</tt>.
static unsigned int convert (const double t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<unsigned int> (t);
}
//! Convert the given \c double to an <tt>unsigned int</tt>, checking for overflow first.
static unsigned int safeConvert (const double t) {
const unsigned int minVal = 0;
const unsigned int maxVal = std::numeric_limits<unsigned int>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned int, double>::safeConvert: "
"Input double t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to unsigned int.");
return static_cast<unsigned int> (t);
}
};
//! Convert from \c double to \c long.
template<>
class ValueTypeConversionTraits<long, double> {
public:
//! Convert the given \c double to \c long.
static long convert (const double t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<long> (t);
}
//! Convert the given \c double to \c long, checking for overflow first.
static long safeConvert (const double t) {
const long minVal = std::numeric_limits<long>::min ();
const long maxVal = std::numeric_limits<long>::max ();
// Cases:
// 1. sizeof(long) < sizeof(double) == 8
// 2. sizeof(long) == sizeof(double) == 8
// 3. sizeof(long) > sizeof(double) == 8
//
// Overflow when converting from double to long is possible only
// for Case 1. Loss of accuracy (rounding) is possible for Cases
// 2 and 3, but safeConvert() only cares about overflow, not
// rounding. In Case 3, casting minVal or maxVal to double could
// result in overflow. Thus, we only test for Case 1.
//
// Case 1 is entirely possible, for example on Win64 (an
// implementation of the LLP64 integer model, on which
// sizeof(long) == 4, and sizeof(long long) == sizeof(void*) ==
// 8).
if (sizeof (long) < sizeof (double)) {
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<long, double>::safeConvert: "
"Input double t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to long.");
}
return static_cast<long> (t);
}
};
//! Convert from \c double to <tt>unsigned long</tt>.
template<>
class ValueTypeConversionTraits<unsigned long, double> {
public:
//! Convert the given \c double to an <tt>unsigned long</tt>.
static unsigned long convert (const double t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<unsigned long> (t);
}
//! Convert the given \c double to an <tt>unsigned long</tt>, checking for overflow first.
static unsigned long safeConvert (const double t) {
const unsigned long minVal = 0;
const unsigned long maxVal = std::numeric_limits<unsigned long>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned long, double>::safeConvert: "
"Input double t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to unsigned long.");
return static_cast<unsigned long> (t);
}
};
#ifdef HAVE_TEUCHOS_LONG_LONG_INT
//! Convert from \c double to <tt>long long</tt>.
template<>
class ValueTypeConversionTraits<long long, double> {
public:
//! Convert the given \c double to <tt>long long</tt>.
static long long convert (const double t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<long long> (t);
}
//! Convert the given \c double to <tt>long long</tt>, checking for overflow first.
static long long safeConvert (const double t) {
// Cases:
// 1. sizeof(long long) < sizeof(double) == 8
// 2. sizeof(long long) == sizeof(double) == 8
// 3. sizeof(long long) > sizeof(double) == 8
//
// C99 (which defines long long) prohibits Case 1. Case 2 could
// result in loss of accuracy (rounding), but safeConvert() only
// cares about overflow, not rounding. In Case 3, casting minVal
// or maxVal to double could result in overflow. Thus, we don't
// need to check anything here.
return static_cast<long long> (t);
}
};
//! Convert from \c double to <tt>unsigned long long</tt>.
template<>
class ValueTypeConversionTraits<unsigned long long, double> {
public:
//! Convert the given \c double to <tt>unsigned long long</tt>.
static unsigned long long convert (const double t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<unsigned long long> (t);
}
//! Convert the given \c double to <tt>unsigned long long</tt>, checking for overflow first.
static unsigned long long safeConvert (const double t) {
const unsigned long long minVal = 0; // unsigned, so min value is 0.
const unsigned long long maxVal = std::numeric_limits<unsigned long long>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned long long, double>::safeConvert: "
"Input double t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to unsigned long long.");
return static_cast<unsigned long long> (t);
}
};
#endif // HAVE_TEUCHOS_LONG_LONG_INT
//! Convert from \c float to \c short.
template<>
class ValueTypeConversionTraits<short, float> {
public:
/// \brief Convert the given \c float to a \c short.
///
/// \warning Single-precision floating-point values may overflow
/// <tt>short</tt>. You should use safeConvert() if you aren't
/// sure that the given value fits in an <tt>short</tt>.
static short convert (const float t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<short> (t);
}
//! Convert the given \c float to a \c short, checking for overflow first.
static short safeConvert (const float t) {
const short minVal = std::numeric_limits<short>::min ();
const short maxVal = std::numeric_limits<short>::max ();
// Cases:
// 1. sizeof(short) < sizeof(float) == 4
// 2. sizeof(short) == sizeof(float) == 4
// 3. sizeof(short) > sizeof(float) == 4
//
// Overflow when converting from float to short is possible only
// for Case 1. Loss of accuracy (rounding) is possible for Cases
// 2 and 3, but safeConvert() only cares about overflow, not
// rounding. In Case 3, casting minVal or maxVal to float in this
// case could result in overflow. Thus, we only do the test for
// Case 1.
//
// All three cases are legal according to both C++03 and C99. I
// (mfh 15 Nov 2012) think Case 1 is the most common, but Case 2
// is certainly reasonable. (For example, some hardware prefers
// to work only with 32-bit words, so _every_ built-in type has
// size a multiple of 4 bytes.)
if (sizeof (short) < sizeof (float)) {
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<short, float>::safeConvert: "
"Input float t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to short.");
}
return static_cast<short> (t);
}
};
//! Convert from \c float to <tt>unsigned short</tt>.
template<>
class ValueTypeConversionTraits<unsigned short, float> {
public:
//! Convert the given \c float to an <tt>unsigned short</tt>.
static unsigned short convert (const float t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<unsigned short> (t);
}
//! Convert the given \c float to an <tt>unsigned short</tt>, checking for overflow first.
static unsigned short safeConvert (const float t) {
const unsigned short minVal = 0;
const unsigned short maxVal = std::numeric_limits<unsigned short>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned short, float>::safeConvert: "
"Input float t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to unsigned short.");
return static_cast<unsigned short> (t);
}
};
//! Convert from \c float to \c int.
template<>
class ValueTypeConversionTraits<int, float> {
public:
//! Convert the given \c float to an \c int.
static int convert (const float t) {
// Implicit conversion from float to int may cause compiler
// warnings, but static_cast does not. Overflow here would mean
// that sizeof(int) < sizeof(float), which is legal, but unlikely
// on platforms of interest.
return static_cast<int> (t);
}
//! Convert the given \c float to an \c int.
static int safeConvert (const float t) {
const int minVal = std::numeric_limits<int>::min ();
const int maxVal = std::numeric_limits<int>::max ();
// Cases:
// 1. sizeof(int) < sizeof(float) == 4
// 2. sizeof(int) == sizeof(float) == 4
// 3. sizeof(int) > sizeof(float) == 4
//
// Overflow when converting from float to int is possible only for
// Case 1. Loss of accuracy (rounding) is possible for Cases 2
// and 3, but safeConvert() only cares about overflow, not
// rounding. Case 3 is rare, but casting minVal or maxVal to
// float in this case could result in loss of accuracy
// (sizeof(int) == 8 or 16) or overflow (sizeof(int) > 16). Thus,
// we only do the test for Case 1.
if (sizeof (int) < sizeof (float)) {
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<int, float>::safeConvert: "
"Input float t = " << t << " is out of the valid range ["
<< minVal << ", " << maxVal << "] for conversion to int.");
}
return static_cast<int> (t);
}
};
//! Convert from \c float to <tt>unsigned int</tt>.
template<>
class ValueTypeConversionTraits<unsigned int, float> {
public:
//! Convert the given \c float to an <tt>unsigned int</tt>.
static unsigned int convert (const float t) {
// Implicit conversion from float to unsigned int may cause
// compiler warnings, but static_cast does not.
return static_cast<unsigned int> (t);
}
//! Convert the given \c float to an <tt>unsigned int</tt>, checking first or under- or overflow.
static unsigned int safeConvert (const float t) {
const unsigned int minVal = 0; // Had better be, since it's unsigned.
const unsigned int maxVal = std::numeric_limits<unsigned int>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned int, float>::safeConvert: "
"Input double t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to unsigned int.");
return static_cast<unsigned int> (t);
}
};
//! Convert from \c float to \c long.
template<>
class ValueTypeConversionTraits<long, float> {
public:
//! Convert the given \c float to an \c long.
static long convert (const float t) {
// Implicit conversion from float to long may cause compiler
// warnings, but static_cast does not. Overflow here would mean
// that sizeof(long) < sizeof(float), which is legal, but unlikely
// on platforms of longerest.
return static_cast<long> (t);
}
//! Convert the given \c float to an \c long, checking first for overflow.
static long safeConvert (const float t) {
const long minVal = std::numeric_limits<long>::min ();
const long maxVal = std::numeric_limits<long>::max ();
// Cases:
// 1. sizeof(long) < sizeof(float) == 4
// 2. sizeof(long) == sizeof(float) == 4
// 3. sizeof(long) > sizeof(float) == 4
//
// Overflow when converting from float to long is possible only
// for Case 1. Loss of accuracy (rounding) is possible for Cases
// 2 and 3, but safeConvert() only cares about overflow, not
// rounding. Casting minVal or maxVal to double in Case 3 could
// result in overflow. Thus, we only do the cast for Case 1.
//
// I've never encountered a Case 1 platform (mfh 14 Nov 2012).
// C99 actually forbids it, though I don't think a valid C++
// compiler (for version C++03 of the language standard) needs to
// implement C99 (mfh 14 Nov 2012). Case 2 occurs in Win64
// (64-bit Windows) and other implementations of (I32L32)LLP64.
// Case 3 is common (e.g., in the (I32)LP64 integer model of
// GNU/Linux and other operating systems).
if (sizeof (long) < sizeof (float)) {
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<long, float>::safeConvert: "
"Input float t = " << t << " is out of the valid range ["
<< minVal << ", " << maxVal << "] for conversion to long.");
}
return static_cast<long> (t);
}
};
//! Convert from \c float to <tt>unsigned long</tt>.
template<>
class ValueTypeConversionTraits<unsigned long, float> {
public:
//! Convert the given \c float to an <tt>unsigned long</tt>.
static unsigned long convert (const float t) {
// Implicit conversion from float to unsigned long may cause
// compiler warnings, but static_cast does not.
return static_cast<unsigned long> (t);
}
//! Convert the given \c float to an <tt>unsigned long</tt>, checking first or under- or overflow.
static unsigned long safeConvert (const float t) {
const unsigned long minVal = 0; // Had better be, since it's unsigned.
const unsigned long maxVal = std::numeric_limits<unsigned long>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned long, float>::safeConvert: "
<< "Input float t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to unsigned long.");
return static_cast<unsigned long> (t);
}
};
#ifdef HAVE_TEUCHOS_LONG_LONG_INT
//! Convert from \c float to <tt>long long</tt>.
template<>
class ValueTypeConversionTraits<long long, float> {
public:
//! Convert the given \c float to a <tt>long long</tt>.
static long long convert (const float t) {
return static_cast<long long> (t);
}
//! Convert the given \c float to a <tt>long long</tt>, checking first for overflow.
static long long safeConvert (const float t) {
// The C99 standard (Section 5.2.4.2.1) actually requires
// sizeof(long long) >= 64, so overflow is impossible.
return static_cast<long long> (t);
}
};
//! Convert from \c float to <tt>unsigned long long</tt>.
template<>
class ValueTypeConversionTraits<unsigned long long, float> {
public:
//! Convert the given \c float to an <tt>unsigned long long</tt>.
static unsigned long long convert (const float t) {
return static_cast<unsigned long long> (t);
}
//! Convert the given \c float to an <tt>unsigned long long</tt>, checking first for overflow.
static unsigned long long safeConvert (const float t) {
const unsigned long long minVal = 0; // unsigned, so min value is 0.
const unsigned long long maxVal = std::numeric_limits<unsigned long long>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned long long, float>::safeConvert: "
"Input float t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to unsigned long long.");
return static_cast<unsigned long long> (t);
}
};
#endif // HAVE_TEUCHOS_LONG_LONG_INT
//
// * Specializations for conversions between a unsigned built-in
// integer type and the signed version of the same type (either
// direction).
//
namespace {
// Implementation of conversion from an unsigned built-in integer
// type, to an signed built-in integer type with the same number of
// bits.
template<class SignedIntType, class UnsignedIntType>
class UnsignedToSignedValueTypeConversionTraits {
public:
/// Convert from unsigned to signed.
///
/// \warning Some unsigned integer values may overflow the signed
/// integer type, resulting in a negative number when the original
/// number was positive. You should use safeConvert() if you
/// aren't sure that the given unsigned value fits in the signed
/// type.
static SignedIntType convert (const UnsignedIntType t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<SignedIntType> (t);
}
//! Convert from unsigned to signed, checking for overflow first.
static SignedIntType safeConvert (const UnsignedIntType t) {
using Teuchos::TypeNameTraits;
const SignedIntType maxSigned = std::numeric_limits<SignedIntType>::max ();
// SignedIntType and UnsignedIntType have the same number of bits,
// so it suffices (via two's complement arithmetic) to check
// whether the cast turned a positive number negative.
const SignedIntType signedVal = static_cast<SignedIntType> (t);
TEUCHOS_TEST_FOR_EXCEPTION(
signedVal < 0,
std::range_error,
"Teuchos::ValueTypeConversionTraits<" << TypeNameTraits<SignedIntType>::name ()
<< ", " << TypeNameTraits<UnsignedIntType>::name () << ">::safeConvert: "
"Input " << TypeNameTraits<UnsignedIntType>::name () << " t = " << t
<< " is out of the valid range [0, " << ", " << maxSigned
<< "] for conversion to " << TypeNameTraits<SignedIntType>::name () << ".");
return signedVal;
}
};
// Implementation of conversion from a signed built-in integer type,
// to an unsigned built-in integer type with the same number of bits.
template<class UnsignedIntType, class SignedIntType>
class SignedToUnsignedValueTypeConversionTraits {
public:
//! Convert the given unsigned integer to a signed integer of the same size.
static UnsignedIntType convert (const SignedIntType t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<UnsignedIntType> (t);
}
//! Convert from signed to unsigned, checking for underflow first.
static UnsignedIntType safeConvert (const SignedIntType t) {
using Teuchos::TypeNameTraits;
// SignedIntType and UnsignedIntType have the same number of bits,
// so it suffices (via two's complement arithmetic) to check
// whether the input is negative.
TEUCHOS_TEST_FOR_EXCEPTION(
t < static_cast<SignedIntType> (0),
std::range_error,
"Teuchos::ValueTypeConversionTraits<" << TypeNameTraits<UnsignedIntType>::name ()
<< ", " << TypeNameTraits<SignedIntType>::name () << ">::safeConvert: "
"Input " << TypeNameTraits<SignedIntType>::name () << " t = " << t
<< " is negative, so it cannot be correctly converted to the unsigned type "
<< TypeNameTraits<UnsignedIntType>::name () << ".");
return static_cast<UnsignedIntType> (t);
}
};
} // namespace (anonymous)
//! Convert from <tt>unsigned short<tt> to \c short.
template<>
class ValueTypeConversionTraits<short, unsigned short> {
public:
static short convert (const unsigned short t) {
return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::convert (t);
}
static short safeConvert (const unsigned short t) {
return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::safeConvert (t);
}
};
//! Convert from <tt>short<tt> to <tt>unsigned short</tt>.
template<>
class ValueTypeConversionTraits<unsigned short, short> {
public:
static unsigned short convert (const short t) {
return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::convert (t);
}
static unsigned short safeConvert (const short t) {
return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::safeConvert (t);
}
};
//! Convert from <tt>unsigned int<tt> to \c int.
template<>
class ValueTypeConversionTraits<int, unsigned int> {
public:
static int convert (const unsigned int t) {
return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::convert (t);
}
static int safeConvert (const unsigned int t) {
return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::safeConvert (t);
}
};
//! Convert from <tt>int<tt> to <tt>unsigned int</tt>.
template<>
class ValueTypeConversionTraits<unsigned int, int> {
public:
static unsigned int convert (const int t) {
return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::convert (t);
}
static unsigned int safeConvert (const int t) {
return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::safeConvert (t);
}
};
//! Convert from <tt>unsigned long<tt> to \c long.
template<>
class ValueTypeConversionTraits<long, unsigned long> {
public:
static long convert (const unsigned long t) {
return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::convert (t);
}
static long safeConvert (const unsigned long t) {
return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::safeConvert (t);
}
};
//! Convert from <tt>long<tt> to <tt>unsigned long</tt>.
template<>
class ValueTypeConversionTraits<unsigned long, long> {
public:
static unsigned long convert (const long t) {
return SignedToUnsignedValueTypeConversionTraits<unsigned long, long>::convert (t);
}
static unsigned long safeConvert (const long t) {
return SignedToUnsignedValueTypeConversionTraits<unsigned long, long>::safeConvert (t);
}
};
#ifdef HAVE_TEUCHOS_LONG_LONG_INT
//! Convert from <tt>unsigned long long<tt> to <tt>long long</tt>.
template<>
class ValueTypeConversionTraits<long long, unsigned long long> {
public:
static long long convert (const unsigned long long t) {
return UnsignedToSignedValueTypeConversionTraits<long long, unsigned long long>::convert (t);
}
static long long safeConvert (const unsigned long long t) {
return UnsignedToSignedValueTypeConversionTraits<long long, unsigned long long>::safeConvert (t);
}
};
//! Convert from <tt>long long<tt> to <tt>unsigned long long</tt>.
template<>
class ValueTypeConversionTraits<unsigned long long, long long> {
public:
static unsigned long long convert (const long long t) {
return SignedToUnsignedValueTypeConversionTraits<unsigned long long, long long>::convert (t);
}
static unsigned long long safeConvert (const long long t) {
return SignedToUnsignedValueTypeConversionTraits<unsigned long long, long long>::safeConvert (t);
}
};
#endif // HAVE_TEUCHOS_LONG_LONG_INT
//
// * Specializations for conversions between different built-in
// integer types.
//
//! Convert from \c int to \c short.
template<>
class ValueTypeConversionTraits<short, int> {
public:
/// \brief Convert the given \c int to a \c short.
///
/// \warning \c int values may overflow \c short, depending on your
/// platform. You should use safeConvert() if you aren't sure
/// that the given \c int value fits in a \c short.
static short convert (const int t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<short> (t);
}
//! Convert from \c int to \c short, checking for overflow first.
static short safeConvert (const int t) {
const short minShort = std::numeric_limits<short>::min ();
const short maxShort = std::numeric_limits<short>::max ();
// Casting from short to int never overflows, since the C++
// standard guarantees that sizeof (short) <= sizeof (int).
TEUCHOS_TEST_FOR_EXCEPTION(
t < static_cast<int> (minShort) ||
t > static_cast<int> (maxShort),
std::range_error,
"Teuchos::ValueTypeConversionTraits<short, int>::safeConvert: "
"Input int t = " << t << " is out of the valid range [" << minShort
<< ", " << maxShort << "] for conversion to short.");
return static_cast<short> (t);
}
};
//! Convert from \c long to \c short.
template<>
class ValueTypeConversionTraits<short, long> {
public:
/// \brief Convert the given \c long to a \c short.
///
/// \warning \c long integer values may overflow \c short, depending
/// on your platform. You should use safeConvert() if you aren't
/// sure that the given \c long value fits in a \c short.
static short convert (const long t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<short> (t);
}
//! Convert from \c long to \c short, checking for overflow first.
static short safeConvert (const long t) {
const short minShort = std::numeric_limits<short>::min ();
const short maxShort = std::numeric_limits<short>::max ();
// Casting from short to long never overflows, since the C++
// standard guarantees that sizeof (short) <= sizeof (long).
TEUCHOS_TEST_FOR_EXCEPTION(
t < static_cast<long> (minShort) ||
t > static_cast<long> (maxShort),
std::range_error,
"Teuchos::ValueTypeConversionTraits<short, long>::safeConvert: "
"Input long t = " << t << " is out of the valid range [" << minShort
<< ", " << maxShort << "] for conversion to short.");
return static_cast<short> (t);
}
};
//! Convert from \c long to \c int.
template<>
class ValueTypeConversionTraits<int, long> {
public:
/// \brief Convert the given \c long to an \c int.
///
/// \warning \c long integer values may overflow \c int, depending
/// on your platform. You should use safeConvert() if you aren't
/// sure that the given \c long value fits in an \c int.
static int convert (const long t) {
// Implicit conversion from long to int may cause compiler
// warnings, but static_cast does not.
return static_cast<int> (t);
}
//! Convert from \c long to \c int, checking for overflow first.
static int safeConvert (const long t) {
const int minInt = std::numeric_limits<int>::min ();
const int maxInt = std::numeric_limits<int>::max ();
// Casting from int to long never overflows, since the C++
// standard guarantees that sizeof (int) <= sizeof (long).
TEUCHOS_TEST_FOR_EXCEPTION(
t < static_cast<long> (minInt) ||
t > static_cast<long> (maxInt),
std::range_error,
"Teuchos::ValueTypeConversionTraits<int, long>::safeConvert: "
"Input long t = " << t << " is out of the valid range [" << minInt
<< ", " << maxInt << "] for conversion to int.");
// Implicit conversion from long to int may cause compiler
// warnings, but static_cast does not.
return static_cast<int> (t);
}
};
//! Convert from <tt>unsigned long</tt> to \c int.
template<>
class ValueTypeConversionTraits<int, unsigned long> {
public:
/// \brief Convert the given <tt>unsigned long</tt> to an \c int.
///
/// \warning <tt>unsigned long</tt> values may overflow
/// <tt>int</tt>, depending on your platform. You should use
/// safeConvert() if you aren't sure that the given <tt>unsigned
/// long</tt> value fits in an <tt>int</tt>.
static int convert (const unsigned long t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<int> (t);
}
//! Convert from <tt>unsigned long</tt> to \c int, checking for overflow first.
static int safeConvert (const unsigned long t) {
const int minInt = std::numeric_limits<int>::min ();
const int maxInt = std::numeric_limits<int>::max ();
// On some platforms, sizeof(int) == sizeof(long). (This is the
// "LLP64" model of Win64, which aims for backwards compatibility
// with 32-bit code by making sizeof(int) == sizeof(long) == 4.)
// If this is the case, then we can't safely cast unsigned long to
// int, or unsigned int to long, because values with the most
// significant bit set will overflow to negative values.
// The C++ standard promises that sizeof (int) <= sizeof (unsigned
// long). We use #if with INT_MAX and LONG_MAX to test for this,
// rather than if statements, in order to avoid a compiler
// warning. Thanks to Jeremie Gaidamour (13 Nov 2012) for letting
// me know about the warning.
#if INT_MAX == LONG_MAX
// The two types have the same number of bits. Thus,
// two's-complement arithmetic means that if casting from unsigned
// long to int results in a negative number, it overflowed.
// Otherwise, it didn't overflow (same number of bits).
TEUCHOS_TEST_FOR_EXCEPTION(
static_cast<int> (t) < static_cast<int> (0),
std::range_error,
"Teuchos::ValueTypeConversionTraits<int, unsigned long>::safeConvert: "
"Input unsigned long t = " << t << " is out of the valid range ["
<< minInt << ", " << maxInt << "] for conversion to int.");
#else // INT_MAX < LONG_MAX
// t is unsigned, so it is >= 0 by definition.
// Casting from int to unsigned long won't overflow in this case.
TEUCHOS_TEST_FOR_EXCEPTION(
t > static_cast<unsigned long> (maxInt),
std::range_error,
"Teuchos::ValueTypeConversionTraits<int, unsigned long>::safeConvert: "
"Input unsigned long t = " << t << " is out of the valid range ["
<< minInt << ", " << maxInt << "] for conversion to int. An unchecked "
"cast would have resulted in " << static_cast<int> (t) << ".");
#endif // INT_MAX == LONG_MAX
// Implicit conversion from unsigned long to int may cause
// compiler warnings, but static_cast does not.
return static_cast<int> (t);
}
};
//! Convert from <tt>unsigned int</tt> to <tt>long</tt>.
template<>
class ValueTypeConversionTraits<long, unsigned int> {
public:
/// \brief Convert the given <tt>unsigned int</tt> to a \c long.
///
/// \warning On some platforms (e.g., Windows, or any other platform
/// that implements the LLP64 model), <tt>unsigned int</tt>
/// integer values may overflow <tt>long</tt>. You should use
/// safeConvert() if you aren't sure that the given <tt>unsigned
/// int</tt> value fits in a <tt>long</tt>.
static long convert (const unsigned int t) {
// Implicit conversion may cause compiler warnings, but
// static_cast does not.
return static_cast<long> (t);
}
//! Convert from <tt>unsigned int</tt> to \c long, checking for overflow first.
static long safeConvert (const unsigned int t) {
// On some platforms, sizeof(int) == sizeof(long). (This is the
// "LLP64" model of Win64, which aims for backwards compatibility
// with 32-bit code by making sizeof(int) == sizeof(long) == 4.)
// If this is the case, then we can't safely cast unsigned long to
// int, or unsigned int to long, because values with the most
// significant bit set will overflow to negative values.
// The C++ standard promises that sizeof (unsigned int) <= sizeof
// (long). If strictly less, then the conversion won't overflow.
// We protect the test with an #ifdef ... #endif to avoid compiler
// warnings like the following: "warning: comparison is always
// false due to limited range of data type".
#if UINT_MAX == LONG_MAX
const long minLong = std::numeric_limits<long>::min ();
const long maxLong = std::numeric_limits<long>::max ();
// The two types have the same number of bits. Thus,
// two's-complement arithmetic means that if casting from
// unsigned int to long results in a negative number, it
// overflowed. Otherwise, it didn't overflow (same number of
// bits).
TEUCHOS_TEST_FOR_EXCEPTION(
static_cast<long> (t) < static_cast<long> (0),
std::range_error,
"Teuchos::ValueTypeConversionTraits<long, unsigned int>::safeConvert: "
"Input unsigned int t = " << t << " is out of the valid range ["
<< minLong << ", " << maxLong << "] for conversion to long.");
#endif // UINT_MAX == LONG_MAX
return static_cast<long> (t);
}
};
//! Convert from \c long to <tt>unsigned int</tt>.
template<>
class ValueTypeConversionTraits<unsigned int, long> {
public:
/// \brief Convert the given \c long to an <tt>unsigned int</tt>.
///
/// \warning \c long integer values may overflow <tt>unsigned
/// int</tt>, depending on your platform. You should use
/// safeConvert() if you aren't sure that the given \c long value
/// fits in an <tt>unsigned int</tt>.
static unsigned int convert (const long t) {
// Implicit conversion from long to unsigned int may cause
// compiler warnings, but static_cast does not.
return static_cast<unsigned int> (t);
}
//! Convert from \c long to <tt>unsigned int</tt>, checking for underflow or overflow first.
static unsigned int safeConvert (const long t) {
// On some platforms, sizeof(int) == sizeof(long). (This is the
// "LLP64" model of Win64, which aims for backwards compatibility
// with 32-bit code by making sizeof(int) == sizeof(long) == 4.)
// In this case, conversion from long to unsigned int can't
// overflow.
// The C++ standard promises that sizeof (unsigned int) <= sizeof (long).
if (sizeof (unsigned int) < sizeof (long)) {
const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < static_cast<long> (0) || t > static_cast<long> (maxInt),
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned int, long>::safeConvert: "
"Input long t = " << t << " is out of the valid range [0, "
<< maxInt << "] for conversion to unsigned int.");
}
// Implicit conversion from long to unsigned int may cause
// compiler warnings, but static_cast does not.
return static_cast<unsigned int> (t);
}
};
//! Convert from <tt>unsigned long</tt> to <tt>unsigned int</tt>.
template<>
class ValueTypeConversionTraits<unsigned int, unsigned long> {
public:
/// \brief Convert the given <tt>unsigned long</tt> to an <tt>unsigned int</tt>.
///
/// \warning <tt>unsigned long</tt> integer values may overflow
/// <tt>unsigned int</tt>, depending on your platform. You should
/// use safeConvert() if you aren't sure that the given
/// <tt>unsigned long</tt> value fits in an <tt>unsigned int</tt>.
static unsigned int convert (const unsigned long t) {
// Implicit conversion from unsigned long to unsigned int may cause
// compiler warnings, but static_cast does not.
return static_cast<unsigned int> (t);
}
//! Convert from <tt>unsigned long</tt> to <tt>unsigned int</tt>, checking for overflow first.
static unsigned int safeConvert (const unsigned long t) {
const unsigned int minInt = 0; // Had better be, since it's unsigned.
const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
// t >= 0 by definition, because it is unsigned.
TEUCHOS_TEST_FOR_EXCEPTION(
t > static_cast<unsigned long> (maxInt),
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned int, unsigned long>::safeConvert: "
"Input unsigned long t = " << t << " is out of the valid range [" << minInt
<< ", " << maxInt << "] for conversion to unsigned int.");
// Implicit conversion from unsigned long to unsigned int may
// cause compiler warnings, but static_cast does not.
return static_cast<unsigned int> (t);
}
};
//! Convert from <tt>unsigned long</tt> to <tt>unsigned short</tt>.
template<>
class ValueTypeConversionTraits<unsigned short, unsigned long> {
public:
/// \brief Convert the given <tt>unsigned long</tt> to an <tt>unsigned short</tt>.
///
/// \warning <tt>unsigned long</tt> integer values may overflow
/// <tt>unsigned short</tt>, depending on your platform. You should
/// use safeConvert() if you aren't sure that the given
/// <tt>unsigned long</tt> value fits in an <tt>unsigned short</tt>.
static unsigned short convert (const unsigned long t) {
// Implicit conversion from unsigned long to unsigned short may cause
// compiler warnings, but static_cast does not.
return static_cast<unsigned short> (t);
}
//! Convert from <tt>unsigned long</tt> to <tt>unsigned short</tt>, checking for overflow first.
static unsigned short safeConvert (const unsigned long t) {
const unsigned short minShort = 0; // Had better be, since it's unsigned.
const unsigned short maxShort = std::numeric_limits<unsigned short>::max ();
// t >= 0 by definition, because it is unsigned.
TEUCHOS_TEST_FOR_EXCEPTION(
t > static_cast<unsigned long> (maxShort),
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned short, unsigned long>::safeConvert: "
"Input unsigned long t = " << t << " is out of the valid range [" << minShort
<< ", " << maxShort << "] for conversion to unsigned short.");
// Implicit conversion from unsigned long to unsigned short may
// cause compiler warnings, but static_cast does not.
return static_cast<unsigned short> (t);
}
};
#ifdef HAVE_TEUCHOS_LONG_LONG_INT
//! Convert from <tt>long long</tt> to \c int.
template<>
class ValueTypeConversionTraits<int, long long> {
public:
/// \brief Convert the given <tt>long long</tt> to an \c int.
///
/// \warning <tt>long long</tt> integer values may overflow \c int.
/// You should use safeConvert() if you aren't sure that the given
/// value fits in an \c int.
static int convert (const long long t) {
// Implicit conversion from long long to int may cause compiler
// warnings, but static_cast does not.
return static_cast<int> (t);
}
//! Convert from <tt>long long</tt> to <tt>int</tt>, checking for overflow first.
static int safeConvert (const long long t) {
const int minInt = std::numeric_limits<int>::min ();
const int maxInt = std::numeric_limits<int>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < static_cast<long long> (minInt) ||
t > static_cast<long long> (maxInt),
std::range_error,
"Teuchos::ValueTypeConversionTraits<int, long long>::safeConvert: "
"Input long long t = " << t << " is out of the valid range [" << minInt
<< ", " << maxInt << "] for conversion to int.");
// Implicit conversion from long long to int may cause compiler
// warnings, but static_cast does not.
return static_cast<int> (t);
}
};
//! Convert from <tt>long long</tt> to <tt>unsigned int</tt>.
template<>
class ValueTypeConversionTraits<unsigned int, long long> {
public:
/// \brief Convert the given <tt>long long</tt> to an <tt>unsigned int</tt>.
///
/// \warning <tt>long long</tt> integer values may overflow
/// <tt>unsigned int</tt>. You should use safeConvert() if you
/// aren't sure that the given value fits in an <tt>unsigned
/// int</tt>.
static unsigned int convert (const long long t) {
// Implicit conversion from long long to unsigned int may cause
// compiler warnings, but static_cast does not.
return static_cast<unsigned int> (t);
}
//! Convert from <tt>long long</tt> to <tt>unsigned int</tt>, checking for overflow first.
static unsigned int safeConvert (const long long t) {
const unsigned int minInt = 0; // Had better be, because it's unsigned.
const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < static_cast<long long> (minInt) || t > static_cast<long long> (maxInt),
std::range_error,
"Teuchos::ValueTypeConversionTraits<unsigned int, long long>::safeConvert: "
"Input long long t = " << t << " is out of the valid range [" << minInt
<< ", " << maxInt << "] for conversion to unsigned int.");
// Implicit conversion from long long to unsigned int may cause
// compiler warnings, but static_cast does not.
return static_cast<unsigned int> (t);
}
};
//! Convert from <tt>unsigned long long</tt> to <tt>int</tt>.
template<>
class ValueTypeConversionTraits<int, unsigned long long> {
public:
/// \brief Convert the given <tt>unsigned long long</tt> to an <tt>int</tt>.
///
/// \warning <tt>unsigned long long</tt> integer values may overflow
/// \c int. You should use safeConvert() if you aren't sure that
/// the given value fits in an \c int.
static int convert (const unsigned long long t) {
// Implicit conversion from unsigned long long to int may cause
// compiler warnings, but static_cast does not.
return static_cast<int> (t);
}
//! Convert from <tt>unsigned long long</tt> to <tt>int</tt>, checking for overflow first.
static int safeConvert (const unsigned long long t) {
const int minInt = std::numeric_limits<int>::min ();
const int maxInt = std::numeric_limits<int>::max ();
// t >= 0 by definition, because it is unsigned.
TEUCHOS_TEST_FOR_EXCEPTION(
t > static_cast<unsigned long long> (maxInt),
std::invalid_argument,
"Teuchos::ValueTypeConversionTraits<int, unsigned long long>::safeConvert: "
"Input unsigned long long t = " << t << " is out of the valid range [" << minInt
<< ", " << maxInt << "] for conversion to int.");
// Implicit conversion from unsigned long long to int may cause
// compiler warnings, but static_cast does not.
return static_cast<int> (t);
}
};
//! Convert from <tt>unsigned long long</tt> to <tt>unsigned int</tt>.
template<>
class ValueTypeConversionTraits<unsigned int, unsigned long long> {
public:
/// \brief Convert the given <tt>unsigned long long</tt> to an <tt>unsigned int</tt>.
///
/// \warning <tt>unsigned long long</tt> integer values may overflow
/// <tt>unsigned int</tt>. You should use safeConvert() if you
/// aren't sure that the given value fits in an <tt>unsigned
/// int</tt>.
static unsigned int convert (const unsigned long long t) {
// Implicit conversion from unsigned long long to unsigned int may
// cause compiler warnings, but static_cast does not.
return static_cast<unsigned int> (t);
}
//! Convert from <tt>unsigned long long</tt> to <tt>unsigned int</tt>, checking for overflow first.
static unsigned int safeConvert (const unsigned long long t) {
const unsigned int minInt = 0; // Had better be, since it's unsigned.
const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
// t >= 0 by definition, because it is unsigned.
TEUCHOS_TEST_FOR_EXCEPTION(
t > static_cast<unsigned long long> (maxInt),
std::invalid_argument,
"Teuchos::ValueTypeConversionTraits<unsigned int, unsigned long long>::safeConvert: "
"Input unsigned long long t = " << t << " is out of the valid range [" << minInt
<< ", " << maxInt << "] for conversion to unsigned int.");
// Implicit conversion from unsigned long long to unsigned int may
// cause compiler warnings, but static_cast does not.
return static_cast<unsigned int> (t);
}
};
#endif // HAVE_TEUCHOS_LONG_LONG_INT
//
// * Conversions from built-in integer types to built-in real-valued
// floating-point types.
//
#ifdef HAVE_TEUCHOS_LONG_LONG_INT
//! Convert from <tt>long long</tt> to \c float.
template<>
class ValueTypeConversionTraits<float, long long> {
public:
/// \brief Convert the given <tt>long long</tt> to a \c float.
///
/// \warning <tt>long long</tt> integer values may overflow
/// <tt>float</tt>. You should use safeConvert() if you aren't
/// sure that the given value fits in a <tt>float</tt>.
static float convert (const long long t) {
// Implicit conversion from long long to float may cause compiler
// warnings, but static_cast does not.
return static_cast<float> (t);
}
//! Convert from <tt>long long</tt> to \c float, checking for overflow first.
static float safeConvert (const long long t) {
// std::numeric_limits<float>::min() gives the minimum _positive_
// normalized value of type float. IEEE 754 floating-point values
// can change sign just by flipping the sign bit, so the "most
// negative" finite float is just the negative of the "most
// positive" finite float.
const float minFloat = -std::numeric_limits<float>::max ();
const float maxFloat = std::numeric_limits<float>::max ();
// mfh 16 Nov 2012: On my platform (gcc 4.7.2, Red Hat Linux 5,
// Intel x86_64), first casting [minFloat,maxFloat] to long long
// (so that the comparison only compares long long values)
// gives different results in the comparison below than just
// comparing t (as a long long) with minFloat and maxFloat. It
// doesn't matter whether you use static_cast<long long> (...) or
// (long long) (...) to do the cast: the original float interval
// of [-3.40282e+38, 3.40282e+38] becomes [-9223372036854775808,
// -9223372036854775808], which is obviously wrong.
TEUCHOS_TEST_FOR_EXCEPTION(
t < minFloat || t > maxFloat,
std::range_error,
"Teuchos::ValueTypeConversionTraits<float, long long>::safeConvert: "
"Input long long t = " << t << " is out of the valid range [" << minFloat
<< ", " << maxFloat << "] for conversion to float.");
// Implicit conversion from long long to float may cause compiler
// warnings, but static_cast does not.
return static_cast<float> (t);
}
};
//! Convert from <tt>unsigned long long</tt> to \c float.
template<>
class ValueTypeConversionTraits<float, unsigned long long> {
public:
/// \brief Convert the given <tt>unsigned long long</tt> to a \c float.
///
/// \warning <tt>unsigned long long</tt> integer values may overflow
/// \c float. You should use safeConvert() if you aren't sure
/// that the given value fits in a \c float.
static float convert (const unsigned long long t) {
// Implicit conversion from unsigned long long to float may cause
// compiler warnings, but static_cast does not.
return static_cast<float> (t);
}
//! Convert from <tt>unsigned long long</tt> to \c float, checking for overflow first.
static float safeConvert (const unsigned long long t) {
// std::numeric_limits<float>::min() gives the minimum _positive_
// normalized value of type float. IEEE 754 floating-point values
// can change sign just by flipping the sign bit, so the "most
// negative" finite float is just the negative of the "most
// positive" finite float.
const float minFloat = -std::numeric_limits<float>::max ();
const float maxFloat = std::numeric_limits<float>::max ();
// t >= 0 by definition, because it is unsigned.
//
// mfh 16 Nov 2012: See my note above on the <float, long long>
// specialization that explains why I don't cast maxFloat to
// unsigned long long here.
TEUCHOS_TEST_FOR_EXCEPTION(
t > maxFloat,
std::invalid_argument,
"Teuchos::ValueTypeConversionTraits<float, unsigned long long>::safeConvert: "
"Input unsigned long long t = " << t << " is out of the valid range [" << minFloat
<< ", " << maxFloat << "] for conversion to float.");
// Implicit conversion from unsigned long long to float may cause
// compiler warnings, but static_cast does not.
return static_cast<float> (t);
}
};
#endif // HAVE_TEUCHOS_LONG_LONG_INT
//
// * Other conversions
//
/** \brief Convert raw C string to std::string. */
template<int N>
class ValueTypeConversionTraits<std::string, char[N]> {
public:
static std::string convert( const char t[] )
{ return std::string(t); }
static std::string safeConvert( const char t[] )
{ return std::string(t); }
};
//
// * Conversions from built-in integer types to std::complex<T>.
//
#ifdef HAVE_TEUCHOS_COMPLEX
//! Convert short to std::complex<RealType>, for any RealType.
template<class RealType>
class ValueTypeConversionTraits<std::complex<RealType>, short> {
public:
inline static std::complex<RealType> convert (const short t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, as<RealType> (0));
}
static std::complex<RealType> safeConvert (const short t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, asSafe<RealType> (0));
}
};
//! Convert unsigned short to std::complex<RealType>, for any RealType.
template<class RealType>
class ValueTypeConversionTraits<std::complex<RealType>, unsigned short> {
public:
inline static std::complex<RealType> convert (const unsigned short t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, as<RealType> (0));
}
static std::complex<RealType> safeConvert (const unsigned short t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, asSafe<RealType> (0));
}
};
//! Convert int to std::complex<RealType>, for any RealType.
template<class RealType>
class ValueTypeConversionTraits<std::complex<RealType>, int> {
public:
inline static std::complex<RealType> convert (const int t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, as<RealType> (0));
}
static std::complex<RealType> safeConvert (const int t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, asSafe<RealType> (0));
}
};
//! Convert unsigned int to std::complex<RealType>, for any RealType.
template<class RealType>
class ValueTypeConversionTraits<std::complex<RealType>, unsigned int> {
public:
inline static std::complex<RealType> convert (const unsigned int t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, as<RealType> (0));
}
static std::complex<RealType> safeConvert (const unsigned int t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, asSafe<RealType> (0));
}
};
//! Convert long to std::complex<RealType>, for any RealType.
template<class RealType>
class ValueTypeConversionTraits<std::complex<RealType>, long> {
public:
inline static std::complex<RealType> convert (const long t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, as<RealType> (0));
}
static std::complex<RealType> safeConvert (const long t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, asSafe<RealType> (0));
}
};
//! Convert unsigned long to std::complex<RealType>, for any RealType.
template<class RealType>
class ValueTypeConversionTraits<std::complex<RealType>, unsigned long> {
public:
inline static std::complex<RealType> convert (const unsigned long t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, as<RealType> (0));
}
static std::complex<RealType> safeConvert (const unsigned long t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, asSafe<RealType> (0));
}
};
#ifdef HAVE_TEUCHOS_LONG_LONG_INT
//! Convert long long to std::complex<RealType>, for any RealType.
template<class RealType>
class ValueTypeConversionTraits<std::complex<RealType>, long long> {
public:
inline static std::complex<RealType> convert (const long long t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, as<RealType> (0));
}
static std::complex<RealType> safeConvert (const long long t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, asSafe<RealType> (0));
}
};
//! Convert unsigned long long to std::complex<RealType>, for any RealType.
template<class RealType>
class ValueTypeConversionTraits<std::complex<RealType>, unsigned long long> {
public:
inline static std::complex<RealType> convert (const unsigned long long t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, as<RealType> (0));
}
static std::complex<RealType> safeConvert (const unsigned long long t) {
// Let RealType handle the conversion of the zero imaginary part.
return std::complex<RealType> (t, asSafe<RealType> (0));
}
};
#endif // HAVE_TEUCHOS_LONG_LONG_INT
#endif // HAVE_TEUCHOS_COMPLEX
//
// * Conversions for dd_real and qd_real
//
#ifdef HAVE_TEUCHOS_QD
/** \brief Convert qd_real to double. */
template <>
class ValueTypeConversionTraits<double, qd_real> {
public:
inline static double convert (const qd_real t) {
return to_double (t);
}
static double safeConvert (const qd_real t) {
// std::numeric_limits<double>::min() gives the minimum _positive_
// normalized value of type double. IEEE 754 floating-point
// values can change sign just by flipping the sign bit, so the
// "most negative" finite double is just the negative of the "most
// positive" finite double.
const qd_real minVal = -std::numeric_limits<double>::max ();
const qd_real maxVal = std::numeric_limits<double>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<double, qd_real>::safeConvert: "
"Input qd_real t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to double.");
return to_double (t);
}
};
/** \brief Convert qd_real to float. */
template <>
class ValueTypeConversionTraits<float, qd_real> {
public:
inline static float convert (const qd_real t) {
// In a debug build, this should also test the double->float
// conversion for overflow.
return as<float> (to_double (t));
}
static float safeConvert (const qd_real t) {
// std::numeric_limits<float>::min() gives the minimum _positive_
// normalized value of type float. IEEE 754 floating-point
// values can change sign just by flipping the sign bit, so the
// "most negative" finite float is just the negative of the "most
// positive" finite float.
//
// qd_real has a constructor for double, but not for float,
// so we cast to double first.
const qd_real minVal = static_cast<double> (-std::numeric_limits<float>::max ());
const qd_real maxVal = static_cast<double> (std::numeric_limits<float>::max ());
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<float, qd_real>::safeConvert: "
"Input qd_real t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to float.");
// This should also test the double->float conversion for overflow.
return asSafe<float> (to_double (t));
}
};
/** \brief Convert qd_real to int. */
template <>
class ValueTypeConversionTraits<int, qd_real> {
public:
inline static int convert (const qd_real t) {
return to_int (t);
}
static int safeConvert (const qd_real t) {
// qd_real has a constructor for int.
const qd_real minVal = std::numeric_limits<int>::min ();
const qd_real maxVal = std::numeric_limits<int>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<int, qd_real>::safeConvert: "
"Input qd_real t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to int.");
return to_int (t);
}
};
/** \brief Convert qd_real to dd_real. */
template <>
class ValueTypeConversionTraits<dd_real, qd_real> {
public:
inline static dd_real convert (const qd_real t) {
return to_dd_real(t);
}
static dd_real safeConvert (const qd_real t) {
// std::numeric_limits<dd_real>::min() gives the minimum
// _positive_ (normalized? not sure what this means for dd_real --
// mfh 14 Nov 2012) value of type dd_real. dd_real values are
// built from two IEEE 754 doubles. This means they can change
// sign just by flipping the sign bit, so the "most negative"
// finite dd_real is just the negative of the "most positive"
// finite dd_real.
//
// qd_real has a constructor for dd_real.
const qd_real minVal = -std::numeric_limits<dd_real>::max ();
const qd_real maxVal = std::numeric_limits<dd_real>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<dd_real, qd_real>::safeConvert: "
"Input qd_real t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to dd_real.");
return to_dd_real (t);
}
};
/** \brief Convert dd_real to double. */
template <>
class ValueTypeConversionTraits<double, dd_real> {
public:
inline static double convert (const dd_real t) {
return to_double (t);
}
static double safeConvert (const dd_real t) {
// std::numeric_limits<double>::min() gives the minimum _positive_
// normalized value of type double. IEEE 754 floating-point
// values can change sign just by flipping the sign bit, so the
// "most negative" finite double is just the negative of the "most
// positive" finite double.
//
// qd_real has a constructor for double.
const dd_real minVal = -std::numeric_limits<double>::max ();
const dd_real maxVal = std::numeric_limits<double>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<double, dd_real>::safeConvert: "
"Input dd_real t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to double.");
return to_double (t);
}
};
/** \brief Convert dd_real to float. */
template <>
class ValueTypeConversionTraits<float, dd_real> {
public:
inline static float convert (const dd_real t) {
// This also checks for double->float overflow in a debug build.
return as<float> (to_double (t));
}
static float safeConvert (const dd_real t) {
// std::numeric_limits<float>::min() gives the minimum _positive_
// normalized value of type float. IEEE 754 floating-point
// values can change sign just by flipping the sign bit, so the
// "most negative" finite float is just the negative of the "most
// positive" finite float.
//
// dd_real has a constructor for double but not for float,
// so we cast to double first.
const dd_real minVal = static_cast<double> (-std::numeric_limits<float>::max ());
const dd_real maxVal = static_cast<double> (std::numeric_limits<float>::max ());
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<float, dd_real>::safeConvert: "
"Input dd_real t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to float.");
// This also checks for double->float overflow.
return as<float> (to_double (t));
}
};
/** \brief Convert dd_real to int. */
template <>
class ValueTypeConversionTraits<int, dd_real> {
public:
inline static int convert (const dd_real t) {
return to_int (t);
}
static int safeConvert (const dd_real t) {
// dd_real has a constructor for int.
const dd_real minVal = std::numeric_limits<int>::min ();
const dd_real maxVal = std::numeric_limits<int>::max ();
TEUCHOS_TEST_FOR_EXCEPTION(
t < minVal || t > maxVal,
std::range_error,
"Teuchos::ValueTypeConversionTraits<int, dd_real>::safeConvert: "
"Input dd_real t = " << t << " is out of the valid range [" << minVal
<< ", " << maxVal << "] for conversion to int.");
return to_int (t);
}
};
/** \brief Convert long unsigned int to qd_real. */
template <>
class ValueTypeConversionTraits<qd_real, long unsigned int> {
public:
inline static qd_real convert( const long unsigned int t ) {
// FIXME (mfh 14 Nov 2012): qd_real unfortunately lacks a
// constructor or conversion function for conversions from
// built-in integer types other than int. However, it does allow
// reading in values from a string. We could use this to convert
// from any type to qd_real, by first writing the value to an
// std::ostringstream, then creating a qd_real from the resulting
// string.
return ValueTypeConversionTraits<qd_real,int>::convert(ValueTypeConversionTraits<int,long unsigned int>::convert(t));
}
inline static qd_real safeConvert( const long unsigned int t )
{ return ValueTypeConversionTraits<qd_real,int>::safeConvert(ValueTypeConversionTraits<int,long unsigned int>::safeConvert(t)); }
};
/** \brief Convert long unsigned int to dd_real. */
template <>
class ValueTypeConversionTraits<dd_real, long unsigned int> {
public:
inline static dd_real convert( const long unsigned int t ) {
// FIXME (mfh 14 Nov 2012): dd_real unfortunately lacks a
// constructor or conversion function for conversions from
// built-in integer types other than int. However, it does allow
// reading in values from a string. We could use this to convert
// from any type to dd_real, by first writing the value to an
// std::ostringstream, then creating a dd_real from the resulting
// string.
return ValueTypeConversionTraits<dd_real,int>::convert(ValueTypeConversionTraits<int,long unsigned int>::convert(t));
}
inline static dd_real safeConvert( const long unsigned int t )
{ return ValueTypeConversionTraits<dd_real,int>::safeConvert(ValueTypeConversionTraits<int,long unsigned int>::safeConvert(t)); }
};
#endif // HAVE_TEUCHOS_QD
// ToDo: Add more specializations as needed!
template<class TypeTo, class TypeFrom>
inline TypeTo as( const TypeFrom& t )
{
#ifdef HAVE_TEUCHOS_DEBUG
return ValueTypeConversionTraits<TypeTo,TypeFrom>::safeConvert(t);
#else
return ValueTypeConversionTraits<TypeTo,TypeFrom>::convert(t);
#endif // HAVE_TEUCHOS_DEBUG
}
template<class TypeTo, class TypeFrom>
inline TypeTo asSafe( const TypeFrom& t )
{
return ValueTypeConversionTraits<TypeTo,TypeFrom>::safeConvert(t);
}
} // end namespace Teuchos
#endif // TEUCHOS_AS_HPP
|