|
1 | 1 | (* ::Package:: *)
|
2 | 2 |
|
3 |
| - |
| 3 | +(* Autogenerated Package *) |
4 | 4 |
|
5 | 5 | (* ::Section:: *)
|
6 | 6 | (*Interfaces*)
|
|
39 | 39 | "Alias for defining object methods";
|
40 | 40 | InterfaceAttribute::usage=
|
41 | 41 | "Alias for defining object attributes";
|
| 42 | +InterfaceOverride::usage= |
| 43 | + "Alias for defining object UpValues"; |
42 | 44 |
|
43 | 45 |
|
44 | 46 | Begin["`Private`"];
|
|
314 | 316 | "Constructor"->Automatic,
|
315 | 317 | "MutationHandler"->Automatic,
|
316 | 318 | "MutationFunctions"->None,
|
| 319 | + "Protect"->False, |
317 | 320 | "AccessorFunctions"->Automatic,
|
318 | 321 | "NormalFunction"->Automatic,
|
319 | 322 | "Formatted"->True,
|
320 | 323 | "Icon"->None,
|
321 | 324 | "DefaultMethods"->{
|
322 |
| - Keys, SetProperty, PropertyValue, |
| 325 | + Association, Keys, Values, SetProperty, PropertyValue, |
323 | 326 | RemoveProperty, PropertyList
|
324 | 327 | },
|
325 | 328 | "DefaultAttributes"->{
|
|
416 | 419 | ];
|
417 | 420 | iRegisterDefaultInterfaceMethods[head, OptionValue["DefaultMethods"]];
|
418 | 421 | iRegisterDefaultInterfaceAttributes[head, OptionValue["DefaultAttributes"]];
|
| 422 | + If[OptionValue["Protect"], Protect[head]]; |
419 | 423 | head
|
420 | 424 | ];
|
421 | 425 |
|
|
601 | 605 | },
|
602 | 606 | InterfaceConstructor[head]=constructor;
|
603 | 607 | (* Constructor DownValue on the object *)
|
| 608 | + Unprotect[head]; |
604 | 609 | head//ClearAll;
|
605 | 610 | head~SetAttributes~HoldAllComplete;
|
606 | 611 | head[args___]?checkInvalid:=
|
|
623 | 628 |
|
624 | 629 |
|
625 | 630 |
|
| 631 | +$noArgMutations= |
| 632 | + Alternatives@@ |
| 633 | + {Unset, Increment, Decrement}; |
| 634 | + |
| 635 | + |
| 636 | +$oneArgMutations= |
| 637 | + Alternatives@@ |
| 638 | + { |
| 639 | + Set, SetDelayed, |
| 640 | + AddTo, SubtractFrom, TimesBy, DivideBy, |
| 641 | + AppendTo, PrependTo, |
| 642 | + AssociateTo, KeyDropFrom |
| 643 | + }; |
| 644 | + |
| 645 | + |
| 646 | +iRegisterInterfaceMutationHandler[ |
| 647 | + head_, |
| 648 | + func_, |
| 649 | + dispatcher_ |
| 650 | + ]:= |
| 651 | + With[ |
| 652 | + { |
| 653 | + oQ=InterfaceValidator[head], |
| 654 | + symQ=Unique[symbolQ], |
| 655 | + oA=$oneArgMutations, |
| 656 | + nA=$noArgMutations, |
| 657 | + d= |
| 658 | + If[!AssociationQ@dispatcher, |
| 659 | + <|"Keys"->dispatcher|>, |
| 660 | + dispatcher |
| 661 | + ] |
| 662 | + }, |
| 663 | + symQ~SetAttributes~{Temporary, HoldFirst}; |
| 664 | + symQ[sym_]:= |
| 665 | + MatchQ[OwnValues[sym], {_:>_head?oQ}]; |
| 666 | + func // ClearAll; |
| 667 | + func~SetAttributes~HoldAllComplete; |
| 668 | + If[KeyExistsQ[d, "Self"], |
| 669 | + With[{subdispatch=dispatcher["Self"]}, |
| 670 | + func[ |
| 671 | + (h: oA)[obj_Symbol?symQ, val_] |
| 672 | + ] := |
| 673 | + With[{res=subdispatch[h][obj, val]}, |
| 674 | + (obj=res)/;Head[res]===head |
| 675 | + ]; |
| 676 | + func[ |
| 677 | + (h : nA)[obj_Symbol?symQ] |
| 678 | + ] := |
| 679 | + With[{res=subdispatch[h][obj]}, |
| 680 | + (obj=res)/;Head[res]===head |
| 681 | + ]; |
| 682 | + ]; |
| 683 | + ]; |
| 684 | + If[KeyExistsQ[d, "Keys"], |
| 685 | + With[{subdispatch=dispatcher["Keys"]}, |
| 686 | + func[ |
| 687 | + (h: oA)[obj_Symbol?symQ[attr__], val_] |
| 688 | + ] := |
| 689 | + With[{res=subdispatch[h][obj, {attr}, val]}, |
| 690 | + (obj=res)/;Head[res]===head |
| 691 | + ]; |
| 692 | + func[ |
| 693 | + (h : nA)[obj_Symbol?symQ[attr__]] |
| 694 | + ] := |
| 695 | + With[{res=subdispatch[h][obj, {attr}]}, |
| 696 | + (obj=res)/;Head[res]===head |
| 697 | + ]; |
| 698 | + ] |
| 699 | + ]; |
| 700 | + If[KeyExistsQ[d, "Parts"], |
| 701 | + With[{subdispatch=dispatcher["Parts"]}, |
| 702 | + func[ |
| 703 | + (h: oA)[obj_Symbol?symQ[[part__]], val_] |
| 704 | + ] := |
| 705 | + With[{res=subdispatch[h][obj, {part}, val]}, |
| 706 | + (obj=res)/;Head[res]===head |
| 707 | + ]; |
| 708 | + func[ |
| 709 | + (h : nA)[obj_Symbol?symQ[[part__]]] |
| 710 | + ] := |
| 711 | + With[{res=subdispatch[h][obj, {part}]}, |
| 712 | + (obj=res)/;Head[res]===head |
| 713 | + ]; |
| 714 | + ] |
| 715 | + ]; |
| 716 | + (* fallthrough to get normal behavior back *) |
| 717 | + func[___] := Language`MutationFallthrough; |
| 718 | + Language`SetMutationHandler[head, func]; |
| 719 | + ] |
| 720 | + |
| 721 | + |
| 722 | +(* ::Subsubsection::Closed:: *) |
| 723 | +(*iRegisterInterfaceMutationHandler*) |
| 724 | + |
| 725 | + |
| 726 | + |
626 | 727 | $noArgMutations=
|
627 | 728 | Alternatives@@
|
628 | 729 | {Unset, Increment, Decrement};
|
|
731 | 832 | {
|
732 | 833 | mn=methodName,
|
733 | 834 | valid=InterfaceValidator[head],
|
734 |
| - meths=InterfaceMethods[head] |
| 835 | + meths=InterfaceMethods[head], |
| 836 | + attrs=Attributes[head] |
735 | 837 | },
|
| 838 | + If[MemberQ[attrs, Protected], Unprotect[head]]; |
736 | 839 | InterfaceMethods[head]=
|
737 | 840 | If[meths===Null,
|
738 | 841 | <|mn->True|>,
|
|
742 | 845 | def;
|
743 | 846 | head/:lhs?valid[mn][args]:=
|
744 | 847 | def;
|
| 848 | + If[MemberQ[attrs, Protected], Protect[head]]; |
745 | 849 | ];
|
746 | 850 |
|
747 | 851 |
|
|
779 | 883 | {
|
780 | 884 | mn=methodName,
|
781 | 885 | valid=InterfaceValidator[head],
|
782 |
| - meths=InterfaceAttributes[head] |
| 886 | + meths=InterfaceAttributes[head], |
| 887 | + attrs=Attributes[head] |
783 | 888 | },
|
| 889 | + If[MemberQ[attrs, Protected], Unprotect[head]]; |
784 | 890 | InterfaceAttributes[head]=
|
785 | 891 | If[meths===Null,
|
786 | 892 | <|mn->True|>,
|
787 | 893 | Append[meths, mn->True]
|
788 | 894 | ];
|
789 | 895 | head/:lhs?valid[mn]:=
|
790 | 896 | def;
|
| 897 | + If[MemberQ[attrs, Protected], Protect[head]]; |
791 | 898 | ];
|
792 | 899 |
|
793 | 900 |
|
|
802 | 909 | Protect[InterfaceAttribute]
|
803 | 910 |
|
804 | 911 |
|
| 912 | +(* ::Subsubsection::Closed:: *) |
| 913 | +(*iRegisterInterfaceOverride*) |
| 914 | + |
| 915 | + |
| 916 | + |
| 917 | +iRegisterInterfaceOverride~SetAttributes~HoldRest; |
| 918 | +iRegisterInterfaceOverride[ |
| 919 | + head_, |
| 920 | + upval_, |
| 921 | + {lh___}, |
| 922 | + main_, |
| 923 | + {rh___}, |
| 924 | + def_ |
| 925 | + ]:= |
| 926 | + With[ |
| 927 | + { |
| 928 | + mn=upval, |
| 929 | + valid=InterfaceValidator[head], |
| 930 | + attrs=Attributes[head] |
| 931 | + }, |
| 932 | + If[MemberQ[attrs, Protected], Unprotect[head]]; |
| 933 | + head/:upval[lh, main?valid, rh]:= |
| 934 | + def; |
| 935 | + If[MemberQ[attrs, Protected], Protect[head]]; |
| 936 | + ]; |
| 937 | + |
| 938 | + |
| 939 | +Unprotect[InterfaceOverride]; |
| 940 | +InterfaceOverride/: |
| 941 | + ( |
| 942 | + InterfaceOverride[head_][ |
| 943 | + upval_[args___] |
| 944 | + ]:=def_ |
| 945 | + ):= |
| 946 | + Module[ |
| 947 | + { |
| 948 | + pivot, |
| 949 | + argList |
| 950 | + }, |
| 951 | + argList=Thread[HoldComplete[{args}]]; |
| 952 | + pivot= |
| 953 | + SelectFirst[Range[Length[argList]], !FreeQ[argList[[#]], head]&, |
| 954 | + Length[argList]+1]; |
| 955 | + argList= |
| 956 | + Map[ |
| 957 | + Thread[#, HoldComplete]&, |
| 958 | + { |
| 959 | + Take[argList, UpTo[pivot-1]], |
| 960 | + argList[[{pivot}]], |
| 961 | + Drop[argList, UpTo[pivot]] |
| 962 | + } |
| 963 | + ]; |
| 964 | + Replace[argList, |
| 965 | + { |
| 966 | + HoldComplete[{lh___}]|{lh___}, |
| 967 | + HoldComplete[{main_}], |
| 968 | + HoldComplete[{rh___}]|{rh___} |
| 969 | + }:> |
| 970 | + iRegisterInterfaceOverride[head, upval, {lh}, main, {rh}, def] |
| 971 | + ] |
| 972 | + ]; |
| 973 | +Protect[InterfaceOverride] |
| 974 | + |
| 975 | + |
805 | 976 | (* ::Subsubsection::Closed:: *)
|
806 | 977 | (*iRegisterInterfaceAccessor*)
|
807 | 978 |
|
|
815 | 986 | },
|
816 | 987 | If[KeyExistsQ[ea, "Keys"],
|
817 | 988 | With[{lookup=dispatcher["Keys"]},
|
818 |
| - obj_head?valid[attr_String?(!KeyExistsQ[InterfaceMethods[head], #]&)]:= |
| 989 | + obj_head?valid[attr_String?( |
| 990 | + !KeyExistsQ[ |
| 991 | + Join[InterfaceMethods[head], InterfaceAttributes[head]], |
| 992 | + # |
| 993 | + ]&)]:= |
819 | 994 | With[{res=lookup[obj, attr]}, res/;Head[res]=!=lookup];
|
820 |
| - obj_head?valid[attr:Except[_String|_String[]]]:= |
| 995 | + obj_head?valid[attr:Except[_String|_String[___]]]:= |
821 | 996 | With[{res=lookup[obj, attr]}, res/;Head[res]=!=lookup];
|
822 | 997 | obj_head?valid[attr1_, attrs__]:=
|
823 | 998 | With[{res=lookup[obj, attr1, attrs]}, res/;Head[res]=!=lookup];
|
|
934 | 1109 |
|
935 | 1110 |
|
936 | 1111 |
|
| 1112 | +(* ::Subsubsubsection::Closed:: *) |
| 1113 | +(*Association*) |
| 1114 | + |
| 1115 | + |
| 1116 | + |
| 1117 | +registerDefaultMethod[head_, validator_, Association]:= |
| 1118 | + head/:HoldPattern@Association[head[a_]?validator]:= |
| 1119 | + Association[a]; |
| 1120 | + |
| 1121 | + |
937 | 1122 | (* ::Subsubsubsection::Closed:: *)
|
938 | 1123 | (*Keys*)
|
939 | 1124 |
|
|
944 | 1129 | Keys[a];
|
945 | 1130 |
|
946 | 1131 |
|
| 1132 | +(* ::Subsubsubsection::Closed:: *) |
| 1133 | +(*Values*) |
| 1134 | + |
| 1135 | + |
| 1136 | + |
| 1137 | +registerDefaultMethod[head_, validator_, Values]:= |
| 1138 | + head/:HoldPattern@Values[head[a_]?validator]:= |
| 1139 | + Values[a]; |
| 1140 | + |
| 1141 | + |
947 | 1142 | (* ::Subsubsubsection::Closed:: *)
|
948 | 1143 | (*ReplacePart*)
|
949 | 1144 |
|
|
0 commit comments