This file is indexed.

/usr/include/polymake/perl/wrappers.h is in libpolymake-dev-common 3.2r2-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
/* Copyright (c) 1997-2018
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.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.
--------------------------------------------------------------------------------
*/

#ifndef POLYMAKE_PERL_WRAPPERS_H
#define POLYMAKE_PERL_WRAPPERS_H

/* The following classes are designed to be used in the files automatically generated by polymake.
   If you use them in the manually written code, you are doing it on your own risk!

   The changes in syntax and contents must be concerted with the corresponding routines
   in the package Polymake::Core::CPlusPlus
*/

namespace pm { namespace perl {

template <typename Given, typename Target>
class access {
public:
   using type = Target;
   using return_type = Target;
   using value_type = typename attrib<type>::minus_const;

   static return_type get(const Value& v)
   {
      return static_cast<Target>(static_cast<Given>(v));
   }
};

template <typename Target>
class access<Target, Target> {
public:
   using type = Target;
   using return_type = Target;
   using value_type = typename attrib<type>::minus_const;

   static return_type get(const Value& v)
   {
      return static_cast<Target>(v);
   }
};

template <>
class access<std::string, std::string> {
public:
   using type = std::string;
   using return_type = std::string;
   using value_type = std::string;

   static return_type get(const Value& v)
   {
      std::string x;
      v.retrieve(x);
      return x;
   }
};

template <>
class access<const std::string, const std::string>
   : public access<std::string, std::string> {
public:
   using type = const std::string;
};

template <typename Given>
class access<Given, std::string>
   : public access<std::string, std::string> {};

template <typename Given>
class access<Given, const std::string>
   : public access<const std::string, const std::string> {};

template <typename Target>
struct canned_may_be_missing : std::false_type {};

template <typename E, typename... SharedParams>
struct canned_may_be_missing< pm::Array<E, SharedParams...> > : std::true_type {};

// TODO: add a declaration for representative of HashMaps when CPlusPlus.pm learns to generate them for anonymous hash maps

template <typename Given, typename Target, bool try_conv>
class access_canned<Given, Target, try_conv, true> {
public:
   using type = Target;
   using return_type = typename inherit_const<Target&, Given>::type;
   using value_type = typename attrib<type>::minus_const;
   using given_value_type = typename attrib<Given>::minus_const;

   static return_type get(const Value& v)
   {
      const Value::canned_data_t canned=Value::get_canned_data(v.sv);
      const bool maybe_missing=try_conv || canned_may_be_missing<typename attrib<Given>::minus_const>::value;

      if (!maybe_missing || canned.second) {
         if (std::is_same<value_type, given_value_type>::value) {
            if (!try_conv || *canned.first == typeid(value_type))
               return *reinterpret_cast<type*>(canned.second);
         } else {
            if (!try_conv || *canned.first == typeid(given_value_type))
               return convert_input(v, canned, std::is_same<value_type, given_value_type>());
         }

         if (wrapper_type conversion=type_cache<value_type>::get_conversion_constructor(v.sv)) {
            // It is a wrapper for new<T0>(T1), it expects the prototype at stack[0] and the argument at stack[1]
            SV* mini_stack[2]={ NULL, v.sv };
            if (SV* ret=conversion(mini_stack))
               return *reinterpret_cast<value_type*>(Value::get_canned_data(ret).second);
            else
               throw exception();
         }
      }

      return parse_input(v, bool_constant<maybe_missing>());
   }

private:
   static return_type parse_input(const Value& v, std::true_type)
   {
      Value temp_can;
      value_type* value=new(temp_can.allocate_canned(type_cache<value_type>::get_descr(0))) value_type;
      v >> *value;
      const_cast<Value&>(v).sv=temp_can.get_constructed_canned();
      return *value;
   }

   static return_type convert_input(const Value& v, const Value::canned_data_t& canned, std::false_type)
   {
      Value temp_can;
      value_type* value=new(temp_can.allocate_canned(type_cache<value_type>::get_descr(0))) value_type(*reinterpret_cast<Given*>(canned.second));
      const_cast<Value&>(v).sv=temp_can.get_constructed_canned();
      return *value;
   }

   static return_type parse_input(const Value& v, std::false_type)
   {
      // should never happen
      throw std::runtime_error("internal error: wrong use of access_canned::parse_input");
   }

   static return_type convert_input(const Value& v, const Value::canned_data_t& canned, std::true_type)
   {
      // should never happen
      throw std::runtime_error("internal error: wrong use of access_canned::convert_input");
   }
};

template <typename Target, bool try_conv>
class access_canned<Target, Target, try_conv, false> {
public:
   using type = typename inherit_const<unwary_t<typename attrib<Target>::minus_const>, Target>::type;
   using return_type = Target&;
   using value_type = typename attrib<type>::minus_const;

   static return_type get(const Value& v)
   {
      return wary(access_canned<type, type, try_conv, true>::get(v));
   }
};

template <typename Given, typename Target, bool try_conv>
class access_canned<Given, Target, try_conv, false> {
public:
   using type = typename inherit_const<Target, Given>::type;
   using return_type = typename inherit_const<Wary<Target>, Given>::type&;
   using given_value_type = unwary_t<typename attrib<Given>::minus_const>;
   using given_type = typename inherit_const<given_value_type, Given>::type;
   using value_type = typename attrib<type>::minus_const;

   static return_type get(const Value& v)
   {
      return wary(access_canned<given_type, Target, try_conv, true>::get(v));
   }
};

template <typename Given, typename Target>
class access<TryCanned<Given>, Target> : public access_canned<Given, Target, true> {};

template <typename Given, typename Target>
class access<Canned<Given>, Target> : public access_canned<Given, Target, false> {};

template <typename Given>
class access<TryCanned<Given>, TryCanned<Given> > : public access_canned<Given, Given, true> {};

template <typename Given>
class access<Canned<Given>, Canned<Given> > : public access_canned<Given, Given, false> {};

template <typename TData, typename TDeleter>
class access<Canned<const std::unique_ptr<TData, TDeleter>>, Canned<const std::unique_ptr<TData, TDeleter>>>
{
public:
   using type = const std::unique_ptr<TData, TDeleter>;
   using value_type = typename attrib<type>::minus_const;
   using return_type = TData&;

   static return_type get(const Value& v)
   {
      return *access_canned<type, type, false>::get(v);
   }
};

template <typename Target>
class access< Enum<Target>, Enum<Target> > {
public:
   using type = const Target;
   using return_type = Target;
   using value_type = Target;

