Mercurial > pub > Impl
annotate Lib/IMPL/Object/Abstract.pm @ 251:9f394b27dccf
require can handle recursive module references
author | sergey |
---|---|
date | Fri, 16 Nov 2012 16:44:16 +0400 |
parents | 6b1dda998839 |
children | ad93c9f4dd93 |
rev | line source |
---|---|
49 | 1 package IMPL::Object::Abstract; |
2 use strict; | |
3 use warnings; | |
4 | |
166 | 5 use parent qw(IMPL::Class::Meta); |
49 | 6 |
7 our $MemoryLeakProtection; | |
8 my $Cleanup = 0; | |
9 | |
10 my %cacheCTOR; | |
11 | |
12 my $t = 0; | |
13 sub cache_ctor { | |
14 my $class = shift; | |
15 | |
16 no strict 'refs'; | |
17 my @sequence; | |
18 | |
19 my $refCTORS = *{"${class}::CTOR"}{HASH}; | |
20 | |
21 foreach my $super ( @{"${class}::ISA"} ) { | |
194 | 22 my $superSequence = $cacheCTOR{$super} || cache_ctor($super); |
23 | |
24 my $mapper = $refCTORS ? $refCTORS->{$super} : undef; | |
25 if (ref $mapper eq 'CODE') { | |
26 if ($mapper == *_pass_through_mapper{CODE}) { | |
27 push @sequence,@$superSequence; | |
28 } else { | |
29 push @sequence, sub { | |
30 my $this = shift; | |
31 $this->$_($mapper->(@_)) foreach @$superSequence; | |
32 } if @$superSequence; | |
33 } | |
197
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
194
diff
changeset
|
34 } elsif ($mapper and not ref $mapper and $mapper eq '@_') { |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
194
diff
changeset
|
35 push @sequence,@$superSequence; |
194 | 36 } else { |
37 warn "Unsupported mapper type, in '$class' for the super class '$super'" if $mapper; | |
38 push @sequence, sub { | |
39 my $this = shift; | |
40 $this->$_() foreach @$superSequence; | |
41 } if @$superSequence; | |
42 } | |
49 | 43 } |
44 | |
45 push @sequence, *{"${class}::CTOR"}{CODE} if *{"${class}::CTOR"}{CODE}; | |
46 | |
47 $cacheCTOR{$class} = \@sequence; | |
48 return \@sequence; | |
49 } | |
50 | |
90 | 51 sub dump_ctor { |
194 | 52 my ($self) = @_; |
53 $self = ref $self || $self; | |
54 | |
55 warn "dumping $self .ctor"; | |
56 warn "$_" foreach @{$cacheCTOR{$self}||[]}; | |
90 | 57 } |
58 | |
49 | 59 sub callCTOR { |
60 my $self = shift; | |
61 my $class = ref $self; | |
62 | |
63 $self->$_(@_) foreach @{$cacheCTOR{$class} || cache_ctor($class)}; | |
64 } | |
65 | |
66 sub toString { | |
67 my $self = shift; | |
68 | |
69 return (ref $self || $self); | |
70 } | |
71 | |
122 | 72 sub typeof { |
194 | 73 ref $_[0] || $_[0]; |
93 | 74 } |
75 | |
49 | 76 sub isDisposed { |
77 0; | |
78 } | |
79 | |
80 #sub DESTROY { | |
81 # if ($MemoryLeakProtection and $Cleanup) { | |
82 # my $this = shift; | |
83 # warn sprintf("Object leaks: %s of type %s %s",$this->can('ToString') ? $this->ToString : $this,ref $this,UNIVERSAL::can($this,'_dump') ? $this->_dump : ''); | |
84 # } | |
85 #} | |
86 | |
87 sub END { | |
88 $Cleanup = 1; | |
89 } | |
90 | |
174 | 91 sub _pass_through_mapper { |
49 | 92 @_; |
93 } | |
94 | |
95 sub PassArgs { | |
174 | 96 \&_pass_through_mapper; |
49 | 97 } |
98 | |
99 sub PassThroughArgs { | |
100 my $class = shift; | |
101 $class = ref $class || $class; | |
102 no strict 'refs'; | |
103 no warnings 'once'; | |
174 | 104 ${"${class}::CTOR"}{$_} = \&_pass_through_mapper foreach @{"${class}::ISA"}; |
49 | 105 } |
106 | |
107 package self; | |
108 | |
109 our $AUTOLOAD; | |
110 sub AUTOLOAD { | |
108 | 111 goto &{caller(). substr $AUTOLOAD,6}; |
49 | 112 } |
113 | |
114 package supercall; | |
115 | |
116 our $AUTOLOAD; | |
117 sub AUTOLOAD { | |
118 my $sub; | |
119 my $methodName = substr $AUTOLOAD,11; | |
120 no strict 'refs'; | |
121 $sub = $_->can($methodName) and $sub->(@_) foreach @{caller().'::ISA'}; | |
122 } | |
123 | |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
49
diff
changeset
|
124 1; |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
49
diff
changeset
|
125 |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
49
diff
changeset
|
126 __END__ |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
49
diff
changeset
|
127 |
49 | 128 =pod |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
49
diff
changeset
|
129 =head1 SYNOPSIS |
49 | 130 |
131 package MyBaseObject; | |
166 | 132 use parent qw(IMPL::Object::Abstract); |
49 | 133 |
134 sub new { | |
135 # own implementation of the new opeator | |
136 } | |
137 | |
138 sub surrogate { | |
139 # own implementation of the surrogate operator | |
140 } | |
141 | |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
49
diff
changeset
|
142 =head1 DESCRIPTION |
49 | 143 |
180 | 144 Реализация механизма вызова конструкторов и других вспомогательных вещей, кроме операторов |
145 создания экземпляров. | |
49 | 146 =cut |