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
|
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2007-2021 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING. If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////
#if defined (HAVE_CONFIG_H)
# include "config.h"
#endif
#include <istream>
#include <memory>
#include <ostream>
#include "Array-util.h"
#include "byte-swap.h"
#include "oct-locbuf.h"
#include "lo-mappers.h"
#include "Cell.h"
#include "defun.h"
#include "error.h"
#include "file-ops.h"
#include "errwarn.h"
#include "interpreter-private.h"
#include "interpreter.h"
#include "load-path.h"
#include "ls-hdf5.h"
#include "ls-oct-text.h"
#include "ls-oct-binary.h"
#include "ls-utils.h"
#include "mxarray.h"
#include "oct-lvalue.h"
#include "oct-hdf5.h"
#include "ov-class.h"
#include "ov-fcn.h"
#include "ov-typeinfo.h"
#include "ov-usr-fcn.h"
#include "pager.h"
#include "parse.h"
#include "pr-output.h"
#include "unwind-prot.h"
#include "variables.h"
int octave_class::t_id (-1);
const std::string octave_class::t_name ("class");
void
octave_class::register_type (octave::type_info& ti)
{
t_id = ti.register_type (octave_class::t_name, "<unknown>",
octave_value (new octave_class ()));
}
octave_class::octave_class (const octave_map& m, const std::string& id,
const octave_value_list& parents)
: octave_base_value (), map (m), c_name (id), obsolete_copies (0)
{
octave_idx_type n = parents.length ();
for (octave_idx_type idx = 0; idx < n; idx++)
{
octave_value parent = parents(idx);
if (! parent.isobject ())
error ("parents must be objects");
std::string pcnm = parent.class_name ();
if (find_parent_class (pcnm))
error ("duplicate class in parent tree");
parent_list.push_back (pcnm);
octave_idx_type nel = map.numel ();
octave_idx_type p_nel = parent.numel ();
if (nel == 0)
{
if (p_nel == 0)
{
// No elements in MAP or the parent class object,
// so just add the field name.
map.assign (pcnm, Cell (map.dims ()));
}
else if (p_nel == 1)
{
if (map.nfields () == 0)
{
// No elements or fields in MAP, but the
// parent is class object with one element.
// Resize to match size of parent class and
// make the parent a field in MAP.
map.resize (parent.dims ());
map.assign (pcnm, parent);
}
else
{
// No elements in MAP, but we have at least
// one field. So don't resize, just add the
// field name.
map.assign (pcnm, Cell (map.dims ()));
}
}
else if (map.nfields () == 0)
{
// No elements or fields in MAP and more than one
// element in the parent class object, so we can
// resize MAP to match parent dimsenions, then
// distribute the elements of the parent object to
// the elements of MAP.
dim_vector parent_dims = parent.dims ();
map.resize (parent_dims);
Cell c (parent_dims);
octave_map pmap = parent.map_value ();
std::list<std::string> plist
= parent.parent_class_name_list ();
for (octave_idx_type i = 0; i < p_nel; i++)
c(i) = octave_value (pmap.index (i), pcnm, plist);
map.assign (pcnm, c);
}
else
error ("class: parent class dimension mismatch");
}
else if (nel == 1 && p_nel == 1)
{
// Simple assignment.
map.assign (pcnm, parent);
}
else
{
if (p_nel == 1)
{
// Broadcast the scalar parent class object to
// each element of MAP.
Cell pcell (map.dims (), parent);
map.assign (pcnm, pcell);
}
else if (nel == p_nel)
{
// FIXME: is there a better way to do this?
// The parent class object has the same number of
// elements as the map we are using to create the
// new object, so distribute those elements to
// each element of the new object by first
// splitting the elements of the parent class
// object into a cell array with one element per
// cell. Then do the assignment all at once.
Cell c (parent.dims ());
octave_map pmap = parent.map_value ();
std::list<std::string> plist
= parent.parent_class_name_list ();
for (octave_idx_type i = 0; i < p_nel; i++)
c(i) = octave_value (pmap.index (i), pcnm, plist);
map.assign (pcnm, c);
}
else
error ("class: parent class dimension mismatch");
}
}
octave::symbol_table& symtab = octave::__get_symbol_table__ ("octave_class");
symtab.add_to_parent_map (id, parent_list);
}
octave_base_value *
octave_class::unique_clone (void)
{
if (count == obsolete_copies)
{
// All remaining copies are obsolete. We don't actually need to clone.
count++;
return this;
}
else
{
// In theory, this shouldn't be happening, but it's here just in case.
if (count < obsolete_copies)
obsolete_copies = 0;
return clone ();
}
}
std::string
octave_class::get_current_method_class (void)
{
std::string retval = class_name ();
if (nparents () > 0)
{
octave::tree_evaluator& tw
= octave::__get_evaluator__ ("octave_class::get_current_method_class");
octave_function *fcn = tw.current_function ();
// Here we are just looking to see if FCN is a method or constructor
// for any class, not specifically this one.
if (fcn && (fcn->is_class_method () || fcn->is_class_constructor ()))
retval = fcn->dispatch_class ();
}
return retval;
}
OCTAVE_NORETURN static
void
err_invalid_index1 (void)
{
error ("invalid index for class");
}
OCTAVE_NORETURN static
void
err_invalid_index_for_assignment (void)
{
error ("invalid index for class assignment");
}
OCTAVE_NORETURN static
void
err_invalid_index_type (const std::string& nm, char t)
{
error ("%s cannot be indexed with %c", nm.c_str (), t);
}
Cell
octave_class::dotref (const octave_value_list& idx)
{
assert (idx.length () == 1);
std::string method_class = get_current_method_class ();
// Find the class in which this method resides before attempting to access
// the requested field.
octave_base_value *obvp = find_parent_class (method_class);
if (obvp == nullptr)
error ("malformed class");
octave_map my_map = (obvp != this) ? obvp->map_value () : map;
std::string nm = idx(0).xstring_value ("invalid index for class");
octave_map::const_iterator p = my_map.seek (nm);
if (p == my_map.end ())
error ("class has no member '%s'", nm.c_str ());
return my_map.contents (p);
}
Matrix
octave_class::size (void)
{
if (in_class_method () || called_from_builtin ())
return octave_base_value::size ();
Matrix retval (1, 2, 1.0);
octave::symbol_table& symtab
= octave::__get_symbol_table__ ("octave_class::size");
octave_value meth = symtab.find_method ("size", class_name ());
if (meth.is_defined ())
{
count++;
octave_value_list args (1, octave_value (this));
octave_value_list lv = octave::feval (meth.function_value (), args, 1);
if (lv.length () <= 0
|| ! lv(0).is_matrix_type () || ! lv(0).dims ().isvector ())
error ("@%s/size: invalid return value", class_name ().c_str ());
retval = lv(0).matrix_value ();
}
else
{
dim_vector dv = dims ();
int nd = dv.ndims ();
retval.resize (1, nd);
for (int i = 0; i < nd; i++)
retval(i) = dv(i);
}
return retval;
}
octave_idx_type
octave_class::xnumel (const octave_value_list& idx)
{
if (in_class_method () || called_from_builtin ())
return octave_base_value::xnumel (idx);
octave_idx_type retval = -1;
const std::string cn = class_name ();
octave::symbol_table& symtab
= octave::__get_symbol_table__ ("octave_class::numel");
octave_value meth = symtab.find_method ("numel", cn);
if (meth.is_defined ())
{
octave_value_list args (idx.length () + 1, octave_value ());
count++;
args(0) = octave_value (this);
for (octave_idx_type i = 0; i < idx.length (); i++)
args(i+1) = idx(i);
octave_value_list lv = octave::feval (meth.function_value (), args, 1);
if (lv.length () != 1 || ! lv(0).is_scalar_type ())
error ("@%s/numel: invalid return value", cn.c_str ());
retval = lv(0).idx_type_value (true);
}
else
retval = octave_base_value::xnumel (idx);
return retval;
}
octave_value_list
octave_class::subsref (const std::string& type,
const std::list<octave_value_list>& idx,
int nargout)
{
octave_value_list retval;
if (in_class_method () || called_from_builtin ())
{
// FIXME: this block of code is the same as the body of
// octave_struct::subsref. Maybe it could be shared instead of
// duplicated.
int skip = 1;
switch (type[0])
{
case '(':
{
if (type.length () > 1 && type[1] == '.')
{
auto p = idx.begin ();
octave_value_list key_idx = *++p;
Cell tmp = dotref (key_idx);
Cell t = tmp.index (idx.front ());
retval(0) = (t.numel () == 1 ? t(0)
: octave_value (t, true));
// We handled two index elements, so tell
// next_subsref to skip both of them.
skip++;
}
else
retval(0) = octave_value (map.index (idx.front ()),
c_name, parent_list);
}
break;
case '.':
{
if (map.numel () > 0)
{
Cell t = dotref (idx.front ());
retval(0) = (t.numel () == 1) ? t(0) : octave_value (t, true);
}
}
break;
case '{':
err_invalid_index_type (type_name (), type[0]);
break;
default:
panic_impossible ();
}
// FIXME: perhaps there should be an
// octave_value_list::next_subsref member function? See also
// octave_user_function::subsref.
if (idx.size () > 1)
retval = retval(0).next_subsref (nargout, type, idx, skip);
}
else
{
octave::symbol_table& symtab
= octave::__get_symbol_table__ ("octave_class::subsref");
octave_value meth = symtab.find_method ("subsref", class_name ());
if (meth.is_defined ())
{
octave_value_list args;
args(1) = make_idx_args (type, idx, "subsref");
count++;
args(0) = octave_value (this);
// FIXME: for Matlab compatibility, let us attempt to set up a proper
// value for nargout at least in the simple case where the
// cs-list-type expression - i.e., {} or ().x, is the leading one.
// Note that Octave does not actually need this, since it will
// be able to properly react to varargout a posteriori.
bool maybe_cs_list_query = (type[0] == '.' || type[0] == '{'
|| (type.length () > 1 && type[0] == '('
&& type[1] == '.'));
int true_nargout = nargout;
if (maybe_cs_list_query)
{
// Set up a proper nargout for the subsref call by calling numel.
octave_value_list tmp;
if (type[0] != '.') tmp = idx.front ();
true_nargout = xnumel (tmp);
}
retval = octave::feval (meth.function_value (), args, true_nargout);
// Since we're handling subsref, if the list has more than one
// element, return it as a comma-separated list so that we can
// pass it to the evaluator
if (retval.length () > 1)
retval = octave_value (retval);
}
else
{
if (type.length () == 1 && type[0] == '(')
retval(0) = octave_value (map.index (idx.front ()), c_name,
parent_list);
else
err_invalid_index1 ();
}
}
return retval;
}
octave_value
octave_class::numeric_conv (const Cell& val, const std::string& type)
{
octave_value retval;
if (val.numel () != 1)
err_invalid_index_for_assignment ();
retval = val(0);
if (type.length () > 0 && type[0] == '.' && ! retval.isstruct ())
retval = octave_map ();
return retval;
}
octave_value
octave_class::subsasgn (const std::string& type,
const std::list<octave_value_list>& idx,
const octave_value& rhs)
{
count++;
return subsasgn_common (octave_value (this), type, idx, rhs);
}
octave_value
octave_class::undef_subsasgn (const std::string& type,
const std::list<octave_value_list>& idx,
const octave_value& rhs)
{
// For compatibility with Matlab, pass [] as the first argument to the
// the subsasgn function when the LHS of an indexed assignment is
// undefined.
return subsasgn_common (Matrix (), type, idx, rhs);
}
octave_value
octave_class::subsasgn_common (const octave_value& obj,
const std::string& type,
const std::list<octave_value_list>& idx,
const octave_value& rhs)
{
octave_value retval;
if (! (in_class_method () || called_from_builtin ()))
{
octave::symbol_table& symtab
= octave::__get_symbol_table__ ("octave_class::subsasgn_common");
octave_value meth = symtab.find_method ("subsasgn", class_name ());
if (meth.is_defined ())
{
octave_value_list args;
if (rhs.is_cs_list ())
{
octave_value_list lrhs = rhs.list_value ();
args.resize (2 + lrhs.length ());
for (octave_idx_type i = 0; i < lrhs.length (); i++)
args(2+i) = lrhs(i);
}
else
args(2) = rhs;
args(1) = make_idx_args (type, idx, "subsasgn");
args(0) = obj;
// Now comes the magic. Count copies with me:
// 1. myself (obsolete)
// 2. the copy inside args (obsolete)
// 3. the copy in method's symbol table (working)
// ... possibly more (not obsolete).
//
// So we mark 2 copies as obsolete and hold our fingers crossed.
// But prior to doing that, check whether the routine is amenable
// to the optimization.
// It is essential that the handling function doesn't store extra
// copies anywhere. If it does, things will not break but the
// optimization won't work.
octave_value_list tmp;
if (obsolete_copies == 0 && meth.is_user_function ()
&& meth.user_function_value ()->subsasgn_optimization_ok ())
{
octave::unwind_protect frame;
frame.protect_var (obsolete_copies);
obsolete_copies = 2;
tmp = octave::feval (meth.function_value (), args);
}
else
tmp = octave::feval (meth.function_value (), args);
// FIXME: Should the subsasgn method be able to return
// more than one value?
if (tmp.length () > 1)
error ("@%s/subsasgn returned more than one value",
class_name ().c_str ());
else
retval = tmp(0);
return retval;
}
}
// Find the class in which this method resides before
// attempting to do the indexed assignment.
std::string method_class = get_current_method_class ();
octave_base_value *obvp = unique_parent_class (method_class);
if (obvp != this)
{
if (! obvp)
error ("malformed class");
obvp->subsasgn (type, idx, rhs);
count++;
retval = octave_value (this);
return retval;
}
// FIXME: this block of code is the same as the body of
// octave_struct::subsasgn. Maybe it could be shared instead of
// duplicated.
int n = type.length ();
octave_value t_rhs = rhs;
if (n > 1 && ! (type.length () == 2 && type[0] == '(' && type[1] == '.'))
{
switch (type[0])
{
case '(':
{
if (type.length () > 1 && type[1] == '.')
{
auto p = idx.begin ();
octave_value_list t_idx = *p;
octave_value_list key_idx = *++p;
assert (key_idx.length () == 1);
std::string key = key_idx(0).xstring_value ("invalid index for class assignment");
octave_value u;
if (! map.contains (key))
u = octave_value::empty_conv (type.substr (2), rhs);
else
{
Cell map_val = map.contents (key);
Cell map_elt = map_val.index (idx.front (), true);
u = numeric_conv (map_elt, type.substr (2));
}
std::list<octave_value_list> next_idx (idx);
// We handled two index elements, so subsasgn to
// needs to skip both of them.
next_idx.erase (next_idx.begin ());
next_idx.erase (next_idx.begin ());
u.make_unique ();
t_rhs = u.subsasgn (type.substr (2), next_idx, rhs);
}
else
err_invalid_index_for_assignment ();
}
break;
case '.':
{
octave_value_list key_idx = idx.front ();
assert (key_idx.length () == 1);
std::string key = key_idx(0).string_value ();
std::list<octave_value_list> next_idx (idx);
next_idx.erase (next_idx.begin ());
std::string next_type = type.substr (1);
Cell tmpc (1, 1);
auto pkey = map.seek (key);
if (pkey != map.end ())
{
map.contents (pkey).make_unique ();
tmpc = map.contents (pkey);
}
// FIXME: better code reuse?
if (tmpc.numel () != 1)
err_indexed_cs_list ();
octave_value& tmp = tmpc(0);
if (! tmp.is_defined () || tmp.is_zero_by_zero ())
{
tmp = octave_value::empty_conv (next_type, rhs);
tmp.make_unique (); // probably a no-op.
}
else
// optimization: ignore copy still stored inside our map.
tmp.make_unique (1);
t_rhs = tmp.subsasgn (next_type, next_idx, rhs);
}
break;
case '{':
err_invalid_index_type (type_name (), type[0]);
break;
default:
panic_impossible ();
}
}
switch (type[0])
{
case '(':
{
if (n > 1 && type[1] == '.')
{
auto p = idx.begin ();
octave_value_list key_idx = *++p;
assert (key_idx.length () == 1);
std::string key = key_idx(0).xstring_value ("assignment to class element failed");
map.assign (idx.front (), key, t_rhs);
count++;
retval = octave_value (this);
}
else
{
if (t_rhs.isobject () || t_rhs.isstruct ())
{
octave_map rhs_map = t_rhs.xmap_value ("invalid class assignment");
map.assign (idx.front (), rhs_map);
count++;
retval = octave_value (this);
}
else
{
if (! t_rhs.isempty ())
error ("invalid class assignment");
map.delete_elements (idx.front ());
count++;
retval = octave_value (this);
}
}
}
break;
case '.':
{
octave_value_list key_idx = idx.front ();
assert (key_idx.length () == 1);
std::string key = key_idx(0).string_value ();
if (t_rhs.is_cs_list ())
{
Cell tmp_cell = Cell (t_rhs.list_value ());
// The shape of the RHS is irrelevant, we just want
// the number of elements to agree and to preserve the
// shape of the left hand side of the assignment.
if (numel () == tmp_cell.numel ())
tmp_cell = tmp_cell.reshape (dims ());
map.setfield (key, tmp_cell);
}
else
{
Cell tmp_cell(1, 1);
tmp_cell(0) = t_rhs.storable_value ();
map.setfield (key, tmp_cell);
}
count++;
retval = octave_value (this);
}
break;
case '{':
err_invalid_index_type (type_name (), type[0]);
break;
default:
panic_impossible ();
}
return retval;
}
idx_vector
octave_class::index_vector (bool require_integers) const
{
octave::symbol_table& symtab
= octave::__get_symbol_table__ ("octave_class::index_vector");
octave_value meth = symtab.find_method ("subsindex", class_name ());
if (! meth.is_defined ())
error ("no subsindex method defined for class %s",
class_name ().c_str ());
octave_value_list args;
args(0) = octave_value (new octave_class (map, c_name, parent_list));
octave_value_list tmp = octave::feval (meth.function_value (), args, 1);
if (tmp(0).isobject ())
error ("subsindex function must return a valid index vector");
// Index vector returned by subsindex is zero based
// (why this inconsistency Mathworks?), and so we must
// add one to the value returned as the index_vector method
// expects it to be one based.
return do_binary_op (octave_value::op_add, tmp (0),
octave_value (1.0)).index_vector (require_integers);
}
size_t
octave_class::byte_size (void) const
{
// Neglect the size of the fieldnames.
size_t retval = 0;
for (auto it = map.cbegin (); it != map.cend (); it++)
{
std::string key = map.key (it);
octave_value val = octave_value (map.contents (it));
retval += val.byte_size ();
}
return retval;
}
bool
octave_class::is_true (void) const
{
bool retval = false;
octave::symbol_table& symtab
= octave::__get_symbol_table__ ("octave_class::is_true");
octave_value meth = symtab.find_method ("logical", class_name ());
if (meth.is_defined ())
{
octave_value in = new octave_class (*this);
octave_value_list tmp = octave::feval (meth.function_value (), in, 1);
retval = tmp(0).is_true ();
}
return retval;
}
string_vector
octave_class::map_keys (void) const
{
err_wrong_type_arg ("octave_class::map_keys()", type_name ());
}
octave_base_value *
octave_class::find_parent_class (const std::string& parent_class_name)
{
octave_base_value *retval = nullptr;
if (parent_class_name == class_name ())
retval = this;
else
{
for (auto& par : parent_list)
{
octave_map::const_iterator smap = map.seek (par);
const Cell& tmp = map.contents (smap);
octave_value vtmp = tmp(0);
octave_base_value *obvp = vtmp.internal_rep ();
retval = obvp->find_parent_class (parent_class_name);
if (retval)
break;
}
}
return retval;
}
octave_base_value *
octave_class::unique_parent_class (const std::string& parent_class_name)
{
octave_base_value *retval = nullptr;
if (parent_class_name == class_name ())
retval = this;
else
{
for (auto& par : parent_list)
{
auto smap = map.seek (par);
Cell& tmp = map.contents (smap);
octave_value& vtmp = tmp(0);
octave_base_value *obvp = vtmp.internal_rep ();
// Use find_parent_class first to avoid uniquifying if not necessary.
retval = obvp->find_parent_class (parent_class_name);
if (retval)
{
vtmp.make_unique ();
obvp = vtmp.internal_rep ();
retval = obvp->unique_parent_class (parent_class_name);
break;
}
}
}
return retval;
}
bool
octave_class::is_instance_of (const std::string& cls_name) const
{
bool retval = false;
if (cls_name == class_name ())
retval = true;
else
{
for (auto& par : parent_list)
{
octave_map::const_iterator smap = map.seek (par);
const Cell& tmp = map.contents (smap);
const octave_value& vtmp = tmp(0);
retval = vtmp.is_instance_of (cls_name);
if (retval)
break;
}
}
return retval;
}
string_vector
octave_class::string_vector_value (bool pad) const
{
string_vector retval;
octave::symbol_table& symtab
= octave::__get_symbol_table__ ("octave_class::string_vector_value");
octave_value meth = symtab.find_method ("char", class_name ());
if (! meth.is_defined ())
error ("no char method defined for class %s", class_name ().c_str ());
octave_value_list args;
args(0) = octave_value (new octave_class (map, c_name, parent_list));
octave_value_list tmp = octave::feval (meth.function_value (), args, 1);
if (tmp.length () >= 1)
{
if (! tmp(0).is_string ())
error ("cname/char method did not return a string");
retval = tmp(0).string_vector_value (pad);
}
return retval;
}
void
octave_class::print (std::ostream& os, bool)
{
print_raw (os);
}
void
octave_class::print_raw (std::ostream& os, bool) const
{
octave::unwind_protect frame;
indent (os);
os << " <class " << class_name () << '>';
newline (os);
}
// Loading a class properly requires an exemplar map entry for success.
// If we don't have one, we attempt to create one by calling the constructor
// with no arguments.
bool
octave_class::reconstruct_exemplar (void)
{
bool retval = false;
octave_class::exemplar_const_iterator it
= octave_class::exemplar_map.find (c_name);
if (it != octave_class::exemplar_map.end ())
retval = true;
else
{
octave::interpreter& interp
= octave::__get_interpreter__ ("octave_class::reconstruct_exemplar");
octave::symbol_table& symtab = interp.get_symbol_table ();
octave_value ctor = symtab.find_method (c_name, c_name);
bool have_ctor = false;
if (ctor.is_defined () && ctor.is_function ())
{
octave_function *fcn = ctor.function_value ();
if (fcn && fcn->is_class_constructor (c_name))
have_ctor = true;
// Something has gone terribly wrong if
// symbol_table::find_method (c_name, c_name) does not return
// a class constructor for the class c_name...
assert (have_ctor);
}
if (have_ctor)
{
octave::unwind_protect frame;
// Simulate try/catch.
interpreter_try (frame);
bool execution_error = false;
octave_value_list result;
try
{
result = octave::feval (ctor, ovl (), 1);
}
catch (const octave::execution_exception&)
{
interp.recover_from_exception ();
execution_error = true;
}
if (! execution_error && result.length () == 1)
retval = true;
}
else
warning ("no constructor for class %s", c_name.c_str ());
}
return retval;
}
void
octave_class::clear_exemplar_map (void)
{
exemplar_map.clear ();
}
// Load/save does not provide enough information to reconstruct the
// class inheritance structure. reconstruct_parents () attempts to
// do so. If successful, a "true" value is returned.
//
// Note that we don't check the loaded object structure against the
// class structure here so the user's loadobj method has a chance
// to do its magic.
bool
octave_class::reconstruct_parents (void)
{
bool retval = true;
bool might_have_inheritance = false;
std::string dbgstr = "dork";
// First, check to see if there might be an issue with inheritance.
for (auto it = map.cbegin (); it != map.cend (); it++)
{
std::string key = map.key (it);
Cell val = map.contents (it);
if (val(0).isobject ())
{
dbgstr = "blork";
if (key == val(0).class_name ())
{
might_have_inheritance = true;
dbgstr = "cork";
break;
}
}
}
if (might_have_inheritance)
{
octave_class::exemplar_const_iterator it
= octave_class::exemplar_map.find (c_name);
if (it == octave_class::exemplar_map.end ())
retval = false;
else
{
octave_class::exemplar_info exmplr = it->second;
parent_list = exmplr.parents ();
for (auto& par : parent_list)
{
dbgstr = par;
bool dbgbool = map.contains (par);
if (! dbgbool)
{
retval = false;
break;
}
}
}
}
return retval;
}
bool
octave_class::save_ascii (std::ostream& os)
{
os << "# classname: " << class_name () << "\n";
octave_map m;
octave::load_path& lp = octave::__get_load_path__ ("octave_class::save_ascii");
if (lp.find_method (class_name (), "saveobj") != "")
{
octave_value in = new octave_class (*this);
octave_value_list tmp = octave::feval ("saveobj", in, 1);
m = tmp(0).map_value ();
}
else
m = map_value ();
os << "# length: " << m.nfields () << "\n";
auto i = m.begin ();
while (i != m.end ())
{
octave_value val = map.contents (i);
bool b = save_text_data (os, val, m.key (i), false, 0);
if (! b)
return ! os.fail ();
i++;
}
return true;
}
bool
octave_class::load_ascii (std::istream& is)
{
octave_idx_type len = 0;
std::string classname;
if (! extract_keyword (is, "classname", classname) || classname.empty ())
error ("load: failed to extract name of class");
if (! extract_keyword (is, "length", len) || len < 0)
error ("load: failed to extract number of elements in class");
if (len > 0)
{
octave_map m (map);
for (octave_idx_type j = 0; j < len; j++)
{
octave_value t2;
bool dummy;
// recurse to read cell elements
std::string nm
= read_text_data (is, "", dummy, t2, j);
if (! is)
break;
Cell tcell = (t2.iscell () ? t2.xcell_value ("load: internal error loading class elements") : Cell (t2));
m.assign (nm, tcell);
}
if (! is)
error ("load: failed to load class");
c_name = classname;
reconstruct_exemplar ();
map = m;
if (! reconstruct_parents ())
warning ("load: unable to reconstruct object inheritance");
octave::load_path& lp = octave::__get_load_path__ ("octave_class::load_ascii");
if (lp.find_method (classname, "loadobj") != "")
{
octave_value in = new octave_class (*this);
octave_value_list tmp = octave::feval ("loadobj", in, 1);
map = tmp(0).map_value ();
}
}
else if (len == 0)
{
map = octave_map (dim_vector (1, 1));
c_name = classname;
}
else
panic_impossible ();
return true;
}
bool
octave_class::save_binary (std::ostream& os, bool save_as_floats)
{
int32_t classname_len = class_name ().length ();
os.write (reinterpret_cast<char *> (&classname_len), 4);
os << class_name ();
octave_map m;
octave::load_path& lp = octave::__get_load_path__ ("octave_class::save_binary");
if (lp.find_method (class_name (), "saveobj") != "")
{
octave_value in = new octave_class (*this);
octave_value_list tmp = octave::feval ("saveobj", in, 1);
m = tmp(0).map_value ();
}
else
m = map_value ();
int32_t len = m.nfields ();
os.write (reinterpret_cast<char *> (&len), 4);
auto i = m.begin ();
while (i != m.end ())
{
octave_value val = map.contents (i);
bool b = save_binary_data (os, val, m.key (i), "", 0, save_as_floats);
if (! b)
return ! os.fail ();
i++;
}
return true;
}
bool
octave_class::load_binary (std::istream& is, bool swap,
octave::mach_info::float_format fmt)
{
bool success = true;
int32_t classname_len;
is.read (reinterpret_cast<char *> (&classname_len), 4);
if (! is)
return false;
else if (swap)
swap_bytes<4> (&classname_len);
{
OCTAVE_LOCAL_BUFFER (char, classname, classname_len+1);
classname[classname_len] = '\0';
if (! is.read (reinterpret_cast<char *> (classname), classname_len))
return false;
c_name = classname;
}
reconstruct_exemplar ();
int32_t len;
if (! is.read (reinterpret_cast<char *> (&len), 4))
return false;
if (swap)
swap_bytes<4> (&len);
if (len > 0)
{
octave_map m (map);
for (octave_idx_type j = 0; j < len; j++)
{
octave_value t2;
bool dummy;
std::string doc;
// recurse to read cell elements
std::string nm = read_binary_data (is, swap, fmt, "",
dummy, t2, doc);
if (! is)
break;
Cell tcell = (t2.iscell () ? t2.xcell_value ("load: internal error loading class elements") : Cell (t2));
m.assign (nm, tcell);
}
if (is)
{
map = m;
if (! reconstruct_parents ())
warning ("load: unable to reconstruct object inheritance");
octave::load_path& lp = octave::__get_load_path__ ("octave_class::load_binary");
if (lp.find_method (c_name, "loadobj") != "")
{
octave_value in = new octave_class (*this);
octave_value_list tmp = octave::feval ("loadobj", in, 1);
map = tmp(0).map_value ();
}
}
else
{
warning ("load: failed to load class");
success = false;
}
}
else if (len == 0)
map = octave_map (dim_vector (1, 1));
else
panic_impossible ();
return success;
}
bool
octave_class::save_hdf5 (octave_hdf5_id loc_id, const char *name,
bool save_as_floats)
{
#if defined (HAVE_HDF5)
hsize_t hdims[3];
hid_t group_hid = -1;
hid_t type_hid = -1;
hid_t space_hid = -1;
hid_t class_hid = -1;
hid_t data_hid = -1;
octave_map m;
octave_map::iterator i;
octave::load_path& lp = octave::__get_load_path__ ("octave_class::save_hdf5");
#if defined (HAVE_HDF5_18)
group_hid = H5Gcreate (loc_id, name, octave_H5P_DEFAULT, octave_H5P_DEFAULT,
octave_H5P_DEFAULT);
#else
group_hid = H5Gcreate (loc_id, name, 0);
#endif
if (group_hid < 0)
goto error_cleanup;
// Add the class name to the group
type_hid = H5Tcopy (H5T_C_S1); H5Tset_size (type_hid, c_name.length () + 1);
if (type_hid < 0)
goto error_cleanup;
hdims[0] = 0;
space_hid = H5Screate_simple (0, hdims, nullptr);
if (space_hid < 0)
goto error_cleanup;
#if defined (HAVE_HDF5_18)
class_hid = H5Dcreate (group_hid, "classname", type_hid, space_hid,
octave_H5P_DEFAULT, octave_H5P_DEFAULT,
octave_H5P_DEFAULT);
#else
class_hid = H5Dcreate (group_hid, "classname", type_hid, space_hid,
octave_H5P_DEFAULT);
#endif
if (class_hid < 0 || H5Dwrite (class_hid, type_hid, octave_H5S_ALL,
octave_H5S_ALL, octave_H5P_DEFAULT,
c_name.c_str ()) < 0)
goto error_cleanup;
#if defined (HAVE_HDF5_18)
data_hid = H5Gcreate (group_hid, "value", octave_H5P_DEFAULT,
octave_H5P_DEFAULT, octave_H5P_DEFAULT);
#else
data_hid = H5Gcreate (group_hid, "value", 0);
#endif
if (data_hid < 0)
goto error_cleanup;
if (lp.find_method (class_name (), "saveobj") != "")
{
octave_value in = new octave_class (*this);
octave_value_list tmp = octave::feval ("saveobj", in, 1);
m = tmp(0).map_value ();
}
else
m = map_value ();
// recursively add each element of the class to this group
i = m.begin ();
while (i != m.end ())
{
octave_value val = map.contents (i);
bool retval2 = add_hdf5_data (data_hid, val, m.key (i), "", false,
save_as_floats);
if (! retval2)
break;
i++;
}
error_cleanup:
if (data_hid > 0)
H5Gclose (data_hid);
if (class_hid > 0)
H5Dclose (class_hid);
if (space_hid > 0)
H5Sclose (space_hid);
if (type_hid > 0)
H5Tclose (type_hid);
if (group_hid > 0)
H5Gclose (group_hid);
return true;
#else
octave_unused_parameter (loc_id);
octave_unused_parameter (name);
octave_unused_parameter (save_as_floats);
warn_save ("hdf5");
return false;
#endif
}
bool
octave_class::load_hdf5 (octave_hdf5_id loc_id, const char *name)
{
bool retval = false;
#if defined (HAVE_HDF5)
hid_t group_hid = -1;
hid_t data_hid = -1;
hid_t type_hid = -1;
hid_t type_class_hid = -1;
hid_t space_hid = -1;
hid_t subgroup_hid = -1;
hid_t st_id = -1;
hdf5_callback_data dsub;
herr_t retval2 = 0;
octave_map m (dim_vector (1, 1));
int current_item = 0;
hsize_t num_obj = 0;
int slen = 0;
hsize_t rank = 0;
#if defined (HAVE_HDF5_18)
group_hid = H5Gopen (loc_id, name, octave_H5P_DEFAULT);
#else
group_hid = H5Gopen (loc_id, name);
#endif
if (group_hid < 0)
goto error_cleanup;
#if defined (HAVE_HDF5_18)
data_hid = H5Dopen (group_hid, "classname", octave_H5P_DEFAULT);
#else
data_hid = H5Dopen (group_hid, "classname");
#endif
if (data_hid < 0)
goto error_cleanup;
type_hid = H5Dget_type (data_hid);
type_class_hid = H5Tget_class (type_hid);
if (type_class_hid != H5T_STRING)
goto error_cleanup;
space_hid = H5Dget_space (data_hid);
rank = H5Sget_simple_extent_ndims (space_hid);
if (rank != 0)
goto error_cleanup;
slen = H5Tget_size (type_hid);
if (slen < 0)
goto error_cleanup;
// do-while loop here to prevent goto crossing initialization of classname
do
{
OCTAVE_LOCAL_BUFFER (char, classname, slen);
// create datatype for (null-terminated) string to read into:
st_id = H5Tcopy (H5T_C_S1);
H5Tset_size (st_id, slen);
if (H5Dread (data_hid, st_id, octave_H5S_ALL, octave_H5S_ALL,
octave_H5P_DEFAULT, classname)
< 0)
{
H5Tclose (st_id);
H5Dclose (data_hid);
H5Gclose (group_hid);
return false;
}
H5Tclose (st_id);
H5Dclose (data_hid);
data_hid = -1;
c_name = classname;
}
while (0);
reconstruct_exemplar ();
#if defined (HAVE_HDF5_18)
subgroup_hid = H5Gopen (group_hid, name, octave_H5P_DEFAULT);
#else
subgroup_hid = H5Gopen (group_hid, name);
#endif
H5Gget_num_objs (subgroup_hid, &num_obj);
H5Gclose (subgroup_hid);
while (current_item < static_cast<int> (num_obj)
&& (retval2 = hdf5_h5g_iterate (group_hid, name, ¤t_item,
&dsub)) > 0)
{
octave_value t2 = dsub.tc;
Cell tcell = (t2.iscell () ? t2.xcell_value ("load: internal error loading class elements") : Cell (t2));
m.assign (dsub.name, tcell);
}
if (retval2 >= 0)
{
map = m;
if (! reconstruct_parents ())
warning ("load: unable to reconstruct object inheritance");
octave::load_path& lp = octave::__get_load_path__ ("octave_class::load_hdf5");
if (lp.find_method (c_name, "loadobj") != "")
{
octave_value in = new octave_class (*this);
octave_value_list tmp = octave::feval ("loadobj", in, 1);
map = tmp(0).map_value ();
retval = true;
}
}
error_cleanup:
if (data_hid > 0)
H5Dclose (data_hid);
if (data_hid > 0)
H5Gclose (group_hid);
#else
octave_unused_parameter (loc_id);
octave_unused_parameter (name);
warn_load ("hdf5");
#endif
return retval;
}
mxArray *
octave_class::as_mxArray (void) const
{
err_wrong_type_arg ("octave_class::as_mxArray ()", type_name ());
}
bool
octave_class::in_class_method (void)
{
octave::tree_evaluator& tw
= octave::__get_evaluator__ ("octave_class::in_class_method");
octave_function *fcn = tw.current_function ();
return (fcn
&& (fcn->is_class_method ()
|| fcn->is_class_constructor ()
|| fcn->is_anonymous_function_of_class ()
|| fcn->is_private_function_of_class (class_name ()))
&& find_parent_class (fcn->dispatch_class ()));
}
octave_class::exemplar_info::exemplar_info (const octave_value& obj)
: field_names (), parent_class_names ()
{
if (! obj.isobject ())
error ("invalid call to exemplar_info constructor");
octave_map m = obj.map_value ();
field_names = m.keys ();
parent_class_names = obj.parent_class_name_list ();
}
// A map from class names to lists of fields.
std::map<std::string, octave_class::exemplar_info> octave_class::exemplar_map;
bool
octave_class::exemplar_info::compare (const octave_value& obj) const
{
if (! obj.isobject ())
error ("invalid comparison of class exemplar to non-class object");
if (nfields () != obj.nfields ())
error ("mismatch in number of fields");
octave_map obj_map = obj.map_value ();
string_vector obj_fnames = obj_map.keys ();
string_vector fnames = fields ();
for (octave_idx_type i = 0; i < nfields (); i++)
{
if (obj_fnames[i] != fnames[i])
error ("mismatch in field names");
}
if (nparents () != obj.nparents ())
error ("mismatch in number of parent classes");
const std::list<std::string> obj_parents
= obj.parent_class_name_list ();
const std::list<std::string> pnames = parents ();
auto p = obj_parents.begin ();
auto q = pnames.begin ();
while (p != obj_parents.end ())
{
if (*p++ != *q++)
error ("mismatch in parent classes");
}
return true;
}
DEFMETHOD (class, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {@var{classname} =} class (@var{obj})
@deftypefnx {} {} class (@var{s}, @var{id})
@deftypefnx {} {} class (@var{s}, @var{id}, @var{p}, @dots{})
Return the class of the object @var{obj}, or create a class with
fields from structure @var{s} and name (string) @var{id}.
Additional arguments name a list of parent classes from which the new class
is derived.
@seealso{typeinfo, isa}
@end deftypefn */)
{
int nargin = args.length ();
if (nargin == 0)
print_usage ();
octave_value retval;
if (nargin == 1)
// Called for class of object
retval = args(0).class_name ();
else
{
// Called as class constructor
std::string id = args(1).xstring_value ("class: ID (class name) must be a string");
octave::tree_evaluator& tw = interp.get_evaluator ();
octave_function *fcn = tw.caller_function ();
if (! fcn)
error ("class: invalid call from outside class constructor or method");
if (! fcn->is_class_constructor (id) && ! fcn->is_class_method (id))
error ("class: '%s' is invalid as a class name in this context",
id.c_str ());
octave_map m = args(0).xmap_value ("class: S must be a valid structure");
if (nargin == 2)
retval
= octave_value (new octave_class (m, id, std::list<std::string> ()));
else
{
octave_value_list parents = args.slice (2, nargin-2);
retval = octave_value (new octave_class (m, id, parents));
}
octave_class::exemplar_const_iterator it
= octave_class::exemplar_map.find (id);
if (it == octave_class::exemplar_map.end ())
octave_class::exemplar_map[id] = octave_class::exemplar_info (retval);
else if (! it->second.compare (retval))
error ("class: object of class '%s' does not match previously constructed objects",
id.c_str ());
}
return retval;
}
/*
%!assert (class (1.1), "double")
%!assert (class (single (1.1)), "single")
%!assert (class (uint8 (1)), "uint8")
%!testif HAVE_JAVA; usejava ("jvm")
%! jobj = javaObject ("java.lang.StringBuffer");
%! assert (class (jobj), "java.lang.StringBuffer");
## Test Input Validation
%!error class ()
*/
DEFUN (isa, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} isa (@var{obj}, @var{classname})
Return true if @var{obj} is an object from the class @var{classname}.
@var{classname} may also be one of the following class categories:
@table @asis
@item @qcode{"float"}
Floating point value comprising classes @qcode{"double"} and
@qcode{"single"}.
@item @qcode{"integer"}
Integer value comprising classes (u)int8, (u)int16, (u)int32, (u)int64.
@item @qcode{"numeric"}
Numeric value comprising either a floating point or integer value.
@end table
If @var{classname} is a cell array of string, a logical array of the same
size is returned, containing true for each class to which @var{obj}
belongs to.
@seealso{class, typeinfo}
@end deftypefn */)
{
if (args.length () != 2)
print_usage ();
octave_value obj = args(0); // not const because of find_parent_class ()
std::string obj_cls = obj.class_name ();
Array<std::string> clsnames = args(1).xcellstr_value ("isa: CLASSNAME must be a string or cell array of strings");
boolNDArray matches (clsnames.dims (), false);
for (octave_idx_type idx = 0; idx < clsnames.numel (); idx++)
{
std::string cls = clsnames(idx);
if (obj_cls == cls
|| (cls == "float" && obj.isfloat ())
|| (cls == "integer" && obj.isinteger ())
|| (cls == "numeric" && obj.isnumeric ())
|| obj.is_instance_of (cls))
matches(idx) = true;
}
return ovl (matches);
}
/*
%!assert (isa ("char", "float"), false)
%!assert (isa (logical (1), "float"), false)
%!assert (isa (double (13), "float"), true)
%!assert (isa (single (13), "float"), true)
%!assert (isa (int8 (13), "float"), false)
%!assert (isa (int16 (13), "float"), false)
%!assert (isa (int32 (13), "float"), false)
%!assert (isa (int64 (13), "float"), false)
%!assert (isa (uint8 (13), "float"), false)
%!assert (isa (uint16 (13), "float"), false)
%!assert (isa (uint32 (13), "float"), false)
%!assert (isa (uint64 (13), "float"), false)
%!assert (isa ("char", "numeric"), false)
%!assert (isa (logical (1), "numeric"), false)
%!assert (isa (double (13), "numeric"), true)
%!assert (isa (single (13), "numeric"), true)
%!assert (isa (int8 (13), "numeric"), true)
%!assert (isa (int16 (13), "numeric"), true)
%!assert (isa (int32 (13), "numeric"), true)
%!assert (isa (int64 (13), "numeric"), true)
%!assert (isa (uint8 (13), "numeric"), true)
%!assert (isa (uint16 (13), "numeric"), true)
%!assert (isa (uint32 (13), "numeric"), true)
%!assert (isa (uint64 (13), "numeric"), true)
%!assert (isa (uint8 (13), "integer"), true)
%!assert (isa (double (13), "integer"), false)
%!assert (isa (single (13), "integer"), false)
%!assert (isa (single (13), {"integer", "float", "single"}), [false true true])
%!assert (isa (double (13), "double"))
%!assert (isa (single (13), "single"))
%!assert (isa (int8 (13), "int8"))
%!assert (isa (int16 (13), "int16"))
%!assert (isa (int32 (13), "int32"))
%!assert (isa (int64 (13), "int64"))
%!assert (isa (uint8 (13), "uint8"))
%!assert (isa (uint16 (13), "uint16"))
%!assert (isa (uint32 (13), "uint32"))
%!assert (isa (uint64 (13), "uint64"))
%!assert (isa ("string", "char"))
%!assert (isa (true, "logical"))
%!assert (isa (false, "logical"))
%!assert (isa ({1, 2}, "cell"))
%!assert (isa ({1, 2}, {"numeric", "integer", "cell"}), [false false true])
%!testif HAVE_JAVA; usejava ("jvm")
%! ## The first and last assert() are equal on purpose. The assert() in
%! ## the middle with an invalid class name will cause the java code to
%! ## throw exceptions which we then must clear properly (or all other calls
%! ## will fail). So we test this too.
%! assert (isa (javaObject ("java.lang.Double", 10), "java.lang.Number"));
%! assert (isa (javaObject ("java.lang.Double", 10), "not_a_class"), false);
%! assert (isa (javaObject ("java.lang.Double", 10), "java.lang.Number"));
%!test
%! a.b = 1;
%! assert (isa (a, "struct"));
*/
DEFUN (__parent_classes__, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} __parent_classes__ (@var{x})
Undocumented internal function.
@end deftypefn */)
{
if (args.length () != 1)
print_usage ();
octave_value arg = args(0);
if (arg.isobject ())
return ovl (Cell (arg.parent_class_names ()));
else
return ovl (Cell ());
}
DEFUN (isobject, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} isobject (@var{x})
Return true if @var{x} is a class object.
@seealso{class, typeinfo, isa, ismethod, isprop}
@end deftypefn */)
{
if (args.length () != 1)
print_usage ();
return ovl (args(0).isobject ());
}
static bool
is_built_in_class (const std::string& cn)
{
static std::set<std::string> built_in_class_names;
if (built_in_class_names.empty ())
{
built_in_class_names.insert ("double");
built_in_class_names.insert ("single");
built_in_class_names.insert ("cell");
built_in_class_names.insert ("struct");
built_in_class_names.insert ("logical");
built_in_class_names.insert ("char");
built_in_class_names.insert ("function handle");
built_in_class_names.insert ("int8");
built_in_class_names.insert ("uint8");
built_in_class_names.insert ("int16");
built_in_class_names.insert ("uint16");
built_in_class_names.insert ("int32");
built_in_class_names.insert ("uint32");
built_in_class_names.insert ("int64");
built_in_class_names.insert ("uint64");
}
return built_in_class_names.find (cn) != built_in_class_names.end ();
}
DEFMETHOD (superiorto, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} superiorto (@var{class_name}, @dots{})
When called from a class constructor, mark the object currently constructed
as having a higher precedence than @var{class_name}.
More that one such class can be specified in a single call. This function
may @emph{only} be called from a class constructor.
@seealso{inferiorto}
@end deftypefn */)
{
octave::tree_evaluator& tw = interp.get_evaluator ();
octave_function *fcn = tw.caller_function ();
if (! fcn || ! fcn->is_class_constructor ())
error ("superiorto: invalid call from outside class constructor");
for (int i = 0; i < args.length (); i++)
{
std::string inf_class = args(i).xstring_value ("superiorto: CLASS_NAME must be a string");
// User defined classes always have higher precedence
// than built-in classes
if (is_built_in_class (inf_class))
break;
octave::symbol_table& symtab = interp.get_symbol_table ();
std::string sup_class = fcn->name ();
if (! symtab.set_class_relationship (sup_class, inf_class))
error ("superiorto: opposite precedence already set for %s and %s",
sup_class.c_str (), inf_class.c_str ());
}
return ovl ();
}
DEFMETHOD (inferiorto, interp, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} inferiorto (@var{class_name}, @dots{})
When called from a class constructor, mark the object currently constructed
as having a lower precedence than @var{class_name}.
More that one such class can be specified in a single call. This function
may @emph{only} be called from a class constructor.
@seealso{superiorto}
@end deftypefn */)
{
octave::tree_evaluator& tw = interp.get_evaluator ();
octave_function *fcn = tw.caller_function ();
if (! fcn || ! fcn->is_class_constructor ())
error ("inferiorto: invalid call from outside class constructor");
for (int i = 0; i < args.length (); i++)
{
std::string sup_class = args(i).xstring_value ("inferiorto: CLASS_NAME must be a string");
if (is_built_in_class (sup_class))
error ("inferiorto: cannot give user-defined class lower "
"precedence than built-in class");
octave::symbol_table& symtab = interp.get_symbol_table ();
std::string inf_class = fcn->name ();
if (! symtab.set_class_relationship (sup_class, inf_class))
error ("inferiorto: opposite precedence already set for %s and %s",
inf_class.c_str (), sup_class.c_str ());
}
return octave_value();
}
// The following classes allow us to define "inline" function objects as
// legacy @class objects (as they appear to be in Matlab) while
// preserving the is_inline_function and function_value methods that
// were previously available in the octave_fcn_inline class. However,
// inline function objects no longer behave as octave_fcn_handle objects
// so calling is_function_handle for them no longer returns true. I see
// no reasonable way to preserve that behavior. The goal here is to
// allow most code that used the old octave_inline_fcn object to
// continue to work while eliminating the octave_inline_fcn class that
// was derived from the octave_fcn_handle class. Making that change
// appears to be necessary to properly fix function handle behavior and
// improve Matlab compatibility. It's unfortunate if this change causes
// trouble, but I see no better fix. Ultimately, we should replace all
// uses of "inline" function objects with anonymous functions.
class octave_inline;
// The following class can be removed once the
// octave_value::function_value method is removed.
class
octave_inline_fcn : public octave_function
{
public:
octave_inline_fcn (octave_inline *obj) : m_inline_obj (obj) { }
// No copying!
octave_inline_fcn (const octave_inline_fcn& ob) = delete;
octave_inline_fcn& operator = (const octave_inline_fcn& ob) = delete;
~octave_inline_fcn (void) = default;
// Override default call method because we ultimately use feval to
// execute the inline function and that will push a stack frame.
octave_value_list
call (octave::tree_evaluator& tw, int nargout = 0,
const octave_value_list& args = octave_value_list ())
{
return execute (tw, nargout, args);
}
octave_value_list
execute (octave::tree_evaluator& tw, int nargout = 0,
const octave_value_list& args = octave_value_list ());
private:
octave_inline *m_inline_obj;
};
// Once the octave_inline_fcn class is removed, we should also be able
// to eliminate the octave_inline class below and replace the
// octave_value::is_inline_function method with
//
// bool octave_value::is_inline_function (void) const
// {
// return class_name () == "inline";
// }
class
octave_inline : public octave_class
{
public:
octave_inline (const octave_map& m)
: octave_class (m, "inline"), m_fcn_obj (new octave_inline_fcn (this))
{ }
octave_inline (const octave_inline&) = default;
~octave_inline (void) = default;
octave_base_value * clone (void) const { return new octave_inline (*this); }
octave_base_value * empty_clone (void) const
{
return new octave_inline (octave_map (map_keys ()));
}
bool is_inline_function (void) const { return true; }
octave_function * function_value (bool)
{
return m_fcn_obj.get ();
}
private:
std::shared_ptr<octave_inline_fcn> m_fcn_obj;
};
octave_value_list
octave_inline_fcn::execute (octave::tree_evaluator& tw, int nargout,
const octave_value_list& args)
{
octave::interpreter& interp = tw.get_interpreter ();
return interp.feval (octave_value (m_inline_obj, true), args, nargout);
}
DEFUN (__inline_ctor__, args, ,
doc: /* -*- texinfo -*-
@deftypefn {} {} __inline_ctor__ (@var{prop_struct})
Internal function.
Implements final construction for inline objects.
@end deftypefn */)
{
// Input validation has already been done in input.m.
return octave_value (new octave_inline (args(0).map_value ()));
}
|