   static return_type get(const Value& v)
   {
      return Target(v.enum_value());
   }
};

class RegistratorQueue {
public:
   enum Kind {
      // cf. CPlusPlus.pm
      function, embedded_rule, duplicate_class_instance
   };
   RegistratorQueue(const AnyString& name, Kind kind);

protected:
   SV* queue;
};

class RegularFunctionBase
   : public RegistratorQueue {
public:
   static const Kind kind = embedded_rule;
protected:
   void register_it(const AnyString& file, int line, const char* text,
                    indirect_wrapper_type get_flags_ptr, SV* arg_types,
                    void* func_ptr, const char* func_ptr_type) const;
};

class RegularFunction
   : public RegularFunctionBase {
   RegularFunction() = delete;
public:
   template <typename Fptr>
   void add__me(Fptr* fptr, const AnyString& file, int line, const char* text) const
   {
      register_it(file, line, text,
                  &TypeListUtils<Fptr>::get_flags, TypeListUtils<Fptr>::get_type_names(),
                  (void*)fptr, typeid(type2type<Fptr>).name());
   }
};

class FunctionTemplateBase
   : public RegistratorQueue {
   FunctionTemplateBase() = delete;
public:
   static const Kind kind = function;
protected:
   void register_it(wrapper_type wrapper, const AnyString& sig, const AnyString& file, int line,
                    SV* arg_types, SV* cross_apps) const;

   void register_it(wrapper_type wrapper, const AnyString& sig, const AnyString& file, int line,
                    SV* arg_types, int=0) const
   {
      register_it(wrapper, sig, file, line, arg_types, nullptr);
   }
};

template <typename Wrapper>
class FunctionTemplate
   : public FunctionTemplateBase {
   FunctionTemplate() = delete;
public:
   template <typename TAppList>
   void register_it(const AnyString& sig, const AnyString& file, int line, TAppList cross_apps) const
   {
      FunctionTemplateBase::register_it(&Wrapper::call, sig, file, line,
                                        TypeListUtils<typename Wrapper::arg_list>::get_type_names(), cross_apps);
   }
};

template <typename T, bool enabled=!(std::is_trivially_destructible<T>::value || is_masquerade<T>::value)>
class Destroy {
   static void impl(char* p)
   {
      T* obj = reinterpret_cast<T*>(p);
      obj->~T();
   }
public:
   static destructor_type func() { return &impl; }
};

template <typename T>
class Destroy<T, false> {
public:
   static destructor_type func() { return nullptr; }
};

template <typename T, bool enabled=(std::is_same<T, typename object_traits<T>::persistent_type>::value && is_mutable<T>::value && !is_masquerade<T>::value)>
class Copy {
   static void impl(void* place, const char* p)
   {
      const T* src = reinterpret_cast<const T*>(p);
      new(place) T(*src);
   }
public:
   static copy_constructor_type func() { return &impl; }
};

template <typename T>
class Copy<T, false> {
public:
   static copy_constructor_type func() { return nullptr; }
};


template <typename T, typename enabled=void>
class Assign {
public:
   static assignment_type func() { return nullptr; }
};

template <typename T>
class Assign<T, typename std::enable_if<is_mutable<type_behind_t<T>>::value &&
                                        is_readable<type_behind_t<T>>::value &&
                                        !std::is_same<T, Value>::value>::type> {
protected:
   template <typename, typename> friend class Assign;

   static void assign(T* dst, SV* sv, value_flags flags, std::true_type)
   {
      Value src(sv, flags);
      src >> *dst;
   }

   static void assign(T* dst, SV* sv, value_flags flags, std::false_type)
   {
      type_behind_t<T> x;
      Assign<type_behind_t<T>>::assign(&x, sv, flags, std::true_type());
      *dst = std::move(x);
   }

   static void impl(char* p, SV* sv, value_flags flags)
   {
      T* dst = reinterpret_cast<T*>(p);
      assign(dst, sv, flags, std::is_same<typename object_traits<T>::proxy_for, void>());
   }
public:
   static assignment_type func() { return &impl; }
};


class Unprintable {
protected:
   static SV* impl(const char*);
public:
   static conv_to_string_type func() { return &impl; }
};

template <typename T, typename enabled=void>
class ToString : public Unprintable {};

template <typename T>
class ToString<T, typename std::enable_if<is_printable<type_behind_t<T>>::value>::type> {
protected:
   static SV* to_string(const type_behind_t<T>& src)
   {
      Value ret;
      ostream my_stream(ret);
      PlainPrinter<> printer(my_stream);
      printer << src;
      return ret.get_temp();
   }
   static SV* impl(const char* p)
   {
      const T* src = reinterpret_cast<const T*>(p);
      return to_string(*src);
   }
public:
   static conv_to_string_type func() { return &impl; }
};


template <typename T, typename enabled=void>
class Serializable {
public:
   static constexpr class_kind flag_value() { return class_kind_null; }
   static conv_to_serialized_type conv() { return nullptr; }
   static provide_type provide() { return nullptr; }
   static provide_type provide_descr() { return nullptr; }
};

template <typename T>
class Serializable<T, typename std::enable_if<has_serialized<type_behind_t<T>>::value>::type> {
protected:
   static SV* store_serialized(const type_behind_t<T>& src, SV* holder)
   {
      Value ret(value_allow_non_persistent | value_allow_store_ref | value_read_only);
      ret.put(serialize(src), 0, holder);
      return ret.get_temp();
   }

   static SV* impl(const char* p, SV* holder)
   {
      const T* src = reinterpret_cast<const T*>(p);
      return store_serialized(*src, holder);
   }
public:
   typedef pure_type_t<decltype(serialize(std::declval<const type_behind_t<T>&>()))> serialized_t;

   static constexpr class_kind flag_value()
   {
      return check_container_feature<serialized_t, sparse>::value
             ? class_is_serializable | class_is_sparse_serialized
             : class_is_serializable;
   }

