| 1 | #@ (S-)Sym(bolic)Obj(ect) - easy creation of symbol tables and objects. | ||
| 2 | package SymObj; | ||
| 3 | require 5.008_001; | ||
| 4 | our $VERSION = '0.8.2'; | ||
| 5 | our $COPYRIGHT =<<__EOT__; | ||
| 6 | Copyright (c) 2010 - 2016 Steffen (Daode) Nurpmeso <steffen\@sdaoden.eu>. | ||
| 7 | All rights reserved under the terms of the ISC license. | ||
| 8 | __EOT__ | ||
| 9 | # Permission to use, copy, modify, and/or distribute this software for any | ||
| 10 | # purpose with or without fee is hereby granted, provided that the above | ||
| 11 | # copyright notice and this permission notice appear in all copies. | ||
| 12 | # | ||
| 13 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 14 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 15 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 16 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 17 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 18 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 19 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 20 | |||
| 21 | use diagnostics -verbose; | ||
| 22 | use warnings; | ||
| 23 | use strict; | ||
| 24 | no strict 'refs'; # We fool around with that by definition, so this | ||
| 25 | |||
| 26 | sub NONE(){ 0 } | ||
| 27 | sub DEBUG(){ 1<<0 } | ||
| 28 | sub VERBOSE(){ 1<<1 } | ||
| 29 | sub DEEP_CLONE(){ 1<<2 } | ||
| 30 | sub _USRMASK(){ 0x7 } | ||
| 31 | |||
| 32 | sub _CLEANHIER(){ 1<<5 } | ||
| 33 | |||
| 34 | sub _UUID(){ 'S_SymObj__1C8288D6_9EDA_4ECD_927F_2144B94186AD' } | ||
| 35 | |||
| 36 | our $MsgFH = *STDERR; | ||
| 37 | our $Debug = 1; # 0,1,2 | ||
| 38 | |||
| 39 | sub pack_exists{ | ||
| 40 | %{"${_[0]}::"} | ||
| 41 | } | ||
| 42 | |||
| 43 | sub sym_dump{ | ||
| 44 | my ($pkg) = @_; | ||
| 45 | print $MsgFH "SymObj::sym_dump($pkg):\n\t"; | ||
| 46 | if(my $i = ref $pkg){ $pkg = $i } | ||
| 47 | foreach(keys %{*{"${pkg}::"}}){ print $MsgFH "$_ " } | ||
| 48 | #while(my ($k, $v) = each %{*{"${pkg}::"}}){ print $MsgFH "$k($v) " } | ||
| 49 | print $MsgFH "\n" | ||
| 50 | } | ||
| 51 | |||
| 52 | sub obj_dump{ | ||
| 53 | use Data::Dumper; | ||
| 54 | my $self = shift; | ||
| 55 | print $MsgFH "SymObj::obj_dump(): ", Dumper($self), "\n" | ||
| 56 | } | ||
| 57 | |||
| 58 | sub sym_create{ # {{{ | ||
| 59 | my ($flags, $tfields, $ctor) = @_; | ||
| 60 | $flags &= _USRMASK; | ||
| 61 | $flags |= DEBUG if $flags & VERBOSE; | ||
| 62 | $flags |= $Debug > 1 ? (DEBUG | VERBOSE) : DEBUG if $Debug; | ||
| 63 | my ($pkg, $i, $j, @isa, %actorargs, @ctorovers) = | ||
| 64 | (scalar caller, $flags & VERBOSE); | ||
| 65 | print $MsgFH "SymObj::sym_create(): $pkg\n" if $i; | ||
| 66 | |||
| 67 | # For (superior debug ctor) argument checking, create a hash of | ||
| 68 | # public symbols (inherit those from parents first ...) | ||
| 69 | # Note that our @isa<->*_SymObj_ISA is flattened, in construction order | ||
| 70 | $flags |= _CLEANHIER; | ||
| 71 | _resolve_tree($pkg, \%actorargs, $pkg, \$flags, \@isa) | ||
| 72 | if defined ${"${pkg}::"}{ISA}; | ||
| 73 | push @isa, $pkg; | ||
| 74 | |||
| 75 | print $MsgFH ".. (inherited VERBOSE:) SymObj::sym_create(): $pkg\n" | ||
| 76 | if ($flags & VERBOSE) && !$i; | ||
| 77 | |||
| 78 | # Create accessor symtable entries | ||
| 79 | foreach $j (keys %$tfields){ | ||
| 80 | sub TYPE_ARRAY(){ 1<<0 } | ||
| 81 | sub TYPE_HASH(){ 1<<1 } | ||
| 82 | sub TYPE_EXCLUDE(){ 1<<2 } | ||
| 83 | sub TYPE_RDONLY(){ 1<<3 } | ||
| 84 | my ($xj, $pj, $tj) = ($j, $j, 0); | ||
| 85 | |||
| 86 | $j =~ /^([@%])?([?!])?(_)?(.*)/; | ||
| 87 | $tj |= $1 eq '@' ? TYPE_ARRAY : TYPE_HASH if defined $1; | ||
| 88 | $tj |= $2 eq '?' ? TYPE_EXCLUDE : TYPE_RDONLY if defined $2; | ||
| 89 | print $MsgFH "\tSymbol '$pj': does NOT start with _! THIS WILL FAIL!!\n" | ||
| 90 | if !defined $3 && ($flags & DEBUG); | ||
| 91 | unless(defined $4 && length $4){ | ||
| 92 | print $MsgFH "\tSymbol '$pj': consists only of modifier and/or ", | ||
| 93 | "typedef and/or underscore! Skip!!\n" if ($flags & DEBUG); | ||
| 94 | delete $tfields->{$j}; | ||
| 95 | next | ||
| 96 | } | ||
| 97 | $xj = (defined $3 ? $3 : '') . $4; | ||
| 98 | $pj = $4; | ||
| 99 | |||
| 100 | if($tj){ | ||
| 101 | $tfields->{$xj} = $tfields->{$j}; | ||
| 102 | delete $tfields->{$j}; | ||
| 103 | $i = ($tj & TYPE_ARRAY) ? 'ARRAY' : ($tj & TYPE_HASH) ? 'HASH' : '' | ||
| 104 | }else{ | ||
| 105 | $i = ref $tfields->{$xj} | ||
| 106 | } | ||
| 107 | |||
| 108 | # Does any superclass define this symbol already, i.e., is this an | ||
| 109 | # override request? | ||
| 110 | if(exists $actorargs{$pj}){ | ||
| 111 | if($i ne ref ${$actorargs{$pj}}){ | ||
| 112 | print $MsgFH "${pkg}: overrides '$pj' of some super class ", | ||
| 113 | "with incompatible type! Skip!!\n" if $flags & DEBUG; | ||
| 114 | delete $tfields->{$xj}; | ||
| 115 | next | ||
| 116 | } | ||
| 117 | push @ctorovers, $pj | ||
| 118 | # And we do have to override the function(/method) too.. | ||
| 119 | } | ||
| 120 | $actorargs{$pj} = \$tfields->{$xj}; | ||
| 121 | |||
| 122 | # (And hope that perl(1) optimizes away some closure conditions..) | ||
| 123 | if(($tj & TYPE_ARRAY) || $i eq 'ARRAY'){ | ||
| 124 | print $MsgFH "\tsub $pj: array-based\n" if $flags & VERBOSE; | ||
| 125 | *{"${pkg}::__$pj"} = sub{ $_[0]->{$xj} }; | ||
| 126 | *{"${pkg}::$pj"} = sub{ | ||
| 127 | my $self = $_[0]; | ||
| 128 | if(($self = ref $self) && %{"${self}::"}){ | ||
| 129 | $self = shift; | ||
| 130 | if($tj & TYPE_EXCLUDE){ | ||
| 131 | SymObj::_complain_exclude($pkg, $pj) if ($flags & DEBUG); | ||
| 132 | $self = $tfields | ||
| 133 | } | ||
| 134 | }else{ | ||
| 135 | $self = $tfields; | ||
| 136 | } | ||
| 137 | my $f = $self->{$xj}; | ||
| 138 | if(!defined $f || @_){ | ||
| 139 | SymObj::_complain_rdonly($pkg, $pj) | ||
| 140 | if ($tj & TYPE_RDONLY) && @_ && ($flags & DEBUG); | ||
| 141 | $f = SymObj::_array_set($pkg, $self, $pj, $xj, @_); | ||
| 142 | } | ||
| 143 | wantarray ? @$f : $f | ||
| 144 | }; | ||
| 145 | }elsif(($tj & TYPE_HASH) || $i eq 'HASH'){ | ||
| 146 | print $MsgFH "\tsub $pj: hash-based\n" if $flags & VERBOSE; | ||
| 147 | *{"${pkg}::__$pj"} = sub{ $_[0]->{$xj} }; | ||
| 148 | *{"${pkg}::$pj"} = sub{ | ||
| 149 | my $self = $_[0]; | ||
| 150 | if(($self = ref $self) && %{"${self}::"}){ | ||
| 151 | $self = shift; | ||
| 152 | if($tj & TYPE_EXCLUDE){ | ||
| 153 | SymObj::_complain_exclude($pkg, $pj) if ($flags & DEBUG); | ||
| 154 | $self = $tfields | ||
| 155 | } | ||
| 156 | }else{ | ||
| 157 | $self = $tfields | ||
| 158 | } | ||
| 159 | my $f = $self->{$xj}; | ||
| 160 | if(!defined $f || @_){ | ||
| 161 | SymObj::_complain_rdonly($pkg, $pj) | ||
| 162 | if ($tj & TYPE_RDONLY) && @_ && ($flags & DEBUG); | ||
| 163 | $f = SymObj::_hash_set($pkg, $self, $pj, $xj, @_) | ||
| 164 | } | ||
| 165 | wantarray ? %$f : $f | ||
| 166 | }; | ||
| 167 | }else{ | ||
| 168 | # Scalar (or "typeless") | ||
| 169 | print $MsgFH "\tsub $pj: scalar-based ('untyped')\n" | ||
| 170 | if ($flags & VERBOSE); | ||
| 171 | *{"${pkg}::__$pj"} = sub{ \$_[0]->{$xj} }; | ||
| 172 | *{"${pkg}::$pj"} = sub{ | ||
| 173 | my $self = $_[0]; | ||
| 174 | if(($self = ref $self) && %{"${self}::"}){ | ||
| 175 | $self = shift; | ||
| 176 | if($tj & TYPE_EXCLUDE){ | ||
| 177 | SymObj::_complain_exclude($pkg, $pj) if ($flags & DEBUG); | ||
| 178 | $self = $tfields | ||
| 179 | } | ||
| 180 | }else{ | ||
| 181 | $self = $tfields | ||
| 182 | } | ||
| 183 | if(@_){ | ||
| 184 | SymObj::_complain_rdonly($pkg, $pj) | ||
| 185 | if ($tj & TYPE_RDONLY) && ($flags & DEBUG); | ||
| 186 | $self->{$xj} = shift | ||
| 187 | } | ||
| 188 | $self->{$xj} | ||
| 189 | }; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | # Variable fields | ||
| 194 | ${"${pkg}::"}{_SymObj_PACKAGE} = $pkg; | ||
| 195 | ${"${pkg}::"}{_SymObj_ISA} = \@isa; | ||
| 196 | ${"${pkg}::"}{_SymObj_ALL_CTOR_ARGS} = \%actorargs; | ||
| 197 | ${"${pkg}::"}{_SymObj_CTOR_OVERRIDES} = \@ctorovers; | ||
| 198 | ${"${pkg}::"}{_SymObj_FIELDS} = $tfields; | ||
| 199 | ${"${pkg}::"}{_SymObj_FLAGS} = $flags; | ||
| 200 | |||
| 201 | # User constructor? | ||
| 202 | if(!defined $ctor || ref $ctor ne 'CODE'){ | ||
| 203 | my $_c = $ctor; | ||
| 204 | $_c = '__ctor' unless defined $_c; | ||
| 205 | $ctor = sub{ SymObj::_find_usr_ctor(shift, $pkg, $_c) }; | ||
| 206 | } | ||
| 207 | ${"${pkg}::"}{_SymObj_USR_CTOR} = $ctor; | ||
| 208 | |||
| 209 | # new() | ||
| 210 | if($flags & DEBUG){ | ||
| 211 | $ctor = sub{ SymObj::_ctor_dbg($pkg, shift, \@_) }; | ||
| 212 | }elsif($flags & _CLEANHIER){ | ||
| 213 | $ctor = sub{ | ||
| 214 | my ($class, $self) = (shift, {}); | ||
| 215 | $self = bless $self, $class; | ||
| 216 | # Embed arguments | ||
| 217 | $self = SymObj::_ctor_argembed($pkg, $class, $self, \%actorargs, | ||
| 218 | \%actorargs, \@_); | ||
| 219 | # Fill what is not yet filled from arguments.. | ||
| 220 | my $f = ($flags & DEEP_CLONE); | ||
| 221 | while(($i, $j) = each %actorargs){ | ||
| 222 | $i = '_' . $i; | ||
| 223 | next if exists $self->{$i}; | ||
| 224 | $self->{$i} = SymObj::clone_ref($$j, $f) | ||
| 225 | } | ||
| 226 | # Call user CTORs in correct order.. | ||
| 227 | foreach $pkg (@isa){ | ||
| 228 | if(defined(my $sym = ${"${pkg}::"}{_SymObj_USR_CTOR})){ | ||
| 229 | &$sym($self) | ||
| 230 | } | ||
| 231 | } | ||
| 232 | $self | ||
| 233 | }; | ||
| 234 | }else{ | ||
| 235 | $ctor = sub{ SymObj::_ctor_dirtyhier($pkg, shift, \@_) } | ||
| 236 | } | ||
| 237 | *{"${pkg}::new"} = $ctor; | ||
| 238 | 1 | ||
| 239 | } # }}} | ||
| 240 | |||
| 241 | sub clone_ref{ # {{{ | ||
| 242 | my ($r, $deep) = @_; | ||
| 243 | |||
| 244 | if(!$deep){ | ||
| 245 | if(ref $r eq 'ARRAY'){ | ||
| 246 | my @a; | ||
| 247 | push @a, $_ foreach(@$r); | ||
| 248 | $r = \@a; | ||
| 249 | }elsif(ref $r eq 'HASH'){ | ||
| 250 | my (%h, $hk, $hv); | ||
| 251 | while(($hk, $hv) = each %$r){ $h{$hk} = $hv } | ||
| 252 | $r = \%h | ||
| 253 | } | ||
| 254 | }else{ | ||
| 255 | if(ref $r eq 'ARRAY'){ | ||
| 256 | my @a; | ||
| 257 | push @a, SymObj::clone_ref($_, 1) foreach(@$r); | ||
| 258 | $r = \@a | ||
| 259 | }elsif(ref $r eq 'HASH'){ | ||
| 260 | my (%h, $hk, $hv); | ||
| 261 | while(($hk, $hv) = each %$r){ $h{$hk} = SymObj::clone_ref($hv, 1) } | ||
| 262 | $r = \%h | ||
| 263 | } | ||
| 264 | } | ||
| 265 | $r | ||
| 266 | } # }}} | ||
| 267 | |||
| 268 | sub _resolve_tree{ # {{{ | ||
| 269 | my ($pkg, $_actorargs, $_p, $_f, $_isa) = @_; | ||
| 270 | foreach my $c (@{${"${_p}::"}{ISA}}){ | ||
| 271 | unless(%{"${c}::"}){ | ||
| 272 | print $MsgFH "${pkg}: $_p: \@ISA contains non-existent ", | ||
| 273 | "class '$c'!\n" if ($$_f & DEBUG); | ||
| 274 | next | ||
| 275 | } | ||
| 276 | |||
| 277 | my $j = ${"${c}::"}{_SymObj_FLAGS}; | ||
| 278 | unless(defined $j){ | ||
| 279 | print $MsgFH "${pkg}: $_p: '$c' not SymObj managed: hierarchy not ", | ||
| 280 | "clean, STOP!\n" if ($$_f & VERBOSE); | ||
| 281 | $$_f &= ~_CLEANHIER; | ||
| 282 | next | ||
| 283 | } | ||
| 284 | $$_f |= $j & (DEBUG | VERBOSE); # Inherit debug states | ||
| 285 | if(!($j & _CLEANHIER) && ($$_f & _CLEANHIER)){ | ||
| 286 | $$_f &= ~_CLEANHIER; | ||
| 287 | print $MsgFH "${pkg}: $_p: '$c' says hierarchy is not clean..\n" | ||
| 288 | if ($$_f & VERBOSE) | ||
| 289 | } | ||
| 290 | |||
| 291 | while(my ($k, $v) = each %{${"${c}::"}{_SymObj_ALL_CTOR_ARGS}}){ | ||
| 292 | $_actorargs->{$k} = $v | ||
| 293 | } | ||
| 294 | |||
| 295 | _resolve_tree($pkg, $_actorargs, $c, $_f, $_isa) | ||
| 296 | if defined ${"${c}::"}{ISA}; | ||
| 297 | push @$_isa, $c | ||
| 298 | } | ||
| 299 | 1 | ||
| 300 | } # }}} | ||
| 301 | |||
| 302 | sub _complain_exclude{ | ||
| 303 | my ($pkg, $pub) = @_; | ||
| 304 | print $MsgFH "${pkg}::$pub(): field can't be accessed through object, ", | ||
| 305 | "accessing class-static!\n"; | ||
| 306 | 1 | ||
| 307 | } | ||
| 308 | |||
| 309 | sub _complain_rdonly{ | ||
| 310 | my ($pkg, $pub) = @_; | ||
| 311 | print $MsgFH "${pkg}::$pub(): write access to READONLY field!\n"; | ||
| 312 | 1 | ||
| 313 | } | ||
| 314 | |||
| 315 | sub _hash_set{ # {{{ | ||
| 316 | my ($pkg, $self, $pub, $datum) = (shift, shift, shift, shift); | ||
| 317 | my ($flags, $dref, $k) = (${"${pkg}::"}{_SymObj_FLAGS}, $self->{$datum}); | ||
| 318 | |||
| 319 | $dref = $self->{$datum} = {} unless defined $dref; | ||
| 320 | |||
| 321 | foreach my $arg (@_){ | ||
| 322 | if(defined $k){ | ||
| 323 | $dref->{$k} = $arg; | ||
| 324 | $k = undef; | ||
| 325 | next | ||
| 326 | } | ||
| 327 | if(ref $arg eq 'HASH'){ | ||
| 328 | while(my ($k, $v) = each %$arg){ | ||
| 329 | $dref->{$k} = $v | ||
| 330 | } | ||
| 331 | }elsif(ref $arg eq 'ARRAY'){ | ||
| 332 | while(@$arg > 1){ | ||
| 333 | my $v = pop @$arg; | ||
| 334 | my $k = pop @$arg; | ||
| 335 | $dref->{$k} = $v | ||
| 336 | } | ||
| 337 | print $MsgFH "! ${pkg}::$pub(): wrong array member count!\n" | ||
| 338 | if @$arg != 0 && ($flags & DEBUG) | ||
| 339 | }else{ | ||
| 340 | $k = $arg | ||
| 341 | } | ||
| 342 | } | ||
| 343 | print $MsgFH "! ${pkg}::$pub(): '$k' key without a value\n" | ||
| 344 | if defined $k && ($flags & DEBUG); | ||
| 345 | $dref | ||
| 346 | } # }}} | ||
| 347 | |||
| 348 | sub _array_set{ # {{{ | ||
| 349 | my ($pkg, $self, $pub, $datum) = (shift, shift, shift, shift); | ||
| 350 | my $dref = $self->{$datum}; | ||
| 351 | |||
| 352 | $dref = $self->{$datum} = [] unless defined $dref; | ||
| 353 | |||
| 354 | foreach my $arg (@_){ | ||
| 355 | if(ref $arg eq 'ARRAY'){ | ||
| 356 | push @$dref, $_ foreach(@$arg) | ||
| 357 | }elsif(ref $arg eq 'HASH'){ | ||
| 358 | while(my ($k, $v) = each %$arg){ | ||
| 359 | push @$dref, $k; | ||
| 360 | push @$dref, $v | ||
| 361 | } | ||
| 362 | }else{ | ||
| 363 | push @$dref, $arg | ||
| 364 | } | ||
| 365 | } | ||
| 366 | $dref | ||
| 367 | } # }}} | ||
| 368 | |||
| 369 | sub _find_usr_ctor{ # {{{ | ||
| 370 | # No constructor was given to sym_create(), or it was no code-ref. | ||
| 371 | # Try to find out what the user wants. | ||
| 372 | my ($self, $pkg, $ctor) = @_; | ||
| 373 | my $flags = ${"${pkg}::"}{_SymObj_FLAGS}; | ||
| 374 | unless(defined ${"${pkg}::"}{$ctor}){ | ||
| 375 | print $MsgFH "${pkg}: no user ctor\n" if ($flags & VERBOSE); | ||
| 376 | delete ${"${pkg}::"}{_SymObj_USR_CTOR} | ||
| 377 | }else{ | ||
| 378 | print $MsgFH "${pkg}: resolved user ctor '$ctor'\n" if ($flags & VERBOSE); | ||
| 379 | $ctor = ${"${pkg}::"}{$ctor}; | ||
| 380 | ${"${pkg}::"}{_SymObj_USR_CTOR} = $ctor; | ||
| 381 | &$ctor($self) | ||
| 382 | } | ||
| 383 | 1 | ||
| 384 | } # }}} | ||
| 385 | |||
| 386 | sub _ctor_dbg{ # {{{ | ||
| 387 | my ($pkg, $class, $argaref) = @_; | ||
| 388 | my $flags = ${"${pkg}::"}{_SymObj_FLAGS}; | ||
| 389 | print $MsgFH "SymObj::obj_ctor <> new: $pkg called as $class\n" | ||
| 390 | if ($flags & VERBOSE); | ||
| 391 | |||
| 392 | my ($self, $actorargs, $ctorovers, $tfields, $init_chain, $i, $j, $k) = | ||
| 393 | (undef, ${"${pkg}::"}{_SymObj_ALL_CTOR_ARGS}, | ||
| 394 | ${"${pkg}::"}{_SymObj_CTOR_OVERRIDES}, ${"${pkg}::"}{_SymObj_FIELDS}); | ||
| 395 | |||
| 396 | # Use a savage and hacky but multithread-safe way to perform argument | ||
| 397 | # checking only in the ctor of the real (actual sub-) class (a.k.a. once) | ||
| 398 | $init_chain = @$argaref > 0 && defined $argaref->[0] && | ||
| 399 | $argaref->[0] eq _UUID; | ||
| 400 | |||
| 401 | # Inheritance handling | ||
| 402 | if(defined ${"${pkg}::"}{ISA}){ | ||
| 403 | # Append overrides | ||
| 404 | foreach $k (@$ctorovers){ | ||
| 405 | for($i = $init_chain, $j = @$argaref; $i < $j; $i += 2){ | ||
| 406 | goto j_OVW if $k eq $$argaref[$i] | ||
| 407 | } | ||
| 408 | push @$argaref, $k; | ||
| 409 | push @$argaref, $tfields->{'_' . $k}; | ||
| 410 | j_OVW:} | ||
| 411 | |||
| 412 | # Walk the new() chain, but disallow arg-checking for superclasses | ||
| 413 | unshift @$argaref, _UUID if !$init_chain; | ||
| 414 | |||
| 415 | foreach my $c (@{${"${pkg}::"}{ISA}}){ | ||
| 416 | unless(defined ${"${c}::"}{new}){ | ||
| 417 | print $MsgFH "${pkg}: $class->new(): no such package: $c!\n" | ||
| 418 | and next unless %{"${c}::"}; | ||
| 419 | print $MsgFH "${pkg}: $class->new(): $c: misses a new() sub!\n"; | ||
| 420 | next | ||
| 421 | } | ||
| 422 | $i = &{${"${c}::"}{new}}($class, @$argaref); | ||
| 423 | |||
| 424 | # (MI restriction applies here: if $self is yet a {} the other tree | ||
| 425 | # can only be joined in and thus looses it's hash-pointer) | ||
| 426 | $self = $i and next unless defined $self; | ||
| 427 | while(($k, $j) = each %$i){ $self->{$k} = $j } | ||
| 428 | } | ||
| 429 | |||
| 430 | shift @$argaref if !$init_chain | ||
| 431 | } | ||
| 432 | |||
| 433 | # SELF | ||
| 434 | $self = {} unless defined $self; | ||
| 435 | $self = bless $self, $class; | ||
| 436 | |||
| 437 | # Normal arguments; can and should we perform argument checking? | ||
| 438 | shift @$argaref if $init_chain; | ||
| 439 | if(@$argaref & 1){ | ||
| 440 | pop @$argaref; | ||
| 441 | print $MsgFH "${pkg}: $class->new(): odd argument discarded!\n" | ||
| 442 | } | ||
| 443 | |||
| 444 | # Embed arguments | ||
| 445 | $self->{_UID} = undef if $init_chain; | ||
| 446 | $self = SymObj::_ctor_argembed($pkg, $class, $self, $actorargs, $tfields, | ||
| 447 | $argaref); | ||
| 448 | delete $self->{_UID} if $init_chain; | ||
| 449 | |||
| 450 | # Finally: fill in yet unset members of $self via the per-class template. | ||
| 451 | $j = $flags; | ||
| 452 | if(exists ${"${class}::"}{_SymObj_FLAGS}){ | ||
| 453 | $j = ${"${class}::"}{_SymObj_FLAGS} | ||
| 454 | } | ||
| 455 | $j &= DEEP_CLONE; | ||
| 456 | while(($k, $i) = each %$tfields){ | ||
| 457 | next if exists $self->{$k}; | ||
| 458 | if(ref $i && ref $i ne 'HASH' && ref $i ne 'ARRAY'){ | ||
| 459 | print $MsgFH "${pkg}: $class->new(): value of '$k' has an ", | ||
| 460 | "unsupported type!\n"; | ||
| 461 | next | ||
| 462 | } | ||
| 463 | $self->{$k} = SymObj::clone_ref($i, $j) | ||
| 464 | } | ||
| 465 | |||
| 466 | # Call further init code, if defined | ||
| 467 | if(defined($i = ${"${pkg}::"}{_SymObj_USR_CTOR})){ | ||
| 468 | &$i($self) | ||
| 469 | } | ||
| 470 | $self | ||
| 471 | } # }}} | ||
| 472 | |||
| 473 | sub _ctor_dirtyhier{ # (Stripped version of _dbg) {{{ | ||
| 474 | my ($pkg, $class, $argaref) = @_; | ||
| 475 | |||
| 476 | my ($self, $actorargs, $ctorovers, $tfields, $i, $j, $k) = | ||
| 477 | (undef, ${"${pkg}::"}{_SymObj_ALL_CTOR_ARGS}, | ||
| 478 | ${"${pkg}::"}{_SymObj_CTOR_OVERRIDES}, ${"${pkg}::"}{_SymObj_FIELDS}); | ||
| 479 | |||
| 480 | if(defined ${"${pkg}::"}{ISA}){ | ||
| 481 | foreach $k (@$ctorovers){ | ||
| 482 | for($i = 0, $j = @$argaref; $i < $j; $i += 2){ | ||
| 483 | goto j_OVW if $k eq $$argaref[$i] | ||
| 484 | } | ||
| 485 | push @$argaref, $k; | ||
| 486 | push @$argaref, $tfields->{'_' . $k}; | ||
| 487 | j_OVW:} | ||
| 488 | |||
| 489 | foreach my $c (@{${"${pkg}::"}{ISA}}){ | ||
| 490 | unless(defined ${"${c}::"}{new}){ | ||
| 491 | next | ||
| 492 | } | ||
| 493 | $i = &{${"${c}::"}{new}}($class, @$argaref); | ||
| 494 | |||
| 495 | $self = $i and next unless defined $self; | ||
| 496 | while(($k, $j) = each %$i){ $self->{$k} = $j } | ||
| 497 | } | ||
| 498 | } | ||
| 499 | |||
| 500 | $self = {} unless defined $self; | ||
| 501 | $self = bless $self, $class; | ||
| 502 | |||
| 503 | # Embed arguments | ||
| 504 | $self = SymObj::_ctor_argembed($pkg, $class, $self, $actorargs, $tfields, | ||
| 505 | $argaref); | ||
| 506 | |||
| 507 | # Fill what is not yet filled from arguments.. | ||
| 508 | if(exists ${"${class}::"}{_SymObj_FLAGS}){ | ||
| 509 | $j = ${"${class}::"}{_SymObj_FLAGS} | ||
| 510 | }else{ | ||
| 511 | $j = ${"${pkg}::"}{_SymObj_FLAGS} | ||
| 512 | } | ||
| 513 | $j &= DEEP_CLONE; | ||
| 514 | while(($k, $i) = each %$tfields){ | ||
| 515 | next if exists $self->{$k}; | ||
| 516 | $self->{$k} = SymObj::clone_ref($i, $j) | ||
| 517 | } | ||
| 518 | |||
| 519 | if(defined($i = ${"${pkg}::"}{_SymObj_USR_CTOR})){ | ||
| 520 | &$i($self) | ||
| 521 | } | ||
| 522 | $self | ||
| 523 | } # }}} | ||
| 524 | |||
| 525 | sub _ctor_argembed{ # {{{ | ||
| 526 | my ($pkg, $class, $self, $argsr, $myargsr, $usrr) = @_; | ||
| 527 | my ($isident, $k, $pk, $v, $r) = ($argsr == $myargsr); | ||
| 528 | |||
| 529 | while(@$usrr){ | ||
| 530 | $k = shift @$usrr; | ||
| 531 | $v = shift @$usrr; | ||
| 532 | unless(exists $argsr->{$k}){ | ||
| 533 | next if exists $self->{_UID}; | ||
| 534 | print $MsgFH "${pkg}: $class->new(): unknown argument: '$k'\n"; | ||
| 535 | next | ||
| 536 | } | ||
| 537 | $r = ${$argsr->{$k}}; | ||
| 538 | $pk = '_' . $k; | ||
| 539 | # In non-optimized case: is this an arg of $pkg? | ||
| 540 | next unless $isident || exists $myargsr->{$pk}; | ||
| 541 | |||
| 542 | if(ref $r eq 'ARRAY'){ | ||
| 543 | Symobj::_array_set($pkg, $self, $k, $pk, $v) | ||
| 544 | }elsif(ref $r eq 'HASH'){ | ||
| 545 | unless(ref $v eq 'ARRAY' || ref $v eq 'HASH'){ | ||
| 546 | print $MsgFH "${pkg}: $class->new(): ", | ||
| 547 | "'$k' requires ARRAY or HASH argument\n"; | ||
| 548 | next | ||
| 549 | } | ||
| 550 | Symobj::_hash_set($pkg, $self, $k, $pk, $v) | ||
| 551 | }else{ | ||
| 552 | $self->{$pk} = $v | ||
| 553 | } | ||
| 554 | } | ||
| 555 | $self | ||
| 556 | } # }}} | ||
| 557 | 1; | ||
| 558 | __END__ |