| 1 | ######################################################################## | ||
| 2 | # housekeeping | ||
| 3 | ######################################################################## | ||
| 4 | package Object::Lvalue v0.1.0; | ||
| 5 | use v5.34; | ||
| 6 | |||
| 7 | use mro::EVERY; | ||
| 8 | |||
| 9 | use Carp qw( carp croak ); | ||
| 10 | use List::Util qw( uniq sum zip ); | ||
| 11 | use Scalar::Util qw( blessed ); | ||
| 12 | use Sub::Name qw( subname ); | ||
| 13 | use Storable qw( dclone ); | ||
| 14 | use Symbol qw( qualify_to_ref qualify ); | ||
| 15 | |||
| 16 | ######################################################################## | ||
| 17 | # package variables | ||
| 18 | ######################################################################## | ||
| 19 | |||
| 20 | our $ol_pkg = ''; | ||
| 21 | *ol_pkg = \ __PACKAGE__ ; | ||
| 22 | |||
| 23 | our @CARP_NOT = ( $ol_pkg ); | ||
| 24 | |||
| 25 | my $verbose = $ENV{ OBJ_LVAL_VERBOSE } // ''; | ||
| 26 | |||
| 27 | ######################################################################## | ||
| 28 | # utility subs | ||
| 29 | ######################################################################## | ||
| 30 | |||
| 31 | my $debug | ||
| 32 | = sub | ||
| 33 | { | ||
| 34 | say join "\n#\t" => '', @_ | ||
| 35 | if $verbose; | ||
| 36 | }; | ||
| 37 | |||
| 38 | ######################################################################## | ||
| 39 | # methods | ||
| 40 | ######################################################################## | ||
| 41 | # introspection | ||
| 42 | |||
| 43 | sub verbose : lvalue | ||
| 44 | { | ||
| 45 | $verbose | ||
| 46 | } | ||
| 47 | |||
| 48 | sub class_attr | ||
| 49 | { | ||
| 50 | # this package showsup on get_linear_isa, has no attributes. | ||
| 51 | |||
| 52 | state $pkg2attrz = { __PACKGE__ => [] }; | ||
| 53 | |||
| 54 | my $proto = shift; | ||
| 55 | my $class = blessed $proto || $proto; | ||
| 56 | |||
| 57 | my $attrz | ||
| 58 | = @_ | ||
| 59 | ? $pkg2attrz->{ $class } = [ @_ ] | ||
| 60 | : $pkg2attrz->{ $class } | ||
| 61 | ; | ||
| 62 | |||
| 63 | wantarray // return; | ||
| 64 | |||
| 65 | $attrz ||= []; | ||
| 66 | |||
| 67 | wantarray | ||
| 68 | ? @$attrz | ||
| 69 | : $attrz | ||
| 70 | } | ||
| 71 | |||
| 72 | sub attributes | ||
| 73 | { | ||
| 74 | # don't cache results of attributes call until | ||
| 75 | # after the class is installed via import to | ||
| 76 | # avoid caching the prior list. | ||
| 77 | |||
| 78 | state $pkg2attrz = { $ol_pkg => [] }; | ||
| 79 | |||
| 80 | my $proto = shift; | ||
| 81 | my $class = blessed $proto || $proto; | ||
| 82 | |||
| 83 | my $attrz | ||
| 84 | = $pkg2attrz->{ $class } | ||
| 85 | ||= | ||
| 86 | [ | ||
| 87 | uniq | ||
| 88 | map | ||
| 89 | { | ||
| 90 | $_->class_attr->@* | ||
| 91 | } | ||
| 92 | $class->mro::get_linear_isa->@* | ||
| 93 | ]; | ||
| 94 | |||
| 95 | wantarray // return; | ||
| 96 | |||
| 97 | wantarray | ||
| 98 | ? $attrz->@* | ||
| 99 | : [ $attrz->@* ] | ||
| 100 | } | ||
| 101 | |||
| 102 | sub import | ||
| 103 | { | ||
| 104 | # discard this package, | ||
| 105 | # remainder are attributs for this class. | ||
| 106 | |||
| 107 | my ( undef, @attrz ) = @_; | ||
| 108 | |||
| 109 | my $class = caller; | ||
| 110 | |||
| 111 | $debug->( "$class:", @attrz ); | ||
| 112 | |||
| 113 | for my $isa ( qualify_to_ref ISA => $class ) | ||
| 114 | { | ||
| 115 | # do this first for access to attributes in @prior. | ||
| 116 | |||
| 117 | push *$isa->@*, $ol_pkg | ||
| 118 | } | ||
| 119 | |||
| 120 | # check $attributes prior to assigning $pkg2attrz! | ||
| 121 | |||
| 122 | $debug->( "Prior $class:", $class->mro::get_linear_isa->@* ); | ||
| 123 | |||
| 124 | my $first_off | ||
| 125 | = map | ||
| 126 | { | ||
| 127 | $_->class_attr | ||
| 128 | } | ||
| 129 | $class->mro::get_linear_isa->@*; | ||
| 130 | |||
| 131 | my $names = $class->class_attr( @attrz ); | ||
| 132 | |||
| 133 | for( zip [ $first_off .. $first_off + $#attrz ], $names ) | ||
| 134 | { | ||
| 135 | # count of priors == offset of first attribute | ||
| 136 | # in this class; through the offset of its | ||
| 137 | # last attribute. | ||
| 138 | |||
| 139 | my( $offset, $name ) = @$_; | ||
| 140 | |||
| 141 | # they already handle it. | ||
| 142 | next if $class->can( $name ); | ||
| 143 | |||
| 144 | my $fqn = qualify $name => $class; | ||
| 145 | |||
| 146 | $debug->( "Install: $name -> $offset ($fqn)" ); | ||
| 147 | |||
| 148 | *{ qualify_to_ref $fqn } | ||
| 149 | = subname $fqn => sub : lvalue | ||
| 150 | { | ||
| 151 | my $obj = shift; | ||
| 152 | $obj->[ $offset ] | ||
| 153 | }; | ||
| 154 | } | ||
| 155 | |||
| 156 | return | ||
| 157 | } | ||
| 158 | |||
| 159 | ######################################################################## | ||
| 160 | # object manglement | ||
| 161 | |||
| 162 | sub clone | ||
| 163 | { | ||
| 164 | # override this in any classes that store subs in their | ||
| 165 | # struct or need to zero-out accumulator attributes. | ||
| 166 | |||
| 167 | eval { &dclone } | ||
| 168 | or croak "Failed clone: $@" | ||
| 169 | } | ||
| 170 | |||
| 171 | sub copy | ||
| 172 | { | ||
| 173 | my $copy = shift; | ||
| 174 | my $src = shift | ||
| 175 | or croak 'Bogus copy: false source argument'; | ||
| 176 | |||
| 177 | $src->isa( $ol_pkg ) | ||
| 178 | or croak "Botched copy: $src is not $ol_pkg"; | ||
| 179 | |||
| 180 | $copy->@* = $src->@*; | ||
| 181 | |||
| 182 | return | ||
| 183 | }; | ||
| 184 | |||
| 185 | sub shallow | ||
| 186 | { | ||
| 187 | # NOT A CLONE! | ||
| 188 | # this makes a shallow copy for cases where the | ||
| 189 | # content should be sharable (e.g., accumulator | ||
| 190 | # attributes w/ lifespan outside of the object). | ||
| 191 | # | ||
| 192 | # individual classes have the opportunity to | ||
| 193 | # munge the contents after the copy. | ||
| 194 | |||
| 195 | my $obj = shift; | ||
| 196 | |||
| 197 | blessed $obj | ||
| 198 | or croak "Bogus copy: '$obj' not an object"; | ||
| 199 | |||
| 200 | # caller gets back newly copied object or | ||
| 201 | # an exception. | ||
| 202 | |||
| 203 | eval | ||
| 204 | { | ||
| 205 | my $new = $obj->construct; | ||
| 206 | |||
| 207 | $new->EVERY::LAST::copy( $obj ); | ||
| 208 | $new | ||
| 209 | } | ||
| 210 | or croak "Failed shallow: $@" | ||
| 211 | } | ||
| 212 | |||
| 213 | sub initialize | ||
| 214 | { | ||
| 215 | # stub keeps mro::EVERY::LAST happy. | ||
| 216 | } | ||
| 217 | |||
| 218 | sub construct | ||
| 219 | { | ||
| 220 | my $proto = shift; | ||
| 221 | |||
| 222 | bless [], blessed $proto || $proto | ||
| 223 | } | ||
| 224 | |||
| 225 | sub new | ||
| 226 | { | ||
| 227 | my $proto = shift; | ||
| 228 | my $object = $proto->construct; | ||
| 229 | |||
| 230 | $object->EVERY::LAST::initialize( @_ ); | ||
| 231 | $object | ||
| 232 | } | ||
| 233 | |||
| 234 | DESTROY | ||
| 235 | { | ||
| 236 | my $object = shift; | ||
| 237 | |||
| 238 | # from most-to-least derived classes can | ||
| 239 | # back out object contents. | ||
| 240 | |||
| 241 | $object->EVERY::cleanup; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub cleanup | ||
| 245 | { | ||
| 246 | # useless in out-of-order destroy in | ||
| 247 | # exit but helps keep things tidy on | ||
| 248 | # a normal cycle. | ||
| 249 | |||
| 250 | my $object = shift; | ||
| 251 | @$object = (); | ||
| 252 | |||
| 253 | undef $object; | ||
| 254 | } | ||
| 255 | |||
| 256 | # keep require happy | ||
| 257 | 1 | ||
| 258 | __END__ |