   static conv_to_serialized_type conv() { return &impl; }
   static provide_type provide() { return &type_cache<serialized_t>::provide; }
   static provide_type provide_descr() { return &type_cache<serialized_t>::provide_descr; }
};

// This one is used in overloaded operators for sparse proxies of primitive types.
// It should just retrieve the data element.
template <typename TProxy>
class Serializable<TProxy, typename std::enable_if<!std::is_same<TProxy, type_behind_t<TProxy>>::value &&
                                                   std::is_arithmetic<type_behind_t<TProxy>>::value>::type>
   : public Serializable<type_behind_t<TProxy>> {
protected:
   static SV* impl(const char* p, SV*)
   {
      const TProxy* src = reinterpret_cast<const TProxy*>(p);
      Value ret;
      ret << static_cast<type_behind_t<TProxy>>(*src);
      return ret.get_temp();
   }

public:
   static conv_to_serialized_type conv() { return &impl; }
};

class ClassRegistratorBase
   : public RegistratorQueue {
   ClassRegistratorBase() = delete;
public:
   static const Kind kind = duplicate_class_instance;
protected:
   static
   SV* register_class(const AnyString& name, const AnyString& file, int line,
                      SV* someref,
                      const char* typeid_name,
                      bool is_mutable, class_kind kind,
                      SV* vtbl_sv);

   static
   SV* create_builtin_vtbl(
      const std::type_info& type,
      size_t obj_size,
      int primitive_lvalue,
      copy_constructor_type copy_constructor,
      assignment_type assignment,
      destructor_type destructor
   );

   static
   SV* create_scalar_vtbl(
      const std::type_info& type,
      size_t obj_size,
      copy_constructor_type copy_constructor,
      assignment_type assignment,
      destructor_type destructor,
      conv_to_string_type to_string,
      conv_to_serialized_type to_serialized,
      provide_type provide_serialized_type,
      provide_type provide_serialized_descr,
      conv_to_int_type to_int,
      conv_to_float_type to_float
   );

   static
   SV* create_iterator_vtbl(
      const std::type_info& type,
      size_t obj_size,
      copy_constructor_type copy_constructor,
      destructor_type destructor,
      iterator_deref_type deref,
      iterator_incr_type incr,
      conv_to_int_type at_end,
      conv_to_int_type index
   );

   static
   SV* create_opaque_vtbl(
      const std::type_info& type,
      size_t obj_size,
      copy_constructor_type copy_constructor,
      assignment_type assignment,
      destructor_type destructor,
      conv_to_string_type to_string,
      conv_to_serialized_type to_serialized,
      provide_type provide_serialized_type,
      provide_type provide_serialized_descr
   );

   static
   SV* create_container_vtbl(
      const std::type_info& type,
      size_t obj_size, int total_dimension, int own_dimension,
      copy_constructor_type copy_constructor,
      assignment_type assignment,
      destructor_type destructor,
      conv_to_string_type to_string,
      conv_to_serialized_type to_serialized,
      provide_type provide_serialized_type,
      provide_type provide_serialized_descr,
      conv_to_int_type size,
      container_resize_type resize,
      container_store_type store_at_ref,
      provide_type provide_key_type,
      provide_type provide_key_descr,
      provide_type provide_value_type,
      provide_type provide_value_descr
   );

   static
   void fill_iterator_access_vtbl(
      SV* vtbl, int i,
      size_t it_size, size_t cit_size,
      destructor_type it_destructor,
      destructor_type cit_destructor,
      container_begin_type begin,
      container_begin_type cbegin,
      container_access_type deref,
      container_access_type cderef
   );

   static
   void fill_random_access_vtbl(
      SV* vtbl,
      container_access_type random,
      container_access_type crandom
   );

   static
   SV* create_composite_vtbl(
      const std::type_info& type,
      size_t obj_size, int obj_dimension,
      copy_constructor_type copy_constructor,
      assignment_type assignment,
      destructor_type destructor,
      conv_to_string_type to_string,
      conv_to_serialized_type to_serialized,
      provide_type provide_serialized_type,
      provide_type provide_serialized_descr,
      int n_members,
      provide_type provide_member_types,
      provide_type provide_member_descrs,
      provide_type provide_member_names,
      void (*fill)(composite_access_vtbl*)
   );
};

template <typename T>
class Builtin
   : public ClassRegistratorBase {
   Builtin() = delete;
public:
   void add__me(const AnyString& name, const AnyString& file, int line) const
   {
      register_class(
         name, file, line,
         queue,
         typeid(T).name(),
         false, class_is_scalar,
         create_builtin_vtbl(
            typeid(T), sizeof(T), mlist_contains<primitive_lvalues, T>::value,
            Copy<T>::func(),
            Assign<T>::func(),
            Destroy<T>::func()
         )
      );
   }
};

template <typename T, typename Model=typename object_traits<T>::model>
class ClassRegistrator;

template <>
class ClassRegistrator<Scalar,is_opaque> {};
template <>
class ClassRegistrator<Array,is_container> {};

template <typename T>
class ClassRegistrator<T, is_scalar>
   : public ClassRegistratorBase {
protected:
   typedef typename object_traits<T>::persistent_type persistent_type;

   template <typename Target, typename=void>
   struct conv;

   template <typename Target>
   struct conv<Target,
               typename std::enable_if<std::is_constructible<Target, T>::value>::type>
   {
      static Target func(const char* p)
      {
         const T* obj = reinterpret_cast<const T*>(p);
         return static_cast<Target>(*obj);
      }
   };

   template <typename Target>
   struct conv<Target,
               typename std::enable_if<!std::is_constructible<Target, T>::value &&
                                       std::is_constructible<Target, persistent_type>::value>::type>
   {
      static Target func(const char* p)
      {
         const T* obj = reinterpret_cast<const T*>(p);
         return static_cast<Target>(static_cast<const persistent_type&>(*obj));
      }
   };

   template <typename Target>
   struct conv<Target,
               typename std::enable_if<!std::is_constructible<Target, T>::value &&
                                       !std::is_constructible<Target, persistent_type>::value>::type>
   {
      static Target func(const char* p)
      {
         throw std::runtime_error("can't convert " + legible_typename<T>() + " to " + legible_typename<Target>());
      }
   };

public:
   static SV* register_it(const AnyString& name, SV* someref, const AnyString& file=AnyString(), int line=0)
   {
      return register_class(
         name, file, line, someref,
         typeid(T).name(),
         is_mutable<T>::value,
         class_is_scalar | Serializable<T>::flag_value(),
         create_scalar_vtbl(
            typeid(T), sizeof(T),
            Copy<T>::func(),
            Assign<T>::func(),
            Destroy<T>::func(),
            ToString<T>::func(),
            Serializable<T>::conv(),
            Serializable<T>::provide(),
            Serializable<T>::provide_descr(),
            &conv<int>::func,
            &conv<double>::func
         )
      );
   }
};

template <typename T, bool is_iterator=check_iterator_feature<T, end_sensitive>::value>
class OpaqueClassRegistrator
   : public ClassRegistratorBase {
public:
   static SV* register_it(const AnyString& name, SV* someref, const AnyString& file=AnyString(), int line=0)
   {
      return register_class(
         name, file, line, someref,
         typeid(T).name(),
         is_mutable<T>::value,
         class_is_opaque | Serializable<T>::flag_value(),
         create_opaque_vtbl(
            typeid(T), sizeof(T),
            Copy<T>::func(),
            Assign<T>::func(),
            Destroy<T>::func(),
            ToString<T>::func(),
            Serializable<T>::conv(),
            Serializable<T>::provide(),
            Serializable<T>::provide_descr()
         )
      );
   }
};

template <typename TData, typename TDeleter>
class OpaqueClassRegistrator<std::unique_ptr<TData, TDeleter>, false>
   : public ClassRegistratorBase {
public:
   using Tptr = std::unique_ptr<TData, TDeleter>;

   static SV* register_it(const AnyString& name, SV* someref, const AnyString& file=AnyString(), int line=0)
   {
      return register_class(
         name, file, line, someref,
         typeid(Tptr).name(),
         false,
         class_is_opaque | class_kind_null,
         create_opaque_vtbl(
            typeid(Tptr), sizeof(Tptr),
            nullptr,
            nullptr,
            Destroy<Tptr>::func(),
            Unprintable::func(),
            nullptr,
            nullptr,
            nullptr
         )
      );
   }
};

template <typename T>
class OpaqueClassRegistrator<T, true>
   : public ClassRegistratorBase {
protected:
   static const bool read_only=attrib<typename iterator_traits<T>::reference>::is_const;

   static SV* deref(const char* p)
   {
      const T* it = reinterpret_cast<const T*>(p);
      Value ret((read_only ? value_read_only : value_mutable) |
                value_expect_lval | value_allow_non_persistent | value_allow_store_ref);
      ret.put_lvalue(**it, 0, nullptr, (nothing*)nullptr);
      return ret.get_temp();
   }

   static void incr(char* p)
   {
      T* it = reinterpret_cast<T*>(p);
      ++(*it);
   }

   static int at_end(const char* p)
   {
      const T* it = reinterpret_cast<const T*>(p);
      return it->at_end();
   }

   static int index_impl(const char* p)
   {
      const T* it = reinterpret_cast<const T*>(p);
      return it->index();
   }

   static conv_to_int_type index(std::false_type) { return nullptr; }
   static conv_to_int_type index(std::true_type) { return &index_impl; }
   static conv_to_int_type index() { return index(bool_constant<check_iterator_feature<T, indexed>::value>()); }

public:
   static SV* register_it(const AnyString& name, SV* someref, const AnyString& file=AnyString(), int line=0)
   {
      return register_class(
         name, file, line, someref,
         typeid(T).name(),
         true,
         class_is_opaque,
         create_iterator_vtbl(
            typeid(T), sizeof(T),
            Copy<T>::func(),
            Destroy<T>::func(),
            &deref,
            &incr,
            &at_end,
            index()
         )
      );
   }
};

template <typename T>
class ClassRegistrator<T, is_opaque>
   : public OpaqueClassRegistrator<T> {};

template <typename Mode, bool TEnableSparse>
struct transform_input_mode {
   typedef Mode type;
};

template <int TResizeable>
struct transform_input_mode<io_test::as_sparse<TResizeable>, false> {
   typedef io_test::as_array<TResizeable, true> type;
};

template <typename T, bool TEnable=true, bool TEnableSparse=true, bool TMutable=TEnable && !object_traits<T>::is_always_const>
struct input_mode
   : transform_input_mode<typename io_test::input_mode<T, false>::type, TEnableSparse> {};

template <typename T, bool TEnable, bool TEnableSparse>
struct input_mode<T, TEnable, TEnableSparse, false> {
   typedef std::false_type type;
};

template <typename T, int TDim=object_traits<T>::dimension>
struct container_helper;

template <typename T>
struct container_helper<T, 1> {
   typedef T type;
   static type& streamline(T& x) { return x; }
   static const type& streamline(const T& x) { return x; }
};

template <typename T>
struct container_helper<T, 2> {
   typedef Rows<T> type;
   static type& streamline(T& x) { return rows(x); }
   static const type& streamline(const T& x) { return rows(x); }
};

template <typename T, typename Category=typename container_traits<typename container_helper<T>::type>::category,
          bool _obscure=obscure_type<T>::value>
class ContainerClassRegistrator
   : public ClassRegistratorBase {
public:
   typedef container_helper<T> helper;
   typedef typename helper::type Obj;
   static constexpr bool
        is_associative=is_assoc_container<Obj>::value,
             is_sparse=check_container_feature<Obj, sparse>::value,
      is_sparse_native=check_container_feature<T, sparse>::value,
              like_set=is_among<typename object_traits<Obj>::generic_tag, is_set, is_unordered_set>::value;
   typedef typename std::conditional<is_associative, end_sensitive, void>::type iterator_feature;
   typedef typename ensure_features<Obj, iterator_feature>::iterator iterator;
   typedef typename ensure_features<Obj, iterator_feature>::const_iterator const_iterator;

   static const bool allow_non_const_access= !object_traits<T>::is_always_const &&
                                             !std::is_same<iterator, const_iterator>::value &&
                                             !attrib<typename iterator_traits<iterator>::reference>::is_const &&
                                             !object_traits<typename iterator_traits<iterator>::value_type>::is_always_const;

   typedef bool_constant<allow_non_const_access> non_const_access;

   static const int element_dim=object_traits<typename Obj::value_type>::total_dimension;

protected:
   static int size_impl(const char* p)
   {
      const T* obj = reinterpret_cast<const T*>(p);
      return helper::streamline(*obj).size();
   }

   static int dim(const char* p)
   {
      const T* obj = reinterpret_cast<const T*>(p);
      return helper::streamline(*obj).dim();
   }

   static void resize_impl(char* p, int n)
   {
      T* obj = reinterpret_cast<T*>(p);
      helper::streamline(*obj).resize(n);
   }

   static void fixed_size(char* p, int n)
   {
      T* obj = reinterpret_cast<T*>(p);
      if (get_dim(helper::streamline(*obj)) != n)
         throw std::runtime_error("size mismatch");
   }

   static void clear_by_resize(char* p, int)
   {
      T* obj = reinterpret_cast<T*>(p);
      helper::streamline(*obj).clear();
   }

   template <typename E>
   static void check_insertion(const Obj&, const E&, std::false_type) {}

   static void check_insertion(const Obj& obj, int x, std::true_type)
   {
      if (x<0 || x>=obj.dim())
         throw std::runtime_error("element out of range");
   }

   static void push_back(char* p_obj, char* p_it, int, SV* src)
   {
      T* obj = reinterpret_cast<T*>(p_obj);
      iterator* it = reinterpret_cast<iterator*>(p_it);
      typename Obj::value_type x;
      Value v(src);
      v >> x;
      check_insertion(helper::streamline(*obj), x,
                      bool_constant<check_container_feature<Obj, sparse_compatible>::value>());
      helper::streamline(*obj).insert(*it, x);
   }

   static void insert(char* p_obj, char*, int, SV* src)
   {
      T* obj = reinterpret_cast<T*>(p_obj);
      typedef typename item4insertion<typename Obj::value_type>::type item_type;
      item_type x=item_type();
      Value v(src);
      v >> x;
      check_insertion(helper::streamline(*obj), x,
                      bool_constant<check_container_feature<Obj, sparse_compatible>::value>());
      helper::streamline(*obj).insert(x);
   }

   static void store_dense(char*, char* p_it, int, SV* src)
   {
      iterator* it = reinterpret_cast<iterator*>(p_it);
      Value v(src, value_not_trusted);
      v >> non_const(**it);
      ++(*it);
   }

   static void store_sparse(char* p_obj, char* p_it, int index, SV* src)
   {
      T* obj = reinterpret_cast<T*>(p_obj);
      iterator* it = reinterpret_cast<iterator*>(p_it);
      Value v(src, value_not_trusted);
      typename Obj::value_type x;
      v >> x;
      if (!is_zero(x)) {
         if (!it->at_end() && it->index()==index) {
            **it=x; ++(*it);
         } else {
            obj->insert(*it, index, x);
         }
      } else {
         if (!it->at_end() && it->index()==index)
            obj->erase((*it)++);
      }
   }

   template <typename Iterator, bool non_const>
   struct do_it {
      typedef typename assign_const<T, !non_const>::type* ObjPtr;

      static void begin(void* it_place, char* p)
      {
         ObjPtr obj = reinterpret_cast<ObjPtr>(p);
         new(it_place) Iterator(ensure(helper::streamline(*obj), (iterator_feature*)0).begin());
      }

      static void rbegin(void* it_place, char* p)
      {
         ObjPtr obj = reinterpret_cast<ObjPtr>(p);
         new(it_place) Iterator(ensure(helper::streamline(*obj), (iterator_feature*)0).rbegin());
      }

      static void deref(char*, char* p_it, int, SV* dst, SV* container_sv)
      {
         Iterator* it = reinterpret_cast<Iterator*>(p_it);
         Value pv(dst, (non_const ? value_mutable : value_read_only) | value_expect_lval | value_allow_non_persistent | value_allow_store_ref);
         pv.put_lvalue(**it, 0, nullptr, (nothing*)nullptr, container_sv);
         ++(*it);
      }

      static void deref_pair(char*, char* p_it, int i, SV* dst, SV* container_sv)
      {
         Iterator* it = reinterpret_cast<Iterator*>(p_it);
         if (i<=0) {
            // i==-1: FIRSTKEY;  i==0: NEXTKEY
            if (i==0) ++(*it);
            if (!it->at_end()) {
               Value pv(dst, value_read_only | value_allow_non_persistent | value_allow_store_ref);
               pv.put((*it)->first, 0, container_sv);
            }
         } else {
            // i==1: fetch value
            Value pv(dst, (non_const ? value_mutable : value_read_only) | value_allow_non_persistent | value_allow_store_ref);
            pv.put((*it)->second, 0, container_sv);
         }
      }
   };

   template <typename Iterator, bool TDim=element_dim>
   struct do_sparse {
      static void deref(char* p_obj, char* p_it, int index, SV* dst, SV* container_sv)
      {
         Iterator* it = reinterpret_cast<Iterator*>(p_it);
         if (it->at_end() || index < it->index()) {
            Value pv(dst);
            pv.put(undefined(), 0);
         } else {
            do_it<Iterator, true>::deref(p_obj, p_it, index, dst, container_sv);
         }
      }
   };

   template <typename Iterator>
   struct do_sparse<Iterator, 0> {
      static void deref(char* p_obj, char* p_it, int index, SV* dst, SV* container_sv)
      {
         T* obj = reinterpret_cast<T*>(p_obj);
         Iterator* it = reinterpret_cast<Iterator*>(p_it);
         Value pv(dst, value_expect_lval | value_allow_non_persistent);
         sparse_elem_proxy< sparse_proxy_it_base<Obj, Iterator>, typename Obj::value_type, typename Obj::reference::parameters >
            x(sparse_proxy_it_base<Obj, Iterator>(*obj, *it, index));
         if (x.exists()) ++(*it);
         pv.put(std::move(x), 0, container_sv);
      }
   };

   template <typename Iterator, bool TDim=element_dim>
   struct do_const_sparse {
      static void deref(char* p_obj, char* p_it, int index, SV* dst, SV* container_sv)
      {
         Iterator* it = reinterpret_cast<Iterator*>(p_it);
         if (it->at_end() || index < it->index()) {
            Value pv(dst);
            pv.put(undefined(), 0);
         } else {
            do_it<Iterator, false>::deref(p_obj, p_it, index, dst, container_sv);
         }
      }
   };

   template <typename Iterator>
   struct do_const_sparse<Iterator, 0> {
      static void deref(char*, char* p_it, int index, SV* dst, SV* container_sv)
      {
         Iterator* it = reinterpret_cast<Iterator*>(p_it);
         Value pv(dst, value_read_only | value_expect_lval | value_allow_non_persistent | value_allow_store_ref);
         if (!it->at_end() && index==it->index()) {
            pv.put_lvalue(**it, 0, nullptr, (nothing*)nullptr, container_sv);
            ++(*it);
         } else {
            pv.put(zero_value<typename Obj::value_type>(), 0);
         }
      }
   };

   static conv_to_int_type size(std::false_type) { return &size_impl; }
   static conv_to_int_type size(std::true_type)  { return &dim; }

   static container_resize_type resize(io_test::as_list<Obj>)
   {
      return &clear_by_resize;
   }
   static container_resize_type resize(io_test::as_set)
   {
      return &clear_by_resize;
   }
   template <bool allow_sparse>
   static container_resize_type resize(io_test::as_array<1, allow_sparse>)
   {
      return &resize_impl;
   }
   template <bool allow_sparse>
   static container_resize_type resize(io_test::as_array<0, allow_sparse>)
   {
      return &fixed_size;
   }
   static container_resize_type resize(io_test::as_sparse<-1>) { return nullptr; }
   static container_resize_type resize(std::false_type) { return nullptr; }

   static container_store_type store_at_ref(io_test::as_list<Obj>)
   {
      return &push_back;
   }
   static container_store_type store_at_ref(io_test::as_set)
   {
      return &insert;
   }
   template <int resizeable, bool allow_sparse>
   static container_store_type store_at_ref(io_test::as_array<resizeable, allow_sparse>)
   {
      return &store_dense;
   }
   template <int resizeable>
   static container_store_type store_at_ref(io_test::as_sparse<resizeable>)
   {
      return &store_sparse;
   }
   static container_store_type store_at_ref(std::false_type) { return nullptr; }

   static destructor_type it_destructor(std::true_type) { return Destroy<iterator>::func(); }
   static destructor_type it_destructor(std::false_type) { return Destroy<const_iterator>::func(); }

   static container_begin_type begin(std::true_type)
   {
      return &do_it<iterator, true>::begin;
   }
   static container_begin_type begin(std::false_type)
   {
      return &do_it<const_iterator, false>::begin;
   }

   static container_access_type deref(std::false_type, std::false_type, std::true_type)
   {
      return &do_it<iterator, true>::deref;
   }
   static container_access_type deref(std::false_type, std::false_type, std::false_type)
   {
      return &do_it<const_iterator, false>::deref;
   }
   static container_access_type deref(std::true_type, std::false_type, std::true_type)
   {
      return &do_it<iterator, true>::deref_pair;
   }
   static container_access_type deref(std::true_type, std::false_type, std::false_type)
   {
      return &do_it<const_iterator, false>::deref_pair;
   }
   static container_access_type deref(std::false_type, std::true_type, std::true_type)
   {
      return &do_sparse<iterator>::deref;
   }
   static container_access_type deref(std::false_type, std::true_type, std::false_type)
   {
      return &do_const_sparse<const_iterator>::deref;
   }

   static provide_type provide_key_type(std::true_type)
   {
      return &type_cache<typename T::key_type>::provide;
   }
   static provide_type provide_key_type(std::false_type)
   {
      return &type_cache<typename object_traits<typename T::value_type>::persistent_type>::provide;
   }
   static provide_type provide_value_type(std::true_type)
   {
      return &type_cache<typename T::mapped_type>::provide;
   }
   static provide_type provide_value_type(std::false_type)
   {
      return &type_cache<typename object_traits<typename Obj::value_type>::persistent_type>::provide;
   }

   static provide_type provide_key_descr(std::true_type)
   {
      return &type_cache<typename T::key_type>::provide_descr;
   }
   static provide_type provide_key_descr(std::false_type)
   {
      return &type_cache<typename object_traits<typename T::value_type>::persistent_type>::provide_descr;
   }
   static provide_type provide_value_descr(std::true_type)
   {
      return &type_cache<typename T::mapped_type>::provide_descr;
   }
   static provide_type provide_value_descr(std::false_type)
   {
      return &type_cache<typename object_traits<typename Obj::value_type>::persistent_type>::provide_descr;
   }

   static SV* create_vtbl()
   {
      SV* vtbl=create_container_vtbl(
         typeid(T), sizeof(T),
         object_traits<T>::total_dimension, object_traits<T>::dimension,
         Copy<T>::func(),
         Assign<T>::func(),
         Destroy<T>::func(),
         ToString<T>::func(),
         Serializable<T>::conv(),
         Serializable<T>::provide(),
         Serializable<T>::provide_descr(),
         size(bool_constant<is_sparse>()),
         resize(typename input_mode<Obj>::type()),
         store_at_ref(typename input_mode<Obj, !is_associative, element_dim==0>::type()),
         provide_key_type(bool_constant<is_associative>()),
         provide_key_descr(bool_constant<is_associative>()),
         provide_value_type(bool_constant<is_associative>()),
         provide_value_descr(bool_constant<is_associative>())
      );
      fill_iterator_access_vtbl(
         vtbl, 0,
         sizeof(iterator), sizeof(const_iterator),
         it_destructor(non_const_access()),
         it_destructor(std::false_type()),
         begin(non_const_access()),
         begin(std::false_type()),
         deref(bool_constant<is_associative>(), bool_constant<is_sparse>(), non_const_access()),
         deref(bool_constant<is_associative>(), bool_constant<is_sparse>(), std::false_type())
      );
      return vtbl;
   }

   static SV* register_me(const AnyString& name, const AnyString& file, int line, SV* someref, SV* vtbl)
   {
      return register_class(
         name, file, line, someref,
         typeid(T).name(),
         is_mutable<T>::value,
         class_is_container | Serializable<T>::flag_value() |
            (is_sparse && !std::is_same<T, Obj>::value ? class_is_sparse_serialized : class_kind_null) |
            (is_associative   ? class_is_assoc_container :
             is_sparse_native ? class_is_sparse_container :
             like_set         ? class_is_set : class_kind_null),
         vtbl);
   }

public:
   static SV* register_it(const AnyString& name, SV* someref, const AnyString& file=AnyString(), int line=0)
   {
      return register_me(name, file, line, someref, create_vtbl());
   }
};

template <typename T>
class ContainerClassRegistrator<T, bidirectional_iterator_tag, false>
   : public ContainerClassRegistrator<T, forward_iterator_tag, false> {
   typedef ContainerClassRegistrator<T, forward_iterator_tag, false> super;
public:
   typedef typename super::Obj::reverse_iterator reverse_iterator;
   typedef typename super::Obj::const_reverse_iterator const_reverse_iterator;
protected:
   static container_begin_type rbegin(std::true_type)
   {
      return &super::template do_it<reverse_iterator, true>::rbegin;
   }
   static container_begin_type rbegin(std::false_type)
   {
      return &super::template do_it<const_reverse_iterator, false>::rbegin;
   }

   static container_access_type rderef(std::false_type, std::true_type)
   {
      return &super::template do_it<reverse_iterator, true>::deref;
   }
   static container_access_type rderef(std::false_type, std::false_type)
   {
      return &super::template do_it<const_reverse_iterator, false>::deref;
   }
   static container_access_type rderef(std::true_type, std::true_type)
   {
      return &super::template do_sparse<reverse_iterator>::deref;
   }
   static container_access_type rderef(std::true_type, std::false_type)
   {
      return &super::template do_const_sparse<const_reverse_iterator>::deref;
   }

   static destructor_type rit_destructor(std::true_type) { return Destroy<reverse_iterator>::func(); }
   static destructor_type rit_destructor(std::false_type) { return Destroy<const_reverse_iterator>::func(); }
public:
   static SV* create_vtbl(std::false_type)
   {
      SV* vtbl=super::create_vtbl();
      super::fill_iterator_access_vtbl(
         vtbl, 2,
         sizeof(reverse_iterator), sizeof(const_reverse_iterator),
         rit_destructor(typename super::non_const_access()),
         rit_destructor(std::false_type()),
         rbegin(typename super::non_const_access()),
         rbegin(std::false_type()),
         rderef(bool_constant<super::is_sparse>(), typename super::non_const_access()),
         rderef(bool_constant<super::is_sparse>(), std::false_type())
      );
      return vtbl;
   }

   static SV* create_vtbl(std::true_type)
   {
      return super::create_vtbl();
   }
   static SV* create_vtbl()
   {
      return create_vtbl(bool_constant<super::is_associative>());
   }
public:
   static SV* register_it(const AnyString& name, SV* someref, const AnyString& file=AnyString(), int line=0)
   {
      return super::register_me(name, file, line, someref, create_vtbl());
   }
};

template <typename T>
class ContainerClassRegistrator<T, random_access_iterator_tag, false>
   : public ContainerClassRegistrator<T, bidirectional_iterator_tag, false> {
   typedef ContainerClassRegistrator<T, bidirectional_iterator_tag, false> super;
protected:
   static void random_impl(char* p_obj, char*, int index, SV* dst, SV* container_sv)
   {
      T* obj = reinterpret_cast<T*>(p_obj);
      index=index_within_range(super::helper::streamline(*obj), index);
      Value pv(dst, value_expect_lval | value_allow_non_persistent | value_allow_store_ref);
      pv.put_lvalue(super::helper::streamline(*obj)[index], 0, nullptr, (nothing*)nullptr, container_sv);
   }

   static void crandom(char* p_obj, char*, int index, SV* dst, SV* container_sv)
   {
      const T* obj = reinterpret_cast<const T*>(p_obj);
      index=index_within_range(super::helper::streamline(*obj), index);
      Value pv(dst, value_read_only | value_expect_lval | value_allow_non_persistent | value_allow_store_ref);
      pv.put_lvalue(super::helper::streamline(*obj)[index], 0, nullptr, (nothing*)nullptr, container_sv);
   }

   static void random_sparse(char* p_obj, char*, int index, SV* dst, SV* container_sv)
   {
      T* obj = reinterpret_cast<T*>(p_obj);
      index=index_within_range(super::helper::streamline(*obj), index);
      Value pv(dst, value_expect_lval | value_allow_non_persistent);
      pv.put(super::helper::streamline(*obj)[index], 0, container_sv);
   }

   static container_access_type random(std::false_type, std::true_type)
   {
      return &random_impl;
   }
   static container_access_type random(std::true_type, std::true_type)
   {
      return &random_sparse;
   }
   template <typename is_sparse>
   static container_access_type random(is_sparse, std::false_type)
   {
      return &crandom;
   }
public:
   static SV* register_it(const AnyString& name, SV* someref, const AnyString& file=AnyString(), int line=0)
   {
      SV* vtbl=super::create_vtbl();
      super::fill_random_access_vtbl(
         vtbl,
         random(bool_constant<super::is_sparse>(), typename super::non_const_access()),
         random(bool_constant<super::is_sparse>(), std::false_type())
      );
      return super::register_me(name, file, line, someref, vtbl);
   }
};

template <typename T, typename Category>
class ContainerClassRegistrator<T, Category, true>
   : public ClassRegistratorBase {
public:
   static SV* register_it(const AnyString& name, SV* someref, const AnyString& file=AnyString(), int line=0)
   {
      return register_class(
         name, file, line, someref,
         typeid(T).name(),
         false, class_is_opaque,
         create_builtin_vtbl(
            typeid(T), sizeof(T),
            0, 0, 0, Destroy<T>::func()
         )
      );
   }
};

template <typename T>
class ClassRegistrator<T, is_container>
   : public ContainerClassRegistrator<T> {};

template <typename T, int n=0, int l=list_length<typename object_traits<T>::elements>::value>
struct CompositeClassRegistrator {
   typedef typename n_th<typename object_traits<T>::elements, n>::type member_type;
   static const bool allow_non_const_access = !attrib<member_type>::is_const &&
                                              !object_traits<typename deref<member_type>::type>::is_always_const;

