1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978
|
/*
*class++
* Name:
* Object
* Purpose:
* Base class for all AST Objects.
* Constructor Function:
* None.
* Description:
* This class is the base class from which all other classes in the
* AST library are derived. It provides all the basic Object
* behaviour and Object manipulation facilities required throughout
* the library. There is no Object constructor, however, as Objects
* on their own are not useful.
* Inheritance:
* The Object base class does not inherit from any other class.
* Attributes:
* All Objects have the following attributes:
*
* - Class: Object class name
* - ID: Object identification string
* - Ident: Permanent Object identification string
* - Nobject: Number of Objects in class
* - ObjSize: The in-memory size of the Object in bytes
* - RefCount: Count of active Object pointers
* - UseDefs: Allow use of default values for Object attributes?
* Functions:
c The following functions may be applied to all Objects:
f The following routines may be applied to all Objects:
*
c - astAnnul: Annul a pointer to an Object
c - astBegin: Begin a new AST context
c - astClear: Clear attribute values for an Object
c - astClone: Clone a pointer to an Object
c - astCopy: Copy an Object
c - astCreatedAt: Returns information about where an object was created
c - astDelete: Delete an Object
c - astEnd: End an AST context
c - astEscapes: Control whether graphical escape sequences are removed
c - astExempt: Exempt an Object pointer from AST context handling
c - astExport: Export an Object pointer to an outer context
c - astGet<X>: Get an attribute value for an Object
c - astHasAttribute: Test if an Object has a named attribute
c - astImport: Import an Object pointer to the current context
c - astIsA<Class>: Test class membership
c - astLock: Lock an Object for use by the calling thread
c - astToString: Create an in-memory serialisation of an Object
c - astSame: Do two AST pointers refer to the same Object?
c - astSet: Set attribute values for an Object
c - astSet<X>: Set an attribute value for an Object
c - astShow: Display a textual representation of an Object on standard
c output
c - astTest: Test if an attribute value is set for an Object
c - astTune: Set or get an integer AST tuning parameter
c - astTuneC: Set or get a character AST tuning parameter
c - astUnlock: Unlock an Object for use by other threads
c - astFromString: Re-create an Object from an in-memory serialisation
c - astVersion: Return the verson of the AST library being used.
f - AST_ANNUL: Annul a pointer to an Object
f - AST_BEGIN: Begin a new AST context
f - AST_CLEAR: Clear attribute values for an Object
f - AST_CLONE: Clone a pointer to an Object
f - AST_COPY: Copy an Object
f - AST_DELETE: Delete an Object
f - AST_END: End an AST context
f - AST_ESCAPES: Control whether graphical escape sequences are removed
f - AST_EXEMPT: Exempt an Object pointer from AST context handling
f - AST_EXPORT: Export an Object pointer to an outer context
f - AST_GET<X>: Get an attribute value for an Object
f - AST_HASATTRIBUTE: Test if an Object has a named attribute
f - AST_IMPORT: Import an Object pointer to the current context
f - AST_ISA<CLASS>: Test class membership
f - AST_SAME: Do two AST pointers refer to the same Object?
f - AST_SET: Set attribute values for an Object
f - AST_SET<X>: Set an attribute value for an Object
f - AST_SHOW: Display a textual representation of an Object on standard
f output
f - AST_TEST: Test if an attribute value is set for an Object
f - AST_TUNE: Set or get an integer AST tuning parameter
f - AST_TUNEC: Set or get a character AST tuning parameter
f - AST_VERSION: Return the verson of the AST library being used.
* Copyright:
* Copyright (C) 1997-2006 Council for the Central Laboratory of the
* Research Councils
* Copyright (C) 2010 Science & Technology Facilities Council.
* All Rights Reserved.
* Licence:
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
* Authors:
* RFWS: R.F. Warren-Smith (Starlink)
* DSB: David S. Berry (Starlink)
* History:
* 1-FEB-1996 (RFWS):
* Original version.
* 22-APR-1996 (RFWS):
* Added attribute setting functions.
* 2-JUL-1996 (RFWS):
* Added functions to support the external interface (using
* identfiers).
* 10-SEP-1996 (RFWS):
* Added I/O facilities.
* 30-MAY-1997 (RFWS):
* Add ID attribute.
* 14-JUL-1997 (RFWS):
* Add astExempt function.
* 14-OCT-1997 (RFWS):
* Fixed uninitialised use of "dynamic" in astCopy_.
* 14-NOV-1997 (RFWS):
* Remove the subversive C "strtok" function.
* 20-JAN-1998 (RFWS):
* Make the astClear and astRVSet methods virtual.
* 29-APR-1998 (RFWS):
* Fixed bug in algorithm for encoding Object IDs.
* 15-SEP-1999 (RFWS)
* Made astAnnulId accessible from protected code.
* 12-APR-2000 (DSB):
* Zero all memory allocated for a new Object in InitObject before
* storing any new values in the memory.
* 3-APR-2001 (DSB):
* Added the Ident attribute.
* 28-SEP-2001 (DSB):
* Modified VSet to ensure a non-null string always follows the equal
* sign in the attribute setting passed to SetAttrib.
* 27-NOV-2002 (DSB):
* Modified astShow to use astWrite instead of astDump, so that
* invocations of astShow will be included in the count of the
* number of invocations of astWrite returned by astWriteInvocations.
* 8-JAN-2003 (DSB):
* Changed private InitVtab method to protected astInitObjectVtab
* method.
* 8-FEB-2004 (DSB):
* Added astEscapes.
* 10-FEB-2004 (DSB):
* Added debug conditional code to keep track of memory leaks.
* 22-AUG-2004 (DSB):
* Added astEqual
* 27-JAN-2005 (DSB):
* Correct use of ->ident pointers, and added further DEBUG blocks.
* 11-MAR-2005 (DSB):
* Added attribute UseDefs.
* 14-FEB-2006 (DSB):
* Added attribute ObjSize.
* 23-FEB-2006 (DSB):
* Added MemoryCaching tuning parameter.
* 27-FEB-2006 (DSB):
* Include Objects returned by astCopy in the ObjectCaching system.
* 28-FEB-2006 (DSB):
* Use astOK to protect against errors within astGrow.
* 1-MAR-2006 (DSB):
* Replace astSetPermMap within DEBUG blocks by astBeginPM/astEndPM.
* 26-MAY-2006 (DSB):
* Correct handling of commas within the attribute value supplied
* to astSetC.
* 30-MAY-2006 (DSB):
* Correct the correction made to handle commas within attribute
* 6-JUN-2007 (DSB):
* Fix harmless compiler warnings.
* 21-JUN-2007 (DSB):
* In astSet<X>, ignore trailing spaces in the attribute name.
* 22-JUN-2007 (DSB):
* - Make astVSet return a pointer to dynamic memory holding the
* expanded setting string.
* - Add astSetVtab, and astCast.
* 27-JUN-2007 (DSB):
* Modify astInitObject so that it ignores the supplied "size" value
* if some memory is supplied.
* 2-JULY-2007 (DSB):
* Fix memory access problem in VSet.
* 20-SEP-2007 (DSB):
* In astDelete, ensure the error status is reset before calling
* astGrow to extend the vtab free list.
* 22-APR-2008 (DSB):
* Added astSame.
* 24-OCT-2008 (DSB):
* Prevent a mutex deadlock that could occur when annulling an
* Object ID.
* 28-JAN-2008 (DSB):
* Allow unlocked objects to be annulled using astAnnul.
* 14-OCT-2009 (DSB):
* Modify astCast to make it a virtual function and add type
* checking.
* 7-APR-2010 (DSB):
* Added method astHasAttribute.
* 24-AUG-2010 (DSB):
* Allow commas to be included in attribute values supplied to
* astSet or astVSet by putting quotes around the attribute value.
* 16-JUN-2011 (DSB):
* Added component "iref" to the Object structure. This is an
* integer identifier for each object that is unique within the
* class of the object. Useful for debugging.
* 22-JUL-2011 (DSB):
* Add methods astSetProxy and astGetProxy.
* 2-SEP-2011 (DSB):
* Add functions astToString and astFromString.
* 13-SEP-2013 (DSB):
* Report an error in astAnnul if the supplied object handle is owned by
* a different thread. Note, the Object itself does not need to be owned
* by the current thread, since it should be possible for a thread to
* relinquish a pointer to an object (i.e. a handle) without actually
* owning the object itself.
* 6-JAN-2014 (DSB):
* Added method astEnvSet.
* 9-APR-2015 (DSB):
* Modify VSet to handle "%s" setting strings (i.e. where the whole
* list of settings is provided as a single variable argument).
* This is needed because supplying the while settings string in
* place of "%s" is considered a security issue by many compilers.
* 4-JUL-2017 (DSB):
* Within astLockId, perform the correct check that the supplied
* object handle is not locked by another thread.
* 17-SEP-2017 (DSB):
* Add function astCreatedAt. This increases the size of a Handle
* structure by 20 bytes. If this turns out to be problematic
* this facility could be controlled using a configure option.
*class--
*/
/* Module Macros. */
/* ============== */
/* Set the name of the class we are implementing. This indicates to
the header files that define class interfaces that they should make
"protected" symbols available. */
#define astCLASS Object
#define INVALID_CONTEXT -1 /* Context value for handles that have no
associated Object */
#define UNOWNED_CONTEXT -2 /* Context value for handles for objects
that are not locked by any thread */
/* Include files. */
/* ============== */
/* Configuration information */
/* ------------------------ */
#include "version.h" /* Version numbers */
#if HAVE_CONFIG_H
#include <config.h>
#endif
/* Interface definitions. */
/* ---------------------- */
#include "error.h" /* Error reporting facilities */
#include "memory.h" /* Memory allocation facilities */
#include "channel.h" /* I/O channels */
#include "keymap.h" /* Hash tables */
#include "object.h" /* Interface definition for this class */
#include "plot.h" /* Plot class (for astStripEscapes) */
#include "globals.h" /* Thread-safe global data access */
/* Error code definitions. */
/* ----------------------- */
#include "ast_err.h" /* AST error codes */
/* C header files. */
/* --------------- */
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
/* Type Definitions */
/* ================ */
/* Structure used to pass data between astToString/FromString and the
corresponding Channel source and sink functions. */
typedef struct StringData {
char *ptr; /* Pointer to serialisation text */
char *buff; /* Pointer to a buffer for a single line of text */
int len; /* Current length of serialisation text */
} StringData;
/* Module Variables. */
/* ================= */
/* The following globals have the same values in all threads and so do
not need to be in thread-specific data. */
/* ------------------------------------------------------------------ */
/* Character-valued tuning parameters. */
#define MAXLEN_TUNEC 200
static char hrdel[ MAXLEN_TUNEC ] = "%-%^50+%s70+h%+";
static char mndel[ MAXLEN_TUNEC ] = "%-%^50+%s70+m%+";
static char scdel[ MAXLEN_TUNEC ] = "%-%^50+%s70+s%+";
static char dgdel[ MAXLEN_TUNEC ] = "%-%^53+%s60+o%+";
static char amdel[ MAXLEN_TUNEC ] = "%-%^20+%s85+'%+";
static char asdel[ MAXLEN_TUNEC ] = "%-%^20+%s85+\"%+";
static char exdel[ MAXLEN_TUNEC ] = "10%-%^50+%s70+";
/* A pointer full of zeros. */
static AstObject *zero_ptr;
/* A flag which indicates what should happen when an AST Object is
deleted. If this flag is non-zero, the memory used by the Object is
not freed, but a pointer to it is placed on the end of a list of free
memory chunk pointers so that the memory can be re-used if necessary
avoiding the need to re-allocate memory with malloc (which is slow).
A separate list of free memory chunks is kept for each class because
each class object will require chunks of a different size. Pointers
to these lists are stored in the virtual function table associated
with each class. All memory on these lists is freed when object
caching is switched off via the astTune function. */
static int object_caching = 0;
/* Set up global data access, mutexes, etc, needed for thread safety. */
#ifdef THREAD_SAFE
/* Define the initial values for the global data for this module. */
#define GLOBAL_inits \
globals->Retain_Esc = 0; \
globals->Context_Level = 0; \
globals->GetAttrib_Buff[ 0 ] = 0; \
globals->AstGetC_Init = 0; \
globals->AstGetC_Istr = 0; \
globals->Active_Handles = NULL; \
globals->Class_Init = 0; \
globals->Nvtab = 0; \
globals->Known_Vtabs = NULL;
/* Create the function that initialises global data for this module. */
astMAKE_INITGLOBALS(Object)
/* Define macros for accessing each item of thread specific global data. */
#define retain_esc astGLOBAL(Object,Retain_Esc)
#define context_level astGLOBAL(Object,Context_Level)
#define active_handles astGLOBAL(Object,Active_Handles)
#define getattrib_buff astGLOBAL(Object,GetAttrib_Buff)
#define astgetc_strings astGLOBAL(Object,AstGetC_Strings)
#define astgetc_istr astGLOBAL(Object,AstGetC_Istr)
#define astgetc_init astGLOBAL(Object,AstGetC_Init)
#define class_init astGLOBAL(Object,Class_Init)
#define class_vtab astGLOBAL(Object,Class_Vtab)
#define nvtab astGLOBAL(Object,Nvtab)
#define known_vtabs astGLOBAL(Object,Known_Vtabs)
/* mutex1 is used to prevent tuning parameters being accessed by more
than one thread at any one time. */
static pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
#define LOCK_MUTEX1 pthread_mutex_lock( &mutex1 );
#define UNLOCK_MUTEX1 pthread_mutex_unlock( &mutex1 );
/* mutex2 is used to prevent the global lists of object handles being
accessed by more than one thread at any one time. */
static pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
#define LOCK_MUTEX2 pthread_mutex_lock( &mutex2 );
#define UNLOCK_MUTEX2 pthread_mutex_unlock( &mutex2 );
/* Each Object contains two mutexes. The primary mutex (mutex1) is used
to guard access to all aspects of the Object except for the "locker"
and "ref_count" items. The secondary mutex (mutex2) is used to guard
access to these two remaining items. We need this secondary mutex
since the "locker" and "ref_count" items need to be accessable within
a thread even if that thread has not locked the Object using astLock.
Define macros for accessing these two mutexes. */
#define LOCK_PMUTEX(this) (pthread_mutex_lock(&((this)->mutex1)))
#define UNLOCK_PMUTEX(this) (pthread_mutex_unlock(&((this)->mutex1)))
#define LOCK_SMUTEX(this) (pthread_mutex_lock(&((this)->mutex2)))
#define UNLOCK_SMUTEX(this) (pthread_mutex_unlock(&((this)->mutex2)))
/* If thread safety is not needed, declare and initialise globals at static
variables. */
#else
/* Define the class virtual function table and its initialisation flag as
static variables. */
static int class_init = 0; /* Virtual function table initialised? */
static AstObjectVtab class_vtab; /* Virtual function table */
/* A list of pointers to all the known class virtual function tables. */
static int nvtab = 0;
static AstObjectVtab **known_vtabs = NULL;
/* A flag which indicates if AST functions which return text strings
should retain any graphical escape sequences (as interpreted by the
Plot class). */
static int retain_esc = 0;
/* Context level (Begin/End/Exempt/Export) */
static int context_level = 0;
/* Array of list heads for each context (each list is a list of Handle
structures). */
static int *active_handles = NULL;
/* String returned by GetAttrib. */
static char getattrib_buff[ AST__GETATTRIB_BUFF_LEN + 1 ] = "";
/* Pointers to string buffers returned by astGetC. */
static char *astgetc_strings[ AST__ASTGETC_MAX_STRINGS ];
/* Offset of next string in "AstGetC_Strings" */
static int astgetc_istr = 0;
/* "AstGetC_Strings" array initialised? */
static int astgetc_init = 0;
/* Null macros for mutex locking and unlocking */
#define LOCK_MUTEX1
#define UNLOCK_MUTEX1
#define LOCK_MUTEX2
#define UNLOCK_MUTEX2
#define LOCK_PMUTEX(this)
#define LOCK_SMUTEX(this)
#define UNLOCK_PMUTEX(this)
#define UNLOCK_SMUTEX(this)
#endif
/* Prototypes for Private Member Functions. */
/* ======================================== */
static AstObject *Cast( AstObject *, AstObject *, int * );
static const char *GetID( AstObject *, int * );
static const char *GetAttrib( AstObject *, const char *, int * );
static const char *GetIdent( AstObject *, int * );
static const char *Get( AstObject *, const char *, int * );
static const char *FromStringSource( void );
static int Equal( AstObject *, AstObject *, int * );
static int GetObjSize( AstObject *, int * );
static int HasAttribute( AstObject *, const char *, int * );
static int Same( AstObject *, AstObject *, int * );
static int TestAttrib( AstObject *, const char *, int * );
static int TestID( AstObject *, int * );
static int TestIdent( AstObject *, int * );
static unsigned long Magic( const AstObject *, size_t, int * );
static void CleanAttribs( AstObject *, int * );
static void Clear( AstObject *, const char *, int * );
static void ClearAttrib( AstObject *, const char *, int * );
static void ClearIdent( AstObject *, int * );
static void ClearID( AstObject *, int * );
static void Dump( AstObject *, AstChannel *, int * );
static void EmptyObjectCache( int * );
static void ToStringSink( const char * );
static void SetAttrib( AstObject *, const char *, int * );
static void SetID( AstObject *, const char *, int * );
static void SetIdent( AstObject *, const char *, int * );
static void Show( AstObject *, int * );
static void VSet( AstObject *, const char *, char **, va_list, int * );
static void EnvSet( AstObject *, int * );
static int GetUseDefs( AstObject *, int * );
static int TestUseDefs( AstObject *, int * );
static void ClearUseDefs( AstObject *, int * );
static void SetUseDefs( AstObject *, int, int * );
#if defined(THREAD_SAFE)
static void ChangeThreadVtab( AstObject *, int * );
static int ManageLock( AstObject *, int, int, AstObject **, int * );
#endif
/* Member functions. */
/* ================= */
AstObject *astAnnul_( AstObject *this, int *status ) {
/*
*++
* Name:
c astAnnul
f AST_ANNUL
* Purpose:
* Annul a pointer to an Object.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c AstObject *astAnnul( AstObject *this )
f CALL AST_ANNUL( THIS, STATUS )
* Class Membership:
* Object method.
* Description:
c This function annuls a pointer to an Object so that it is no
f This routine annuls a pointer to an Object so that it is no
* longer recognised as a valid pointer by the AST library. Any
* resources associated with the pointer are released and made
* available for re-use.
*
c This function also decrements the Object's RefCount attribute by
f This routine also decrements the Object's RefCount attribute by
* one. If this attribute reaches zero (which happens when the last
* pointer to the Object is annulled), then the Object is deleted.
* Parameters:
c this
c The Object pointer to be annulled.
f THIS = INTEGER (Given and Returned)
f The Object pointer to be annulled. A null pointer value (AST__NULL)
f is always returned.
f STATUS = INTEGER (Given and Returned)
f The global status.
c Returned Value:
c astAnnul()
c A null Object pointer (AST__NULL) is always returned.
c
* Applicability:
* Object
c This function applies to all Objects.
f This routine applies to all Objects.
* Notes:
c - This function will attempt to annul the pointer even if the
c Object is not currently locked by the calling thread (see astLock).
c - This function attempts to execute even if the AST error
c status is set
f - This routine attempts to execute even if STATUS is set to an
f error value
* on entry, although no further error report will be
* made if it subsequently fails under these circumstances. In
* particular, it will fail if the pointer suppled is not valid,
* but this will only be reported if the error status is clear on
* entry.
*--
*/
/* Check the pointer to ensure it identifies a valid Object (this
generates an error if it doesn't). */
if ( !astIsAObject( this ) ) return NULL;
/* Get a lock on the object's secondary mutex. This mutex guards access
to the "ref_count" and "locker" components of the AstObject structure. */
LOCK_SMUTEX(this);
#ifdef MEM_DEBUG
{ int rc;
char buf[100];
rc = this->ref_count;
sprintf(buf,"annulled (refcnt: %d -> %d)", rc, rc-1 );
astMemoryUse( this, buf );
}
#endif
/* Decrement the Object's reference count. */
--(this->ref_count);
/* Unlock the object's secondary mutex. */
UNLOCK_SMUTEX(this);
/* Decrement the Object's reference count and delete the Object if
necessary. */
if ( !this->ref_count ) (void) astDelete( this );
/* Always return NULL. */
return NULL;
}
static AstObject *Cast( AstObject *this, AstObject *obj, int *status ) {
/*
*+
* Name:
* astCast
* Purpose:
* Cast an Object into an instance of a sub-class.
* Type:
* Protected virtual function.
* Synopsis:
* #include "object.h"
* AstObject *astCast( AstObject *this, AstObject *obj )
* Class Membership:
* Object method.
* Description:
* This function returns a deep copy of an ancestral component of the
* supplied object. The required class of the ancestral component is
* specified by another object. Specifically, if "this" and "new" are
* of the same class, a copy of "this" is returned. If "this" is an
* instance of a subclass of "obj", then a copy of the component
* of "this" that matches the class of "obj" is returned. Otherwise,
* a NULL pointer is returned without error.
* Parameters:
* this
* Pointer to the Object to be cast.
* obj
* Pointer to an Object that defines the class of the returned Object.
* The returned Object will be of the same class as "obj".
* Returned Value:
* A pointer to the new Object. NULL if "this" is not a sub-class of
* "obj", or if an error occurs.
* Notes:
* - A NULL pointer will be returned if this function is invoked
* with the global error status set, or if it should fail for any
* reason.
*-
*/
/* Local Variables: */
AstObject *new;
int generation_gap;
/* Initialise */
new = NULL;
/* Check inherited status */
if( !astOK ) return new;
/* Check pointer have been supplied. */
if( this && obj ) {
/* See how many steps up the class inheritance ladder it is from "this" to
"obj". A positive value is returned if "this" is a sub-class of "obj".
A negative value is returned if "obj" is a sub-class of "this". Zero
is returned if they are of the same class. AST__COUSIN is returned if
they share a common ancestor but are not on the same line of descent. */
generation_gap = astClassCompare( astVTAB( this ), astVTAB( obj ) );
/* If the two objects are of the same class, just return a copy of
"this". */
if( generation_gap == 0 ) {
new = astCopy( this );
/* If "this" is a subclass of "obj", return a deep copy of "this" cast
into the class of "obj". */
} else if( generation_gap != AST__COUSIN && generation_gap > 0 ) {
new = astCastCopy( this, obj );
}
}
/* Return the new pointer. */
return new;
}
AstObject *astCastCopy_( AstObject *this, AstObject *obj, int *status ) {
/*
*+
* Name:
* astCastCopy
* Purpose:
* Cast an Object into an instance of a sub-class, without type-checking.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* AstObject *astCastCopy( AstObject *this, AstObject *obj )
* Class Membership:
* Object method.
* Description:
* This function returns a deep copy of an ancestral component of the
* supplied object. The required class of the ancestral component is
* specified by another object. No checks are performed that "this" is
* a sub-class of "obj".
*
* It works by temporarily changing the vtab in "this" to be the same
* as in "obj", and then doing a deep copy, and then re-instating the
* original vtab.
* Parameters:
* this
* Pointer to the Object to be cast.
* obj
* Pointer to an Object that defines the class of the returned Object.
* The returned Object will be of the same class as "obj".
* Returned Value:
* A pointer to the new Object.
* Notes:
* - A NULL pointer will be returned if this function is invoked
* with the global error status set, or if it should fail for any
* reason.
*-
*/
/* Local Variables: */
AstObject *new;
AstObjectVtab *this_vtab;
size_t this_size;
/* Initialise */
new = NULL;
/* Check inherited status */
if( !astOK ) return new;
/* Check pointer have been supplied. */
if( this && obj ) {
/* Save a pointer to the original virtual function tables for "this". */
this_vtab = astVTAB( this );
/* Temporarily change the vtab of "this" to that of "obJ". */
this->vtab = astVTAB( obj );
/* Temporarily change the size of "this" to be the size of "obj". */
this_size = this->size;
this->size = obj->size;
/* Now take a copy of the object (now considered to be an instance of the
class specified by "obj"). */
new = astCopy( this );
/* Re-instate the original Object vtab and size. */
this->vtab = this_vtab;
this->size = this_size;
/* The sub-clas to which "this" originally belonged may have extended the
range of values allowed for one or more of the attributes inherited from
the "obj" class. This means that the current attribute values stored
in the returned object may be inappropriate for the class of "obj". An
example is the System attribute defined by the Frame class, and extended
by sub-classes of Frame. So we now call astCleanAttribs to ensure that
any inappropriate attribute values are cleared in the returned object. */
astCleanAttribs( new );
}
/* Return the new pointer. */
return new;
}
#if defined(THREAD_SAFE)
static void ChangeThreadVtab( AstObject *this, int *status ){
/*
* Name:
* ChangeThreadVtab
* Purpose:
* Modify an Object structure so that it refers to a vtab created by
* the currently executing thread.
* Type:
* Private function.
* Synopsis:
* #include "object.h"
* void ChangeThreadVtab( AstObject *this, int *status )
* Class Membership:
* Object member function.
* Description:
* Each Object structure contains a pointer to a virtual function
* table (vtab) that identifies information about the class to
* which the Object belongs (function pointers, Object caches,
* etc). In order to avoid use of mutexes (which can slow down AST
* applications enormously), each thread has its own set of vtab
* structures (one for each AST class) stored in thread-specific
* data. Each time an Object is locked by the currently executing
* thread, this function should be called to change the vtab pointer
* in the Object to refer to the vtab relevant to the currently
* executing thread.
* Parameters:
* this
* Pointer to the Object.
* status
* Pointer to the inherited status variable.
*/
/* Local Variables: */
astDECLARE_GLOBALS
const char *class;
int i;
/* Check the global error status. */
if ( !astOK ) return;
/* Get a pointer to Thread-specific data for the currently executing thread. */
astGET_GLOBALS(this);
/* Get the class name for the supplied Object. This uses the existing
vtab pointer in the Object structure to locate the required GetClass
method and the class name. This vtab pointer may be for a vtab created
by a different thread to the one currently executing, but this shouldn't
matter since we are not modifying the vtab contents. */
class = astGetClass( this );
/* Check a class name was obtained */
if( class ) {
/* Loop round the vtab structures created by the currently executing thread. */
for( i = 0; i < nvtab; i++ ) {
/* If the current vtab is for a class that matches the class of the
supplied Object, then store a pointer to the vtab in the Object
structure, and exit. */
if( !strcmp( class, known_vtabs[ i ]->class ) ) {
this->vtab = known_vtabs[ i ];
break;
}
}
}
}
#endif
AstObject *astCheckLock_( AstObject *this, int *status ) {
/*
*+
* Name:
* astCheckLock
* Purpose:
* Check that supplied Object is locked by the calling thread.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* AstObject *astCheckLock( AstObject *this )
* Class Membership:
* Object method.
* Description:
* This function reports an error if the supplied object has not
* previously been locked (using astLock) by the calling thread.
* Parameters:
* this
* Pointer to the Object.
* Returned Value:
* A copy of the supplied pointer ("this") is returned. The Object
* reference count is not changed.
* Notes:
* - This function attempts to execute even if an error has already
* occurred.
*-
*/
/* This function does nothing in the non-threads version of libast. */
#if defined(THREAD_SAFE)
/* Local Variables; */
AstObject *fail;
/* Check the supplied pointer. */
if( this ) {
/* First use the private ManageLock function rather than the virtual
astManageLock method to check the top level Object is locked for use
by the current thread. This saves time and allows a more appropriate
error message to be issued. */
if( ManageLock( this, AST__CHECKLOCK, 0, NULL, status ) ) {
if( astOK ) {
astError( AST__LCKERR, "astCheckLock(%s): The supplied %s cannot "
"be used since it is not locked for use by the current "
"thread (programming error).", status, astGetClass( this ),
astGetClass( this ) );
}
/* If the top level Object is locked, now use the virtual astManageLock
method to check any objects contained within the top level Object. */
} else if( astManageLock( this, AST__CHECKLOCK, 0, &fail ) ) {
if( astOK ) {
astError( AST__LCKERR, "astCheckLock(%s): The supplied %s cannot "
"be used since a %s contained within the %s is not "
"locked for use by the current thread (programming "
"error).", status, astGetClass( this ),
astGetClass( this ), astGetClass( fail ),
astGetClass( this ) );
}
}
}
#endif
/* Return the supploed pointer. */
return this;
}
int astClassCompare_( AstObjectVtab *class1, AstObjectVtab *class2,
int *status ) {
/*
*+
* Name:
* astClassCompare
* Purpose:
* Determine the relationship between two AST classes.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* int astClassCompare( AstObjectVtab *class1, AstObjectVtab *class2 )
* Class Membership:
* Object method.
* Description:
* This function returns the number of steps up the class inheritance
* ladder from the class specified by "class1" to the class specified
* by "class2".
* Parameters:
* class1
* Pointer to a virtual function table describing the first AST class.
* class2
* Pointer to a virtual function table describing the second AST class.
* Returned Value:
* The generation gap between "class1" and "class2". The result will be
* positive if "class1" is a subclass of "class2", negative if "class2"
* is a subclass of "class1", zero if they are of the same class (or
* an error occurs), or AST__COUSIN if they are not on the same line
* of descent.
*-
*/
/* Local Variables: */
AstClassIdentifier *class1_id;
AstClassIdentifier *class2_id;
AstClassIdentifier *id;
int *class1_check;
int *class2_check;
int result;
/* Initialise */
result = 0;
/* Check inherited status */
if( !astOK ) return result;
/* Check pointer have been supplied. */
if( class1 && class2 ) {
/* Get pointers to the AstClassIdentifier that identifies the top-level
class of each vtab. */
class1_id = class1->top_id;
class2_id = class2->top_id;
/* Class membership is specified by the "check" value in each class
identifier. Get the check values for both vtabs. */
class1_check = class1_id->check;
class2_check = class2_id->check;
/* Try walking up the class heirarchy of "class1" until the class of
"class2" is reached. The top-level AstObject class has a NULL "parent"
pointer in its class identifier structure. */
id = class1_id;
while( id && ( id->check != class2_check ) ) {
id = id->parent;
result++;
}
/* If "class1" is not a subclass of "class2", try walking up the class
heirarchy of "class2" until the class of "class1" is reached. */
if( !id ) {
result = 0;
id = class2_id;
while( id && ( id->check != class1_check ) ) {
id = id->parent;
result--;
}
/* If "class2" is not a subclass of "class1", return AST__COUSIN. */
if( !id ) result = AST__COUSIN;
}
}
/* Return the generation gap. */
return result;
}
static void CleanAttribs( AstObject *this_object, int *status ) {
/*
*+
* Name:
* astCleanAttribs
* Purpose:
* Clear any invalid set attribute values.
* Type:
* Protected virtual function.
* Synopsis:
* #include "object.h"
* void astCleanAttribs( AstObject *this, int *status )
* Class Membership:
* Object method.
* Description:
* This function clears any attributes that are currently set to
* invalid values in the supplied object. This can happen for instance
* when an object is cast into an instance of a parent class using
* astCast, since sub-classes can extend the range of valid values
* an attribute can take.
* Parameters:
* this
* Pointer to the Object to be cleaned.
*-
*/
/* The base Object class has no attributes that need cleaning. */
}
static void Clear( AstObject *this, const char *attrib, int *status ) {
/*
*++
* Name:
c astClear
f AST_CLEAR
* Purpose:
* Clear attribute values for an Object.
* Type:
* Public virtual function.
* Synopsis:
c #include "object.h"
c void astClear( AstObject *this, const char *attrib )
f CALL AST_CLEAR( THIS, ATTRIB, STATUS )
* Class Membership:
* Object method.
* Description:
c This function clears the values of a specified set of attributes
f This routine clears the values of a specified set of attributes
* for an Object. Clearing an attribute cancels any value that has
* previously been explicitly set for it, so that the standard
* default attribute value will subsequently be used instead. This
c also causes the astTest function to return the value zero for
f also causes the AST_TEST function to return the value .FALSE. for
* the attribute, indicating that no value has been set.
* Parameters:
c this
f THIS = INTEGER (Given)
* Pointer to the Object.
c attrib
f ATTRIB = CHARACTER * ( * ) (Given)
c Pointer to a null-terminated character string containing a
c comma-separated list of the names of the attributes to be cleared.
f A character string containing a comma-separated list of the
f names of the attributes to be cleared.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Applicability:
* Object
c This function applies to all Objects.
f This routine applies to all Objects.
* Notes:
* - Attribute names are not case sensitive and may be surrounded
* by white space.
* - It does no harm to clear an attribute whose value has not been
* set.
* - An error will result if an attempt is made to clear the value
* of a read-only attribute.
*--
*/
/* Local Variables: */
char *buff; /* Pointer to character buffer */
char *name; /* Pointer to individual attribute name */
char *name_end; /* Pointer to null at end of name */
int i; /* Loop counter for characters */
int j; /* Non-blank character count */
int len; /* Length of attrib string */
/* Check the global error status. */
if ( !astOK ) return;
/* Obtain the length of the attrib string. */
len = (int) strlen( attrib );
if ( len != 0 ) {
/* Allocate memory and store a copy of the string. */
buff = astStore( NULL, attrib, (size_t) ( len + 1 ) );
if ( astOK ) {
/* Loop to process each element in the comma-separated list. */
name = buff;
while ( name ) {
/* Change the comma at the end of each element to a null to terminate
the name. */
if ( ( name_end = strchr( name, ',' ) ) ) *name_end = '\0';
/* Remove white space and upper case characters from the attribute
name. */
for ( i = j = 0; name[ i ]; i++ ) {
if ( !isspace( name[ i ] ) ) name[ j++ ] = tolower( name[ i ] );
}
/* Terminate the attribute name and pass it to astClearAttrib to clear
the attribute (unless it is all blank, in which case we ignore
it). */
name[ j ] = '\0';
if ( j ) astClearAttrib( this, name );
/* Check for errors and abort if any clear operation fails. Otherwise,
process the next attribute. */
if ( !astOK ) break;
name = name_end ? name_end + 1 : NULL;
}
}
/* Free the memory allocated for the string buffer. */
buff = astFree( buff );
}
}
static void ClearAttrib( AstObject *this, const char *attrib, int *status ) {
/*
*+
* Name:
* astClearAttrib
* Purpose:
* Clear an attribute value for an Object.
* Type:
* Protected virtual function.
* Synopsis:
* #include "object.h"
* void astClearAttrib( AstObject *this, const char *attrib )
* Class Membership:
* Object method.
* Description:
* This function clears the value of a specified attribute for an
* Object, so that the default value will subsequently be used.
* Parameters:
* this
* Pointer to the Object.
* attrib
* Pointer to a null-terminated string specifying the attribute
* name. This should be in lower case with no surrounding white
* space.
* Notes:
* - The Object class does not have any writable attributes, so
* this function merely reports an error. It is intended to be
* extended by other class definitions.
*-
*/
/* Check the global error status. */
if ( !astOK ) return;
/* Check the attribute name and clear the appropriate attribute. */
/* ID. */
/* --- */
if ( !strcmp( attrib, "id" ) ) {
astClearID( this );
/* Ident. */
/* ------ */
} else if ( !strcmp( attrib, "ident" ) ) {
astClearIdent( this );
/* UseDefs. */
/* -------- */
} else if ( !strcmp( attrib, "usedefs" ) ) {
astClearUseDefs( this );
/* Read-only attributes. */
/* --------------------- */
/* Test if the attribute string matches any of the read-only
attributes of this class. If it does, then report an error. */
} else if ( !strcmp( attrib, "class" ) ||
!strcmp( attrib, "nobject" ) ||
!strcmp( attrib, "objsize" ) ||
!strcmp( attrib, "refcount" ) ) {
astError( AST__NOWRT, "astClear: Invalid attempt to clear the \"%s\" "
"value for a %s.", status, attrib, astGetClass( this ) );
astError( AST__NOWRT, "This is a read-only attribute." , status);
/* Since no writable attributes are defined for the Object class, any
attempt to clear a value for anything else is also an error. */
} else {
astError( AST__BADAT, "astClear: The attribute name \"%s\" is invalid "
"for a %s.", status, attrib, astGetClass( this ) );
}
}
AstObject *astClone_( AstObject *this, int *status ) {
/*
*++
* Name:
c astClone
f AST_CLONE
* Purpose:
* Clone (duplicate) an Object pointer.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c AstObject *astClone( AstObject *this )
f RESULT = AST_CLONE( THIS, STATUS )
* Class Membership:
* Object method.
* Description:
* This function returns a duplicate pointer to an existing
* Object. It also increments the Object's RefCount attribute to
* keep track of how many pointers have been issued.
*
* Note that this function is NOT equivalent to an assignment
* statement, as in general the two pointers will not have the same
* value.
* Parameters:
c this
f THIS = INTEGER (Given)
* Original pointer to the Object.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Returned Value:
c astClone()
f AST_CLONE = INTEGER
* A duplicate pointer to the same Object.
* Applicability:
* Object
* This function applies to all Objects.
* Notes:
* - A null Object pointer (AST__NULL) will be returned if this
c function is invoked with the AST error status set, or if it
f function is invoked with STATUS set to an error value, or if it
* should fail for any reason.
*--
*/
/* Check the global error status. */
if ( !astOK ) return NULL;
/* Get a lock on the object's secondary mutex. This mutex guards access
to the "ref_count" and "locker" components of the AstObject structure. */
LOCK_SMUTEX(this);
#ifdef MEM_DEBUG
{ int rc;
char buf[100];
rc = this->ref_count;
sprintf(buf,"cloned (refcnt: %d -> %d)", rc, rc+1 );
astMemoryUse( this, buf );
}
#endif
/* Increment the Object's reference count. */
this->ref_count++;
/* Unlock the object's secondary mutex. */
UNLOCK_SMUTEX(this);
/* Return a new pointer to the Object. */
return this;
}
AstObject *astCopy_( const AstObject *this, int *status ) {
/*
*++
* Name:
c astCopy
f AST_COPY
* Purpose:
* Copy an Object.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c AstObject *astCopy( const AstObject *this )
f RESULT = AST_COPY( THIS, STATUS )
* Class Membership:
* Object method.
* Description:
* This function creates a copy of an Object and returns a pointer
* to the resulting new Object. It makes a "deep" copy, which
* contains no references to any other Object (i.e. if the original
* Object contains references to other Objects, then the actual
* data are copied, not simply the references). This means that
* modifications may safely be made to the copy without indirectly
* affecting any other Object.
* Parameters:
c this
f THIS = INTEGER (Given)
* Pointer to the Object to be copied.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Returned Value:
c astCopy()
f AST_COPY = INTEGER
* Pointer to the new Object.
* Applicability:
* Object
* This function applies to all Objects.
* Notes:
* - A null Object pointer (AST__NULL) will be returned if this
c function is invoked with the AST error status set, or if it
f function is invoked with STATUS set to an error value, or if it
* should fail for any reason.
*--
*/
/* Local Variables: */
AstObject *new; /* Pointer to new object */
AstObjectVtab *vtab; /* Pointer to object vtab */
int i; /* Loop counter for copy constructors */
/* Initiallise. */
new = NULL;
/* Check the global error status. */
if ( !astOK ) return new;
/* Re-use cached memory, or allocate new memory using the size of the input
object, to store the output Object. */
vtab = this->vtab;
if( object_caching ){
if( vtab->nfree > 0 ) {
new = vtab->free_list[ --(vtab->nfree) ];
vtab->free_list[ vtab->nfree ] = NULL;
} else {
new = astMalloc( this->size );
}
} else {
new = astMalloc( this->size );
}
if ( astOK ) {
/* Perform an initial byte-by-byte copy of the entire object
structure. */
(void) memcpy( (void *) new, (const void *) this, this->size );
/* Initialise any components of the new Object structure that need to
differ from the input. */
new->check = Magic( new, new->size, status );
new->dynamic = 1;
new->ref_count = 1;
new->id = NULL; /* ID attribute is not copied (but Ident is copied) */
new->proxy = NULL;
/* Copy the persistent identifier string. */
if( this->ident ) {
new->ident = astStore( NULL, this->ident, strlen( this->ident ) + 1 );
}
/* Create a new mutex for the new Object, and lock it for use by the
current thread. */
#ifdef THREAD_SAFE
if( pthread_mutex_init( &(new->mutex1), NULL ) != 0 && astOK ) {
astError( AST__INTER, "astInitObject(%s): Failed to "
"initialise POSIX mutex1 for the new Object.", status,
vtab->class );
}
if( pthread_mutex_init( &(new->mutex2), NULL ) != 0 && astOK ) {
astError( AST__INTER, "astInitObject(%s): Failed to "
"initialise POSIX mutex2 for the new Object.", status,
vtab->class );
}
new->locker = -1;
new->globals = NULL;
(void) ManageLock( new, AST__LOCK, 0, NULL, status );
#endif
/* Loop to execute any copy constructors declared by derived classes. */
for ( i = 0; i < vtab->ncopy; i++ ) {
/* Invoke each copy constructor in turn. */
(*vtab->copy[ i ])( this, new, status );
/* If any copy constructor fails, work backwards through the
corresponding destructor functions, invoking each in turn to undo
the copy operations that have been completed so far. */
if ( !astOK ) {
for ( ; i >= 0; i-- ) {
(*vtab->delete[ i ])( new, status );
}
/* Zero the entire new Object structure (to prevent accidental re-use
of any of its values after deletion). */
(void) memset( new, 0, new->size );
/* Free the Object's memory and ensure that a NULL pointer will be
returned. */
new = astFree( new );
/* Quit trying to copy the Object. */
break;
}
}
}
/* If OK, increment the count of active objects. */
if ( astOK ) vtab->nobject++;
/* Return a pointer to the new Object. */
return new;
}
AstObject *astDelete_( AstObject *this, int *status ) {
/*
*++
* Name:
c astDelete
f AST_DELETE
* Purpose:
* Delete an Object.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c AstObject *astDelete( AstObject *this )
f CALL AST_DELETE( THIS, STATUS )
* Class Membership:
* Object method.
* Description:
c This function deletes an Object, freeing all resources
f This routine deletes an Object, freeing all resources
* associated with it and rendering any remaining pointers to the
* Object invalid.
*
* Note that deletion is unconditional, regardless of whether other
* pointers to the Object are still in use (possibly within other
* Objects). A safer approach is to defer deletion, until all
c references to an Object have expired, by using astBegin/astEnd
c (together with astClone and astAnnul if necessary).
f references to an Object have expired, by using AST_BEGIN/AST_END
f (together with AST_CLONE and AST_ANNUL if necessary).
* Parameters:
c this
c Pointer to the Object to be deleted.
f THIS = INTEGER (Given and Returned)
f Pointer to the Object to be deleted. A null pointer value
f (AST__NULL) is always returned.
f STATUS = INTEGER (Given and Returned)
f The global status.
c Returned Value:
c astDelete()
c A null Object pointer (AST__NULL) is always returned.
c
* Applicability:
* Object
c This function applies to all Objects.
f This routine applies to all Objects.
* Notes:
c - This function attempts to execute even if the AST error status
c is set
f - This routine attempts to execute even if STATUS is set to an error
f value
* on entry, although no further error report will be
* made if it subsequently fails under these circumstances.
*--
*/
/* Local Variables: */
AstObjectVtab *vtab; /* Pointer to virtual function table */
int dynamic; /* Was memory allocated dynamically? */
int i; /* Loop counter for destructors */
int ifree; /* Index of next slot on free list */
int status_value; /* AST error status value */
size_t size; /* Object size */
/* Check the pointer to ensure it identifies a valid Object (this
generates an error if it doesn't). */
if ( !astIsAObject( this ) ) return NULL;
/* Loop through all the destructors associated with the Object by derived
classes (working up the class hierarchy). */
for ( i = this->vtab->ndelete - 1; i >= 0; i-- ) {
/* Invoke each destructor in turn. Attempt to continue even if destructors
fail. */
( *this->vtab->delete[ i ] )( this, status );
}
/* Free the ID strings. */
this->id = astFree( this->id );
this->ident = astFree( this->ident );
/* Attempt to unlock the Object and destroy its mutexes. */
#if defined(THREAD_SAFE)
(void) ManageLock( this, AST__UNLOCK, 0, NULL, status );
pthread_mutex_destroy( &(this->mutex1) );
pthread_mutex_destroy( &(this->mutex2) );
#endif
/* Save the virtual function table address and note if the Object's
memory was allocated dynamically. Also note its size. */
vtab = this->vtab;
dynamic = this->dynamic;
size = this->size;
/* Zero the entire Object structure (to prevent accidental re-use of
any of its values after deletion). */
(void) memset( this, 0, size );
/* If necessary, free the Object's memory. If object caching is switched
on, the memory is not in fact freed; it is merely placed onto the end
of the list of free memory blocks included in the virtual function table
of the AST class concerned. astGrow returns immediately if an error
has already occurred, so we need to reset the error status explicitly
before calling astGrow. */
if ( dynamic ) {
if( object_caching ) {
ifree = (vtab->nfree)++;
status_value = astStatus;
astClearStatus;
vtab->free_list = astGrow( vtab->free_list, vtab->nfree,
sizeof(AstObject *) );
astSetStatus( status_value );
if( vtab->free_list ) vtab->free_list[ ifree ] = this;
} else {
(void) astFree( this );
}
}
/* Decrement the count of active Objects. */
vtab->nobject--;
/* Always return NULL. */
return NULL;
}
static void Dump( AstObject *this, AstChannel *channel, int *status ) {
/*
*+
* Name:
* astDump
* Purpose:
* Write an Object to a Channel.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* void astDump( AstObject *this, AstChannel *channel )
* Class Membership:
* Object method.
* Description:
* This function writes an Object to a Channel, appending it to any
* previous Objects written to that Channel.
* Parameters:
* this
* Pointer to the Object to be written.
* channel
* Pointer to the output Channel.
*-
*/
/* Local Variables: */
AstObjectVtab *vtab; /* Pointer to virtual function table */
const char *sval; /* Pointer to string value */
int helpful; /* Helpful to show value even if not set? */
int idump; /* Loop counter for dump functions */
int ival; /* Attribute value */
int set; /* Attribute value set? */
/* Check the global error status. */
if ( !astOK ) return;
/* Write an initial "Begin" item, giving the class name of the Object
being written. Also supply a pointer to the comment associated with
the most recently-declared dump function in the Object's virtual
function table. This should describe the class to which the Object
belongs (assuming it has correctly declared its dump function). */
astWriteBegin( channel, astGetClass( this ),
this->vtab->dump_comment[ this->vtab->ndump - 1 ] );
/* Write out instance variable information for the base Object
class. Accompany these with appropriate comment strings, possibly
depending on the values being written.*/
/* In the case of attributes, we first use the appropriate (private)
Test... member function to see if they are set. If so, we then use
the (private) Get... function to obtain the value to be written
out.
For attributes which are not set, we use the astGet... method to
obtain the value instead. This will supply a default value
(possibly provided by a derived class which over-rides this method)
which is more useful to a human reader as it corresponds to the
actual default attribute value. Since "set" will be zero, these
values are for information only and will not be read back. */
/* ID. */
/* --- */
set = TestID( this, status );
sval = set ? GetID( this, status ) : astGetID( this );
/* Don't show an un-set ID value if it is blank. */
helpful = ( sval && *sval );
astWriteString( channel, "ID", set, helpful, sval,
"Object identification string" );
/* Ident. */
/* --- */
set = TestIdent( this, status );
sval = set ? GetIdent( this, status ) : astGetIdent( this );
/* Don't show an un-set Ident value if it is blank. */
helpful = ( sval && *sval );
astWriteString( channel, "Ident", set, helpful, sval,
"Permanent Object identification string" );
/* UseDefs */
/* ------- */
set = TestUseDefs( this, status );
ival = set ? GetUseDefs( this, status ) : astGetUseDefs( this );
astWriteInt( channel, "UseDfs", set, 0, ival,
ival ? "Default attribute values can be used" :
"Default values cannot be used" );
/* RefCnt. */
/* ------- */
LOCK_SMUTEX(this);
ival = this->ref_count;
UNLOCK_SMUTEX(this);
astWriteInt( channel, "RefCnt", 0, 0, ival,
"Count of active Object pointers" );
/* Nobj. */
/* ----- */
vtab = this->vtab;
astWriteInt( channel, "Nobj", 0, 0, vtab->nobject,
"Count of active Objects in same class" );
/* Terminate the information above with an "IsA" item for the base
Object class. */
astWriteIsA( channel, "Object", "AST Object" );
/* Now loop to perform the same operation for each additional class
from which the Object inherits (the Object class itself does not
declare a dump function). Invoke the dump function for each class
in turn, working down the class hierarchy, to write out instance
variable information for that class. */
for ( idump = 0; idump < this->vtab->ndump; idump++ ) {
( *this->vtab->dump[ idump ] )( this, channel, status );
/* Terminate the output from all except the final dump function with
an appropriate "IsA" item describing the class whose data have just
been written. */
if ( idump != ( this->vtab->ndump - 1 ) ) {
astWriteIsA( channel, this->vtab->dump_class[ idump ],
this->vtab->dump_comment[ idump ] );
}
/* Quit looping if an error occurs. */
if ( !astOK ) break;
}
/* Terminate the output from the final dump function with an "End"
item to match the initial "Begin" item. */
astWriteEnd( channel, astGetClass( this ) );
}
static void EmptyObjectCache( int *status ){
/*
* Name:
* EmptyObjectCache
* Purpose:
* Free all memory blocks currently on the free list of any class.
* Type:
* Private function.
* Synopsis:
* #include "object.h"
* EmptyObjectCache( int *status )
* Class Membership:
* Object member function.
* Description:
* This function empties the cache of Object memory by freeing all
* memory blocks on the free_list of all classes.
* Parameters:
* status
* Pointer to the inherited status variable.
* Notes:
* - This function attempts to execute even if an error has occurred.
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
int iblock; /* Index of next entry in free list */
int itab; /* Index of next virtual function table */
AstObjectVtab *vtab; /* Pointer to next virtual function table */
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(NULL);
/* Loop round all the virtual function tables which are known about. */
for( itab = 0; itab < nvtab; itab++ ) {
vtab = known_vtabs[ itab ];
/* Free all memory blocks stored on the free list for this class. */
for( iblock = 0; iblock < vtab->nfree; iblock++ ) {
(vtab->free_list)[ iblock ] = astFree( (vtab->free_list)[ iblock ] );
}
/* Free the memory used to hold the free list, and indicate it has zero
length. */
vtab->free_list = astFree( vtab->free_list );
vtab->nfree = 0;
}
}
static void EnvSet( AstObject *this, int *status ) {
/*
*+
* Name:
* astEnvSet
* Purpose:
* Set default values for an Object's attributes.
* Type:
* Protected virtual function.
* Synopsis:
* #include "object.h"
* void astEnvSet( AstObject *this )
* Class Membership:
* Object method.
* Description:
* This function assigns a set of attribute values for an Object,
* the attributes and their values being specified by means of an
* environment variable of the form "<CLASSNAME>_OPTIONS" that has
* a value of the form:
*
* "attribute1 = value1, attribute2 = value2, ... "
*
* Here, "attribute" specifies an attribute name and the value to
* the right of each "=" sign should be a suitable textual
* representation of the value to be assigned to that
* attribute. This will be interpreted according to the attribute's
* data type.
* Parameters:
* this
* Pointer to the Object.
* Notes:
* - See astVSet for details of how the setting strings are
* interpreted.
*-
*/
/* Local Variables: */
char varname[ 100 ];
const char *attrs = NULL;
const char *class = NULL;
/* Check the global error status. */
if ( !astOK ) return;
/* Get the string holding default attribute values for the class of the
supplied object. This string is held in the class virtual function
table. */
attrs = this->vtab->defaults;
/* If this is the first time the defaults have been requested, get the
list of defaults from the environment variable "<CLASSNAME>_OPTIONS"
and store in the virtual function table. */
if( !attrs ) {
/* Get the class name. */
class = astGetClass( this );
/* Form the upper-case name of the environment variable. */
if( class ) {
sprintf( varname, "%s_OPTIONS", class );
astChrCase( NULL, varname, 1, sizeof( varname ) );
/* Get the value of the environment variable. */
attrs = getenv( varname );
/* If no defaults were specified store the string "None". */
if( ! attrs ) attrs = "None";
/* Store a copy in the virtual function table. */
astBeginPM;
this->vtab->defaults = astStore( NULL, attrs, strlen( attrs ) + 1 );
astEndPM;
}
}
/* If any defaults were specified, set the corresponding attributes. */
if( attrs && strcmp( attrs, "None" ) ) astSet( this, attrs, status );
}
static int Equal( AstObject *this, AstObject *that, int *status ){
/*
*+
* Name:
* astEqual
* Purpose:
* Check equality of two AST Objects.
* Type:
* Public (but undocumented) function.
* Synopsis:
* #include "object.h"
* int astEqual( AstObject *this, AstObject *this )
* Class Membership:
* Object virtual function.
* Description:
* This function returns non-zero if the two pointers identify
* equivalent objects.
* Parameters:
* this
* Pointer to the first Object.
* that
* Pointer to the second Object.
* Returned Value:
* Non-zero if the objects are equivalent.
* Notes:
* - This function is available in the public interface even though it is
* documented as protected. This is because it is difficult to document
* precisely which aspects of two Objects must be equal in order for this
* function to return a non-zero value. Each class of Object supplies
* its own Equal method that tests which-ever attributes the class
* considers to be significiant.
* - The implementation of this function provided by the base Object
* class simply compares the class names and the structure size.
* Sub-classes should override this method to provide more appropriate tests.
* - Zero is returned if an error has already occurred, or if
* this function should fail for any reason.
*-
*/
/* Local Variables: */
int result;
/* Check inherited status */
if( !astOK ) return 0;
/* Objects are equivalent if they are the same object. */
if( this == that ) {
result = 1;
/* Otherwise, check the structure size and class names */
} else {
result = ( this->size == that->size &&
!strcmp( astGetClass( this ), astGetClass( that ) ) );
}
return result;
}
static const char *Get( AstObject *this, const char *attrib, int *status ) {
/*
* Name:
* Get
* Purpose:
* Get the value of a specified attribute for an Object.
* Type:
* Private function.
* Synopsis:
* #include "object.h"
* const char *Get( AstObject *this, const char *attrib, int *status )
* Class Membership:
* Object member function.
* Description:
* This function returns a pointer to the value of a specified
* attribute for an Object, formatted as a character string. It is
* mainly a wrap-up used internally for invoking the astGetAttrib
* method. It converts the attribute name to lower case and removes
* white space before invoking the method. This saves derived
* classes that over-ride the astGetAttrib method from having to do
* this themselves.
* Parameters:
* this
* Pointer to the Object.
* attrib
* Pointer to a null-terminated string containing the name of
* the attribute whose value is required. This may contain mixed
* case and white space, but should not be composed entirely of
* white space.
* status
* Pointer to the inherited status variable.
* Returned Value:
* - Pointer to a null-terminated string containing the attribute
* value.
* Notes:
* - The returned string pointer may point at memory allocated
* within the Object, or at static memory. The contents of the
* string may be over-written or the pointer may become invalid
* following a further invocation of the same function or any
* modification of the Object. A copy of the string should
* therefore be made if necessary.
* - A NULL pointer will be returned if this function is invoked
* with the global error status set, or if it should fail for any
* reason.
*/
/* Local Variables: */
char *buff; /* Pointer to local string buffer */
const char *result; /* Pointer value to return */
int i; /* Loop counter for characters */
int j; /* Non-blank character count */
/* Initialise. */
result = NULL;
/* Check the global error status. */
if ( !astOK ) return result;
/* Allocate a local buffer long enough to hold the attribute name
string. */
buff = astMalloc( strlen( attrib ) + (size_t) 1 );
if ( astOK ) {
/* Copy the attribute name characters into the buffer, omitting all
white space and converting to lower case. */
for ( i = j = 0; attrib[ i ]; i++ ) {
if ( !isspace( attrib[ i ] ) ) buff[ j++ ] = tolower( attrib[ i ] );
}
/* Terminate the copied string. */
buff[ j ] = '\0';
/* If no characters were copied, the attribute name was blank, so
report an error. */
if ( !j ) {
if( astOK ) astError( AST__BADAT, "astGet(%s): A blank attribute "
"name was given.", status, astGetClass( this ) );
/* Of OK, invoke astGetAttrib to obtain a pointer to the attribute
value formatted as a character string. */
} else {
result = astGetAttrib( this, buff );
/* If required, strip out graphical escape sequences. */
if( !astEscapes( -1 ) ) result = astStripEscapes( result );
}
}
/* Free the local string buffer. */
buff = astFree( buff );
/* If an error occurred, clear the result value. */
if ( !astOK ) result = NULL;
/* Return the result. */
return result;
}
static const char *GetAttrib( AstObject *this, const char *attrib, int *status ) {
/*
*+
* Name:
* astGetAttrib
* Purpose:
* Get the value of a specified attribute for an Object.
* Type:
* Protected virtual function.
* Synopsis:
* #include "object.h"
* const char *astGetAttrib( AstObject *this, const char *attrib )
* Class Membership:
* Object method.
* Description:
* This function returns a pointer to the value of a specified
* attribute for an Object, formatted as a character string.
* Parameters:
* this
* Pointer to the Object.
* attrib
* Pointer to a null-terminated string containing the name of
* the attribute whose value is required. This name should be in
* lower case, with all white space removed.
* Returned Value:
* - Pointer to a null-terminated string containing the attribute
* value.
* Notes:
* - The returned string pointer may point at memory allocated
* within the Object, or at static memory. The contents of the
* string may be over-written or the pointer may become invalid
* following a further invocation of the same function or any
* modification of the Object. A copy of the string should
* therefore be made if necessary.
* - A NULL pointer will be returned if this function is invoked
* with the global error status set, or if it should fail for any
* reason.
*-
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
const char *result; /* Pointer value to return */
int nobject; /* Nobject attribute value */
int objsize; /* ObjSize attribute value */
int ref_count; /* RefCount attribute value */
int usedefs; /* UseDefs attribute value */
/* Initialise. */
result = NULL;
/* Check the global error status. */
if ( !astOK ) return result;
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(this);
/* Compare "attrib" with each recognised attribute name in turn,
obtaining the value of the required attribute. If necessary, write
the value into "getattrib_buff" as a null-terminated string in an
appropriate format. Set "result" to point at the result string. */
/* Class. */
/* ------ */
if ( !strcmp( attrib, "class" ) ) {
result = astGetClass( this );
/* ID. */
/* --- */
} else if ( !strcmp( attrib, "id" ) ) {
result = astGetID( this );
/* Ident. */
/* ------ */
} else if ( !strcmp( attrib, "ident" ) ) {
result = astGetIdent( this );
/* UseDefs */
/* ------- */
} else if ( !strcmp( attrib, "usedefs" ) ) {
usedefs = astGetUseDefs( this );
if ( astOK ) {
(void) sprintf( getattrib_buff, "%d", usedefs );
result = getattrib_buff;
}
/* Nobject. */
/* -------- */
} else if ( !strcmp( attrib, "nobject" ) ) {
nobject = astGetNobject( this );
if ( astOK ) {
(void) sprintf( getattrib_buff, "%d", nobject );
result = getattrib_buff;
}
/* ObjSize */
/* ------- */
} else if ( !strcmp( attrib, "objsize" ) ) {
objsize = astGetObjSize( this );
if ( astOK ) {
(void) sprintf( getattrib_buff, "%d", objsize );
result = getattrib_buff;
}
/* RefCount. */
/* --------- */
} else if ( !strcmp( attrib, "refcount" ) ) {
ref_count = astGetRefCount( this );
if ( astOK ) {
(void) sprintf( getattrib_buff, "%d", ref_count );
result = getattrib_buff;
}
/* If the attribute name was not recognised, then report an error. */
} else if( astOK ){
astError( AST__BADAT, "astGet: The %s given does not have an attribute "
"called \"%s\".", status, astGetClass( this ), attrib );
}
/* Return the result. */
return result;
}
const char *astGetClass_( const AstObject *this, int *status ) {
/*
*+
* Name:
* astGetClass
* Purpose:
* Obtain the value of the Class attribute for an Object.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* const char *astGetClass( const AstObject *this )
* Class Membership:
* Object method.
* Description:
* This function returns a pointer to the Class string for an
* Object. This contains the name of the class which created the
* Object.
* Parameters:
* this
* Pointer to the Object.
* Returned Value:
* Pointer to a string containing the class name.
* Notes:
* - This function does not check the global error status before
* executing. This is to allow it to be used to obtain class names
* for inclusion in error messages.
* - A pointer to an explanatory string will be returned if this
* function is given a pointer which does not identify an Object.
*-
*/
/* Local Variables: */
const char *name; /* Pointer to returned string */
/* First check if the Object pointer supplied is NULL, and set the
returned pointer accordingly. */
if ( !this ) {
name = "<NULL>";
/* Also check if the supposed Object has the correct "magic number" in
its check field. If not, it is not an Object. */
} else if ( this->check != Magic( this, this->size, status ) ) {
name = "<unknown>";
/* If OK, obtain a pointer to the class name from the Object's virtual
function table. */
} else {
name = this->vtab->class;
}
/* Return the result. */
return name;
}
int astGetNobject_( const AstObject *this, int *status ) {
/*
*+
* Name:
* astGetNobject
* Purpose:
* Obtain the value of the Nobject attribute for an Object.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* int astGetNobject( const AstObject *this )
* Class Membership:
* Object method.
* Description:
* This function returns the value of the Nobject attribute for an
* Object. This is a count of the number of active Objects in the
* same class as the Object supplied. This count does not include
* Objects in derived classes.
* Parameters:
* this
* Pointer to the Object.
* Returned Value:
* The number of active Objects.
* Notes:
* - A value of zero will be returned if this function is invoked
* with the global error status set, or if it should fail for any
* reason.
*-
*/
/* Check the global error status. */
if ( !astOK ) return 0;
/* Return the active object count. */
return this->vtab->nobject;
}
static int GetObjSize( AstObject *this, int *status ) {
/*
*+
* Name:
* astGetObjSize
* Purpose:
* Determine the in-memory size of the Object.
* Type:
* Protected virtual function.
* Synopsis:
* #include "object.h"
* int astGetObjSize( AstObject *this )
* Class Membership:
* Object method.
* Description:
* This function returns the in-memory size of an Object.
* Parameters:
* this
* Pointer to the Object.
* Returned Value:
* The Object size, in bytes.
* Notes:
* - A value of zero will be returned if this function is invoked
* with the global error status set, or if it should fail for any
* reason.
*-
*/
/* Check the global error status. */
if ( !astOK ) return 0;
/* Return the object size. */
return this->size;
}
void *astGetProxy_( AstObject *this, int *status ) {
/*
*+
* Name:
* astGetProxy
* Purpose:
* Get a pointer to the foreign language proxy used to represent a
* given AST Object.
* Type:
* Undocumented public function.
* Synopsis:
* #include "object.h"
* void *astGetProxy( AstObject *this )
* Class Membership:
* Object method.
* Description:
* This function returns any pointer stored previously in the AST
* Object using astSetProxy. If no such pointer has been stored, a
* NULL pointer is returned.
* Parameters:
* this
* Pointer to the Object.
* Returned Value:
* Pointer to the proxy object, or NULL.
* Notes:
* - This function is public, but is currently undocumented since it
* is only of interest to people writing AST interfaces for other
* languages.
* - This function attempts to execute even if the AST error status
* is set on entry, although no further error report will be made
* if it subsequently fails under these circumstances.
*-
*/
return this ? this->proxy : NULL;
}
int astGetRefCount_( AstObject *this, int *status ) {
/*
*+
* Name:
* astGetRefCount
* Purpose:
* Obtain the value of the RefCount attribute for an Object.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* int astGetRefCount( const AstObject *this )
* Class Membership:
* Object method.
* Description:
* This function returns the value of the read-only RefCount
* attribute for an Object. This is a "reference count" of the
* number of active pointers to it, as accounted for by astClone
* and astAnnul (plus the pointer issued when it was created). If
* the reference count for an Object falls to zero when astAnnul is
* invoked, the object will be deleted.
* Parameters:
* this
* Pointer to the Object.
* Returned Value:
* The reference count.
* Notes:
* - A value of zero will be returned if this function is invoked
* with the global error status set, or if it should fail for any
* reason.
*-
*/
/* Local Variables; */
int result; /* Returned value */
/* Check the global error status. */
if ( !astOK ) return 0;
/* Get a lock on the object's secondary mutex. This mutex guards access
to the "ref_count" and "locker" components of the AstObject structure. */
LOCK_SMUTEX(this);
/* Get the reference count. */
result = this->ref_count;
/* Unlock the object's secondary mutex. */
UNLOCK_SMUTEX(this);
/* Return the result. */
return result;
}
/*
*++
* Name:
c astGet<X>
f AST_GET<X>
* Purpose:
* Get an attribute value for an Object.
* Type:
* Public functions.
* Synopsis:
c #include "object.h"
c <X>type astGet<X>( AstObject *this, const char *attrib )
f RESULT = AST_GET<X>( THIS, ATTRIB, STATUS )
* Class Membership:
* Object methods.
* Description:
* This is a family of functions which return a specified attribute
* value for an Object using one of several different data
* types. The type is selected by replacing <X> in the function name
c by C, D, F, I or L, to obtain a result in const char* (i.e. string),
c double, float, int, or long format, respectively.
f by C, D, I, L or R, to obtain a result in Character, Double
f precision, Integer, Logical or Real format, respectively.
*
* If possible, the attribute value is converted to the type you
* request. If conversion is not possible, an error will result.
* Parameters:
c this
f THIS = INTEGER (Given)
* Pointer to the Object.
c attrib
f ATTRIB = CHARACTER * ( * ) (Given)
c Pointer to a null-terminated string containing the name of
c the attribute whose value is required.
f A character string containing the name of the attribute whose
f value is required.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Returned Value:
c astGet<X>()
f AST_GET<X> = <X>type
c The attribute value, in the data type corresponding to <X> (or,
c in the case of astGetC, a pointer to a constant null-terminated
c character string containing this value).
f The attribute value, in the data type corresponding to <X>.
* Applicability:
* Object
* These functions apply to all Objects.
* Examples:
c printf( "RefCount = %d\n", astGetI( z, "RefCount" ) );
c Prints the RefCount attribute value for Object "z" as an int.
c title = astGetC( axis, "Title" );
c Obtains a pointer to a null-terminated character string containing
c the Title attribute of Object "axis".
f WRITE( *, '('' RefCount = '', A10 )' ) AST_GETC( Z, 'RefCount', STATUS )
f Prints the RefCount attribute value for Object Z as a character
f string.
f NAXES = AST_GETI( FRAME, 'Naxes', STATUS )
f Obtains the value of the Naxes attribute for Object FRAME as an
f integer.
* Notes:
* - Attribute names are not case sensitive and may be surrounded
* by white space.
* - An appropriate "null" value will be returned if this function
c is invoked with the AST error status set, or if it should
f is invoked with STATUS set to an error value, or if it should
* fail for any reason. This null value is zero for numeric
c values and NULL for pointer values.
f values, .FALSE. for logical values, and blank for character values.
f - Numerical attribute values of zero translate to logical value
f .FALSE. and all other numerical values translate to .TRUE..
c - The pointer returned by astGetC is guaranteed to remain valid
c and the string to which it points will not be over-written for a
c total of 50 successive invocations of this function. After this,
c the memory containing the string may be re-used, so a copy of
c the string should be made if it is needed for longer than this.
*--
*/
/* Define a macro that expands to implement the astGetX_ member
functions required. The arguments to this macro are:
code
The character that appears at the end of the function name.
type
The C type of the function return value.
format
A quoted string containing a astSscanf format specifier that
will read the attribute value into a variable of the required
data type. This format should transfer 1 astSscanf value.
*/
#define MAKE_GETX(code,type,format) \
type astGet##code##_( AstObject *this, const char *attrib, int *status ) { \
\
/* Local Variables: */ \
const char *str; /* Pointer to string attribute value */ \
int nc; /* Number of characters read from string */ \
int nval; /* Number of values read from string */ \
type result; /* Value to return */ \
type value; /* Converted value */ \
\
/* Initialise. */ \
result = (type) 0; \
\
/* Check the global error status. */ \
if ( !astOK ) return result; \
\
/* Obtain the attribute value as a string. */ \
str = Get( this, attrib, status ); \
if ( astOK ) { \
\
/* Read the value from the string, ignoring surrounding white \
space. */ \
nc = 0; \
nval = astSscanf( str, " " format " %n", &value, &nc ); \
\
/* Check that the number of values read was 1 and that all the \
string's characters were consumed. If so, use the result. */ \
if ( ( nval == 1 ) && ( nc >= (int) strlen( str ) ) ) { \
result = value; \
\
/* If the read was unsuccessful, report an error. */ \
} else if( astOK ) { \
astError( AST__ATGER, "astGet" #code "(%s): The attribute " \
"value \"%s=%s\" cannot be read using the requested data " \
"type.", status,astGetClass( this ), attrib, str ); \
} \
} \
\
/* Return the result. */ \
return result; \
}
/* Use this macro to create all the GetX_ private member functions,
except SetC (which is handled separately). */
MAKE_GETX(D,double,"%lf")
MAKE_GETX(F,float,"%f")
MAKE_GETX(I,int,"%d")
MAKE_GETX(L,long,"%ld")
/* Handle GetC separately because memory must be allocated to hold the
returned character values. */
const char *astGetC_( AstObject *this, const char *attrib, int *status ) {
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
const char *result; /* Pointer value to return */
const char *value; /* Pointer to attribute value */
int i; /* Loop count */
/* Initialise. */
result = NULL;
/* Check the global error status. */
if ( !astOK ) return result;
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(this);
/* If the "strings" array has not been initialised, fill it with
NULL pointers. */
if ( !astgetc_init ) {
astgetc_init = 1;
for ( i = 0; i < AST__ASTGETC_MAX_STRINGS; i++ ) astgetc_strings[ i ] = NULL;
}
/* Obtain a pointer to the required attribute value, formatted as a
character string. */
value = Get( this, attrib, status );
/* Use a null string if a NULL pointer was returned by Get. */
if( !value ) value = "";
/* If OK, store a copy of the resulting string in dynamically
allocated memory, putting a pointer to the copy into the next
element of the "astgetc_strings" array. (This process also de-allocates
any previously allocated memory pointed at by this "strings"
element, so the earlier string is effectively replaced by the new
one.) */
if ( astOK ) {
astBeginPM;
astgetc_strings[ astgetc_istr ] = astStore( astgetc_strings[ astgetc_istr ],
value, strlen( value ) + (size_t) 1 );
astEndPM;
/* If OK, return a pointer to the copy and increment "astgetc_istr" to use the
next element of "astgetc_strings" on the next invocation. Recycle
"astgetc_istr" to zero when all elements have been used. */
if ( astOK ) {
result = astgetc_strings[ astgetc_istr++ ];
if ( astgetc_istr == ( AST__ASTGETC_MAX_STRINGS - 1 ) ) astgetc_istr = 0;
}
}
/* Return the result. */
return result;
}
static int HasAttribute( AstObject *this, const char *attrib, int *status ) {
/*
*++
* Name:
c astHasAttribute
f AST_HASATTRIBUTE
* Purpose:
* Test if an Object has a named attribute.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c int astHasAttribute( AstObject *this, const char *attrib )
f RESULT = AST_HASATTRIBUTE( THIS, ATTRIB, STATUS )
* Class Membership:
* Object method.
* Description:
c This function returns a boolean result (0 or 1) to indicate
f This function returns a logical result to indicate
* whether the supplied Object has an attribute with the supplied name.
* Parameters:
c this
f THIS = INTEGER (Given)
* Pointer to the first Object.
c attrib
f ATTRIB = INTEGER (Given)
c Pointer to a string holding the
f The
* name of the attribute to be tested.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Returned Value:
c astHasAttribute()
c One if the Object has the named attribute, otherwise zero.
f AST_SAME = LOGICAL
f .TRUE. if the Object has the named attribute, otherwise
f .FALSE.
* Applicability:
* Object
c This function applies to all Objects.
f This routine applies to all Objects.
* Notes:
c - A value of zero will be returned if this function is invoked
c with the AST error status set, or if it should fail for any reason.
f - A value of .FALSE. will be returned if this function is invoked
f with STATUS set to an error value, or if it should fail for any reason.
*--
*/
/* Local Variables: */
int oldrep; /* Original AST error reporting flag */
int result; /* Returned value */
/* Check the global error status. */
if ( !astOK ) return 0;
/* Temporarily switch off error reporting. */
oldrep = astReporting( 0 );
/* Attempt to get a value for the specified attribute. */
(void) Get( this, attrib, status );
/* An error will have been reported if the object does not have the
requested attribute. Set the result and clear the error status. */
if( !astOK ) {
result = 0;
astClearStatus;
} else {
result = 1;
}
/* Re-instate the original error reporting flag. */
(void) astReporting( oldrep );
/* Return the result. */
return result;
}
static unsigned long Magic( const AstObject *this, size_t size, int *status ) {
/*
* Name:
* Magic
* Purpose:
* Generate a "magic number" for an Object.
* Type:
* Private function.
* Synopsis:
* #include "object.h"
* unsigned long Magic( const AstObject *this, size_t size, int *status )
* Class Membership:
* Object member function.
* Description:
* This function generates a "magic number" which is a function of an Object
* pointer (address) and an Object size. This number may be stored in an
* Object to allow it to be recognised as a valid Object by other routines
* and to provide security against argument passing errors, etc.
* Parameters:
* this
* Pointer to an Object.
* size
* The Object size.
* status
* Pointer to the inherited status variable.
* Returned Value:
* The magic number.
* Notes:
* - This function does not perform any error checking.
*/
/* Form the bit-wise exclusive OR between the Object address and the Object
size, then add 2 and invert the bits. Return the result as an unsigned
long integer. */
return ~( ( ( (unsigned long) this ) ^ ( (unsigned long) size ) ) +
( (unsigned long) 2 ) );
}
#if defined(THREAD_SAFE)
static int ManageLock( AstObject *this, int mode, int extra,
AstObject **fail, int *status ) {
/*
*+
* Name:
* astManageLock
* Purpose:
* Manage the thread lock on an Object.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* int astManageLock( AstObject *this, int mode, int extra,
* AstObject **fail )
* Class Membership:
* Object method.
* Description:
* This function manages the thread lock on the supplied Object. The
* lock can be locked, unlocked or checked by this function as
* deteremined by parameter "mode". See astLock for details of the way
* these locks are used.
* Parameters:
* this
* Pointer to the Object.
* mode
* An integer flag indicating what the function should do:
*
* AST__LOCK: Lock the Object for exclusive use by the calling
* thread. The "extra" value indicates what should be done if the
* Object is already locked (wait or report an error - see astLock).
*
* AST__UNLOCK: Unlock the Object for use by other threads.
*
* AST__CHECKLOCK: Check that the object is locked for use by the
* calling thread.
* extra
* Extra mode-specific information.
* fail
* If a non-zero function value is returned, a pointer to the
* Object that caused the failure is returned at "*fail". This may
* be "this" or it may be an Object contained within "this". Note,
* the Object's reference count is not incremented, and so the
* returned pointer should not be annulled. A NULL pointer is
* returned if this function returns a value of zero.
* Returned Value:
* A status value:
* 0 - Success.
* 1 - Could not lock or unlock the object because it was already
* locked by another thread.
* 2 - Failed to lock a POSIX mutex
* 3 - Failed to unlock a POSIX mutex
* 4 - Bad "mode" value supplied.
* 5 - Check failed - object is locked by a different thread
* 6 - Check failed - object is unlocked
*
* Notes:
* - This function attempts to execute even if an error has already
* occurred.
*-
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
int result; /* Returned value */
/* Initialise */
result = 0;
if( fail ) *fail = NULL;
/* Check the supplied point is not NULL. */
if( ! this ) return result;
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(NULL);
/* Get a lock on the object's secondary mutex. This gives us exclusive
access to the "locker" (and "ref_count") component in the AstObject
structure. All other components in the structure are guarded by the
primary mutex (this->mutex1). */
if( LOCK_SMUTEX(this) ) {
result = 2;
/* If the secondary mutex was locked succesfully, first deal with cases
where the caller wants to lock the Object for exclusive use by the
calling thread. */
} else if( mode == AST__LOCK ) {
/* If the Object is not currently locked, lock the Object primary mutex
and record the identity of the calling thread in the Object. */
if( this->locker == -1 ) {
if( LOCK_PMUTEX(this) ) result = 2;
this->locker = AST__THREAD_ID;
this->globals = AST__GLOBALS;
ChangeThreadVtab( this, status );
/* If the Object is already locked by the calling thread, do nothing. */
} else if( this->locker == AST__THREAD_ID ) {
/* If the object is locked by a different thread, and the caller is
willing to wait, attempt to lock the Object primary mutex. This will
cause the calling thread to block until the Object is release by the
thread that currently has it locked. Then store the identity of the
calling thread (the new lock owner). We first need to release the
secondary mutex so that the other thread can modify the "locker"
component in the AstObject structure when it releases the Object
(using this function). We then re-lock the secondary mutex so this
thread can change the "locker" component safely. */
} else if( extra ) {
if( UNLOCK_SMUTEX(this) ) {
result = 3;
} else if( LOCK_PMUTEX(this) ) {
result = 2;
} else if( LOCK_SMUTEX(this) ) {
result = 2;
}
this->locker = AST__THREAD_ID;
this->globals = AST__GLOBALS;
ChangeThreadVtab( this, status );
/* If the caller does not want to wait until the Object is available,
return a status of 1. */
} else {
result = 1;
}
/* Unlock the Object for use by other threads. */
} else if( mode == AST__UNLOCK ) {
/* Do nothing if the Object is currently unlocked. */
if( this->locker == -1 ) {
/* If the object is currently locked by the calling thread, clear the
identity of the thread that owns the lock and unlock the primary
mutex. */
} else if( this->locker == AST__THREAD_ID ) {
this->locker = -1;
this->globals = NULL;
if( UNLOCK_PMUTEX(this) ) result = 3;
/* Return an error status value if the Object is locked by another
thread. */
} else {
result = 1;
}
/* Check the Object is locked by the calling thread. Return a status of 1 if
not. */
} else if( mode == AST__CHECKLOCK ) {
if( this->locker == -1 ) {
result = 6;
} else if( this->locker != AST__THREAD_ID ) {
result = 5;
}
/* Return a status of 4 for any other modes. */
} else {
result = 4;
}
/* Unlock the secondary mutex so that other threads can access the "locker"
component in the Object to see if it is locked. */
if( UNLOCK_SMUTEX(this) ) result = 3;
/* If the operation failed, return a pointer to the failed object. */
if( result && fail ) *fail = this;
/* Return the status value */
return result;
}
#endif
char *astToString_( AstObject *this, int *status ) {
/*
c++
* Name:
* astToString
* Purpose:
* Create an in-memory serialisation of an Object
* Type:
* Public function.
* Synopsis:
* #include "object.h"
* char *astToString( AstObject *this )
* Class Membership:
* Object method.
* Description:
* This function returns a string holding a minimal textual
* serialisation of the supplied AST Object. The Object can re
* re-created from the serialisation using astFromString.
* Parameters:
* this
* Pointer to the Object to be serialised.
* Returned Value:
* astToString()
* Pointer to dynamically allocated memory holding the
* serialisation, or NULL if an error occurs. The pointer
* should be freed when no longer needed using astFree.
c--
*/
/* Local Variables: */
StringData data; /* Data passed to the sink function */
AstChannel *channel; /* Pointer to output Channel */
/* Check the global error status. */
if ( !astOK ) return NULL;
/* Create a Channel which will write to an expanding dynamically
allocated memory buffer. Set Channel attributes to exclude all
non-essential characters. */
channel = astChannel( NULL, ToStringSink, "Comment=0,Full=-1,Indent=0",
status );
/* Initialise the data structure used to communicate with the sink
function, and store a pointer to it in the Channel. */
data.ptr = NULL;
data.buff = NULL;
data.len = 0;
astPutChannelData( channel, &data );
/* Write the Object to the Channel. */
astWrite( channel, this );
/* Annul the Channel pointer. */
channel = astAnnul( channel );
/* Free the returned string if an error has occurred. */
if( !astOK ) data.ptr = astFree( data.ptr );
/* Return the pointer. */
return data.ptr;
}
static void ToStringSink( const char *text ){
/*
* Name:
* ToStringSink
* Purpose:
* A Channel sink function for use by the astToString method.
* Type:
* Private function.
* Synopsis:
* #include "object.h"
* ToStringSink( const char *text )
* Class Membership:
* Object member function.
* Description:
* This function appends the supplied line of text to the end of a
* dynamically growing memory block.
* Parameters:
* text
* Pointer to the null-terminated line of text to be stored.
*/
/* Local Variables: */
StringData *data; /* Data passed to the sink function */
int *status; /* Pointer to local status value */
int status_value; /* Local status value */
/* Set up the local status */
status_value = 0;
status = &status_value;
/* Get a pointer to the structure holding the current memory pointer and
the length of the currently allocated memory. */
data = astChannelData;
/* Append the supplied text to the end of the string, and update the
string length. */
data->ptr = astAppendString( data->ptr, &(data->len), text );
/* Append a newline character to the end of the string, and update the
string length. */
data->ptr = astAppendString( data->ptr, &(data->len), "\n" );
}
void astSet_( void *this_void, const char *settings, int *status, ... ) {
/*
*++
* Name:
c astSet
f AST_SET
* Purpose:
* Set attribute values for an Object.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c void astSet( AstObject *this, const char *settings, ... )
f CALL AST_SET( THIS, SETTINGS, STATUS )
* Class Membership:
* Object method.
* Description:
c This function assigns a set of attribute values to an Object,
f This routine assigns a set of attribute values to an Object,
* over-riding any previous values. The attributes and their new
* values are specified via a character string, which should
* contain a comma-separated list of the form:
*
* "attribute_1 = value_1, attribute_2 = value_2, ... "
*
* where "attribute_n" specifies an attribute name, and the value
* to the right of each "=" sign should be a suitable textual
* representation of the value to be assigned. This value will be
* interpreted according to the attribute's data type.
c
c The string supplied may also contain "printf"-style format
c specifiers, identified by "%" signs in the usual way. If
c present, these will be substituted by values supplied as
c additional optional arguments (using the normal "printf" rules)
c before the string is used.
* Parameters:
c this
f THIS = INTEGER (Given)
* Pointer to the Object.
c settings
f SETTINGS = CHARACTER * ( * ) (Given)
c Pointer to a null-terminated character string containing a
c comma-separated list of attribute settings in the form described
c above.
f A character string containing a comma-separated list of
f attribute settings in the form described above.
c ...
c Optional additional arguments which supply values to be
c substituted for any "printf"-style format specifiers that
c appear in the "settings" string.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Applicability:
* Object
c This function applies to all Objects.
f This routine applies to all Objects.
* Examples:
c astSet( map, "Report = 1, Zoom = 25.0" );
c Sets the Report attribute for Object "map" to the value 1 and
c the Zoom attribute to 25.0.
c astSet( frame, "Label( %d ) =Offset along axis %d", axis, axis );
c Sets the Label(axis) attribute for Object "frame" to a
c suitable string, where the axis number is obtained from
c "axis", a variable of type int.
c astSet( frame, "Title =%s", mystring );
c Sets the Title attribute for Object "frame" to the contents of
c the string "mystring".
f CALL AST_SET( MAP, 'Report = 1, Zoom = 25.0', STATUS )
f Sets the Report attribute for Object MAP to the value 1 and
f the Zoom attribute to 25.0.
f CALL AST_SET( FRAME, 'Label( 1 ) =Offset from cluster axis', STATUS )
f Sets the Label(1) attribute for Object FRAME to a suitable
f string.
* Notes:
* - Attribute names are not case sensitive and may be surrounded
* by white space.
* - White space may also surround attribute values, where it will
* generally be ignored (except for string-valued attributes where
* it is significant and forms part of the value to be assigned).
* - To include a literal comma in the value assigned to an attribute,
* the whole attribute value should be enclosed in quotation markes.
c Alternatively, you can use "%s" format and supply the value as a
c separate additional argument to astSet (or use the astSetC
c function instead).
c - The same procedure may be adopted if "%" signs are to be included
c and are not to be interpreted as format specifiers (alternatively,
c the "printf" convention of writing "%%" may be used).
* - An error will result if an attempt is made to set a value for
* a read-only attribute.
*--
* Implementation Notes:
* - Because this function has a variable argument list, it is
* invoked by a macro that evaluates to a function pointer (not a
* function invocation) and no checking or casting of arguments is
* performed before the function is invoked. Because of this, the
* Object identifier is of type (void *) and is converted and
* validated within the function itself.
* - This implementation of astSet is designed to be used within AST,
* and has an explicit status parameter. From outside AST, the astSet
* macro will invoke the astSetId_ function which does not have an
* status parameter.
*--
*/
/* Local Variables: */
AstObject *this; /* Pointer to the Object structure */
va_list args; /* Variable argument list */
/* Check the global error status. */
if ( !astOK ) return;
/* Obtain and validate a pointer to the Object structure. */
this = astCheckObject( this_void );
if ( astOK ) {
/* Obtain the variable argument list and pass all arguments to the
astVSet method for interpretation. */
va_start( args, status );
astVSet( this, settings, NULL, args );
va_end( args );
}
}
static void SetAttrib( AstObject *this, const char *setting, int *status ) {
/*
*+
* Name:
* astSetAttrib
* Purpose:
* Set an attribute value for an Object.
* Type:
* Protected virtual function.
* Synopsis:
* #include "object.h"
* void astSetAttrib( AstObject *this, const char *setting )
* Class Membership:
* Object method.
* Description:
* This function assigns an attribute value for an Object, the attribute and
* its value being specified by means of a string of the form:
*
* "attribute= value "
*
* Here, "attribute" specifies the attribute name and should be in lower
* case with no white space present. The value to the right of the "="
* should be a suitable textual representation of the value to be assigned
* and this will be interpreted according to the attribute's data type.
* White space surrounding the value is only significant for string
* attributes.
* Parameters:
* this
* Pointer to the Object.
* setting
* Pointer to a null-terminated string specifying the new attribute
* value.
* Notes:
* - The Object class does not have any writable attributes, so
* this function merely reports an error. It is intended to be
* extended by other class definitions.
*-
*/
/* Local Variables: */
int id; /* Offset of ID string */
int ival; /* Integer attribute value */
int len; /* Length of setting string */
int nc; /* Number of characters read by astSscanf */
/* Check the global error status. */
if ( !astOK ) return;
/* Obtain the length of the setting string. */
len = (int) strlen( setting );
/* Test for each recognised attribute in turn, using "astSscanf" to parse
the setting string and extract the attribute value (or an offset to
it in the case of string values). In each case, use the value set
in "nc" to check that the entire string was matched. Once a value
has been obtained, use the appropriate method to set it. */
/* ID. */
/* --- */
if ( nc = 0, ( 0 == astSscanf( setting, "id=%n%*[^\n]%n", &id, &nc ) )
&& ( nc >= len ) ) {
astSetID( this, setting + id );
/* Ident. */
/* ------ */
} else if ( nc = 0, ( 0 == astSscanf( setting, "ident=%n%*[^\n]%n", &id, &nc ) )
&& ( nc >= len ) ) {
astSetIdent( this, setting + id );
/* UseDefs */
/* ------- */
} else if ( nc = 0,
( 1 == astSscanf( setting, "usedefs= %d %n", &ival, &nc ) )
&& ( nc >= len ) ) {
astSetUseDefs( this, ival );
/* Define a macro to see if the setting string matches any of the
read-only attributes of this class and use this to report an error
if it does. */
#define MATCH(attrib) \
( nc = 0, ( 0 == astSscanf( setting, attrib "=%*[^\n]%n", &nc ) ) && \
( nc >= len ) )
} else if ( MATCH( "class" ) ||
MATCH( "nobject" ) ||
MATCH( "objsize" ) ||
MATCH( "refcount" ) ) {
astError( AST__NOWRT, "astSet: The setting \"%s\" is invalid for a %s.", status,
setting, astGetClass( this ) );
astError( AST__NOWRT, "This is a read-only attribute." , status);
/* Since no writable attributes are defined for the Object class, any
attempt to set a value for anything else is also an error. */
} else {
astError( AST__BADAT, "astSet: The attribute setting \"%s\" is invalid "
"for a %s.", status, setting, astGetClass( this ) );
}
/* Undefine macros local to this function. */
#undef MATCH
}
void astSetCopy_( AstObjectVtab *vtab,
void (* copy)( const AstObject *, AstObject *, int * ), int *status ) {
/*
*+
* Name:
* astSetCopy
* Purpose:
* Declare a copy constructor for an Object.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* void astSetCopy( AstObjectVtab *vtab,
* void (* copy)( const AstObject *, AstObject * ) )
* Class Membership:
* Object method.
* Description:
* This function is provided so that class definitions can declare a copy
* constructor to be associated with an Object that is being constructed.
* When a copy is later performed on the Object, the copy constructor of
* each class to which the Object belongs will be invoked in turn (working
* down the class hierarchy). The copy constructor is passed pointers to the
* source and destination Objects. It should implement the copy and return
* void.
* Parameters:
* vtab
* Pointer to the Object's virtual function table, in which the copy
* constructor's pointer is to be stored for future use.
* copy
* Pointer to the copy constructor function.
* Notes:
* - When an Object is copied, a byte-by-byte copy of its structure is
* automatically made before any copy constructors are invoked. A copy
* constructor need only be provided if this does not suffice (e.g. if the
* structure contains pointers to other data).
* - If a copy constructor is declared for a class, then a
* destructor for that class must also be declared (using
* astSetDelete) so that there is a one-to-one correspondence
* between copy constructors and their associated destructors.
* - Copy constructors should check the global error status in the normal
* way and should set it (and report an error) if they fail.
*-
*/
/* Check the global status. */
if ( !astOK ) return;
/* Indicate that subsequent memory allocations may never be freed (other
than by any AST exit handler). */
astBeginPM;
/* Expand the array of copy constructor pointers in the virtual function table
(if necessary) to accommodate the new one. */
vtab->copy = astGrow( vtab->copy, vtab->ncopy + 1,
sizeof( void (*)( const AstObject *, AstObject * ) ) );
/* If OK, store the new function pointer and increment the count of copy
constructors. */
if ( astOK ) {
vtab->copy[ vtab->ncopy++ ] = copy;
}
/* Mark the end of the section in which memory allocations may never be freed
(other than by any AST exit handler). */
astEndPM;
}
void astSetDelete_( AstObjectVtab *vtab, void (* delete)( AstObject *, int * ), int *status ) {
/*
*+
* Name:
* astSetDelete
* Purpose:
* Declare a destructor for an Object.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* void astSetDelete( AstObjectVtab *vtab, void (* delete)( AstObject * ) )
* Class Membership:
* Object method.
* Description:
* This function is provided so that class definitions can declare a
* destructor to be associated with an Object. When the Object is later
* deleted, the destructor declared by each class to which the Object
* belongs will be invoked in turn (working up the class hierarchy). The
* destructor is passed a pointer to the Object. It should free any
* resources (e.g. memory) associated with it and return void. It should
* not free the memory containing the Object itself.
* Parameters:
* vtab
* Pointer to the Object's virtual function table, in which the
* destructor's pointer is to be stored for future use.
* delete
* Pointer to the destructor function.
* Notes:
* - A destructor need not be declared for a class if there are no
* resources to free.
* - If a destructor is declared for a class, then a copy
* constructor for that class must also be declared (using
* astSetCopy) so that there is a one-to-one correspondence between
* copy constructors and their associated destructors.
* - A destructor function should generally attempt to execute even
* if the global error status is set on entry, but should not
* report further errors in that case (errors should be reported
* normally if status is not set on entry).
*-
*/
/* Check the global status. */
if ( !astOK ) return;
/* Indicate that subsequent memory allocations may never be freed (other
than by any AST exit handler). */
astBeginPM;
/* Expand the array of destructor pointers in the virtual function table (if
necessary) to accommodate the new one. */
vtab->delete = astGrow( vtab->delete, vtab->ndelete + 1,
sizeof( void (*)( AstObject * ) ) );
/* If OK, store the new function pointer and increment the count of
destructors. */
if ( astOK ) {
vtab->delete[ vtab->ndelete++ ] = delete;
}
/* Mark the end of the section in which memory allocations may never be freed
(other than by any AST exit handler). */
astEndPM;
}
void astSetDump_( AstObjectVtab *vtab,
void (* dump)( AstObject *, AstChannel *, int * ),
const char *class, const char *comment, int *status ) {
/*
*+
* Name:
* astSetDump
* Purpose:
* Declare a dump function for an Object.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* void astSetDump( AstObjectVtab *vtab,
* void (* dump)( AstObject *, AstChannel * ),
* const char *class, const char *comment )
* Class Membership:
* Object method.
* Description:
* This function is provided so that class definitions can declare
* a dump function to be associated with an Object that is being
* constructed. When the astWrite (or astShow or astToString) method
* is later used to write the Object to a Channel, the dump function
* of each class to which the Object belongs will be invoked in turn
* (working down the class hierarchy). The dump function is passed
* pointers to the Object and the output Channel. It should write
* out any internal values (e.g. instance variables) for its class
* that are to be kept (using the protected astWrite... methods of
* the Channel) and return void.
* Parameters:
* vtab
* Pointer to the Object's virtual function table, in which the
* dump function's pointer is to be stored for future use.
* dump
* Pointer to the dump function.
* class
* Pointer to a constant null-terminated string (residing in
* static memory) containing the name of the class that is
* declaring the dump function.
* comment
* Pointer to a constant null-terminated string (residing in
* static memory) containing a comment to associate with the
* dump function. This should normally describe the purpose of
* the class that is declaring the dump function.
* Notes:
* - Dump functions should check the global error status in the
* normal way and should set it (and report an error) if they fail.
*-
*/
/* Check the global error status. */
if ( !astOK ) return;
/* Indicate that subsequent memory allocations may never be freed (other
than by any AST exit handler). */
astBeginPM;
/* Expand the arrays of pointers to dump functions and related data in
the virtual function table (if necessary) to accommodate the new
one. */
vtab->dump = astGrow( vtab->dump, vtab->ndump + 1,
sizeof( void (*)( AstObject *, AstChannel * ) ) );
vtab->dump_class = astGrow( vtab->dump_class, vtab->ndump + 1,
sizeof( char * ) );
vtab->dump_comment = astGrow( vtab->dump_comment, vtab->ndump + 1,
sizeof( char * ) );
/* If OK, store the new pointers (to the dump function, class name and
comment) and increment the count of dump functions. */
if ( astOK ) {
vtab->dump[ vtab->ndump ] = dump;
vtab->dump_class[ vtab->ndump ] = class;
vtab->dump_comment[ vtab->ndump ] = comment;
vtab->ndump++;
}
/* Mark the end of the section in which memory allocations may never be
freed (other than by any AST exit handler). */
astEndPM;
}
void astSetProxy_( AstObject *this, void *proxy, int *status ) {
/*
*+
* Name:
* astSetProxy
* Purpose:
* Store a pointer to the foreign language proxy used to represent a
* given AST Object.
* Type:
* Undocumented public function.
* Synopsis:
* #include "object.h"
* void astSetProxy( AstObject *this, void *proxy )
* Class Membership:
* Object method.
* Description:
* This function stores the supplied pointer in the AST Object so that
* it can be retrieved later using astGetProxy.
*
* The supplied pointer should point to a structure that is used
* to represent the AST Object within some external system. It is
* expected that the external system will check each object reference
* returned by AST to see if it has an associated proxy object. If not
* (i.e. if astGetProxy returns NULL), a new external object will be
* created to represent the AST Object, and a pointer to it will be
* stored in the AST Object using astSetProxy. If the AST Object
* already has a proxy, the AST reference is annulled and the existing
* proxy object is used by the external system.
* Parameters:
* this
* Pointer to the Object.
* proxy
* Pointer to the proxy object, or NULL.
* Notes:
* - The suppied pointer is not used within AST itself, other than to
* be returned by the astGetProxy method.
* - This function is public, but is currently undocumented since it
* is only of interest to people writing AST interfaces for other
* languages.
*-
*/
if( !astOK ) return;
this->proxy = proxy;
}
void astSetVtab_( AstObject *this, AstObjectVtab *vtab, int *status ) {
/*
*+
* Name:
* astSetVtab
* Purpose:
* Change the virtual function table associated with an Object.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* void astSetVtab( AstObject *this, AstObjectVtab *vtab )
* Class Membership:
* Object method.
* Description:
* This function changes the virtual function table associated with an
* Object. This may be needed, for instance, if a super-class
* initialises a parent class structure with a NULL vtab, causing the
* vtab of the parent class to be used instead of the super-class.
* Whilst the super-class object is being constructed its inherited methods
* will be determined by the parent class. Once the super-class object
* has been constructed, it can invoke this fuction in order to
* set the vtab to the super-class vtab, thus causing the method
* implementations provided by the super-cvlass to be used.
* Parameters:
* this
* Pointer to the Object to be modified.
* vtab
* Pointer to the virtual function table to store in the Object.
*-
*/
if( this ) this->vtab = vtab;
}
static int Same( AstObject *this, AstObject *that, int *status ) {
/*
*++
* Name:
c astSame
f AST_SAME
* Purpose:
* Test if two AST pointers refer to the same Object.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c int astSame( AstObject *this, AstObject *that )
f RESULT = AST_SAME( THIS, THAT, STATUS )
* Class Membership:
* Object method.
* Description:
c This function returns a boolean result (0 or 1) to indicate
f This function returns a logical result to indicate
* whether two pointers refer to the same Object.
* Parameters:
c this
f THIS = INTEGER (Given)
* Pointer to the first Object.
c that
f THAT = INTEGER (Given)
* Pointer to the second Object.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Returned Value:
c astSame()
c One if the two pointers refer to the same Object, otherwise zero.
f AST_SAME = LOGICAL
f .TRUE. if the two pointers refer to the same Object, otherwise
f .FALSE.
* Applicability:
* Object
c This function applies to all Objects.
f This routine applies to all Objects.
* Notes:
* - Two independent Objects that happen to be identical are not
* considered to be the same Object by this function.
c - A value of zero will be returned if this function is invoked
c with the AST error status set, or if it should fail for any reason.
f - A value of .FALSE. will be returned if this function is invoked
f with STATUS set to an error value, or if it should fail for any reason.
*--
*/
/* Check the global error status. */
if ( !astOK ) return 0;
/* Return the result. */
return ( this == that ) ? 1 : 0;
}
/*
*++
* Name:
c astSet<X>
f AST_SET<X>
* Purpose:
* Set an attribute value for an Object.
* Type:
* Public functions.
* Synopsis:
c #include "object.h"
c void astSet<X>( AstObject *this, const char *attrib, <X>type value )
f CALL AST_SET<X>( THIS, ATTRIB, VALUE, STATUS )
* Class Membership:
* Object methods.
* Description:
c This is a family of functions which set a specified attribute
f This is a family of routines which set a specified attribute
* value for an Object using one of several different data
c types. The type is selected by replacing <X> in the function name
f types. The type is selected by replacing <X> in the routine name
c by C, D, F, I or L, to supply a value in const char* (i.e. string),
c double, float, int, or long format, respectively.
f by C, D, I, L or R, to supply a value in Character, Double
f precision, Integer, Logical or Real format, respectively.
*
* If possible, the value you supply is converted to the type of
* the attribute. If conversion is not possible, an error will
* result.
* Parameters:
c this
f THIS = INTEGER (Given)
* Pointer to the Object.
c attrib
f ATTRIB = CHARACTER * ( * ) (Given)
c Pointer to a null-terminated character string containing the
c name of the attribute whose value is to be set.
f A character string containing the name of the attribute whose
f value is to be set.
c value
f VALUE = <X>type (Given)
c The value to be set for the attribute, in the data type corresponding
c to <X> (or, in the case of astSetC, a pointer to a null-terminated
c character string containing this value).
f The value to be set for the attribute, in the data type corresponding
f to <X>.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Applicability:
* Object
c These functions apply to all Objects.
f These routines apply to all Objects.
* Examples:
c astSetI( frame, "Preserve", 1 );
c Sets the Preserve attribute value for Object "frame" to 1.
c astSetC( plot, "Format(1)", "%.2g" );
c Sets the Format(1) attribute value for Object "plot" to the
c character string "%.2g".
f CALL AST_SETC( PLOT, 'Title', CVALUE, STATUS )
f Sets the Title attribute value for Object PLOT to the contents
f of the character variable CVALUE.
f CALL AST_SETL( FRAME, 'Preserve', .TRUE., STATUS );
f Sets the Preserve attribute value for Object FRAME to 1 (true).
* Notes:
* - Attribute names are not case sensitive and may be surrounded
* by white space.
f - The logical value .FALSE. will translate to a numerical attribute
f value of zero and logical .TRUE. will translate to one.
* - An error will result if an attempt is made to set a value for
* a read-only attribute.
*--
*/
/* Define a macro that expands to implement the astSetX_ member
functions required. The arguments to this macro are:
code
The character that appears at the end of the function name.
type
The C type of the function "value" parameter.
format
A quoted string containing a sprintf format specifier that will
format the supplied value as a character string. This format should
consume 2 sprintf arguments: a field width and the value to be
formatted.
fmtlen
The number of characters in the format specifier (above).
fieldsz
The value of the field width to be used by the format specifier.
*/
#define MAKE_SETX(code,type,format,fmtlen,fieldsz) \
void astSet##code##_( AstObject *this, const char *attrib, type value, int *status ) { \
\
/* Local Variables: */ \
char *setting; /* Pointer to attribute setting string */ \
int len; /* Length of attribute name */ \
\
/* Check the global status. */ \
if ( !astOK ) return; \
\
/* Obtain the length of the attribute name and allocate memory to hold \
this name plus the format specifier to be appended to it. */ \
len = (int) astChrLen( attrib ); \
setting = astMalloc( (size_t) ( len + fmtlen + 2 ) ); \
\
/* Make a copy of the attribute name in the allocated memory. */ \
if ( astOK ) { \
(void) memcpy( setting, attrib, (size_t) len ); \
setting[ len ] = 0; \
\
/* Append "=", followed by the format specifier, to construct a \
suitable "setting" string for use by astSet. */ \
(void) strcat( setting, "=" format ); \
\
/* Invoke astSet to set the attribute value. */ \
astSet( this, setting, status, fieldsz, value ); \
} \
\
/* Free the allocated memory. */ \
setting = astFree( setting ); \
}
/* Use this macro to create all the SetX_ private member functions. */
MAKE_SETX(D,double,"%.*g",4,AST__DBL_DIG)
MAKE_SETX(F,float,"%.*g",4,FLT_DIG)
MAKE_SETX(I,int,"%.*d",4,1)
MAKE_SETX(L,long,"%.*ld",5,1)
/* The astSetC_ function is implemented separately so that commas can be
handled. Since astSetC can only be used to set a single attribute
value, we know that any commas in the supplied value are included
within the attribuite value, rather than being used as delimiters
between adjacent attribute settings. To avoid VSet using them as
delimiters, they are replaced here by '\r' before calling astSet, and
VSet then converts them back to commas. */
void astSetC_( AstObject *this, const char *attrib, const char *value, int *status ) {
/* Local Variables: */
char *d; /* Pointer to next setting character */
char *newv; /* Pointer to new attribute value string */
char *setting; /* Pointer to attribute setting string */
const char *c; /* Pointer to next value character */
int len; /* Length of attribute name */
/* Check the global status. */
if ( !astOK ) return;
/* Produce a copy of the supplied attribute value in which any commas
are replaced by carriage returns ("\r"). */
newv = astMalloc( (size_t)( strlen( value ) + 1 ) );
if( newv ) {
d = newv;
c = value;
while( *c ) {
if( *c == ',' ) {
*d = '\r';
} else {
*d = *c;
}
c++;
d++;
}
*d = 0;
/* Obtain the length of the attribute name and allocate memory to hold
this name plus the format specifier to be appended to it. */
len = (int) astChrLen( attrib );
setting = astMalloc( (size_t) ( len + 5 ) );
/* Make a copy of the attribute name in the allocated memory. */
if ( astOK ) {
(void) memcpy( setting, attrib, (size_t) len );
setting[ len ] = 0;
/* Append "=", followed by the format specifier, to construct a
suitable "setting" string for use by astSet. */
(void) strcat( setting, "=%*s" );
/* Invoke astSet to set the attribute value. */
astSet( this, setting, status, 0, newv );
}
/* Free the allocated memory. */
setting = astFree( setting );
}
newv = astFree( newv );
}
static void Show( AstObject *this, int *status ) {
/*
*++
* Name:
c astShow
f AST_SHOW
* Purpose:
* Display a textual representation of an Object on standard output.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c void astShow( AstObject *this )
f CALL AST_SHOW( THIS, STATUS )
* Class Membership:
* Object method.
* Description:
c This function displays a textual description of any AST Object
f This routine displays a textual description of any AST Object
* on standard output. It is provided primarily as an aid to
* debugging.
* Parameters:
c this
f THIS = INTEGER (Given)
* Pointer to the Object to be displayed.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Applicability:
* Object
c This function applies to all Objects.
f This routine applies to all Objects.
*--
*/
/* Local Variables: */
AstChannel *channel; /* Pointer to output Channel */
/* Check the global error status. */
if ( !astOK ) return;
/* Create a Channel which will write to standard output. */
channel = astChannel( NULL, NULL, "", status );
/* Write the Object to the Channel. */
astWrite( channel, this );
/* Annul the Channel pointer. */
channel = astAnnul( channel );
}
int astTest_( AstObject *this, const char *attrib, int *status ) {
/*
*++
* Name:
c astTest
f AST_TEST
* Purpose:
* Test if an Object attribute value is set.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c int astTest( AstObject *this, const char *attrib )
f RESULT = AST_TEST( THIS, ATTRIB, STATUS )
* Class Membership:
* Object method.
* Description:
c This function returns a boolean result (0 or 1) to indicate
f This function returns a logical result to indicate
* whether a value has been explicitly set for one of an Object's
* attributes.
* Parameters:
c this
f THIS = INTEGER (Given)
* Pointer to the Object.
c attrib
f ATTRIB = CHARACTER * ( * ) (Given)
c Pointer to a null-terminated character string containing
c the name of the attribute to be tested.
f A character string containing the name of the attribute to be
f tested.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Returned Value:
c astTest()
c One if a value has previously been explicitly set for the attribute
c (and hasn't been cleared), otherwise zero.
f AST_TEST = LOGICAL
f .TRUE. if a value has previously been explicitly set for the
f attribute (and hasn't been cleared), otherwise .FALSE..
* Applicability:
* Object
c This function applies to all Objects.
f This routine applies to all Objects.
* Notes:
* - Attribute names are not case sensitive and may be surrounded
* by white space.
c - A value of zero will be returned if this function is invoked
f - A value of .FALSE. will be returned if this function is invoked
c with the AST error status set, or if it should fail for any reason.
f with STATUS set to an error value, or if it should fail for any reason.
c - A value of zero will also be returned if this function is used
f - A value of .FALSE. will also be returned if this function is used
* to test a read-only attribute, although no error will result.
*--
*/
/* Local Variables: */
char *buff; /* Pointer to character buffer */
int i; /* Loop counter for characters */
int j; /* Non-blank character count */
int len; /* Length of attrib string */
int result; /* Result value to return */
/* Initialise. */
result = 0;
/* Check the global error status. */
if ( !astOK ) return result;
/* Obtain the length of the attrib string. */
len = (int) strlen( attrib );
/* Allocate memory and store a copy of the string. */
buff = astStore( NULL, attrib, (size_t) ( len + 1 ) );
if ( astOK ) {
/* Remove white space and upper case characters. */
for ( i = j = 0; buff[ i ]; i++ ) {
if ( !isspace( buff[ i ] ) ) buff[ j++ ] = tolower( buff[ i ] );
}
/* Terminate the attribute name and pass it to astTestAttrib to test
the attribute. */
buff[ j ] = '\0';
result = astTestAttrib( this, buff );
}
/* Free the memory allocated for the string buffer. */
buff = astFree( buff );
/* If an error occurred, clear the result value. */
if ( !astOK ) result = 0;
/* Return the result. */
return result;
}
static int TestAttrib( AstObject *this, const char *attrib, int *status ) {
/*
*+
* Name:
* astTestAttrib
* Purpose:
* Test if a specified attribute value is set for an Object.
* Type:
* Protected virtual function.
* Synopsis:
* #include "object.h"
* int astTestAttrib( AstObject *this, const char *attrib )
* Class Membership:
* Object method.
* Description:
* This function returns a boolean result (0 or 1) to indicate whether
* a value has been set for one of an Object's attributes.
* Parameters:
* this
* Pointer to the Object.
* attrib
* Pointer to a null-terminated string specifying the attribute
* name. This should be in lower case with no surrounding white
* space.
* Returned Value:
* One if a value has been set, otherwise zero.
* Notes:
* - A value of zero will be returned if this function is invoked
* with the global status set, or if it should fail for any reason.
*-
*/
/* Local Variables: */
int result; /* Result value to return */
/* Initialise. */
result = 0;
/* Check the global error status. */
if ( !astOK ) return result;
/* Check the attribute name and test the appropriate attribute. */
/* ID. */
/* --- */
if ( !strcmp( attrib, "id" ) ) {
result = astTestID( this );
/* Ident. */
/* ------ */
} else if ( !strcmp( attrib, "ident" ) ) {
result = astTestIdent( this );
/* UseDefs */
/* ------- */
} else if ( !strcmp( attrib, "usedefs" ) ) {
result = astTestUseDefs( this );
/* Test if the attribute string matches any of the read-only
attributes of this class. If it does, then return zero. */
} else if ( !strcmp( attrib, "class" ) ||
!strcmp( attrib, "nobject" ) ||
!strcmp( attrib, "objsize" ) ||
!strcmp( attrib, "refcount" ) ) {
result = 0;
/* Any attempt to test any other attribute is an error. */
} else if( astOK ){
astError( AST__BADAT, "astTest: The attribute name \"%s\" is invalid "
"for a %s.", status, attrib, astGetClass( this ) );
}
/* Return the result, */
return result;
}
int astTune_( const char *name, int value, int *status ) {
/*
*++
* Name:
c astTune
f AST_TUNE
* Purpose:
* Set or get an integer-valued AST global tuning parameter.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c int astTune( const char *name, int value )
f RESULT = AST_TUNE( NAME, VALUE, STATUS )
* Class Membership:
* Object function.
* Description:
* This function returns the current value of an integer-valued AST
* global tuning parameter, optionally storing a new value for the
* parameter. For character-valued tuning parameters, see
c astTuneC.
f AST_TUNEC.
* Parameters:
c name
f NAME = CHARACTER * ( * ) (Given)
* The name of the tuning parameter (case-insensitive).
c value
f VALUE = INTEGER (Given)
* The new value for the tuning parameter. If this is AST__TUNULL,
* the existing current value will be retained.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Returned Value:
c astTune()
f AST_TUNE = INTEGER
c The original value of the tuning parameter. A default value will
* be returned if no value has been set for the parameter.
* Tuning Parameters:
* ObjectCaching
* A boolean flag which indicates what should happen
* to the memory occupied by an AST Object when the Object is deleted
* (i.e. when its reference count falls to zero or it is deleted using
c astDelete).
f AST_DELETE).
* If this is zero, the memory is simply freed using the systems "free"
* function. If it is non-zero, the memory is not freed. Instead a
* pointer to it is stored in a pool of such pointers, all of which
* refer to allocated but currently unused blocks of memory. This allows
* AST to speed up subsequent Object creation by re-using previously
* allocated memory blocks rather than allocating new memory using the
* systems malloc function. The default value for this parameter is
* zero. Setting it to a non-zero value will result in Object memory
* being cached in future. Setting it back to zero causes any memory
* blocks currently in the pool to be freed. Note, this tuning parameter
* only controls the caching of memory used to store AST Objects. To
* cache other memory blocks allocated by AST, use MemoryCaching.
* MemoryCaching
* A boolean flag similar to ObjectCaching except
* that it controls caching of all memory blocks of less than 300 bytes
* allocated by AST (whether for internal or external use), not just
* memory used to store AST Objects.
* Notes:
c - This function attempts to execute even if the AST error
c status is set
f - This routine attempts to execute even if STATUS is set to an
f error value
* on entry, although no further error report will be
* made if it subsequently fails under these circumstances.
* - All threads in a process share the same AST tuning parameters
* values.
*--
*/
int result = AST__TUNULL;
if( name ) {
LOCK_MUTEX1;
if( astChrMatch( name, "ObjectCaching" ) ) {
result = object_caching;
if( value != AST__TUNULL ) {
object_caching = value;
if( !object_caching ) EmptyObjectCache( status );
}
} else if( astChrMatch( name, "MemoryCaching" ) ) {
result = astMemCaching( value );
} else if( astOK ) {
astError( AST__TUNAM, "astTune: Unknown AST tuning parameter "
"specified \"%s\".", status, name );
}
UNLOCK_MUTEX1;
}
return result;
}
void astTuneC_( const char *name, const char *value, char *buff,
int bufflen, int *status ) {
/*
*++
* Name:
c astTuneC
f AST_TUNEC
* Purpose:
* Set or get a character-valued AST global tuning parameter.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c void astTuneC( const char *name, const char *value, char *buff,
c int bufflen )
f CALL AST_TUNEC( NAME, VALUE, BUFF, STATUS )
* Class Membership:
* Object function.
* Description:
* This function returns the current value of a character-valued
* AST global tuning parameter, optionally storing a new value
* for the parameter. For integer-valued tuning parameters, see
c astTune.
f AST_TUNE.
* Parameters:
c name
f NAME = CHARACTER * ( * ) (Given)
* The name of the tuning parameter (case-insensitive).
c value
f VALUE = CHARACTER * ( ) (Given)
* The new value for the tuning parameter. If this is
f AST__TUNULLC,
c NULL,
* the existing current value will be retained.
c buff
f BUFF = CHARACTER * ( ) (Given)
* A character string in which to return the original value of
* the tuning parameter. An error will be reported if the buffer
* is too small to hold the value.
c NULL may be supplied if the old value is not required.
c bufflen
c The size of the supplied "buff" array. Ignored if "buff" is NULL.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Tuning Parameters:
* HRDel
* A string to be drawn following the hours field in a formatted
* sky axis value when "g" format is in use (see the Format
* attribute). This string may include escape sequences to produce
* super-scripts, etc. (see the Escapes attribute for details
* of the escape sequences allowed). The default value is
* "%-%^50+%s70+h%+" which produces a super-script "h".
* MNDel
* A string to be drawn following the minutes field in a formatted
* sky axis value when "g" format is in use. The default value is
* "%-%^50+%s70+m%+" which produces a super-script "m".
* SCDel
* A string to be drawn following the seconds field in a formatted
* sky axis value when "g" format is in use. The default value is
* "%-%^50+%s70+s%+" which produces a super-script "s".
* DGDel
* A string to be drawn following the degrees field in a formatted
* sky axis value when "g" format is in use. The default value is
* "%-%^53+%s60+o%+" which produces a super-script "o".
* AMDel
* A string to be drawn following the arc-minutes field in a formatted
* sky axis value when "g" format is in use. The default value is
* "%-%^20+%s85+'%+" which produces a super-script "'" (single quote).
* ASDel
* A string to be drawn following the arc-seconds field in a formatted
* sky axis value when "g" format is in use. The default value is
* "%-%^20+%s85+\"%+" which produces a super-script """ (double quote).
* EXDel
* A string to be drawn to introduce the exponent in a value when "g"
* format is in use. The default value is "10%-%^50+%s70+" which
* produces "10" followed by the exponent as a super-script.
* Notes:
c - This function attempts to execute even if the AST error
c status is set
f - This routine attempts to execute even if STATUS is set to an
f error value
* on entry, although no further error report will be
* made if it subsequently fails under these circumstances.
* - All threads in a process share the same AST tuning parameters
* values.
*--
*/
/* Local Variables: */
char *p = NULL;
int len;
/* Check the name of a tuning parameter was supplied. */
if( name ) {
/* Serialise access to the tuning parameters since they are common to all
threads. */
LOCK_MUTEX1;
/* Get a pointer to the buffer that holds the value of the requested
tuning parameter. */
if( astChrMatch( name, "hrdel" ) ) {
p = hrdel;
} else if( astChrMatch( name, "mndel" ) ) {
p = mndel;
} else if( astChrMatch( name, "scdel" ) ) {
p = scdel;
} else if( astChrMatch( name, "dgdel" ) ) {
p = dgdel;
} else if( astChrMatch( name, "amdel" ) ) {
p = amdel;
} else if( astChrMatch( name, "asdel" ) ) {
p = asdel;
} else if( astChrMatch( name, "exdel" ) ) {
p = exdel;
/* Report an error if an the tuning parameter name is unknown. */
} else if( astOK ) {
p = NULL;
astError( AST__TUNAM, "astTuneC: Unknown AST tuning parameter "
"specified \"%s\".", status, name );
}
/* If the existing value was found. */
if( p ) {
/* And is to be returned in the supplied buffer... */
if( buff ) {
/* Check that the buffer is long enough. If so, copy the current value
into the buffer, otherwise report an error. */
len = strlen( p ) ;
if( len < bufflen ) {
strcpy( buff, p );
} else {
astError( AST__TUNAM, "astTuneC: Supplied string variable "
"is too small - the current '%s' value (%s) has "
"%d characters.", status, name, p, len );
}
}
/* If a new value is to be stored.... */
if( value ) {
/* Report an error if it is too long to fit in the static buffer. */
len = strlen( value ) ;
if( len >= MAXLEN_TUNEC ) {
astError( AST__TUNAM, "astTuneC: Supplied value for '%s' "
"(%s) is too long - must not be longer than %d "
"characters.", status, name, value, MAXLEN_TUNEC );
/* Otherwise, copy the new value into the static buffer. */
} else {
strcpy( p, value );
}
}
}
UNLOCK_MUTEX1;
}
}
AstObject *astFromString_( const char *string, int *status ) {
/*
c++
* Name:
* astFromString
* Purpose:
* Re-create an Object from an in-memory serialisation
* Type:
* Public function.
* Synopsis:
* #include "object.h"
* AstObject *astFromString( const char *string )
* Class Membership:
* Object method.
* Description:
* This function returns a pointer to a new Object created from the
* supplied text string, which should have been created by astToString.
* Parameters:
* string
* Pointer to a text string holding an Object serialisation created
* previously by astToString.
* Returned Value:
* astFromString()
* Pointer to a new Object created from the supplied serialisation,
* or NULL if the serialisation was invalid, or an error occurred.
c--
*/
/* Local Variables: */
StringData data; /* Data passed to the source function */
AstChannel *channel; /* Pointer to output Channel */
AstObject *result; /* Pointer to returned Object */
/* Check the global error status and supplied serialisation. */
if ( !astOK || !string ) return NULL;
/* Create a Channel which will read from the supplied serialisation. */
channel = astChannel( FromStringSource, NULL, "", status );
/* Initialise the data structure used to communicate with the source
function, and store a pointer to it in the Channel. */
data.ptr = (char *) string;
data.buff = NULL;
data.len = 0;
astPutChannelData( channel, &data );
/* Read an Object from the Channel. */
result = astRead( channel );
/* Annul the Channel pointer. */
channel = astAnnul( channel );
/* Free the line buffer. */
data.buff = astFree( data.buff );
/* Annul the returned Object if an error has occurred. */
if( !astOK ) result = astAnnul( result );
/* Return the Object pointer. */
return result;
}
static const char *FromStringSource( void ){
/*
* Name:
* FromStringSource
* Purpose:
* A Channel source function for use by the astFromString method.
* Type:
* Private function.
* Synopsis:
* #include "object.h"
* result = FromStringSource( void )
* Class Membership:
* Object member function.
* Description:
* This function reads the next line of text from a serialisation and
* returns a pointer to it, or NULL if no lines remain.
* Returned Value:
* Pointer to the null terminated line of text or NULL if no lines
* remain.
*/
/* Local Variables: */
StringData *data; /* Data passed to the sink function */
char *nl; /* Pointer to next newline character */
int *status; /* Pointer to local status value */
int nc; /* Number of characters to read from serialisation */
int status_value; /* Local status value */
/* Set up the local status */
status_value = 0;
status = &status_value;
/* Get a pointer to the structure holding a pointer to the next line, and
to the buffer to return. */
data = astChannelData;
/* Return NULL if no text remains to be read. */
if( !data->ptr || (data->ptr)[0] == 0 ) return NULL;
/* Find the next newline (if any) in the serialisation. */
nl = strchr( data->ptr, '\n' );
/* Get the number of characters to copy. */
nc = nl ? nl - data->ptr : strlen( data->ptr );
/* Copy them into the returned buffer, including an extra character for
the terminating null. */
data->buff = astStore( data->buff, data->ptr, nc + 1 );
/* Store the terminating null. */
(data->buff)[ nc ] = 0;
/* Update the pointer to the next character to read from the
serialisation. */
data->ptr = nl ? nl + 1 : NULL;
/* Return the buffer. */
return data->buff;
}
static void VSet( AstObject *this, const char *settings, char **text,
va_list args, int *status ) {
/*
*+
* Name:
* astVSet
* Purpose:
* Set values for an Object's attributes.
* Type:
* Protected virtual function.
* Synopsis:
* #include "object.h"
* void astVSet( AstObject *this, const char *settings, char **text,
* va_list args )
* Class Membership:
* Object method.
* Description:
* This function assigns a set of attribute values for an Object,
* the attributes and their values being specified by means of a
* string containing a comma-separated list of the form:
*
* "attribute1 = value1, attribute2 = value2, ... "
*
* Here, "attribute" specifies an attribute name and the value to
* the right of each "=" sign should be a suitable textual
* representation of the value to be assigned to that
* attribute. This will be interpreted according to the attribute's
* data type.
*
* The string supplied may also contain "printf"-style format
* specifiers identified by a "%" sign in the usual way. If
* present, these will be substituted by values supplied as
* optional arguments (as a va_list variable argument list), using
* the normal "printf" rules, before the string is used.
* Parameters:
* this
* Pointer to the Object.
* settings
* Pointer to a null-terminated string containing a
* comma-separated list of attribute settings.
* text
* Pointer to a location at which to return a pointer to dynamic
* memory holding a copy of the expanded setting string. This memory
* should be freed using astFree when no longer needed. If a NULL
* pointer is supplied, no string is created.
* args
* The variable argument list which contains values to be
* substituted for any "printf"-style format specifiers that
* appear in the "settings" string.
* Notes:
* - Attribute names are not case sensitive.
* - White space may surround attribute names and will be ignored.
* - White space may also surround attribute values where it will
* be ignored (except for string-valued attributes where it is
* significant and forms part of the value to be assigned).
* - After this function has substituted values for "printf"-style
* format specifiers it splits the "settings" string into
* individual attribute settings which it passes one at a time to
* the protected astSetAttrib method (after removal of white space
* and conversion of attribute names to lower case). The
* astSetAttrib method should therefore be extended by derived
* classes which define new attributes, and this will allow the
* astVSet (and astSet) methods to have access to those attributes.
* - This function provides the same functionality as the astSet
* public method but accepts a va_list variable argument list
* instead of a variable number of arguments. It is provided for
* use by functions in other class implementations which accept a
* variable number of arguments and must therefore pass their
* argument list to this method in va_list form.
*-
*/
#define MIN_BUFF_LEN 1024
#define ERRBUF_LEN 80
/* Local Variables: */
char errbuf[ ERRBUF_LEN ]; /* Buffer for system error message */
char setting_buf[ MIN_BUFF_LEN ]; /* Expanded "%s" settting string */
char *dyn_buf; /* Pointer to dynamic buffer for expanded setting */
char *errstat; /* Pointer to error message */
char *assign; /* Pointer to assigment substring */
char *assign_end; /* Pointer to null at end of assignment */
char *buff1; /* Pointer to temporary string buffer */
char *buff2; /* Pointer to temporary string buffer */
char *buff3; /* Pointer to temporary string buffer */
char *eq1; /* Pointer to 1st equals sign */
int buff_len; /* Length of temporary buffer */
int expanded; /* Has the Settings string been expanded yet? */
int i; /* Loop counter for characters */
int j; /* Offset for revised assignment character */
int len; /* Length of settings string */
int lo; /* Convert next character to lower case? */
int nc; /* Number of vsprintf output characters */
int quoted; /* Are we in a quoted string? */
int stat; /* Value of errno after an error */
int tq; /* Test if the next non-space is a quote? */
/* Initialise */
if( text ) *text = NULL;
/* Check the global error status. */
if ( !astOK ) return;
/* Obtain the length of the "settings" string and test it is not
zero. If it is, there is nothing more to do. */
len = (int) strlen( settings );
if ( len != 0 ) {
/* If the setting string is just "%s" (with optional trailing and leading
white space) then the variable argument potentially contains more than
one attribute setting, in which case we expand the setting string now
and use the expanded string in place of the supplied string in the rest
of this function. */
nc = 0;
sscanf( settings, " %%s %n", &nc );
if( nc == len ) {
/* Expand the supplied string using a fixed-length buffer. This writes at
most MIN_BUFF_LEN characters to "buf", but returns the number of
characters that would have been needed to write the whole string. */
len = vsnprintf( setting_buf, sizeof(setting_buf), settings, args );
/* If the fixed-length buffer is too short, use a dynamically allocated
buffer instead. */
if( len + 1 > MIN_BUFF_LEN ) {
dyn_buf = astMalloc( len + 1 );
if( astOK ) {
len = vsnprintf( dyn_buf, len + 1, settings, args );
settings = dyn_buf;
}
} else {
dyn_buf = NULL;
settings = setting_buf;
}
/* Indicate that "settings" has been expanded. */
expanded = 1;
} else {
expanded = 0;
dyn_buf = NULL;
}
/* Allocate memory and store a copy of the string. */
buff1 = astStore( NULL, settings, (size_t) ( len + 1 ) );
if ( astOK ) {
/* Convert each comma in the string into '\n'. This is to distinguish
commas initially present from those introduced by the formatting to
be performed below. We only do this if there is more than one equals
sign in the setting string, since otherwise any commas are probably
characters contained within a string attribute value. Ignore commas
that occur within quoted strings. */
eq1 = strchr( buff1, '=' );
if( eq1 && strchr( eq1 + 1, '=' ) ) {
quoted = 0;
for ( i = 0; i < len; i++ ) {
if( !quoted ) {
if ( buff1[ i ] == ',' ) {
buff1[ i ] = '\n';
} else if( buff1[ i ] == '"' ) {
quoted = 1;
}
} else if( buff1[ i ] == '"' ){
quoted = 0;
}
}
}
/* Calculate a size for a further buffer twice the size of the first
one. Ensure it is not less than a minimum size and then allocate
this buffer. */
buff_len = 2 * len;
if ( buff_len < MIN_BUFF_LEN ) buff_len = MIN_BUFF_LEN;
buff2 = astMalloc( (size_t) ( buff_len + 1 ) );
if ( astOK ) {
/* Use "vsprintf" to substitute values for any format specifiers in
the "settings" string, writing the resulting string into the second
buffer. If the "settings" string has already been expanded, then just
copy it. */
errno = 0;
if( !expanded ) {
nc = vsprintf( buff2, buff1, args );
} else {
strcpy( buff2, buff1 );
nc = strlen( buff1 );
}
/* Get a copy of the expanded string to return as the function value and
convert newlines back to commas. */
if( text ) {
*text = astStore( NULL, buff2, nc + 1 );
if( *text ) {
for ( i = 0; i <= nc; i++ ) {
if ( (*text)[ i ] == '\n' ) (*text)[ i ] = ',';
}
}
}
/* The possibilities for error detection are limited here, but check
if an error value was returned and report an error. Include
information from errno if it was set. */
if ( nc < 0 ) {
if( astOK ) {
stat = errno;
if( stat ) {
#if HAVE_STRERROR_R
strerror_r( stat, errbuf, ERRBUF_LEN );
errstat = errbuf;
#else
errstat = strerror( stat );
#endif
} else {
*errbuf = 0;
errstat = errbuf;
}
astError( AST__ATSER, "astVSet(%s): Error formatting an "
"attribute setting%s%s.", status, astGetClass( this ),
stat? " - " : "", errstat );
astError( AST__ATSER, "The setting string was \"%s\".", status,
settings );
}
/* Also check that the result buffer did not overflow. If it did,
memory will probably have been corrupted but this cannot be
prevented with "vsprintf" (although we try and make the buffer
large enough). Report the error and abort. */
} else if ( nc > buff_len ) {
if( astOK ) {
astError( AST__ATSER, "astVSet(%s): Internal buffer overflow "
"while formatting an attribute setting - the result "
"exceeds %d characters.", status, astGetClass( this ),
buff_len );
astError( AST__ATSER, "The setting string was \"%s\".", status,
settings );
}
/* If all is OK, loop to process each formatted attribute assignment
(these are now separated by '\n' characters). */
} else {
assign = buff2;
while ( assign ) {
/* Change the '\n' at the end of each assignment to a null to
terminate it. */
if ( ( assign_end = strchr( assign, '\n' ) ) ) {
*assign_end = '\0';
}
/* Before making the assignment, loop to remove white space and upper
case characters from the attribute name. */
lo = 1;
tq = -1;
quoted = 0;
for ( i = j = 0; assign[ i ]; i++ ) {
/* Note when an '=' sign is encountered (this signals the end of the
attribute name). */
if ( assign[ i ] == '=' ) lo = 0;
/* Before the '=' sign, convert all characters to lower case and move
everything to the left to eliminate white space. Afer the '=' sign,
copy all characters to their new location unchanged, except for any
delimiting quotes, which are removed. astSetC replaces commas in the
attribute value by '\r' characters. Reverse this now. */
if ( !lo || !isspace( assign[ i ] ) ) {
if( assign[ i ] == '\r' ) {
assign[ j++ ] = ',';
} else if( lo ) {
assign[ j++ ] = tolower( assign[ i ] );
} else {
assign[ j++ ] = assign[ i ];
if( tq > 0 && !isspace( assign[ i ] ) ) {
if( assign[ i ] == '"' ) {
quoted = 1;
j--;
}
tq = 0;
}
}
}
/* If the current character is the initial '=' sign, set "tq" positive,
meaning "check if the next non-space character is a quote". */
if ( assign[ i ] == '=' && tq == -1 ) tq = 1;
}
/* if the value was quoted. remove the trailing quote. */
if( quoted ) {
j--;
while( isspace( assign[ j ] ) ) j--;
if( assign[ j ] == '"' ) j--;
j++;
}
/* Terminate the revised assignment string and pass it to astSetAttrib
to make the assignment (unless the string was all blank, in which
case we ignore it). */
assign[ j ] = '\0';
if ( j ) {
/* If there are no characters to the right of the equals sign append a
space after the equals sign. Without this, a string such as "Title="
would not be succesfully matched against the attribute name "Title"
within SetAttrib. */
if( assign[ j - 1 ] == '=' ) {
buff3 = astStore( NULL, assign,
(size_t) j + 2 );
if ( astOK ) {
buff3[ j ] = ' ';
buff3[ j + 1 ] = '\0';
astSetAttrib( this, buff3 );
}
buff3 = astFree( buff3 );
} else {
astSetAttrib( this, assign );
}
}
/* Check for errors and abort if any assignment fails. Otherwise,
process the next assignment substring. */
if ( !astOK ) break;
assign = assign_end ? assign_end + 1 : NULL;
}
}
}
/* Free the memory allocated for string buffers. */
buff2 = astFree( buff2 );
dyn_buf = astFree( dyn_buf );
}
buff1 = astFree( buff1 );
}
}
#undef ERRBUF_LEN
#undef MIN_BUFF_LEN
/* Attribute access functions. */
/* --------------------------- */
/*
*att++
* Name:
* Class
* Purpose:
* Object class name.
* Type:
* Public attribute.
* Synopsis:
* Character string, read-only.
* Description:
* This attribute gives the name of the class to which an Object
* belongs.
* Applicability:
* Object
* All Objects have this attribute.
*att--
*/
/*
*att++
* Name:
* ID
* Purpose:
* Object identification string.
* Type:
* Public attribute.
* Synopsis:
* String.
* Description:
* This attribute contains a string which may be used to identify
* the Object to which it is attached. There is no restriction on
* the contents of this string, which is not used internally by the
* AST library, and is simply returned without change when
* required. The default value is an empty string.
*
* An identification string can be valuable when, for example,
c several Objects have been stored in a file (using astWrite) and
f several Objects have been stored in a file (using AST_WRITE) and
c are later retrieved (using astRead). Consistent use of the ID
f are later retrieved (using AST_READ). Consistent use of the ID
* attribute allows the retrieved Objects to be identified without
* depending simply on the order in which they were stored.
*
* This attribute may also be useful during debugging, to
c distinguish similar Objects when using astShow to display them.
f distinguish similar Objects when using AST_SHOW to display them.
* Applicability:
* Object
* All Objects have this attribute.
* Notes:
* - Unlike most other attributes, the value of the ID attribute is
* not transferred when an Object is copied. Instead, its value is
* undefined (and therefore defaults to an empty string) in any
* copy. However, it is retained in any external representation of
c an Object produced by the astWrite function.
f an Object produced by the AST_WRITE routine.
*att--
*/
/* Clear the ID value by freeing the allocated memory and assigning a
NULL pointer. */
astMAKE_CLEAR(Object,ID,id,astFree( this->id ))
/* If the ID value is not set, supply a default in the form of a
pointer to the constant string "". */
astMAKE_GET(Object,ID,const char *,NULL,( this->id ? this->id : "" ))
/* Set an ID value by freeing any previously allocated memory,
allocating new memory and storing the string. */
astMAKE_SET(Object,ID,const char *,id,astStore( this->id, value,
strlen( value ) + (size_t) 1 ))
/* The ID value is set if the pointer to it is not NULL. */
astMAKE_TEST(Object,ID,( this->id != NULL ))
/*
*att++
* Name:
* Ident
* Purpose:
* Permanent Object identification string.
* Type:
* Public attribute.
* Synopsis:
* String.
* Description:
* This attribute is like the ID attribute, in that it contains a
* string which may be used to identify the Object to which it is
* attached. The only difference between ID and Ident is that Ident
* is transferred when an Object is copied, but ID is not.
* Applicability:
* Object
* All Objects have this attribute.
*att--
*/
/* Clear the Ident value by freeing the allocated memory and assigning a
NULL pointer. */
astMAKE_CLEAR(Object,Ident,ident,astFree( this->ident ))
/* If the Ident value is not set, supply a default in the form of a
pointer to the constant string "". */
astMAKE_GET(Object,Ident,const char *,NULL,( this->ident ? this->ident : "" ))
/* Set an Ident value by freeing any previously allocated memory,
allocating new memory and storing the string. */
astMAKE_SET(Object,Ident,const char *,ident,astStore( this->ident, value,
strlen( value ) + (size_t) 1 ))
/* The Ident value is set if the pointer to it is not NULL. */
astMAKE_TEST(Object,Ident,( this->ident != NULL ))
/*
*att++
* Name:
* UseDefs
* Purpose:
* Use default values for unspecified attributes?
* Type:
* Public attribute.
* Synopsis:
* Integer (boolean).
* Description:
* This attribute specifies whether default values should be used
* internally for object attributes which have not been assigned a
* value explicitly. If a non-zero value (the default) is supplied for
* UseDefs, then default values will be used for attributes which have
* not explicitly been assigned a value. If zero is supplied for UseDefs,
* then an error will be reported if an attribute for which no explicit
* value has been supplied is needed internally within AST.
*
* Many attributes (including the UseDefs attribute itself) are unaffected
* by the setting of the UseDefs attribute, and default values will always
* be used without error for such attributes. The "Applicability:" section
* below lists the attributes which are affected by the setting of UseDefs.
* Note, UseDefs only affects access to attributes internally within
* AST. The public accessor functions such as
c astGetC
f AST_GETC
* is unaffected by the UseDefs attribute - default values will always
* be returned if no value has been set. Application code should use the
c astTest
f AST_TEST
* function if required to determine if a value has been set for an
* attribute.
* Applicability:
* Object
* All Objects have this attribute, but ignore its setting except
* as described below for individual classes.
* FrameSet
* The default value of UseDefs for a FrameSet is redefined to be
* the UseDefs value of its current Frame.
* CmpFrame
* The default value of UseDefs for a CmpFrame is redefined to be
* the UseDefs value of its first component Frame.
* Region
* The default value of UseDefs for a Region is redefined to be
* the UseDefs value of its encapsulated Frame.
* Frame
* If UseDefs is zero, an error is reported when aligning Frames if the
* Epoch, ObsLat or ObsLon attribute is required but has not been
* assigned a value explicitly.
* SkyFrame
* If UseDefs is zero, an error is reported when aligning SkyFrames
* if any of the following attributes are required but have not been
* assigned a value explicitly: Epoch, Equinox.
* SpecFrame
* If UseDefs is zero, an error is reported when aligning SpecFrames
* if any of the following attributes are required but have not been
* assigned a value explicitly: Epoch, RefRA, RefDec, RestFreq,
* SourceVel, StdOfRest.
* DSBSpecFrame
* If UseDefs is zero, an error is reported when aligning DSBSpecFrames
* or when accessing the ImagFreq attribute if any of the following
* attributes are required but have not been assigned a value explicitly:
* Epoch, DSBCentre, IF.
*att--
*/
astMAKE_CLEAR(Object,UseDefs,usedefs,CHAR_MAX)
astMAKE_GET(Object,UseDefs,int,1,((this->usedefs!=CHAR_MAX)?this->usedefs:1))
astMAKE_SET(Object,UseDefs,int,usedefs,((value)?1:0))
astMAKE_TEST(Object,UseDefs,(this->usedefs!=CHAR_MAX))
/*
*att++
* Name:
* Nobject
* Purpose:
* Number of Objects in class.
* Type:
* Public attribute.
* Synopsis:
* Integer, read-only.
* Description:
* This attribute gives the total number of Objects currently in
* existence in the same class as the Object whose attribute value
* is requested. This count does not include Objects which belong
* to derived (more specialised) classes.
*
* This attribute is mainly intended for debugging. It can be used
* to detect whether Objects which should have been deleted have,
* in fact, been deleted.
* Applicability:
* Object
* All Objects have this attribute.
*att--
*/
/*
*att++
* Name:
* ObjSize
* Purpose:
* The in-memory size of the Object.
* Type:
* Public attribute.
* Synopsis:
* Integer, read-only.
* Description:
* This attribute gives the total number of bytes of memory used by
* the Object. This includes any Objects which are encapsulated within
* the supplied Object.
* Applicability:
* Object
* All Objects have this attribute.
*att--
*/
/*
*att++
* Name:
* RefCount
* Purpose:
* Count of active Object pointers.
* Type:
* Public attribute.
* Synopsis:
* Integer, read-only.
* Description:
* This attribute gives the number of active pointers associated
* with an Object. It is modified whenever pointers are created or
c annulled (by astClone, astAnnul or astEnd for example). The count
f annulled (by AST_CLONE, AST_ANNUL or AST_END for example). The count
* includes the initial pointer issued when the Object was created.
*
* If the reference count for an Object falls to zero as the result
* of annulling a pointer to it, then the Object will be deleted.
* Applicability:
* Object
* All Objects have this attribute.
*att--
*/
/* Standard class functions. */
/* ========================= */
/*
*+
* Name:
* astCheck<Class>
* Purpose:
* Validate class membership.
* Type:
* Protected function.
* Synopsis:
* #include "class.h"
* Ast<Class> *astCheck<Class>( Ast<Class> *this )
* Class Membership:
* <Class> class function.
* Description:
* This function validates membership of the class called <Class>,
* or of any class derived from it. If the Object is not a member,
* or the pointer supplied does not identify a valid Object, an
* error is reported and the global error status is set to
* AST__OBJIN.
* Parameters:
* this
* Pointer to the Object.
* Returned Value:
* The function always returns a copy of the "this" pointer
* (whether it finds it valid or not).
* Notes:
* - Each class provides a function (astCheck<Class>) of this form,
* where <Class> and <class> are replaced by the class name (with
* appropriate capitalisation).
* - Normal error status checking is performed, so this function
* will not execute if the global error status is set on entry (the
* usual function value will be returned, however).
* - This function is primarily intended for validating Object
* pointers passed to member functions as part of a class
* interface.
*-
*/
/* Implement the astCheckObject function using the macro defined for this
purpose in the "object.h" header file. */
astMAKE_CHECK(Object)
int astIsAObject_( const AstObject *this, int *status ) {
/*
*++
* Name:
c astIsA<Class>
f AST_ISA<CLASS>
* Purpose:
* Test membership of a class by an Object.
* Type:
* Public function.
* Synopsis:
c #include "class.h"
c int astIsA<Class>( const Ast<Class> *this )
f RESULT = AST_ISA<CLASS>( THIS, STATUS )
* Class Membership:
* Class function.
* Description:
* This is a family of functions which test whether an Object is a
c member of the class called <Class>, or of any class derived from
f member of the class called <CLASS>, or of any class derived from
* it.
* Parameters:
c this
f THIS = INTEGER (Given)
* Pointer to the Object.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Returned Value:
c astIsA<Class>()
c One if the Object belongs to the class called <Class> (or to a
c class derived from it), otherwise zero.
f AST_ISA<CLASS> = LOGICAL
f .TRUE. if the Object belongs to the class called <CLASS> (or to
f a class derived from it), otherwise .FALSE..
* Applicability:
* Object
* These functions apply to all Objects.
* Examples:
c member = astIsAFrame( obj );
c Tests whether Object "obj" is a member of the Frame class, or
c of any class derived from a Frame.
f MEMBER = AST_ISAFRAME( OBJ, STATUS );
f Tests whether Object OBJ is a member of the Frame class, or
f of any class derived from a Frame.
* Notes:
c - Every AST class provides a function (astIsA<Class>) of this
c form, where <Class> should be replaced by the class name.
f - Every AST class provides a function (AST_ISA<CLASS>) of this
f form, where <CLASS> should be replaced by the class name.
c - This function attempts to execute even if the AST error status
c is set
f - This function attempts to execute even if STATUS is set to an
f error value
* on entry, although no further error report will be made
* if it subsequently fails under these circumstances.
c - A value of zero will be returned if this function should fail
f - A value of .FALSE. will be returned if this function should fail
* for any reason. In particular, it will fail if the pointer
* supplied does not identify an Object of any sort.
*--
*/
/* Local Variables: */
int valid; /* Valid object? */
/* Since this is the base class, the implementation of this function
differs from that in derived classes (in that it fails and
potentially reports an error if the returned result is zero). */
/* Initialise. */
valid = 0;
/* Check if a NULL pointer was supplied (this can never be valid). If
OK, check if the Object contains the correct "magic number" in its
check field. */
if ( !this || ( this->check != Magic( this, this->size, status ) ) ) {
/* If it is not a valid Object, then report an error (but only if the
global error status has not already been set). */
if ( astOK ) {
astError( AST__OBJIN, "astIsAObject(%s): Invalid Object pointer "
"given (points at address %p).", status, astGetClass( this ),
(void *) this );
}
/* Otherwise, note that the Object is valid. */
} else {
valid = 1;
}
/* Return the result. */
return valid;
}
void astInitObjectVtab_( AstObjectVtab *vtab, const char *name, int *status ) {
/*
*+
* Name:
* astInitObjectVtab
* Purpose:
* Initialise a virtual function table for a Object.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* void astInitObjectVtab( AstObjectVtab *vtab, const char *name )
* Class Membership:
* Object vtab initialiser.
* Description:
* This function initialises the component of a virtual function
* table which is used by the Object class.
* Parameters:
* vtab
* Pointer to the virtual function table.
* name
* Pointer to a constant null-terminated character string which contains
* the name of the class to which the virtual function table belongs (it
* is this pointer value that will subsequently be returned by the Object
* astClass function).
*-
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
int ivtab; /* Index of next entry in known_vtabs */
/* Check the local error status. */
if ( !astOK ) return;
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(NULL);
/* Initialise the contents of the class identifier. Since this is the
base class, we assign null values to the fields. */
vtab->id.check = NULL;
vtab->id.parent = NULL;
/* Store pointers to the member functions (implemented here) that provide
virtual methods for this class. */
vtab->Clear = Clear;
vtab->ClearAttrib = ClearAttrib;
vtab->ClearID = ClearID;
vtab->ClearIdent = ClearIdent;
vtab->Dump = Dump;
vtab->Equal = Equal;
vtab->GetAttrib = GetAttrib;
vtab->GetID = GetID;
vtab->GetIdent = GetIdent;
vtab->HasAttribute = HasAttribute;
vtab->Same = Same;
vtab->SetAttrib = SetAttrib;
vtab->SetID = SetID;
vtab->SetIdent = SetIdent;
vtab->Show = Show;
vtab->TestAttrib = TestAttrib;
vtab->TestID = TestID;
vtab->TestIdent = TestIdent;
vtab->EnvSet = EnvSet;
vtab->VSet = VSet;
vtab->Cast = Cast;
vtab->GetObjSize = GetObjSize;
vtab->CleanAttribs = CleanAttribs;
vtab->TestUseDefs = TestUseDefs;
vtab->SetUseDefs = SetUseDefs;
vtab->ClearUseDefs = ClearUseDefs;
vtab->GetUseDefs = GetUseDefs;
#if defined(THREAD_SAFE)
vtab->ManageLock = ManageLock;
#endif
/* Store the pointer to the class name. */
vtab->class = name;
/* Initialise the count of active objects and the number of destructors,
copy constructors and dump functions. */
vtab->nobject = 0;
vtab->ndelete = 0;
vtab->ncopy = 0;
vtab->ndump = 0;
/* Initialise the arrays of destructor, copy constructor and dump
function pointers. */
vtab->delete = NULL;
vtab->copy = NULL;
vtab->dump = NULL;
vtab->dump_class = NULL;
vtab->dump_comment = NULL;
/* Initialise the default attributes to use when creating objects. */
vtab->defaults = NULL;
/* The virtual function table for each class contains a list of pointers
to memory blocks which have previously been used to store an Object of
the same class, but which have since been deleted using astDelete.
These memory blocks are free to be re-used when a new Object of the
same class is initialised. This saves on the overheads associated with
continuously allocating small blocks of memory using malloc. */
vtab->nfree = 0;
vtab->free_list = NULL;
/* Add the supplied virtual function table pointer to the end of the list
of known vtabs. */
ivtab = nvtab++;
astBeginPM;
known_vtabs = astGrow( known_vtabs, nvtab, sizeof( AstObjectVtab *) );
astEndPM;
if( astOK && known_vtabs ) known_vtabs[ ivtab ] = vtab;
/* Fill a pointer value with zeros (not necessarily the same thing as a
NULL pointer) for subsequent use. */
(void) memset( &zero_ptr, 0, sizeof( AstObject * ) );
/* If we have just initialised the vtab for the current class, indicate
that the vtab is now initialised. */
if( vtab == &class_vtab ) class_init = 1;
}
AstObject *astInitObject_( void *mem, size_t size, int init,
AstObjectVtab *vtab, const char *name, int *status ) {
/*
*+
* Name:
* astInitObject
* Purpose:
* Initialise an Object.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* AstObject *astInitObject( void *mem, size_t size, int init,
* AstObjectVtab *vtab, const char *name )
* Class Membership:
* Object initialiser.
* Description:
* This function is provided for use by class implementations to initialise
* a new Object. It allocates memory (if necessary) to accommodate the
* Object plus any additional data associated with the derived class. It
* then initialises an Object structure at the start of this memory. If the
* "init" flag is set, it also initialises the contents of a virtual
* function table for an Object at the start of the memory passed via the
* "vtab" parameter.
* Parameters:
* mem
* A pointer to the memory in which the Object is to be initialised.
* This must be of sufficient size to accommodate the Object data
* (sizeof(Object)) plus any data used by the derived class. If a value
* of NULL is given, this function will allocate the memory itself using
* the "size" parameter to determine its size.
* size
* The amount of memory used by the Object (plus derived class data).
* This will be used to allocate memory if a value of NULL is given for
* the "mem" parameter. This value is also stored in the Object
* structure, so a valid value must be supplied even if not required for
* allocating memory.
* init
* A logical flag indicating if the Object's virtual function table is
* to be initialised. If this value is non-zero, the virtual function
* table will be initialised by this function.
* vtab
* Pointer to the start of the virtual function table to be associated
* with the new Object.
* name
* Pointer to a constant null-terminated character string which contains
* the name of the class to which the new object belongs (it is this
* pointer value that will subsequently be returned by the Object
* astClass function).
* Returned Value:
* A pointer to the new Object.
* Notes:
* - A null pointer will be returned if this function is invoked with the
* global error status set, or if it should fail for any reason.
*-
*/
/* Local Variables: */
AstObject *new; /* Pointer to new Object */
/* Initialise. */
new = NULL;
/* Check the global error status. */
if ( !astOK ) return new;
/* Determine if memory must be allocated dynamically. If so, use the last
block of memory in the list of previously allocated but currently
unused blocks identified by the vtab "free_list" array, reducing the
length of the free list by one, and nullifying the entry in the list
for safety. If the list is originally empty, allocate memory for a new
object using astMalloc. */
if( !mem ) {
if( object_caching ) {
if( vtab->nfree > 0 ) {
mem = vtab->free_list[ --(vtab->nfree) ];
vtab->free_list[ vtab->nfree ] = NULL;
if( astSizeOf( mem ) != size && astOK ) {
astError( AST__INTER, "astInitObject(%s): Free block has size "
"%d but the %s requires %d bytes (internal AST "
"programming error).", status, vtab->class,
(int) astSizeOf( mem ), vtab->class, (int) size );
}
} else {
mem = astMalloc( size );
}
} else {
mem = astMalloc( size );
}
/* If memory had already been allocated, adjust the "size" value to match
the size of the allocated memory. */
} else {
size = astSizeOf( mem );
}
/* Obtain a pointer to the new Object. */
if ( astOK ) {
new = (AstObject *) mem;
/* Zero the entire new Object structure (to prevent accidental re-use
of any of its values after deletion). */
(void) memset( new, 0, size );
/* If necessary, initialise the virtual function table. */
/* ---------------------------------------------------- */
if ( init ) astInitObjectVtab( vtab, name );
if( astOK ) {
/* Initialise the Object data. */
/* --------------------------- */
/* Store a unique "magic" value in the Object structure. This will be
used (e.g. by astIsAObject) to determine if a pointer identifies a
valid Object. Note that this differs from the practice in derived
classes, where this number is stored in the virtual function
table. We take a different approach here so that we need not follow
a pointer to the virtual function table obtained from a structure
that hasn't yet been validated as an Object. This minimises the
risk of a memory access violation. */
new->check = Magic( new, size, status );
/* Associate the Object with its virtual function table. */
new->vtab = vtab;
/* Store the Object size and note if its memory was dynamically allocated. */
new->size = size;
new->dynamic = astIsDynamic( new );
/* Initialise the reference count (of Object pointers in use). */
new->ref_count = 1;
/* Initialise the ID strings. */
new->id = NULL;
new->ident = NULL;
/* Use default values for unspecified attributes. */
new->usedefs = CHAR_MAX;
/* Increment the count of active Objects in the virtual function table.
Use the count as a unique identifier (unique within the class) for
the Object. */
new->iref = vtab->nobject++;
/* Initialise the pointer to an external object that acts as a proxy for
the AST Object within foreign language interfaces. */
new->proxy = NULL;
}
/* If an error occurred, clean up by deleting the new Object. Otherwise
lock the object for use by the currently executing thread. */
if ( !astOK ) {
new = astDelete( new );
#ifdef THREAD_SAFE
} else {
if( pthread_mutex_init( &(new->mutex1), NULL ) != 0 && astOK ) {
astError( AST__INTER, "astInitObject(%s): Failed to "
"initialise POSIX mutex1 for the new Object.", status,
vtab->class );
}
if( pthread_mutex_init( &(new->mutex2), NULL ) != 0 && astOK ) {
astError( AST__INTER, "astInitObject(%s): Failed to "
"initialise POSIX mutex2 for the new Object.", status,
vtab->class );
}
new->locker = -1;
new->globals = NULL;
(void) ManageLock( new, AST__LOCK, 0, NULL, status );
if( !astOK ) new = astDelete( new );
#endif
}
}
/* Return a pointer to the new Object. */
return new;
}
AstObject *astLoadObject_( void *mem, size_t size,
AstObjectVtab *vtab, const char *name,
AstChannel *channel, int *status ) {
/*
*+
* Name:
* astLoadObject
* Purpose:
* Load an Object.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* AstObject *astLoadObject( void *mem, size_t size,
* AstObjectVtab *vtab, const char *name,
* AstChannel *channel )
* Class Membership:
* Object loader.
* Description:
* This function is provided to load a new Object using data read
* from a Channel, and to allocate memory for it if necessary.
*
* If the "init" flag is set, it also initialises the contents of a
* virtual function table for an Object at the start of the memory
* passed via the "vtab" parameter.
* Parameters:
* mem
* A pointer to the memory into which the Object is to be
* loaded. This must be of sufficient size to accommodate the
* Object data (sizeof(Object)) plus any data used by derived
* classes. If a value of NULL is given, this function will
* allocate the memory itself using the "size" parameter to
* determine its size.
* size
* The amount of memory used by the Object (plus derived class
* data). This will be used to allocate memory if a value of
* NULL is given for the "mem" parameter. This value is also
* stored in the Object structure, so a valid value must be
* supplied even if not required for allocating memory.
*
* If the "vtab" parameter is NULL, the "size" value is ignored
* and sizeof(AstObject) is used instead.
* vtab
* Pointer to the start of the virtual function table to be
* associated with the new Object. If this is NULL, a pointer to
* the (static) virtual function table for the Object class is
* used instead.
* name
* Pointer to a constant null-terminated character string which
* contains the name of the class to which the new Object
* belongs (it is this pointer value that will subsequently be
* returned by the astGetClass method).
*
* If the "vtab" parameter is NULL, the "name" value is ignored
* and a pointer to the string "Object" is used instead.
* Returned Value:
* A pointer to the new Object.
* Notes:
* - A null pointer will be returned if this function is invoked
* with the global error status set, or if it should fail for any
* reason.
*-
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
AstObject *new; /* Pointer to the new Object */
/* Initialise. */
new = NULL;
/* Check the global error status. */
if ( !astOK ) return new;
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(channel);
/* If a NULL virtual function table has been supplied, then this is
the first loader to be invoked for this Object. In this case the
Object belongs to this class, so supply appropriate values for
initialising it and its virtual function table. */
if ( !vtab ) {
size = sizeof( AstObject );
vtab = &class_vtab;
name = "Object";
/* If required, initialise the virtual function table for this class. */
if ( !class_init ) {
astInitObjectVtab( vtab, name );
class_init = 1;
}
}
/* There is no parent class to load, so simply initialise a new Object
structure as if a new Object were being created from scratch. */
new = astInitObject( mem, size, 0, vtab, name );
if ( astOK ) {
/* Read input data. */
/* ================ */
/* Request the input Channel to read all the input data appropriate to
this class into the internal "values list". */
astReadClassData( channel, "Object" );
/* Now read each individual data item from this list and use it to
initialise the appropriate instance variable(s) for this class. */
new->id = astReadString( channel, "id", NULL );
new->ident = astReadString( channel, "ident", NULL );
new->usedefs = astReadInt( channel, "usedfs", CHAR_MAX );
/* We simply read the values for the read-only attributes (just in
case they've been un-commented in the external representation) and
discard them. This prevents any possibility of error due to un-read
input. */
(void) astReadInt( channel, "refcnt", 0 );
(void) astReadInt( channel, "nobj", 0 );
/* Initialise the pointer to an external object that acts as a proxy for
the AST Object within foreign language interfaces. */
new->proxy = NULL;
/* If an error occurred, clean up by deleting the new Object. */
if ( !astOK ) new = astDelete( new );
}
/* Return the new Object pointer. */
return new;
}
/* Virtual function interfaces. */
/* ============================ */
/* These provide the external interface to the virtual functions defined by
this class. Each simply checks the global error status and then locates and
executes the appropriate member function, using the function pointer stored
in the object's virtual function table (this pointer is located using the
astMEMBER macro defined in "object.h").
Note that the member function may not be the one defined here, as it may
have been over-ridden by a derived class. However, it should still have the
same interface. */
void astClear_( AstObject *this, const char *attrib, int *status ) {
if ( !astOK ) return;
(**astMEMBER(this,Object,Clear))( this, attrib, status );
}
void astClearAttrib_( AstObject *this, const char *attrib, int *status ) {
if ( !astOK ) return;
(**astMEMBER(this,Object,ClearAttrib))( this, attrib, status );
}
void astDump_( AstObject *this, AstChannel *channel, int *status ) {
if ( !astOK ) return;
(**astMEMBER(this,Object,Dump))( this, channel, status );
}
#if defined(THREAD_SAFE)
int astManageLock_( AstObject *this, int mode, int extra, AstObject **fail,
int *status ) {
if( !this ) return 0;
return (**astMEMBER(this,Object,ManageLock))( this, mode, extra, fail, status );
}
#endif
int astEqual_( AstObject *this, AstObject *that, int *status ) {
if ( !astOK ) return 0;
if( this == that ) return 1;
return (**astMEMBER(this,Object,Equal))( this, that, status );
}
const char *astGetAttrib_( AstObject *this, const char *attrib, int *status ) {
if ( !astOK ) return NULL;
return (**astMEMBER(this,Object,GetAttrib))( this, attrib, status );
}
void astSetAttrib_( AstObject *this, const char *setting, int *status ) {
if ( !astOK ) return;
(**astMEMBER(this,Object,SetAttrib))( this, setting, status );
}
void astShow_( AstObject *this, int *status ) {
if ( !astOK ) return;
(**astMEMBER(this,Object,Show))( this, status );
}
int astTestAttrib_( AstObject *this, const char *attrib, int *status ) {
if ( !astOK ) return 0;
return (**astMEMBER(this,Object,TestAttrib))( this, attrib, status );
}
void astEnvSet_( AstObject *this, int *status ) {
if ( !astOK ) return;
(**astMEMBER(this,Object,EnvSet))( this, status );
}
void astVSet_( AstObject *this, const char *settings, char **text, va_list args, int *status ) {
if ( !astOK ) return;
(**astMEMBER(this,Object,VSet))( this, settings, text, args, status );
}
int astGetObjSize_( AstObject *this, int *status ) {
if ( !astOK || !this ) return 0;
return (**astMEMBER(this,Object,GetObjSize))( this, status );
}
void astCleanAttribs_( AstObject *this, int *status ) {
if ( !astOK ) return;
(**astMEMBER(this,Object,CleanAttribs))( this, status );
}
AstObject *astCast_( AstObject *this, AstObject *obj, int *status ) {
if ( !astOK ) return NULL;
return (**astMEMBER(this,Object,Cast))( this, obj, status );
}
int astSame_( AstObject *this, AstObject *that, int *status ) {
if ( !astOK ) return 0;
return (**astMEMBER(this,Object,Same))( this, that, status );
}
int astHasAttribute_( AstObject *this, const char *attrib, int *status ) {
if ( !astOK ) return 0;
return (**astMEMBER(this,Object,HasAttribute))( this, attrib, status );
}
/* External interface. */
/* =================== */
/* The following relates to the external interface to Objects and not
specifically to the implementation of the Object class itself
(although it contains external functions which replace the internal
versions defined earlier). */
/* Type Definitions. */
/* ----------------- */
/* Define the Handle structure. This is attached to Objects when they
are accessed via the public (external, user-callable) interface.
Handles provide a buffer between the Object identifiers issued to
external users and the naked C pointers used to handle Objects
internally. They also implement the context levels used by
astBegin, astEnd, astExempt and astExport (which are only available
to external users). */
typedef struct Handle {
AstObject *ptr; /* C Pointer to the associated Object */
int context; /* Context level for this Object */
int check; /* Check value to ensure validity */
#if defined(THREAD_SAFE)
int thread; /* Identifier for owning thread */
#endif
#if defined(MEM_DEBUG)
int id; /* The id associated with the memory block
holding the Object last associated with
this handle. */
AstObjectVtab *vtab; /* Pointer to the firtual function table of
the Object last associated with this
handle. */
#endif
/* Links between Handles are implemented using integer offsets rather
than through pointers. */
int flink; /* Backward link to previous Handle */
int blink; /* Forward link to next Handle */
/* Information that records where the Handle was created. */
const char *routine; /* Routine name */
const char *file; /* File name */
int line; /* Line number */
} Handle;
/* Define a union with an overlapping int and AstObject*. This is used
to transfer an integer bit pattern into and out of a pointer
variable used to store an Object identifier. This avoids any
implementation dependent aspects of integer-to-pointer
conversions. */
typedef union IdUnion {
AstObject *pointer;
int integer;
} IdUnion;
/* Define a union which allows a bit pattern to be accessed as a
signed or unsigned int. */
typedef union MixedInts {
int i;
unsigned u;
} MixedInts;
/* Static Variables. */
/* ----------------- */
/* The array of Handle structures is a pool of resources available to all
threads. Each thread has its own conext level and its own "active_handles"
array to identify the first Handle at each context level. */
static Handle *handles = NULL; /* Pointer to allocated array of Handles */
static int free_handles = -1; /* Offset to head of free Handle list */
static int nhandles = 0; /* Number of Handles in "handles" array */
static unsigned nids = 0; /* Number of IDs issued to external users */
#if defined(THREAD_SAFE)
static int unowned_handles = -1; /* Offset to head of unowned Handle
list. In a single threaded environment,
all handles must be owned by a thread. */
#endif
#ifdef MEM_DEBUG
static int Watched_Handle = -1; /* A handle index to be watched. Activity
on this handle is reported using
astHandleUse and astHandleAlarm. */
static int Watched_Pointer = -1; /* A pointer ID value to be watched. */
#endif
/* External Interface Function Prototypes. */
/* --------------------------------------- */
/* MYSTATIC should normally be set to "static" to make the following
function have local symbols. But it may be set to blank for debugging
purposes in order to enable these functions to appear in a backtrace
such as produced by the astBacktrace function. */
#define MYSTATIC
/* Private functions associated with the external interface. */
MYSTATIC AstObject *AssocId( int, int * );
MYSTATIC int CheckId( AstObject *, int, int * );
MYSTATIC void AnnulHandle( int, int * );
MYSTATIC void InitContext( int * );
MYSTATIC void InsertHandle( int, int *, int * );
MYSTATIC void RemoveHandle( int, int *, int * );
#if defined(MEM_DEBUG)
MYSTATIC void CheckList( int *, int * );
MYSTATIC void CheckInList( int, int *, int, int * );
MYSTATIC int CheckThread( int, int *, int * );
MYSTATIC const char *HandleString( int, char * );
MYSTATIC const char *HeadString( int *, char * );
#endif
/* The following functions have public prototypes only (i.e. no
protected prototypes), so we must provide local prototypes for use
within this module. */
AstObject *astDeleteId_( AstObject *, int * );
void astBegin_( void );
void astEnd_( int * );
void astExemptId_( AstObject *, int * );
void astImportId_( AstObject *, int * );
void astSetId_( void *, const char *, ... );
void astLockId_( AstObject *, int, int * );
void astUnlockId_( AstObject *, int, int * );
/* External Interface Functions. */
/* ----------------------------- */
AstKeyMap *astActiveObjects_( const char *class, int subclass, int current,
int *status ) {
/*
c++
* Name:
* astActiveObjects
* Purpose:
* Return pointers for all active Objects.
* Type:
* Public function.
* Synopsis:
* #include "object.h"
* AstKeyMap *astActiveObjects( const char *class, int subclass,
* int current )
* Class Membership:
* Object method.
* Description:
* This function returns a KeyMap holding currently active AST Object
* pointers. Each entry in the KeyMap will have a key that is an AST
* class name such as "FrameSet", "SkyFrame", "ZoomMap", etc. The
* value of the entry will be a 1-dimensional list of pointers for
* objects of the same class. Note, the returned pointers should NOT
* be annulled when they are no longer needed.
*
* The pointers to return in the KeyMap may be restricted either by
* class or by context level using the function arguments.
* Parameters:
* class
* If NULL, then the returned KeyMap will contain pointers ofr
* Objects of all classes. If not NULL, then "class" should be a
* pointer to a null-terminated string holding the name of an AST
* class. The returned KeyMap will contain pointers only for the
* specified class. See also "subclass".
* subclass
* A Boolean flag indicating if all subclasses of the class
* specified by "class" should be included in the returned KeyMap.
* If zero, then subclass objects are not returned. Otherwise they
* are returned. The supplied "subclass" value is ignored if
* "class" is NULL.
* current
* A Boolean flag indicating if the returned list of pointers
* should be restricted to pointers issued within the current AST
* object context (see astBegin and astEnd).
* Returned Value:
* astActiveObjects()
* A pointer to a new KeyMap holding the required object pointers.
* They KeyMap pointer should be annulled when it is no longer
* needed, but the object pointers within the KeyMap should not be
* annulled. A NULL pointer is returned if an error has occurred
* prior to calling this function.
*
* The values stored in the KeyMap should be accessed as generic C
* pointers using the KeyMap "P" data type (e.g. using function
* astMapGetlemP etc).
* Notes:
* - This function will only return objects locked by the currently
* executing thread.
* - The KeyMap pointer returned by this function is not included in the
* list of active objects stored in the KeyMap.
* - Objects that were created using the Fortran interface will have a
* null "file" value and will have a routine name equal to the upper case
* Fortran routine that issued the pointer (e.g. "AST_CLONE", "AST_FRAME",
* etc).
c--
*/
/* Local Variables: */
AstKeyMap *result; /* Returned KeyMap */
astDECLARE_GLOBALS /* Thread-specific global data */
int i; /* Loop count */
int ihandle; /* Offset of Handle to be annulled */
AstObjectVtab *req_vtab; /* Vtab for requested class */
Handle *handle; /* Pointer to current Handle */
int generation_gap; /* Hereditary relationshp between two classes */
/* Check the global error status. */
if ( !astOK ) return NULL;
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(NULL);
/* Create an empty KeyMap to hold the results. */
result = astKeyMap( " ", status );
/* If we will need to check if each object is a subclass of a specified
class, we need a pointer to the VTAB descriibing the specified class. */
req_vtab = NULL;
if( class && subclass ) {
/* Loop round the vtab structures created by the currently executing thread. */
for( i = 0; i < nvtab; i++ ) {
/* If the current vtab is for a class that matches the requested class,
store a pointer to the vtab. */
if( !strcmp( class, known_vtabs[ i ]->class ) ) {
req_vtab = known_vtabs[ i ];
break;
}
}
}
/* Get exclusive access to the handles array. */
LOCK_MUTEX2;
/* Get the index of the first Handle to check. If we are checking only the
current context level, then get this index from the appropriate element
of the active_handles array. Otherwise, we check the whole of the
handles array, starting at element zero. */
if( current && active_handles ) {
ihandle = active_handles[ context_level ];
} else {
ihandle = 0;
}
/* Loop over the Handles array, starting from the above element. */
handle = handles + ihandle;
for( ; ihandle < nhandles; ihandle++,handle++ ) {
/* Skip Handles that have no associated object. */
if( !handle->ptr ) continue;
/* Skip handles that are in an unrequired context. */
if( current && handle->context != context_level ) continue;
#if defined(THREAD_SAFE)
/* Skip handles that are not locked for use by the current thread. */
if( handle->thread != AST__THREAD_ID ) continue;
#endif
/* If required, check that the current handle is for an object of the
specified class. */
if( class ) {
/* Skip the handle if no VTAB was found for the requested class. */
if( !req_vtab ) continue;
/* Get the generation gap between the current handle's object and the
specified class. */
generation_gap = astClassCompare( astVTAB( handle->ptr ), req_vtab );
/* Skip the handle if it is not for the specified class or a subclass. */
if( generation_gap < 0 || generation_gap == AST__COUSIN ||
( generation_gap > 0 && !subclass ) ) continue;
}
/* Get the integer identifier associated with the handle, convert it to a
pointer and append to the end of the KeyMap entry describing the handle's
class. */
astMapPutElemP( result, astGetClass( handle->ptr ), -1,
astI2P( handle->check ) );
}
/* Relinquish access to the handles array. */
UNLOCK_MUTEX2;
/* Return the KeyMap. */
return result;
}
MYSTATIC void AnnulHandle( int ihandle, int *status ) {
/*
* Name:
* AnnulHandle
* Purpose:
* Annul a Handle and its associated Object pointer.
* Type:
* Private function.
* Synopsis:
* #include "object.h"
* void AnnulHandle( int ihandle, int *status )
* Class Membership:
* Object member function.
* Description:
* This function annuls an active Handle by annulling the Object
* pointer it is associated with, deactivating the Handle and
* returning it to the free Handle list for re-use.
*
* The reference count for the associated Object is decremented by
* this function and the Object will be deleted if this reference
* count falls to zero.
* Parameters:
* ihandle
* Array offset that identifies the Handle to be annulled in the
* "handles" array. This is fully validated by this function.
* status
* Pointer to the inherited status variable.
* Notes:
* - The Handle supplied should be active, otherwise an error will
* result and the Handle will not be touched (but no error report
* will be made if the global error status has already been set).
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
AstObject *ptr; /* Object pointer */
int context; /* Context level where Handle was issued */
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(NULL);
/* Check that the handle offset supplied is valid and report an error
if it is not (but only if the global error status has not already
been set). */
if ( ( ihandle < 0 ) || ( ihandle >= nhandles ) ) {
if ( astOK ) {
astError( AST__INHAN, "astAnnulHandle: Invalid attempt to annul an "
"Object Handle (no. %u).", status, ihandle );
astError( AST__INHAN, "This Handle number is not valid (possible "
"internal programming error)." , status);
}
/* If OK, obtain the Handle's context level. */
} else {
context = handles[ ihandle ].context;
/* If this indicates that the Handle isn't active, then report an
error (but only if the global error status has not already been
set). We allow handles that are currently not owned by any thread to
be annulled. */
if ( context < 0 && context != UNOWNED_CONTEXT ) {
if ( astOK ) {
astError( AST__INHAN, "astAnnulHandle: Invalid attempt to annul "
"an Object Handle (no. %u).", status, ihandle );
astError( AST__INHAN, "This Handle is not active (possible "
"internal programming error)." , status);
}
/* If the Handle is active, annul its Object pointer. The astAnnul
function may call Delete functions supplied by any class, and these
Delete functions may involve annulling external Object IDs, which in
turn requires access to the handles array. For this reason, we release
the mutex that protects access to the handles arrays so that it can
potentially be re-aquired within astAnnul without causing deadlock. */
} else {
#ifdef MEM_DEBUG
astHandleUse( ihandle, "annulled using check value %d ",
handles[ ihandle ].check );
#endif
ptr = handles[ ihandle ].ptr;
UNLOCK_MUTEX2;
ptr = astAnnul( ptr );
LOCK_MUTEX2;
/* Remove the Handle from the active list for its context level. */
if( context == UNOWNED_CONTEXT ) {
#if defined(THREAD_SAFE)
RemoveHandle( ihandle, &unowned_handles, status );
#else
if( astOK ) astError( AST__INTER, "AnnulHandle: reference to "
"'unowned_handles' in a non-thread-safe context "
"(internal AST programming error).", status );
#endif
} else if( active_handles ) {
RemoveHandle( ihandle, &active_handles[ context ], status );
} else if( astOK ){
astError( AST__INTER, "AnnulHandle: active_handles array has "
"not been initialised (internal AST programming error).",
status );
}
/* Reset the Handle's "context" value (making it inactive) and its "check"
value (so it is no longer associated with an identifier value). */
handles[ ihandle ].ptr = NULL;
handles[ ihandle ].context = INVALID_CONTEXT;
handles[ ihandle ].check = 0;
#if defined(THREAD_SAFE)
handles[ ihandle ].thread = -1;
#endif
/* Place the modified Handle on the free Handles list ready for re-use. */
InsertHandle( ihandle, &free_handles, status );
}
}
}
AstObject *astAnnulId_( AstObject *this_id, int *status ) {
/*
*+
* Name:
* AnnulId
* Purpose:
* Annul an external Object identifier.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* AstObject *astAnnulId( AstObject *this )
* Class Membership:
* Object member function.
* Description:
* This function implements the external (public) interface to the
* astAnnul method. It accepts an active Object identifier, which
* it annuls, causing the associated Handle to be deactivated and
* returned to the free Handles list for re-use. It also causes the
* reference count of the associated Object to be decremented and
* the Object to be deleted if this reference count falls to zero.
* Parameters:
* this
* The Object identifier to be annulled.
* Returned Value:
* A NULL C pointer is always returned (this should be translated
* into an identifier value of zero for external use).
* Notes:
* - This function attempts to execute even if the global error
* status is set.
* - The identifier supplied should be associated with an active
* Object, otherwise an error will result (but no error report will
* be made if the global error status has already been set).
* - This function is invoked via the astAnnul macro for external use.
* For internal use (from protected code which needs to handle external
* IDs) it should be invoked via the astAnnulId macro (since astAnnul
* expects a true C pointer as its argument when used internally).
*-
*/
/* Obtain the Object pointer from the ID supplied and validate the
pointer to ensure it identifies a valid Object (this generates an
error if it doesn't). Note, we use "astMakePointer_NoLockCheck",
rather than the usual "astMakePointer" since a thread should be able
to renounce interest in an object without needing to own the object.
If we used "astMakePointer" then a thread could not annul a pointer
unless it owned the object. But having annulled the pointer it could
then not unlock the object for use by another thread (since the
pointer it would need to do this has just been annulled). */
if ( !astIsAObject( astMakePointer_NoLockCheck( this_id ) ) ) return NULL;
/* Obtain the Handle offset for this Object and annul the Handle and
its associated Object pointer. Report an error if the handle is
currently owned by a different thread. That is, the *Object* need
not be locked by the current thread (as indicated by the use of
astMakePointer above), but the *handle* must be owned by the current
thread. */
LOCK_MUTEX2;
AnnulHandle( CheckId( this_id, 1, status ), status );
UNLOCK_MUTEX2;
/* Always return a NULL pointer value. */
return NULL;
}
MYSTATIC AstObject *AssocId( int ihandle, int *status ) {
/*
* Name:
* AssocId
* Purpose:
* Associate an identifier value with a Handle.
* Type:
* Private function.
* Synopsis:
* #include "object.h"
* AstObject *AssocId( int ihandle )
* Class Membership:
* Object member function.
* Description:
* This function takes a zero-based offset into the "handles" array
* that identifies a Handle associated with an active Object. It
* encodes this into an identifier value to be issued to an
* external user to identify that Handle and its Object, and then
* associates this identifier value with the Handle.
* Parameters:
* ihandle
* Offset in the "handles" array that identifies the Handle,
* which should be active (i.e. associated with an active
* Object). This function will modify the "check" field in this
* Handle to associate it with the identifier value it returns.
* Returned Value:
* The resulting identifier value.
* Notes:
* - The returned identifier value is, in fact, an int. This
* allows the value to be passed easily to other languages
* (e.g. Fortran) and stored as an integer without loss of
* information.
* - The value is stored within C code as type AstObject*. This
* means that C code (e.g. function prototypes, etc.) need not be
* aware that an identifier (as opposed to an Object pointer) is
* being used, even though the actual bit patterns will be
* different. The same C code can then be used for both internal
* and external purposes so long as care is taken to convert
* between the two representations at appropriate points.
* - The reverse operation (conversion of an ID back to a handle
* offset) is performed by CheckId.
* - A zero identifier value will be returned if this function is
* invoked with the global status set or if it should fail for any
* reason.
*/
/* Local Variables: */
AstObject *result; /* Pointer value to return */
MixedInts test; /* Union for testing encoding */
MixedInts work; /* Union for encoding ID value */
/* Initialise. */
result = astI2P( 0 );
/* Check the global error status. */
if ( !astOK ) return result;
/* Copy the Handle offset and clear the lowest 8 bits by shifting
left. */
work.i = ihandle;
work.u = work.u << 8U;
/* Make a copy of the result shifted right again. Test if any bits
have been lost in this process. If so, there are too many Handles
in use at once to encode them into IDs, so report an error. */
test.u = work.u >> 8U;
if ( test.i != ihandle ) {
astError( AST__XSOBJ, "AssocId(%s): There are too many AST Objects in "
"use at once.", status, astGetClass( handles[ ihandle ].ptr ) );
/* If OK, scramble the value by exclusive-ORing with the bit pattern
in AST__FAC (a value unique to this library), also shifted left by
8 bits. This makes it even less likely that numbers from other
sources will be accepted in error as valid IDs. */
} else {
work.u ^= ( ( (unsigned) AST__FAC ) << 8U );
/* Fill the lowest 8 bits with a count of the total number of IDs
issued so far (which we increment here). This makes each ID unique,
so that an old one that identifies a Handle that has been annulled
and re-used (i.e. associated with a new ID) can be spotted. We
only use the lowest 8 bits of this count because this provides
adequate error detection to reveal programming errors and we do not
need higher security than this. We also prevent a count of zero
being used, as this could result in a zero identifier value (this
being reserved as the "null" value). */
if ( ++nids > 255U ) nids = 1U;
work.u |= nids;
/* Store the value as a check count in the Handle. This will be used
to validate the ID in future. */
handles[ ihandle ].check = work.i;
/* Pack the value into the pointer to be returned. */
result = astI2P( work.i );
}
/* Return the result. */
return result;
}
void astBegin_( void ) {
/*
*++
* Name:
c astBegin
f AST_BEGIN
* Purpose:
* Begin a new AST context.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c void astBegin
f CALL AST_BEGIN( STATUS )
* Class Membership:
* Object class function.
* Description:
c This macro invokes a function to begin a new AST context.
c Any Object pointers
f This routine begins a new AST context. Any Object pointers
* created within this context will be annulled when it is later
c ended using astEnd (just as if astAnnul had been invoked),
f ended using AST_END (just as if AST_ANNUL had been invoked),
c unless they have first been exported using astExport or rendered
c exempt using astExempt. If
f unless they have first been exported using AST_EXPORT or rendered
f exempt using AST_EXEMPT. If
* annulling a pointer causes an Object's RefCount attribute to
* fall to zero (which happens when the last pointer to it is
* annulled), then the Object will be deleted.
f Parameters:
f STATUS = INTEGER (Given and Returned)
f The global status.
* Applicability:
* Object
c This macro applies to all Objects.
f This routine applies to all Objects.
* Notes:
c - astBegin attempts to execute even if the AST error status
c is set on entry.
f - This routine attempts to execute even if STATUS is set to an
f error value.
c - Contexts delimited by astBegin and astEnd may be nested to any
c depth.
f - Contexts delimited by AST_BEGIN and AST_END may be nested to any
f depth.
*--
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
int stat; /* Copy of global status */
int *status; /* Pointer to inherited status value */
/* Get a pointer to thread-specific global data. */
astGET_GLOBALS(NULL);
/* Now that the thread-specific data has been initialised, we can get a
pointer to the threads inherited status value. */
status = astGetStatusPtr;
/* Save and clear the global status so that memory allocation can be
performed within this function even under error conditions. */
stat = astStatus;
astClearStatus;
/* Ensure that the active_handles array has been initialised. */
if ( !active_handles ) InitContext( status );
/* Extend the "active handles" array to accommodate a new context
level. This array contains integer offsets into the "handles" array
to identify the handle which is at the head of the list of active
handles for each context level. */
astBeginPM;
active_handles = astGrow( active_handles, context_level + 2,
sizeof( int ) );
astEndPM;
/* Initialise the array element for the new context level to indicate
an empty Handle list. */
if ( astOK ) active_handles[ ++context_level ] = -1;
/* Restore the original global status value. */
astSetStatus( stat );
}
MYSTATIC int CheckId( AstObject *this_id, int lock_check, int *status ) {
/*
* Name:
* CheckId
* Purpose:
* Check an identifier value and convert it into a Handle offset.
* Type:
* Private function.
* Synopsis:
* #include "object.h"
* int CheckId( AstObject *this, int lock_check, int *status )
* Class Membership:
* Object member function.
* Description:
* This function takes an identifier value encoded by AssocId and
* validates it to ensure it is associated with an active
* Handle. If valid, it converts it back into a zero-based offset
* which may be used to access the Handle in the "handles"
* array. Otherwise, an error is reported.
* Parameters:
* this
* The identifier value to be decoded.
* lock_check
* Should an error be reported if the handle is in an Object
* context for a different thread?
* status
* Pointer to the inherited status variable.
* Returned Value:
* The resulting Handle offset, or -1 if the identifier is not
* valid or any other error occurs.
* Notes:
* - This function attempts to execute even if the global error
* status is set, but no further error report will be made if it
* fails under these circumstances.
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
MixedInts work; /* Union for decoding ID value */
int id; /* ID value as an int */
int ihandle; /* Result to return */
#ifdef MEM_DEBUG
int oldok = astOK;
#endif
/* Initialise. */
ihandle = -1;
/* Get a pointer to thread-specific global data. */
astGET_GLOBALS(NULL);
/* Retrieve the integer Object identifier value from the pointer
supplied. */
id = astP2I( this_id );
/* Check if a value of zero has been supplied and report an error if
it has. */
if ( !id ) {
if ( astOK ) {
astError( AST__OBJIN, "Invalid Object pointer given (value is "
"zero)." , status);
}
/* If OK, reverse the encoding process performed by AssocId to
retrieve the Handle offset. */
} else {
work.i = id;
work.u = ( work.u ^ ( ( (unsigned) AST__FAC ) << 8U ) ) >> 8U;
/* Check that the offset obtained doesn't extend beyond the limits of
the "handles" array. Report an error if it does. */
if ( ( work.i < 0 ) || ( work.i >= nhandles ) ) {
if ( astOK ) {
astError( AST__OBJIN, "Invalid Object pointer given (value is "
"%d).", status, id );
}
/* See if the "check" field matches the ID value and the Handle is
valid (i.e. is associated with an active Object). If not, the
Handle has been annulled and possibly re-used, so report an
error. */
} else if ( ( handles[ work.i ].check != id ) ||
( handles[ work.i ].context == INVALID_CONTEXT ) ) {
if ( astOK ) {
astError( AST__OBJIN, "Invalid Object pointer given (value is "
"%d).", status, id );
astError( AST__OBJIN, "This pointer has been annulled, or the "
"associated Object deleted." , status);
}
#if defined(THREAD_SAFE)
} else if( lock_check && handles[ work.i ].context != UNOWNED_CONTEXT &&
handles[ work.i ].thread != AST__THREAD_ID ) {
if ( astOK ) {
astError( AST__OBJIN, "Invalid Object pointer given (value is "
"%d).", status, id );
astError( AST__OBJIN, "This pointer is currently owned by "
"another thread (possible programming error)." , status);
}
#endif
/* If OK, set the Handle offset to be returned. */
} else {
ihandle = work.i;
}
#ifdef MEM_DEBUG
if ( oldok && !astOK && ( work.i >= 0 ) && ( work.i < nhandles ) ) {
char buf[200];
astError( astStatus, "Handle properties: %s ", status,
HandleString( work.i, buf ) );
}
#endif
}
/* Return the result. */
return ihandle;
}
void astCreatedAtId_( AstObject *this_id, const char **routine,
const char **file, int *line, int *status ){
/*
c++
* Name:
* astCreatedAt
* Purpose:
* Return the routine, file and line number at which an Object was
* created.
* Type:
* Public function.
* Synopsis:
* #include "object.h"
* void astCreatedAt( AstObject *this, const char **routine,
* const char **file, int *line )
* Class Membership:
* Object method.
* Description:
* This function returns pointers to two strings containing the
* name of the routine or function within which the object was created
* and the name of the source file containing that routine. It also
* returns the number of the line within the file at which the object
* was created. It is intended for use in debugging memory leaks etc.
*
* Precisely, the information returned identifies the point at which
* the Object's public identifier (i.e. the supplied pointer) was
* first issued. This may not correspond to the actual creation of the
* Object if the object is contained within some higher level Object.
* For instance, if the astGetFrame method is used to get a pointer to
* a Frame within a FrameSet, the information returned by this
* function if supplied with the Frame pointer would identify the call
* to astGetFrame, rather than the line at which the FrameSet created
* its internal copy of the Frame. Likewise, if this function is used
* to get information from an Object pointer returned by astClone, the
* information will describe the call to astClone, not the call that
* created the original Object.
* Parameters:
* this
* Pointer to the Object.
* routine
* Address of a pointer to a null terminated C string in which to
* return the routine name (the string will reside in static memory).
* The pointer will be set to NULL on exit if no routine name is
* available.
* file
* Address of a pointer to a null terminated C string in which to
* return the file name (the string will reside in static memory).
* The pointer will be set to NULL on exit if no file name is
* available.
* line
* Address of an int in which to store the line number in the file.
* A line number of zero is returned if no line number is available.
* Notes:
* - NULL pointers and a line number of zero are returned if an error
* has already occurred prior to calling this function.
c--
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
int ihandle; /* Result to return */
/* Initialise */
*routine = NULL;
*file = NULL;
*line = 0;
/* Check the global error status. */
if ( !astOK ) return;
/* Gain exclusive access to the handles array. */
LOCK_MUTEX2;
/* Obtain the Handle offset for this Object. */
ihandle = CheckId( this_id, 1, status );
if ( ihandle != -1 ) {
/* Copy the required pointers etc to the supplied addresses. */
*routine = handles[ ihandle ].routine;
*file = handles[ ihandle ].file;
*line = handles[ ihandle ].line;
}
/* Unlock the mutex that guards access to the handles array */
UNLOCK_MUTEX2;
}
AstObject *astDeleteId_( AstObject *this_id, int *status ) {
/*
*+
* Name:
* astDeleteId
* Purpose:
* Delete an Object via an identifier.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* AstObject *astDeleteId_( AstObject *this )
* Class Membership:
* Object member function.
* Description:
* This function implements the external (public) interface to the
* astDelete method. It accepts an active Object identifier and
* deletes the associated Object. Before doing so, it annuls all
* active identifiers associated with the object, deactivates their
* Handles and returns these Handles to the free Handles list for
* re-use.
* Parameters:
* this
* An identifier for the Object to be deleted.
* Returned Value:
* A NULL C pointer is always returned (this should be translated
* into an identifier value of zero for external use).
* Notes:
* - This function attempts to execute even if the global error
* status is set.
* - The identifier supplied should be associated with an active
* Object, otherwise an error will result (but no error report will
* be made if the global error status has already been set).
* - Although this function is documented as "private" and should
* not be invoked directly from outside this class, it is not a
* static function and has a public prototype. This is because it
* must be invoked via the astDelete macro (defined in the
* "object.h" include file) as part of the public interface.
*-
*/
/* Local Variables: */
AstObject *this; /* Pointer to Object */
int i; /* Loop counter for Handles */
int ihandle; /* Object Handle offset */
/* Obtain the Object pointer from the ID supplied and validate the
pointer to ensure it identifies a valid Object (this generates an
error if it doesn't). */
if ( !astIsAObject( this = astMakePointer( this_id ) ) ) return NULL;
/* Gain exclusive access to the handles array. */
LOCK_MUTEX2;
/* Obtain the Handle offset for this Object. */
ihandle = CheckId( this_id, 1, status );
if ( ihandle != -1 ) {
/* Since the Object is to be deleted, we must annul all identifiers
that refer to it. Loop to inspect each currently allocated Handle
in the "handles" array. */
for ( i = 0; i < nhandles; i++ ) {
/* Select active handles and test if their Object pointer refers to
the Object to be deleted. */
if ( ( handles[ i ].context != INVALID_CONTEXT ) &&
( handles[ i ].ptr == this ) ) {
/* If so, explicitly set the reference count for the Object to 2 so
that it will not be deleted (yet) when we annul the pointer
associated with the Handle. */
handles[ i ].ptr->ref_count = 2;
/* Annul the Handle, which frees its resources, decrements the Object
reference count and makes any ID associated with the Handle become
invalid. */
AnnulHandle( i, status );
}
}
/* If required, tell the user that the handle's object has been deleted. */
#ifdef MEM_DEBUG
astHandleUse( ihandle, "object-deleted" );
#endif
}
UNLOCK_MUTEX2;
/* When all Handles associated with the Object have been annulled,
delete the object itself. This over-rides the reference count and
causes any remaining pointers to the Object (e.g. in internal code
or within other Objects) to become invalid. */
this = astDelete( this );
/* Always return a NULL pointer value. */
return NULL;
}
void astEnd_( int *status ) {
/*
*++
* Name:
c astEnd
f AST_END
* Purpose:
* End an AST context.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c void astEnd
f CALL AST_END( STATUS )
* Class Membership:
* Object class function.
* Description:
c This macro invokes a function to end an AST context which was
f This routine ends an AST context which was
c begun with a matching invocation of astBegin. Any Object
f begun with a matching invocation of AST_BEGIN. Any Object
* pointers created within this context will be annulled (just as
c if astAnnul had been invoked) and will cease to be valid
f if AST_ANNUL had been invoked) and will cease to be valid
* afterwards, unless they have previously been exported using
c astExport or rendered exempt using astExempt.
f AST_EXPORT or rendered exempt using AST_EXEMPT.
* If annulling a pointer causes an Object's RefCount attribute to
* fall to zero (which happens when the last pointer to it is
* annulled), then the Object will be deleted.
* Parameters:
f STATUS = INTEGER (Given and Returned)
f The global status.
* Applicability:
* Object
c This macro applies to all Objects.
f This routine applies to all Objects.
* Notes:
c - astEnd attempts to execute even if the AST error status is set.
f - This routine attempts to execute even if STATUS is set to an
f error value.
c - Contexts delimited by astBegin and astEnd may be nested to any
c depth.
f - Contexts delimited by AST_BEGIN and AST_END may be nested to any
f depth.
*--
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
int ihandle; /* Offset of Handle to be annulled */
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(NULL);
/* Check that the current context level is at least 1, otherwise there
has been no matching use of astBegin, so report an error (unless
the global status has already been set). */
if ( context_level < 1 ) {
if ( astOK ) {
astError( AST__ENDIN, "astEnd: Invalid use of astEnd without a "
"matching astBegin." , status);
}
/* If OK, loop while there are still active Handles associated with
the current context level. First gain exclusive access to the handles
array. */
} else if ( active_handles ) {
LOCK_MUTEX2;
while ( ( ihandle = active_handles[ context_level ] ) != -1 ) {
/* Annul the Handle at the head of the active Handles list. */
AnnulHandle( ihandle, status );
/* It is just posible that under error conditions inactive Handles
might get left in the active_handles list and AnnulHandle would
then fail. Since this would result in an infinite loop, we check to
see if the handle we have just annulled is still in the list. If
so, transfer it to the free Handles list for re-use. */
if ( ihandle == active_handles[ context_level ] ) {
RemoveHandle( ihandle, &active_handles[ context_level ], status );
InsertHandle( ihandle, &free_handles, status );
}
}
/* Ensure the context level is decremented unless it was zero to start
with. */
context_level--;
/* Relinquish access to the handles array. */
UNLOCK_MUTEX2;
}
}
void astExemptId_( AstObject *this_id, int *status ) {
/*
*++
* Name:
c astExempt
f AST_EXEMPT
* Purpose:
* Exempt an Object pointer from AST context handling.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c void astExempt( AstObject *this )
f CALL AST_EXEMPT( THIS, STATUS )
* Class Membership:
* Object method.
* Description:
c This function exempts an Object pointer from AST context
f This routine exempts an Object pointer from AST context
c handling, as implemented by astBegin and astEnd. This means that
f handling, as implemented by AST_BEGIN and AST_END. This means that
c the pointer will not be affected when astEnd is invoked and will
f the pointer will not be affected when AST_END is called and will
* remain active until the end of the program, or until explicitly
c annulled using astAnnul.
f annulled using AST_ANNUL.
*
c If possible, you should avoid using this function when writing
f If possible, you should avoid using this routine when writing
* applications. It is provided mainly for developers of other
* libraries, who may wish to retain references to AST Objects in
* internal data structures, and who therefore need to avoid the
c effects of astBegin and astEnd.
f effects of AST_BEGIN and AST_END.
* Parameters:
c this
f THIS = INTEGER (Given)
* Object pointer to be exempted from context handling.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Applicability:
* Object
c This function applies to all Objects.
f This routine applies to all Objects.
*--
* Implementation Notes:
* - This function does not exist in the "protected" interface to
* the Object class and is not available to other class
* implementations.
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
int context; /* Handle context level */
int ihandle; /* Offset of Handle in "handles" array */
/* Check the global error status. */
if ( !astOK ) return;
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(NULL);
/* Obtain the Object pointer from the ID supplied and validate the
pointer to ensure it identifies a valid Object. */
(void) astCheckObject( astMakePointer( this_id ) );
if ( astOK ) {
/* Gain exclusive access to the handles array. */
LOCK_MUTEX2;
/* Obtain the Handle offset for this Object. */
ihandle = CheckId( this_id, 1, status );
if ( ihandle != -1 ) {
/* Extract the context level at which the Object was created. */
context = handles[ ihandle ].context;
/* Set the new context level to zero, where it cannot be affected by
ending any context. */
handles[ ihandle ].context = 0;
/* Remove the object's Handle from its original active Handles list
and insert it into the list appropriate to its new context
level. */
#if defined(THREAD_SAFE)
if( context == UNOWNED_CONTEXT ) {
RemoveHandle( ihandle, &unowned_handles, status );
} else {
RemoveHandle( ihandle, &active_handles[ context ], status );
}
#else
RemoveHandle( ihandle, &active_handles[ context ], status );
#endif
InsertHandle( ihandle, &active_handles[ 0 ], status );
/* If required, tell the user that the handle has been exempted. */
#ifdef MEM_DEBUG
astHandleUse( ihandle, "exempted" );
#endif
}
UNLOCK_MUTEX2;
}
}
void astExportId_( AstObject *this_id, int *status ) {
/*
*++
* Name:
c astExport
f AST_EXPORT
* Purpose:
* Export an Object pointer to an outer context.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c void astExport( AstObject *this )
f CALL AST_EXPORT( THIS, STATUS )
* Class Membership:
* Object method.
* Description:
c This function exports an Object pointer from the current AST context
f This routine exports an Object pointer from the current AST context
* into the context that encloses the current one. This means that
* the pointer will no longer be annulled when the current context
c is ended (with astEnd), but only when the next outer context (if
f is ended (with AST_END), but only when the next outer context (if
* any) ends.
* Parameters:
c this
f THIS = INTEGER (Given)
* Object pointer to be exported.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Applicability:
* Object
c This function applies to all Objects.
f This routine applies to all Objects.
* Notes:
c - It is only sensible to apply this function to pointers that
f - It is only sensible to apply this routine to pointers that
* have been created within (or exported to) the current context
c and have not been rendered exempt using astExempt.
f and have not been rendered exempt using AST_EXEMPT.
* Applying it to an unsuitable Object pointer has no effect.
*--
* Implementation Notes:
* - This function does not exist in the "protected" interface to
* the Object class and is not available to other class
* implementations.
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
int context; /* Handle context level */
int ihandle; /* Offset of Handle in "handles" array */
/* Check the global error status. */
if ( !astOK ) return;
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(NULL);
/* Obtain the Object pointer from the ID supplied and validate the
pointer to ensure it identifies a valid Object. */
(void) astCheckObject( astMakePointer( this_id ) );
if ( astOK ) {
/* Gain exclusive access to the handles array. */
LOCK_MUTEX2;
/* Obtain the Handle offset for this Object. */
ihandle = CheckId( this_id, 1, status );
if ( ihandle != -1 ) {
/* Check that the current context level is at least 1 and report an
error if it is not. */
if ( context_level < 1 ) {
if( astOK ) astError( AST__EXPIN, "astExport(%s): Attempt to export an Object "
"from context level zero.", status,
astGetClass( handles[ ihandle ].ptr ) );
/* Extract the context level at which the Object was created. */
} else {
context = handles[ ihandle ].context;
/* Check that the Object's existing context level is high enough to be
affected by being exported to the next outer context level. If not,
do nothing. */
if ( context > ( context_level - 1 ) ) {
/* Set the new context level. */
handles[ ihandle ].context = context_level - 1;
/* Remove the object's Handle from its original active Handles list
and insert it into the list appropriate to its new context
level. */
RemoveHandle( ihandle, &active_handles[ context ], status );
InsertHandle( ihandle, &active_handles[ context_level - 1 ],
status );
/* If required, tell the user that the handle has been exempted. */
#ifdef MEM_DEBUG
astHandleUse( ihandle, "exported from context level %d",
context_level );
#endif
}
}
}
UNLOCK_MUTEX2;
}
}
void astImportId_( AstObject *this_id, int *status ) {
/*
*++
* Name:
c astImport
f AST_IMPORT
* Purpose:
* Import an Object pointer to the current context.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c void astImport( AstObject *this )
f CALL AST_IMPORT( THIS, STATUS )
* Class Membership:
* Object method.
* Description:
c This function
f This routine
* imports an Object pointer that was created in a higher or lower
* level context, into the current AST context.
* This means that the pointer will be annulled when the current context
c is ended (with astEnd).
f is ended (with AST_END).
* Parameters:
c this
f THIS = INTEGER (Given)
* Object pointer to be imported.
f STATUS = INTEGER (Given and Returned)
f The global status.
* Applicability:
* Object
c This function applies to all Objects.
f This routine applies to all Objects.
*--
* Implementation Notes:
* - This function does not exist in the "protected" interface to
* the Object class and is not available to other class
* implementations.
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
int context; /* Handle context level */
int ihandle; /* Offset of Handle in "handles" array */
/* Check the global error status. */
if ( !astOK ) return;
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(NULL);
/* Obtain the Object pointer from the ID supplied and validate the
pointer to ensure it identifies a valid Object. */
(void) astCheckObject( astMakePointer( this_id ) );
if ( astOK ) {
/* Gain exclusive access to the handles array. */
LOCK_MUTEX2;
/* Obtain the Handle offset for this Object. */
ihandle = CheckId( this_id, 1, status );
if ( ihandle != -1 ) {
/* Extract the context level at which the Object was created. */
context = handles[ ihandle ].context;
/* Do nothing if the Identifier already belongs to the current context. */
if( context != context_level ) {
/* Set the new context level. */
handles[ ihandle ].context = context_level;
/* Remove the object's Handle from its original active Handles list
and insert it into the list appropriate to its new context
level. */
RemoveHandle( ihandle, &active_handles[ context ], status );
InsertHandle( ihandle, &active_handles[ context_level ], status );
/* If required, tell the user that the handle has been imported. */
#ifdef MEM_DEBUG
astHandleUse( ihandle, "imported into context level %d",
context_level );
#endif
}
}
UNLOCK_MUTEX2;
}
}
void astLockId_( AstObject *this_id, int wait, int *status ) {
/*
c++
* Name:
* astLock
* Purpose:
* Lock an Object for exclusive use by the calling thread.
* Type:
* Public function.
* Synopsis:
* #include "object.h"
* void astLock( AstObject *this, int wait )
* Class Membership:
* Object method.
* Description:
* The thread-safe public interface to AST is designed so that an
* error is reported if any thread attempts to use an Object that it
* has not previously locked for its own exclusive use using this
* function. When an Object is created, it is initially locked by the
* thread that creates it, so newly created objects do not need to be
* explicitly locked. However, if an Object pointer is passed to
* another thread, the original thread must first unlock it (using
* astUnlock) and the new thread must then lock it (using astLock)
* before the new thread can use the Object.
*
* The "wait" parameter determines what happens if the supplied Object
* is curently locked by another thread when this function is invoked.
* Parameters:
* this
* Pointer to the Object to be locked.
* wait
* If the Object is curently locked by another thread then this
* function will either report an error or block. If a non-zero value
* is supplied for "wait", the calling thread waits until the object
* is available for it to use. Otherwise, an error is reported and
* the function returns immediately without locking the Object.
* Applicability:
* Object
* This function applies to all Objects.
* Notes:
* - The astAnnul function is exceptional in that it can be used on
* pointers for Objects that are not currently locked by the calling
* thread. All other AST functions will report an error.
* - The Locked object will belong to the current AST context.
* - This function returns without action if the Object is already
* locked by the calling thread.
* - If simultaneous use of the same object is required by two or more
* threads, astCopy should be used to to produce a deep copy of
* the Object for each thread. Each copy should then be unlocked by
* the parent thread (i.e. the thread that created the copy), and then
* locked by the child thread (i.e. the thread that wants to use the
* copy).
* - This function is only available in the C interface.
* - This function returns without action if the AST library has
* been built without POSIX thread support (i.e. the "-with-pthreads"
* option was not specified when running the "configure" script).
c--
*/
/* This function odes nothing if thread support is not enabvled. */
#if defined(THREAD_SAFE)
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
AstObject *fail; /* Pointer to Object that failed to lock */
AstObject *this; /* Pointer to Object */
int ihandle; /* Index of supplied objetc handle */
int lstat; /* Local status value */
/* Obtain the Object pointer from the ID supplied and validate the
pointer to ensure it identifies a valid Object (this generates an
error if it doesn't). Note, we use the "astMakePointer_NoLockCheck",
since the usual "astMakePointer" macro invokes astCheckLock to report
an error if the Object is not currently locked by the calling thread. */
if ( !astIsAObject( this = astMakePointer_NoLockCheck( this_id ) ) ) return;
/* Ensure the global data for this class is accessable. Do not use the
globals pointer stored in "*this" because "*this" may be locked by
another thread and so we would pick up the wrong globals. */
astGET_GLOBALS(NULL);
/* Ensure the running thread has sole access to the static handles arrays. */
LOCK_MUTEX2;
/* Ensure the Handles arrays have been initialised. */
if ( !active_handles ) InitContext( status );
/* Get the Handle index for the supplied object identifier. No error is
reported if the handle is not curently associated with a thread.
However, an error is reported if the Handle is associated with any
thread other than the running thread. */
ihandle = CheckId( this_id, 1, status );
/* We've finished with the handles arrays, for the moment. */
UNLOCK_MUTEX2;
/* Check the object pointer was valid. */
if( ihandle != -1 ){
/* The protected astManageLock function implements the public functions,
astLock and astUnlock. */
lstat = astManageLock( this, AST__LOCK, wait, &fail );
if( astOK ) {
if( lstat == 1 ) {
if( fail == this ) {
astError( AST__LCKERR, "astLock(%s): Failed to lock the %s because"
" it is already locked by another thread (programming "
"error).", status, astGetClass( this ),
astGetClass( this ) );
} else {
astError( AST__LCKERR, "astLock(%s): Failed to lock the %s because"
" a %s contained within it is already locked by another "
"thread (programming error).", status,
astGetClass( this ), astGetClass( this ),
astGetClass( fail ) );
}
} else if( lstat == 2 ) {
astError( AST__LCKERR, "astLock(%s): Failed to lock a POSIX mutex.", status,
astGetClass( this ) );
/* If the Object is now locked for the running thread... */
} else {
/* We need access to the handles arrays again. */
LOCK_MUTEX2;
/* If the supplied handle is not currently assigned to any thread, assign
it to the running thread. */
if( handles[ ihandle ].context == UNOWNED_CONTEXT ) {
RemoveHandle( ihandle, &unowned_handles, status );
#if defined(MEM_DEBUG)
astHandleUse( ihandle, "locked by thread %d at context level %d",
handles[ ihandle ].thread, context_level );
#endif
handles[ ihandle ].thread = AST__THREAD_ID;
handles[ ihandle ].context = context_level;
InsertHandle( ihandle, &active_handles[ context_level ],
status );
}
/* Finished with the handles arrays again. */
UNLOCK_MUTEX2;
}
}
}
#endif
}
void astUnlockId_( AstObject *this_id, int report, int *status ) {
/*
c++
* Name:
* astUnlock
* Purpose:
* Unlock an Object for use by other threads.
* Type:
* Public function.
* Synopsis:
* #include "object.h"
* void astUnlock( AstObject *this, int report )
* Class Membership:
* Object method.
* Description:
* Unlocks an Object previously locked using astLock, so that other
* threads can use the Object. See astLock for further details.
* Parameters:
* this
* Pointer to the Object to be unlocked.
* report
* If non-zero, an error will be reported if the supplied Object,
* or any Object contained within the supplied Object, is not
* currently locked by the running thread. If zero, such Objects
* will be left unchanged, and no error will be reported.
* Applicability:
* Object
* This function applies to all Objects.
* Notes:
* - This function attempts to execute even if the global error
* status is set, but no further error report will be made if it
* subsequently fails under these circumstances.
* - All unlocked Objects are excluded from AST context handling until
* they are re-locked using astLock.
* - This function is only available in the C interface.
* - This function returns without action if the Object is not currently
* locked by any thread. If it is locked by the running thread, it is
* unlocked. If it is locked by another thread, an error will be reported
* if "error" is non-zero.
* - This function returns without action if the AST library has
* been built without POSIX thread support (i.e. the "-with-pthreads"
* option was not specified when running the "configure" script).
c--
*/
/* This function odes nothing if thread support is not enabvled. */
#if defined(THREAD_SAFE)
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
AstErrorContext error_context;/* Info about the current error context */
AstObject *fail; /* Pointer to Object that failed */
AstObject *this; /* Pointer to Object */
int ihandle; /* Index of supplied objetc handle */
int lstat; /* Local status value */
/* Obtain the Object pointer from the ID supplied and validate the
pointer to ensure it identifies a valid Object (this generates an
error if it doesn't). Note, we use the "astMakePointer_NoLockCheck",
since the usual "astMakePointer" macro invokes astCheckLock to report
an error if the Object is not currently locked by the calling thread. */
if ( !astIsAObject( this = astMakePointer_NoLockCheck( this_id ) ) ) return;
/* Ensure the global data for this class is accessable. */
astGET_GLOBALS(this);
/* Start a new error reporting context. This saves any existing error status
and then clear the status value. It also defer further error reporting. */
astErrorBegin( &error_context );
/* Ensure the running thread has sole access to the static handles arrays. */
LOCK_MUTEX2;
/* Ensure the Handles arrays have been initialised. */
if ( !active_handles ) InitContext( status );
/* Get the Handle index for the supplied object identifier. Report an
error if the handle is associated with a different thread (no error
if the handle has no associated thread or is associated with the
current thread). */
ihandle = CheckId( this_id, 1, status );
/* Break the associated of the handle with the current thread so that the
handle is not assigned to any thread. We do this before unlocking the
Object structure (using astManageLock) since as soon as astManageLock
returns, another thread that is waiting for the object to be unlocked
may start up and modify the handle properties. */
if( ihandle >= 0 && handles[ ihandle ].context >= 0 ) {
RemoveHandle( ihandle, &active_handles[ handles[ ihandle ].context ],
status );
#if defined(MEM_DEBUG)
astHandleUse( ihandle, "unlocked from thread %d at context "
"level %d", handles[ ihandle ].thread,
handles[ ihandle ].context );
#endif
handles[ ihandle ].thread = -1;
handles[ ihandle ].context = UNOWNED_CONTEXT;
InsertHandle( ihandle, &unowned_handles, status );
}
/* We've finished with the handles arrays, for the moment. */
UNLOCK_MUTEX2;
/* Check the supplied object pointer was valid. */
if( ihandle != -1 ){
/* The protected astManageLock function implements the public functions,
astLock and astUnlock. */
lstat = astManageLock( this, AST__UNLOCK, 0, &fail );
if( astOK ) {
if( lstat == 1 ) {
if( report ) {
if( fail == this ) {
astError( AST__LCKERR, "astUnlock(%s): Failed to unlock the %s "
"because it is locked by another thread (programming "
"error).", status, astGetClass( this ),
astGetClass( this ) );
} else {
astError( AST__LCKERR, "astUnlock(%s): Failed to unlock the %s "
"because a %s contained within it is locked by another "
"thread (programming error).", status,
astGetClass( this ), astGetClass( this ),
astGetClass( fail ) );
}
}
} else if( lstat == 3 ) {
astError( AST__LCKERR, "astUnlock(%s): Failed to unlock a POSIX mutex.", status,
astGetClass( this ) );
}
}
}
/* End the error reporting context. If an error has occurred within this
function, then this will display the deferred error messages so long
as there was no error condition on entry to this function. If there
was an error condition on entry, then the original status value will be
re-instated. */
astErrorEnd( &error_context );
#endif
}
AstObject *astI2P_( int integer, int *status ) {
/*
*+
* Name:
* astI2P
* Purpose:
* Pack an integer Object ID into a pointer.
* Type:
* Public function.
* Synopsis:
* #include "object.h"
* AstObject *astI2P( int integer )
* Class Membership:
* Object class function.
* Description:
* This function accepts an integer (int) value representing an
* Object identifier and packs its bit pattern into a pointer value
* (from which it may subsequently be retrieved using astP2I).
*
* These functions should be used to avoid any dependency on
* integer-to-pointer conversions (given that the values are not
* true pointers) which might affect the exchange of Object
* identifier values with other languages, such as Fortran 77,
* where they are stored as integers.
* Parameters:
* integer
* The integer value to be stored.
* Returned Value:
* The resulting pointer value (which is not usually a valid C pointer).
* Notes:
* - This function does not perform error checking and does not
* generate errors.
* - This is a protected function in the sense that it is not
* intended that external users should invoke it directly. It is
* accessible from external code, however, in order that public
* macros invoked from that code can make use of it.
*-
*/
/* Local Variables: */
IdUnion temp; /* Overlapping int and pointer */
/* Clear the pointer value in the "temp" IdUnion and then set the
integer part of it to the value to be stored. */
temp.pointer = zero_ptr;
temp.integer = integer;
/* Return the resulting pointer value. */
return temp.pointer;
}
MYSTATIC void InitContext( int *status ) {
/*
* Name:
* InitContext
* Purpose:
* Initialise the first AST context level.
* Type:
* Private function.
* Synopsis:
* #include "object.h"
* void InitContext( int *status )
* Class Membership:
* Object member function.
* Description:
* This function initialises the first AST context level (the level
* before any call to astBegin has been made). It should be invoked
* once, before any use is made of context levels.
* Parameters:
* status
* Pointer to the inherited status variable.
* Parameters:
* None.
* Notes:
* - This function does nothing after the first successful invocation.
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
/* Check the global error status. */
if ( !astOK ) return;
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(NULL);
/* Check that the active_handles array hasn't already been allocated. */
if ( !active_handles ) {
/* Allocate and initialise the "active_handles" array. */
astBeginPM;
active_handles = astMalloc( sizeof( int ) );
astEndPM;
if ( astOK ) active_handles[ 0 ] = -1;
}
}
MYSTATIC void InsertHandle( int ihandle, int *head, int *status ) {
/*
* Name:
* InsertHandle
* Purpose:
* Insert a Handle into a list.
* Type:
* Private function.
* Synopsis:
* #include "object.h"
* void InsertHandle( int ihandle, int *head, int *status )
* Class Membership:
* Object member function.
* Description:
* This function inserts a Handle structure into a doubly linked
* list of such structures composed of elements drawn from the
* "handles" array. The new list member is inserted in front of the
* member previously occupying the "head of list" position.
* Parameters:
* ihandle
* Offset in the "handles" array that identifies the handle to
* be added to the list.
* head
* Address of an int which holds the offset in the "handles"
* array of the element at the head of the list (or -1 if the
* list is empty). This value will be updated to identify the
* new list member.
* status
* Pointer to the inherited status variable.
* Notes:
* - This function does not perform error chacking and does not
* generate errors.
* - The lists generated by this function use integer offsets into
* the "handles" array for their links, rather than pointers. This
* is because the array may be re-located in memory when it needs
* to be extended, so pointers to its element would not remain
* valid.
* - The list elements are drawn from the "handles" array in the
* first place so that they can be addressed by small integers (the
* offset in the array). This allows references to Handles to be
* encoded along with security information into an integer that is
* sufficiently short to be exported to other languages
* (e.g. Fortran) which might not be able to accommodate
* full-length C pointers.
*/
#if defined(MEM_DEBUG)
char buf[80];
astHandleUse( ihandle, "about to be inserted into %s (%d)",
HeadString( head, buf ), head );
CheckList( head, status );
CheckInList( ihandle, head, 0, status );
#endif
/* Check a head pointer was supplied (may not be if an error has
occurred). */
if( ! head ) return;
/* If the list is empty, the sole new element points at itself. */
if ( *head == -1 ) {
handles[ ihandle ].flink = ihandle;
handles[ ihandle ].blink = ihandle;
/* Otherwise, insert the new element in front of the element at the
head of the list. */
} else {
handles[ ihandle ].flink = *head;
handles[ ihandle ].blink = handles[ *head ].blink;
handles[ ( handles[ *head ].blink) ].flink = ihandle;
handles[ *head ].blink = ihandle;
}
/* Update the list head to identify the new element. */
*head = ihandle;
#if defined(MEM_DEBUG)
CheckList( head, status );
astHandleUse( ihandle, "has been inserted into %s", buf );
#endif
}
AstObject *astMakeId_( AstObject *this, int *status ) {
/*
*+
* Name:
* astMakeId
* Purpose:
* Issue an identifier for an Object.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* AstObject *astMakeId( AstObject *this )
* Class Membership:
* Object member function.
* Description:
* This function takes a normal C pointer to an Object and
* associates an identifier with it.
* Parameters:
* this
* Pointer to an Object. Note that this function copies this
* value (it is not cloned), so the caller should not annul the
* pointer afterwards.
* Returned Value:
* The resulting identifier value.
* Notes:
* - An identifier value of zero will be returned and the supplied
* Object pointer will be annulled if this function is invoked with
* the global error status set or if it should fail for any reason.
* - A zero identifier value will also be returned if a NULL object
* pointer is supplied, but this will not provoke an error.
* - This is a protected function in the sense that it is not
* intended that external users should invoke it directly. It is
* accessible from external code, however, in order that public
* macros invoked from that code can make use of it.
*-
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
AstObject *id; /* ID value to return */
int ihandle; /* Handle offset */
/* Initialise. */
id = astI2P( 0 );
ihandle = 0;
/* Check the global error status. */
if ( astOK ) {
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(this);
/* Gain exclusive access to the handles array. */
LOCK_MUTEX2;
/* If a non-NULL Object pointer was given, we must obtain a Handle
structure to associate with it (otherwise a zero identifier value
is returned without error). */
if ( this ) {
/* If the free Handles list is not empty, obtain a Handle from it. */
if ( free_handles != -1 ) {
ihandle = free_handles;
RemoveHandle( ihandle, &free_handles, status );
/* Otherwise, allocate a new Handle by extending the "handles" array
and using the offset of the new element. */
} else {
astBeginPM;
handles = astGrow( handles, nhandles + 1, sizeof( Handle ) );
astEndPM;
if ( astOK ) {
ihandle = nhandles++;
handles[ ihandle ].ptr = NULL;
handles[ ihandle ].context = INVALID_CONTEXT;
handles[ ihandle ].check = 0;
handles[ ihandle ].flink = -1;
handles[ ihandle ].blink = -1;
handles[ ihandle ].line = 0;
handles[ ihandle ].file = NULL;
handles[ ihandle ].routine = NULL;
#if defined(THREAD_SAFE)
handles[ ihandle ].thread = 0;
#endif
#if defined(MEM_DEBUG)
handles[ ihandle ].id = 0;
handles[ ihandle ].vtab = NULL;
#endif
}
}
/* If the first AST context level has not yet been initialised, invoke
InitContext to initialise it and allocate memory for the
"active_handles" array which stores context information. */
if ( astOK ) {
if ( !active_handles ) InitContext( status );
/* Store the Object pointer and current context level in the Handle. */
if ( astOK ) {
handles[ ihandle ].ptr = this;
handles[ ihandle ].context = context_level;
#if defined(THREAD_SAFE)
handles[ ihandle ].thread = AST__THREAD_ID;
#endif
/* Store information that records where the Handle is created - routine
name, file name and line number. */
astGetAt( &handles[ ihandle ].routine,
&handles[ ihandle ].file,
&handles[ ihandle ].line );
/* Store extra debugging information in the handle if enabled */
#if defined(MEM_DEBUG)
handles[ ihandle ].id = astMemoryId( this );
handles[ ihandle ].vtab = this->vtab;
if( Watched_Pointer == -1 ) {
astHandleUse( ihandle, "associated with a %s (id %d)",
astGetClass( this ), astMemoryId( this ));
}
#endif
/* Insert the Handle into the active Handles list for the current
context level. */
InsertHandle( ihandle, &active_handles[ context_level ], status );
/* Associate an identifier value with the Handle. */
id = AssocId( ihandle, status );
#if defined(MEM_DEBUG)
int iid = astP2I( id );
if( iid == Watched_Pointer ) {
Watched_Handle = ihandle;
printf( "astHandleAlarm: Watched AST pointer (value %d) has been "
"issued for Object handle index %d\n", iid, ihandle );
astHandleUse( ihandle, "associated with a %s (id %d)",
astGetClass( this ), astMemoryId( this ));
}
#endif
/* If an error occurred, clean up by annulling the Handle. This
ensures that the Object pointer is annulled and returns the unused
Handle to the Free Handle list. */
if ( !astOK ) {
AnnulHandle( ihandle, status );
this = NULL;
}
/* If the Handle wasn't used (because of an earlier error), return it
to the free Handles list. */
} else {
InsertHandle( ihandle, &free_handles, status );
}
}
}
UNLOCK_MUTEX2;
}
/* If a bad status value was either supplied or generated within this
function, then annul the supplied pointer (unless it is NULL). */
if ( !astOK && this ) (void) astAnnul( this );
/* Return the identifier value. */
return id;
}
AstObject *astMakePointer_( AstObject *this_id, int *status ) {
/*
*+
* Name:
* astMakePointer
* Purpose:
* Obtain a true C pointer from an Object identifier.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* AstObject *astMakePointer( AstObject *this )
* Class Membership:
* Object class function.
* Description:
* This function accepts an external Object identifier and returns
* a true C Object pointer that may be de-referenced to access the
* Object's data and methods.
* Parameters:
* this
* The identifier value.
* Returned Value:
* The true C pointer value.
* Notes:
* - In a thread-safe context, an error is reported if the supplied
* Object has not been locked by the calling thread (using astLock)
* prior to invoking this function.
* - The object reference count is not modified by this function,
* so the returned pointer should not be annulled by the caller.
* - Typically, this function should be used whenever a public
* function must accept identifier values directly (rather than
* having them translated automatically into pointers during
* argument validation by the astCheck<Class> range of functions).
* - Note that this function will NOT accept a true C pointer as an
* argument, even when invoked from internal code (with astCLASS
* defined). An identifier value MUST be supplied, and an error
* will result if it is not associated with an active Object.
* - This function attempts to execute even if the global error
* status is set, but no further error report will be made if it
* subsequently fails under these circumstances.
* - This is a protected function in the sense that it is not
* intended that external users should invoke it directly. It is
* accessible from external code, however, in order that public
* macros invoked from that code can make use of it.
*-
*/
/* Local Variables: */
AstObject *ptr; /* Pointer value to return */
int ihandle; /* Handle offset in "handles" array */
/* Initialise. */
ptr = NULL;
/* Gain exclusive access to the handles array. */
LOCK_MUTEX2;
/* Validate the identifier supplied and derive the Handle offset. */
ihandle = CheckId( this_id, 1, status );
/* If the identifier was valid, extract the Object pointer from the
Handle. */
if ( ihandle != -1 ) ptr = handles[ ihandle ].ptr;
UNLOCK_MUTEX2;
/* Return the result. */
return ptr;
}
AstObject *astMakePointer_NoLockCheck_( AstObject *this_id, int *status ) {
/*
*+
* Name:
* astMakePointer_NoLockCheck
* Purpose:
* Obtain a true C pointer from an Object identifier.
* Type:
* Protected function.
* Synopsis:
* #include "object.h"
* AstObject *astMakePointer_NoLockCheck( AstObject *this )
* Class Membership:
* Object class function.
* Description:
* This function accepts an external Object identifier and returns
* a true C Object pointer that may be de-referenced to access the
* Object's data and methods.
*
* It is identicial to astMakePointer except that it does not check
* that the supplied Object is locked by the running thread.
* Parameters:
* this
* The identifier value.
* Returned Value:
* The true C pointer value.
* Notes:
* - The object reference count is not modified by this function,
* so the returned pointer should not be annulled by the caller.
* - Typically, this function should be used whenever a public
* function must accept identifier values directly (rather than
* having them translated automatically into pointers during
* argument validation by the astCheck<Class> range of functions).
* - Note that this function will NOT accept a true C pointer as an
* argument, even when invoked from internal code (with astCLASS
* defined). An identifier value MUST be supplied, and an error
* will result if it is not associated with an active Object.
* - This function attempts to execute even if the global error
* status is set, but no further error report will be made if it
* subsequently fails under these circumstances.
* - This is a protected function in the sense that it is not
* intended that external users should invoke it directly. It is
* accessible from external code, however, in order that public
* macros invoked from that code can make use of it.
*-
*/
/* Local Variables: */
AstObject *ptr; /* Pointer value to return */
int ihandle; /* Handle offset in "handles" array */
/* Initialise. */
ptr = NULL;
/* Gain exclusive access to the handles array. */
LOCK_MUTEX2;
/* Validate the identifier supplied and derive the Handle offset. */
ihandle = CheckId( this_id, 0, status );
/* If the identifier was valid, extract the Object pointer from the
Handle. */
if ( ihandle != -1 ) ptr = handles[ ihandle ].ptr;
UNLOCK_MUTEX2;
/* Return the result. */
return ptr;
}
int astP2I_( AstObject *pointer, int *status ) {
/*
*+
* Name:
* astP2I
* Purpose:
* Extract an integer Object ID from a pointer.
* Type:
* Public function.
* Synopsis:
* #include "object.h"
* int astP2I( AstObject *pointer )
* Class Membership:
* Object class function.
* Description:
* This function retrieves an integer (int) value representing an
* Object identifier from a pointer value (into which it has
* previously been packed using astI2P).
*
* These functions should be used to avoid any dependency on
* integer-to-pointer conversions (given that the values are not
* true pointers) which might affect the exchange of Object
* identifier values with other languages, such as Fortran 77,
* where they are stored as integers.
* Parameters:
* pointer
* The pointer value into which the ID has previously been packed.
* Returned Value:
* The extracted Object identifier value as an integer (int).
* Notes:
* - This function does not perform error checking and does not
* generate errors.
* - This is a protected function in the sense that it is not
* intended that external users should invoke it directly. It is
* accessible from external code, however, in order that public
* macros invoked from that code can make use of it.
*-
*/
/* Local Variables: */
IdUnion temp; /* Overlapping int and pointer */
/* Store the pointer value supplied. */
temp.pointer = pointer;
/* Return the integer part of it. */
return temp.integer;
}
MYSTATIC void RemoveHandle( int ihandle, int *head, int *status ) {
/*
* Name:
* RemoveHandle
* Purpose:
* Remove a Handle from a list.
* Type:
* Private function.
* Synopsis:
* #include "object.h"
* void RemoveHandle( int ihandle, int *head )
* Class Membership:
* Object member function.
* Description:
* This function removes a Handle structure from a doubly linked
* list of such structures composed of elements drawn from the
* "handles" array. The "head of list" position is updated if
* necessary.
* Parameters:
* ihandle
* Offset in the "handles" array that identifies the Handle to
* be removed from the list (note that the Handle must actually
* be in the list, although this function does not check this).
* head
* Address of an int which holds the offset in the "handles"
* array of the element at the head of the list. This value will
* be updated if necessary to identify the new list head (or set
* to -1 if removing the Handle causes the list to become
* empty).
* Notes:
* - This function does not perform error chacking and does not
* generate errors.
* - See the InsertHandle function for details of how and why lists
* of Handles are constructed.
*/
#if defined(MEM_DEBUG)
char buf[80];
astHandleUse( ihandle, "about to be removed from %s", HeadString( head, buf ) );
CheckList( head, status );
CheckInList( ihandle, head, 1, status );
#endif
/* Check a head pointer was supplied (may not be if an error has
occurred). */
if( ! head ) return;
/* Remove the Handle from the list by re-establishing links between
the elements on either side of it. */
handles[ ( handles[ ihandle ].blink ) ].flink = handles[ ihandle ].flink;
handles[ ( handles[ ihandle ].flink ) ].blink = handles[ ihandle ].blink;
/* If the element removed was at the head of the list, update the head
of list offset to identify the following element. */
if ( ihandle == *head ) {
*head = handles[ ihandle ].flink;
/* If the head of list still identifies the removed element, then note
that the list is now empty. */
if ( *head == ihandle ) *head = -1;
}
/* Make the removed element point at itself. */
handles[ ihandle ].flink = ihandle;
handles[ ihandle ].blink = ihandle;
#if defined(MEM_DEBUG)
astHandleUse( ihandle, "has been removed from %s", buf );
CheckList( head, status );
#endif
}
void astSetId_( void *this_id_void, const char *settings, ... ) {
/*
* Name:
* astSetId_
* Purpose:
* Set values for an Object's attributes via an identifier.
* Type:
* Private function.
* Synopsis:
* #include "object.h"
* void astSetId_( AstObject *this, const char *settings, ... )
* Class Membership:
* Object member function.
* Description:
* This function implements the axternal interface to the astSet
* method (q.v.). It accepts an Object identifier, but otherwise
* behaves identically to astSet.
* Parameters:
* this
* Object identifier.
* settings
* Pointer to a null-terminated string containing a
* comma-separated list of attribute settings.
* ...
* Optional arguments which supply values to be substituted for
* any "printf"-style format specifiers that appear in the
* "settings" string.
* Notes:
* - Because this function has a variable argument list, it is
* invoked by a macro that evaluates to a function pointer (not a
* function invocation) and no checking or casting of arguments is
* performed before the function is invoked. Because of this, the
* Object identifier is of type (void *) and is converted and
* validated within the function itself.
*/
/* Local Variables: */
AstObject *this; /* Pointer to the Object structure */
int *status; /* Pointer to inherited status variable */
va_list args; /* Variable argument list */
/* Get a pointer to the integer holding the inherited status value. */
status = astGetStatusPtr;
/* Check the global error status. */
if ( !astOK ) return;
/* Obtain the Object pointer from the ID supplied and validate the
pointer to ensure it identifies a valid Object. */
this = astCheckObject( astMakePointer( this_id_void ) );
if ( astOK ) {
/* Obtain the variable argument list and pass all arguments to the
astVSet method for interpretation. */
va_start( args, settings );
astVSet( this, settings, NULL, args );
va_end( args );
}
}
int astThreadId_( AstObject *this_id, int ptr, int *status ) {
/*
c++
* Name:
* astThread
* Purpose:
* Determine the thread that owns an Object.
* Type:
* Public function.
* Synopsis:
* #include "object.h"
* int astThread( AstObject *this, int ptr )
* Class Membership:
* Object method.
* Description:
* Returns an integer that indicates whether the supplied Object (or
* Object pointer) is currently unlocked, or is currently locked by
* the running thread, or another thread.
* Parameters:
* this
* Pointer to the Object to be checked.
* ptr
* If non-zero, returns information about the supplied Object
* pointer, rather than the Object structure itself. See "Object
* Pointers and Structures" below.
* Returned Value:
* astThread()
* A value of AST__UNLOCKED is returned if the Object (or pointer)
* is currently unlocked (i.e. has been unlocked using astUnlock
* but has not yet been locked using astLock). A value of
* AST__RUNNING is returned if the Object (or pointer) is currently
* locked by the running thread. A value of AST__OTHER is returned
* if the Object (or pointer) is currently locked by the another
* thread.
* Object Pointers and Structures:
* At any one time, an AST Object can have several distinct pointers,
* any one of which can be used to access the Object structure. For
* instance, the astClone function will produce a new distinct pointer
* for a given Object. In fact, an AST "pointer" is not a real pointer
* at all - it is an identifier for a "handle" structure, encoded to
* make it look like a pointer. Each handle contains (amongst othere
* things) a "real" pointer to the Object structure. This allows more
* than one handle to refer to the same Object structure. So when you
* call astClone (for instance) you get back an identifier for a new
* handle that refers to the same Object as the supplied handle.
*
* In order to use an Object for anything useful, it must be locked
* for use by the running thread (either implicitly at creation or
* explicitly using astLock). The identity of the thread is stored in
* both the Object structure, and in the handle that was passed to
* astLock (or returned by the constructor function). Thus it is
* possible for a thread to have active pointers for Objects that are
* currently locked by another thread. In general, if such a pointer is
* passed to an AST function an error will be reported indicating that
* the Object is currently locked by another thread. The two exceptions
* to this is that astAnnul can be used to annull such a pointer, and
* this function can be used to return information about the pointer.
*
* The other practical consequence of this is that when astEnd is
* called, all active pointers currently owned by the running thread
* (at the current context level) are annulled. This includes pointers
* for Objects that are currently locked by other threads.
*
* If the "ptr" parameter is zero, then the returned value describes
* the Object structure itself. If "ptr" is non-zero, then the returned
* value describes the supplied Object pointer (i.e. handle), rather
* than the Object structure.
* Notes:
* - This function attempts to execute even if the global error
* status is set, but no further error report will be made if it
* subsequently fails under these circumstances.
* - This function is only available in the C interface.
* - This function always returns AST__RUNNING if the AST library has
* been built without POSIX thread support (i.e. the "-with-pthreads"
* option was not specified when running the "configure" script).
c--
*/
/* Local Variables: */
astDECLARE_GLOBALS /* Thread-specific global data */
int result; /* The returned value */
#if defined(THREAD_SAFE)
/* More local Variables: */
AstObject *this;
int ihandle;
int check;
/* Ensure global variables are accessable. */
astGET_GLOBALS(NULL);
#endif
/* Initialise the returned value */
result = AST__RUNNING;
/* Nothing more to do if AST was not build with thread support. */
#if defined(THREAD_SAFE)
/* If the ownership of the handle is being queried... */
if( ptr ) {
/* Lock the mutex that guards access to the handles array */
LOCK_MUTEX2;
/* Check the supplied object identifier is valid and get the
corresponding index into the handles array. */
ihandle = CheckId( this_id, 1, status );
if( ihandle != -1 ) {
/* Set the returned value on the basis of the threa didentifier stored in
the handle structure. */
if( handles[ ihandle ].thread == -1 ) {
result = AST__UNLOCKED;
} else if( handles[ ihandle ].thread != AST__THREAD_ID ) {
result = AST__OTHER;
}
}
/* Unlock the mutex that guards access to the handles array */
UNLOCK_MUTEX2;
/* Otherwise, the ownership of the Object is being queried. Obtain the
Object pointer from the ID supplied and validate the pointer to ensure
it identifies a valid Object (this generates an error if it doesn't).
Note, we use the "astMakePointer_NoLockCheck", since the usual
"astMakePointer" macro invokes astCheckLock to report an error if the
Object is not currently locked by the calling thread. */
} else if( astIsAObject( this = astMakePointer_NoLockCheck( this_id ) ) ) {
/* Determine which thread (if any) has the object locked, and set an
appropriate return value. */
check = astManageLock( this, AST__CHECKLOCK, 0, NULL );
if( check == 5 ) {
result = AST__OTHER;
} else if( check == 6 ) {
result = AST__UNLOCKED;
}
}
#endif
/* Return the result. */
return result;
}
int astVersion_( int *status ) {
/*
*++
* Name:
c astVersion
f AST_VERSION
* Purpose:
* Return the version of the AST library being used.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c int astVersion
f RESULT = AST_VERSION()
* Class Membership:
* Object class function.
* Description:
c This macro invokes a function which
f This function
* returns an integer representing the version of the AST library
* being used. The library version is formatted as a string such as
* "2.0-7" which contains integers representing the "major version" (2),
* the "minor version" (0) and the "release" (7). The integer returned
* by this function combines all three integers together into a single
* integer using the expresion:
*
* (major version)*1E6 + (minor version)*1E3 + (release)
* Returned Value:
c astVersion
f AST_VERSION = INTEGER
* The major version, minor version and release numbers for the AST
* library, encoded as a single integer.
* Applicability:
* Object
c This macro applies to all Objects.
f This routine applies to all Objects.
*--
*/
return (int) AST__VMAJOR*1E6 + AST__VMINOR*1E3 + AST__RELEASE;
}
int astEscapes_( int new_value, int *status ) {
/*
*++
* Name:
c astEscapes
f AST_ESCAPES
* Purpose:
* Control whether graphical escape sequences are included in strings.
* Type:
* Public function.
* Synopsis:
c #include "object.h"
c int astEscapes( int new_value )
f RESULT = AST_ESCAPES( NEWVAL, STATUS )
* Class Membership:
* Object class function.
* Description:
* The Plot class defines a set of escape sequences which can be
* included within a text string in order to control the appearance of
* sub-strings within the text. See the Escape attribute for a
* description of these escape sequences. It is usually inappropriate
* for AST to return strings containing such escape sequences when
* called by application code. For instance, an application which
* displays the value of the Title attribute of a Frame usually does
* not want the displayed string to include potentially long escape
* sequences which a human read would have difficuly interpreting.
* Therefore the default behaviour is for AST to strip out such escape
* sequences when called by application code. This default behaviour
* can be changed using this function.
* Parameters:
c new_value
f NEWVAL = INTEGER (Given)
* A flag which indicates if escapes sequences should be included
* in returned strings. If zero is supplied, escape sequences will
* be stripped out of all strings returned by any AST function. If
* a positive value is supplied, then any escape sequences will be
* retained in the value returned to the caller. If a negative
* value is supplied, the current value of the flag will be left
* unchanged.
* Returned Value:
c astEscapes
f AST_ESCAPES = INTEGER
* The value of the flag on entry to this function.
* Applicability:
* Object
c This macro applies to all Objects.
f This routine applies to all Objects.
* Notes:
* - This function also controls whether the
c astStripEscapes
f AST_STRIPESCAPES
* function removes escape sequences from the supplied string, or
* returns the supplied string without change.
* - This function attempts to execute even if an error has already
* occurred.
*--
*/
/* Local Variables: */
astDECLARE_GLOBALS
int old_val;
/* Get a pointer to Thread-specific global data. */
astGET_GLOBALS(NULL);
/* Save the old value. */
old_val = retain_esc;
/* Set the new value. */
if( new_value > 0 ) {
retain_esc = 1;
} else if( new_value == 0 ) {
retain_esc = 0;
}
/* Return the old value. */
return old_val;
}
#if defined(MEM_DEBUG)
/* Check each handle in a list is uniquely connected to one other handle
in both the forward and backward directions. */
void CheckList( int *head, int *status ) {
int ok;
int ihandle;
char buf[200];
astDECLARE_GLOBALS
if( !astOK ) return;
astGET_GLOBALS(NULL);
ok = 1;
if ( *head != -1 ) {
ihandle = *head;
if( handles[ handles[ ihandle ].blink ].flink != ihandle ||
handles[ handles[ ihandle ].flink ].blink != ihandle ) {
ok = 0;
} else {
if( CheckThread( ihandle, head, status ) ) {
ihandle= handles[ *head ].blink;
while( ihandle != *head ) {
if( handles[ handles[ ihandle ].blink ].flink != ihandle ||
handles[ handles[ ihandle ].flink ].blink != ihandle ||
CheckThread( ihandle, head, status ) == 0 ) {
ok = 0;
break;
}
ihandle= handles[ ihandle ].blink;
}
}
}
}
if( !ok ) {
printf("CheckList error in %s\n", HeadString( head, buf ) );
printf(" Central handle: %s\n", HandleString( ihandle, buf ) );
if( handles[ handles[ ihandle ].blink ].flink != ihandle ) {
printf(" Central handle->blink: %s\n",
HandleString( handles[ ihandle ].blink, buf ) );
printf(" Central handle->blink->flink: %s\n",
HandleString( handles[ handles[ ihandle ].blink ].flink, buf ) );
}
if( handles[ handles[ ihandle ].flink ].blink != ihandle ) {
printf(" Central handle->flink: %s\n",
HandleString( handles[ ihandle ].flink, buf ) );
printf(" Central handle->flink->blink: %s\n",
HandleString( handles[ handles[ ihandle ].flink ].blink, buf ) );
}
}
}
/* Check if a specified handle is, or is not, in a given list of handles. */
void CheckInList( int ihandle, int *head, int in, int *status ){
char list[80], buf[200];
int found = 0;
if( !astOK ) return;
if ( *head != -1 ) {
if( ihandle == *head ) {
found = 1;
} else {
if( CheckThread( ihandle, head, status ) ) {
int jhandle= handles[ *head ].blink;
while( jhandle != *head ) {
if( ihandle == jhandle ) {
found = 1;
break;
}
jhandle= handles[ jhandle ].blink;
}
}
}
}
if( in && !found ) {
printf("Error: Handle %s not in %s\n", HandleString( ihandle, buf ),
HeadString( head, list ) );
} else if( !in && found ) {
printf("Error: Handle %s is in %s\n", HandleString( ihandle, buf ),
HeadString( head, list ) );
}
}
/* Check that a handle is owned by the currently executing thread. */
int CheckThread( int ihandle, int *head, int *status ) {
int result = 1;
#if defined(THREAD_SAFE)
char buf[200];
astDECLARE_GLOBALS
if( !astOK ) return result;
astGET_GLOBALS(NULL);
if( *head == unowned_handles ) {
if( handles[ ihandle ].thread != -1 ) {
printf("Handle %s has wrong thread: is %d, should "
"be -1 (i.e. unowned)\n", HandleString( ihandle, buf ),
handles[ ihandle ].thread );
result = 0;
}
} else if( *head == free_handles ) {
if( handles[ ihandle ].thread != -1 ) {
printf("Handle %s has wrong thread: is %d, should "
"be -1 (i.e. free)\n", HandleString( ihandle, buf ),
handles[ ihandle ].thread );
result = 0;
}
} else if( handles[ ihandle ].thread != AST__THREAD_ID ) {
printf("Handle %s has wrong thread: is %d, should "
"be %d\n", HandleString( ihandle, buf ),
handles[ ihandle ].thread, AST__THREAD_ID );
result = 0;
}
#endif
return result;
}
void astWatchPointer_( int ptr_id ){
Watched_Pointer = ptr_id;
Watched_Handle = -1;
}
void astWatchHandle_( int handle ){
Watched_Handle = handle;
Watched_Pointer = -1;
}
void astHandleUse_( int handle, const char *verb, ... ){
const char *routine;
const char *file;
int line;
va_list args;
if( handle == Watched_Handle ) {
astGetAt( &routine, &file, &line );
if( routine && file ) {
printf( "astHandleAlarm: In function %s (%s line %d)\n",
routine, file, line );
}
va_start( args, verb );
astHandleAlarm( verb, args );
va_end( args );
}
}
void astHandleAlarm_( const char *verb, va_list args ){
char buff[200], hbuf[200];
astDECLARE_GLOBALS
astGET_GLOBALS(NULL);
vsprintf( buff, verb, args );
#if defined(THREAD_SAFE)
printf( "astHandleAlarm: Handle %s %s (current thread is %d).\n\n",
HandleString( Watched_Handle, hbuf ), buff, AST__THREAD_ID );
#else
printf( "astHandleAlarm: Handle %s %s.\n\n",
HandleString( Watched_Handle, hbuf ), buff );
#endif
}
MYSTATIC const char *HandleString( int ihandle, char *buf ){
#if defined(THREAD_SAFE)
astDECLARE_GLOBALS
astGET_GLOBALS(NULL);
if( ihandle >= 0 ) {
sprintf( buf, "(index:%d v:%d c:%d t:%d i:%d cl:%s) [cur. thread: %d]",
ihandle,
handles[ ihandle ].check,
handles[ ihandle ].context, handles[ ihandle ].thread,
handles[ ihandle ].id,
handles[ ihandle ].vtab ? handles[ ihandle ].vtab->class : "<none>",
AST__THREAD_ID );
} else {
sprintf( buf, "(index:%d <invalid>) [cur. thread: %d]", ihandle,
AST__THREAD_ID );
}
#else
if( ihandle >= 0 ) {
sprintf( buf, "(index:%d v:%d c:%d i:%d cl:%s)", ihandle,
handles[ ihandle ].check,
handles[ ihandle ].context, handles[ ihandle ].id,
handles[ ihandle ].vtab ? handles[ ihandle ].vtab->class : "<none>" );
} else {
sprintf( buf, "(index:%d <invalid>)", ihandle );
}
#endif
return buf;
}
MYSTATIC const char *HeadString( int *head, char *list ){
int i;
astDECLARE_GLOBALS
astGET_GLOBALS(NULL);
if( head == &free_handles ) {
strcpy( list, "free_handles" );
#if defined(THREAD_SAFE)
} else if( head == &unowned_handles ) {
strcpy( list, "unowned_handles" );
#endif
} else {
*list = 0;
for( i = 0; i <= context_level; i++ ) {
if( *head == active_handles[ i ] ) {
sprintf( list, "active_handles[%d]", i );
break;
}
}
if( *list == 0 ) sprintf( list, "unknown handles list with head %d",
*head );
}
return list;
}
#endif
|