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
|
/* Copyright (c) 1997-2024
Ewgenij Gawrilow, Michael Joswig, and the polymake team
Technische Universität Berlin, Germany
https://polymake.org
This program 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 2, or (at your option) any
later version: http://www.gnu.org/licenses/gpl.txt.
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 General Public License for more details.
--------------------------------------------------------------------------------
*/
#pragma once
#include "polymake/GenericIO.h"
#include "polymake/internal/assoc.h"
#include "polymake/internal/CharBuffer.h"
#include "polymake/internal/Wary.h"
#include "polymake/internal/Array.h"
#include "polymake/optional"
#include <limits>
#include <cmath>
#include <string>
#include <typeinfo>
#include <memory>
// TODO: eliminate all references to SV outside the perl-aware code, use opaque ScalarHolder<ownership> instead
#ifndef POLYMAKE_WITHIN_PERL
struct sv;
#define SV ::sv
#endif
namespace pm { namespace perl {
class Undefined : public std::runtime_error {
public:
Undefined();
};
class exception : public std::runtime_error {
public:
exception();
exception(const char* msg) : std::runtime_error(msg) {}
};
class SVHolder {
protected:
SV* sv;
SVHolder();
explicit SVHolder(SV* sv_arg) noexcept
: sv(sv_arg) {}
SVHolder(SV* sv_arg, std::true_type);
void set_copy(SV* sv_arg);
void forget();
public:
SV* get() const noexcept { return sv; }
SV* get_temp();
bool is_tuple() const;
};
class Scalar : public SVHolder {
public:
Scalar() {}
Scalar(const Scalar& x) : SVHolder(x.get(), std::true_type()) {}
Scalar& operator= (const Scalar& x)
{
set_copy(x.get());
return *this;
}
~Scalar() { forget(); }
// The following static functions have nothing to do with this class as such;
// it's just a suitable place to gather them withour polluting the namespace.
// Each of them produces finally a scalar value or converts it to something else.
static SV* undef();
static SV* const_string(const char* s, size_t l);
static SV* const_string(const char* s)
{
return const_string(s, strlen(s));
}
static SV* const_string(const AnyString& s)
{
return const_string(s.ptr, s.len);
}
static SV* const_string_with_int(const char* s, size_t l, int i);
static SV* const_string_with_int(const char* s, int i)
{
return const_string_with_int(s, strlen(s), i);
}
static SV* const_string_with_int(const AnyString& s, int i)
{
return const_string_with_int(s.ptr, s.len, i);
}
static SV* const_int(int i);
static Int convert_to_Int(SV* sv);
static double convert_to_Float(SV* sv);
};
// forward declarations of mutual friends
class Value; class BigObject; class BigObjectType; class PropertyValue;
class Hash; class OptionSet; class ListResult;
class Stack; class Main;
template <typename Element=Value> class ArrayOwner;
using Array = ArrayOwner<>;
template <typename T> class type_cache;
class ArrayHolder : public SVHolder {
static SV* init_me(Int size);
protected:
ArrayHolder(SV* sv_arg, std::true_type)
: SVHolder(sv_arg, std::true_type()) {}
public:
explicit ArrayHolder(Int reserve = 0)
: SVHolder(init_me(reserve)) {}
explicit ArrayHolder(const Value&);
ArrayHolder(const SVHolder& x, Int reserve)
: SVHolder(x)
{
upgrade(reserve);
}
explicit ArrayHolder(SV* sv_arg, ValueFlags flags = ValueFlags::is_trusted)
: SVHolder(sv_arg)
{
if (flags * ValueFlags::not_trusted)
verify();
else if (flags * ValueFlags::allow_undef)
upgrade(0);
}
void upgrade(Int size);
void verify() const;
void set_contains_aliases();
Int size() const;
void resize(Int n);
SV* operator[] (Int i) const;
SV* shift();
SV* pop();
void unshift(SV* x);
void push(SV* x);
void unshift(const SVHolder& x) { unshift(x.get()); }
void push(const SVHolder& x) { push(x.get()); }
};
class HashHolder : public SVHolder {
protected:
HashHolder() : SVHolder(init_me()) {}
HashHolder(SV* sv_arg, std::true_type) : SVHolder(sv_arg, std::true_type()) {}
static SV* init_me();
SV* fetch(const AnyString& key, bool create) const;
public:
explicit HashHolder(SV* sv_arg)
: SVHolder(sv_arg) {}
void verify();
bool exists(const AnyString& key) const;
};
class istreambuf : public streambuf_ext {
public:
istreambuf(SV* sv);
size_type lines() override
{
return CharBuffer::count_lines(this);
}
};
class ostreambuf : public std::streambuf {
public:
ostreambuf(SV* sv);
~ostreambuf();
protected:
SV* val;
int_type overflow(int_type c=traits_type::eof());
};
template <typename StreamBuffer>
class BufferHolder {
protected:
StreamBuffer my_buf;
explicit BufferHolder(SV* sv) : my_buf(sv) {}
};
class istream : public BufferHolder<istreambuf>,
public std::istream {
friend class Value;
public:
explicit istream(SV* sv);
void finish()
{
if (good() && CharBuffer::next_non_ws(&my_buf) >= 0)
setstate(failbit);
}
protected:
std::runtime_error parse_error() const;
};
class ostream : public BufferHolder<ostreambuf>,
public std::ostream {
public:
explicit ostream(SVHolder& sv)
: BufferHolder<ostreambuf>(sv.get()), std::ostream(&my_buf)
{
precision(10);
exceptions(failbit | badbit);
}
};
template <typename X> struct TryCanned;
template <typename X> struct Canned;
template <typename X> struct Enum;
template <typename X> struct ReturningList;
template <typename Options=mlist<>> class ValueOutput;
template <typename Options=mlist<>> class ValueInput;
class ListValueInputBase {
protected:
SV* arr_or_hash;
SV* dim_sv;
Int i;
Int size_;
Int cols_;
Int dim_;
bool sparse_;
explicit ListValueInputBase(SV* sv);
~ListValueInputBase() { finish(); }
public:
Int size() const { return size_; }
void skip_item() { ++i; }
void skip_rest() { i = size_; }
bool at_end() const { return i >= size_; }
Int get_dim(bool tell_size_if_dense) const
{
return dim_ >= 0 ? dim_ : tell_size_if_dense ? size_ : -1;
}
bool sparse_representation() const { return sparse_; }
bool is_ordered() const;
protected:
SV* get_first() const;
SV* get_next();
Int get_index() const;
void retrieve_key(std::string& dst) const;
void finish();
};
template <typename ElementType, typename Options=mlist<>>
class ListValueInput
: public ListValueInputBase
, public GenericInputImpl< ListValueInput<ElementType, Options> >
, public GenericIOoptions< ListValueInput<ElementType, Options>, Options, 1 > {
public:
using value_type = ElementType;
explicit ListValueInput(SV* sv)
: ListValueInputBase(sv)
{}
template <typename T>
ListValueInput& operator>> (T& x)
{
if (this->get_option(CheckEOF<std::false_type>()) && at_end())
throw std::runtime_error("list input - size mismatch");
retrieve(x, is_instance_of<ElementType, pair>());
return *this;
}
Int cols(bool tell_size_if_dense);
Int index(const Int index_bound) const
{
const Int ix = get_index();
if (!this->get_option(TrustedValue<std::true_type>()) && (ix < 0 || ix >= index_bound))
throw std::runtime_error("sparse input - index out of range");
return ix;
}
void finish()
{
ListValueInputBase::finish();
if (this->get_option(CheckEOF<std::false_type>()) && !at_end())
throw std::runtime_error("list input - size mismatch");
}
template <typename X>
ListValueInput& set_option(SparseRepresentation<X>) { return *this; }
using GenericIOoptions<ListValueInput, Options, 1>::set_option;
private:
template <typename T, bool anything>
void retrieve(T&, bool_constant<anything>);
template <typename T>
void retrieve(std::pair<Int, T>& x, std::true_type)
{
if (!sparse_representation()) {
retrieve(x, std::false_type());
} else {
// Maps with integer keys can be serialized as sparse containers
x.first = get_index();
retrieve(x.second, std::false_type());
}
}
template <typename T>
void retrieve(std::pair<std::string, T>& x, std::true_type)
{
if (is_ordered()) {
retrieve(x, std::false_type());
} else {
retrieve_key(x.first);
retrieve(x.second, std::false_type());
}
}
};
template <typename Options>
class ValueInput
: public SVHolder
, public GenericInputImpl< ValueInput<Options> >
, public GenericIOoptions< ValueInput<Options>, Options > {
template <typename, typename> friend class ListValueInput;
public:
ValueInput(SV* sv_arg)
: SVHolder(sv_arg) {}
template <typename Data>
void fallback(Data& x)
{
istream is(sv);
is >> x; is.finish();
}
using SVHolder::is_tuple;
template <typename ObjectRef>
struct list_cursor {
using cursor_options = typename mtagged_list_remove<Options, SparseRepresentation>::type;
using type = ListValueInput<typename deref<ObjectRef>::type::value_type, cursor_options>;
};
template <typename ObjectRef>
struct composite_cursor {
using cursor_options = typename mtagged_list_replace<
typename mtagged_list_remove<Options, SparseRepresentation>::type,
CheckEOF<std::true_type>>::type;
using type = ListValueInput<void, cursor_options>;
};
template <typename T>
decltype(auto) begin_list(T*)
{
return typename list_cursor<T>::type(sv);
}
template <typename T>
decltype(auto) begin_composite(T*)
{
return typename composite_cursor<T>::type(sv);
}
};
template <typename Options, bool returning_list = tagged_list_extract_integral<Options, ReturningList>(false)>
class ListValueOutput
: public ArrayHolder
, public GenericIOoptions< ListValueOutput<Options>, Options > {
ListValueOutput();
public:
using super = SVHolder;
using super_arg = SV*;
static const bool stack_based = false;
template <typename T>
ListValueOutput& operator<< (T&& x);
ListValueOutput& non_existent()
{
return *this << Undefined();
}
void finish() const {}
};
template <typename Options>
class ValueOutput
: public ListValueOutput<Options>::super
, public GenericOutputImpl< ValueOutput<Options> >
, public GenericIOoptions< ValueOutput<Options>, Options > {
using super = typename ListValueOutput<Options>::super;
using super_arg = typename ListValueOutput<Options>::super_arg;
public:
template <typename Data>
void fallback(const Data& x)
{
store(x, bool_constant<ListValueOutput<Options>::stack_based>());
}
void
fallback(const char* x, size_t l)
{
store_string(x, l, bool_constant<ListValueOutput<Options>::stack_based>());
}
// TODO: investigate and remove
bool is_tuple() const { return false; }
private:
template <typename Data>
void store(const Data& x, std::false_type)
{
ostream os(*this);
os << x;
}
template <typename Data>
void store(const Data& x, std::true_type)
{
SVHolder s;
ostream os(s);
os << x;
this->push_temp(s);
}
void store_string(const char* x, size_t l, std::false_type);
void store_string(const char* x, size_t l, std::true_type);
public:
ValueOutput() {}
explicit ValueOutput(super_arg sv_arg)
: super(sv_arg) {}
template <typename ObjectRef>
struct list_cursor {
using type = ListValueOutput<Options>&;
};
template <typename ObjectRef>
struct composite_cursor {
using Object = typename deref<ObjectRef>::type;
static constexpr int
total = list_length<typename object_traits<Object>::elements>::value,
ignored = list_accumulate_unary<list_count, ignore_in_composite, typename object_traits<Object>::elements>::value;
static constexpr bool
compress = ignored > 0 && total - ignored <= 1;
using type = std::conditional_t<compress, ValueOutput, ListValueOutput<Options>>&;
};
template <typename ObjectRef>
struct sparse_cursor : list_cursor<ObjectRef> {};
template <typename T>
ListValueOutput<Options>& begin_list(const T* x)
{
ListValueOutput<Options>& pvl=static_cast<ListValueOutput<Options>&>(static_cast<super&>(*this));
pvl.upgrade(x && !object_traits<T>::is_lazy && container_traits<T>::is_forward ? x->size() : 0);
return pvl;
}
private:
template <typename T>
ListValueOutput<Options>& begin_composite_impl(const T*, std::false_type)
{
ListValueOutput<Options>& pvl=static_cast<ListValueOutput<Options>&>(static_cast<super&>(*this));
pvl.upgrade(list_length<typename object_traits<T>::elements>::value);
return pvl;
}
template <typename T>
ValueOutput& begin_composite_impl(const T*, std::true_type)
{
return *this;
}
public:
template <typename T>
typename composite_cursor<T>::type
begin_composite(const T*)
{
return begin_composite_impl((const T*)nullptr, bool_constant<composite_cursor<T>::compress>());
}
template <typename T>
ListValueOutput<Options>& begin_sparse(const T* x)
{
return begin_list(x);
}
static constexpr int choose_sparse_representation() { return -1; }
};
class Stack {
protected:
Stack();
Stack(SV** start);
explicit Stack(Int reserve);
private:
Stack(const Stack&) = delete;
void operator= (const Stack&) = delete;
protected:
void xpush(SV* x) const;
void push(SV* x) const;
void push(const AnyString& s) const;
void extend(Int n);
public:
Stack(Stack&&) = default;
void push(SVHolder& x) const
{
xpush(x.get());
}
void push_temp(SVHolder& x) const
{
xpush(x.get_temp());
}
void cancel();
};
class ListReturn : public Stack {
private:
void operator= (const ListReturn&) = delete;
ListReturn(const ListReturn&) = delete;
public:
ListReturn() {}
explicit ListReturn(SV** stack_arg)
: Stack(stack_arg) {}
ListReturn(ListReturn&&) = default;
template <typename T>
ListReturn& operator<< (T&& x)
{
upgrade(1);
store(std::forward<T>(x));
return *this;
}
template <typename... T>
ListReturn& operator<< (std::tuple<T...>&& t)
{
store(std::move(t), typename index_sequence_for<pure_type_t<decltype(t)>>::type());
return *this;
}
void upgrade(Int size);
private:
template <typename T>
void store(T&& x);
template <typename T, int... Indexes>
void store(T&& t, std::index_sequence<Indexes...>)
{
upgrade(sizeof...(Indexes));
(void)std::initializer_list<bool>{ (store(std::get<Indexes>(std::forward<T>(t))), true)... };
}
};
template <typename Options>
class ListValueOutput<Options, true>
: public ListReturn
, public GenericIOoptions< ListValueOutput<Options, true>, Options > {
ListValueOutput();
public:
using super = Stack;
using super_arg = SV**;
static const bool stack_based=true;
void finish() const {}
};
using ListSlurp = ValueOutput<mlist<ReturningList<std::true_type>>>;
} // end namespace perl
template <>
struct is_printable<perl::Value> : std::false_type {};
template <>
struct is_parseable<perl::Value> : std::false_type {};
template <>
struct is_printable<perl::BigObject> : std::false_type {};
template <>
struct is_writeable<perl::BigObject> : std::true_type {};
// forward declaration of a specialization
template <>
class Array<perl::BigObject>;
template <>
struct is_printable< Array<perl::BigObject> > : std::false_type {};
template <>
struct is_writeable< Array<perl::BigObject> > : std::true_type {};
namespace perl {
template <typename T,
bool _has_generic=has_generic_type<T>::value>
struct generic_representative {
using type = typename object_traits<T>::persistent_type;
};
template <typename T>
struct generic_representative<T, true> {
using type = typename T::generic_type::persistent_type;
};
// primitive perl types which need special handling if returned by reference in lvalue context
using primitive_lvalues = mlist<bool, Int, double, std::string>;
template <typename T>
struct is_optional_value
: is_instance_of<T, optional> {};
template <typename T>
struct numeric_traits : std::numeric_limits<type_behind_t<T>> {
using real_type = type_behind_t<T>;
static constexpr bool check_range = std::numeric_limits<real_type>::is_bounded && std::numeric_limits<real_type>::is_integer;
};
template <typename Target> class access;
template <typename Target> struct represents_BigObject : std::false_type {};
class Value
: public SVHolder {
public:
explicit Value(ValueFlags opt_arg=ValueFlags::is_trusted)
: options(opt_arg)
{}
explicit Value(SV* sv_arg, ValueFlags opt_arg=ValueFlags::is_trusted) noexcept
: SVHolder(sv_arg)
, options(opt_arg)
{}
ValueFlags get_flags() const noexcept { return options; }
struct Anchor {
void store(SV* sv) noexcept;
void store(const Value& v) noexcept { store(v.get()); }
SV* stored;
};
struct NoAnchors {
constexpr operator Anchor* () const { return nullptr; }
};
template <bool is_readonly> class Array_element_factory;
template <typename Target> struct check_for_magic_storage;
using nomagic_types = mlist<Undefined, AnyString, std::string, BigObject, BigObjectType, PropertyValue, Scalar, Array, Hash, ListReturn, pm::Array<BigObject>>;
using nomagic_lvalue_types = mlist<Scalar, Array, Hash>;
protected:
enum number_flags { not_a_number, number_is_zero, number_is_int, number_is_float, number_is_object };
ValueFlags options;
bool is_defined() const noexcept;
bool is_TRUE() const;
Int Int_value() const;
Int enum_value(size_t s, bool expect_ref) const;
double Float_value() const;
bool is_plain_text(bool expect_numeric_scalar=false) const;
struct canned_data_t {
const std::type_info* ti;
char* value;
bool read_only;
};
static
canned_data_t get_canned_data(SV*) noexcept;
const std::type_info* get_canned_typeinfo() const noexcept { return get_canned_data(sv).ti; }
char* get_canned_value() const noexcept { return get_canned_data(sv).value; }
Int get_canned_dim(bool tell_size_if_dense) const;
template <typename... AnchorList>
static void store_anchors(Anchor* place, AnchorList&&... anchors) noexcept
{
(void)std::initializer_list<bool>{ ((place++)->store(anchors), true)... };
}
std::pair<void*, Anchor*> allocate_canned(SV* proto, int n_anchors) const;
void* allocate_canned(SV* proto) const { return allocate_canned(proto, 0).first; }
void mark_canned_as_initialized();
Anchor* store_canned_ref_impl(void* obj, SV* type_descr, ValueFlags flags, int n_anchors) const;
template <typename Numtype>
static
void assign_Int(Numtype& x, Int i, std::false_type) { x = i; }
static
void assign_Int(long& x, Int i, std::true_type) { x = i; }
template <typename Numtype>
static
void assign_Int(Numtype& x, Int i, std::true_type)
{
if (i < min_value_as<Int>(mlist<Numtype>()) ||
i > max_value_as<Int>(mlist<Numtype>()))
throw std::runtime_error("input numeric property out of range");
x = static_cast<typename numeric_traits<Numtype>::real_type>(i);
}
template <typename Numtype>
static
void assign_Float(Numtype& x, double d, std::false_type) { x=d; }
template <typename Numtype>
static
void assign_Float(Numtype& x, double d, std::true_type)
{
if (d < min_value_as<double>(mlist<Numtype>()) ||
d > max_value_as<double>(mlist<Numtype>()))
throw std::runtime_error("input numeric property out of range");
x = static_cast<typename numeric_traits<Numtype>::real_type>(lrint(d));
}
number_flags classify_number() const;
template <typename Numtype>
void num_input(Numtype& x) const
{
switch (classify_number()) {
case number_is_zero:
x= 0;
break;
case number_is_int:
assign_Int(x, Int_value(), bool_constant<numeric_traits<Numtype>::check_range>());
break;
case number_is_float:
assign_Float(x, Float_value(), bool_constant<numeric_traits<Numtype>::check_range>());
break;
case number_is_object:
assign_Int(x, Scalar::convert_to_Int(sv), bool_constant<numeric_traits<Numtype>::check_range>());
break;
case not_a_number:
throw std::runtime_error("invalid value for an input numerical property");
}
}
std::false_type* retrieve(std::string& x) const;
std::false_type* retrieve(AnyString &x) const;
std::false_type* retrieve(char &x) const;
std::false_type* retrieve(double& x) const;
std::false_type* retrieve(bool& x) const;
std::false_type* retrieve(Int& x) const { num_input(x); return nullptr; }
template <typename T>
std::enable_if_t<std::is_enum<T>::value, std::false_type*>
retrieve(T& x) const
{
const Int val = enum_value(sizeof(T), false);
x = static_cast<T>(val);
return nullptr;
}
std::false_type* retrieve(float& x) const
{
double xi;
retrieve(xi);
x=static_cast<float>(xi);
return nullptr;
}
std::false_type* retrieve(Array& x) const;
std::false_type* retrieve(BigObject& x) const;
std::false_type* retrieve(BigObjectType& x) const;
std::false_type* retrieve(pm::Array<BigObject>& x) const;
template <typename Target>
std::enable_if_t<represents_BigObject<Target>::value, std::false_type*>
retrieve(Target& x) const;
template <typename Target, typename Options>
void do_parse(Target& x, Options) const
{
istream my_stream(sv);
PlainParser<Options> parser(my_stream);
try {
parser >> x;
my_stream.finish();
} catch (const std::ios::failure&) {
throw my_stream.parse_error();
}
}
// opaque type
template <typename Target, typename Serializable>
void retrieve(Target& x, std::false_type, Serializable) const
{
if (options * ValueFlags::not_trusted)
ValueInput<mlist<TrustedValue<std::false_type>>>(sv) >> x;
else
ValueInput<>(sv) >> x;
}
// numeric scalar type, non-serializable
template <typename Target>
void retrieve(Target& x, std::true_type, std::false_type) const
{
num_input(x);
}
// numeric scalar type, serializable
template <typename Target>
void retrieve(Target& x, std::true_type, std::true_type) const
{
if (is_tuple())
retrieve(x, std::false_type(), std::true_type());
else
num_input(x);
}
template <typename Target>
std::enable_if_t<check_for_magic_storage<Target>::value && is_parseable<Target>::value>
retrieve_nomagic(Target& x) const
{
if (is_plain_text(numeric_traits<Target>::is_specialized)) {
parse(x);
} else {
retrieve(x, bool_constant<numeric_traits<Target>::is_specialized>(), has_serialized<Target>());
}
}
template <typename Target>
std::enable_if_t<check_for_magic_storage<Target>::value && !is_parseable<Target>::value>
retrieve_nomagic(Target& x) const
{
retrieve(x, bool_constant<numeric_traits<Target>::is_specialized>(), has_serialized<Target>());
}
template <typename Target>
std::enable_if_t<!check_for_magic_storage<Target>::value>
retrieve_nomagic(Target& x) const
{
retrieve(x);
}
template <typename Target>
std::enable_if_t<object_traits<Target>::is_persistent && std::is_destructible<Target>::value, bool>
retrieve_with_conversion(Target& x) const
{
if (options * ValueFlags::allow_conversion) {
using conv_f = Target (*)(const Value&);
if (conv_f conversion=reinterpret_cast<conv_f>(type_cache<Target>::get_conversion_operator(sv))) {
x=conversion(*this);
return true;
}
}
return false;
}
template <typename Target>
std::enable_if_t<!(object_traits<Target>::is_persistent && std::is_destructible<Target>::value), bool>
retrieve_with_conversion(Target&) const
{
return false;
}
template <typename Target>
std::enable_if_t<std::is_copy_assignable<Target>::value &&
!(represents_BigObject<Target>::value || std::is_enum<Target>::value), std::true_type*>
retrieve(Target& x) const
{
if (!(options * ValueFlags::ignore_magic)) {
const canned_data_t canned = get_canned_data(sv);
if (canned.ti) {
if (*canned.ti == typeid(Target)) {
if (MaybeWary<Target>::value && options * ValueFlags::not_trusted)
maybe_wary(x)=*reinterpret_cast<const Target*>(canned.value);
else
x=*reinterpret_cast<const Target*>(canned.value);
return nullptr;
}
using ass_f = void (*)(Target&, const Value&);
if (ass_f assignment=reinterpret_cast<ass_f>(type_cache<Target>::get_assignment_operator(sv))) {
assignment(x, *this);
return nullptr;
}
if (retrieve_with_conversion(x))
return nullptr;
if (type_cache<Target>::magic_allowed())
throw std::runtime_error("invalid assignment of " + legible_typename(*canned.ti) + " to " + legible_typename<Target>());
}
}
retrieve_nomagic(x);
return nullptr;
}
template <typename Target>
std::enable_if_t<!std::is_copy_assignable<Target>::value, std::true_type*>
retrieve(Target& x) const
{
const canned_data_t canned = get_canned_data(sv);
if (!(canned.ti && *canned.ti == typeid(Target) && !canned.read_only))
throw Undefined();
x = std::move(*reinterpret_cast<Target*>(canned.value));
}
template <typename Target>
Target retrieve_copy(std::enable_if_t<mlist_contains<nomagic_types, Target>::value || !check_for_magic_storage<Target>::value, std::nullptr_t> = nullptr) const
{
Target x{};
if (__builtin_expect(sv && is_defined(), 1)) {
retrieve_nomagic(x);
} else if (!(options * ValueFlags::allow_undef)) {
throw Undefined();
}
return x;
}
// some code duplication with generic retrieve() is deliberate
template <typename Target>
Target retrieve_copy(std::enable_if_t<std::is_copy_constructible<Target>::value &&
!mlist_contains<nomagic_types, Target>::value &&
check_for_magic_storage<Target>::value, std::nullptr_t> = nullptr) const
{
if (__builtin_expect(!sv || !is_defined(), 0)) {
if (!(options * ValueFlags::allow_undef))
throw Undefined();
return Target{};
}
if (!(options * ValueFlags::ignore_magic)) {
const canned_data_t canned = get_canned_data(sv);
if (canned.ti) {
if (*canned.ti == typeid(Target))
return *reinterpret_cast<const Target*>(canned.value);
using conv_f = Target (*)(const Value&);
if (conv_f conversion=reinterpret_cast<conv_f>(type_cache<Target>::get_conversion_operator(sv)))
return conversion(*this);
if (type_cache<Target>::magic_allowed())
throw std::runtime_error("invalid conversion from " + legible_typename(*canned.ti) + " to " + legible_typename<Target>());
}
}
Target x{};
retrieve_nomagic(x);
return x;
}
template <typename Source>
void store_as_perl(const Source& x)
{
static_cast<ValueOutput<>&>(static_cast<SVHolder&>(*this)) << x;
}
template <typename Stored, typename SourceRef>
Anchor* store_canned_value(SourceRef&& x, SV* type_descr, int n_anchors)
{
if (type_descr) {
auto place=allocate_canned(type_descr, n_anchors);
new(place.first) Stored(std::forward<SourceRef>(x));
mark_canned_as_initialized();
return place.second;
}
store_as_perl(x);
return nullptr;
}
template <typename Source>
Anchor* store_canned_ref(const Source& x, SV* type_descr, int n_anchors)
{
if (type_descr)
return store_canned_ref_impl((void*)&x, type_descr, options, n_anchors);
store_as_perl(x);
return nullptr;
}
// non-persistent regular type
template <typename SourceRef>
Anchor* store_canned_value(SourceRef&& x, int n_anchors, std::false_type, std::false_type, std::false_type)
{
using Source = pure_type_t<SourceRef>;
using Persistent = typename object_traits<Source>::persistent_type;
if (options * ValueFlags::allow_non_persistent)
return store_canned_value<Source>(std::forward<SourceRef>(x), type_cache<Source>::get_descr(), n_anchors);
else
return store_canned_value<Persistent>(std::forward<SourceRef>(x), type_cache<Persistent>::get_descr(), 0);
}
// lazy type
template <typename SourceRef, typename IsMasquerade, typename IsPersistent>
Anchor* store_canned_value(SourceRef&& x, int n_anchors, IsMasquerade, std::true_type, IsPersistent)
{
using Source = pure_type_t<SourceRef>;
using Persistent = typename object_traits<Source>::persistent_type;
return store_canned_value<Persistent>(std::forward<SourceRef>(x), type_cache<Persistent>::get_descr(), 0);
}
// non-persistent regular type
template <typename Source, typename IsMasquerade>
Anchor* store_canned_ref(const Source& x, int n_anchors, IsMasquerade, std::false_type, std::false_type)
{
using Persistent = typename object_traits<Source>::persistent_type;
if (options * ValueFlags::allow_non_persistent)
return store_canned_ref(x, type_cache<Source>::get_descr(), n_anchors);
else
return store_canned_value<Persistent>(x, type_cache<Persistent>::get_descr(), 0);
}
// lazy type - never called
template <typename Source, typename IsMasquerade, typename IsPersistent>
Anchor* store_canned_ref(const Source& x, int n_anchors, IsMasquerade, std::true_type, IsPersistent)
{
return nullptr;
}
// persistent regular type
template <typename SourceRef>
Anchor* store_canned_value(SourceRef&& x, int n_anchors, std::false_type, std::false_type, std::true_type)
{
using Source = pure_type_t<SourceRef>;
return store_canned_value<Source>(std::forward<SourceRef>(x), type_cache<Source>::get_descr(), n_anchors);
}
// persistent regular type
template <typename Source>
Anchor* store_canned_ref(const Source& x, int n_anchors, std::false_type, std::false_type, std::true_type)
{
return store_canned_ref(x, type_cache<Source>::get_descr(), n_anchors);
}
// masquerade type belonging to a generic family
template <typename SourceRef>
Anchor* store_canned_value(SourceRef&& x, int n_anchors, std::true_type, std::false_type, std::false_type)
{
using Source = pure_type_t<SourceRef>;
using Persistent = typename object_traits<Source>::persistent_type;
return store_canned_value<Persistent>(std::forward<SourceRef>(x), type_cache<Persistent>::get_descr(), 0);
}
// masquerade type without persistent substitute
template <typename Source>
Anchor* store_canned_value(const Source& x, int n_anchors, std::true_type, std::false_type, std::true_type)
{
store_as_perl(x);
return nullptr;
}
// masquerade type without persistent substitute
template <typename Source>
Anchor* store_canned_ref(const Source& x, int n_anchors, std::true_type, std::false_type, std::true_type)
{
if (options * ValueFlags::allow_non_persistent) {
return store_canned_ref(x, type_cache<Source>::get_descr(), n_anchors);
}
store_as_perl(x);
return nullptr;
}
Anchor* store_primitive_ref(const bool& x, SV* type_descr, int n_anchors);
Anchor* store_primitive_ref(const Int& x, SV* type_descr, int n_anchors);
Anchor* store_primitive_ref(const double& x, SV* type_descr, int n_anchors);
Anchor* store_primitive_ref(const std::string& x, SV* type_descr, int n_anchors);
void set_string_value(const char* x);
void set_string_value(const char* x, size_t l);
void set_copy(const SVHolder& x);
NoAnchors put_val(Int x, int=0);
NoAnchors put_val(int x, int=0) { return put_val(Int(x)); }
NoAnchors put_val(size_t x, int=0) { return put_val(Int(x)); }
NoAnchors put_val(bool x, int=0);
NoAnchors put_val(double x, int=0);
NoAnchors put_val(const Undefined&, int=0);
template <typename T>
std::enable_if_t<std::is_enum<T>::value, NoAnchors>
put_val(const T& x, int=0)
{
const Int val = static_cast<Int>(x);
return put_val(val);
}
NoAnchors put_val(const AnyString& x, int=0)
{
if (x)
set_string_value(x.ptr, x.len);
else
put_val(Undefined());
return NoAnchors();
}
// need this one separately because otherwise the vile compiler coerces the string array to a boolean
template <size_t n>
NoAnchors put_val(const char (&x)[n], int=0)
{
set_string_value(x+0, n-1);
return NoAnchors();
}
NoAnchors put_val(char x, int=0)
{
set_string_value(&x, 1);
return NoAnchors();
}
NoAnchors put_val(const BigObject& x, int=0);
NoAnchors put_val(const BigObjectType& x, int=0);
NoAnchors put_val(const PropertyValue& x, int=0);
NoAnchors put_val(const Scalar& x, int=0);
NoAnchors put_val(const Array& x, int=0);
NoAnchors put_val(const Hash& x, int=0);
NoAnchors put_val(const ListReturn& x, int=0);
NoAnchors put_val(const pm::Array<BigObject>& x, int=0);
template <typename SourceRef>
std::enable_if_t<is_class_or_union<pure_type_t<SourceRef>>::value &&
!(is_derived_from_any<pure_type_t<SourceRef>, nomagic_types>::value ||
represents_BigObject<pure_type_t<SourceRef>>::value ||
is_optional_value<pure_type_t<SourceRef>>::value ||
!std::is_same<typename object_traits<pure_type_t<SourceRef>>::proxy_for, void>::value),
Anchor*>
put_val(SourceRef&& x, int n_anchors)
{
using Source = pure_type_t<SourceRef>;
using Persistent = typename object_traits<Source>::persistent_type;
if (object_traits<Source>::is_lazy ||
!(std::is_rvalue_reference<SourceRef&&>::value
? options * ValueFlags::allow_store_temp_ref
: options * ValueFlags::allow_store_ref)) {
// must store a copy
return store_canned_value(std::forward<SourceRef>(x), n_anchors, is_masquerade<Source>(),
bool_constant<object_traits<Source>::is_lazy>(), std::is_same<Source, Persistent>());
} else {
// can store a reference
return store_canned_ref(x, n_anchors, is_masquerade<Source>(),
bool_constant<object_traits<Source>::is_lazy>(), std::is_same<Source, Persistent>());
}
}
template <typename SourceRef>
std::enable_if_t<represents_BigObject<pure_type_t<SourceRef>>::value, NoAnchors>
put_val(SourceRef&& x, int);
template <typename Source, typename Deleter>
Anchor* put_val(std::unique_ptr<Source, Deleter>&& ptr, int n_anchors)
{
if (SV* type_descr=type_cache<std::unique_ptr<Source, Deleter>>::get_descr()) {
if (options * ValueFlags::allow_non_persistent) {
return store_canned_value<std::unique_ptr<Source, Deleter>>(std::move(ptr), type_descr, n_anchors);
} else {
throw std::invalid_argument("can't store a pointer to an opaque C++ object");
}
} else {
throw std::invalid_argument("can't store an opaque C++ type without perl binding");
}
}
template <typename SourceRef>
std::enable_if_t<is_optional_value<pure_type_t<SourceRef>>::value, Anchor*>
put_val(SourceRef&& x, int n_anchors)
{
if (x) {
return put_val(std::forward<SourceRef>(x).value(), n_anchors);
} else {
put_val(Undefined());
return nullptr;
}
}
template <typename SourceRef>
std::enable_if_t<!std::is_same<typename object_traits<pure_type_t<SourceRef>>::proxy_for, void>::value, Anchor*>
put_val(SourceRef&& x, int n_anchors)
{
SV* type_descr;
using Source = pure_type_t<SourceRef>;
if ((options & (ValueFlags::allow_non_persistent | ValueFlags::expect_lval | ValueFlags::read_only)) ==
(ValueFlags::allow_non_persistent | ValueFlags::expect_lval) &&
(type_descr=type_cache<Source>::get_descr())) {
return store_canned_value<Source>(std::move(x), type_descr, n_anchors);
} else {
return put_val(static_cast<const type_behind_t<Source>&>(x), 0);
}
}
public:
template <typename SourceRef, typename... AnchorList>
void put(SourceRef&& x, AnchorList&&... anchors)
{
Anchor* anchor_place = put_val(std::forward<SourceRef>(x), sizeof...(AnchorList));
if (sizeof...(AnchorList) && anchor_place)
store_anchors(anchor_place, std::forward<AnchorList>(anchors)...);
}
template <typename SourceRef, typename... AnchorList>
std::enable_if_t<!mlist_contains<mlist_concat<primitive_lvalues, nomagic_lvalue_types>::type, pure_type_t<SourceRef>>::value>
put_lvalue(SourceRef&& x, AnchorList&&... anchors)
{
put(std::forward<SourceRef>(x), std::forward<AnchorList>(anchors)...);
}
template <typename SourceRef, typename... AnchorList>
std::enable_if_t<mlist_contains<primitive_lvalues, pure_type_t<SourceRef>>::value && std::is_lvalue_reference<SourceRef&&>::value>
put_lvalue(SourceRef&& x, AnchorList&&... anchors)
{
using Source = pure_type_t<SourceRef>;
Anchor* anchor_place = store_primitive_ref(x, type_cache<Source>::get_descr(), sizeof...(AnchorList));
if (sizeof...(AnchorList) && anchor_place)
store_anchors(anchor_place, std::forward<AnchorList>(anchors)...);
}
template <typename SourceRef, typename... AnchorList>
std::enable_if_t<mlist_contains<primitive_lvalues, pure_type_t<SourceRef>>::value && !std::is_lvalue_reference<SourceRef&&>::value>
put_lvalue(SourceRef&& x, AnchorList&&... anchors)
{
put_val(x); // no anchors can ever be needed for a primitive scalar
}
template <typename Target>
void parse(Target& x) const
{
if (options * ValueFlags::not_trusted)
do_parse(x, mlist<TrustedValue<std::false_type>>());
else
do_parse(x, mlist<>());
}
template <typename Target>
operator Target () const
{
return retrieve_copy<Target>();
}
explicit operator bool () const { return is_TRUE(); }
bool operator! () const { return !is_TRUE(); }
template <typename Target>
friend
std::enable_if_t<!is_effectively_const<Target>::value, bool>
operator>> (const Value& me, Target&& x)
{
if (!me.sv || !me.is_defined()) {
if (!(me.options * ValueFlags::allow_undef))
throw Undefined();
return false;
}
me.retrieve(x);
return true;
}
template <typename Source>
friend void operator<< (const Value& me, Source&& x)
{
const_cast<Value&>(me).put(std::forward<Source>(x));
}
template <typename Target>
void* allocate(SV* proto)
{
return allocate_canned(type_cache<Target>::get_descr(proto));
}
SV* get_constructed_canned();
template <typename Target>
decltype(auto) get() const
{
return access<Target>::get(*this);
}
using SVHolder::get;
template <typename T>
Int get_dim(bool tell_size_if_dense) const
{
Int d = -1;
if (is_plain_text()) {
istream my_stream(sv);
if (options * ValueFlags::not_trusted)
d = PlainParser<mlist<TrustedValue<std::false_type>>>(my_stream).begin_list((T*)nullptr).get_dim(tell_size_if_dense);
else
d = PlainParser<>(my_stream).begin_list((T*)nullptr).get_dim(tell_size_if_dense);
} else if (get_canned_typeinfo()) {
d = get_canned_dim(tell_size_if_dense);
} else {
if (options * ValueFlags::not_trusted)
d = ListValueInput<T, mlist<TrustedValue<std::false_type>>>(sv).get_dim(tell_size_if_dense);
else
d = ListValueInput<T>(sv).get_dim(tell_size_if_dense);
}
return d;
}
protected:
template <typename Target>
Target& parse_and_can() const
{
Value temp_can;
Target* temp_val=new(temp_can.allocate<Target>(nullptr)) Target{};
retrieve_nomagic(*temp_val);
const_cast<Value&>(*this).sv = temp_can.get_constructed_canned();
return *temp_val;
}
template <typename Target, typename Source,
typename = std::enable_if_t<!std::is_same<pure_type_t<Target>, pure_type_t<Source>>::value>>
Target& convert_and_can(Source& src) const
{
using data_t = std::remove_const_t<Target>;
Value temp_can;
data_t* temp_val=new(temp_can.allocate<data_t>(nullptr)) data_t{src};
const_cast<Value&>(*this).sv = temp_can.get_constructed_canned();
return *temp_val;
}
template <typename Target>
Target& convert_and_can(Target& src) const
{
return src;
}
template <typename Target, typename Deleter>
Target& convert_and_can(const std::unique_ptr<Target, Deleter>& src) const
{
return *src;
}
template <typename Target>
Target& convert_and_can(const canned_data_t& canned) const
{
using conv_f = Target (*)(const Value&);
if (conv_f conversion=reinterpret_cast<conv_f>(type_cache<Target>::get_conversion_operator(sv))) {
Value temp_can;
Target* temp_val=new(temp_can.allocate<Target>(nullptr)) Target{conversion(*this)};
const_cast<Value&>(*this).sv = temp_can.get_constructed_canned();
return *temp_val;
}
throw std::runtime_error("invalid conversion from " + legible_typename(*canned.ti) + " to " + legible_typename<Target>());
}
template <typename> friend class access;
friend class ArrayHolder;
friend class OptionSet;
friend class ListResult;
template <typename> friend class ValueOutput;
template <typename, typename> friend class ListValueInput;
};
template <typename Target>
struct Value::check_for_magic_storage
: std::remove_pointer_t<decltype(Value().retrieve(std::declval<Target&>()))> {};
template <size_t S>
class ArgValues {
using value_tuple = typename mlist2tuple<typename mreplicate<Value, S>::type>::type;
value_tuple values;
ArgValues(const ArgValues&) = delete;
template <size_t... I>
ArgValues(SV** stack, ValueFlags opt_arg, std::index_sequence<I...>)
: values(Value(stack[I], opt_arg)...) {}
public:
explicit ArgValues(SV** stack, ValueFlags opt_arg=ValueFlags::is_trusted)
: ArgValues(stack, opt_arg, std::make_index_sequence<S>()) {}
template <size_t I, typename Target>
decltype(auto) get() const
{
return std::get<I>(values).template get<Target>();
}
};
template <bool is_readonly>
class Value::Array_element_factory {
public:
using argument_type = Int;
using result_type = Value;
explicit Array_element_factory(const ArrayHolder* array_arg = nullptr)
: array(array_arg) {}
result_type operator() (Int i) const
{
return result_type((*array)[i], (is_readonly ? ValueFlags::read_only : ValueFlags::is_mutable) | ValueFlags::not_trusted);
}
protected:
const ArrayHolder* array;
};
}
template <bool is_readonly>
struct operation_cross_const_helper<perl::Value::Array_element_factory<is_readonly>> {
using operation = perl::Value::Array_element_factory<false>;
using const_operation = perl::Value::Array_element_factory<true>;
};
namespace perl {
inline ArrayHolder::ArrayHolder(const Value& v)
: SVHolder(v.sv)
{
if (v.options * ValueFlags::not_trusted) verify();
}
template <typename ElementType, typename Options>
template <typename T, bool anything>
void ListValueInput<ElementType, Options>::retrieve(T& x, bool_constant<anything>)
{
Value elem(get_next(), this->get_option(TrustedValue<std::true_type>()) ? ValueFlags::is_trusted : ValueFlags::not_trusted);
elem >> x;
}
template <typename ElementType, typename Options>
Int ListValueInput<ElementType, Options>::cols(bool tell_size_if_dense)
{
if (cols_ < 0) {
if (SV* first_sv = get_first()) {
Value first(first_sv, this->get_option(TrustedValue<std::true_type>()) ? ValueFlags::is_trusted : ValueFlags::not_trusted);
cols_ = first.get_dim<ElementType>(tell_size_if_dense);
}
}
return cols_;
}
template <typename Options>
void ValueOutput<Options>::store_string(const char* x, size_t l, std::false_type)
{
static_cast<Value*>(static_cast<super*>(this))->set_string_value(x, l);
}
template <typename Options>
void ValueOutput<Options>::store_string(const char* x, size_t l, std::true_type)
{
Value v;
v.set_string_value(x,l);
this->push_temp(v);
}
template <typename Options, bool returning_list>
template <typename T>
ListValueOutput<Options, returning_list>&
ListValueOutput<Options, returning_list>::operator<< (T&& x)
{
Value elem;
elem << std::forward<T>(x);
push(elem);
return *this;
}
template <typename T>
void ListReturn::store(T&& x)
{
Value result;
result << std::forward<T>(x);
push(result.get_temp());
}
template <typename Element>
class ArrayOwner
: public ArrayHolder
, public modified_container_impl< ArrayOwner<Element>,
mlist< ContainerTag< sequence >,
OperationTag< typename Element::template Array_element_factory<false> > > > {
friend class Value;
protected:
explicit ArrayOwner(SV* sv_arg, ValueFlags flags=ValueFlags::is_trusted)
: ArrayHolder(sv_arg, flags) {}
explicit ArrayOwner(const Value& v)
: ArrayHolder(v) {}
public:
ArrayOwner() {}
explicit ArrayOwner(Int n) { resize(n); }
ArrayOwner(const ArrayOwner& x)
: ArrayHolder(x.get(), std::true_type()) {}
ArrayOwner& operator= (const ArrayOwner& x)
{
set_copy(x.get());
return *this;
}
ArrayOwner(ArrayOwner&& x) noexcept
: ArrayHolder(x.sv)
{
x.sv=nullptr;
}
ArrayOwner& operator= (ArrayOwner&& x) noexcept
{
forget();
sv=x.sv;
x.sv=nullptr;
return *this;
}
~ArrayOwner() noexcept { forget(); }
using ArrayHolder::size;
using modified_container_impl<ArrayOwner>::operator[];
bool empty() const
{
return size()==0;
}
sequence get_container() const
{
return sequence(0, size());
}
typename Element::template Array_element_factory<false> get_operation()
{
return typename Element::template Array_element_factory<false>(this);
}
typename Element::template Array_element_factory<true> get_operation() const
{
return typename Element::template Array_element_factory<true>(this);
}
void clear() { resize(0); }
};
class OptionSet
: public HashHolder {
public:
OptionSet()
: HashHolder() { this->get_temp(); }
OptionSet(const Value& v)
: HashHolder(v.sv)
{
verify();
}
// create an option set on the fly, to be passed to a function or user method
template <typename FirstVal, typename... MoreArgs>
OptionSet(const AnyString& first_key, FirstVal&& first_val, MoreArgs&&... more_args)
: OptionSet()
{
store_values(first_key, std::forward<FirstVal>(first_val), std::forward<MoreArgs>(more_args)...);
}
public:
Value operator[] (const AnyString& key) const
{
return Value(fetch(key, false), ValueFlags::not_trusted | ValueFlags::allow_undef);
}
Value operator[] (const AnyString& key)
{
return Value(fetch(key, true), ValueFlags::not_trusted | ValueFlags::allow_undef | ValueFlags::allow_non_persistent);
}
protected:
void store_values() {}
template <typename FirstVal, typename... MoreArgs>
void store_values(const AnyString& first_key, FirstVal&& first_val, MoreArgs&&... more_args)
{
Value v(fetch(first_key, true), ValueFlags::allow_non_persistent | ValueFlags::allow_store_any_ref);
v.put(std::forward<FirstVal>(first_val));
store_values(std::forward<MoreArgs>(more_args)...);
}
};
class Hash : public HashHolder {
public:
Hash() {}
Hash(const Hash& x)
: HashHolder(x.get(), std::true_type()) {}
Hash& operator= (const Hash& x)
{
set_copy(x.get());
return *this;
}
~Hash() { forget(); }
Value operator[] (const AnyString& key) const
{
return Value(fetch(key, false), ValueFlags::not_trusted | ValueFlags::allow_undef);
}
Value operator[] (const AnyString& key)
{
return Value(fetch(key, true), ValueFlags::not_trusted | ValueFlags::allow_undef | ValueFlags::allow_non_persistent);
}
};
inline Value::NoAnchors Value::put_val(const Scalar& x, int) { set_copy(x); return NoAnchors(); }
inline Value::NoAnchors Value::put_val(const Array& x, int) { set_copy(x); return NoAnchors(); }
inline Value::NoAnchors Value::put_val(const Hash& x, int) { set_copy(x); return NoAnchors(); }
inline Value::NoAnchors Value::put_val(const ListReturn& x, int) { forget(); sv=nullptr; return NoAnchors(); }
template <typename Target>
class access {
public:
using type = Target;
using return_type = std::remove_const_t<type>;
static return_type get(const Value& v)
{
return v.operator return_type();
}
};
template <>
class access<void> {
public:
using type = void;
static const Value& get(const Value& v)
{
return v;
}
};
template <typename T>
class access<T()>
: public access<void> {};
template <>
class access<SV*> {
public:
using type = void;
static SV* get(const Value& v)
{
return v.get();
}
};
template <>
class access<OptionSet>
: public access<void> {};
template <typename Given, typename Target>
class access<Target(Given)> {
public:
using type = Given;
using given_type = std::remove_const_t<Given>;
using return_type = std::remove_const_t<Target>;
static return_type get(const Value& v)
{
return static_cast<return_type>(v.operator given_type());
}
};
// TODO: add a declaration for representative of HashMaps when CPlusPlus.pm learns to generate them for anonymous hash maps
template <typename Target>
using canned_may_be_missing = is_instance_of<Target, pm::Array>;
template <typename Given>
using ignore_constness = is_instance_of<std::remove_const_t<Given>, std::unique_ptr>;
template <typename Target, typename Given>
class access<Target(Canned<Given&>)> {
public:
using type = Given&;
static constexpr bool enforce_const = !ignore_constness<Given>::value &&
(std::is_const<Given>::value || !std::is_same<std::remove_const_t<Given>, Target>::value);
using return_type = std::conditional_t<enforce_const, std::add_const_t<Target>, Target>;
static return_type& get(const Value& v)
{
const Value::canned_data_t canned=Value::get_canned_data(v.sv);
constexpr bool maybe_missing = std::is_const<Given>::value && canned_may_be_missing<std::remove_const_t<Given>>::value;
if (maybe_missing && !canned.ti)
return parse(v, bool_constant<maybe_missing>());
assert(canned.value && *canned.ti == typeid(Given));
if (!ignore_constness<Given>::value && !std::is_const<return_type>::value && canned.read_only)
throw std::runtime_error("read-only object " + legible_typename<Given>() + " can't be bound to a non-const lvalue reference");
return v.convert_and_can<return_type>(*reinterpret_cast<Given*>(canned.value));
}
private:
static return_type& parse(const Value& v, std::true_type)
{
return v.parse_and_can<std::remove_const_t<Target>>();
}
static return_type& parse(const Value& v, std::false_type); // undefined
};
template <typename Target>
class access<TryCanned<Target>> {
public:
using type = Target&;
static type get(const Value& v)
{
const Value::canned_data_t canned=Value::get_canned_data(v.sv);
if (!canned.ti)
return v.parse_and_can<std::remove_const_t<Target>>();
if (*canned.ti == typeid(Target)) {
if (canned.read_only && !std::is_const<Target>::value)
throw std::runtime_error("read-only object " + legible_typename<Target>() + " can't be bound to a non-const lvalue reference");
return *reinterpret_cast<Target*>(canned.value);
}
if (!std::is_const<Target>::value) {
throw std::runtime_error("object " + legible_typename(*canned.ti) + " can't be bound to a non-const lvalue reference to " + legible_typename<Target>());
}
return v.convert_and_can<std::remove_const_t<Target>>(canned);
}
};
template <typename Target>
class access<Canned<Target&>>
: public access<Target(Canned<Target&>)> {};
template <typename Target>
class access<Canned<const Target&>>
: public access<Target(Canned<const Target&>)> {};
template <typename Target>
class access<Canned<Target>> {
public:
using type = Target;
static Target&& get(const Value& v)
{
const Value::canned_data_t canned=Value::get_canned_data(v.sv);
assert(canned.ti && canned.value && *canned.ti == typeid(Target) && !canned.read_only);
return std::move(*reinterpret_cast<Target*>(canned.value));
}
};
template <typename Target, typename Deleter>
class access<Canned<const std::unique_ptr<Target, Deleter>&>>
: public access<Target(Canned<const std::unique_ptr<Target, Deleter>&>)> {};
template <typename Target>
class access<Canned<Wary<Target>&>> {
public:
using type = Target&;
static Wary<Target>& get(const Value& v)
{
return wary(access<Canned<Target&>>::get(v));
}
};
template <typename Target>
class access<Canned<const Wary<Target>&>> {
public:
using type = const Target&;
static const Wary<Target>& get(const Value& v)
{
return wary(access<Canned<const Target&>>::get(v));
}
};
template <typename Target>
class access<Canned<Wary<Target>>> {
public:
using type = Target;
static Wary<Target>&& get(const Value& v)
{
return wary(access<Canned<Target>>::get(v));
}
};
template <typename Target>
class access<Enum<Target>> {
public:
using type = Target;
static Target get(const Value& v)
{
return static_cast<Target>(v.enum_value(sizeof(Target), true));
}
};
} }
// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
|