   static void get_impl(char* p, SV* dst, SV* container_sv)
   {
      T* obj = reinterpret_cast<T*>(p);
      Value pv(dst, value_expect_lval | value_allow_non_persistent | value_allow_store_ref);
      pv.put_lvalue(visit_n_th(*obj, int_constant<n>()), 0, nullptr, (nothing*)nullptr, container_sv);
   }

   static void cget(char* p, SV* dst, SV* container_sv)
   {
      const T* obj = reinterpret_cast<const T*>(p);
      Value pv(dst, value_read_only | value_expect_lval | value_allow_non_persistent | value_allow_store_ref);
      pv.put_lvalue(visit_n_th(*obj, int_constant<n>()), 0, nullptr, (nothing*)nullptr, container_sv);
   }

   static void store_impl(char* p, SV* src)
   {
      T* obj = reinterpret_cast<T*>(p);
      Value v(src, value_not_trusted);
      v >> visit_n_th(*obj, int_constant<n>());
   }

   static composite_access_type get(std::true_type)  { return &get_impl; }
   static composite_access_type get(std::false_type) { return &cget; }
   static composite_store_type store(std::true_type)  { return &store_impl; }
   static composite_store_type store(std::false_type) { return nullptr; }

   static void init(composite_access_vtbl* acct)
   {
      acct->get[0]=get(bool_constant<allow_non_const_access>());
      acct->get[1]=get(std::false_type());
      acct->store=store(bool_constant<allow_non_const_access>());
      CompositeClassRegistrator<T, n+1, l>::init(++acct);
   }

   static SV* provide_member_names() { return member_names(recognizer_bait(0), (T*)0); }
};

template <typename T, int l>
struct CompositeClassRegistrator<T,l,l> {
   static void init(composite_access_vtbl*) {}
};

template <typename T>
struct get_persistent_type {
   typedef typename object_traits<T>::persistent_type type;
};

template <typename T>
class ClassRegistrator<T, is_composite>
   : public ClassRegistratorBase {
public:
   typedef typename list_transform_unary<get_persistent_type, typename object_traits<T>::elements>::type elements;

   static SV* register_it(const AnyString& name, SV* someref, const AnyString& file=AnyString(), int line=0)
   {
      return register_class(
         name, file, line, someref,
         typeid(T).name(),
         is_mutable<T>::value,
         class_is_composite | Serializable<T>::flag_value(),
         create_composite_vtbl(
            typeid(T), sizeof(T), object_traits<T>::total_dimension,
            Copy<T>::func(),
            Assign<T>::func(),
            Destroy<T>::func(),
            ToString<T>::func(),
            Serializable<T>::conv(),
            Serializable<T>::provide(),
            Serializable<T>::provide_descr(),
            list_length<elements>::value,
            &TypeListUtils<elements>::provide_types,
            &TypeListUtils<elements>::provide_descrs,
            &CompositeClassRegistrator<T>::provide_member_names,
            &CompositeClassRegistrator<T>::init
         )
      );
   }
};

} }
namespace polymake { namespace perl_bindings {

template <typename T>
class Class
   : public pm::perl::ClassRegistrator<T> {
   Class() = delete;
public:
   void add__me(const AnyString& name, const AnyString& file, int line) const
   {
      this->register_it(name, this->queue, file, line);
   }
};

} }
namespace pm { namespace perl {

class ClassTemplate {
   ClassTemplate() = delete;
public:
   static void add__me(const AnyString& name);
};

SV* get_parameterized_type_impl(const AnyString& pkg, bool exact_match);

template <typename TypeList, bool exact_match> inline
SV* get_parameterized_type(const AnyString& pkg, bool_constant<exact_match>)
{
   Stack stack(true, 1+TypeListUtils<TypeList>::type_cnt);
   if (TypeListUtils<TypeList>::push_types(stack)) {
      return get_parameterized_type_impl(pkg, exact_match);
   } else {
      stack.cancel();
      return NULL;
   }
}

class EmbeddedRule
   : public RegistratorQueue {
private:
   EmbeddedRule() = delete;
public:
   static const Kind kind = embedded_rule;
   void add__me(const AnyString& file, int line, const AnyString& text) const;
};

template <typename Target, typename Source,
          bool enabled=can_assign_to<typename access<Source>::type, Target>::value>
struct Operator_assign_impl {
   static wrapper_type func() { return nullptr; }
};

template <typename Target, typename Source>
struct Operator_assign_impl<Target, Source, true> {
   static void call(Target& dst, const Value& src)
   {
      if (MaybeWary<Target>::value && (src.get_flags() & value_not_trusted))
         maybe_wary(dst)=src.get<Source>();
      else
         dst=src.get<Source>();
   }
   static wrapper_type func() { return reinterpret_cast<wrapper_type>(&call); }
};

template <typename Target, typename Source>
class Operator_assign
   : public FunctionTemplateBase {
   Operator_assign() = delete;
public:
   typedef cons<Target, Source> arg_list;

   template <typename TAppList>
   void add__me(const AnyString& file, int line, TAppList cross_apps) const
   {
      register_it(Operator_assign_impl<Target, Source>::func(),
                  "=ass", file, line, TypeListUtils<arg_list>::get_type_names(), cross_apps);
   }
};

template <typename Target, typename Source,
          bool enabled=std::is_constructible<Target, typename access<Source>::type>::value>
struct Operator_convert_impl {
   static wrapper_type func() { return nullptr; }
};

template <typename Target, typename Source>
struct Operator_convert_impl<Target, Source, true> {
   static Target call(const Value& src)
   {
      return Target(src.get<Source>());
   }
   static wrapper_type func() { return reinterpret_cast<wrapper_type>(&call); }
};

template <typename Target, typename Source>
class Operator_convert
   : public FunctionTemplateBase {
   Operator_convert() = delete;
public:
   typedef cons<Target, Source> arg_list;

   template <typename TAppList>
   void add__me(const AnyString& file, int line, TAppList cross_apps) const
   {
      register_it(Operator_convert_impl<Target, Source>::func(),
                  ".cnv", file, line, TypeListUtils<arg_list>::get_type_names(), cross_apps);
   }
};

Wrapper4UnaryOperator(-, neg);
Wrapper4UnaryOperator(~, com);

Wrapper4UnaryAnchOperator(-, neg);
Wrapper4UnaryAnchOperator(~, com);

Wrapper4BinaryOperator(+, add);
Wrapper4BinaryOperator(-, sub);
Wrapper4BinaryOperator(*, mul);
Wrapper4BinaryOperator(/, div);
Wrapper4BinaryOperator(%, mod);
Wrapper4BinaryOperator(&, and);
Wrapper4BinaryOperator(|, _or);
Wrapper4BinaryOperator(^, xor);
Wrapper4BinaryOperator(<<, lsh);
Wrapper4BinaryOperator(>>, rsh);

Wrapper4BinaryAnchOperator(+, add);
Wrapper4BinaryAnchOperator(-, sub);
Wrapper4BinaryAnchOperator(*, mul);
Wrapper4BinaryAnchOperator(/, div);
Wrapper4BinaryAnchOperator(%, mod);
Wrapper4BinaryAnchOperator(&, and);
Wrapper4BinaryAnchOperator(|, _or);
Wrapper4BinaryAnchOperator(^, xor);
Wrapper4BinaryAnchOperator(<<, lsh);
Wrapper4BinaryAnchOperator(>>, rsh);

Wrapper4UnaryAssignOperator(++, inc);
Wrapper4UnaryAssignOperator(--, dec);

Wrapper4BinaryAssignOperator(+=, add);
Wrapper4BinaryAssignOperator(-=, sub);
Wrapper4BinaryAssignOperator(*=, mul);
Wrapper4BinaryAssignOperator(/=, div);
Wrapper4BinaryAssignOperator(%=, mod);
Wrapper4BinaryAssignOperator(&=, and);
Wrapper4BinaryAssignOperator(|=, _or);
Wrapper4BinaryAssignOperator(^=, xor);
Wrapper4BinaryAssignOperator(<<=, lsh);
Wrapper4BinaryAssignOperator(>>=, rsh);

Wrapper4BinaryOperator(==, _eq);
Wrapper4BinaryOperator(!=, _ne);
Wrapper4BinaryOperator(<, _lt);
Wrapper4BinaryOperator(<=, _le);
Wrapper4BinaryOperator(>, _gt);
Wrapper4BinaryOperator(>=, _ge);

template <typename Arg0>
WrapperStart( Operator_Unary_boo, ".boo", Arg0 ) {
   Value arg0(stack[0]);
   WrapperReturn( !is_zero( arg0.get<Arg0>() ) );
};

template <typename Arg0>
WrapperStart( Operator_Unary_not, ".not", Arg0 ) {
   Value arg0(stack[0]);
   WrapperReturn( is_zero( arg0.get<Arg0>() ) );
};

template <typename Arg0, typename Arg1>
WrapperStart( Operator_Binary_brk, ":brk", Arg0,Arg1 ) {
   Value arg0(stack[0]), arg1(stack[1]);
   WrapperReturnLvalue( Arg0, arg0.get<Arg0>() [ arg1.get<Arg1>() ] );
};

} }

#endif // POLYMAKE_PERL_WRAPPERS_H

// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End: